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.
|
|
|
|
|
2008-10-28 14:19:01 +01:00
|
|
|
"""UI interface to a buddy in the presence service
|
|
|
|
|
|
|
|
STABLE.
|
|
|
|
"""
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
import logging
|
2010-07-08 17:20:51 +02:00
|
|
|
from functools import partial
|
2010-06-22 16:31:21 +02:00
|
|
|
|
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
|
2010-06-22 16:31:21 +02:00
|
|
|
import gconf
|
2010-07-08 17:20:51 +02:00
|
|
|
from telepathy.interfaces import ACCOUNT, \
|
|
|
|
CONNECTION, \
|
|
|
|
CONNECTION_INTERFACE_ALIASING, \
|
2010-06-22 16:31:21 +02:00
|
|
|
CONNECTION_INTERFACE_CONTACTS
|
2010-07-08 17:20:51 +02:00
|
|
|
from telepathy.constants import HANDLE_TYPE_CONTACT
|
2010-06-22 16:31:21 +02:00
|
|
|
|
2010-07-12 20:33:19 +02:00
|
|
|
from sugar.presence.util import get_connection_manager
|
|
|
|
|
2010-07-08 17:20:51 +02:00
|
|
|
ACCOUNT_MANAGER_SERVICE = 'org.freedesktop.Telepathy.AccountManager'
|
2010-06-22 16:31:21 +02:00
|
|
|
CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo'
|
|
|
|
|
|
|
|
_logger = logging.getLogger('sugar.presence.buddy')
|
2006-06-09 23:23:42 +02:00
|
|
|
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
class BaseBuddy(gobject.GObject):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""UI interface for a Buddy in the presence service
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
Each buddy interface tracks a set of activities and properties
|
2009-08-25 19:55:48 +02:00
|
|
|
that can be queried to provide UI controls for manipulating
|
2007-04-15 06:27:48 +02:00
|
|
|
the presence interface.
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
Properties Dictionary:
|
2009-08-25 19:55:48 +02:00
|
|
|
'key': public key,
|
|
|
|
'nick': nickname ,
|
|
|
|
'color': color (XXX what format),
|
|
|
|
'current-activity': (XXX dbus path?),
|
|
|
|
'owner': (XXX dbus path?),
|
2007-04-15 06:27:48 +02:00
|
|
|
'icon': (XXX pixel data for an icon?)
|
|
|
|
See __gproperties__
|
|
|
|
"""
|
2009-08-25 21:12:40 +02:00
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
__gtype_name__ = 'PresenceBaseBuddy'
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
__gsignals__ = {
|
2009-08-25 21:12:40 +02:00
|
|
|
'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
|
2006-12-04 20:12:24 +01:00
|
|
|
'joined-activity': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2009-08-25 21:12:40 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-12-04 20:12:24 +01:00
|
|
|
'left-activity': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2009-08-25 21:12:40 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-12-04 20:12:24 +01:00
|
|
|
'property-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2009-08-25 21:12:40 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2006-12-04 20:12:24 +01:00
|
|
|
}
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
def __init__(self):
|
2006-12-04 20:12:24 +01:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
self._key = None
|
2007-04-09 20:40:56 +02:00
|
|
|
self._icon = None
|
2010-06-22 16:31:21 +02:00
|
|
|
self._nick = None
|
|
|
|
self._color = None
|
|
|
|
self._current_activity = None
|
|
|
|
self._owner = False
|
|
|
|
self._ip4_address = None
|
|
|
|
self._tags = None
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2008-01-09 21:21:06 +01:00
|
|
|
def destroy(self):
|
|
|
|
self._icon_changed_signal.remove()
|
|
|
|
self._joined_activity_signal.remove()
|
|
|
|
self._left_activity_signal.remove()
|
|
|
|
self._property_changed_signal.remove()
|
2009-08-25 19:55:48 +02:00
|
|
|
|
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-08-20 21:56:49 +02:00
|
|
|
props = self._buddy.GetProperties(byte_arrays=True)
|
2006-12-04 20:12:24 +01:00
|
|
|
if not props:
|
|
|
|
return {}
|
|
|
|
return props
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
def get_key(self):
|
|
|
|
return self._key
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
def set_key(self, key):
|
|
|
|
self._key = key
|
|
|
|
|
|
|
|
key = gobject.property(type=str, getter=get_key, setter=set_key)
|
|
|
|
|
|
|
|
def get_icon(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
icon = gobject.property(type=str, getter=get_icon)
|
|
|
|
|
|
|
|
def get_nick(self):
|
|
|
|
return self._nick
|
|
|
|
|
|
|
|
def set_nick(self, nick):
|
|
|
|
self._nick = nick
|
|
|
|
|
|
|
|
nick = gobject.property(type=str, getter=get_nick, setter=set_nick)
|
|
|
|
|
|
|
|
def get_color(self):
|
|
|
|
return self._color
|
|
|
|
|
|
|
|
def set_color(self, color):
|
|
|
|
self._color = color
|
|
|
|
|
|
|
|
color = gobject.property(type=str, getter=get_color, setter=set_color)
|
|
|
|
|
|
|
|
def get_current_activity(self):
|
|
|
|
if self._current_activity is None:
|
2007-04-26 22:51:37 +02:00
|
|
|
return None
|
2010-06-22 16:31:21 +02:00
|
|
|
for activity in self._activities.values():
|
|
|
|
if activity.props.id == self._current_activity:
|
|
|
|
return activity
|
|
|
|
return None
|
|
|
|
|
|
|
|
current_activity = gobject.property(type=object, getter=get_current_activity)
|
|
|
|
|
|
|
|
def get_owner(self):
|
|
|
|
return self._owner
|
|
|
|
|
|
|
|
def set_owner(self, owner):
|
|
|
|
self._owner = owner
|
|
|
|
|
|
|
|
owner = gobject.property(type=bool, getter=get_owner, setter=set_owner, default=False)
|
|
|
|
|
|
|
|
def get_ip4_address(self):
|
|
|
|
return self._ip4_address
|
|
|
|
|
|
|
|
def set_ip4_address(self, ip4_address):
|
|
|
|
self._ip4_address = ip4_address
|
|
|
|
|
|
|
|
ip4_address = gobject.property(type=str, getter=get_ip4_address, setter=set_ip4_address)
|
|
|
|
|
|
|
|
def get_tags(self):
|
|
|
|
return self._tags
|
|
|
|
|
|
|
|
def set_tags(self, tags):
|
|
|
|
self._tags = tags
|
|
|
|
|
|
|
|
tags = gobject.property(type=str, getter=get_tags, setter=set_tags)
|
2007-04-09 20:40:56 +02:00
|
|
|
|
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"""
|
2010-07-02 15:14:08 +02:00
|
|
|
return None
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2009-08-24 14:51:00 +02:00
|
|
|
def _emit_icon_changed_signal(self, icon_data):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Emit GObject signal when icon has changed"""
|
2009-08-24 14:51:00 +02:00
|
|
|
self._icon = str(icon_data)
|
2006-12-04 20:12:24 +01:00
|
|
|
self.emit('icon-changed')
|
|
|
|
return False
|
|
|
|
|
2010-06-22 16:31:21 +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
|
|
|
|
2010-06-22 16:31:21 +02: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
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
def __joined_activity_cb(self, object_path):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Handle dbus signal by emitting a GObject signal
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
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
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
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
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
def __left_activity_cb(self, object_path):
|
2007-04-15 06:27:48 +02:00
|
|
|
"""Handle dbus signal by emitting a GObject signal
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
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):
|
2009-08-25 19:55:48 +02:00
|
|
|
"""Emit property-changed signal with property dictionary
|
|
|
|
|
|
|
|
Generates a property-changed signal with the results of
|
2007-04-15 06:27:48 +02:00
|
|
|
_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
|
|
|
|
|
2010-06-22 16:31:21 +02:00
|
|
|
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
|
2009-08-25 19:55:48 +02:00
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
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()
|
2007-08-30 13:00:56 +02:00
|
|
|
pbl.write(self.props.icon)
|
2006-12-04 20:12:24 +01:00
|
|
|
pbl.close()
|
|
|
|
return pbl.get_pixbuf()
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_joined_activities(self):
|
2009-08-25 19:55: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.
|
|
|
|
|
2007-04-15 06:27:48 +02:00
|
|
|
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
|
2010-06-22 16:31:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Buddy(BaseBuddy):
|
|
|
|
__gtype_name__ = 'PresenceBuddy'
|
2010-07-08 17:20:51 +02:00
|
|
|
def __init__(self, account_path, contact_id):
|
|
|
|
_logger.debug('Buddy.__init__')
|
2010-06-22 16:31:21 +02:00
|
|
|
BaseBuddy.__init__(self)
|
|
|
|
|
2010-07-08 17:20:51 +02:00
|
|
|
self._account_path = account_path
|
|
|
|
self.contact_id = contact_id
|
|
|
|
self.contact_handle = None
|
|
|
|
|
|
|
|
_logger.info('KILL_PS Handle the connection going away and coming back')
|
2010-07-12 20:33:19 +02:00
|
|
|
connection_manager = get_connection_manager()
|
|
|
|
connection = connection_manager.get_connection(account_path)
|
2010-07-08 17:20:51 +02:00
|
|
|
|
2010-07-12 20:33:19 +02:00
|
|
|
connection_name = connection.object_path.replace('/', '.')[1:]
|
2010-07-08 17:20:51 +02:00
|
|
|
|
2010-07-12 20:33:19 +02:00
|
|
|
bus = dbus.SessionBus()
|
|
|
|
obj = bus.get_object(connection_name, connection.object_path)
|
2010-07-08 17:20:51 +02:00
|
|
|
handles = obj.RequestHandles(HANDLE_TYPE_CONTACT, [self.contact_id],
|
|
|
|
dbus_interface=CONNECTION)
|
|
|
|
self.contact_handle = handles[0]
|
2010-06-22 16:31:21 +02:00
|
|
|
|
|
|
|
self._get_properties_call = bus.call_async(
|
2010-07-08 17:20:51 +02:00
|
|
|
connection_name,
|
2010-07-12 20:33:19 +02:00
|
|
|
connection.object_path,
|
2010-06-22 16:31:21 +02:00
|
|
|
CONN_INTERFACE_BUDDY_INFO,
|
|
|
|
'GetProperties',
|
|
|
|
'u',
|
2010-07-08 17:20:51 +02:00
|
|
|
(self.contact_handle,),
|
2010-06-22 16:31:21 +02:00
|
|
|
reply_handler=self.__got_properties_cb,
|
|
|
|
error_handler=self.__error_handler_cb,
|
|
|
|
utf8_strings=True,
|
|
|
|
byte_arrays=True)
|
|
|
|
|
|
|
|
self._get_attributes_call = bus.call_async(
|
2010-07-08 17:20:51 +02:00
|
|
|
connection_name,
|
2010-07-12 20:33:19 +02:00
|
|
|
connection.object_path,
|
2010-06-22 16:31:21 +02:00
|
|
|
CONNECTION_INTERFACE_CONTACTS,
|
|
|
|
'GetContactAttributes',
|
|
|
|
'auasb',
|
2010-07-08 17:20:51 +02:00
|
|
|
([self.contact_handle], [CONNECTION_INTERFACE_ALIASING], False),
|
2010-06-22 16:31:21 +02:00
|
|
|
reply_handler=self.__got_attributes_cb,
|
|
|
|
error_handler=self.__error_handler_cb)
|
|
|
|
|
|
|
|
def __got_properties_cb(self, properties):
|
2010-06-30 18:01:58 +02:00
|
|
|
_logger.debug('__got_properties_cb %r', properties)
|
2010-06-22 16:31:21 +02:00
|
|
|
self._get_properties_call = None
|
|
|
|
self._update_properties(properties)
|
|
|
|
|
|
|
|
def __got_attributes_cb(self, attributes):
|
2010-06-30 18:01:58 +02:00
|
|
|
_logger.debug('__got_attributes_cb %r', attributes)
|
2010-06-22 16:31:21 +02:00
|
|
|
self._get_attributes_call = None
|
2010-07-08 17:20:51 +02:00
|
|
|
self._update_attributes(attributes[self.contact_handle])
|
2010-06-22 16:31:21 +02:00
|
|
|
|
|
|
|
def __error_handler_cb(self, error):
|
2010-06-30 18:01:58 +02:00
|
|
|
_logger.debug('__error_handler_cb %r', error)
|
2010-06-22 16:31:21 +02:00
|
|
|
|
|
|
|
def __properties_changed_cb(self, new_props):
|
|
|
|
_logger.debug('%r: Buddy properties changed to %r', self, new_props)
|
|
|
|
self._update_properties(new_props)
|
|
|
|
|
|
|
|
def _update_properties(self, properties):
|
|
|
|
if 'key' in properties:
|
|
|
|
self.props.key = properties['key']
|
|
|
|
if 'icon' in properties:
|
|
|
|
self.props.icon = properties['icon']
|
|
|
|
if 'color' in properties:
|
|
|
|
self.props.color = properties['color']
|
|
|
|
if 'current-activity' in properties:
|
|
|
|
self.props.current_activity = properties['current-activity']
|
|
|
|
if 'owner' in properties:
|
|
|
|
self.props.owner = properties['owner']
|
|
|
|
if 'ip4-address' in properties:
|
|
|
|
self.props.ip4_address = properties['ip4-address']
|
|
|
|
if 'tags' in properties:
|
|
|
|
self.props.tags = properties['tags']
|
|
|
|
|
|
|
|
def _update_attributes(self, attributes):
|
|
|
|
nick_key = CONNECTION_INTERFACE_ALIASING + '/alias'
|
|
|
|
if nick_key in attributes:
|
|
|
|
self.props.nick = attributes[nick_key]
|
|
|
|
|
|
|
|
def do_get_property(self, pspec):
|
2010-06-30 18:01:58 +02:00
|
|
|
if pspec.name == 'nick' and self._get_attributes_call is not None:
|
|
|
|
_logger.debug('%r: Blocking on GetContactAttributes() because someone '
|
|
|
|
'wants property nick', self)
|
|
|
|
self._get_attributes_call.block()
|
|
|
|
elif pspec.name != 'nick' and self._get_properties_call is not None:
|
2010-06-22 16:31:21 +02:00
|
|
|
_logger.debug('%r: Blocking on GetProperties() because someone '
|
|
|
|
'wants property %s', self, pspec.name)
|
|
|
|
self._get_properties_call.block()
|
|
|
|
|
|
|
|
return BaseBuddy.do_get_property(self, pspec)
|
|
|
|
|
|
|
|
|
|
|
|
class Owner(BaseBuddy):
|
|
|
|
|
|
|
|
__gtype_name__ = 'PresenceOwner'
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
BaseBuddy.__init__(self)
|
|
|
|
|
|
|
|
client = gconf.client_get_default()
|
|
|
|
self.props.nick = client.get_string("/desktop/sugar/user/nick")
|
|
|
|
self.props.color = client.get_string("/desktop/sugar/user/color")
|