#!/usr/bin/python

# This is a part of the external demo applet for Cairo-Dock
# Copyright : (C) 2010-2011 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

# The name of this applet is "demo_python"; it is placed in a folder named "demo_python", with a file named "auto-load.conf" which describes it.
# Copy this folder into ~/.config/cairo-dock/third-party to let the dock register it automatically.
# In the folder we have :
# - "demo_python"       : the executable script, without extension
# - "demo_python.conf"  : the default config file
# - "auto-load.conf"    : the file describing our applet
# - "icon"              : the default icon of the applet (optionnal)
# - "preview"           : a preview of this applet (optionnal)

### This very simple applet features a counter from 0 to iMaxValue It displays the counter on the icon with a gauge and a quick info.
### Scroll on the icon increase or decrease the counter.
### The menu offers the possibility to set some default value.
### Left-click on the icon will set a random value.
### Middle-click on the icon will raise a dialog asking you to set the value you want.
### If you drop some text on the icon, it will be used as the icon's label.

####################
### dependancies ###
####################
import random
from CDApplet import CDApplet

####################
### Applet class ###
####################
class Applet(CDApplet):
	def __init__(self):
		# define internal variables
		self.count = 0
		# call high-level init
		CDApplet.__init__(self)
	
	##### private methods #####
	
	def set_counter(self,count):
		self.count = count
		percent = float(self.count)/self.config['iMaxValue']
		self.icon.RenderValues([percent])
		self.icon.SetQuickInfo(format(self.count, "d"))
	
	##### applet definition #####
	
	def get_config(self,keyfile):
		print "*** get config"
		self.config['cTheme'] 		= keyfile.get('Configuration', 'theme')
		self.config['iMaxValue'] 	= keyfile.getint('Configuration', 'max value')
		self.config['yesno'] 		= keyfile.getboolean('Configuration', 'yesno')
	
	def end(self):
		print "*** end of demo_python"
	
	def begin(self):
		print "*** begin"
		self.icon.ShowDialog("I'm connected to Cairo-Dock !", 4)  # show a dialog with this message for 4 seconds.
		self.icon.AddDataRenderer("gauge", 1, self.config['cTheme'])  # set a gauge with the theme read in config to display the value of the counter.
		self.set_counter(0)  # set the initial value of our counter.
		self.sub_icons.AddSubIcons(["icon 1", "firefox-3.0", "id1", "icon 2", "trash", "id2", "icon 3", "thunderbird", "id3", "icon 4", "nautilus", "id4"])  # add 4 icons in our sub-dock. The tab contains triplets of {label, image, ID}.
		self.sub_icons.RemoveSubIcon("id2")  # remove the 2nd icon of our sub-dock.
		self.sub_icons.SetQuickInfo("1", "id1")  # write the ID on each icon of the sub-dock.
		self.sub_icons.SetQuickInfo("3", "id3")
		self.sub_icons.SetQuickInfo("4", "id4")
		self.icon.BindShortkey(["<Control>F9"])  # bind to ctrl+F9
	
	def reload(self):
		print "*** reload"
		self.icon.AddDataRenderer("gauge", 1, self.config['cTheme'])
		self.icon.RenderValues([float(self.count)/self.config['iMaxValue']])
		self.sub_icons.RemoveSubIcon("any")
		self.sub_icons.AddSubIcons(["icon 1", "firefox-3.0", "id1", "icon 2", "natilus", "id2", "icon 3", "thunderbird", "id3"])
	
	##### callbacks #####
	
	def on_click(self,iState):
		print "*** click"
		self.set_counter(random.randint(0,self.config['iMaxValue']))
	
	def on_middle_click(self):
		print "*** middle click"
		dialog_attributes = {
			"icon" : "stock_properties",
			"message" : "Set the value you want",
			"buttons" : "ok;cancel"}
		widget_attributes = {
			"widget-type" : "scale",
			"max-value" : self.config['iMaxValue'],
			"message" : "Set the value you want"}
		
		self.icon.PopupDialog(dialog_attributes, widget_attributes)
	
	def on_build_menu(self):
		print "*** build menu"
		items = [ {
				"label": "set min value",
				"icon" : "gtk-zoom-out",
				"id"   : 1
			}, {
				"label": "set medium value",
				"icon" : "gtk-zoom-fit",
				"id"   : 2 
			}, {
				"label": "set max value",
				"icon" : "gtk-zoom-in",
				"id"   : 3
			} ]
		self.icon.AddMenuItems(items)
		
	def on_menu_select(self,iNumEntry):
		print "*** choice",iNumEntry,"has been selected !"
		if iNumEntry == 1:
			self.set_counter(0)
		elif iNumEntry == 2:
			self.set_counter(self.config['iMaxValue']/2)
		elif iNumEntry == 3:
			self.set_counter(self.config['iMaxValue'])
	
	def on_scroll(self,bScrollUp):
		print "*** scroll !"
		if bScrollUp:
			count = min(self.config['iMaxValue'], self.count+1)
		else:
			count = max(0, self.count-1)
		self.set_counter(count)
	
	def on_drop_data(self,cReceivedData):
		print "*** received",cReceivedData
		self.icon.SetLabel(cReceivedData)
	
	def on_answer_dialog(self,button, answer):
		print "*** answer dialog :",button, answer
		self.set_counter(int (answer))
	
	def on_shortkey(self,key):
		print "*** shortkey :",key
	
	
############
### main ###
############
if __name__ == '__main__':
	Applet().run()
