It works!
[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 time
46
47 from subprocess import Popen,PIPE
48
49 from portrait import FremantleRotation
50 import launcher
51 from xdg.IconTheme import getIconPath
52 from win_config import WinConfig
53
54 import config
55 import apps
56 from icon import Icon
57 from icongrid import IconGrid
58 from sig import Disconnector
59
60 # Restore path
61 sys.path=orig_path
62
63 # IconGrid must be before HomePluginItem for its connect()
64 # and do_button_*() to override those of HomePluginItem
65 class DrlaunchPlugin(IconGrid, HomePluginItem, FremantleRotation, Disconnector):
66     def __init__(self):
67         IconGrid.__init__(self)
68         HomePluginItem.__init__(self)
69         FremantleRotation.__init__(self, 'DrlaunchPlugin',
70             mode=FremantleRotation.AUTOMATIC, dontrotate=True)
71         Disconnector.__init__(self)
72
73         self.winConfig=None
74
75         self.set_settings(True)
76
77         self.id=None
78         self.config=None
79
80         self.reset_mode()
81
82     def get_id0(self):
83         """If this is called from the constructor then the program
84         core dumps """
85         aid=self.get_applet_id()
86
87         # Get desktop activity if D.A.M. is present
88         
89         act="/usr/bin/activity"
90
91         if os.path.exists(act):
92             r=Popen([act, "current"], stdout=PIPE).communicate()
93             activity=r[0].strip()
94         else:
95             activity=""
96
97         ret="%s-%s" % (aid, activity)
98
99         return(ret)
100
101     def get_id(self):
102         if self.id==None:
103             self.id=self.get_id0()
104
105         return(self.id)
106
107     def get_config(self):
108         if self.config==None:
109             id=self.get_id()
110             self.config=config.Config(id)
111
112         return(self.config)
113
114     def do_realize(self):
115         launcher.init()
116         config=self.get_config()
117         config.load()
118
119         IconGrid.do_realize(self, config)
120
121         self.setSize(config.getSize())
122         self.reloadIcons()
123
124         screen=self.get_screen()
125         self.set_colormap(screen.get_rgba_colormap())
126         self.set_app_paintable(True)
127
128         self.c(self, 'show-settings', self.slot_show_settings)
129         self.c(self, 'long-press', self.signalLongpress)
130         self.c(self, 'click', self.signalClick)
131         self.c(self, 'notify', self.signalNotify)
132
133         HomePluginItem.do_realize(self)
134
135     def on_orientation_changed(self, orientation):
136         # Avoid bugs
137         if orientation==None or len(orientation)==0:
138             return
139
140         # Get the first character of the string (l/p)
141         o=orientation[0]
142         self.setMode(o)
143 #       self.queue_draw()
144
145     def do_expose_event(self, event):
146         IconGrid.do_expose_event(self, event)
147         HomePluginItem.do_expose_event(self, event)
148         self.reset_mode()
149
150     def slot_show_settings(self, dt):
151         if self.winConfig!=None:
152             # Doesn't work
153             # self.winConfig.show_all()
154             return
155
156         s=WinConfig(self.get_config())
157         s.show_all()
158         #s.c(s, 'delete-event', self.slotConfigDestroy)
159         self.c(s, 'delete-event', self.slotConfigDestroy)
160         #s.connect('destroy', self.slotConfigDestroy)
161         self.winConfig=s
162
163     def slotConfigDestroy(self, sender, event):
164 #       print "Sender:", sender
165         dt=sender.getData()
166
167         # Disconnect signals for that object in order to be deleted
168         self.dis_finish(self.winConfig)
169         #self.winConfig.finish()
170         #self.winConfig.destroy()
171
172         self.winConfig=None
173
174         cfg=self.get_config()
175
176         cfg.setSize(dt['size'])
177         cfg.setApps(dt['apps'])
178         cfg.setIndiv(dt['indiv'])
179         cfg.setLongpress(dt['longpress'])
180         cfg.setAnimate(dt['animate'])
181         cfg.setNoBg(dt['nobg'])
182         cfg.setThemeBg(dt['themebg'])
183         cfg.setIconSize(dt['iconsize'])
184         cfg.setIconPadding(dt['iconpadding'])
185         cfg.setIconMargin(dt['iconmargin'])
186         cfg.save()
187         
188         # Resize widget
189         self.setSize(dt['size'])
190         self.reloadIcons()
191         self.icons.resizeMax()
192
193         # Free memory that is used for animations if animations are disabled
194         if not dt['animate']:
195             self.clearAnimationCache()
196
197         # Free memory of backgrounds in case they changed
198         self.clearBgCache()
199
200         self.queue_draw()
201
202 #       print "slot-config-destroy-end"
203
204         return(False)
205
206     def handle_click(self, sender, icon):
207         """ common handler for longpress and click """
208         if icon.appname!=None and icon.appname!='':
209             launcher.launch(icon.appname)
210
211     def signalLongpress(self, sender, icon):
212         self.handle_click(sender, icon)
213
214     def signalClick(self, sender, icon):
215         config=self.get_config()
216
217         if not config.getLongpress():
218             self.handle_click(sender, icon)
219
220     def signalNotify(self, sender, property):
221         if property.name=='is-on-current-desktop':
222             v=self.get_property(property.name)
223             if v and self.draw_pending:
224                 self.queue_draw()
225
226     def resize2(self):
227         config=self.get_config()
228
229         w=(self.size[0] * config.iconsize) + \
230             (self.size[0] * config.getIconSpace())
231         h=(self.size[1] * config.iconsize) + \
232             (self.size[1] * config.getIconSpace())
233         self.set_size_request(w, h)
234         self.resize(w, h)
235
236     def setSize(self, size):
237         IconGrid.setSize(self, size)
238         self.resize2()
239
240 hd_plugin_type = DrlaunchPlugin
241
242 if __name__=="__main__":
243     gobject.type_register(hd_plugin_type)
244     obj=gobject.new(hd_plugin_type, plugin_id="plugin_id")
245     obj.show_all()
246     gtk.main()
247
248
249
250 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
251