2007-04-21 03:09:51 +02:00
|
|
|
"""Telepathy-python presence server interface/implementation plugin"""
|
2007-02-26 01:24:48 +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-05-24 12:16:37 +02:00
|
|
|
# Standard library
|
2007-02-26 01:24:48 +01:00
|
|
|
import logging
|
2007-02-28 04:38:28 +01:00
|
|
|
import os
|
2007-05-24 12:16:37 +02:00
|
|
|
import sys
|
2007-05-24 12:13:58 +02:00
|
|
|
try:
|
|
|
|
# Python >= 2.5
|
|
|
|
from hashlib import md5
|
|
|
|
except ImportError:
|
|
|
|
from md5 import new as md5
|
|
|
|
|
2007-05-24 12:16:37 +02:00
|
|
|
# Other libraries
|
|
|
|
import dbus
|
|
|
|
import gobject
|
|
|
|
import gtk
|
2007-05-24 12:05:52 +02:00
|
|
|
from telepathy.client import (ConnectionManager, ManagerRegistry, Connection,
|
|
|
|
Channel)
|
|
|
|
from telepathy.interfaces import (CONN_MGR_INTERFACE, CONN_INTERFACE,
|
|
|
|
CHANNEL_TYPE_CONTACT_LIST, CHANNEL_INTERFACE_GROUP,
|
|
|
|
CONN_INTERFACE_ALIASING, CONN_INTERFACE_AVATARS, CONN_INTERFACE_PRESENCE,
|
|
|
|
CHANNEL_TYPE_TEXT, CHANNEL_TYPE_STREAMED_MEDIA, PROPERTIES_INTERFACE)
|
|
|
|
from telepathy.constants import (HANDLE_TYPE_CONTACT,
|
|
|
|
HANDLE_TYPE_LIST, HANDLE_TYPE_CONTACT, HANDLE_TYPE_ROOM,
|
|
|
|
CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED,
|
|
|
|
CONNECTION_STATUS_CONNECTING,
|
2007-05-22 17:45:20 +02:00
|
|
|
CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED,
|
2007-05-24 14:04:00 +02:00
|
|
|
CONNECTION_STATUS_REASON_NONE_SPECIFIED,
|
2007-05-22 17:45:20 +02:00
|
|
|
PROPERTY_FLAG_WRITE)
|
2007-05-24 12:16:37 +02:00
|
|
|
from sugar import util
|
|
|
|
|
|
|
|
# Presence Service local modules
|
|
|
|
from buddyiconcache import BuddyIconCache
|
|
|
|
import psutils
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-02-27 23:51:53 +01:00
|
|
|
CONN_INTERFACE_BUDDY_INFO = 'org.laptop.Telepathy.BuddyInfo'
|
2007-03-14 15:59:30 +01:00
|
|
|
CONN_INTERFACE_ACTIVITY_PROPERTIES = 'org.laptop.Telepathy.ActivityProperties'
|
2007-02-27 23:51:53 +01:00
|
|
|
|
2007-02-26 19:18:06 +01:00
|
|
|
_PROTOCOL = "jabber"
|
2007-05-24 13:14:55 +02:00
|
|
|
_OBJ_PATH_PREFIX = "/org/freedesktop/Telepathy/Connection/gabble/jabber/"
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger = logging.getLogger('s-p-s.server_plugin')
|
|
|
|
|
2007-02-28 04:38:28 +01:00
|
|
|
|
2007-03-09 22:29:49 +01:00
|
|
|
def _buddy_icon_save_cb(buf, data):
|
|
|
|
data[0] += buf
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _get_buddy_icon_at_size(icon, maxw, maxh, maxsize):
|
|
|
|
loader = gtk.gdk.PixbufLoader()
|
|
|
|
loader.write(icon)
|
|
|
|
loader.close()
|
|
|
|
unscaled_pixbuf = loader.get_pixbuf()
|
|
|
|
del loader
|
|
|
|
|
|
|
|
pixbuf = unscaled_pixbuf.scale_simple(maxw, maxh, gtk.gdk.INTERP_BILINEAR)
|
|
|
|
del unscaled_pixbuf
|
2007-02-28 04:38:28 +01:00
|
|
|
|
2007-03-09 22:29:49 +01:00
|
|
|
data = [""]
|
2007-02-28 04:38:28 +01:00
|
|
|
quality = 90
|
|
|
|
img_size = maxsize + 1
|
|
|
|
while img_size > maxsize:
|
2007-03-09 22:29:49 +01:00
|
|
|
del data
|
|
|
|
data = [""]
|
2007-05-24 12:29:22 +02:00
|
|
|
pixbuf.save_to_callback(_buddy_icon_save_cb, "jpeg",
|
|
|
|
{"quality":"%d" % quality}, data)
|
2007-02-28 04:38:28 +01:00
|
|
|
quality -= 10
|
2007-03-09 22:29:49 +01:00
|
|
|
img_size = len(data[0])
|
2007-02-28 04:38:28 +01:00
|
|
|
del pixbuf
|
|
|
|
|
|
|
|
if img_size > maxsize:
|
2007-03-09 22:29:49 +01:00
|
|
|
del data
|
2007-02-28 04:38:28 +01:00
|
|
|
raise RuntimeError("could not size image less than %d bytes" % maxsize)
|
|
|
|
|
2007-03-09 22:29:49 +01:00
|
|
|
return str(data[0])
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-02-28 04:38:28 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
class ServerPlugin(gobject.GObject):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Telepathy-python-based presence server interface
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
The ServerPlugin instance translates network events from
|
|
|
|
Telepathy Python into GObject events. It provides direct
|
|
|
|
python calls to perform the required network operations
|
2007-04-21 03:09:51 +02:00
|
|
|
to implement the PresenceService.
|
|
|
|
"""
|
2007-02-26 01:24:48 +01:00
|
|
|
__gsignals__ = {
|
2007-05-24 13:00:34 +02:00
|
|
|
'contact-online':
|
|
|
|
# Contact has come online and we've discovered all their buddy
|
|
|
|
# properties.
|
|
|
|
# args: contact handle: int; dict {name: str => property: object}
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object, object]),
|
|
|
|
'contact-offline':
|
|
|
|
# Contact has gone offline.
|
|
|
|
# args: contact handle
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object]),
|
|
|
|
'status':
|
|
|
|
# Connection status changed.
|
|
|
|
# args: status, reason as for Telepathy StatusChanged
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [int, int]),
|
|
|
|
'avatar-updated':
|
|
|
|
# Contact's avatar has changed
|
|
|
|
# args: contact handle: int; icon data: str
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object, object]),
|
|
|
|
'buddy-properties-changed':
|
|
|
|
# OLPC buddy properties changed; as for PropertiesChanged
|
|
|
|
# args:
|
|
|
|
# contact handle: int
|
|
|
|
# properties: dict {name: str => property: object}
|
|
|
|
# FIXME: are these all the properties or just those that changed?
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object, object]),
|
|
|
|
'buddy-activities-changed':
|
|
|
|
# OLPC activities changed
|
|
|
|
# args:
|
|
|
|
# contact handle: int
|
|
|
|
# activity IDs: list of str
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object, object]),
|
|
|
|
'activity-invitation':
|
|
|
|
# We were invited to join an activity
|
|
|
|
# args: activity ID: str
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object]),
|
|
|
|
'private-invitation':
|
|
|
|
# We were invited to join a chat or a media call
|
|
|
|
# args: channel object path
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object]),
|
|
|
|
'activity-properties-changed':
|
|
|
|
# An activity's properties changed; as for
|
|
|
|
# ActivityPropertiesChanged
|
|
|
|
# args: activity ID: str; properties: dict { str => object }
|
|
|
|
# FIXME: are these all the properties or just those that changed?
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object, object]),
|
|
|
|
'activity-shared':
|
|
|
|
# share_activity() succeeded
|
|
|
|
# args:
|
|
|
|
# activity ID: str
|
|
|
|
# channel: telepathy.client.Channel, or None on failure
|
|
|
|
# error: None, or Exception on failure
|
|
|
|
# userdata as passed to share_activity
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object, object, object, object]),
|
|
|
|
'activity-joined':
|
|
|
|
# join_activity() succeeded
|
|
|
|
# args: as for activity-shared
|
|
|
|
(gobject.SIGNAL_RUN_FIRST, None, [object, object, object, object]),
|
2007-02-26 01:24:48 +01:00
|
|
|
}
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-03-09 22:29:49 +01:00
|
|
|
def __init__(self, registry, owner):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Initialize the ServerPlugin instance
|
|
|
|
|
|
|
|
registry -- telepathy.client.ManagerRegistry from the
|
|
|
|
PresenceService, used to find the "gabble" connection
|
2007-04-21 03:09:51 +02:00
|
|
|
manager in this case...
|
2007-05-24 12:29:22 +02:00
|
|
|
owner -- presence.buddy.GenericOwner instance (normally a
|
2007-04-21 03:09:51 +02:00
|
|
|
presence.buddy.ShellOwner instance)
|
|
|
|
"""
|
2007-02-26 01:24:48 +01:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2007-05-18 16:21:36 +02:00
|
|
|
self._conn = None
|
2007-02-26 17:18:52 +01:00
|
|
|
self._icon_cache = BuddyIconCache()
|
|
|
|
|
2007-05-14 08:46:51 +02:00
|
|
|
self._registry = registry
|
2007-03-01 04:13:27 +01:00
|
|
|
self._online_contacts = {} # handle -> jid
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
# activity id -> handle
|
|
|
|
self._activities = {}
|
|
|
|
# (activity_id, handle of the activity channel)
|
|
|
|
self._joined_activities = []
|
2007-03-09 22:29:49 +01:00
|
|
|
|
|
|
|
self._owner = owner
|
2007-05-24 13:14:55 +02:00
|
|
|
self._owner.connect("property-changed",
|
|
|
|
self._owner_property_changed_cb)
|
2007-03-09 22:29:49 +01:00
|
|
|
self._owner.connect("icon-changed", self._owner_icon_changed_cb)
|
2007-05-22 17:43:45 +02:00
|
|
|
self.self_handle = None
|
2007-03-09 22:29:49 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
self._account = self._get_account_info()
|
2007-05-11 23:25:44 +02:00
|
|
|
self._conn_status = CONNECTION_STATUS_DISCONNECTED
|
2007-05-14 19:58:26 +02:00
|
|
|
self._reconnect_id = 0
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
# Monitor IPv4 address as an indicator of the network connection
|
|
|
|
self._ip4am = psutils.IP4AddressMonitor.get_instance()
|
|
|
|
self._ip4am.connect('address-changed', self._ip4_address_changed_cb)
|
|
|
|
|
2007-05-22 17:43:06 +02:00
|
|
|
self._publish_channel = None
|
|
|
|
self._subscribe_channel = None
|
|
|
|
self._subscribe_members = set()
|
|
|
|
self._subscribe_local_pending = set()
|
|
|
|
self._subscribe_remote_pending = set()
|
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
def _ip4_address_changed_cb(self, ip4am, address):
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("::: IP4 address now %s", address)
|
2007-05-14 03:34:15 +02:00
|
|
|
if address:
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("::: valid IP4 address, conn_status %s",
|
|
|
|
self._conn_status)
|
2007-05-14 03:34:15 +02:00
|
|
|
if self._conn_status == CONNECTION_STATUS_DISCONNECTED:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("::: will connect")
|
2007-05-14 03:34:15 +02:00
|
|
|
self.start()
|
|
|
|
else:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("::: invalid IP4 address, will disconnect")
|
2007-05-14 03:34:15 +02:00
|
|
|
self.cleanup()
|
2007-03-03 09:15:18 +01:00
|
|
|
|
2007-03-09 22:29:49 +01:00
|
|
|
def _owner_property_changed_cb(self, owner, properties):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Local user's configuration properties have changed
|
|
|
|
|
|
|
|
owner -- the Buddy object for the local user
|
|
|
|
properties -- set of updated properties
|
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
calls:
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
_set_self_current_activity current-activity
|
|
|
|
_set_self_alias nick
|
|
|
|
_set_self_olpc_properties color
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
depending on which properties are present in the
|
2007-04-21 03:09:51 +02:00
|
|
|
set of properties.
|
|
|
|
"""
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Owner properties changed: %s", properties)
|
2007-04-12 22:48:40 +02:00
|
|
|
|
|
|
|
if properties.has_key("current-activity"):
|
|
|
|
self._set_self_current_activity()
|
|
|
|
|
|
|
|
if properties.has_key("nick"):
|
|
|
|
self._set_self_alias()
|
2007-05-15 04:48:53 +02:00
|
|
|
# Hack; send twice to make sure the server gets it
|
|
|
|
gobject.timeout_add(1000, self._set_self_alias)
|
2007-04-12 22:48:40 +02:00
|
|
|
|
2007-05-01 05:44:39 +02:00
|
|
|
if properties.has_key("color") or properties.has_key("ip4-address"):
|
2007-05-14 22:17:33 +02:00
|
|
|
if self._conn_status == CONNECTION_STATUS_CONNECTED:
|
|
|
|
self._set_self_olpc_properties()
|
2007-03-09 22:29:49 +01:00
|
|
|
|
|
|
|
def _owner_icon_changed_cb(self, owner, icon):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Owner has changed their icon, forward to network"""
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Owner icon changed to size %d", len(str(icon)))
|
2007-04-12 22:48:40 +02:00
|
|
|
self._set_self_avatar(icon)
|
2007-03-09 22:29:49 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
def _get_account_info(self):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Retrieve metadata dictionary describing this account
|
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
returns dictionary with:
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
server : server url from owner
|
2007-04-21 03:09:51 +02:00
|
|
|
account : printable-ssh-key-hash@server
|
|
|
|
password : ssh-key-hash
|
2007-05-24 12:29:22 +02:00
|
|
|
register : whether to register (i.e. whether not yet
|
2007-04-21 03:09:51 +02:00
|
|
|
registered)
|
|
|
|
"""
|
2007-02-27 19:08:17 +01:00
|
|
|
account_info = {}
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
account_info['server'] = self._owner.get_server()
|
2007-02-27 19:08:17 +01:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
khash = util.printable_hash(util._sha_data(self._owner.props.key))
|
2007-02-26 01:24:48 +01:00
|
|
|
account_info['account'] = "%s@%s" % (khash, account_info['server'])
|
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
account_info['password'] = self._owner.get_key_hash()
|
|
|
|
account_info['register'] = not self._owner.get_registered()
|
|
|
|
|
|
|
|
print "ACCT: %s" % account_info
|
2007-02-26 01:24:48 +01:00
|
|
|
return account_info
|
|
|
|
|
2007-02-26 19:18:06 +01:00
|
|
|
def _find_existing_connection(self):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Try to find an existing Telepathy connection to this server
|
|
|
|
|
|
|
|
filters the set of connections from
|
2007-04-21 03:09:51 +02:00
|
|
|
telepathy.client.Connection.get_connections
|
2007-05-24 12:29:22 +02:00
|
|
|
to find a connection using our protocol with the
|
|
|
|
"self handle" of that connection being a handle
|
2007-04-21 03:09:51 +02:00
|
|
|
which matches our account (see _get_account_info)
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
returns connection or None
|
|
|
|
"""
|
2007-02-26 19:18:06 +01:00
|
|
|
our_name = self._account['account']
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
# Search existing connections, if any, that we might be able to use
|
|
|
|
connections = Connection.get_connections()
|
|
|
|
conn = None
|
|
|
|
for item in connections:
|
2007-05-24 13:14:55 +02:00
|
|
|
if not item.object_path.startswith(_OBJ_PATH_PREFIX):
|
2007-02-26 01:24:48 +01:00
|
|
|
continue
|
2007-02-26 19:18:06 +01:00
|
|
|
if item[CONN_INTERFACE].GetProtocol() != _PROTOCOL:
|
2007-02-26 01:24:48 +01:00
|
|
|
continue
|
|
|
|
if item[CONN_INTERFACE].GetStatus() == CONNECTION_STATUS_CONNECTED:
|
2007-05-24 13:14:55 +02:00
|
|
|
test_handle = item[CONN_INTERFACE].RequestHandles(
|
|
|
|
HANDLE_TYPE_CONTACT, [our_name])[0]
|
2007-02-26 01:24:48 +01:00
|
|
|
if item[CONN_INTERFACE].GetSelfHandle() != test_handle:
|
|
|
|
continue
|
2007-02-26 19:18:06 +01:00
|
|
|
return item
|
|
|
|
return None
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-03-06 17:15:55 +01:00
|
|
|
def get_connection(self):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Retrieve our telepathy.client.Connection object"""
|
2007-03-06 17:15:55 +01:00
|
|
|
return self._conn
|
|
|
|
|
2007-02-26 21:38:50 +01:00
|
|
|
def _init_connection(self):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Set up our connection
|
|
|
|
|
|
|
|
if there is no existing connection
|
2007-04-21 03:09:51 +02:00
|
|
|
(_find_existing_connection returns None)
|
|
|
|
produce a new connection with our protocol for our
|
|
|
|
account.
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
if there is an existing connection, reuse it by
|
2007-04-21 03:09:51 +02:00
|
|
|
registering for various of events on it.
|
|
|
|
"""
|
2007-02-26 19:18:06 +01:00
|
|
|
conn = self._find_existing_connection()
|
2007-02-26 01:24:48 +01:00
|
|
|
if not conn:
|
2007-02-26 04:45:16 +01:00
|
|
|
acct = self._account.copy()
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
# Create a new connection
|
2007-05-14 08:46:51 +02:00
|
|
|
gabble_mgr = self._registry.GetManager('gabble')
|
2007-05-24 13:14:55 +02:00
|
|
|
name, path = gabble_mgr[CONN_MGR_INTERFACE].RequestConnection(
|
|
|
|
_PROTOCOL, acct)
|
2007-02-26 04:45:16 +01:00
|
|
|
conn = Connection(name, path)
|
2007-02-26 19:18:06 +01:00
|
|
|
del acct
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
conn[CONN_INTERFACE].connect_to_signal('StatusChanged',
|
|
|
|
self._status_changed_cb)
|
|
|
|
conn[CONN_INTERFACE].connect_to_signal('NewChannel',
|
|
|
|
self._new_channel_cb)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
# hack
|
|
|
|
conn._valid_interfaces.add(CONN_INTERFACE_PRESENCE)
|
2007-03-06 17:22:33 +01:00
|
|
|
conn._valid_interfaces.add(CONN_INTERFACE_BUDDY_INFO)
|
2007-03-14 15:59:30 +01:00
|
|
|
conn._valid_interfaces.add(CONN_INTERFACE_ACTIVITY_PROPERTIES)
|
2007-03-06 17:22:33 +01:00
|
|
|
conn._valid_interfaces.add(CONN_INTERFACE_AVATARS)
|
|
|
|
conn._valid_interfaces.add(CONN_INTERFACE_ALIASING)
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
conn[CONN_INTERFACE_PRESENCE].connect_to_signal('PresenceUpdate',
|
2007-03-01 04:13:27 +01:00
|
|
|
self._presence_update_cb)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
self._conn = conn
|
|
|
|
status = self._conn[CONN_INTERFACE].GetStatus()
|
2007-05-24 14:04:00 +02:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
if status == CONNECTION_STATUS_DISCONNECTED:
|
2007-05-24 14:04:00 +02:00
|
|
|
def connect_reply():
|
|
|
|
_logger.debug('Connect() succeeded')
|
|
|
|
def connect_error(e):
|
|
|
|
_logger.debug('Connect() failed: %s', e)
|
|
|
|
|
|
|
|
self._conn[CONN_INTERFACE].Connect(reply_handler=connect_reply,
|
|
|
|
error_handler=connect_error)
|
|
|
|
|
|
|
|
self._handle_connection_status_change(status,
|
|
|
|
CONNECTION_STATUS_REASON_NONE_SPECIFIED)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
def _request_list_channel(self, name):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Request a contact-list channel from Telepathy
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
name -- publish/subscribe, for the type of channel
|
|
|
|
"""
|
2007-02-26 01:24:48 +01:00
|
|
|
handle = self._conn[CONN_INTERFACE].RequestHandles(
|
2007-05-24 12:05:52 +02:00
|
|
|
HANDLE_TYPE_LIST, [name])[0]
|
2007-02-26 01:24:48 +01:00
|
|
|
chan_path = self._conn[CONN_INTERFACE].RequestChannel(
|
2007-05-24 12:05:52 +02:00
|
|
|
CHANNEL_TYPE_CONTACT_LIST, HANDLE_TYPE_LIST,
|
2007-02-26 01:24:48 +01:00
|
|
|
handle, True)
|
2007-05-23 18:42:31 +02:00
|
|
|
channel = Channel(self._conn.service_name, chan_path)
|
2007-02-26 01:24:48 +01:00
|
|
|
# hack
|
|
|
|
channel._valid_interfaces.add(CHANNEL_INTERFACE_GROUP)
|
|
|
|
return channel
|
|
|
|
|
|
|
|
def _connected_cb(self):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Callback on successful connection to a server
|
2007-04-21 03:09:51 +02:00
|
|
|
"""
|
2007-05-11 23:25:44 +02:00
|
|
|
|
2007-02-27 19:08:17 +01:00
|
|
|
if self._account['register']:
|
|
|
|
# we successfully register this account
|
2007-05-08 22:28:55 +02:00
|
|
|
self._owner.set_registered(True)
|
2007-02-27 19:08:17 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
# the group of contacts who may receive your presence
|
|
|
|
publish = self._request_list_channel('publish')
|
2007-05-16 12:39:16 +02:00
|
|
|
self._publish_channel = publish
|
|
|
|
publish[CHANNEL_INTERFACE_GROUP].connect_to_signal('MembersChanged',
|
|
|
|
self._publish_members_changed_cb)
|
|
|
|
publish_handles, local_pending, remote_pending = \
|
|
|
|
publish[CHANNEL_INTERFACE_GROUP].GetAllMembers()
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
# the group of contacts for whom you wish to receive presence
|
|
|
|
subscribe = self._request_list_channel('subscribe')
|
2007-05-16 12:39:16 +02:00
|
|
|
self._subscribe_channel = subscribe
|
|
|
|
subscribe[CHANNEL_INTERFACE_GROUP].connect_to_signal('MembersChanged',
|
|
|
|
self._subscribe_members_changed_cb)
|
|
|
|
subscribe_handles, subscribe_lp, subscribe_rp = \
|
|
|
|
subscribe[CHANNEL_INTERFACE_GROUP].GetAllMembers()
|
|
|
|
self._subscribe_members = set(subscribe_handles)
|
|
|
|
self._subscribe_local_pending = set(subscribe_lp)
|
|
|
|
self._subscribe_remote_pending = set(subscribe_rp)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
if local_pending:
|
|
|
|
# accept pending subscriptions
|
|
|
|
publish[CHANNEL_INTERFACE_GROUP].AddMembers(local_pending, '')
|
|
|
|
|
2007-05-22 17:43:45 +02:00
|
|
|
self.self_handle = self._conn[CONN_INTERFACE].GetSelfHandle()
|
|
|
|
self._online_contacts[self.self_handle] = self._account['account']
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
# request subscriptions from people subscribed to us if we're not
|
|
|
|
# subscribed to them
|
2007-05-14 03:34:15 +02:00
|
|
|
not_subscribed = list(set(publish_handles) - set(subscribe_handles))
|
|
|
|
subscribe[CHANNEL_INTERFACE_GROUP].AddMembers(not_subscribed, '')
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-03-01 04:13:27 +01:00
|
|
|
if CONN_INTERFACE_BUDDY_INFO not in self._conn.get_valid_interfaces():
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug('OLPC information not available')
|
2007-05-11 23:25:44 +02:00
|
|
|
return False
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].connect_to_signal(
|
|
|
|
'PropertiesChanged', self._buddy_properties_changed_cb)
|
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].connect_to_signal(
|
|
|
|
'ActivitiesChanged', self._buddy_activities_changed_cb)
|
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].connect_to_signal(
|
|
|
|
'CurrentActivityChanged',
|
2007-04-12 22:48:40 +02:00
|
|
|
self._buddy_current_activity_changed_cb)
|
|
|
|
|
|
|
|
self._conn[CONN_INTERFACE_AVATARS].connect_to_signal('AvatarUpdated',
|
|
|
|
self._avatar_updated_cb)
|
|
|
|
self._conn[CONN_INTERFACE_ALIASING].connect_to_signal('AliasesChanged',
|
|
|
|
self._alias_changed_cb)
|
2007-05-24 13:14:55 +02:00
|
|
|
self._conn[CONN_INTERFACE_ACTIVITY_PROPERTIES].connect_to_signal(
|
|
|
|
'ActivityPropertiesChanged',
|
2007-04-12 22:48:40 +02:00
|
|
|
self._activity_properties_changed_cb)
|
|
|
|
|
|
|
|
# Set initial buddy properties, avatar, and activities
|
2007-04-17 04:39:07 +02:00
|
|
|
self._set_self_olpc_properties()
|
2007-04-12 22:48:40 +02:00
|
|
|
self._set_self_alias()
|
2007-05-15 04:48:53 +02:00
|
|
|
# Hack; send twice to make sure the server gets it
|
|
|
|
gobject.timeout_add(1000, self._set_self_alias)
|
2007-04-12 22:48:40 +02:00
|
|
|
self._set_self_activities()
|
|
|
|
self._set_self_current_activity()
|
|
|
|
self._set_self_avatar()
|
2007-02-27 23:51:53 +01:00
|
|
|
|
2007-05-16 12:39:16 +02:00
|
|
|
# Request presence for everyone we're subscribed to
|
2007-05-14 03:34:15 +02:00
|
|
|
self._conn[CONN_INTERFACE_PRESENCE].RequestPresence(subscribe_handles)
|
2007-05-11 23:25:44 +02:00
|
|
|
return True
|
2007-04-12 22:48:40 +02:00
|
|
|
|
|
|
|
def _set_self_avatar_cb(self, token):
|
|
|
|
self._icon_cache.set_avatar(hash, token)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-04-12 22:48:40 +02:00
|
|
|
def _set_self_avatar(self, icon_data=None):
|
|
|
|
if not icon_data:
|
|
|
|
icon_data = self._owner.props.icon
|
2007-03-07 13:15:38 +01:00
|
|
|
|
2007-05-24 12:13:58 +02:00
|
|
|
m = md5()
|
2007-04-26 18:25:40 +02:00
|
|
|
m.update(icon_data)
|
|
|
|
hash = m.hexdigest()
|
2007-03-05 12:52:29 +01:00
|
|
|
|
|
|
|
self_handle = self._conn[CONN_INTERFACE].GetSelfHandle()
|
2007-05-24 13:14:55 +02:00
|
|
|
token = self._conn[CONN_INTERFACE_AVATARS].GetAvatarTokens(
|
|
|
|
[self_handle])[0]
|
2007-03-05 12:52:29 +01:00
|
|
|
|
|
|
|
if self._icon_cache.check_avatar(hash, token):
|
|
|
|
# avatar is up to date
|
|
|
|
return
|
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
types, minw, minh, maxw, maxh, maxsize = \
|
|
|
|
self._conn[CONN_INTERFACE_AVATARS].GetAvatarRequirements()
|
2007-03-05 12:52:29 +01:00
|
|
|
if not "image/jpeg" in types:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("server does not accept JPEG format avatars.")
|
2007-03-05 12:52:29 +01:00
|
|
|
return
|
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
img_data = _get_buddy_icon_at_size(icon_data, min(maxw, 96),
|
|
|
|
min(maxh, 96), maxsize)
|
2007-04-12 22:48:40 +02:00
|
|
|
self._conn[CONN_INTERFACE_AVATARS].SetAvatar(img_data, "image/jpeg",
|
|
|
|
reply_handler=self._set_self_avatar_cb,
|
2007-05-24 12:11:03 +02:00
|
|
|
error_handler=lambda e: self._log_error_cb("setting avatar", e))
|
2007-03-05 12:52:29 +01:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
def _join_activity_channel_props_set_cb(self, activity_id, signal, handle,
|
|
|
|
channel, userdata):
|
2007-04-13 19:11:59 +02:00
|
|
|
self._joined_activities.append((activity_id, handle))
|
|
|
|
self._set_self_activities()
|
|
|
|
self.emit(signal, activity_id, channel, None, userdata)
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
def _join_activity_channel_props_listed_cb(self, activity_id, signal,
|
|
|
|
handle, channel, userdata,
|
|
|
|
props, prop_specs):
|
2007-05-22 17:45:20 +02:00
|
|
|
|
|
|
|
props_to_set = []
|
|
|
|
for ident, name, sig, flags in prop_specs:
|
|
|
|
value = props.pop(name, None)
|
|
|
|
if value is not None:
|
|
|
|
if flags & PROPERTY_FLAG_WRITE:
|
|
|
|
props_to_set.append((ident, value))
|
|
|
|
# FIXME: else error, but only if we're creating the room?
|
|
|
|
# FIXME: if props is nonempty, then we want to set props that aren't
|
|
|
|
# supported here - raise an error?
|
|
|
|
|
|
|
|
if props_to_set:
|
|
|
|
channel[PROPERTIES_INTERFACE].SetProperties(props_to_set,
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda: self._join_activity_channel_props_set_cb(
|
|
|
|
activity_id, signal, handle, channel, userdata),
|
|
|
|
error_handler=lambda e: self._join_error_cb(
|
|
|
|
activity_id, signal, userdata,
|
|
|
|
'SetProperties(%r)' % props_to_set, e))
|
2007-05-22 17:45:20 +02:00
|
|
|
else:
|
2007-05-24 13:14:55 +02:00
|
|
|
self._join_activity_channel_props_set_cb(activity_id, signal,
|
|
|
|
handle, channel, userdata)
|
2007-05-22 17:45:20 +02:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
def _join_activity_create_channel_cb(self, activity_id, signal, handle,
|
|
|
|
userdata, chan_path):
|
2007-05-23 18:42:31 +02:00
|
|
|
channel = Channel(self._conn.service_name, chan_path)
|
2007-05-22 17:45:20 +02:00
|
|
|
props = {
|
|
|
|
'anonymous': False, # otherwise buddy resolution breaks
|
|
|
|
'invite-only': False, # XXX: should be True in future
|
|
|
|
#'name': ... # XXX: set from activity name?
|
|
|
|
'persistent': False, # vanish when there are no members
|
|
|
|
'private': False, # XXX: should be True unless public
|
|
|
|
}
|
|
|
|
channel[PROPERTIES_INTERFACE].ListProperties(
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda prop_specs: self._join_activity_channel_props_listed_cb(
|
|
|
|
activity_id, signal, handle, channel, userdata, props, prop_specs),
|
|
|
|
error_handler=lambda e: self._join_error_cb(
|
|
|
|
activity_id, signal, userdata, 'ListProperties', e))
|
2007-05-22 17:45:20 +02:00
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
def _join_activity_get_channel_cb(self, activity_id, signal, userdata,
|
|
|
|
handles):
|
2007-04-13 19:11:59 +02:00
|
|
|
if not self._activities.has_key(activity_id):
|
|
|
|
self._activities[activity_id] = handles[0]
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
if (activity_id, handles[0]) in self._joined_activities:
|
|
|
|
e = RuntimeError("Already joined activity %s" % activity_id)
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug('%s', e)
|
2007-04-13 19:11:59 +02:00
|
|
|
self.emit(signal, activity_id, None, e, userdata)
|
2007-03-06 17:15:55 +01:00
|
|
|
return
|
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
self._conn[CONN_INTERFACE].RequestChannel(CHANNEL_TYPE_TEXT,
|
2007-05-24 12:05:52 +02:00
|
|
|
HANDLE_TYPE_ROOM, handles[0], True,
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda *args: self._join_activity_create_channel_cb(
|
|
|
|
activity_id, signal, handles[0], userdata, *args),
|
|
|
|
error_handler=lambda e: self._join_error_cb(activity_id, signal,
|
|
|
|
userdata, 'RequestChannel(TEXT, ROOM, %r, True)' % handles[0],
|
|
|
|
e))
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-05-22 17:45:20 +02:00
|
|
|
def _join_error_cb(self, activity_id, signal, userdata, where, err):
|
2007-05-24 13:14:55 +02:00
|
|
|
e = Exception("Error joining/sharing activity %s: (%s): %s"
|
|
|
|
% (activity_id, err))
|
|
|
|
_logger.debug('%s', e)
|
2007-04-13 19:11:59 +02:00
|
|
|
self.emit(signal, activity_id, None, e, userdata)
|
|
|
|
|
|
|
|
def _internal_join_activity(self, activity_id, signal, userdata):
|
|
|
|
handle = self._activities.get(activity_id)
|
|
|
|
if not handle:
|
2007-04-17 04:40:20 +02:00
|
|
|
# FIXME: figure out why the server can't figure this out itself
|
|
|
|
room_jid = activity_id + "@conference." + self._account["server"]
|
2007-05-24 13:14:55 +02:00
|
|
|
self._conn[CONN_INTERFACE].RequestHandles(HANDLE_TYPE_ROOM,
|
|
|
|
[room_jid],
|
|
|
|
reply_handler=lambda *args: self._join_activity_get_channel_cb(
|
|
|
|
activity_id, signal, userdata, *args),
|
|
|
|
error_handler=lambda e: self._join_error_cb(activity_id,
|
|
|
|
signal, userdata, 'RequestHandles([%u])' % room_jid,
|
|
|
|
e))
|
2007-04-13 19:11:59 +02:00
|
|
|
else:
|
2007-05-24 13:14:55 +02:00
|
|
|
self._join_activity_get_channel_cb(activity_id, signal, userdata,
|
|
|
|
[handle])
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
def share_activity(self, activity_id, userdata):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Share activity with the network
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
activity_id -- unique ID for the activity
|
2007-04-21 03:09:51 +02:00
|
|
|
userdata -- opaque token to be passed in the resulting event
|
|
|
|
(id, callback, errback) normally
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
Asks the Telepathy server to create a "conference" channel
|
|
|
|
for the activity or return a handle to an already created
|
2007-04-21 03:09:51 +02:00
|
|
|
conference channel for the activity.
|
|
|
|
"""
|
2007-04-13 19:11:59 +02:00
|
|
|
self._internal_join_activity(activity_id, "activity-shared", userdata)
|
|
|
|
|
|
|
|
def join_activity(self, activity_id, userdata):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Join an activity on the network (or locally)
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
activity_id -- unique ID for the activity
|
2007-04-21 03:09:51 +02:00
|
|
|
userdata -- opaque token to be passed in the resulting event
|
|
|
|
(id, callback, errback) normally
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
Asks the Telepathy server to create a "conference" channel
|
|
|
|
for the activity or return a handle to an already created
|
2007-04-21 03:09:51 +02:00
|
|
|
conference channel for the activity.
|
|
|
|
"""
|
2007-04-13 19:11:59 +02:00
|
|
|
self._internal_join_activity(activity_id, "activity-joined", userdata)
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-04-12 22:48:40 +02:00
|
|
|
def _ignore_success_cb(self):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Ignore an event (null-operation)"""
|
2007-04-12 22:48:40 +02:00
|
|
|
|
|
|
|
def _log_error_cb(self, msg, err):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Log a message (error) at debug level with prefix msg"""
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Error %s: %s", msg, err)
|
2007-04-12 22:48:40 +02:00
|
|
|
|
2007-04-17 04:39:07 +02:00
|
|
|
def _set_self_olpc_properties(self):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Set color and key on our Telepathy server identity"""
|
2007-02-28 04:38:28 +01:00
|
|
|
props = {}
|
2007-03-09 22:29:49 +01:00
|
|
|
props['color'] = self._owner.props.color
|
2007-04-17 04:39:07 +02:00
|
|
|
props['key'] = dbus.ByteArray(self._owner.props.key)
|
2007-05-01 05:44:39 +02:00
|
|
|
addr = self._owner.props.ip4_address
|
|
|
|
if not addr:
|
|
|
|
props['ip4-address'] = ""
|
|
|
|
else:
|
|
|
|
props['ip4-address'] = addr
|
2007-04-12 22:48:40 +02:00
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].SetProperties(props,
|
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-05-24 12:11:03 +02:00
|
|
|
error_handler=lambda e: self._log_error_cb("setting properties", e))
|
2007-04-12 22:48:40 +02:00
|
|
|
|
|
|
|
def _set_self_alias(self):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Forwarded to SetActivities on AliasInfo channel"""
|
2007-04-12 22:48:40 +02:00
|
|
|
alias = self._owner.props.nick
|
2007-02-28 17:02:43 +01:00
|
|
|
self_handle = self._conn[CONN_INTERFACE].GetSelfHandle()
|
2007-04-12 22:48:40 +02:00
|
|
|
self._conn[CONN_INTERFACE_ALIASING].SetAliases({self_handle : alias},
|
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-05-24 12:11:03 +02:00
|
|
|
error_handler=lambda e: self._log_error_cb("setting alias", e))
|
2007-05-15 04:48:53 +02:00
|
|
|
return False
|
2007-02-28 17:02:43 +01:00
|
|
|
|
2007-04-12 22:48:40 +02:00
|
|
|
def _set_self_activities(self):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Forward set of joined activities to network
|
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
uses SetActivities on BuddyInfo channel
|
|
|
|
"""
|
2007-05-24 13:14:55 +02:00
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].SetActivities(
|
|
|
|
self._joined_activities,
|
2007-04-12 22:48:40 +02:00
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-05-24 12:11:03 +02:00
|
|
|
error_handler=lambda e: self._log_error_cb("setting activities", e))
|
2007-03-05 18:56:17 +01:00
|
|
|
|
2007-04-12 22:48:40 +02:00
|
|
|
def _set_self_current_activity(self):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Forward our current activity (or "") to network
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
uses SetCurrentActivity on BuddyInfo channel
|
|
|
|
"""
|
2007-03-16 17:19:24 +01:00
|
|
|
cur_activity = self._owner.props.current_activity
|
|
|
|
cur_activity_handle = 0
|
|
|
|
if not cur_activity:
|
|
|
|
cur_activity = ""
|
|
|
|
else:
|
|
|
|
cur_activity_handle = self._get_handle_for_activity(cur_activity)
|
|
|
|
if not cur_activity_handle:
|
|
|
|
# dont advertise a current activity that's not shared
|
|
|
|
cur_activity = ""
|
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Setting current activity to '%s' (handle %s)",
|
|
|
|
cur_activity, cur_activity_handle)
|
2007-04-12 22:48:40 +02:00
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].SetCurrentActivity(cur_activity,
|
|
|
|
cur_activity_handle,
|
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-05-24 12:11:03 +02:00
|
|
|
error_handler=lambda e: self._log_error_cb("setting current activity", e))
|
2007-02-28 04:38:28 +01:00
|
|
|
|
2007-03-16 17:19:24 +01:00
|
|
|
def _get_handle_for_activity(self, activity_id):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Retrieve current handle for given activity or None"""
|
2007-03-16 17:19:24 +01:00
|
|
|
for (act, handle) in self._joined_activities:
|
|
|
|
if activity_id == act:
|
|
|
|
return handle
|
|
|
|
return None
|
|
|
|
|
2007-05-14 19:58:26 +02:00
|
|
|
def _start_helper(self):
|
|
|
|
"""Helper so start() doesn't have to return False"""
|
|
|
|
self.start()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _reconnect_cb(self):
|
|
|
|
"""Schedule a reconnection attempt"""
|
|
|
|
gobject.idle_add(self._start_helper)
|
|
|
|
self._reconnect_id = 0
|
|
|
|
return False
|
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
def _handle_connection_status_change(self, status, reason):
|
|
|
|
if status == self._conn_status:
|
|
|
|
return
|
|
|
|
|
|
|
|
if status == CONNECTION_STATUS_CONNECTING:
|
|
|
|
self._conn_status = status
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("status: connecting...")
|
2007-05-14 03:34:15 +02:00
|
|
|
elif status == CONNECTION_STATUS_CONNECTED:
|
2007-05-11 23:25:44 +02:00
|
|
|
if self._connected_cb():
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("status: connected")
|
2007-05-14 03:34:15 +02:00
|
|
|
self._conn_status = status
|
2007-05-11 23:25:44 +02:00
|
|
|
else:
|
|
|
|
self.cleanup()
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("status: was connected, but an error occurred")
|
2007-05-14 03:34:15 +02:00
|
|
|
elif status == CONNECTION_STATUS_DISCONNECTED:
|
2007-05-11 23:25:44 +02:00
|
|
|
self.cleanup()
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("status: disconnected (reason %r)", reason)
|
2007-02-26 21:38:50 +01:00
|
|
|
if reason == CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
|
|
|
|
# FIXME: handle connection failure; retry later?
|
|
|
|
pass
|
2007-05-14 19:58:26 +02:00
|
|
|
else:
|
|
|
|
# If disconnected, but still have a network connection, retry
|
|
|
|
# If disconnected and no network connection, do nothing here
|
|
|
|
# and let the IP4AddressMonitor address-changed signal handle
|
|
|
|
# reconnection
|
|
|
|
if self._ip4am.props.address:
|
2007-05-24 13:14:55 +02:00
|
|
|
self._reconnect_id = gobject.timeout_add(5000,
|
|
|
|
self._reconnect_cb)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-11 23:25:44 +02:00
|
|
|
self.emit('status', self._conn_status, int(reason))
|
2007-05-14 19:58:26 +02:00
|
|
|
return False
|
2007-05-11 23:25:44 +02:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
def _status_changed_cb(self, status, reason):
|
|
|
|
"""Handle notification of connection-status change
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
status -- CONNECTION_STATUS_*
|
|
|
|
reason -- integer code describing the reason...
|
|
|
|
"""
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("::: connection status changed to %s", status)
|
2007-05-14 03:34:15 +02:00
|
|
|
self._handle_connection_status_change(status, reason)
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
def start(self):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Start up the Telepathy networking connections
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
if we are already connected, query for the initial contact
|
|
|
|
information.
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-04-21 03:09:51 +02:00
|
|
|
if we are already connecting, do nothing
|
2007-05-24 12:29:22 +02:00
|
|
|
|
|
|
|
otherwise initiate a connection and transfer control to
|
2007-04-21 03:09:51 +02:00
|
|
|
_connect_reply_cb or _connect_error_cb
|
|
|
|
"""
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("Starting up...")
|
2007-03-03 09:15:18 +01:00
|
|
|
|
2007-05-14 19:58:26 +02:00
|
|
|
if self._reconnect_id > 0:
|
|
|
|
gobject.source_remove(self._reconnect_id)
|
|
|
|
self._reconnect_id = 0
|
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
# Only init connection if we have a valid IP address
|
|
|
|
if self._ip4am.props.address:
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("::: Have IP4 address %s, will connect",
|
|
|
|
self._ip4am.props.address)
|
2007-05-14 03:34:15 +02:00
|
|
|
self._init_connection()
|
|
|
|
else:
|
2007-05-15 15:58:15 +02:00
|
|
|
_logger.debug("::: No IP4 address, postponing connection")
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
def cleanup(self):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""If we still have a connection, disconnect it"""
|
2007-05-11 23:25:44 +02:00
|
|
|
if self._conn:
|
2007-05-15 21:25:30 +02:00
|
|
|
try:
|
|
|
|
self._conn[CONN_INTERFACE].Disconnect()
|
|
|
|
except:
|
|
|
|
pass
|
2007-05-11 23:25:44 +02:00
|
|
|
self._conn = None
|
|
|
|
self._conn_status = CONNECTION_STATUS_DISCONNECTED
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
for handle in self._online_contacts.keys():
|
|
|
|
self._contact_offline(handle)
|
|
|
|
self._online_contacts = {}
|
|
|
|
self._joined_activites = []
|
|
|
|
self._activites = {}
|
|
|
|
|
2007-05-14 19:58:26 +02:00
|
|
|
if self._reconnect_id > 0:
|
|
|
|
gobject.source_remove(self._reconnect_id)
|
|
|
|
self._reconnect_id = 0
|
|
|
|
|
2007-03-01 04:13:27 +01:00
|
|
|
def _contact_offline(self, handle):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle contact going offline (send message, update set)"""
|
2007-04-12 04:49:14 +02:00
|
|
|
if not self._online_contacts.has_key(handle):
|
|
|
|
return
|
|
|
|
if self._online_contacts[handle]:
|
|
|
|
self.emit("contact-offline", handle)
|
2007-03-01 04:13:27 +01:00
|
|
|
del self._online_contacts[handle]
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-04-10 21:00:49 +02:00
|
|
|
def _contact_online_activities_cb(self, handle, activities):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle contact's activity list update"""
|
2007-04-10 21:00:49 +02:00
|
|
|
self._buddy_activities_changed_cb(handle, activities)
|
2007-03-03 09:15:18 +01:00
|
|
|
|
2007-04-10 21:00:49 +02:00
|
|
|
def _contact_online_activities_error_cb(self, handle, err):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle contact's activity list being unavailable"""
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - Error getting activities: %s",
|
|
|
|
handle, err)
|
2007-05-14 22:20:23 +02:00
|
|
|
# Don't drop the buddy if we can't get their activities, for now
|
|
|
|
#self._contact_offline(handle)
|
2007-03-01 04:13:27 +01:00
|
|
|
|
2007-04-10 21:00:49 +02:00
|
|
|
def _contact_online_aliases_cb(self, handle, props, aliases):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle contact's alias being received (do further queries)"""
|
2007-05-14 19:58:26 +02:00
|
|
|
if not self._conn or not aliases or not len(aliases):
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - No aliases", handle)
|
2007-04-12 04:49:14 +02:00
|
|
|
self._contact_offline(handle)
|
2007-04-10 21:00:49 +02:00
|
|
|
return
|
2007-04-09 22:05:32 +02:00
|
|
|
|
2007-04-10 21:00:49 +02:00
|
|
|
props['nick'] = aliases[0]
|
2007-05-24 13:14:55 +02:00
|
|
|
jid = self._conn[CONN_INTERFACE].InspectHandles(HANDLE_TYPE_CONTACT,
|
|
|
|
[handle])[0]
|
2007-03-01 04:13:27 +01:00
|
|
|
self._online_contacts[handle] = jid
|
2007-02-28 17:02:43 +01:00
|
|
|
self.emit("contact-online", handle, props)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-04-10 21:00:49 +02:00
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].GetActivities(handle,
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda *args: self._contact_online_activities_cb(
|
|
|
|
handle, *args),
|
|
|
|
error_handler=lambda e: self._contact_online_activities_error_cb(
|
|
|
|
handle, e))
|
2007-04-10 21:00:49 +02:00
|
|
|
|
2007-05-15 21:25:30 +02:00
|
|
|
def _contact_online_aliases_error_cb(self, handle, props, retry, err):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle failure to retrieve given user's alias/information"""
|
2007-05-15 21:25:30 +02:00
|
|
|
if retry:
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - Error getting nickname (will retry):"
|
|
|
|
"%s", handle, err)
|
2007-05-15 21:25:30 +02:00
|
|
|
self._conn[CONN_INTERFACE_ALIASING].RequestAliases([handle],
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda *args: self._contact_online_aliases_cb(
|
|
|
|
handle, props, *args),
|
|
|
|
error_handler=lambda e: self._contact_online_aliases_error_cb(
|
|
|
|
handle, props, False, e))
|
2007-05-15 21:25:30 +02:00
|
|
|
else:
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - Error getting nickname: %s",
|
|
|
|
handle, err)
|
2007-05-15 21:25:30 +02:00
|
|
|
self._contact_offline(handle)
|
2007-04-10 21:00:49 +02:00
|
|
|
|
|
|
|
def _contact_online_properties_cb(self, handle, props):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle failure to retrieve given user's alias/information"""
|
2007-04-10 21:00:49 +02:00
|
|
|
if not props.has_key('key'):
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - invalid key.", handle)
|
2007-04-12 04:49:14 +02:00
|
|
|
self._contact_offline(handle)
|
|
|
|
return
|
2007-04-10 21:00:49 +02:00
|
|
|
if not props.has_key('color'):
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - invalid color.", handle)
|
2007-04-12 04:49:14 +02:00
|
|
|
self._contact_offline(handle)
|
|
|
|
return
|
2007-04-10 21:00:49 +02:00
|
|
|
|
|
|
|
self._conn[CONN_INTERFACE_ALIASING].RequestAliases([handle],
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda *args: self._contact_online_aliases_cb(
|
|
|
|
handle, props, *args),
|
|
|
|
error_handler=lambda e: self._contact_online_aliases_error_cb(
|
|
|
|
handle, props, True, e))
|
2007-05-16 07:03:55 +02:00
|
|
|
|
|
|
|
def _contact_online_request_properties(self, handle, tries):
|
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].GetProperties(handle,
|
2007-05-24 12:19:39 +02:00
|
|
|
byte_arrays=True,
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda *args: self._contact_online_properties_cb(
|
|
|
|
handle, *args),
|
|
|
|
error_handler=lambda e: self._contact_online_properties_error_cb(
|
|
|
|
handle, tries, e))
|
2007-05-16 07:03:55 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
def _contact_online_properties_error_cb(self, handle, tries, err):
|
2007-05-24 12:29:22 +02:00
|
|
|
"""Handle error retrieving property-set for a user (handle)"""
|
2007-05-16 07:03:55 +02:00
|
|
|
if tries <= 3:
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - Error getting properties (will retry):"
|
|
|
|
" %s", handle, err)
|
2007-05-16 07:03:55 +02:00
|
|
|
tries += 1
|
2007-05-24 13:14:55 +02:00
|
|
|
gobject.timeout_add(1000, self._contact_online_request_properties,
|
|
|
|
handle, tries)
|
2007-05-16 07:03:55 +02:00
|
|
|
else:
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s - Error getting properties: %s",
|
|
|
|
handle, err)
|
2007-05-16 07:03:55 +02:00
|
|
|
self._contact_offline(handle)
|
2007-04-10 21:00:49 +02:00
|
|
|
|
|
|
|
def _contact_online(self, handle):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle a contact coming online"""
|
2007-05-16 12:39:16 +02:00
|
|
|
if (handle not in self._subscribe_members and
|
|
|
|
handle not in self._subscribe_local_pending and
|
|
|
|
handle not in self._subscribe_remote_pending):
|
|
|
|
# it's probably a channel-specific handle - can't create a Buddy
|
|
|
|
# object
|
|
|
|
return
|
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
self._online_contacts[handle] = None
|
2007-05-03 06:43:53 +02:00
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
2007-05-24 13:14:55 +02:00
|
|
|
jid = self._conn[CONN_INTERFACE].InspectHandles(
|
|
|
|
HANDLE_TYPE_CONTACT, [handle])[0]
|
2007-05-03 06:43:53 +02:00
|
|
|
self._online_contacts[handle] = jid
|
|
|
|
# ignore network events for Owner property changes since those
|
|
|
|
# are handled locally
|
|
|
|
return
|
|
|
|
|
2007-05-16 07:03:55 +02:00
|
|
|
self._contact_online_request_properties(handle, 1)
|
2007-03-05 18:56:17 +01:00
|
|
|
|
2007-05-16 12:39:16 +02:00
|
|
|
def _subscribe_members_changed_cb(self, added, removed, local_pending,
|
|
|
|
remote_pending, actor, reason):
|
|
|
|
for handle in added:
|
|
|
|
self._subscribe_members.add(handle)
|
|
|
|
for handle in local_pending:
|
|
|
|
self._subscribe_local_pending.add(handle)
|
|
|
|
for handle in remote_pending:
|
|
|
|
self._subscribe_remote_pending.add(handle)
|
|
|
|
for handle in removed:
|
|
|
|
self._subscribe_members.discard(handle)
|
|
|
|
self._subscribe_local_pending.discard(handle)
|
|
|
|
self._subscribe_remote_pending.discard(handle)
|
|
|
|
|
|
|
|
def _publish_members_changed_cb(self, added, removed, local_pending,
|
|
|
|
remote_pending, actor, reason):
|
|
|
|
|
|
|
|
if local_pending:
|
|
|
|
# accept all requested subscriptions
|
|
|
|
self._publish_channel[CHANNEL_INTERFACE_GROUP].AddMembers(
|
|
|
|
local_pending, '')
|
|
|
|
|
|
|
|
# subscribe to people who've subscribed to us, if necessary
|
|
|
|
added = list(set(added) - self._subscribe_members
|
|
|
|
- self._subscribe_remote_pending)
|
|
|
|
if added:
|
|
|
|
self._subscribe_channel[CHANNEL_INTERFACE_GROUP].AddMembers(
|
|
|
|
added, '')
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
def _presence_update_cb(self, presence):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Send update for online/offline status of presence"""
|
2007-02-26 01:24:48 +01:00
|
|
|
for handle in presence:
|
|
|
|
timestamp, statuses = presence[handle]
|
|
|
|
online = handle in self._online_contacts
|
|
|
|
for status, params in statuses.items():
|
2007-05-14 03:34:15 +02:00
|
|
|
if not online and status == "offline":
|
|
|
|
# weren't online in the first place...
|
|
|
|
continue
|
2007-05-24 13:14:55 +02:00
|
|
|
jid = self._conn[CONN_INTERFACE].InspectHandles(
|
|
|
|
HANDLE_TYPE_CONTACT, [handle])[0]
|
2007-03-16 15:12:50 +01:00
|
|
|
olstr = "ONLINE"
|
|
|
|
if not online: olstr = "OFFLINE"
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s (%s) was %s, status now '%s'.",
|
|
|
|
handle, jid, olstr, status)
|
|
|
|
if not online and status in ["available", "away", "brb",
|
|
|
|
"busy", "dnd", "xa"]:
|
2007-04-10 21:00:49 +02:00
|
|
|
self._contact_online(handle)
|
2007-04-12 04:49:14 +02:00
|
|
|
elif status in ["offline", "invisible"]:
|
2007-03-01 04:13:27 +01:00
|
|
|
self._contact_offline(handle)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-05-15 21:25:30 +02:00
|
|
|
def _request_avatar_cb(self, handle, new_avatar_token, avatar, mime_type):
|
|
|
|
jid = self._online_contacts[handle]
|
|
|
|
if not jid:
|
|
|
|
logging.debug("Handle %s not valid yet..." % handle)
|
|
|
|
return
|
|
|
|
icon = ''.join(map(chr, avatar))
|
|
|
|
self._icon_cache.store_icon(jid, new_avatar_token, icon)
|
|
|
|
self.emit("avatar-updated", handle, icon)
|
|
|
|
|
2007-02-26 17:18:52 +01:00
|
|
|
def _avatar_updated_cb(self, handle, new_avatar_token):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle update of given user (handle)'s avatar"""
|
2007-03-16 15:43:15 +01:00
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
|
|
|
# ignore network events for Owner property changes since those
|
|
|
|
# are handled locally
|
|
|
|
return
|
|
|
|
|
2007-04-12 05:45:20 +02:00
|
|
|
if not self._online_contacts.has_key(handle):
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s unknown.", handle)
|
2007-04-12 05:45:20 +02:00
|
|
|
return
|
|
|
|
|
2007-03-01 04:13:27 +01:00
|
|
|
jid = self._online_contacts[handle]
|
2007-04-12 04:49:14 +02:00
|
|
|
if not jid:
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s not valid yet...", handle)
|
2007-04-12 04:49:14 +02:00
|
|
|
return
|
|
|
|
|
2007-02-26 17:18:52 +01:00
|
|
|
icon = self._icon_cache.get_icon(jid, new_avatar_token)
|
|
|
|
if not icon:
|
|
|
|
# cache miss
|
2007-05-15 21:25:30 +02:00
|
|
|
self._conn[CONN_INTERFACE_AVATARS].RequestAvatar(handle,
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda *args: self._request_avatar_cb(handle,
|
|
|
|
new_avatar_token, *args),
|
|
|
|
error_handler=lambda e: self._log_error_cb(
|
|
|
|
"getting avatar", e))
|
2007-05-15 21:25:30 +02:00
|
|
|
else:
|
|
|
|
self.emit("avatar-updated", handle, icon)
|
2007-02-27 23:51:53 +01:00
|
|
|
|
2007-02-28 17:02:43 +01:00
|
|
|
def _alias_changed_cb(self, aliases):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle update of aliases for all users"""
|
2007-02-28 17:02:43 +01:00
|
|
|
for handle, alias in aliases:
|
2007-03-08 15:45:12 +01:00
|
|
|
prop = {'nick': alias}
|
|
|
|
#print "Buddy %s alias changed to %s" % (handle, alias)
|
2007-05-24 13:14:55 +02:00
|
|
|
if (self._online_contacts.has_key(handle) and
|
|
|
|
self._online_contacts[handle]):
|
2007-04-12 04:49:14 +02:00
|
|
|
self._buddy_properties_changed_cb(handle, prop)
|
2007-02-28 17:02:43 +01:00
|
|
|
|
2007-03-16 15:43:15 +01:00
|
|
|
def _buddy_properties_changed_cb(self, handle, properties):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle update of given user (handle)'s properties"""
|
2007-03-16 15:43:15 +01:00
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
|
|
|
# ignore network events for Owner property changes since those
|
|
|
|
# are handled locally
|
|
|
|
return
|
2007-05-24 13:14:55 +02:00
|
|
|
if (self._online_contacts.has_key(handle) and
|
|
|
|
self._online_contacts[handle]):
|
2007-04-20 19:10:40 +02:00
|
|
|
self.emit("buddy-properties-changed", handle, properties)
|
2007-03-16 15:43:15 +01:00
|
|
|
|
|
|
|
def _buddy_activities_changed_cb(self, handle, activities):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle update of given user (handle)'s activities"""
|
2007-03-16 15:43:15 +01:00
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
|
|
|
# ignore network events for Owner activity changes since those
|
|
|
|
# are handled locally
|
|
|
|
return
|
2007-05-24 13:14:55 +02:00
|
|
|
if (not self._online_contacts.has_key(handle) or
|
|
|
|
not self._online_contacts[handle]):
|
2007-04-12 04:49:14 +02:00
|
|
|
return
|
2007-02-27 23:51:53 +01:00
|
|
|
|
2007-03-05 18:56:17 +01:00
|
|
|
for act_id, act_handle in activities:
|
|
|
|
self._activities[act_id] = act_handle
|
|
|
|
activities_id = map(lambda x: x[0], activities)
|
2007-03-16 15:43:15 +01:00
|
|
|
self.emit("buddy-activities-changed", handle, activities_id)
|
2007-03-07 15:27:47 +01:00
|
|
|
|
2007-03-16 17:19:24 +01:00
|
|
|
def _buddy_current_activity_changed_cb(self, handle, activity, channel):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle update of given user (handle)'s current activity"""
|
2007-05-24 12:29:22 +02:00
|
|
|
|
2007-03-16 17:19:24 +01:00
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
2007-05-24 13:14:55 +02:00
|
|
|
# ignore network events for Owner current activity changes since
|
|
|
|
# those are handled locally
|
2007-03-16 17:19:24 +01:00
|
|
|
return
|
2007-05-24 13:14:55 +02:00
|
|
|
if (not self._online_contacts.has_key(handle) or
|
|
|
|
not self._online_contacts[handle]):
|
2007-04-12 04:49:14 +02:00
|
|
|
return
|
2007-03-16 17:19:24 +01:00
|
|
|
|
|
|
|
if not len(activity) or not util.validate_activity_id(activity):
|
|
|
|
activity = None
|
|
|
|
prop = {'current-activity': activity}
|
2007-05-24 13:14:55 +02:00
|
|
|
_logger.debug("Handle %s: current activity now %s", handle, activity)
|
2007-03-16 17:19:24 +01:00
|
|
|
self._buddy_properties_changed_cb(handle, prop)
|
|
|
|
|
2007-05-23 19:39:24 +02:00
|
|
|
def _new_channel_cb(self, object_path, channel_type, handle_type, handle,
|
|
|
|
suppress_handler):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle creation of a new channel
|
|
|
|
"""
|
2007-05-24 12:05:52 +02:00
|
|
|
if (handle_type == HANDLE_TYPE_ROOM and
|
2007-05-23 19:39:24 +02:00
|
|
|
channel_type == CHANNEL_TYPE_TEXT):
|
|
|
|
def ready(channel):
|
2007-03-07 15:27:47 +01:00
|
|
|
|
2007-05-23 19:39:24 +02:00
|
|
|
for act_id, act_handle in self._activities.iteritems():
|
2007-03-07 15:27:47 +01:00
|
|
|
if handle == act_handle:
|
2007-05-23 19:39:24 +02:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
def members_changed(message, added, removed, local_pending,
|
|
|
|
remote_pending, actor, reason):
|
|
|
|
# FIXME: if contacts were added, who don't have this
|
|
|
|
# activity in their PEP node for whatever reason, then
|
|
|
|
# emit buddy-activities-changed for them (otherwise they
|
|
|
|
# could be in an activity while pretending they weren't,
|
|
|
|
# which would be crazy)
|
|
|
|
pass
|
|
|
|
|
|
|
|
def got_all_members(current, local_pending, remote_pending):
|
|
|
|
if local_pending:
|
|
|
|
for act_id, act_handle in self._activities.iteritems():
|
|
|
|
if handle == act_handle:
|
|
|
|
self.emit('activity-invitation', act_id)
|
|
|
|
def got_all_members_err(e):
|
|
|
|
logger.debug('Unable to get channel members for %s:',
|
|
|
|
object_path, exc_info=1)
|
|
|
|
|
|
|
|
# hook the MembersChanged signal so we get told when people
|
|
|
|
# join/leave
|
|
|
|
group = channel[CHANNEL_INTERFACE_GROUP]
|
|
|
|
group.connect_to_signal('MembersChanged', members_changed)
|
|
|
|
group.GetAllMembers(reply_handler=got_all_members,
|
|
|
|
error_handler=got_all_members_err)
|
|
|
|
|
|
|
|
# we throw away the channel as soon as ready() finishes
|
|
|
|
Channel(self._conn.service_name, object_path,
|
|
|
|
ready_handler=ready)
|
|
|
|
|
2007-05-24 12:05:52 +02:00
|
|
|
elif (handle_type == HANDLE_TYPE_CONTACT and
|
2007-05-23 19:39:24 +02:00
|
|
|
channel_type in (CHANNEL_TYPE_TEXT,
|
|
|
|
CHANNEL_TYPE_STREAMED_MEDIA)):
|
2007-03-07 19:46:48 +01:00
|
|
|
self.emit("private-invitation", object_path)
|
2007-03-14 15:59:30 +01:00
|
|
|
|
2007-04-13 21:42:54 +02:00
|
|
|
def update_activity_properties(self, act_id):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Request update from network on the activity properties of act_id"""
|
2007-04-13 21:42:54 +02:00
|
|
|
handle = self._activities.get(act_id)
|
|
|
|
if not handle:
|
|
|
|
raise RuntimeError("Unknown activity %s: couldn't find handle.")
|
|
|
|
|
|
|
|
self._conn[CONN_INTERFACE_ACTIVITY_PROPERTIES].GetProperties(handle,
|
2007-05-24 13:14:55 +02:00
|
|
|
reply_handler=lambda *args: self._activity_properties_changed_cb(
|
|
|
|
handle, *args),
|
|
|
|
error_handler=lambda e: self._log_error_cb(
|
|
|
|
"getting activity properties", e))
|
2007-04-13 21:42:54 +02:00
|
|
|
|
2007-03-14 15:59:30 +01:00
|
|
|
def set_activity_properties(self, act_id, props):
|
2007-05-24 13:14:55 +02:00
|
|
|
"""Send update to network on the activity properties of act_id (props).
|
|
|
|
"""
|
2007-03-14 15:59:30 +01:00
|
|
|
handle = self._activities.get(act_id)
|
|
|
|
if not handle:
|
2007-04-13 21:42:54 +02:00
|
|
|
raise RuntimeError("Unknown activity %s: couldn't find handle.")
|
|
|
|
|
2007-05-24 13:14:55 +02:00
|
|
|
self._conn[CONN_INTERFACE_ACTIVITY_PROPERTIES].SetProperties(handle,
|
|
|
|
props, reply_handler=self._ignore_success_cb,
|
|
|
|
error_handler=lambda e: self._log_error_cb(
|
|
|
|
"setting activity properties", e))
|
2007-03-14 15:59:30 +01:00
|
|
|
|
|
|
|
def _activity_properties_changed_cb(self, room, properties):
|
2007-04-21 03:09:51 +02:00
|
|
|
"""Handle update of properties for a "room" (activity handle)"""
|
2007-03-14 15:59:30 +01:00
|
|
|
for act_id, act_handle in self._activities.items():
|
|
|
|
if room == act_handle:
|
|
|
|
self.emit("activity-properties-changed", act_id, properties)
|
2007-04-13 21:42:54 +02:00
|
|
|
return
|