#!/usr/bin/python

# This is a part of the external applets for Cairo-Dock
# Copyright : (C) 2010 by Fabounet
# E-mail : fabounet@glx-dock.org
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/licenses/licenses.html#GPL

####################
### dependancies ###
####################
import sys

try:
	import gtk
except:
	from gi.repository import Gtk as gtk

from CDApplet import *
sys.path.append("/usr/lib/cardapio")
dependancies_ok = True
try:
	from Cardapio import *
except:
	dependancies_ok = False

####################
### Applet class ###
####################
class Applet(CDApplet):
	def __init__(self):
		self.cardapiomenu = None
		self.bMenuHasBeenShown = False
		CDApplet.__init__(self)
	
	##### private methods #####
	
	def show_menu(self):
		if (self.cardapiomenu is None):
			dialog_attributes = {
				"message" : _("Cardapio is not installed. Please install it before you run this applet."),
				"time-length" : 6 }
			widget_attributes = {}
			self.icon.PopupDialog (dialog_attributes, widget_attributes)
			return
		props = self.icon.GetAll()
		x = props["x"]
		y = props["y"]
		w = props["width"]
		h = props["height"]
		orientation = props["orientation"]
		right = False
		bottom = False
		icon_x = 0
		icon_y = 0
		window_width, window_height = self.cardapiomenu._view.get_window_size()
		if orientation == CDApplet.BOTTOM or orientation == CDApplet.TOP:  # horizontal dock
			icon_x = x - w/2
			if self.bMenuHasBeenShown and icon_x + window_width >= gtk.gdk.screen_width():  # Cardapio has a very weird window placement, so we do the opposite weird operation.
				icon_x = gtk.gdk.screen_width() - window_width - 1
			if orientation == CDApplet.BOTTOM:
				icon_y = y - h/2
				bottom = True
			else:
				icon_y = y + h/2
		else:  # vertical dock
			if (y < gtk.gdk.screen_height() / 2):
				icon_y = y - h/2
			else:
				icon_y = y + h/2
			if orientation == CDApplet.RIGHT:
				icon_x = x - w/2
				right = True
			elif orientation == CDApplet.LEFT:
				icon_x = x + w/2
		self.bMenuHasBeenShown = True
		self.cardapiomenu.show_hide_near_point(x=icon_x, y=icon_y, force_anchor_right=right, force_anchor_bottom=bottom)  # we could use the Dbus method, but since we already need the menu instance to unset the window decorations (silly), why bother ?
	
	##### applet definition #####
	
	def get_config(self,keyfile):
		self.config['shortkey'] 	= keyfile.get('Configuration', 'shortkey')
	
	def end(self):
		# the following function seems to freeze, so we skip it
		###self.cardapiomenu.save_and_quit()
		pass
	
	def begin(self):
		if (not dependancies_ok):
			return
		self.cardapiomenu = Cardapio(show = Constants.DONT_SHOW)  # launch in the background
		#self.cardapiomenu.window.set_decorated(False)  # no more needed
		
		self.icon.BindShortkey([self.config['shortkey']])
	
	def reload(self):
		self.icon.BindShortkey([self.config['shortkey']])
	
	##### callbacks #####
	
	def on_click(self,iState):
		self.show_menu()
	
	def on_build_menu(self):
		items = [ { "label": _("Edit Menus"),
				"icon" : "gtk-preferences",
				"id"   : 1 } ]
		self.icon.AddMenuItems(items)
		
	def on_menu_select(self,iNumEntry):
		if iNumEntry == 1:
			subprocess.Popen("alacarte")
	
	def on_shortkey(self,key):
		self.show_menu()
	
	
############
### main ###
############
if __name__ == '__main__':
	Applet().run()
