~ubuntu-branches/ubuntu/oneiric/cairo-dock-plug-ins/oneiric

« back to all changes in this revision

Viewing changes to Dbus/interfaces/python/CDApplet.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthieu Baerts (matttbe)
  • Date: 2011-04-20 20:46:51 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20110420204651-ftnpzesj6uc7qeul
Tags: 2.3.0~1-0ubuntu1
* New Upstream Version (LP: #723995)
* Upstream short ChangeLog (since 2.3.0~0rc1):
 - Updated translations
 - Updated the integration of the new versions of kwin and compiz
    (Switcher, ShowDesktop, etc.)
 - Removed a lot of useless g_print
 - Updated a few plug-ins to fit with the new version of the API (gldit)
 - Fixed a few bugs
 - Updated MeMenu, MessagingMenu and Status-Notifier to works
    with the latest version of dbusmenu, etc.
* Switch to dpkg-source 3.0 (quilt) format
* debian/cairo-dock-plug-ins.install:
 - Added new files (interfaces for python, ruby, vala and mono)
* debian/control:
 - Added new dependences for new applets (sensors and zeitgeist)
    and new interfaces (python, valac, ruby and mono)
 - Updated the version of cairo-dock build-dependences
* debian/rules:
 - Added a new CMake flag to install python interface in debian/tmp
* Updated debian/watch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
#
 
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.
 
9
#
 
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
 
15
 
 
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).
 
18
 
 
19
####################
 
20
### dependancies ###
 
21
####################
 
22
import sys
 
23
import os.path
 
24
import ConfigParser
 
25
import gobject
 
26
import glib
 
27
import dbus
 
28
from dbus.mainloop.glib import DBusGMainLoop
 
29
 
 
30
DBusGMainLoop(set_as_default=True)
 
31
 
 
32
####################
 
33
### Applet class ###
 
34
####################
 
35
class CDApplet:
 
36
        
 
37
        BOTTOM = 0
 
38
        TOP    = 1
 
39
        RIGHT  = 2
 
40
        LEFT   = 3
 
41
        
 
42
        DOCK    = 0
 
43
        DESKLET = 1
 
44
        
 
45
        UPPER_LEFT  = 0
 
46
        LOWER_RIGHT = 1
 
47
        LOWER_LEFT  = 2
 
48
        UPPER_RIGHT = 3
 
49
        MIDDLE      = 4
 
50
        
 
51
        MENU_ENTRY        = 0
 
52
        MENU_SUB_MENU     = 1
 
53
        MENU_SEPARATOR    = 2
 
54
        MENU_CHECKBOX     = 3
 
55
        MENU_RADIO_BUTTON = 4
 
56
 
 
57
        MAIN_MENU_ID = 0
 
58
        
 
59
        DIALOG_KEY_ENTER = -1
 
60
        DIALOG_KEY_ESCAPE = -2
 
61
        
 
62
        def __init__(self):
 
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)
 
