move drlaunch in drlaunch
[drlaunch] / drlaunch / src / widget.py
1 #!/usr/bin/env python
2 # coding=UTF-8
3
4 # Copyright (C) 2010 Stefanos Harhalakis
5 #
6 # This file is part of wifieye.
7 #
8 # wifieye is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # wifieye is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with wifieye.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
22
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
24
25 # HACK
26 # Add the current module's directory to sys.path to bypass
27 # problems when running as widget.
28 # Restore the path at the end of the imports
29 import sys
30 import os
31
32 orig_path=sys.path[:]
33 tmp_path=os.path.dirname( os.path.realpath( __file__ ) )
34 sys.path.append(tmp_path)
35
36 # End of hack
37
38 import gtk
39 import gobject
40 import hildon
41 from hildondesktop import *
42 from gtk import gdk
43 from math import pi
44 import cairo
45 import gconf
46 import time
47
48 from subprocess import Popen,PIPE
49
50 from portrait import FremantleRotation
51 import launcher
52 from xdg.IconTheme import getIconPath
53 from win_config import WinConfig
54
55 import config
56 import apps
57 from icon import Icon
58 from icongrid import IconGrid
59 from sig import Disconnector
60
61 # Restore path
62 sys.path=orig_path
63
64 # IconGrid must be before HomePluginItem for its connect()
65 # and do_button_*() to override those of HomePluginItem
66 class DrlaunchPlugin(IconGrid, HomePluginItem, FremantleRotation, Disconnector):
67     def __init__(self):
68         IconGrid.__init__(self)
69         HomePluginItem.__init__(self)
70         FremantleRotation.__init__(self, 'DrlaunchPlugin',
71             mode=FremantleRotation.AUTOMATIC, dontrotate=True)
72         Disconnector.__init__(self)
73
74         self.winConfig=None
75
76         self.gconf=gconf.client_get_default()
77
78         self.set_settings(True)
79
80         self.id=None
81         self.config=None
82
83         self.reset_mode()
84
85     def get_id0(self):
86         """If this is called from the constructor then the program
87         core dumps """
88         aid=self.get_applet_id()
89
90         # Get desktop activity if D.A.M. is present
91         
92         act="/usr/bin/activity"
93
94         if os.path.exists(act):
95             r=Popen([act, "current"], stdout=PIPE).communicate()
96             activity=r[0].strip()
97         else:
98             activity=""
99
100         ret="%s-%s" % (aid, activity)
101
102         return(ret)
103
104     def get_id(self):
105         if self.id==None:
106             self.id=self.get_id0()
107
108         return(self.id)
109
110     def get_config(self):
111         if self.config==None:
112             id=self.get_id()
113             self.config=config.Config(id)
114
115         return(self.config)
116
117     def get_desktop_orientation(self):
118         """
119         Return desktop orientation
120
121         NOTE: This is the desktop orientation as it was introduced in CSSU.
122         Not the device orientation.
123
124         @return "portrait" or "landscape"
125         """
126
127         sw=gdk.screen_width()
128         sh=gdk.screen_height()
129
130         if sw>=sh:
131             ret='landscape'
132         else:
133             ret='portrait'
134
135         return(ret)
136
137     def is_rotating_desktop(self):
138         """
139         Check whether the desktop will change to portrait mode, as
140         added in CSSU.
141
142         @return True/False
143         """
144
145         c=self.gconf
146
147         # This returns False if the key doesn't exist
148         ret=c.get_bool('/apps/osso/hildon-desktop/ui_can_rotate')
149
150         return(ret)
151
152     def do_realize(self):
153         launcher.init()
154         config=self.get_config()
155         config.load()
156
157         IconGrid.do_realize(self, config)
158
159         self.setSize(config.getSize())
160         self.reloadIcons()
161
162         screen=self.get_screen()
163         self.set_colormap(screen.get_rgba_colormap())
164         self.set_app_paintable(True)
165
166         self.c(self, 'show-settings', self.slot_show_settings)
167         self.c(self, 'long-press', self.signalLongpress)
168         self.c(self, 'click', self.signalClick)
169         self.c(self, 'notify', self.signalNotify)
170
171         HomePluginItem.do_realize(self)
172
173     def on_orientation_changed(self, orientation):
174         # Avoid bugs
175         if orientation==None or len(orientation)==0:
176             return
177
178         # Get the first character of the string (l/p)
179         o=orientation[0]
180
181         # Get desktop orientation
182         #do=self.get_desktop_orientation()
183
184         # Is desktop rotation (per CSSU) enabled?
185         rd=self.is_rotating_desktop()
186
187         #print "desktop: %s / %s, device: %s" % (do, rd, o)
188
189         # In case of a rotating desktop, force orientation to be
190         # 'landscape'
191         if rd:
192             o='l'
193
194         self.setMode(o)
195 #       self.queue_draw()
196
197     def do_expose_event(self, event):
198         IconGrid.do_expose_event(self, event)
199         HomePluginItem.do_expose_event(self, event)
200         self.reset_mode()
201
202     def slot_show_settings(self, dt):
203         if self.winConfig!=None:
204             # Doesn't work
205             # self.winConfig.show_all()
206             return
207
208         s=WinConfig(self.get_config())
209         s.show_all()
210         #s.c(s, 'delete-event', self.slotConfigDestroy)
211         self.c(s, 'delete-event', self.slotConfigDestroy)
212         #s.connect('destroy', self.slotConfigDestroy)
213         self.winConfig=s
214
215     def slotConfigDestroy(self, sender, event):
216 #       print "Sender:", sender
217         dt=sender.getData()
218
219         # Disconnect signals for that object in order to be deleted
220         self.dis_finish(self.winConfig)
221         #self.winConfig.finish()
222         #self.winConfig.destroy()
223
224         self.winConfig=None
225
226         cfg=self.get_config()
227
228         cfg.setSize(dt['size'])
229         cfg.setApps(dt['apps'])
230         cfg.setIndiv(dt['indiv'])
231         cfg.setLongpress(dt['longpress'])
232         cfg.setAnimate(dt['animate'])
233         cfg.setNoBg(dt['nobg'])
234         cfg.setThemeBg(dt['themebg'])
235         cfg.setIconSize(dt['iconsize'])
236         cfg.setIconPadding(dt['iconpadding'])
237         cfg.setIconMargin(dt['iconmargin'])
238         cfg.save()
239         
240         # Resize widget
241         self.icons.resizeMax()
242         self.setSize(dt['size'])
243         self.reloadIcons()
244
245         # Free memory that is used for animations if animations are disabled
246         if not dt['animate']:
247             self.clearAnimationCache()
248
249         # Free memory of backgrounds in case they changed
250         self.clearBgCache()
251
252         self.queue_draw()
253
254 #       print "slot-config-destroy-end"
255
256         return(False)
257
258     def handle_click(self, sender, icon):
259         """ common handler for longpress and click """
260         if icon.appname!=None and icon.appname!='':
261             launcher.launch(icon.appname)
262
263     def signalLongpress(self, sender, icon):
264         self.handle_click(sender, icon)
265
266     def signalClick(self, sender, icon):
267         config=self.get_config()
268
269         if not config.getLongpress():
270             self.handle_click(sender, icon)
271
272     def signalNotify(self, sender, property):
273         if property.name=='is-on-current-desktop':
274             v=self.get_property(property.name)
275             if v and self.draw_pending:
276                 self.queue_draw()
277
278     def resize2(self):
279         config=self.get_config()
280
281         w=(self.size[0] * config.iconsize) + \
282             (self.size[0] * config.getIconSpace())
283         h=(self.size[1] * config.iconsize) + \
284             (self.size[1] * config.getIconSpace())
285         self.set_size_request(w, h)
286         self.resize(w, h)
287
288     def setSize(self, size):
289         IconGrid.setSize(self, size)
290         self.resize2()
291
292 hd_plugin_type = DrlaunchPlugin
293
294 if __name__=="__main__":
295     gobject.type_register(hd_plugin_type)
296     obj=gobject.new(hd_plugin_type, plugin_id="plugin_id")
297     obj.show_all()
298     gtk.main()
299
300
301
302 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
303