Mercurial > libervia-backend
view sat_bridge/DBus.py @ 55:20e54702fc0b
wix: changed main frame default size
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 10 Jan 2010 17:32:13 +1100 |
parents | 6455fb62ff83 |
children | a5b5fb5fc9fd |
line wrap: on
line source
#!/usr/bin/python #-*- coding: utf-8 -*- """ SAT: a jabber client Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from bridge import Bridge import dbus import dbus.service import dbus.mainloop.glib import pdb from logging import debug, info, error const_INT_PREFIX = "org.goffi.SAT" #Interface prefix const_COMM_SUFFIX = ".communication" const_REQ_SUFFIX = ".request" class DbusObject(dbus.service.Object): def __init__(self, bus, path): dbus.service.Object.__init__(self, bus, path) debug("Init DbusObject...") self.cb={} def register(self, name, cb): self.cb[name]=cb ### signals ### @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='') def connected(self): debug("Connected signal") @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='') def disconnected(self): debug("Disconnected signal") @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='sa{ss}as') def newContact(self, contact, attributes, groups): debug("new contact signal (%s) sended", contact) @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='ssss') def newMessage(self, from_jid, msg, type='chat', to=''): debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to) @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='ssia{ss}') def presenceUpdate(self, entity, show, priority, statuses): debug("presence update signal (from:%s show:%s priority:%d statuses:%s) sended" , entity, show, priority, statuses) @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='ss') def subscribe(self, type, entity): debug("subscribe (type: [%s] from:[%s])" , type, entity) @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='sss') def paramUpdate(self, name, value, category): debug("param update signal: %s=%s in category %s", name, value, category) @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, signature='s') def contactDeleted(self, entity): debug("contact deleted signal: %s", entity) @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX, signature='ssa{ss}') def askConfirmation(self, type, id, data): debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX, signature='ssa{ss}') def actionResult(self, type, id, data): debug("result of action: id = [%s] type = %s data = %s", id, type, data) @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX, signature='ssa{sa{ss}}') def actionResultExt(self, type, id, data): debug("extended result of action: id = [%s] type = %s data = %s", id, type, data) @dbus.service.signal(const_INT_PREFIX+const_REQ_SUFFIX, signature='sa{ss}') def updatedValue(self, name, value): debug("updated value: %s = %s", name, value) ### methods ### @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='sssi', out_signature='s') def registerNewAccount(self, login, password, host, port=5222): info ("New account registration asked") return self.cb["registerNewAccount"](login, password, host, port) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='') def connect(self): info ("Connection asked") return self.cb["connect"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='') def disconnect(self): info ("Disconnection asked") return self.cb["disconnect"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='b') def isConnected(self): info ("Connection status asked") return self.cb["isConnected"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='a(sa{ss}as)') def getContacts(self): debug("getContacts...") return self.cb["getContacts"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='a{sa{s(sia{ss})}}') def getPresenceStatus(self): debug("getPresenceStatus...") return self.cb["getPresenceStatus"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='a{ss}') def getWaitingSub(self): debug("getWaitingSub...") return self.cb["getWaitingSub"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ss', out_signature='') def sendMessage(self, to, message): debug("sendMessage...") self.cb["sendMessage"](to, message) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ssia{ss}', out_signature='') def setPresence(self, to="", show="", priority=0, statuses={}): self.cb["setPresence"](to, show, priority, statuses) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ss', out_signature='') def subscription(self, type, entity): self.cb["subscription"](type, entity) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='sss', out_signature='') def setParam(self, name, value, category): self.cb["setParam"](name, str(value), category) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ss', out_signature='s') def getParamA(self, name, category="default"): return self.cb["getParamA"](name, category) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='s') def getParams(self): return self.cb["getParams"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='s', out_signature='s') def getParamsForCategory(self, category): return self.cb["getParamsForCategory"](category) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='', out_signature='as') def getParamsCategories(self): return self.cb["getParamsCategories"]() @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='ssi', out_signature='a{i(ss)}') def getHistory(self, from_jid, to_jid, size): debug("History asked for %s", to_jid) return self.cb["getHistory"](from_jid, to_jid, size) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='s', out_signature='') def addContact(self, entity): debug("Subscription asked for %s", entity) return self.cb["addContact"](entity) @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX, in_signature='s', out_signature='') def delContact(self, entity): debug("Unsubscription asked for %s", entity) return self.cb["delContact"](entity) @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX, in_signature='sa{ss}', out_signature='s') def launchAction(self, type, data): return self.cb["launchAction"](type, data) @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX, in_signature='sba{ss}', out_signature='') def confirmationAnswer(self, id, accepted, data): debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") return self.cb["confirmationAnswer"](id, accepted, data) @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX, in_signature='s', out_signature='a{ss}') def getProgress(self, id): #debug("Progress asked for %s", id) return self.cb["getProgress"](id) def __attribute_string(self, in_sign): i=0 idx=0 attr_string="" while i<len(in_sign): if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: raise Exception #FIXME: create an exception here (unmanaged attribute type) attr_string += ("" if idx==0 else ",") + ("arg_%i" % idx) idx+=1 if in_sign[i] == 'a': while (True): i+=1 if i>=len(in_sign): raise Exception #FIXME: create an exception here (the '}' is not presend) if in_sign[i] == '}' or in_sign[i] == ')': break i+=1 return attr_string def addMethod(self, name, int_suffix, in_sign, out_sign): """Dynamically add a method to Dbus Bridge""" #FIXME: Better way ??? attributes = self.__attribute_string(in_sign) code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') exec (code) method = locals()[name] setattr(DbusObject, name, dbus.service.method( const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method)) class DBusBridge(Bridge): def __init__(self): dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) Bridge.__init__(self) info ("Init DBus...") self.session_bus = dbus.SessionBus() self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') def connected(self): self.dbus_bridge.connected() def disconnected(self): self.dbus_bridge.disconnected() def newContact(self, contact, attributes, groups): self.dbus_bridge.newContact(contact, attributes, groups) def newMessage(self,from_jid,msg,type='chat', to=''): debug("sending message...") self.dbus_bridge.newMessage(from_jid, msg, type, to) def presenceUpdate(self, entity, show, priority, statuses): debug("updating presence for %s",entity) self.dbus_bridge.presenceUpdate(entity, show, priority, statuses) def subscribe(self, type, entity): debug("subscribe request for %s",entity) self.dbus_bridge.subscribe(type, entity) def paramUpdate(self, name, value, category): debug("updating param [%s] %s ", category, name) self.dbus_bridge.paramUpdate(name, value, category) def contactDeleted(self, entity): debug("sending contact deleted signal %s ", entity) self.dbus_bridge.contactDeleted(entity) def askConfirmation(self, type, id, data): self.dbus_bridge.askConfirmation(type, id, data) def actionResult(self, type, id, data): self.dbus_bridge.actionResult(type, id, data) def actionResultExt(self, type, id, data): self.dbus_bridge.actionResultExt(type, id, data) def updatedValue(self, name, value): self.dbus_bridge.updatedValue(name, value) def register(self, name, callback): debug("registering DBus bridge method [%s]",name) self.dbus_bridge.register(name, callback) def addMethod(self, name, int_suffix, in_sign, out_sign, method): """Dynamically add a method to Dbus Bridge""" print ("Adding method [%s] to DBus bridge" % name) self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign) self.register(name, method)