Multiple instance support.
[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
70         self.winConfig=None
71
72         self.set_settings(True)
73
74         self.id=None
75         self.config=None
76
77     def get_id0(self):
78         """If this is called from the constructor then the program
79         core dumps """
80         aid=self.get_applet_id()
81
82         # Get desktop activity if D.A.M. is present
83         
84         act="/usr/bin/activity"
85
86         if os.path.exists(act):
87             r=Popen([act, "current"], stdout=PIPE).communicate()
88             activity=r[0].strip()
89         else:
90             activity=""
91
92         ret="%s-%s" % (aid, activity)
93
94         return(ret)
95
96     def get_id(self):
97         if self.id==None:
98             self.id=self.get_id0()
99
100         return(self.id)
101
102     def get_config(self):
103         if self.config==None:
104             id=self.get_id()
105             self.config=config.Config(id)
106
107         return(self.config)
108
109     def do_realize(self):
110         launcher.init()
111         config=self.get_config()
112         config.load()
113
114         IconGrid.do_realize(self, config)
115
116         self.setSize(config.getSize())
117         self.reloadIcons()
118
119         screen=self.get_screen()
120         self.set_colormap(screen.get_rgba_colormap())
121         self.set_app_paintable(True)
122
123         self.connect('show-settings', self.slot_show_settings)
124         self.connect('long-press', self.signalLongpress)
125         self.connect('click', self.signalClick)
126
127         HomePluginItem.do_realize(self)
128
129     def on_orientation_changed(self, orientation):
130         o=orientation[0]
131         self.setMode(o)
132 #       self.queue_draw()
133
134     def do_expose_event(self, event):
135         IconGrid.do_expose_event(self, event)
136         HomePluginItem.do_expose_event(self, event)
137
138     def slot_show_settings(self, dt):
139         if self.winConfig!=None:
140             # Doesn't work
141             # self.winConfig.show_all()
142             return
143
144         s=WinConfig(self.get_config())
145         s.show_all()
146         s.connect('destroy', self.slotConfigDestroy)
147         self.winConfig=s
148
149     def slotConfigDestroy(self, sender):
150         self.winConfig=None
151
152         dt=sender.getData()
153
154         config=self.get_config()
155
156         config.setSize(dt['size'])
157         config.setApps(dt['apps'])
158         config.setIndiv(dt['indiv'])
159         config.setLongpress(dt['longpress'])
160         config.save()
161         
162         # Resize widget
163         self.setSize(dt['size'])
164         self.reloadIcons()
165
166         self.queue_draw()
167
168     def handle_click(self, sender, icon):
169         """ common handler for longpress and click """
170         if icon.name!=None and icon.name!='':
171             launcher.launch(icon.name)
172
173     def signalLongpress(self, sender, icon):
174         self.handle_click(sender, icon)
175
176     def signalClick(self, sender, icon):
177         config=self.get_config()
178
179         if not config.getLongpress():
180             self.handle_click(sender, icon)
181
182     def resize(self):
183         config=self.get_config()
184
185         w=(self.size[0] * config.iconsize) + \
186             (self.size[0] * config.iconspace)
187         h=(self.size[1] * config.iconsize) + \
188             (self.size[1] * config.iconspace)
189         self.set_size_request(w, h)
190
191     def setSize(self, size):
192         IconGrid.setSize(self, size)
193         self.resize()
194
195 hd_plugin_type = DrlaunchPlugin
196
197 if __name__=="__main__":
198     gobject.type_register(hd_plugin_type)
199     obj=gobject.new(hd_plugin_type, plugin_id="plugin_id")
200     obj.show_all()
201     gtk.main()
202
203
204
205 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
206