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