70
                 """
 
71
                self.icon = None
 
72
                self.sub_icons = None
 
73
                self.config = {}
 
74
                self.loop = None
 
75
                self._bEnded = False
 
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]
 
81
                
 
82
                self._get_config()
 
83
                self._connect_to_dock()
 
84
        
 
85
        def run(self):
 
86
                """ start the applet and enter the main loop; we never get out of this function """
 
87
                self.begin()
 
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()
 
90
                        self.loop.run()
 
91
                print ">>> applet '"+self.cAppletName+"' terminated."
 
92
                sys.exit(0)
 
93
        
 
94
        ##################################
 
95
        ### callbacks on the main icon ###
 
96
        ##################################
 
97
        def on_click(self,iState):
 
98
                """ action on click """
 
99
                pass
 
100
        
 
101
        def on_middle_click(self):
 
102
                """ action on middle-click """
 
103
                pass
 
104
        
 
105
        def _on_build_menu(self):
 
106
                self._cMenuIconId = None
 
107
                self.on_build_menu()
 
108
                pass
 
109
                
 
110
        def on_build_menu(self):
 
111
                """ build our menu """
 
112
                pass
 
113
        
 
114
        def _on_menu_select(self,iNumEntry):
 
115
                if self._cMenuIconId == None:
 
116
                        self.on_menu_select(iNumEntry)
 
117
                else:
 
118
                        self.on_menu_select_sub_icon(iNumEntry,self._cMenuIconId)
 
119
        
 
120
        def on_menu_select(self,iNumEntry):
 
121
                """ action on selecting an entry of our menu """
 
122
                pass
 
123
        
 
124
        def on_scroll(self,bScrollUp):
 
125
                """ action on scroll """
 
126
                pass
 
127
        
 
128
        def on_drop_data(self,cReceivedData):
 
129
                """ action on dropping something on our applet """
 
130
        
 
131
        def on_answer(self,answer):
 
132
                """ action on answering ok to a dialog with ok/cancel buttons (deprecated) """
 
133
                pass
 
134
        
 
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)
 
139
        
 
140
        def on_shortkey(self,cKey):
 
141
                """ action on pressing one of the shortkeys we bound beforehand """
 
142
                pass
 
143
        
 
144
        def on_change_focus(self,bIsActive):
 
145
                """ action when the window controlled by the applet takes or looses the focus """
 
146
                pass
 
147
        
 
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"""
 
153
                pass
 
154
        
 
155
        def on_middle_click_sub_icon(self, cIconID):
 
156
                """ action on middle-click on one of our sub-icons"""
 
157
                pass
 
158
        
 
159
        def on_scroll_sub_icon(self, bScrollUp, cIconID):
 
160
                """ action on scroll on one of our sub-icons"""
 
161
                pass
 
162
        
 
163
        def _on_build_menu_sub_icon(self, cIconID):
 
164
                self._cMenuIconId = cIconID
 
165
                self.on_build_menu_sub_icon(cIconID)
 
166
        
 
167
        def on_build_menu_sub_icon(self, cIconID):
 
168
                """ build our menu on one of our sub-icons"""
 
169
                pass
 
170
        
 
171
        def on_menu_select_sub_icon(self, iNumEntry, cIconID):
 
172
                """ action on selecting an entry of our menu on a sub-icon"""
 
173
                pass
 
174
        
 
175
        def on_drop_data_sub_icon(self, cReceivedData, cIconID):
 
176
                """ action on dropping something on one of our sub-icons"""
 
177
                pass
 
178
        
 
179
        def on_answer_dialog_sub_icon(self, button, answer, cIconID):
 
180
                """ action on answering a dialog about a sub-icon"""
 
181
                pass
 
182
        
 
183
        ###############################
 
184
        ### callbacks on the applet ###
 
185
        ###############################
 
186
        def begin(self):
 
187
                """ action when the applet is started """
 
188
                pass
 
189
        
 
190
        def end(self):
 
191
                """ action when the applet is terminated """
 
192
                pass
 
193
        
 
194
        def _on_stop(self):
 
195
                self._bEnded = True
 
196
                self.end()
 
197
                if self.loop != None:
 
198
                        self.loop.quit()
 
199
        
 
200
        def reload(self):
 
201
                """ called when our applet is reloaded (config parameters have changed) """
 
202
                pass
 
203
        
 
204
        def _on_reload(self,bConfigHasChanged):
 
205
                if bConfigHasChanged:
 
206
                        self._get_config()
 
207
                        self.reload()
 
208
        
 
209
        def get_config(self,keyfile):
 
210
                """ get our parameters from the key-file """
 
211
                pass
 
212
        
 
213
        def _get_config(self):
 
214
                keyfile = ConfigParser.RawConfigParser()
 
215
                keyfile.read(self.cConfFile)
 
216
                self.get_config(keyfile)
 
217
        
 
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()
 
222
                try:
 
223
                        applet_object = bus.get_object("org.cairodock.CairoDock", self.cBusPath)
 
224
                except:
 
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"
 
226
                        sys.exit(2)
 
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)