1
# This is a part of the external demo applet for Cairo-Dock
2
# Copyright : (C) 2010-2011 by Fabounet
3
# E-mail : fabounet@glx-dock.org
5
# This program is free software; you can redistribute it and/or
6
# modify it under the terms of the GNU General Public License
7
# as published by the Free Software Foundation; either version 2
8
# of the License, or (at your option) any later version.
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
# http://www.gnu.org/licenses/licenses.html#GPL
16
# Base class for Cairo-Dock's applets.
17
# Make your own class derive from a CDApplet, and override the functions you need (the ones which don't start with an underscore).
28
from dbus.mainloop.glib import DBusGMainLoop
30
DBusGMainLoop(set_as_default=True)
60
DIALOG_KEY_ESCAPE = -2
63
""" initialize the applet. Must be called by any class that inheritates from it.
64
It defines the following:
65
- icon : our main icon
66
- sub_icons: our sub-icons
67
- config : a dictionnary where our configuration parameters are stored
68
- cAppletName : name of our applet (the same as the folder where files are stored)
69
- cConfFile : path to our config file (you will rarely need it)
76
self._cMenuIconId = None
77
self.cAppletName = sys.argv[0][2:]
78
self.cBusPath = sys.argv[2]
79
self.cConfFile = sys.argv[3]
80
self.cParentAppName = sys.argv[4]
83
self._connect_to_dock()
86
""" start the applet and enter the main loop; we never get out of this function """
88
if not self._bEnded: # _bEnded can be true if the applet runs its own main loop, in which case we stay stuck in the 'begin' function until the end of the applet; in this case, we don't want to run a main loop again!
89
self.loop = gobject.MainLoop()
91
print ">>> applet '"+self.cAppletName+"' terminated."
94
##################################
95
### callbacks on the main icon ###
96
##################################
97
def on_click(self,iState):
98
""" action on click """
101
def on_middle_click(self):
102
""" action on middle-click """
105
def _on_build_menu(self):
106
self._cMenuIconId = None
110
def on_build_menu(self):
111
""" build our menu """
114
def _on_menu_select(self,iNumEntry):
115
if self._cMenuIconId == None:
116
self.on_menu_select(iNumEntry)
118
self.on_menu_select_sub_icon(iNumEntry,self._cMenuIconId)
120
def on_menu_select(self,iNumEntry):
121
""" action on selecting an entry of our menu """
124
def on_scroll(self,bScrollUp):
125
""" action on scroll """
128
def on_drop_data(self,cReceivedData):
129
""" action on dropping something on our applet """
131
def on_answer(self,answer):
132
""" action on answering ok to a dialog with ok/cancel buttons (deprecated) """
135
def on_answer_dialog(self, button, answer):
136
""" action on answering a dialog """
137
if button == -1 or button == 0:
138
self.on_answer(answer)
140
def on_shortkey(self,cKey):
141
""" action on pressing one of the shortkeys we bound beforehand """
144
def on_change_focus(self,bIsActive):
145
""" action when the window controlled by the applet takes or looses the focus """
148
##################################
149
### callbacks on the sub-icons ###
150
##################################
151
def on_click_sub_icon(self, iState, cIconID):
152
""" action on click on one of our sub-icons"""
155
def on_middle_click_sub_icon(self, cIconID):
156
""" action on middle-click on one of our sub-icons"""
159
def on_scroll_sub_icon(self, bScrollUp, cIconID):
160
""" action on scroll on one of our sub-icons"""
163
def _on_build_menu_sub_icon(self, cIconID):
164
self._cMenuIconId = cIconID
165
self.on_build_menu_sub_icon(cIconID)
167
def on_build_menu_sub_icon(self, cIconID):
168
""" build our menu on one of our sub-icons"""
171
def on_menu_select_sub_icon(self, iNumEntry, cIconID):
172
""" action on selecting an entry of our menu on a sub-icon"""
175
def on_drop_data_sub_icon(self, cReceivedData, cIconID):
176
""" action on dropping something on one of our sub-icons"""
179
def on_answer_dialog_sub_icon(self, button, answer, cIconID):
180
""" action on answering a dialog about a sub-icon"""
183
###############################
184
### callbacks on the applet ###
185
###############################
187
""" action when the applet is started """
191
""" action when the applet is terminated """
197
if self.loop != None:
201
""" called when our applet is reloaded (config parameters have changed) """
204
def _on_reload(self,bConfigHasChanged):
205
if bConfigHasChanged:
209
def get_config(self,keyfile):
210
""" get our parameters from the key-file """
213
def _get_config(self):
214
keyfile = ConfigParser.RawConfigParser()
215
keyfile.read(self.cConfFile)
216
self.get_config(keyfile)
218
def _connect_to_dock(self):
219
# get our applet on the bus.
220
#~ applet_path = "/org/cairodock/CairoDock/"+self.cAppletName # path where our object is stored on the bus.
221
bus = dbus.SessionBus()
223
applet_object = bus.get_object("org.cairodock.CairoDock", self.cBusPath)
225
print ">>> object '"+self.cBusPath+"' can't be found on the bus, exit.\nMake sure that the 'Dbus' plug-in is activated in Cairo-Dock"
227
self.icon = dbus.Interface(applet_object, "org.cairodock.CairoDock.applet") # this object represents our icon inside the dock or a desklet.
228
sub_icons_object = bus.get_object("org.cairodock.CairoDock", self.cBusPath+"/sub_icons")
229
self.sub_icons = dbus.Interface(sub_icons_object, "org.cairodock.CairoDock.subapplet") # this object represents the list of icons contained in our sub-dock, or in our desklet. We'll add them one by one later, giving them a unique ID, which will be used to identify each of them.
230
# connect to signals.
231
self.icon.connect_to_signal("on_click", self.on_click) # when the user left-clicks on our icon.
232
self.icon.connect_to_signal("on_middle_click", self.on_middle_click) # when the user middle-clicks on our icon.
233
self.icon.connect_to_signal("on_build_menu", self._on_build_menu) # when the user right-clicks on our applet (which builds the menu)
234
self.icon.connect_to_signal("on_menu_select", self._on_menu_select) # when the user selects an entry of this menu.
235
self.icon.connect_to_signal("on_scroll", self.on_scroll) # when the user scroll up or down on our icon.
236
self.icon.connect_to_signal("on_drop_data", self.on_drop_data) # when the user drops something on our icon.
237
self.icon.connect_to_signal("on_answer_dialog", self.on_answer_dialog) # when the user answer a question.
238
self.icon.connect_to_signal("on_shortkey", self.on_shortkey) # when the user press the shortkey.
239
self.icon.connect_to_signal("on_change_focus", self.on_change_focus) # when the window's focus changes.
240
self.icon.connect_to_signal("on_stop_module", self._on_stop) # when the user deactivate our applet (or the DBus plug-in, or when the Cairo-Dock is stopped).
241
self.icon.connect_to_signal("on_reload_module", self._on_reload) # when the user changes something in our config, or when the desklet is resized (with no change in the config).
242
self.sub_icons.connect_to_signal("on_click_sub_icon", self.on_click_sub_icon) # when the user left-clicks on a sub-icon.
243
self.sub_icons.connect_to_signal("on_middle_click_sub_icon", self.on_middle_click_sub_icon)
244
self.sub_icons.connect_to_signal("on_scroll_sub_icon", self.on_scroll_sub_icon)
245
self.sub_icons.connect_to_signal("on_build_menu_sub_icon", self._on_build_menu_sub_icon)
246
self.sub_icons.connect_to_signal("on_drop_data_sub_icon", self.on_drop_data_sub_icon)