From 2c272cc8925540562b9a45202b674d7a75a33263 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Sun, 21 May 2006 21:30:26 -0400 Subject: [PATCH] Do buddy icons --- sugar/chat/chat.py | 51 ++++++++++++++++++++++++++++++++++++++------- sugar/p2p/Buddy.py | 33 ++++++++++++++++++++++++++--- sugar/p2p/Group.py | 13 +++++++----- sugar/p2p/Stream.py | 24 +++++++++++++++++++-- 4 files changed, 104 insertions(+), 17 deletions(-) diff --git a/sugar/chat/chat.py b/sugar/chat/chat.py index a04b8b4b..d7bc85d0 100755 --- a/sugar/chat/chat.py +++ b/sugar/chat/chat.py @@ -2,6 +2,7 @@ # -*- tab-width: 4; indent-tabs-mode: t -*- import sys +import base64 import dbus import dbus.service @@ -292,7 +293,7 @@ class BuddyChat(Chat): self.activity_set_tab_icon_name("im") self.activity_show_icon(True) self._stream_writer = self._controller.new_buddy_writer(self._buddy.get_service_name()) - + def recv_message(self, sender, msg): Chat.recv_message(self, self._buddy, msg) self._controller.notify_new_message(self, self._buddy) @@ -300,7 +301,21 @@ class BuddyChat(Chat): def activity_on_close_from_user(self): Chat.activity_on_close_from_user(self) del self._chats[self._buddy] - + + +class BuddyIconRequestHandler(object): + def __init__(self, group, stream): + self._group = group + self._stream = stream + self._stream.register_handler(self._handle_buddy_icon_request, "get_buddy_icon") + + def _handle_buddy_icon_request(self): + """XMLRPC method, return the owner's icon encoded with base64.""" + icon = self._group.get_owner().get_icon() + if icon: + return base64.b64encode(icon) + return '' + class GroupChat(Chat): @@ -327,7 +342,8 @@ class GroupChat(Chat): def _start(self): self._group = LocalGroup() - self._group.add_presence_listener(self._on_group_event) + self._group.add_presence_listener(self._on_group_presence_event) + self._group.add_service_listener(self._on_group_service_event) self._group.join() name = self._group.get_owner().get_service_name() @@ -336,6 +352,7 @@ class GroupChat(Chat): # specific buddy chats buddy_service = Service(name, CHAT_SERVICE_TYPE, CHAT_SERVICE_PORT) self._buddy_stream = Stream.new_from_service(buddy_service, self._group) + self._buddy_icon_handler = BuddyIconRequestHandler(self._group, self._buddy_stream) self._buddy_stream.set_data_listener(self._buddy_recv_message) buddy_service.register(self._group) @@ -343,8 +360,8 @@ class GroupChat(Chat): group_service = Service(name, GROUP_CHAT_SERVICE_TYPE, GROUP_CHAT_SERVICE_PORT, GROUP_CHAT_SERVICE_ADDRESS) - self._group.add_service(group_service) - + self._group.add_service(group_service) + self._group_stream = Stream.new_from_service(group_service, self._group) self._group_stream.set_data_listener(self._group_recv_message) self._stream_writer = self._group_stream.new_writer() @@ -432,7 +449,6 @@ class GroupChat(Chat): def _on_buddyList_buddy_selected(self, widget, *args): (model, aniter) = widget.get_selection().get_selected() name = self._buddy_list_model.get(aniter, self._MODEL_COL_NICK) - print "Selected %s" % name def _on_buddyList_buddy_double_clicked(self, widget, *args): """ Select the chat for this buddy or group """ @@ -444,7 +460,28 @@ class GroupChat(Chat): self._chats[buddy] = chat chat.activity_connect_to_shell() - def _on_group_event(self, action, buddy): + def _request_buddy_icon(self, buddy): + writer = self.new_buddy_writer(buddy.get_service_name()) + icon = writer.custom_request("get_buddy_icon") + if icon and len(icon): + icon = base64.b64decode(icon) + print "Setting buddy icon for '%s' to %s" % (buddy.get_nick_name(), icon) + buddy.set_icon(icon) + + def _on_group_service_event(self, action, service): + if action == Group.SERVICE_ADDED: + # Look for the olpc chat service + if service.get_type() == CHAT_SERVICE_TYPE: + # Find the buddy this service belongs to + buddy = self._group.get_buddy(service.get_name()) + if buddy and buddy.get_address() == service.get_address(): + # Try to get the buddy's icon + if buddy.get_nick_name() != self._group.get_owner().get_nick_name(): + self._request_buddy_icon(buddy) + elif action == Group.SERVICE_REMOVED: + pass + + def _on_group_presence_event(self, action, buddy): if buddy.get_nick_name() == self._group.get_owner().get_nick_name(): # Do not show ourself in the buddy list pass diff --git a/sugar/p2p/Buddy.py b/sugar/p2p/Buddy.py index 9a4d985a..f3895a32 100644 --- a/sugar/p2p/Buddy.py +++ b/sugar/p2p/Buddy.py @@ -6,18 +6,35 @@ from Service import Service PRESENCE_SERVICE_TYPE = "_olpc_presence._tcp" PRESENCE_SERVICE_PORT = 6000 -class Buddy: +class BuddyBase: def __init__(self, service, nick_name): self._service = service self._nick_name = nick_name + + def get_icon(self): + """Return the buddies icon, if any.""" + return self._icon + def get_address(self): + return self._service.get_address() + def get_service_name(self): return self._service.get_name() def get_nick_name(self): return self._nick_name -class Owner(Buddy): +class Buddy(BuddyBase): + """Normal buddy class.""" + + def set_icon(self, icon): + """Can only set icon for other buddies. The Owner + takes care of setting it's own icon.""" + self._icon = icon + + +class Owner(BuddyBase): + """Class representing the owner of this machine/instance.""" def __init__(self, group): self._group = group @@ -26,8 +43,18 @@ class Owner(Buddy): nick = "n00b" service = Service(nick, PRESENCE_SERVICE_TYPE, PRESENCE_SERVICE_PORT) + BuddyBase.__init__(self, service, nick) + + sugar_dir = os.path.abspath(os.path.expanduser("~/.sugar")) + icon = None + for fname in os.listdir(sugar_dir): + if not fname.startswith("buddy-icon."): + continue + fd = open(os.path.join(sugar_dir, fname), "r") + self._icon = fd.read() + fd.close() + break - Buddy.__init__(self, service, nick) def register(self): self._service.register(self._group) diff --git a/sugar/p2p/Group.py b/sugar/p2p/Group.py index 4faca270..c21bbf8d 100644 --- a/sugar/p2p/Group.py +++ b/sugar/p2p/Group.py @@ -7,6 +7,8 @@ from Service import Service from sugar.p2p.model.Store import Store import presence +_OLPC_SERVICE_TYPE_PREFIX = "_olpc" + class Group: SERVICE_ADDED = "service_added" SERVICE_REMOVED = "service_removed" @@ -78,11 +80,12 @@ class LocalGroup(Group): def get_service(self, name, stype): if self._services.has_key((name, stype)): return self._services[(name, stype)] - else: - return None + return None def get_buddy(self, name): - return self._buddies[name] + if self._buddies.has_key(name): + return self._buddies[name] + return None def _add_buddy(self, buddy): bid = buddy.get_nick_name() @@ -104,7 +107,7 @@ class LocalGroup(Group): elif action == presence.ACTION_SERVICE_REMOVED: if stype == PRESENCE_SERVICE_TYPE: self._remove_buddy(name) - elif stype.startswith("_olpc"): + elif stype.startswith(_OLPC_SERVICE_TYPE_PREFIX): self.remove_service((name, stype)) def _on_service_resolved(self, interface, protocol, name, stype, domain, @@ -119,5 +122,5 @@ class LocalGroup(Group): if stype == PRESENCE_SERVICE_TYPE: self._add_buddy(Buddy(service, name)) - elif stype.startswith("_olpc"): + elif stype.startswith(_OLPC_SERVICE_TYPE_PREFIX): self.add_service(service) diff --git a/sugar/p2p/Stream.py b/sugar/p2p/Stream.py index 77d8945b..e6c1a912 100644 --- a/sugar/p2p/Stream.py +++ b/sugar/p2p/Stream.py @@ -1,11 +1,14 @@ import xmlrpclib import socket +import traceback import network from MostlyReliablePipe import MostlyReliablePipe class Stream(object): def __init__(self, service, group): + if not service: + raise ValueError("service must be valid") self._service = service self._group = group self._owner_nick_name = self._group.get_owner().get_nick_name() @@ -32,6 +35,8 @@ class Stream(object): class UnicastStreamWriter(object): def __init__(self, stream, service, owner_nick_name): # set up the writer + if not service: + raise ValueError("service must be valid") self._service = service self._owner_nick_name = owner_nick_name self._address = self._service.get_address() @@ -40,6 +45,7 @@ class UnicastStreamWriter(object): self._writer = xmlrpclib.ServerProxy(self._xmlrpc_addr) def write(self, data): + """Write some data to the default endpoint of this pipe on the remote server.""" try: self._writer.message(self._owner_nick_name, data) return True @@ -47,6 +53,15 @@ class UnicastStreamWriter(object): traceback.print_exc() return False + def custom_request(self, method_name, *args): + """Call a custom XML-RPC method on the remote server.""" + try: + method = getattr(self._writer, method_name) + return method(*args) + except (socket.error, xmlrpclib.Fault, xmlrpclib.ProtocolError): + traceback.print_exc() + return None + class UnicastStream(Stream): def __init__(self, service, group): @@ -62,18 +77,23 @@ class UnicastStream(Stream): while not started and tries > 0: try: self._reader = network.GlibXMLRPCServer(("", port)) - self._reader.register_instance(self) + self._reader.register_function(self._message, "message") started = True except(socket.error): port = port + 1 tries = tries - 1 self._service.set_port(port) - def message(self, nick_name, message): + def _message(self, nick_name, message): """Called by the XMLRPC server when network data arrives.""" self.recv(nick_name, message) return True + def register_handler(self, handler, name): + if name == "message": + raise ValueError("Handler name 'message' is a reserved handler.") + self._reader.register_function(handler, name) + def new_writer(self, service): return UnicastStreamWriter(self, service, self._owner_nick_name)