Added logging capability if /tmp/drlaunch.log is present.
[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
59 # Restore path
60 sys.path=orig_path
61
62 # IconGrid must be before HomePluginItem for its connect()
63 # and do_button_*() to override those of HomePluginItem
64 class DrlaunchPlugin(IconGrid, HomePluginItem, FremantleRotation):
65     def __init__(self):
66         IconGrid.__init__(self)
67         HomePluginItem.__init__(self)
68         FremantleRotation.__init__(self, 'DrlaunchPlugin',
69             mode=FremantleRotation.AUTOMATIC, dontrotate=True)
70
71         self.winConfig=None
72
73         self.set_settings(True)
74
75         self.id=None
76         self.config=None
77
78     def get_id0(self):
79         """If this is called from the constructor then the program
80         core dumps """
81         aid=self.get_applet_id()
82
83         # Get desktop activity if D.A.M. is present
84         
85         act="/usr/bin/activity"
86
87         if os.path.exists(act):
88             r=Popen([act, "current"], stdout=PIPE).communicate()
89             activity=r[0].strip()
90         else:
91             activity=""
92
93         ret="%s-%s" % (aid, activity)
94
95         return(ret)
96
97     def get_id(self):
98         if self.id==None:
99             self.id=self.get_id0()
100
101         return(self.id)
102
103     def get_config(self):
104         if self.config==None:
105             id=self.get_id()
106             self.config=config.Config(id)
107
108         return(self.config)
109
110     def do_realize(self):
111         launcher.init()
112         config=self.get_config()
113         config.load()
114
115         IconGrid.do_realize(self, config)
116
117         self.setSize(config.getSize())
118         self.reloadIcons()
119
120         screen=self.get_screen()
121         self.set_colormap(screen.get_rgba_colormap())
122         self.set_app_paintable(True)
123
124         self.connect('show-settings', self.slot_show_settings)
125         self.connect('long-press', self.signalLongpress)
126         self.connect('click', self.signalClick)
127         self.connect('notify', self.signalNotify)
128
129         HomePluginItem.do_realize(self)
130
131     def on_orientation_changed(self, orientation):
132         # Get the first character of the string (l/p)
133         o=orientation[0]
134         self.setMode(o)
135 #       self.queue_draw()
136
137     def do_expose_event(self, event):
138         IconGrid.do_expose_event(self, event)
139         HomePluginItem.do_expose_event(self, event)
140
141     def slot_show_settings(self, dt):
142         if self.winConfig!=None:
143             # Doesn't work
144             # self.winConfig.show_all()
145             return
146
147         s=WinConfig(self.get_config())
148         s.show_all()
149         s.connect('destroy', self.slotConfigDestroy)
150         self.winConfig=s
151
152     def slotConfigDestroy(self, sender):
153         self.winConfig=None
154
155         dt=sender.getData()
156
157         config=self.get_config()
158
159         config.setSize(dt['size'])
160         config.setApps(dt['apps'])
161         config.setIndiv(dt['indiv'])
162         config.setLongpress(dt['longpress'])
163         config.setAnimate(dt['animate'])
164         config.setNoBg(dt['nobg'])
165         config.save()
166         
167         # Resize widget
168         self.setSize(dt['size'])
169         self.reloadIcons()
170
171         # Free memory that is used for animations if animations are disabled
172         if not dt['animate']:
173             self.clearAnimationCache()
174
175         self.queue_draw()
176
177     def handle_click(self, sender, icon):
178         """ common handler for longpress and click """
179         if icon.name!=None and icon.name!='':
180             print "name:", icon.name
181             launcher.launch(icon.name)
182
183     def signalLongpress(self, sender, icon):
184         self.handle_click(sender, icon)
185
186     def signalClick(self, sender, icon):
187         config=self.get_config()
188
189         if not config.getLongpress():
190             self.handle_click(sender, icon)
191
192     def signalNotify(self, sender, property):
193         if property.name=='is-on-current-desktop':
194             v=self.get_property(property.name)
195             if v and self.draw_pending:
196                 self.queue_draw()
197
198     def resize2(self):
199         config=self.get_config()
200
201         w=(self.size[0] * config.iconsize) + \
202             (self.size[0] * config.iconspace)
203         h=(self.size[1] * config.iconsize) + \
204             (self.size[1] * config.iconspace)
205         self.set_size_request(w, h)
206         self.resize(w, h)
207
208     def setSize(self, size):
209         IconGrid.setSize(self, size)
210         self.resize2()
211
212 hd_plugin_type = DrlaunchPlugin
213
214 if __name__=="__main__":
215     gobject.type_register(hd_plugin_type)
216     obj=gobject.new(hd_plugin_type, plugin_id="plugin_id")
217     obj.show_all()
218     gtk.main()
219
220
221
222 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
223