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