2007-02-21 12:41:26 +01:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
|
|
|
#
|
|
|
|
# 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 2 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, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
import gobject
|
2007-02-21 13:06:45 +01:00
|
|
|
import dbus, dbus.service, dbus.glib
|
2007-02-22 20:11:31 +01:00
|
|
|
from telepathy.client import ManagerRegistry, Connection
|
2007-02-23 18:04:16 +01:00
|
|
|
from telepathy.interfaces import (CONN_MGR_INTERFACE, CONN_INTERFACE)
|
|
|
|
from telepathy.constants import (CONNECTION_STATUS_CONNECTING, CONNECTION_STATUS_CONNECTED,
|
|
|
|
CONNECTION_STATUS_DISCONNECTED, CONNECTION_HANDLE_TYPE_CONTACT)
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
from server_plugin import ServerPlugin
|
|
|
|
from linklocal_plugin import LinkLocalPlugin
|
|
|
|
|
2007-02-23 17:30:25 +01:00
|
|
|
from buddy import Buddy, Owner
|
|
|
|
from activity import Activity
|
2007-02-21 13:06:45 +01:00
|
|
|
|
|
|
|
_PRESENCE_SERVICE = "org.laptop.Sugar.Presence"
|
|
|
|
_PRESENCE_INTERFACE = "org.laptop.Sugar.Presence"
|
|
|
|
_PRESENCE_PATH = "/org/laptop/Sugar/Presence"
|
|
|
|
|
|
|
|
|
|
|
|
class NotFoundError(dbus.DBusException):
|
|
|
|
def __init__(self):
|
|
|
|
dbus.DBusException.__init__(self)
|
|
|
|
self._dbus_error_name = _PRESENCE_INTERFACE + '.NotFound'
|
2007-02-21 12:41:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
class PresenceService(dbus.service.Object):
|
2007-02-21 13:06:45 +01:00
|
|
|
def __init__(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
self._next_object_id = 0
|
|
|
|
|
2007-02-21 13:20:59 +01:00
|
|
|
self._buddies = {} # key -> Buddy
|
2007-02-23 17:30:25 +01:00
|
|
|
self._handles = {} # tp client -> (handle -> Buddy)
|
2007-02-21 13:20:59 +01:00
|
|
|
self._activities = {} # activity id -> Activity
|
|
|
|
|
2007-02-21 13:06:45 +01:00
|
|
|
bus = dbus.SessionBus()
|
|
|
|
self._bus_name = dbus.service.BusName(_PRESENCE_SERVICE, bus=bus)
|
2007-02-21 13:58:16 +01:00
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
# Create the Owner object
|
|
|
|
objid = self._get_next_object_id()
|
2007-02-26 17:18:52 +01:00
|
|
|
self._owner = Owner(self, self._bus_name, objid)
|
2007-02-22 20:11:31 +01:00
|
|
|
self._buddies[self._owner.get_key()] = self._owner
|
|
|
|
|
|
|
|
self._registry = ManagerRegistry()
|
|
|
|
self._registry.LoadManagers()
|
2007-02-21 13:58:16 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
# Set up the server connection
|
|
|
|
self._server_plugin = ServerPlugin(self._registry)
|
|
|
|
self._handles[self._server_plugin] = {}
|
2007-02-23 17:30:25 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
self._server_plugin.connect('status', self._server_status_cb)
|
|
|
|
self._server_plugin.connect('contact-online', self._contact_online)
|
|
|
|
self._server_plugin.connect('contact-offline', self._contact_offline)
|
2007-02-26 17:18:52 +01:00
|
|
|
self._server_plugin.connect('avatar-updated', self._avatar_updated)
|
2007-02-26 01:24:48 +01:00
|
|
|
self._server_plugin.start()
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
# Set up the link local connection
|
|
|
|
self._ll_plugin = LinkLocalPlugin(self._registry)
|
|
|
|
self._handles[self._ll_plugin] = {}
|
2007-02-23 14:15:51 +01:00
|
|
|
|
|
|
|
dbus.service.Object.__init__(self, self._bus_name, _PRESENCE_PATH)
|
|
|
|
|
2007-02-26 04:45:16 +01:00
|
|
|
def _server_status_cb(self, plugin, status, reason):
|
2007-02-26 01:24:48 +01:00
|
|
|
pass
|
2007-02-23 18:04:16 +01:00
|
|
|
|
2007-02-23 17:30:25 +01:00
|
|
|
def _contact_online(self, tp, handle, key):
|
2007-02-26 12:28:50 +01:00
|
|
|
new_buddy = False
|
2007-02-23 17:30:25 +01:00
|
|
|
buddy = self._buddies.get(key)
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-02-23 17:30:25 +01:00
|
|
|
if not buddy:
|
|
|
|
# we don't know yet this buddy
|
|
|
|
objid = self._get_next_object_id()
|
2007-02-26 17:18:52 +01:00
|
|
|
buddy = Buddy(self._bus_name, objid, handle=handle)
|
2007-02-23 17:30:25 +01:00
|
|
|
buddy.set_key(key)
|
2007-02-26 17:18:52 +01:00
|
|
|
print "create buddy", key
|
2007-02-23 17:30:25 +01:00
|
|
|
self._buddies[key] = buddy
|
2007-02-26 12:28:50 +01:00
|
|
|
new_buddy = True
|
2007-02-23 17:30:25 +01:00
|
|
|
|
|
|
|
buddies = self._handles[tp]
|
|
|
|
buddies[handle] = buddy
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-02-26 12:28:50 +01:00
|
|
|
# store the handle of the buddy for this CM
|
|
|
|
buddy.handles[tp] = handle
|
|
|
|
|
|
|
|
if new_buddy:
|
|
|
|
self.BuddyAppeared(buddy.object_path())
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-02-23 17:30:25 +01:00
|
|
|
def _contact_offline(self, tp, handle):
|
|
|
|
buddy = self._handles[tp].pop(handle)
|
|
|
|
key = buddy.get_key()
|
|
|
|
|
2007-02-26 12:28:50 +01:00
|
|
|
# the handle of the buddy for this CM is not valid anymore
|
|
|
|
buddy.handles.pop(tp)
|
2007-02-23 17:30:25 +01:00
|
|
|
|
2007-02-26 12:28:50 +01:00
|
|
|
if not buddy.handles:
|
|
|
|
# we remove the last handle of the buddy, so we don't see
|
|
|
|
# it anymore.
|
|
|
|
self._buddies.pop(key)
|
|
|
|
print "remove buddy"
|
|
|
|
self.BuddyDisappeared(buddy.object_path())
|
2007-02-23 14:15:51 +01:00
|
|
|
|
|
|
|
def _get_next_object_id(self):
|
|
|
|
"""Increment and return the object ID counter."""
|
|
|
|
self._next_object_id = self._next_object_id + 1
|
|
|
|
return self._next_object_id
|
|
|
|
|
2007-02-26 17:18:52 +01:00
|
|
|
def _avatar_updated(self, tp, handle, avatar):
|
|
|
|
buddy = self._handles[tp].get(handle)
|
|
|
|
|
|
|
|
if buddy:
|
|
|
|
buddy.set_icon(avatar)
|
|
|
|
|
2007-02-21 13:06:45 +01:00
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def ActivityAppeared(self, activity):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def ActivityDisappeared(self, activity):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def BuddyAppeared(self, buddy):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.signal(_PRESENCE_INTERFACE, signature="o")
|
|
|
|
def BuddyDisappeared(self, buddy):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, out_signature="ao")
|
|
|
|
def GetActivities(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
ret = []
|
|
|
|
for act in self._activities.values():
|
|
|
|
ret.append(act.object_path())
|
|
|
|
return ret
|
2007-02-21 13:06:45 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature="s", out_signature="o")
|
|
|
|
def GetActivityById(self, actid):
|
2007-02-21 13:58:16 +01:00
|
|
|
if self._activities.has_key(actid):
|
|
|
|
return self._activities[actid].object_path()
|
2007-02-21 13:08:40 +01:00
|
|
|
raise NotFoundError("The activity was not found.")
|
2007-02-21 13:06:45 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, out_signature="ao")
|
|
|
|
def GetBuddies(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
ret = []
|
|
|
|
for buddy in self._buddies.values():
|
|
|
|
ret.append(buddy.object_path())
|
|
|
|
return ret
|
2007-02-21 13:06:45 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature="ay", out_signature="o")
|
|
|
|
def GetBuddyByPublicKey(self, key):
|
2007-02-21 13:58:16 +01:00
|
|
|
if self._buddies.has_key(key):
|
|
|
|
return self._buddies[key].object_path()
|
2007-02-21 13:08:40 +01:00
|
|
|
raise NotFoundError("The buddy was not found.")
|
2007-02-21 13:06:45 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, out_signature="o")
|
|
|
|
def GetOwner(self):
|
2007-02-21 13:58:16 +01:00
|
|
|
if not self._owner:
|
|
|
|
raise NotFoundError("The owner was not found.")
|
|
|
|
else:
|
|
|
|
return self._owner.get_object_path()
|
2007-02-21 12:41:26 +01:00
|
|
|
|
2007-02-21 13:06:45 +01:00
|
|
|
@dbus.service.method(_PRESENCE_INTERFACE, in_signature="sssa{sv}", out_signature="o")
|
|
|
|
def ShareActivity(self, actid, atype, name, properties):
|
2007-02-21 13:08:40 +01:00
|
|
|
raise NotImplementedError("not implemented yet")
|
2007-02-21 12:41:26 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
loop = gobject.MainLoop()
|
|
|
|
ps = PresenceService()
|
|
|
|
try:
|
|
|
|
loop.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print 'Ctrl+C pressed, exiting...'
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|