2006-06-20 09:19:33 +02:00
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import base64
|
|
|
|
|
2006-09-04 21:34:54 +02:00
|
|
|
import conf
|
2006-06-13 00:51:40 +02:00
|
|
|
from sugar import env
|
|
|
|
from sugar.p2p import Stream
|
2006-07-23 06:56:40 +02:00
|
|
|
from sugar.presence import PresenceService
|
2006-08-30 11:46:14 +02:00
|
|
|
from Friends import Friends
|
2006-09-04 17:00:45 +02:00
|
|
|
from Invites import Invites
|
2006-07-23 06:56:40 +02:00
|
|
|
|
|
|
|
PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp"
|
2006-06-16 19:19:35 +02:00
|
|
|
|
2006-06-13 00:51:40 +02:00
|
|
|
class ShellOwner(object):
|
|
|
|
"""Class representing the owner of this machine/instance. This class
|
|
|
|
runs in the shell and serves up the buddy icon and other stuff. It's the
|
|
|
|
server portion of the Owner, paired with the client portion in Buddy.py."""
|
|
|
|
def __init__(self):
|
2006-08-26 13:03:06 +02:00
|
|
|
profile = conf.get_profile()
|
|
|
|
|
|
|
|
self._nick = profile.get_nick_name()
|
|
|
|
user_dir = profile.get_path()
|
2006-07-26 02:04:15 +02:00
|
|
|
|
2006-06-13 00:51:40 +02:00
|
|
|
self._icon = None
|
|
|
|
for fname in os.listdir(user_dir):
|
|
|
|
if not fname.startswith("buddy-icon."):
|
|
|
|
continue
|
|
|
|
fd = open(os.path.join(user_dir, fname), "r")
|
|
|
|
self._icon = fd.read()
|
|
|
|
fd.close()
|
|
|
|
break
|
|
|
|
|
2006-08-23 17:38:56 +02:00
|
|
|
self._pservice = PresenceService.get_instance()
|
2006-08-30 11:46:14 +02:00
|
|
|
self._friends = Friends()
|
2006-09-04 17:00:45 +02:00
|
|
|
self._invites = Invites()
|
2006-08-30 11:46:14 +02:00
|
|
|
|
|
|
|
def get_friends(self):
|
|
|
|
return self._friends
|
2006-07-26 02:04:15 +02:00
|
|
|
|
2006-09-04 17:00:45 +02:00
|
|
|
def get_invites(self):
|
|
|
|
return self._invites
|
|
|
|
|
2006-07-26 02:04:15 +02:00
|
|
|
def announce(self):
|
|
|
|
# Create and announce our presence
|
2006-08-25 00:49:39 +02:00
|
|
|
color = conf.get_profile().get_color()
|
|
|
|
props = { 'color': color.get_fill_color() }
|
2006-08-23 17:14:46 +02:00
|
|
|
self._service = self._pservice.register_service(self._nick,
|
|
|
|
PRESENCE_SERVICE_TYPE, properties=props)
|
2006-07-26 02:04:15 +02:00
|
|
|
print "Owner '%s' using port %d" % (self._nick, self._service.get_port())
|
2006-07-23 06:56:40 +02:00
|
|
|
self._icon_stream = Stream.Stream.new_from_service(self._service)
|
|
|
|
self._icon_stream.register_reader_handler(self._handle_buddy_icon_request, "get_buddy_icon")
|
2006-09-04 14:30:44 +02:00
|
|
|
self._icon_stream.register_reader_handler(self._handle_invite, "invite")
|
2006-06-13 00:51:40 +02:00
|
|
|
|
|
|
|
def _handle_buddy_icon_request(self):
|
|
|
|
"""XMLRPC method, return the owner's icon encoded with base64."""
|
|
|
|
if self._icon:
|
|
|
|
return base64.b64encode(self._icon)
|
|
|
|
return ""
|
2006-09-04 14:30:44 +02:00
|
|
|
|
2006-09-04 17:00:45 +02:00
|
|
|
def _handle_invite(self, issuer, bundle_id, activity_id):
|
2006-09-04 14:30:44 +02:00
|
|
|
"""XMLRPC method, called when the owner is invited to an activity."""
|
2006-09-04 17:00:45 +02:00
|
|
|
self._invites.add_invite(issuer, bundle_id, activity_id)
|
|
|
|
return ''
|