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