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
|
|
|
|
|
|
|
|
import gobject
|
2007-02-27 23:51:53 +01:00
|
|
|
import dbus
|
2007-02-26 01:24:48 +01:00
|
|
|
from sugar import util
|
2007-02-28 04:38:28 +01:00
|
|
|
import gtk
|
2007-02-26 17:18:52 +01:00
|
|
|
from buddyiconcache import BuddyIconCache
|
2007-02-26 01:24:48 +01:00
|
|
|
import logging
|
2007-02-28 04:38:28 +01:00
|
|
|
import os
|
2007-03-05 12:52:29 +01:00
|
|
|
import hashlib
|
2007-04-10 16:59:35 +02:00
|
|
|
import psutils
|
2007-02-26 01:24:48 +01: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,
|
2007-03-07 19:46:48 +01:00
|
|
|
CONN_INTERFACE_AVATARS, CONN_INTERFACE_PRESENCE, CHANNEL_TYPE_TEXT, CHANNEL_TYPE_STREAMED_MEDIA)
|
2007-02-26 01:24:48 +01:00
|
|
|
from telepathy.constants import (
|
|
|
|
CONNECTION_HANDLE_TYPE_NONE, CONNECTION_HANDLE_TYPE_CONTACT,
|
|
|
|
CONNECTION_STATUS_CONNECTED, CONNECTION_STATUS_DISCONNECTED, CONNECTION_STATUS_CONNECTING,
|
2007-03-06 17:15:55 +01:00
|
|
|
CONNECTION_HANDLE_TYPE_LIST, CONNECTION_HANDLE_TYPE_CONTACT, CONNECTION_HANDLE_TYPE_ROOM,
|
2007-02-26 04:45:16 +01:00
|
|
|
CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED)
|
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-02-26 01:24:48 +01:00
|
|
|
|
2007-02-28 04:38:28 +01:00
|
|
|
class InvalidBuddyError(Exception):
|
|
|
|
pass
|
|
|
|
|
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 = [""]
|
|
|
|
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-02-28 04:38:28 +01:00
|
|
|
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
class ServerPlugin(gobject.GObject):
|
|
|
|
__gsignals__ = {
|
|
|
|
'contact-online': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
|
|
|
|
'contact-offline': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'status': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-02-26 17:18:52 +01:00
|
|
|
([gobject.TYPE_INT, gobject.TYPE_INT])),
|
|
|
|
'avatar-updated': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-02-27 23:51:53 +01:00
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
|
2007-03-14 15:59:30 +01:00
|
|
|
'buddy-properties-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-02-27 23:51:53 +01:00
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
|
2007-03-14 15:59:30 +01:00
|
|
|
'buddy-activities-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-03-07 15:27:47 +01:00
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
|
2007-03-07 19:46:48 +01:00
|
|
|
'activity-invitation': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'private-invitation': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-03-14 15:59:30 +01:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-properties-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
|
2007-04-13 19:11:59 +02:00
|
|
|
'activity-shared': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT,
|
|
|
|
gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-joined': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT,
|
|
|
|
gobject.TYPE_PYOBJECT, gobject.TYPE_PYOBJECT]))
|
2007-02-26 01:24:48 +01:00
|
|
|
}
|
|
|
|
|
2007-03-09 22:29:49 +01:00
|
|
|
def __init__(self, registry, owner):
|
2007-02-26 01:24:48 +01:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2007-02-26 17:18:52 +01:00
|
|
|
self._icon_cache = BuddyIconCache()
|
|
|
|
|
2007-02-26 19:18:06 +01:00
|
|
|
self._gabble_mgr = registry.GetManager('gabble')
|
2007-03-01 04:13:27 +01:00
|
|
|
self._online_contacts = {} # handle -> jid
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-03-05 18:56:17 +01:00
|
|
|
self._activities = {} # activity id -> handle
|
2007-03-06 17:15:55 +01:00
|
|
|
self._joined_activities = [] # (activity_id, handle of the activity channel)
|
2007-03-09 22:29:49 +01:00
|
|
|
|
|
|
|
self._owner = owner
|
|
|
|
self._owner.connect("property-changed", self._owner_property_changed_cb)
|
|
|
|
self._owner.connect("icon-changed", self._owner_icon_changed_cb)
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
self._account = self._get_account_info()
|
|
|
|
|
|
|
|
self._conn = self._init_connection()
|
|
|
|
|
2007-03-03 09:15:18 +01:00
|
|
|
self._reconnect_id = 0
|
|
|
|
|
2007-03-09 22:29:49 +01:00
|
|
|
def _owner_property_changed_cb(self, owner, properties):
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.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()
|
|
|
|
|
|
|
|
if properties.has_key("color"):
|
2007-04-13 18:29:50 +02:00
|
|
|
self._set_self_olpc_properties()
|
2007-03-09 22:29:49 +01:00
|
|
|
|
|
|
|
def _owner_icon_changed_cb(self, owner, icon):
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.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-02-27 19:08:17 +01:00
|
|
|
account_info = {}
|
|
|
|
|
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):
|
|
|
|
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:
|
|
|
|
if not item.object_path.startswith("/org/freedesktop/Telepathy/Connection/gabble/jabber/"):
|
|
|
|
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-02-26 19:18:06 +01:00
|
|
|
test_handle = item[CONN_INTERFACE].RequestHandles(CONNECTION_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):
|
|
|
|
return self._conn
|
|
|
|
|
2007-02-26 21:38:50 +01:00
|
|
|
def _init_connection(self):
|
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-02-26 19:18:06 +01:00
|
|
|
name, path = self._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
|
|
|
|
|
|
|
conn[CONN_INTERFACE].connect_to_signal('StatusChanged', self._status_changed_cb)
|
2007-03-07 15:27:47 +01:00
|
|
|
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
|
|
|
|
|
|
|
return conn
|
|
|
|
|
|
|
|
def _request_list_channel(self, name):
|
|
|
|
handle = self._conn[CONN_INTERFACE].RequestHandles(
|
|
|
|
CONNECTION_HANDLE_TYPE_LIST, [name])[0]
|
|
|
|
chan_path = self._conn[CONN_INTERFACE].RequestChannel(
|
|
|
|
CHANNEL_TYPE_CONTACT_LIST, CONNECTION_HANDLE_TYPE_LIST,
|
|
|
|
handle, True)
|
|
|
|
channel = Channel(self._conn._dbus_object._named_service, chan_path)
|
|
|
|
# hack
|
|
|
|
channel._valid_interfaces.add(CHANNEL_INTERFACE_GROUP)
|
|
|
|
return channel
|
|
|
|
|
|
|
|
def _connected_cb(self):
|
2007-02-27 19:08:17 +01:00
|
|
|
if self._account['register']:
|
|
|
|
# we successfully register this account
|
2007-04-12 04:49:14 +02:00
|
|
|
self._owner.props.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')
|
|
|
|
publish_handles, local_pending, remote_pending = publish[CHANNEL_INTERFACE_GROUP].GetAllMembers()
|
|
|
|
|
|
|
|
# the group of contacts for whom you wish to receive presence
|
|
|
|
subscribe = self._request_list_channel('subscribe')
|
|
|
|
subscribe_handles = subscribe[CHANNEL_INTERFACE_GROUP].GetMembers()
|
|
|
|
|
|
|
|
if local_pending:
|
|
|
|
# accept pending subscriptions
|
|
|
|
#print 'pending: %r' % local_pending
|
|
|
|
publish[CHANNEL_INTERFACE_GROUP].AddMembers(local_pending, '')
|
|
|
|
|
|
|
|
not_subscribed = list(set(publish_handles) - set(subscribe_handles))
|
|
|
|
self_handle = self._conn[CONN_INTERFACE].GetSelfHandle()
|
2007-03-01 04:13:27 +01:00
|
|
|
self._online_contacts[self_handle] = self._account['account']
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
for handle in not_subscribed:
|
|
|
|
# request subscriptions from people subscribed to us if we're not subscribed to them
|
|
|
|
subscribe[CHANNEL_INTERFACE_GROUP].AddMembers([self_handle], '')
|
|
|
|
|
2007-03-01 04:13:27 +01:00
|
|
|
if CONN_INTERFACE_BUDDY_INFO not in self._conn.get_valid_interfaces():
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug('OLPC information not available')
|
2007-03-02 17:10:44 +01:00
|
|
|
self.cleanup()
|
2007-03-01 04:13:27 +01:00
|
|
|
return
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-04-12 22:48:40 +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',
|
|
|
|
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)
|
|
|
|
self._conn[CONN_INTERFACE_ACTIVITY_PROPERTIES].connect_to_signal('ActivityPropertiesChanged',
|
|
|
|
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()
|
|
|
|
self._set_self_activities()
|
|
|
|
self._set_self_current_activity()
|
|
|
|
self._set_self_avatar()
|
2007-02-27 23:51:53 +01:00
|
|
|
|
2007-03-01 04:13:27 +01:00
|
|
|
# Request presence for everyone on the channel
|
2007-04-12 22:48:40 +02:00
|
|
|
self._conn[CONN_INTERFACE_PRESENCE].GetPresence(subscribe_handles,
|
|
|
|
ignore_reply=True)
|
|
|
|
|
|
|
|
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-03-05 12:52:29 +01:00
|
|
|
md5 = hashlib.md5()
|
2007-03-09 22:29:49 +01:00
|
|
|
md5.update(icon_data)
|
2007-03-05 12:52:29 +01:00
|
|
|
hash = md5.hexdigest()
|
|
|
|
|
|
|
|
self_handle = self._conn[CONN_INTERFACE].GetSelfHandle()
|
|
|
|
token = self._conn[CONN_INTERFACE_AVATARS].GetAvatarTokens([self_handle])[0]
|
|
|
|
|
|
|
|
if self._icon_cache.check_avatar(hash, token):
|
|
|
|
# avatar is up to date
|
|
|
|
return
|
|
|
|
|
|
|
|
types, minw, minh, maxw, maxh, maxsize = self._conn[CONN_INTERFACE_AVATARS].GetAvatarRequirements()
|
|
|
|
if not "image/jpeg" in types:
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug("server does not accept JPEG format avatars.")
|
2007-03-05 12:52:29 +01:00
|
|
|
return
|
|
|
|
|
2007-03-09 22:29:49 +01: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-04-13 21:42:54 +02:00
|
|
|
error_handler=lambda *args: self._log_error_cb("setting avatar", *args))
|
2007-03-05 12:52:29 +01:00
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
def _join_activity_create_channel_cb(self, activity_id, signal, handle, userdata, chan_path):
|
|
|
|
channel = Channel(self._conn._dbus_object._named_service, chan_path)
|
|
|
|
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-04-13 19:11:59 +02:00
|
|
|
def _join_activity_get_channel_cb(self, activity_id, signal, userdata, handles):
|
|
|
|
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)
|
|
|
|
logging.debug(str(e))
|
|
|
|
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,
|
|
|
|
CONNECTION_HANDLE_TYPE_ROOM, handles[0], True,
|
2007-04-13 20:06:49 +02:00
|
|
|
reply_handler=lambda *args: self._join_activity_create_channel_cb(activity_id, signal, handles[0], userdata, *args),
|
2007-04-13 19:11:59 +02:00
|
|
|
error_handler=lambda *args: self._join_error_cb(activity_id, signal, userdata, *args))
|
2007-03-06 17:15:55 +01:00
|
|
|
|
2007-04-13 19:11:59 +02:00
|
|
|
def _join_error_cb(self, activity_id, signal, userdata, err):
|
|
|
|
e = Exception("Error joining/sharing activity %s: %s" % (activity_id, err))
|
|
|
|
logging.debug(str(e))
|
|
|
|
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"]
|
|
|
|
self._conn[CONN_INTERFACE].RequestHandles(CONNECTION_HANDLE_TYPE_ROOM, [room_jid],
|
2007-04-13 19:11:59 +02:00
|
|
|
reply_handler=lambda *args: self._join_activity_get_channel_cb(activity_id, signal, userdata, *args),
|
|
|
|
error_handler=lambda *args: self._join_error_cb(activity_id, signal, userdata, *args))
|
|
|
|
else:
|
|
|
|
self._join_activity_get_channel_cb(activity_id, userdata, [handle])
|
|
|
|
|
|
|
|
def share_activity(self, activity_id, userdata):
|
|
|
|
self._internal_join_activity(activity_id, "activity-shared", userdata)
|
|
|
|
|
|
|
|
def join_activity(self, activity_id, userdata):
|
|
|
|
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):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _log_error_cb(self, msg, err):
|
2007-04-13 21:42:54 +02:00
|
|
|
logging.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-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-04-12 22:48:40 +02:00
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].SetProperties(props,
|
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-04-13 21:42:54 +02:00
|
|
|
error_handler=lambda *args: self._log_error_cb("setting properties", *args))
|
2007-04-12 22:48:40 +02:00
|
|
|
|
|
|
|
def _set_self_alias(self):
|
|
|
|
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-04-13 21:42:54 +02:00
|
|
|
error_handler=lambda *args: self._log_error_cb("setting alias", *args))
|
2007-02-28 17:02:43 +01:00
|
|
|
|
2007-04-12 22:48:40 +02:00
|
|
|
def _set_self_activities(self):
|
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].SetActivities(self._joined_activities,
|
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-04-13 21:42:54 +02:00
|
|
|
error_handler=lambda *args: self._log_error_cb("setting activities", *args))
|
2007-03-05 18:56:17 +01:00
|
|
|
|
2007-04-12 22:48:40 +02:00
|
|
|
def _set_self_current_activity(self):
|
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-04-12 22:48:40 +02:00
|
|
|
logging.debug("Setting current activity to '%s' (handle %s)" % (cur_activity, cur_activity_handle))
|
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].SetCurrentActivity(cur_activity,
|
|
|
|
cur_activity_handle,
|
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-04-13 21:42:54 +02:00
|
|
|
error_handler=lambda *args: self._log_error_cb("setting current activity", *args))
|
2007-02-28 04:38:28 +01:00
|
|
|
|
2007-03-16 17:19:24 +01:00
|
|
|
def _get_handle_for_activity(self, activity_id):
|
|
|
|
for (act, handle) in self._joined_activities:
|
|
|
|
if activity_id == act:
|
|
|
|
return handle
|
|
|
|
return None
|
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
def _status_changed_cb(self, state, reason):
|
|
|
|
if state == CONNECTION_STATUS_CONNECTING:
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug("State: connecting...")
|
2007-02-26 01:24:48 +01:00
|
|
|
elif state == CONNECTION_STATUS_CONNECTED:
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug("State: connected")
|
2007-02-26 01:24:48 +01:00
|
|
|
self._connected_cb()
|
2007-03-06 17:15:55 +01:00
|
|
|
self.emit('status', state, int(reason))
|
2007-02-26 01:24:48 +01:00
|
|
|
elif state == CONNECTION_STATUS_DISCONNECTED:
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug("State: disconnected (reason %r)" % reason)
|
2007-02-26 01:24:48 +01:00
|
|
|
self.emit('status', state, int(reason))
|
2007-03-03 09:51:27 +01:00
|
|
|
self._conn = None
|
2007-02-26 21:38:50 +01:00
|
|
|
if reason == CONNECTION_STATUS_REASON_AUTHENTICATION_FAILED:
|
|
|
|
# FIXME: handle connection failure; retry later?
|
|
|
|
pass
|
2007-02-26 04:45:16 +01:00
|
|
|
return False
|
2007-02-26 01:24:48 +01:00
|
|
|
|
|
|
|
def start(self):
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug("Starting up...")
|
2007-02-26 01:24:48 +01:00
|
|
|
# If the connection is already connected query initial contacts
|
|
|
|
conn_status = self._conn[CONN_INTERFACE].GetStatus()
|
|
|
|
if conn_status == CONNECTION_STATUS_CONNECTED:
|
|
|
|
self._connected_cb()
|
|
|
|
subscribe = self._request_list_channel('subscribe')
|
|
|
|
subscribe_handles = subscribe[CHANNEL_INTERFACE_GROUP].GetMembers()
|
|
|
|
self._conn[CONN_INTERFACE_PRESENCE].RequestPresence(subscribe_handles)
|
|
|
|
elif conn_status == CONNECTION_STATUS_CONNECTING:
|
|
|
|
pass
|
|
|
|
else:
|
2007-03-03 09:15:18 +01:00
|
|
|
self._conn[CONN_INTERFACE].Connect(reply_handler=self._connect_reply_cb,
|
|
|
|
error_handler=self._connect_error_cb)
|
|
|
|
|
|
|
|
def _connect_reply_cb(self):
|
|
|
|
if self._reconnect_id > 0:
|
|
|
|
gobject.source_remove(self._reconnect_id)
|
|
|
|
|
|
|
|
def _reconnect(self):
|
|
|
|
self._reconnect_id = 0
|
|
|
|
self.start()
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _connect_error_cb(self, exception):
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug("Connect error: %s" % exception)
|
2007-03-03 09:15:18 +01:00
|
|
|
if not self._reconnect_id:
|
|
|
|
self._reconnect_id = gobject.timeout_add(10000, self._reconnect)
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
def cleanup(self):
|
2007-03-03 09:51:27 +01:00
|
|
|
if not self._conn:
|
|
|
|
return
|
2007-02-26 01:24:48 +01:00
|
|
|
self._conn[CONN_INTERFACE].Disconnect()
|
|
|
|
|
2007-03-01 04:13:27 +01:00
|
|
|
def _contact_offline(self, handle):
|
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):
|
|
|
|
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):
|
|
|
|
logging.debug("Handle %s - Error getting activities: %s" % (handle, err))
|
2007-04-12 04:49:14 +02:00
|
|
|
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):
|
|
|
|
if not aliases or not len(aliases):
|
|
|
|
logging.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-03-01 04:13:27 +01:00
|
|
|
jid = self._conn[CONN_INTERFACE].InspectHandles(CONNECTION_HANDLE_TYPE_CONTACT, [handle])[0]
|
|
|
|
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,
|
|
|
|
reply_handler=lambda *args: self._contact_online_activities_cb(handle, *args),
|
|
|
|
error_handler=lambda *args: self._contact_online_activities_error_cb(handle, *args))
|
|
|
|
|
|
|
|
def _contact_online_aliases_error_cb(self, handle, err):
|
|
|
|
logging.debug("Handle %s - Error getting nickname: %s" % (handle, err))
|
2007-04-12 04:49:14 +02:00
|
|
|
self._contact_offline(handle)
|
2007-04-10 21:00:49 +02:00
|
|
|
|
|
|
|
def _contact_online_properties_cb(self, handle, props):
|
|
|
|
if not props.has_key('key'):
|
|
|
|
logging.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'):
|
|
|
|
logging.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
|
|
|
|
|
|
|
# Convert key from dbus byte array to python string
|
|
|
|
props["key"] = psutils.bytes_to_string(props["key"])
|
|
|
|
|
|
|
|
self._conn[CONN_INTERFACE_ALIASING].RequestAliases([handle],
|
|
|
|
reply_handler=lambda *args: self._contact_online_aliases_cb(handle, props, *args),
|
|
|
|
error_handler=lambda *args: self._contact_online_aliases_error_cb(handle, *args))
|
|
|
|
|
|
|
|
def _contact_online_properties_error_cb(self, handle, err):
|
|
|
|
logging.debug("Handle %s - Error getting properties: %s" % (handle, err))
|
2007-04-12 04:49:14 +02:00
|
|
|
self._contact_offline(handle)
|
2007-04-10 21:00:49 +02:00
|
|
|
|
|
|
|
def _contact_online(self, handle):
|
2007-04-12 04:49:14 +02:00
|
|
|
self._online_contacts[handle] = None
|
2007-04-10 21:00:49 +02:00
|
|
|
self._conn[CONN_INTERFACE_BUDDY_INFO].GetProperties(handle,
|
|
|
|
reply_handler=lambda *args: self._contact_online_properties_cb(handle, *args),
|
|
|
|
error_handler=lambda *args: self._contact_online_properties_error_cb(handle, *args))
|
2007-03-05 18:56:17 +01:00
|
|
|
|
2007-02-26 01:24:48 +01:00
|
|
|
def _presence_update_cb(self, presence):
|
|
|
|
for handle in presence:
|
|
|
|
timestamp, statuses = presence[handle]
|
|
|
|
online = handle in self._online_contacts
|
|
|
|
for status, params in statuses.items():
|
2007-03-07 13:15:38 +01:00
|
|
|
jid = self._conn[CONN_INTERFACE].InspectHandles(CONNECTION_HANDLE_TYPE_CONTACT, [handle])[0]
|
2007-03-16 15:12:50 +01:00
|
|
|
olstr = "ONLINE"
|
|
|
|
if not online: olstr = "OFFLINE"
|
2007-04-10 21:00:49 +02:00
|
|
|
logging.debug("Handle %s (%s) was %s, status now '%s'." % (handle, jid, olstr, status))
|
2007-02-26 01:24:48 +01:00
|
|
|
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-02-26 17:18:52 +01:00
|
|
|
def _avatar_updated_cb(self, handle, new_avatar_token):
|
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-04-17 04:37:56 +02:00
|
|
|
logging.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-04-17 04:37:56 +02:00
|
|
|
logging.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
|
|
|
|
avatar, mime_type = self._conn[CONN_INTERFACE_AVATARS].RequestAvatar(handle)
|
|
|
|
icon = ''.join(map(chr, avatar))
|
|
|
|
self._icon_cache.store_icon(jid, new_avatar_token, icon)
|
|
|
|
|
|
|
|
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):
|
|
|
|
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-04-12 04:49:14 +02:00
|
|
|
if self._online_contacts.has_key(handle) and self._online_contacts[handle]:
|
|
|
|
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):
|
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
|
|
|
# ignore network events for Owner property changes since those
|
|
|
|
# are handled locally
|
|
|
|
return
|
2007-04-12 04:49:14 +02:00
|
|
|
if self._online_contacts.has_key(handle) and self._online_contacts[handle]:
|
|
|
|
self.emit("buddy-properties-changed", handle, properties)
|
2007-03-16 15:43:15 +01:00
|
|
|
|
|
|
|
def _buddy_activities_changed_cb(self, handle, activities):
|
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
|
|
|
# ignore network events for Owner activity changes since those
|
|
|
|
# are handled locally
|
|
|
|
return
|
2007-04-12 04:49:14 +02:00
|
|
|
if not self._online_contacts.has_key(handle) or not self._online_contacts[handle]:
|
|
|
|
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):
|
|
|
|
if handle == self._conn[CONN_INTERFACE].GetSelfHandle():
|
|
|
|
# ignore network events for Owner current activity changes since those
|
|
|
|
# are handled locally
|
|
|
|
return
|
2007-04-12 04:49:14 +02:00
|
|
|
if not self._online_contacts.has_key(handle) or not self._online_contacts[handle]:
|
|
|
|
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}
|
|
|
|
logging.debug("Handle %s: current activity now %s" % (handle, activity))
|
|
|
|
self._buddy_properties_changed_cb(handle, prop)
|
|
|
|
|
2007-03-07 15:27:47 +01:00
|
|
|
def _new_channel_cb(self, object_path, channel_type, handle_type, handle, suppress_handler):
|
2007-03-07 19:46:48 +01:00
|
|
|
if handle_type == CONNECTION_HANDLE_TYPE_ROOM and channel_type == CHANNEL_TYPE_TEXT:
|
2007-03-07 15:27:47 +01:00
|
|
|
channel = Channel(self._conn._dbus_object._named_service, object_path)
|
|
|
|
|
|
|
|
# hack
|
|
|
|
channel._valid_interfaces.add(CHANNEL_INTERFACE_GROUP)
|
|
|
|
|
|
|
|
current, local_pending, remote_pending = channel[CHANNEL_INTERFACE_GROUP].GetAllMembers()
|
|
|
|
|
|
|
|
if local_pending:
|
|
|
|
for act_id, act_handle in self._activities.items():
|
|
|
|
if handle == act_handle:
|
2007-03-07 19:46:48 +01:00
|
|
|
self.emit("activity-invitation", act_id)
|
|
|
|
|
|
|
|
elif handle_type == CONNECTION_HANDLE_TYPE_CONTACT and \
|
|
|
|
channel_type in [CHANNEL_TYPE_TEXT, CHANNEL_TYPE_STREAMED_MEDIA]:
|
|
|
|
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):
|
|
|
|
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,
|
|
|
|
reply_handler=lambda *args: self._activity_properties_changed_cb(handle, *args),
|
|
|
|
error_handler=lambda *args: self._log_error_cb("getting activity properties", *args))
|
|
|
|
|
2007-03-14 15:59:30 +01:00
|
|
|
def set_activity_properties(self, act_id, props):
|
|
|
|
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-04-12 23:03:30 +02:00
|
|
|
self._conn[CONN_INTERFACE_ACTIVITY_PROPERTIES].SetProperties(handle, props,
|
|
|
|
reply_handler=self._ignore_success_cb,
|
2007-04-13 21:42:54 +02:00
|
|
|
error_handler=lambda *args: self._log_error_cb("setting activity properties", *args))
|
2007-03-14 15:59:30 +01:00
|
|
|
|
|
|
|
def _activity_properties_changed_cb(self, room, properties):
|
|
|
|
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
|