11e4cf9d4c57d4936bf689413339ea05ce88e6ce
[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         # Avoid bugs
133         if orientation==None or len(orientation)==0:
134             return
135
136         # Get the first character of the string (l/p)
137         o=orientation[0]
138         self.setMode(o)
139 #       self.queue_draw()
140
141     def do_expose_event(self, event):
142         IconGrid.do_expose_event(self, event)
143         HomePluginItem.do_expose_event(self, event)
144
145     def slot_show_settings(self, dt):
146         if self.winConfig!=None:
147             # Doesn't work
148             # self.winConfig.show_all()
149             return
150
151         s=WinConfig(self.get_config())
152         s.show_all()
153         s.connect('destroy', self.slotConfigDestroy)
154         self.winConfig=s
155
156     def slotConfigDestroy(self, sender):
157         self.winConfig=None
158
159         dt=sender.getData()
160
161         config=self.get_config()
162
163         config.setSize(dt['size'])
164         config.setApps(dt['apps'])
165         config.setIndiv(dt['indiv'])
166         config.setLongpress(dt['longpress'])
167         config.setAnimate(dt['animate'])
168         config.setNoBg(dt['nobg'])
169         config.setThemeBg(dt['themebg'])
170         config.save()
171         
172         # Resize widget
173         self.setSize(dt['size'])
174         self.reloadIcons()
175
176         # Free memory that is used for animations if animations are disabled
177         if not dt['animate']:
178             self.clearAnimationCache()
179
180         self.queue_draw()
181
182     def handle_click(self, sender, icon):
183         """ common handler for longpress and click """
184         if icon.name!=None and icon.name!='':
185             print "name:", icon.name
186             launcher.launch(icon.name)
187
188     def signalLongpress(self, sender, icon):
189         self.handle_click(sender, icon)
190
191     def signalClick(self, sender, icon):
192         config=self.get_config()
193
194         if not config.getLongpress():
195             self.handle_click(sender, icon)
196
197     def signalNotify(self, sender, property):
198         if property.name=='is-on-current-desktop':
199             v=self.get_property(property.name)
200             if v and self.draw_pending:
201                 self.queue_draw()
202
203     def resize2(self):
204         config=self.get_config()
205
206         w=(self.size[0] * config.iconsize) + \
207             (self.size[0] * config.iconspace)
208         h=(self.size[1] * config.iconsize) + \
209             (self.size[1] * config.iconspace)
210         self.set_size_request(w, h)
211         self.resize(w, h)
212
213     def setSize(self, size):
214         IconGrid.setSize(self, size)
215         self.resize2()
216
217 hd_plugin_type = DrlaunchPlugin
218
219 if __name__=="__main__":
220     gobject.type_register(hd_plugin_type)
221     obj=gobject.new(hd_plugin_type, plugin_id="plugin_id")
222     obj.show_all()
223     gtk.main()
224
225
226
227 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
228