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