sugar-toolkit-gtk3/sugar/presence/Buddy.py

149 lines
4.4 KiB
Python
Raw Normal View History

2006-06-09 23:23:42 +02:00
import pwd
import os
2006-06-13 00:31:26 +02:00
import base64
2006-06-09 23:23:42 +02:00
import pygtk
pygtk.require('2.0')
2006-06-13 00:31:26 +02:00
import gtk, gobject
2006-06-09 23:23:42 +02:00
2006-06-13 00:31:26 +02:00
from sugar.p2p import Stream
from sugar.p2p import network
2006-06-09 23:23:42 +02:00
PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp"
2006-06-13 00:31:26 +02:00
class Buddy(gobject.GObject):
2006-06-09 23:23:42 +02:00
"""Represents another person on the network and keeps track of the
activities and resources they make available for sharing."""
2006-06-13 00:31:26 +02:00
__gsignals__ = {
'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([])),
'service-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'service-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
}
2006-06-09 23:23:42 +02:00
def __init__(self, service):
2006-06-13 00:31:26 +02:00
gobject.GObject.__init__(self)
2006-06-09 23:23:42 +02:00
self._services = {}
self._nick_name = service.get_name()
self._address = service.get_address()
self._valid = False
self._icon = None
2006-06-13 00:31:26 +02:00
self._icon_tries = 0
self._owner = False
2006-06-09 23:23:42 +02:00
self.add_service(service)
2006-06-13 00:31:26 +02:00
def _request_buddy_icon_cb(self, result_status, response, user_data):
"""Callback when icon request has completed."""
icon = response
service = user_data
if result_status == network.RESULT_SUCCESS:
if icon and len(icon):
icon = base64.b64decode(icon)
print "Buddy icon for '%s' is size %d" % (self._nick_name, len(icon))
self.set_icon(icon)
if (result_status == network.RESULT_FAILED or not icon) and self._icon_tries < 3:
self._icon_tries = self._icon_tries + 1
print "Failed to retrieve buddy icon for '%s' on try %d of %d" % (self._nick_name, \
self._icon_tries, 3)
gobject.timeout_add(1000, self._request_buddy_icon, service)
return False
def _request_buddy_icon(self, service):
"""Contact the buddy to retrieve the buddy icon."""
buddy_stream = Stream.Stream.new_from_service(service, start_reader=False)
writer = buddy_stream.new_writer(service)
success = writer.custom_request("get_buddy_icon", self._request_buddy_icon_cb, service)
if not success:
del writer, buddy_stream
gobject.timeout_add(1000, self._request_buddy_icon, service)
return False
2006-06-09 23:23:42 +02:00
def add_service(self, service):
2006-06-13 00:31:26 +02:00
"""Adds a new service to this buddy's service list, returning
True if the service was successfully added, and False if it was not."""
if service.get_name() != self._nick_name:
return False
if service.get_address() != self._address:
return False
2006-06-09 23:23:42 +02:00
if service.get_type() in self._services.keys():
2006-06-13 00:31:26 +02:00
return False
self._services[service.get_type()] = service
if self._valid:
self.emit("service-added", service)
2006-06-09 23:23:42 +02:00
if service.get_type() == PRESENCE_SERVICE_TYPE:
# A buddy isn't valid until its official presence
# service has been found and resolved
self._valid = True
2006-06-14 21:01:17 +02:00
print 'Requesting buddy icon %s' % self._nick_name
2006-06-13 00:31:26 +02:00
self._request_buddy_icon(service)
return True
2006-06-09 23:23:42 +02:00
def remove_service(self, service):
"""Remove a service from a buddy; ie, the activity was closed
or the buddy went away."""
2006-06-13 00:31:26 +02:00
if service.get_address() != self._address:
return
if service.get_name() != self._nick_name:
return
if self._services.has_key(service.get_type()):
if self._valid:
self.emit("service-removed", service)
2006-06-09 23:23:42 +02:00
del self._services[service.get_type()]
if service.get_type() == PRESENCE_SERVICE_TYPE:
self._valid = False
2006-06-13 00:31:26 +02:00
def get_service_of_type(self, stype):
"""Return a service of a certain type, or None if the buddy
doesn't provide that service."""
if self._services.has_key(stype):
return self._services[stype]
return None
2006-06-09 23:23:42 +02:00
def is_valid(self):
"""Return whether the buddy is valid or not. A buddy is
not valid until its official presence service has been found
and successfully resolved."""
return self._valid
def get_icon_pixbuf(self):
if self._icon:
pbl = gtk.gdk.PixbufLoader()
pbl.write(self._icon)
pbl.close()
return pbl.get_pixbuf()
else:
return None
def get_icon(self):
"""Return the buddies icon, if any."""
return self._icon
def get_address(self):
return self._address
def get_nick_name(self):
return self._nick_name
def set_icon(self, icon):
"""Can only set icon for other buddies. The Owner
takes care of setting it's own icon."""
2006-06-13 00:31:26 +02:00
if icon != self._icon:
self._icon = icon
self.emit("icon-changed")
2006-06-09 23:23:42 +02:00
2006-06-13 00:31:26 +02:00
def is_owner(self):
return self._owner
2006-06-09 23:23:42 +02:00
2006-06-13 00:31:26 +02:00
class Owner(Buddy):
"""Class representing the owner of the machine. This is the client
portion of the Owner, paired with the server portion in Owner.py."""
def __init__(self, service):
Buddy.__init__(self, service)
self._owner = True