2006-05-12 08:34:20 +02:00
|
|
|
import pwd
|
|
|
|
import os
|
|
|
|
|
2006-05-22 21:25:10 +02:00
|
|
|
import pygtk
|
|
|
|
pygtk.require('2.0')
|
|
|
|
import gtk
|
|
|
|
|
2006-05-15 20:48:08 +02:00
|
|
|
from Service import Service
|
2006-05-22 08:11:39 +02:00
|
|
|
from sugar import env
|
2006-05-12 08:34:20 +02:00
|
|
|
|
|
|
|
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[:]
|
|
|
|
|
|
|
|
|
|
|
|
class Buddy(object):
|
|
|
|
def __init__(self, service):
|
|
|
|
self._services = {}
|
|
|
|
self._services[service.get_type()] = service
|
|
|
|
self._nick_name = service.get_name()
|
|
|
|
self._address = service.get_address()
|
2006-05-22 19:30:13 +02:00
|
|
|
self._icon = None
|
2006-05-22 04:21:42 +02:00
|
|
|
|
2006-05-22 21:25:10 +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-12 08:34:20 +02:00
|
|
|
|
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
|
2006-05-12 08:34:20 +02:00
|
|
|
|
|
|
|
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 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."""
|
2006-05-12 08:34:20 +02:00
|
|
|
def __init__(self, group):
|
|
|
|
self._group = group
|
|
|
|
|
|
|
|
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
|
|
|
|
2006-05-22 19:30:13 +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
|
2006-05-22 19:30:13 +02:00
|
|
|
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-12 08:34:20 +02:00
|
|
|
|
2006-05-22 08:11:39 +02:00
|
|
|
def set_icon(self, icon):
|
|
|
|
"""Can only set icon in constructor for now."""
|
|
|
|
pass
|
2006-05-12 08:34:20 +02:00
|
|
|
|
|
|
|
def register(self):
|
2006-05-22 08:11:39 +02:00
|
|
|
self._presence_service.register(self._group)
|
2006-05-22 22:05:17 +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
|