1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 import dbus
28 import dbus.service
29 if getattr(dbus, 'version', (0,0,0)) >= (0,41,0):
30 if getattr(dbus, 'version', (0,0,0)) <= (0,80,0):
31
32 import dbus.glib
33 else:
34
35 from dbus.mainloop.glib import DBusGMainLoop
36 DBusGMainLoop(set_as_default=True)
37 import gettext
38 import screenlets
39
40 gettext.textdomain('screenlets')
41 gettext.bindtextdomain('screenlets',screenlets.INSTALL_PREFIX + '/share/locale')
44 return gettext.gettext(s)
45
46
47
48 action = dbus.service.method
49 signal = dbus.service.signal
54 """The ScreenletService contains the boilerplate code for creating new
55 dbus-objects for Screenlets. Subclasses can easily implement new functions
56 to allow controlling and managing Screenlets through DBus in any way
57 imaginable. This class should implement the default actions available for
58 all screenlets - add, delete, get, set, (move?)"""
59
60 BUS = 'org.screenlets'
61 PATH = '/org/screenlets/'
62 IFACE = 'org.screenlets.ScreenletService'
63
64 - def __init__ (self, screenlet, name, id=None):
65
66 if name == '':
67 raise Exception('No name set in ScreenletService.__init__!');
68
69 self.screenlet = screenlet
70 self.name = name
71 self.BUS = self.BUS + '.' + name
72 self.objpath = self.PATH + name
73 if id:
74 self.objpath += '/' + id
75
76 dbus.service.Object.__init__(self, dbus.service.BusName(self.BUS,
77 bus=dbus.SessionBus(), do_not_queue=True), self.objpath)
78
79 @action(IFACE)
81 print "TEST: %s" % str(self.screenlet)
82
83 @action(IFACE)
84 - def debug (self, string):
85 """Dump a string to the console."""
86 print "DEBUG: %s" % string
87
88 @action(IFACE)
90 """Ask the assigned Screenlet to add a new instance of itself to
91 its session. The new Screenlet will have the ID defined by 'id'.
92 The ID of the new instance is returned, so you can auto-generate an ID
93 by passing an empty string. The function returns None if adding a
94 new instance failed for some reason."""
95 sl = self.screenlet.session.create_instance(id)
96 sl.finish_loading()
97 if sl != None:
98 return sl.id
99 return False
100
101 @action(IFACE)
102 - def get (self, id, attrib):
103 """Ask the assigned Screenlet to return the given attribute's value. If
104 'id' is defined, the instance with the given id will be accessed,
105 else the main instance is used. Protected attributes are not returned
106 by this function.
107 TODO: Throw exception on error? ... could be abused to crash the app"""
108 if id:
109 sl = self.screenlet.session.get_instance_by_id(id)
110 if not sl:
111 sl = self.screenlet
112 o = sl.get_option_by_name(attrib)
113 try:
114 if not o.protected:
115 return getattr(sl, attrib)
116 else:
117 print "Cannot get/set protected options through service."
118 return None
119 except AttributeError:
120 print 'Error getting attribute'
121 return None
122 @action(IFACE)
124 """Get the ID of the first existing instance of the assigned
125 Screenlet (within the screenlet's active session)."""
126 if len(self.screenlet.session.instances):
127 return self.screenlet.session.instances[0].id
128 return None
129
130 @action(IFACE)
132 """Return a list with IDs of all existing instances of the assigned
133 Screenlet (within the screenlet's active session)."""
134 lst = []
135 for sl in self.screenlet.session.instances:
136 lst.append (sl.id)
137 return lst
138
139 @action(IFACE)
141 """Quit all instances of the screenlet. Similar to selecting Quit
142 from the menu."""
143 self.screenlet.destroy(self.screenlet.window)
144
145 @action(IFACE)
146 - def set (self, id, attrib, value):
147 """Ask the assigned Screenlet to set the given attribute to 'value'. The
148 instance with the given id will be accessed. """
149 sl = self.screenlet.session.get_instance_by_id(id)
150 if sl == None:
151 raise Exception('Trying to access invalid instance "%s".' % id)
152 if sl.get_option_by_name(attrib) == None:
153 raise Exception('Trying to access invalid option "%s".' % attrib)
154 else:
155 try:
156 o = sl.get_option_by_name(attrib)
157 if not o.protected:
158 setattr(sl, attrib, value)
159 else:
160 print "Cannot get/set protected options through service."
161 except: pass
162
163 @signal(IFACE)
165 """This signal gets emitted whenever a new instance of the assigned
166 Screenlet gets added."""
167
168 @signal(IFACE)
170 """This signal gets emitted whenever an instance of the assigned
171 Screenlet gets removed."""
172
175 """This currently returns a dbus.Interface-object for remote-accessing the
176 ScreenletService through dbus, but that may change in the future to some
177 more abstracted system with support for multiple IPC-backends."""
178 bus = dbus.SessionBus()
179 if bus:
180 try:
181 path = ScreenletService.PATH + name
182 proxy_obj = bus.get_object(ScreenletService.BUS + '.' + name, path)
183 if proxy_obj:
184
185 return dbus.Interface(proxy_obj, interface)
186 except Exception, ex:
187 print "Error in screenlets.services.get_service_by_name: %s" % str(ex)
188 return None
189
191 """Checks if the given service is available (ie. the given Screenlet has at
192 least one running instance) and returns True or False."""
193 bus = dbus.SessionBus()
194 if bus:
195 try:
196 path = ScreenletService.PATH + name
197 if bus.get_object(ScreenletService.BUS + '.' + name, path):
198 return True
199 except Exception:
200 pass
201 return False
202
203
204