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

126 lines
2.9 KiB
Python
Raw Normal View History

import pwd
import os
import pygtk
pygtk.require('2.0')
2006-05-22 22:35:31 +02:00
import gtk, gobject
2006-05-15 20:48:08 +02:00
from Service import Service
2006-05-22 08:11:39 +02:00
from sugar import env
PRESENCE_SERVICE_TYPE = "_olpc_presence._tcp"
PRESENCE_SERVICE_PORT = 6000
2006-05-22 08:11:39 +02:00
__buddy_service_types = [PRESENCE_SERVICE_TYPE]
def recognize_buddy_service_type(stype):
if stype not in __buddy_service_types:
__buddy_service_types.append(stype)
def get_recognized_buddy_service_types():
return __buddy_service_types[:]
2006-05-22 22:35:31 +02:00
class Buddy(gobject.GObject):
__gsignals__ = {
'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([]))
}
2006-05-22 08:11:39 +02:00
def __init__(self, service):
2006-05-22 22:35:31 +02:00
gobject.GObject.__init__(self)
2006-05-22 08:11:39 +02:00
self._services = {}
self._services[service.get_type()] = service
self._nick_name = service.get_name()
self._address = service.get_address()
self._icon = None
2006-05-22 04:21:42 +02:00
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
2006-05-22 04:21:42 +02:00
def get_icon(self):
"""Return the buddies icon, if any."""
return self._icon
2006-05-22 04:21:42 +02:00
def get_address(self):
2006-05-22 08:11:39 +02:00
return self._address
def add_service(self, service):
if service.get_name() != self._nick_name:
return False
if service.get_address() != self._address:
return False
if self._services.has_key(service.get_type()):
return False
self._services[service.get_type()] = service
2006-05-22 04:21:42 +02:00
2006-05-22 08:11:39 +02:00
def remove_service(self, stype):
if self._services.has_key(stype):
del self._services[stype]
def get_service(self, stype):
if self._services.has_key(stype):
return self._services[stype]
return None
def get_nick_name(self):
return self._nick_name
2006-05-22 04:21:42 +02:00
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
2006-05-22 22:35:31 +02:00
self.emit('icon-changed')
2006-05-22 08:11:39 +02:00
2006-05-22 04:21:42 +02:00
2006-05-22 08:11:39 +02:00
class Owner(Buddy):
2006-05-22 04:21:42 +02:00
"""Class representing the owner of this machine/instance."""
def __init__(self, group):
self._group = group
nick = env.get_nick_name()
if not nick:
nick = pwd.getpwuid(os.getuid())[0]
if not nick or not len(nick):
nick = "n00b"
2006-05-22 08:11:39 +02:00
self._presence_service = Service(nick, PRESENCE_SERVICE_TYPE, PRESENCE_SERVICE_PORT)
Buddy.__init__(self, self._presence_service)
2006-05-22 04:21:42 +02:00
user_dir = env.get_user_dir()
if not os.path.exists(user_dir):
try:
os.makedirs(user_dir)
except OSError:
pass
for fname in os.listdir(user_dir):
2006-05-22 04:21:42 +02:00
if not fname.startswith("buddy-icon."):
continue
fd = open(os.path.join(user_dir, fname), "r")
2006-05-22 04:21:42 +02:00
self._icon = fd.read()
fd.close()
break
2006-05-22 08:11:39 +02:00
def set_icon(self, icon):
"""Can only set icon in constructor for now."""
pass
def register(self):
2006-05-22 08:11:39 +02:00
self._presence_service.register(self._group)
2006-05-22 22:36:02 +02:00
def notify_service_registered(self, service):
"""New services registered in our group are automatically owned
by us."""
self._services[service.get_type()] = service
def add_service(self, service):
"""Do nothing here, since all services we need to know about
are registered with us by our group."""
pass