comparison src/bridge/DBus.py @ 224:9c6ee3f9ab29

files reorganisation
author Goffi <goffi@goffi.org>
date Tue, 04 Jan 2011 19:30:27 +0100
parents src/sat/bridge/DBus.py@86d249b6d9b7
children b1794cbb88e5
comparison
equal deleted inserted replaced
223:86d249b6d9b7 224:9c6ee3f9ab29
1 #!/usr/bin/python
2 #-*- coding: utf-8 -*-
3
4 """
5 SAT: a jabber client
6 Copyright (C) 2009, 2010 Jérôme Poisson (goffi@goffi.org)
7
8 This program 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 This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22
23 from bridge import Bridge
24 import dbus
25 import dbus.service
26 import dbus.mainloop.glib
27 import pdb
28 from logging import debug, info, error
29
30 const_INT_PREFIX = "org.goffi.SAT" #Interface prefix
31 const_COMM_SUFFIX = ".communication"
32 const_REQ_SUFFIX = ".request"
33
34 class DbusObject(dbus.service.Object):
35
36 def __init__(self, bus, path):
37 dbus.service.Object.__init__(self, bus, path)
38 debug("Init DbusObject...")
39 self.cb={}
40
41 def register(self, name, cb):
42 self.cb[name]=cb
43
44 ### signals ###
45
46 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
47 signature='s')
48 def connected(self, profile):
49 debug("Connected signal")
50
51 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
52 signature='s')
53 def disconnected(self, profile):
54 debug("Disconnected signal")
55
56 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
57 signature='sa{ss}ass')
58 def newContact(self, contact, attributes, groups, profile):
59 debug("new contact signal (%s) sended (profile: %s)", contact, profile)
60
61 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
62 signature='sssss')
63 def newMessage(self, from_jid, msg, type, to, profile):
64 debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to)
65
66 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
67 signature='ssss')
68 def newAlert(self, msg, title, type, profile):
69 debug("new alert signal (title:%s type:%s msg:%s profile:%s) sended", type, title, msg, profile)
70
71 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
72 signature='ssia{ss}s')
73 def presenceUpdate(self, entity, show, priority, statuses, profile):
74 debug("presence update signal (from:%s show:%s priority:%d statuses:%s profile:%s) sended" , entity, show, priority, statuses, profile)
75
76 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
77 signature='sss')
78 def subscribe(self, type, entity, profile):
79 debug("subscribe (type: [%s] from:[%s] profile:[%s])" , type, entity, profile)
80
81 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
82 signature='ssss')
83 def paramUpdate(self, name, value, category, profile):
84 debug("param update signal: %s=%s in category %s (profile: %s)", name, value, category, profile)
85
86 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
87 signature='ss')
88 def contactDeleted(self, entity, profile):
89 debug("contact deleted signal: %s (profile: %s)", entity, profile)
90
91 @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX,
92 signature='ssa{ss}')
93 def askConfirmation(self, type, id, data):
94 debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data)
95
96 @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX,
97 signature='ssa{ss}')
98 def actionResult(self, type, id, data):
99 debug("result of action: id = [%s] type = %s data = %s", id, type, data)
100
101 @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX,
102 signature='ssa{sa{ss}}')
103 def actionResultExt(self, type, id, data):
104 debug("extended result of action: id = [%s] type = %s data = %s", id, type, data)
105
106 @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX,
107 signature='sa{ss}')
108 def updatedValue(self, name, value):
109 debug("updated value: %s = %s", name, value)
110
111 ### methods ###
112
113
114 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
115 in_signature='', out_signature='s')
116 def getVersion(self):
117 return self.cb["getVersion"]()
118
119 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
120 in_signature='s', out_signature='s')
121 def getProfileName(self, profile_key):
122 return self.cb["getProfileName"](profile_key)
123
124 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
125 in_signature='', out_signature='as')
126 def getProfilesList(self):
127 info ('Profile list asked')
128 return self.cb["getProfilesList"]()
129
130 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
131 in_signature='s', out_signature='i')
132 def createProfile(self, name):
133 info ('Profile creation asked')
134 return self.cb["createProfile"](unicode(name))
135
136 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
137 in_signature='s', out_signature='i')
138 def deleteProfile(self, name):
139 info ('Profile deletion asked')
140 return self.cb["deleteProfile"](str(name))
141
142 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
143 in_signature='sssi', out_signature='s')
144 def registerNewAccount(self, login, password, host, port=5222):
145 info ("New account registration asked")
146 return self.cb["registerNewAccount"](login, password, host, port)
147
148 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
149 in_signature='s', out_signature='')
150 def connect(self, profile_key='@DEFAULT@'):
151 info ("Connection asked")
152 return self.cb["connect"](profile_key)
153
154 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
155 in_signature='s', out_signature='')
156 def disconnect(self, profile_key='@DEFAULT@'):
157 info ("Disconnection asked")
158 return self.cb["disconnect"](profile_key)
159
160 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
161 in_signature='', out_signature='b')
162 def isConnected(self, profile_key='@DEFAULT@'):
163 info ("Connection status asked")
164 return self.cb["isConnected"](profile_key)
165
166 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
167 in_signature='s', out_signature='a(sa{ss}as)')
168 def getContacts(self, profile_key='@DEFAULT@'):
169 debug("getContacts...")
170 return self.cb["getContacts"](profile_key)
171
172 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
173 in_signature='s', out_signature='a{sa{s(sia{ss})}}')
174 def getPresenceStatus(self, profile_key='@DEFAULT@'):
175 debug("getPresenceStatus...")
176 return self.cb["getPresenceStatus"](profile_key)
177
178 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
179 in_signature='s', out_signature='a{ss}')
180 def getWaitingSub(self, profile_key='@DEFAULT@'):
181 debug("getWaitingSub...")
182 return self.cb["getWaitingSub"](profile_key)
183
184 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
185 in_signature='ssss', out_signature='')
186 def sendMessage(self, to, message, type='chat', profile_key='@DEFAULT@'):
187 debug("sendMessage...")
188 print "sendtype=", type #gof
189 self.cb["sendMessage"](to, message, type, profile_key)
190
191 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
192 in_signature='ssia{ss}s', out_signature='')
193 def setPresence(self, to="", show="", priority=0, statuses={}, profile_key='@DEFAULT@'):
194 self.cb["setPresence"](to, show, priority, statuses, profile_key)
195
196 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
197 in_signature='sss', out_signature='')
198 def subscription(self, type, entity, profile_key='@DEFAULT@'):
199 self.cb["subscription"](type, entity, profile_key)
200
201 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
202 in_signature='ssss', out_signature='')
203 def setParam(self, name, value, category, profile_key='@DEFAULT@'):
204 self.cb["setParam"](unicode(name), unicode(value), unicode(category), profile_key)
205
206 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
207 in_signature='sss', out_signature='s')
208 def getParamA(self, name, category="default", profile_key='@DEFAULT@'):
209 return self.cb["getParamA"](name, category, profile_key = profile_key)
210
211
212 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
213 in_signature='s', out_signature='s')
214 def getParamsUI(self, profile_key='@DEFAULT@'):
215 return self.cb["getParamsUI"](profile_key)
216
217 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
218 in_signature='s', out_signature='s')
219 def getParams(self, profile_key='@DEFAULT@'):
220 return self.cb["getParams"](profile_key)
221
222 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
223 in_signature='ss', out_signature='s')
224 def getParamsForCategory(self, category, profile_key='@DEFAULT@'):
225 return self.cb["getParamsForCategory"](category, profile_key)
226
227 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
228 in_signature='', out_signature='as')
229 def getParamsCategories(self):
230 return self.cb["getParamsCategories"]()
231
232 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
233 in_signature='ssi', out_signature='a{i(ss)}')
234 def getHistory(self, from_jid, to_jid, size):
235 debug("History asked for %s", to_jid)
236 return self.cb["getHistory"](from_jid, to_jid, size)
237
238 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
239 in_signature='ss', out_signature='')
240 def addContact(self, entity, profile_key='@DEFAULT@'):
241 debug("Subscription asked for %s (profile %s)", entity, profile_key)
242 return self.cb["addContact"](entity, profile_key)
243
244 @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
245 in_signature='ss', out_signature='')
246 def delContact(self, entity, profile_key='@DEFAULT@'):
247 debug("Unsubscription asked for %s (profile %s)", entity, profile_key)
248 return self.cb["delContact"](entity, profile_key)
249
250 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
251 in_signature='sa{ss}s', out_signature='s')
252 def launchAction(self, type, data, profile_key='@DEFAULT@'):
253 return self.cb["launchAction"](type, data, profile_key)
254
255 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
256 in_signature='sba{ss}', out_signature='')
257 def confirmationAnswer(self, id, accepted, data):
258 debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused")
259 return self.cb["confirmationAnswer"](id, accepted, data)
260
261
262 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
263 in_signature='s', out_signature='a{ss}')
264 def getProgress(self, id):
265 #debug("Progress asked for %s", id)
266 return self.cb["getProgress"](id)
267
268 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
269 in_signature='', out_signature='a(sss)')
270 def getMenus(self):
271 return self.cb["getMenus"]()
272
273 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
274 in_signature='sss', out_signature='s')
275 def getMenuHelp(self, category, name, type="NORMAL"):
276 return self.cb["getMenuHelp"](category, name, type)
277
278 @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
279 in_signature='ssss', out_signature='s')
280 def callMenu(self, category, name, type, profile_key):
281 return self.cb["callMenu"](category, name, type, profile_key)
282
283 def __attribute_string(self, in_sign):
284 i=0
285 idx=0
286 attr_string=""
287 while i<len(in_sign):
288 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
289 raise Exception #FIXME: create an exception here (unmanaged attribute type)
290
291 attr_string += ("" if idx==0 else ",") + ("arg_%i" % idx)
292 idx+=1
293
294 if in_sign[i] == 'a':
295 i+=1
296 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays
297 i+=1
298 continue #we have a simple type for the array
299 while (True): #we have a dict or a list of tuples
300 i+=1
301 if i>=len(in_sign):
302 raise Exception #FIXME: create an exception here (the '}' is not presend)
303 if in_sign[i] == '}' or in_sign[i] == ')':
304 break
305 i+=1
306 return attr_string
307
308
309
310 def addMethod(self, name, int_suffix, in_sign, out_sign):
311 """Dynamically add a method to Dbus Bridge"""
312 #FIXME: Better way ???
313 attributes = self.__attribute_string(in_sign)
314
315 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
316 exec (code)
317 method = locals()[name]
318 setattr(DbusObject, name, dbus.service.method(
319 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method))
320
321 def addSignal(self, name, int_suffix, signature):
322 """Dynamically add a signal to Dbus Bridge"""
323 #FIXME: Better way ???
324 attributes = self.__attribute_string(signature)
325
326 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec')
327 exec (code)
328 signal = locals()[name]
329 setattr(DbusObject, name, dbus.service.signal(
330 const_INT_PREFIX+int_suffix, signature=signature)(signal))
331
332 class DBusBridge(Bridge):
333 def __init__(self):
334 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
335 Bridge.__init__(self)
336 info ("Init DBus...")
337 self.session_bus = dbus.SessionBus()
338 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus)
339 self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge')
340
341 def connected(self, profile):
342 self.dbus_bridge.connected(profile)
343
344 def disconnected(self, profile):
345 self.dbus_bridge.disconnected(profile)
346
347 def newContact(self, contact, attributes, groups, profile):
348 self.dbus_bridge.newContact(contact, attributes, groups, profile)
349
350 def newMessage(self, from_jid, msg, type='chat', to='', profile='@NONE@'):
351 debug("sending message...")
352 self.dbus_bridge.newMessage(from_jid, msg, type, to, profile)
353
354 def newAlert(self, msg, title="", alert_type="INFO", profile='@NONE@'):
355 self.dbus_bridge.newAlert(msg, title, alert_type, profile)
356
357 def presenceUpdate(self, entity, show, priority, statuses, profile):
358 debug("updating presence for %s",entity)
359 self.dbus_bridge.presenceUpdate(entity, show, priority, statuses, profile)
360
361 def roomJoined(self, room_id, room_service, room_nicks, user_nick, profile):
362 self.dbus_bridge.roomJoined(room_id, room_service, room_nicks, user_nick, profile)
363
364 def subscribe(self, sub_type, entity, profile):
365 debug("subscribe request for %s",entity)
366 self.dbus_bridge.subscribe(sub_type, entity, profile)
367
368 def paramUpdate(self, name, value, category, profile):
369 debug("updating param [%s] %s ", category, name)
370 self.dbus_bridge.paramUpdate(name, value, category, profile)
371
372 def contactDeleted(self, entity, profile):
373 debug("sending contact deleted signal %s ", entity)
374 self.dbus_bridge.contactDeleted(entity, profile)
375
376 def askConfirmation(self, type, id, data):
377 self.dbus_bridge.askConfirmation(type, id, data)
378
379 def actionResult(self, type, id, data):
380 self.dbus_bridge.actionResult(type, id, data)
381
382 def actionResultExt(self, type, id, data):
383 self.dbus_bridge.actionResultExt(type, id, data)
384
385 def updatedValue(self, name, value):
386 self.dbus_bridge.updatedValue(name, value)
387
388 def register(self, name, callback):
389 debug("registering DBus bridge method [%s]", name)
390 self.dbus_bridge.register(name, callback)
391
392 def addMethod(self, name, int_suffix, in_sign, out_sign, method):
393 """Dynamically add a method to Dbus Bridge"""
394 print ("Adding method [%s] to DBus bridge" % name)
395 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign)
396 self.register(name, method)
397
398 def addSignal(self, name, int_suffix, signature):
399 self.dbus_bridge.addSignal(name, int_suffix, signature)
400 setattr(DBusBridge, name, getattr(self.dbus_bridge, name))
401