view sat_bridge/DBus.py @ 1:a06a151fc31f

Disconnect first draft
author Goffi <goffi@goffi.org>
date Sun, 18 Oct 2009 23:20:45 +0200
parents c4bc297b82f0
children c14a3a7018a5
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

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("org.goffi.SAT.communication",
                         signature='sa{ss}as')
    def newContact(self, contact, attributes, groups):
        debug("new contact signal (%s) sended", contact)

    @dbus.service.signal("org.goffi.SAT.communication",
                         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("org.goffi.SAT.communication",
                         signature='ssssi')
    def presenceUpdate(self, jid, type, show, status, priority):
        debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority)

    @dbus.service.signal("org.goffi.SAT.communication",
                         signature='sss')
    def paramUpdate(self, name, value, namespace):
        debug("param update signal: %s=%s in namespace %s", name, value, namespace)

    @dbus.service.signal("org.goffi.SAT.communication",
                         signature='s')
    def contactDeleted(self, jid):
        debug("contact deleted signal: %s", jid)
    
    @dbus.service.signal("org.goffi.SAT.request",
                         signature='ssa{ss}')
    def askConfirmation(self, type, id, data):
        debug("asking for confirmation: id = [%s]  type = %s data = %s", id, type, data)



    ### methods ###    

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='', out_signature='')
    def connect(self):
        info ("Connection asked")
        return self.cb["connect"]()

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='', out_signature='')
    def disconnect(self):
        info ("Disconnection asked")
        return self.cb["disconnect"]()
    
    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='', out_signature='a(sa{ss}as)')
    def getContacts(self):
        debug("getContacts...")
        return self.cb["getContacts"]()

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='', out_signature='a(ssssi)')
    def getPresenceStatus(self):
        debug("getPresenceStatus...")
        return self.cb["getPresenceStatus"]()

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='ss', out_signature='')
    def sendMessage(self, to, message):
        debug("sendMessage...")
        self.cb["sendMessage"](to, message)

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='ss', out_signature='s')
    def sendFile(self, to, path):
        debug("sendFile...")
        return self.cb["sendFile"](to, path)

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='ssssi', out_signature='')
    def setPresence(self, to="", type="", show="", status="", priority=0):
        self.cb["setPresence"](to, type, show, status, priority)


    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='sss', out_signature='')
    def setParam(self, name, value, namespace="default"):
        self.cb["setParam"](name, str(value), namespace)
        
    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='ss', out_signature='(ss)')
    def getParam(self, name, namespace="default"):
        return self.cb["getParam"](name, namespace)

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='s', out_signature='a(sss)')
    def getParams(self, namespace):
        return self.cb["getParams"](namespace)

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='', out_signature='as')
    def getParamsCategories(self):
        return self.cb["getParamsCategories"]()

    @dbus.service.method("org.goffi.SAT.communication",
                         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("org.goffi.SAT.communication",
                         in_signature='s', out_signature='')
    def addContact(self, jid):
        debug("Subscription asked for %s", jid)
        return self.cb["addContact"](jid)
    
    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='s', out_signature='')
    def delContact(self, jid):
        debug("Unsubscription asked for %s", jid)
        return self.cb["delContact"](jid)

    @dbus.service.method("org.goffi.SAT.communication",
                         in_signature='', out_signature='b')
    def isConnected(self):
        debug("Connection status requested")
        return self.cb["isConnected"]()

    @dbus.service.method("org.goffi.SAT.request",
                         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("org.goffi.SAT.request",
                         in_signature='s', out_signature='a{ss}')
    def getProgress(self, id):
        #debug("Progress asked for %s", id)
        return self.cb["getProgress"](id)

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("org.goffi.SAT", self.session_bus)
        self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge')

    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, jid, type, show, status, priority):
        debug("updating presence for %s",jid)
        self.dbus_bridge.presenceUpdate(jid, type, show, status, priority)

    def paramUpdate(self, name, value, namespace):
        debug("updating param [%s] %s ", namespace, name)
        self.dbus_bridge.paramUpdate(name, value, namespace)

    def contactDeleted(self, jid):
        debug("sending contact deleted signal %s ", jid)
        self.dbus_bridge.contactDeleted(jid)

    def askConfirmation(self, type, id, data):
        self.dbus_bridge.askConfirmation(type, id, data)

    def register(self, name, callback):
        debug("enregistrement de %s",name)
        self.dbus_bridge.register(name, callback)