2007-04-15 06:27:48 +02:00
|
|
|
"""UI interface to a buddy in the presence service"""
|
2007-04-09 20:40:56 +02:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
2006-10-15 01:08:44 +02:00
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library 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
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the
|
|
|
|
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
# Boston, MA 02111-1307, USA.
|
|
|
|
|
2006-07-16 17:25:32 +02:00
|
|
|
import gobject
|
2006-07-23 16:21:00 +02:00
|
|
|
import gtk
|
2006-07-25 22:52:45 +02:00
|
|
|
import dbus
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2007-04-11 04:24:31 +02:00
|
|
|
def _bytes_to_string(bytes):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Convertes an short-int (char) array to a string
|
|
|
|
|
|
|
|
returns string or None for a null sequence
|
|
|
|
"""
|
2007-04-11 04:24:31 +02:00
|
|
|
if len(bytes):
|
2007-04-15 06:27:48 +02:00
|
|
|
# if there's an internal buffer, we could use
|
|
|
|
# ctypes to pull it out without this...
|
2007-04-11 04:24:31 +02:00
|
|
|
return ''.join([chr(item) for item in bytes])
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2006-06-13 00:31:26 +02:00
|
|
|
class Buddy(gobject.GObject):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""UI interface for a Buddy in the presence service
|
|
|
|
|
|
|
|
Each buddy interface tracks a set of activities and properties
|
|
|
|
that can be queried to provide UI controls for manipulating
|
|
|
|
the presence interface.
|
|
|
|
|
|
|
|
Properties Dictionary:
|
|
|
|
'key': public key,
|
|
|
|
'nick': nickname ,
|
|
|
|
'color': color (XXX what format),
|
|
|
|
'current-activity': (XXX dbus path?),
|
|
|
|
'owner': (XXX dbus path?),
|
|
|
|
'icon': (XXX pixel data for an icon?)
|
|
|
|
See __gproperties__
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([])),
|
|
|
|
'joined-activity': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'left-activity': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'property-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
}
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
__gproperties__ = {
|
|
|
|
'key' : (str, None, None, None, gobject.PARAM_READABLE),
|
|
|
|
'icon' : (object, None, None, gobject.PARAM_READABLE),
|
|
|
|
'nick' : (str, None, None, None, gobject.PARAM_READABLE),
|
|
|
|
'color' : (str, None, None, None, gobject.PARAM_READABLE),
|
2007-04-26 22:51:37 +02:00
|
|
|
'current-activity' : (object, None, None, gobject.PARAM_READABLE),
|
2007-04-09 20:40:56 +02:00
|
|
|
'owner' : (bool, None, None, False, gobject.PARAM_READABLE)
|
|
|
|
}
|
|
|
|
|
|
|
|
_PRESENCE_SERVICE = "org.laptop.Sugar.Presence"
|
|
|
|
_BUDDY_DBUS_INTERFACE = "org.laptop.Sugar.Presence.Buddy"
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def __init__(self, bus, new_obj_cb, del_obj_cb, object_path):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Initialise the reference to the buddy
|
|
|
|
|
|
|
|
bus -- dbus bus object
|
|
|
|
new_obj_cb -- callback to call when this buddy joins an activity
|
|
|
|
del_obj_cb -- callback to call when this buddy leaves an activity
|
|
|
|
object_path -- path to the buddy object
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
self._object_path = object_path
|
|
|
|
self._ps_new_object = new_obj_cb
|
|
|
|
self._ps_del_object = del_obj_cb
|
|
|
|
self._properties = {}
|
|
|
|
bobj = bus.get_object(self._PRESENCE_SERVICE, object_path)
|
|
|
|
self._buddy = dbus.Interface(bobj, self._BUDDY_DBUS_INTERFACE)
|
|
|
|
self._buddy.connect_to_signal('IconChanged', self._icon_changed_cb)
|
|
|
|
self._buddy.connect_to_signal('JoinedActivity', self._joined_activity_cb)
|
|
|
|
self._buddy.connect_to_signal('LeftActivity', self._left_activity_cb)
|
|
|
|
self._buddy.connect_to_signal('PropertyChanged', self._property_changed_cb)
|
|
|
|
self._properties = self._get_properties_helper()
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
self._activities = {}
|
2007-04-26 22:51:37 +02:00
|
|
|
activities = self._buddy.GetJoinedActivities()
|
|
|
|
for op in activities:
|
|
|
|
self._activities[op] = self._ps_new_object(op)
|
2007-04-09 20:40:56 +02:00
|
|
|
self._icon = None
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _get_properties_helper(self):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Retrieve the Buddy's property dictionary from the service object
|
|
|
|
"""
|
2007-04-09 20:40:56 +02:00
|
|
|
props = self._buddy.GetProperties()
|
2006-12-04 20:12:24 +01:00
|
|
|
if not props:
|
|
|
|
return {}
|
|
|
|
return props
|
|
|
|
|
2007-04-09 20:40:56 +02:00
|
|
|
def do_get_property(self, pspec):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Retrieve a particular property from our property dictionary
|
|
|
|
|
|
|
|
pspec -- XXX some sort of GTK specifier object with attributes
|
|
|
|
including 'name', 'active' and 'icon-name'
|
|
|
|
"""
|
2007-04-09 20:40:56 +02:00
|
|
|
if pspec.name == "key":
|
|
|
|
return self._properties["key"]
|
|
|
|
elif pspec.name == "nick":
|
|
|
|
return self._properties["nick"]
|
|
|
|
elif pspec.name == "color":
|
|
|
|
return self._properties["color"]
|
|
|
|
elif pspec.name == "current-activity":
|
|
|
|
if not self._properties.has_key("current-activity"):
|
|
|
|
return None
|
|
|
|
curact = self._properties["current-activity"]
|
|
|
|
if not len(curact):
|
|
|
|
return None
|
2007-04-26 22:51:37 +02:00
|
|
|
for activity in self._activities.values():
|
|
|
|
if activity.props.id == curact:
|
|
|
|
return activity
|
|
|
|
return None
|
2007-04-09 20:40:56 +02:00
|
|
|
elif pspec.name == "owner":
|
|
|
|
return self._properties["owner"]
|
|
|
|
elif pspec.name == "icon":
|
|
|
|
if not self._icon:
|
2007-04-11 04:24:31 +02:00
|
|
|
self._icon = _bytes_to_string(self._buddy.GetIcon())
|
2007-04-09 20:40:56 +02:00
|
|
|
return self._icon
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def object_path(self):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Retrieve our dbus object path"""
|
2006-12-04 20:12:24 +01:00
|
|
|
return self._object_path
|
|
|
|
|
2007-04-11 04:24:31 +02:00
|
|
|
def _emit_icon_changed_signal(self, bytes):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Emit GObject signal when icon has changed"""
|
2007-04-11 04:24:31 +02:00
|
|
|
self._icon = _bytes_to_string(bytes)
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('icon-changed')
|
|
|
|
return False
|
|
|
|
|
2007-04-10 21:23:01 +02:00
|
|
|
def _icon_changed_cb(self, icon_data):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Handle dbus signal by emitting a GObject signal"""
|
2007-04-10 21:23:01 +02:00
|
|
|
gobject.idle_add(self._emit_icon_changed_signal, icon_data)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _emit_joined_activity_signal(self, object_path):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Emit activity joined signal with Activity object"""
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('joined-activity', self._ps_new_object(object_path))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _joined_activity_cb(self, object_path):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Handle dbus signal by emitting a GObject signal
|
|
|
|
|
|
|
|
Stores the activity in activities dictionary as well
|
|
|
|
"""
|
2007-04-09 20:40:56 +02:00
|
|
|
if not self._activities.has_key(object_path):
|
|
|
|
self._activities[object_path] = self._ps_new_object(object_path)
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.idle_add(self._emit_joined_activity_signal, object_path)
|
|
|
|
|
|
|
|
def _emit_left_activity_signal(self, object_path):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Emit activity left signal with Activity object
|
|
|
|
|
|
|
|
XXX this calls self._ps_new_object instead of self._ps_del_object,
|
|
|
|
which would seem to be the incorrect callback?
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('left-activity', self._ps_new_object(object_path))
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _left_activity_cb(self, object_path):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Handle dbus signal by emitting a GObject signal
|
|
|
|
|
|
|
|
Also removes from the activities dictionary
|
|
|
|
"""
|
2007-04-09 20:40:56 +02:00
|
|
|
if self._activities.has_key(object_path):
|
|
|
|
del self._activities[object_path]
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.idle_add(self._emit_left_activity_signal, object_path)
|
|
|
|
|
|
|
|
def _handle_property_changed_signal(self, prop_list):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Emit property-changed signal with property dictionary
|
|
|
|
|
|
|
|
Generates a property-changed signal with the results of
|
|
|
|
_get_properties_helper()
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
self._properties = self._get_properties_helper()
|
2007-04-09 20:40:56 +02:00
|
|
|
# FIXME: don't leak unexposed property names
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('property-changed', prop_list)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _property_changed_cb(self, prop_list):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Handle dbus signal by emitting a GObject signal"""
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.idle_add(self._handle_property_changed_signal, prop_list)
|
|
|
|
|
|
|
|
def get_icon_pixbuf(self):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Retrieve Buddy's icon as a GTK pixel buffer
|
|
|
|
|
|
|
|
XXX Why aren't the icons coming in as SVG?
|
|
|
|
"""
|
2007-04-09 20:40:56 +02:00
|
|
|
if self.props.icon and len(self.props.icon):
|
2006-12-04 20:12:24 +01:00
|
|
|
pbl = gtk.gdk.PixbufLoader()
|
|
|
|
icon_data = ""
|
2007-04-09 20:40:56 +02:00
|
|
|
for item in self.props.icon:
|
2007-04-15 06:27:48 +02:00
|
|
|
# XXX this is a slow way to convert the data
|
|
|
|
# under Python 2.5 and below, collect in a
|
|
|
|
# list and then join with "", see
|
|
|
|
# _bytes_to_string in this module
|
2006-12-04 20:12:24 +01:00
|
|
|
icon_data = icon_data + chr(item)
|
|
|
|
pbl.write(icon_data)
|
|
|
|
pbl.close()
|
|
|
|
return pbl.get_pixbuf()
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_joined_activities(self):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Retrieve the set of all activities which this buddy has joined
|
|
|
|
|
|
|
|
Uses the GetJoinedActivities method on the service
|
|
|
|
object to produce object paths, wraps each in an
|
|
|
|
Activity object.
|
|
|
|
|
|
|
|
returns list of presence Activity objects
|
|
|
|
"""
|
2006-12-04 20:12:24 +01:00
|
|
|
try:
|
2007-04-09 20:40:56 +02:00
|
|
|
resp = self._buddy.GetJoinedActivities()
|
2006-12-04 20:12:24 +01:00
|
|
|
except dbus.exceptions.DBusException:
|
|
|
|
return []
|
|
|
|
acts = []
|
|
|
|
for item in resp:
|
|
|
|
acts.append(self._ps_new_object(item))
|
|
|
|
return acts
|