2007-04-20 22:25:37 +02:00
|
|
|
"""An "actor" on the network, whether remote or local"""
|
2007-02-22 20:11:31 +01:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
|
|
|
# Copyright (C) 2007, Collabora Ltd.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
import os
|
2007-02-22 20:11:31 +01:00
|
|
|
import gobject
|
2007-05-11 23:27:36 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
from dbus.gobject_service import ExportedGObject
|
2007-05-14 03:34:15 +02:00
|
|
|
import psutils
|
2007-03-09 22:28:35 +01:00
|
|
|
|
2007-05-24 15:27:36 +02:00
|
|
|
from sugar import env, profile
|
2007-04-12 04:49:14 +02:00
|
|
|
import logging
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
_BUDDY_PATH = "/org/laptop/Sugar/Presence/Buddies/"
|
|
|
|
_BUDDY_INTERFACE = "org.laptop.Sugar.Presence.Buddy"
|
|
|
|
_OWNER_INTERFACE = "org.laptop.Sugar.Presence.Buddy.Owner"
|
|
|
|
|
2007-04-20 18:34:16 +02:00
|
|
|
_PROP_NICK = "nick"
|
|
|
|
_PROP_KEY = "key"
|
|
|
|
_PROP_ICON = "icon"
|
|
|
|
_PROP_CURACT = "current-activity"
|
|
|
|
_PROP_COLOR = "color"
|
|
|
|
_PROP_OWNER = "owner"
|
|
|
|
_PROP_VALID = "valid"
|
|
|
|
|
2007-05-01 05:44:39 +02:00
|
|
|
# Will go away soon
|
|
|
|
_PROP_IP4_ADDRESS = "ip4-address"
|
|
|
|
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger = logging.getLogger('s-p-s.buddy')
|
|
|
|
|
|
|
|
|
2007-05-11 23:27:36 +02:00
|
|
|
class Buddy(ExportedGObject):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Person on the network (tracks properties and shared activites)
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
The Buddy is a collection of metadata describing a particular
|
|
|
|
actor/person on the network. The Buddy object tracks a set of
|
|
|
|
activities which the actor has shared with the presence service.
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
Buddies have a "valid" property which is used to flag Buddies
|
|
|
|
which are no longer reachable. That is, a Buddy may represent
|
|
|
|
a no-longer reachable target on the network.
|
2007-05-25 11:56:41 +02:00
|
|
|
|
|
|
|
The Buddy emits GObject events that the PresenceService uses
|
2007-04-20 22:25:37 +02:00
|
|
|
to track changes in its status.
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
Attributes:
|
2007-05-25 11:56:41 +02:00
|
|
|
|
|
|
|
_activities -- dictionary mapping activity ID to
|
|
|
|
activity.Activity objects
|
2007-05-25 13:26:53 +02:00
|
|
|
handles -- dictionary mapping Telepathy client plugin to
|
|
|
|
contact handle (an integer representing the JID or unique ID);
|
|
|
|
channel-specific handles do not appear here
|
2007-04-20 22:25:37 +02:00
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
__gsignals__ = {
|
2007-05-24 15:27:36 +02:00
|
|
|
'validity-changed':
|
|
|
|
# The buddy's validity changed.
|
|
|
|
# Validity starts off False, and becomes True when the buddy
|
|
|
|
# has a color, a nick and a key.
|
|
|
|
# * the new validity: bool
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [bool]),
|
|
|
|
'property-changed':
|
|
|
|
# One of the buddy's properties has changed.
|
|
|
|
# * those properties that have changed:
|
|
|
|
# dict { str => object }
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object]),
|
|
|
|
'icon-changed':
|
|
|
|
# The buddy's icon changed.
|
|
|
|
# * the bytes of the icon: str
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object]),
|
|
|
|
'disappeared':
|
|
|
|
# The buddy is offline (has no Telepathy handles and is not the
|
|
|
|
# Owner)
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, []),
|
2007-03-02 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-04-20 18:34:16 +02:00
|
|
|
_PROP_KEY : (str, None, None, None,
|
2007-05-25 11:56:41 +02:00
|
|
|
gobject.PARAM_READWRITE |
|
|
|
|
gobject.PARAM_CONSTRUCT_ONLY),
|
2007-04-20 18:34:16 +02:00
|
|
|
_PROP_ICON : (object, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_NICK : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_COLOR : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_CURACT : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_VALID : (bool, None, None, False, gobject.PARAM_READABLE),
|
2007-05-01 05:44:39 +02:00
|
|
|
_PROP_OWNER : (bool, None, None, False, gobject.PARAM_READABLE),
|
|
|
|
_PROP_IP4_ADDRESS : (str, None, None, None, gobject.PARAM_READWRITE)
|
2007-03-02 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
def __init__(self, bus, object_id, **kwargs):
|
2007-05-25 11:56:41 +02:00
|
|
|
"""Initialize the Buddy object
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
bus -- connection to the D-Bus session bus
|
2007-05-25 11:56:41 +02:00
|
|
|
object_id -- the activity's unique identifier
|
2007-04-20 22:25:37 +02:00
|
|
|
kwargs -- used to initialize the object's properties
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
constructs a DBUS "object path" from the _BUDDY_PATH
|
|
|
|
and object_id
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
if not object_id or not isinstance(object_id, int):
|
|
|
|
raise ValueError("object id must be a valid number")
|
|
|
|
|
2007-05-25 13:26:53 +02:00
|
|
|
self._object_path = _BUDDY_PATH + str(object_id)
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
self._activities = {} # Activity ID -> Activity
|
2007-04-22 01:09:59 +02:00
|
|
|
self._activity_sigids = {}
|
2007-02-26 12:28:50 +01:00
|
|
|
self.handles = {} # tp client -> handle
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
self._valid = False
|
|
|
|
self._owner = False
|
|
|
|
self._key = None
|
2007-03-06 17:26:21 +01:00
|
|
|
self._icon = ''
|
2007-03-12 16:31:34 +01:00
|
|
|
self._current_activity = None
|
|
|
|
self._nick = None
|
|
|
|
self._color = None
|
2007-05-01 05:44:39 +02:00
|
|
|
self._ip4_address = None
|
2007-03-02 17:10:44 +01:00
|
|
|
|
2007-04-20 18:40:53 +02:00
|
|
|
if not kwargs.get(_PROP_KEY):
|
2007-03-08 18:51:10 +01:00
|
|
|
raise ValueError("key required")
|
|
|
|
|
2007-05-25 11:56:41 +02:00
|
|
|
_ALLOWED_INIT_PROPS = [_PROP_NICK, _PROP_KEY, _PROP_ICON,
|
|
|
|
_PROP_CURACT, _PROP_COLOR, _PROP_IP4_ADDRESS]
|
2007-04-20 18:34:16 +02:00
|
|
|
for (key, value) in kwargs.items():
|
|
|
|
if key not in _ALLOWED_INIT_PROPS:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Invalid init property '%s'; ignoring..." % key)
|
2007-04-20 18:34:16 +02:00
|
|
|
del kwargs[key]
|
2007-05-11 23:27:36 +02:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
# Set icon after superclass init, because it sends DBus and GObject
|
|
|
|
# signals when set
|
|
|
|
icon_data = None
|
|
|
|
if kwargs.has_key(_PROP_ICON):
|
|
|
|
icon_data = kwargs[_PROP_ICON]
|
|
|
|
del kwargs[_PROP_ICON]
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
ExportedGObject.__init__(self, bus, self._object_path,
|
2007-05-11 23:27:36 +02:00
|
|
|
gobject_properties=kwargs)
|
2007-03-02 17:10:44 +01:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
if icon_data:
|
|
|
|
self.props.icon = icon_data
|
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
def do_get_property(self, pspec):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve current value for the given property specifier
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
pspec -- property specifier with a "name" attribute
|
|
|
|
"""
|
2007-04-20 18:34:16 +02:00
|
|
|
if pspec.name == _PROP_KEY:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._key
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_ICON:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._icon
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_NICK:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._nick
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_COLOR:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._color
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_CURACT:
|
2007-03-02 17:10:44 +01:00
|
|
|
if not self._current_activity:
|
|
|
|
return None
|
|
|
|
if not self._activities.has_key(self._current_activity):
|
|
|
|
return None
|
2007-03-16 17:18:54 +01:00
|
|
|
return self._current_activity
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_VALID:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._valid
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_OWNER:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._owner
|
2007-05-01 05:44:39 +02:00
|
|
|
elif pspec.name == _PROP_IP4_ADDRESS:
|
|
|
|
return self._ip4_address
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
2007-05-25 11:56:41 +02:00
|
|
|
"""Set given property
|
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
pspec -- property specifier with a "name" attribute
|
|
|
|
value -- value to set
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
emits 'icon-changed' signal on icon setting
|
|
|
|
calls _update_validity on all calls
|
|
|
|
"""
|
2007-04-20 18:34:16 +02:00
|
|
|
if pspec.name == _PROP_ICON:
|
2007-03-09 22:28:35 +01:00
|
|
|
if str(value) != self._icon:
|
|
|
|
self._icon = str(value)
|
|
|
|
self.IconChanged(self._icon)
|
|
|
|
self.emit('icon-changed', self._icon)
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_NICK:
|
2007-03-02 17:10:44 +01:00
|
|
|
self._nick = value
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_COLOR:
|
2007-03-02 17:10:44 +01:00
|
|
|
self._color = value
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_CURACT:
|
2007-03-02 17:10:44 +01:00
|
|
|
self._current_activity = value
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_KEY:
|
2007-05-01 05:44:39 +02:00
|
|
|
if self._key:
|
|
|
|
raise RuntimeError("Key already set.")
|
2007-03-02 17:10:44 +01:00
|
|
|
self._key = value
|
2007-05-01 05:44:39 +02:00
|
|
|
elif pspec.name == _PROP_IP4_ADDRESS:
|
|
|
|
self._ip4_address = value
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
self._update_validity()
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
# dbus signals
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="ay")
|
|
|
|
def IconChanged(self, icon_data):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal with icon_data"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="o")
|
|
|
|
def JoinedActivity(self, activity_path):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal when buddy joins activity
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
activity_path -- DBUS path to the activity object
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="o")
|
|
|
|
def LeftActivity(self, activity_path):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal when buddy leaves activity
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
activity_path -- DBUS path to the activity object
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="a{sv}")
|
|
|
|
def PropertyChanged(self, updated):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal when buddy's property changes
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
updated -- updated property-set (dictionary) with the
|
2007-05-25 11:56:41 +02:00
|
|
|
Buddy's property (changed) values. Note: not the
|
2007-04-20 22:25:37 +02:00
|
|
|
full set of properties, just the changes.
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-05-18 16:22:32 +02:00
|
|
|
def add_telepathy_handle(self, tp_client, handle):
|
|
|
|
"""Add a Telepathy handle."""
|
|
|
|
conn = tp_client.get_connection()
|
|
|
|
self.TelepathyHandleAdded(conn.service_name, conn.object_path, handle)
|
|
|
|
self.handles[tp_client] = handle
|
|
|
|
|
2007-05-17 18:41:42 +02:00
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE, signature='sou')
|
|
|
|
def TelepathyHandleAdded(self, tp_conn_name, tp_conn_path, handle):
|
|
|
|
"""Another Telepathy handle has become associated with the buddy.
|
|
|
|
|
|
|
|
This must only be emitted for non-channel-specific handles.
|
|
|
|
|
|
|
|
tp_conn_name -- The bus name at which the Telepathy connection may be
|
|
|
|
found
|
|
|
|
tp_conn_path -- The object path at which the Telepathy connection may
|
|
|
|
be found
|
|
|
|
handle -- The handle of type CONTACT, which is not channel-specific,
|
|
|
|
newly associated with the buddy
|
|
|
|
"""
|
|
|
|
|
2007-05-18 16:22:32 +02:00
|
|
|
def remove_telepathy_handle(self, tp_client, handle):
|
|
|
|
"""Remove a Telepathy handle."""
|
|
|
|
conn = tp_client.get_connection()
|
|
|
|
my_handle = self.handles.get(tp_client, 0)
|
|
|
|
if my_handle == handle:
|
|
|
|
del self.handles[tp_client]
|
|
|
|
self.TelepathyHandleRemoved(conn.service_name, conn.object_path,
|
|
|
|
handle)
|
2007-05-22 17:30:02 +02:00
|
|
|
# the Owner can't disappear - that would be silly
|
|
|
|
if not self.handles and not self._owner:
|
2007-05-18 16:22:32 +02:00
|
|
|
self.emit('disappeared')
|
|
|
|
else:
|
|
|
|
_logger.debug('Telepathy handle %u supposedly removed, but '
|
|
|
|
'my handle on that connection is %u - ignoring',
|
|
|
|
handle, my_handle)
|
|
|
|
|
2007-05-17 18:41:42 +02:00
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE, signature='sou')
|
|
|
|
def TelepathyHandleRemoved(self, tp_conn_name, tp_conn_path, handle):
|
|
|
|
"""A Telepathy handle has ceased to be associated with the buddy,
|
|
|
|
probably because that contact went offline.
|
|
|
|
|
|
|
|
The parameters are the same as for TelepathyHandleAdded.
|
|
|
|
"""
|
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
# dbus methods
|
|
|
|
@dbus.service.method(_BUDDY_INTERFACE,
|
|
|
|
in_signature="", out_signature="ay")
|
|
|
|
def GetIcon(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve Buddy's icon data
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
returns empty string or dbus.ByteArray
|
|
|
|
"""
|
2007-03-02 17:10:44 +01:00
|
|
|
if not self.props.icon:
|
2007-02-22 20:11:31 +01:00
|
|
|
return ""
|
2007-03-03 09:12:27 +01:00
|
|
|
return dbus.ByteArray(self.props.icon)
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_BUDDY_INTERFACE,
|
|
|
|
in_signature="", out_signature="ao")
|
|
|
|
def GetJoinedActivities(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve set of Buddy's joined activities (paths)
|
2007-05-25 11:56:41 +02:00
|
|
|
|
|
|
|
returns list of dbus service paths for the Buddy's joined
|
2007-04-20 22:25:37 +02:00
|
|
|
activities
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
acts = []
|
|
|
|
for act in self.get_joined_activities():
|
2007-04-22 01:20:06 +02:00
|
|
|
if act.props.valid:
|
|
|
|
acts.append(act.object_path())
|
2007-02-22 20:11:31 +01:00
|
|
|
return acts
|
|
|
|
|
|
|
|
@dbus.service.method(_BUDDY_INTERFACE,
|
|
|
|
in_signature="", out_signature="a{sv}")
|
|
|
|
def GetProperties(self):
|
2007-05-25 11:56:41 +02:00
|
|
|
"""Retrieve set of Buddy's properties
|
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
returns dictionary of
|
|
|
|
nick : str(nickname)
|
|
|
|
owner : bool( whether this Buddy is an owner??? )
|
|
|
|
XXX what is the owner flag for?
|
|
|
|
key : str(public-key)
|
|
|
|
color: Buddy's icon colour
|
|
|
|
XXX what type?
|
2007-05-25 11:56:41 +02:00
|
|
|
current-activity: Buddy's current activity_id, or
|
2007-04-20 22:25:37 +02:00
|
|
|
"" if no current activity
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
props = {}
|
2007-04-20 18:34:16 +02:00
|
|
|
props[_PROP_NICK] = self.props.nick
|
|
|
|
props[_PROP_OWNER] = self.props.owner
|
|
|
|
props[_PROP_KEY] = self.props.key
|
|
|
|
props[_PROP_COLOR] = self.props.color
|
2007-05-01 05:44:39 +02:00
|
|
|
|
|
|
|
if self.props.ip4_address:
|
|
|
|
props[_PROP_IP4_ADDRESS] = self.props.ip4_address
|
|
|
|
else:
|
|
|
|
props[_PROP_IP4_ADDRESS] = ""
|
|
|
|
|
2007-04-10 16:59:35 +02:00
|
|
|
if self.props.current_activity:
|
2007-04-20 18:34:16 +02:00
|
|
|
props[_PROP_CURACT] = self.props.current_activity
|
2007-04-10 16:59:35 +02:00
|
|
|
else:
|
2007-04-20 18:34:16 +02:00
|
|
|
props[_PROP_CURACT] = ""
|
2007-02-22 20:11:31 +01:00
|
|
|
return props
|
|
|
|
|
2007-05-17 18:41:42 +02:00
|
|
|
@dbus.service.method(_BUDDY_INTERFACE,
|
|
|
|
in_signature='', out_signature='a(sou)')
|
|
|
|
def GetTelepathyHandles(self):
|
|
|
|
"""Return a list of non-channel-specific Telepathy contact handles
|
|
|
|
associated with this Buddy.
|
|
|
|
|
|
|
|
:Returns:
|
|
|
|
An array of triples (connection well-known bus name, connection
|
|
|
|
object path, handle).
|
|
|
|
"""
|
|
|
|
ret = []
|
|
|
|
for plugin in self.handles:
|
|
|
|
conn = plugin.get_connection()
|
|
|
|
ret.append((str(conn.service_name), conn.object_path,
|
|
|
|
self.handles[plugin]))
|
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
# methods
|
|
|
|
def object_path(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve our dbus.ObjectPath object"""
|
2007-02-22 20:11:31 +01:00
|
|
|
return dbus.ObjectPath(self._object_path)
|
|
|
|
|
2007-04-22 01:09:59 +02:00
|
|
|
def _activity_validity_changed_cb(self, activity, valid):
|
|
|
|
"""Join or leave the activity when its validity changes"""
|
|
|
|
if valid:
|
|
|
|
self.JoinedActivity(activity.object_path())
|
|
|
|
else:
|
|
|
|
self.LeftActivity(activity.object_path())
|
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
def add_activity(self, activity):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Add an activity to the Buddy's set of activities
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
activity -- activity.Activity instance
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
calls JoinedActivity
|
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
actid = activity.props.id
|
2007-02-22 20:11:31 +01:00
|
|
|
if self._activities.has_key(actid):
|
|
|
|
return
|
|
|
|
self._activities[actid] = activity
|
2007-04-22 01:09:59 +02:00
|
|
|
# join/leave activity when it's validity changes
|
2007-05-25 11:56:41 +02:00
|
|
|
sigid = activity.connect("validity-changed",
|
|
|
|
self._activity_validity_changed_cb)
|
2007-04-22 01:09:59 +02:00
|
|
|
self._activity_sigids[actid] = sigid
|
2007-03-08 18:51:10 +01:00
|
|
|
if activity.props.valid:
|
2007-02-22 20:11:31 +01:00
|
|
|
self.JoinedActivity(activity.object_path())
|
|
|
|
|
|
|
|
def remove_activity(self, activity):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Remove the activity from the Buddy's set of activities
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
activity -- activity.Activity instance
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
calls LeftActivity
|
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
actid = activity.props.id
|
2007-02-22 20:11:31 +01:00
|
|
|
if not self._activities.has_key(actid):
|
|
|
|
return
|
2007-04-22 01:09:59 +02:00
|
|
|
activity.disconnect(self._activity_sigids[actid])
|
|
|
|
del self._activity_sigids[actid]
|
2007-02-22 20:11:31 +01:00
|
|
|
del self._activities[actid]
|
2007-03-08 18:51:10 +01:00
|
|
|
if activity.props.valid:
|
2007-02-22 20:11:31 +01:00
|
|
|
self.LeftActivity(activity.object_path())
|
|
|
|
|
|
|
|
def get_joined_activities(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieves list of still-valid activity objects"""
|
2007-02-22 20:11:31 +01:00
|
|
|
acts = []
|
|
|
|
for act in self._activities.values():
|
2007-04-22 01:20:06 +02:00
|
|
|
acts.append(act)
|
2007-02-22 20:11:31 +01:00
|
|
|
return acts
|
|
|
|
|
2007-02-28 17:02:43 +01:00
|
|
|
def set_properties(self, properties):
|
2007-05-25 11:56:41 +02:00
|
|
|
"""Set the given set of properties on the object
|
|
|
|
|
|
|
|
properties -- set of property values to set
|
|
|
|
|
|
|
|
if no change, no events generated
|
|
|
|
if change, generates property-changed and
|
2007-04-20 22:25:37 +02:00
|
|
|
calls _update_validity
|
|
|
|
"""
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = False
|
2007-04-17 19:27:34 +02:00
|
|
|
changed_props = {}
|
2007-04-20 18:34:16 +02:00
|
|
|
if _PROP_NICK in properties.keys():
|
|
|
|
nick = properties[_PROP_NICK]
|
2007-03-09 22:28:35 +01:00
|
|
|
if nick != self._nick:
|
|
|
|
self._nick = nick
|
2007-04-20 18:34:16 +02:00
|
|
|
changed_props[_PROP_NICK] = nick
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = True
|
2007-04-20 18:34:16 +02:00
|
|
|
if _PROP_COLOR in properties.keys():
|
|
|
|
color = properties[_PROP_COLOR]
|
2007-03-09 22:28:35 +01:00
|
|
|
if color != self._color:
|
|
|
|
self._color = color
|
2007-04-20 18:34:16 +02:00
|
|
|
changed_props[_PROP_COLOR] = color
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = True
|
2007-04-20 18:34:16 +02:00
|
|
|
if _PROP_CURACT in properties.keys():
|
|
|
|
curact = properties[_PROP_CURACT]
|
2007-03-09 22:28:35 +01:00
|
|
|
if curact != self._current_activity:
|
|
|
|
self._current_activity = curact
|
2007-04-20 18:34:16 +02:00
|
|
|
changed_props[_PROP_CURACT] = curact
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = True
|
2007-05-01 05:44:39 +02:00
|
|
|
if _PROP_IP4_ADDRESS in properties.keys():
|
|
|
|
ip4addr = properties[_PROP_IP4_ADDRESS]
|
|
|
|
if ip4addr != self._ip4_address:
|
|
|
|
self._ip4_address = ip4addr
|
|
|
|
changed_props[_PROP_IP4_ADDRESS] = ip4addr
|
|
|
|
changed = True
|
2007-03-09 22:28:35 +01:00
|
|
|
|
2007-04-17 19:27:34 +02:00
|
|
|
if not changed or not len(changed_props.keys()):
|
2007-03-09 22:28:35 +01:00
|
|
|
return
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
# Try emitting PropertyChanged before updating validity
|
|
|
|
# to avoid leaking a PropertyChanged signal before the buddy is
|
|
|
|
# actually valid the first time after creation
|
|
|
|
if self._valid:
|
2007-05-17 15:32:34 +02:00
|
|
|
dbus_changed = {}
|
|
|
|
for key, value in changed_props.items():
|
|
|
|
if value:
|
|
|
|
dbus_changed[key] = value
|
|
|
|
else:
|
|
|
|
dbus_changed[key] = ""
|
|
|
|
self.PropertyChanged(dbus_changed)
|
|
|
|
|
2007-04-17 19:27:34 +02:00
|
|
|
self.emit('property-changed', changed_props)
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
self._update_validity()
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
def _update_validity(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Check whether we are now valid
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
validity is True if color, nick and key are non-null
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
emits validity-changed if we have changed validity
|
|
|
|
"""
|
2007-03-02 17:10:44 +01:00
|
|
|
try:
|
|
|
|
old_valid = self._valid
|
|
|
|
if self._color and self._nick and self._key:
|
|
|
|
self._valid = True
|
|
|
|
else:
|
|
|
|
self._valid = False
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
if old_valid != self._valid:
|
|
|
|
self.emit("validity-changed", self._valid)
|
|
|
|
except AttributeError:
|
|
|
|
self._valid = False
|
2007-02-23 17:30:25 +01:00
|
|
|
|
2007-05-24 15:27:36 +02:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
class GenericOwner(Buddy):
|
2007-05-25 11:56:41 +02:00
|
|
|
"""Common functionality for Local User-like objects
|
|
|
|
|
|
|
|
The TestOwner wants to produce something *like* a
|
2007-04-20 22:25:37 +02:00
|
|
|
ShellOwner, but with randomised changes and the like.
|
2007-05-25 11:56:41 +02:00
|
|
|
This class provides the common features for a real
|
2007-04-20 22:25:37 +02:00
|
|
|
local owner and a testing one.
|
|
|
|
"""
|
2007-04-12 04:49:14 +02:00
|
|
|
__gtype_name__ = "GenericOwner"
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
def __init__(self, ps, bus, object_id, **kwargs):
|
2007-05-25 11:56:41 +02:00
|
|
|
"""Initialize the GenericOwner instance
|
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
ps -- presenceservice.PresenceService object
|
2007-05-25 17:21:02 +02:00
|
|
|
bus -- a connection to the D-Bus session bus
|
2007-05-25 11:56:41 +02:00
|
|
|
object_id -- the activity's unique identifier
|
2007-04-20 22:25:37 +02:00
|
|
|
kwargs -- used to initialize the object's properties
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
calls Buddy.__init__
|
|
|
|
"""
|
2007-04-17 04:44:54 +02:00
|
|
|
self._ps = ps
|
2007-05-25 13:24:32 +02:00
|
|
|
self._server = kwargs.pop("server", "olpc.collabora.co.uk")
|
|
|
|
self._key_hash = kwargs.pop("key_hash", None)
|
|
|
|
self._registered = kwargs.pop("registered", False)
|
2007-04-12 04:49:14 +02:00
|
|
|
|
2007-05-17 19:54:26 +02:00
|
|
|
self._ip4_addr_monitor = psutils.IP4AddressMonitor.get_instance()
|
2007-05-25 11:56:41 +02:00
|
|
|
self._ip4_addr_monitor.connect("address-changed",
|
|
|
|
self._ip4_address_changed_cb)
|
2007-05-17 19:54:26 +02:00
|
|
|
if self._ip4_addr_monitor.props.address:
|
|
|
|
kwargs["ip4-address"] = self._ip4_addr_monitor.props.address
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
Buddy.__init__(self, bus, object_id, **kwargs)
|
2007-04-12 04:49:14 +02:00
|
|
|
self._owner = True
|
|
|
|
|
2007-05-01 05:44:39 +02:00
|
|
|
self._bus = dbus.SessionBus()
|
|
|
|
|
|
|
|
def _ip4_address_changed_cb(self, monitor, address):
|
|
|
|
"""Handle IPv4 address change, set property to generate event"""
|
|
|
|
props = {_PROP_IP4_ADDRESS: address}
|
|
|
|
self.set_properties(props)
|
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
def get_registered(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve whether owner has registered with presence server"""
|
2007-04-12 04:49:14 +02:00
|
|
|
return self._registered
|
|
|
|
|
|
|
|
def get_server(self):
|
2007-05-25 13:24:32 +02:00
|
|
|
"""Retrieve XMPP server hostname (used by the server plugin)"""
|
2007-04-12 04:49:14 +02:00
|
|
|
return self._server
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
def get_key_hash(self):
|
2007-05-25 13:24:32 +02:00
|
|
|
"""Retrieve the user's private-key hash (used by the server plugin
|
|
|
|
as a password)
|
|
|
|
"""
|
2007-04-12 04:49:14 +02:00
|
|
|
return self._key_hash
|
|
|
|
|
|
|
|
def set_registered(self, registered):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Customisation point: handle the registration of the owner"""
|
2007-04-12 04:49:14 +02:00
|
|
|
raise RuntimeError("Subclasses must implement")
|
|
|
|
|
2007-05-24 15:27:36 +02:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
class ShellOwner(GenericOwner):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Representation of the local-machine owner using Sugar's Shell
|
2007-05-25 11:56:41 +02:00
|
|
|
|
|
|
|
The ShellOwner uses the Sugar Shell's dbus services to
|
2007-04-20 22:25:37 +02:00
|
|
|
register for updates about the user's profile description.
|
|
|
|
"""
|
2007-04-12 04:49:14 +02:00
|
|
|
__gtype_name__ = "ShellOwner"
|
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
_SHELL_SERVICE = "org.laptop.Shell"
|
|
|
|
_SHELL_OWNER_INTERFACE = "org.laptop.Shell.Owner"
|
|
|
|
_SHELL_PATH = "/org/laptop/Shell"
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
def __init__(self, ps, bus, object_id, test=False):
|
2007-05-25 11:56:41 +02:00
|
|
|
"""Initialize the ShellOwner instance
|
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
ps -- presenceservice.PresenceService object
|
2007-05-25 17:21:02 +02:00
|
|
|
bus -- a connection to the D-Bus session bus
|
2007-05-25 11:56:41 +02:00
|
|
|
object_id -- the activity's unique identifier
|
2007-04-20 22:25:37 +02:00
|
|
|
test -- ignored
|
2007-05-25 11:56:41 +02:00
|
|
|
|
|
|
|
Retrieves initial property values from the profile
|
2007-04-20 22:25:37 +02:00
|
|
|
module. Loads the buddy icon from file as well.
|
|
|
|
XXX note: no error handling on that
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
calls GenericOwner.__init__
|
|
|
|
"""
|
2007-04-12 04:49:14 +02:00
|
|
|
server = profile.get_server()
|
|
|
|
key_hash = profile.get_private_key_hash()
|
|
|
|
registered = profile.get_server_registered()
|
2007-03-01 04:13:27 +01:00
|
|
|
key = profile.get_pubkey()
|
2007-03-02 17:10:44 +01:00
|
|
|
nick = profile.get_nick_name()
|
|
|
|
color = profile.get_color().to_string()
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
icon_file = os.path.join(env.get_profile_path(), "buddy-icon.jpg")
|
|
|
|
f = open(icon_file, "r")
|
|
|
|
icon = f.read()
|
|
|
|
f.close()
|
|
|
|
|
2007-05-25 17:21:02 +02:00
|
|
|
GenericOwner.__init__(self, ps, bus, object_id, key=key,
|
2007-05-25 11:56:41 +02:00
|
|
|
nick=nick, color=color, icon=icon, server=server,
|
|
|
|
key_hash=key_hash, registered=registered)
|
2007-04-12 04:49:14 +02:00
|
|
|
|
2007-05-25 13:06:57 +02:00
|
|
|
# Ask to get notifications on Owner object property changes in the
|
|
|
|
# shell. If it's not currently running, no problem - we'll get the
|
|
|
|
# signals when it does run
|
|
|
|
for (signal, cb) in (('IconChanged', self._icon_changed_cb),
|
|
|
|
('ColorChanged', self._color_changed_cb),
|
|
|
|
('NickChanged', self._nick_changed_cb)):
|
|
|
|
self._bus.add_signal_receiver(cb, signal_name=signal,
|
|
|
|
dbus_interface=self._SHELL_OWNER_INTERFACE,
|
|
|
|
bus_name=self._SHELL_SERVICE,
|
|
|
|
path=self._SHELL_PATH)
|
2007-03-09 22:28:35 +01:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
def set_registered(self, value):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle notification that we have been registered"""
|
2007-04-12 04:49:14 +02:00
|
|
|
if value:
|
|
|
|
profile.set_server_registered()
|
2007-03-09 22:28:35 +01:00
|
|
|
|
|
|
|
def _icon_changed_cb(self, icon):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle icon change, set property to generate event"""
|
2007-03-09 22:28:35 +01:00
|
|
|
self.props.icon = icon
|
|
|
|
|
|
|
|
def _color_changed_cb(self, color):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle color change, set property to generate event"""
|
2007-04-20 18:34:16 +02:00
|
|
|
props = {_PROP_COLOR: color}
|
2007-03-09 22:28:35 +01:00
|
|
|
self.set_properties(props)
|
|
|
|
|
|
|
|
def _nick_changed_cb(self, nick):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle nickname change, set property to generate event"""
|
2007-04-20 18:34:16 +02:00
|
|
|
props = {_PROP_NICK: nick}
|
2007-03-09 22:28:35 +01:00
|
|
|
self.set_properties(props)
|
|
|
|
|
|
|
|
def _cur_activity_changed_cb(self, activity_id):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle current-activity change, set property to generate event
|
2007-05-25 11:56:41 +02:00
|
|
|
|
2007-04-20 22:25:37 +02:00
|
|
|
Filters out local activities (those not in self.activites)
|
2007-05-25 11:56:41 +02:00
|
|
|
because the network users can't join those activities, so
|
2007-04-20 22:25:37 +02:00
|
|
|
the activity_id shared will be None in those cases...
|
|
|
|
"""
|
2007-03-09 22:28:35 +01:00
|
|
|
if not self._activities.has_key(activity_id):
|
2007-03-16 17:18:54 +01:00
|
|
|
# This activity is local-only
|
|
|
|
activity_id = None
|
2007-04-20 18:40:53 +02:00
|
|
|
props = {_PROP_CURACT: activity_id}
|
2007-03-09 22:28:35 +01:00
|
|
|
self.set_properties(props)
|