2006-07-25 22:52:45 +02:00
|
|
|
import dbus, dbus.glib, gobject
|
2006-06-13 21:25:54 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
import Buddy, Service, Activity
|
|
|
|
|
|
|
|
class ObjectCache(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._cache = {}
|
|
|
|
|
|
|
|
def get(self, object_path):
|
|
|
|
try:
|
|
|
|
return self._cache[object_path]
|
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def add(self, obj):
|
|
|
|
op = obj.object_path()
|
|
|
|
if not self._cache.has_key(op):
|
|
|
|
self._cache[op] = obj
|
|
|
|
|
|
|
|
def remove(self, object_path):
|
|
|
|
if self._cache.has_key(object_path):
|
|
|
|
del self._cache[object_path]
|
2006-06-13 21:25:54 +02:00
|
|
|
|
2006-07-24 18:27:21 +02:00
|
|
|
|
|
|
|
DBUS_SERVICE = "org.laptop.Presence"
|
|
|
|
DBUS_INTERFACE = "org.laptop.Presence"
|
|
|
|
DBUS_PATH = "/org/laptop/Presence"
|
|
|
|
|
|
|
|
|
2006-06-13 00:31:26 +02:00
|
|
|
class PresenceService(gobject.GObject):
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-06-13 00:31:26 +02:00
|
|
|
__gsignals__ = {
|
2006-07-23 16:21:00 +02:00
|
|
|
'buddy-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-07-19 21:27:37 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-07-23 16:21:00 +02:00
|
|
|
'buddy-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-07-19 21:27:37 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-07-23 16:21:00 +02:00
|
|
|
'service-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-06-15 23:41:26 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-07-23 16:21:00 +02:00
|
|
|
'service-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-06-15 23:41:26 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-07-23 16:21:00 +02:00
|
|
|
'activity-appeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-07-19 21:27:37 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-07-23 16:21:00 +02:00
|
|
|
'activity-disappeared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2006-07-19 21:27:37 +02:00
|
|
|
([gobject.TYPE_PYOBJECT]))
|
2006-06-13 00:31:26 +02:00
|
|
|
}
|
|
|
|
|
2006-07-24 18:27:21 +02:00
|
|
|
_PS_BUDDY_OP = DBUS_PATH + "/Buddies/"
|
|
|
|
_PS_SERVICE_OP = DBUS_PATH + "/Services/"
|
|
|
|
_PS_ACTIVITY_OP = DBUS_PATH + "/Activities/"
|
2006-07-19 21:27:37 +02:00
|
|
|
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-06-22 21:59:38 +02:00
|
|
|
def __init__(self):
|
2006-06-13 00:31:26 +02:00
|
|
|
gobject.GObject.__init__(self)
|
2006-07-23 06:56:40 +02:00
|
|
|
self._objcache = ObjectCache()
|
2006-07-19 21:27:37 +02:00
|
|
|
self._bus = dbus.SessionBus()
|
2006-07-24 18:27:21 +02:00
|
|
|
self._ps = dbus.Interface(self._bus.get_object(DBUS_SERVICE,
|
|
|
|
DBUS_PATH), DBUS_INTERFACE)
|
2006-07-19 21:27:37 +02:00
|
|
|
self._ps.connect_to_signal('BuddyAppeared', self._buddy_appeared_cb)
|
|
|
|
self._ps.connect_to_signal('BuddyDisappeared', self._buddy_disappeared_cb)
|
2006-07-23 16:21:00 +02:00
|
|
|
self._ps.connect_to_signal('ServiceAppeared', self._service_appeared_cb)
|
|
|
|
self._ps.connect_to_signal('ServiceDisappeared', self._service_disappeared_cb)
|
|
|
|
self._ps.connect_to_signal('ActivityAppeared', self._activity_appeared_cb)
|
|
|
|
self._ps.connect_to_signal('ActivityDisappeared', self._activity_disappeared_cb)
|
2006-07-19 21:27:37 +02:00
|
|
|
|
|
|
|
def _new_object(self, object_path):
|
|
|
|
obj = self._objcache.get(object_path)
|
|
|
|
if not obj:
|
|
|
|
if object_path.startswith(self._PS_SERVICE_OP):
|
|
|
|
obj = Service.Service(self._bus, self._new_object,
|
|
|
|
self._del_object, object_path)
|
|
|
|
elif object_path.startswith(self._PS_BUDDY_OP):
|
|
|
|
obj = Buddy.Buddy(self._bus, self._new_object,
|
|
|
|
self._del_object, object_path)
|
|
|
|
elif object_path.startswith(self._PS_ACTIVITY_OP):
|
|
|
|
obj = Activity.Activity(self._bus, self._new_object,
|
|
|
|
self._del_object, object_path)
|
|
|
|
else:
|
|
|
|
raise RuntimeError("Unknown object type")
|
|
|
|
self._objcache.add(obj)
|
|
|
|
return obj
|
2006-06-13 00:31:26 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _del_object(self, object_path):
|
|
|
|
# FIXME
|
|
|
|
pass
|
2006-06-22 05:49:37 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _emit_buddy_appeared_signal(self, object_path):
|
2006-07-23 16:21:00 +02:00
|
|
|
self.emit('buddy-appeared', self._new_object(object_path))
|
2006-06-13 00:31:26 +02:00
|
|
|
return False
|
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _buddy_appeared_cb(self, op):
|
|
|
|
gobject.idle_add(self._emit_buddy_appeared_signal, op)
|
2006-06-13 00:31:26 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _emit_buddy_disappeared_signal(self, object_path):
|
2006-07-23 16:21:00 +02:00
|
|
|
self.emit('buddy-disappeared', self._new_object(object_path))
|
2006-06-09 23:23:42 +02:00
|
|
|
return False
|
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _buddy_disappeared_cb(self, object_path):
|
|
|
|
gobject.idle_add(self._emit_buddy_disappeared_signal, object_path)
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _emit_service_appeared_signal(self, object_path):
|
2006-07-23 16:21:00 +02:00
|
|
|
self.emit('service-appeared', self._new_object(object_path))
|
2006-06-09 23:23:42 +02:00
|
|
|
return False
|
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _service_appeared_cb(self, object_path):
|
|
|
|
gobject.idle_add(self._emit_service_appeared_signal, object_path)
|
2006-06-13 03:33:05 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _emit_service_disappeared_signal(self, object_path):
|
2006-07-23 16:21:00 +02:00
|
|
|
self.emit('service-disappeared', self._new_object(object_path))
|
2006-06-09 23:23:42 +02:00
|
|
|
return False
|
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _service_disappeared_cb(self, object_path):
|
|
|
|
gobject.idle_add(self._emit_service_disappeared_signal, object_path)
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _emit_activity_appeared_signal(self, object_path):
|
2006-07-23 16:21:00 +02:00
|
|
|
self.emit('activity-appeared', self._new_object(object_path))
|
2006-07-19 21:27:37 +02:00
|
|
|
return False
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _activity_appeared_cb(self, object_path):
|
|
|
|
gobject.idle_add(self._emit_activity_appeared_signal, object_path)
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _emit_activity_disappeared_signal(self, object_path):
|
2006-07-23 16:21:00 +02:00
|
|
|
self.emit('activity-disappeared', self._new_object(object_path))
|
2006-06-09 23:23:42 +02:00
|
|
|
return False
|
|
|
|
|
2006-07-19 21:27:37 +02:00
|
|
|
def _activity_disappeared_cb(self, object_path):
|
|
|
|
gobject.idle_add(self._emit_activity_disappeared_signal, object_path)
|
|
|
|
|
2006-08-07 11:08:10 +02:00
|
|
|
def get(self, object_path):
|
|
|
|
return self._new_object(object_path)
|
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_services(self):
|
2006-07-19 21:27:37 +02:00
|
|
|
resp = self._ps.getServices()
|
|
|
|
servs = []
|
|
|
|
for item in resp:
|
|
|
|
servs.append(self._new_object(item))
|
|
|
|
return servs
|
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_services_of_type(self, stype):
|
2006-07-19 21:27:37 +02:00
|
|
|
resp = self._ps.getServicesOfType(stype)
|
|
|
|
servs = []
|
|
|
|
for item in resp:
|
|
|
|
servs.append(self._new_object(item))
|
|
|
|
return servs
|
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_activities(self):
|
2006-07-19 21:27:37 +02:00
|
|
|
resp = self._ps.getActivities()
|
|
|
|
acts = []
|
|
|
|
for item in resp:
|
|
|
|
acts.append(self._new_object(item))
|
|
|
|
return acts
|
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_activity(self, activity_id):
|
2006-07-19 21:27:37 +02:00
|
|
|
try:
|
|
|
|
act_op = self._ps.getActivity(activity_id)
|
2006-07-25 22:52:45 +02:00
|
|
|
except dbus.exceptions.DBusException:
|
2006-07-19 21:27:37 +02:00
|
|
|
return None
|
|
|
|
return self._new_object(act_op)
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_buddies(self):
|
2006-07-19 21:27:37 +02:00
|
|
|
resp = self._ps.getBuddies()
|
|
|
|
buddies = []
|
|
|
|
for item in resp:
|
|
|
|
buddies.append(self._new_object(item))
|
|
|
|
return buddies
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_buddy_by_name(self, name):
|
2006-06-09 23:23:42 +02:00
|
|
|
try:
|
2006-07-19 21:27:37 +02:00
|
|
|
buddy_op = self._ps.getBuddyByName(name)
|
2006-07-25 22:52:45 +02:00
|
|
|
except dbus.exceptions.DBusException:
|
2006-07-19 21:27:37 +02:00
|
|
|
return None
|
|
|
|
return self._new_object(buddy_op)
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_buddy_by_address(self, addr):
|
2006-07-19 21:27:37 +02:00
|
|
|
try:
|
|
|
|
buddy_op = self._ps.getBuddyByAddress(addr)
|
2006-07-25 22:52:45 +02:00
|
|
|
except dbus.exceptions.DBusException:
|
2006-07-19 21:27:37 +02:00
|
|
|
return None
|
|
|
|
return self._new_object(buddy_op)
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def get_owner(self):
|
2006-06-09 23:23:42 +02:00
|
|
|
try:
|
2006-07-19 21:27:37 +02:00
|
|
|
owner_op = self._ps.getOwner()
|
2006-07-25 22:52:45 +02:00
|
|
|
except dbus.exceptions.DBusException:
|
2006-07-19 21:27:37 +02:00
|
|
|
return None
|
2006-07-23 16:21:00 +02:00
|
|
|
return self._new_object(owner_op)
|
2006-07-19 21:27:37 +02:00
|
|
|
|
2006-07-26 02:04:15 +02:00
|
|
|
def share_activity(self, activity, stype, properties={}, address=None, port=-1, domain=u"local"):
|
|
|
|
actid = activity.get_id()
|
|
|
|
if address == None:
|
|
|
|
address = u""
|
|
|
|
serv_op = self._ps.shareActivity(actid, stype, properties, address, port, domain)
|
|
|
|
return self._new_object(serv_op)
|
|
|
|
|
|
|
|
def register_service(self, name, stype, properties={}, address=None, port=-1, domain=u"local"):
|
|
|
|
if address == None:
|
|
|
|
address = u""
|
2006-07-23 06:56:40 +02:00
|
|
|
serv_op = self._ps.registerService(name, stype, properties, address, port, domain)
|
|
|
|
return self._new_object(serv_op)
|
|
|
|
|
2006-08-02 16:27:09 +02:00
|
|
|
def unregister_service(self, service):
|
|
|
|
self._ps.unregisterService(service.object_path())
|
|
|
|
|
2006-07-23 16:21:00 +02:00
|
|
|
def register_service_type(self, stype):
|
|
|
|
self._ps.registerServiceType(stype)
|
|
|
|
|
|
|
|
def unregister_service_type(self, stype):
|
|
|
|
self._ps.unregisterServiceType(stype)
|
2006-08-13 00:28:38 +02:00
|
|
|
|
|
|
|
def start():
|
|
|
|
bus = dbus.SessionBus()
|
|
|
|
ps = dbus.Interface(bus.get_object(DBUS_SERVICE, DBUS_PATH), DBUS_INTERFACE)
|
|
|
|
ps.start()
|