Move the model to his own module

This commit is contained in:
Marco Pesenti Gritti
2006-09-15 12:52:37 +02:00
parent 14383f4fc7
commit ca19f0f251
12 changed files with 14 additions and 10 deletions
+78
View File
@@ -0,0 +1,78 @@
import os
from ConfigParser import ConfigParser
import gobject
from sugar.canvas.IconColor import IconColor
from sugar.presence import PresenceService
from sugar import env
class Friend:
def __init__(self, name, color):
self._name = name
self._color = color
def get_name(self):
return self._name
def get_color(self):
return IconColor(self._color)
def get_buddy(self):
pservice = PresenceService.get_instance()
return pservice.get_buddy_by_name(self._name)
class Friends(gobject.GObject):
__gsignals__ = {
'friend-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([object])),
'friend-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([object])),
}
def __init__(self):
gobject.GObject.__init__(self)
self._list = []
self._path = os.path.join(env.get_profile_path(), 'friends')
self.load()
def has_buddy(self, buddy):
for friend in self:
if friend.get_name() == buddy.get_name():
return True
return False
def add_friend(self, name, color):
friend = Friend(name, color)
self._list.append(friend)
self.emit('friend-added', friend)
def add_buddy(self, buddy):
if not self.has_buddy(buddy):
self.add_friend(buddy.get_name(), buddy.get_color())
self.save()
def __iter__(self):
return self._list.__iter__()
def load(self):
cp = ConfigParser()
if cp.read([self._path]):
for name in cp.sections():
self.add_friend(name, cp.get(name, 'color'))
def save(self):
cp = ConfigParser()
for friend in self:
section = friend.get_name()
cp.add_section(section)
cp.set(section, 'color', friend.get_color().to_string())
fileobject = open(self._path, 'w')
cp.write(fileobject)
fileobject.close()
+54
View File
@@ -0,0 +1,54 @@
import gobject
import conf
from sugar.presence import PresenceService
from sugar.canvas.IconColor import IconColor
class Invite:
def __init__(self, issuer, bundle_id, activity_id):
self._issuer = issuer
self._activity_id = activity_id
self._bundle_id = bundle_id
def get_icon(self):
reg = conf.get_activity_registry()
return reg.get_activity(self._bundle_id).get_icon()
def get_color(self):
pservice = PresenceService.get_instance()
buddy = pservice.get_buddy_by_name(self._issuer)
if buddy != None:
return IconColor(buddy.get_color())
else:
return IconColor('white')
def get_activity_id(self):
return self._activity_id
def get_bundle_id(self):
return self._bundle_id
class Invites(gobject.GObject):
__gsignals__ = {
'invite-added': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([object])),
'invite-removed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([object])),
}
def __init__(self):
gobject.GObject.__init__(self)
self._list = []
def add_invite(self, issuer, bundle_id, activity_id):
invite = Invite(issuer, bundle_id, activity_id)
self._list.append(invite)
self.emit('invite-added', invite)
def remove_invite(self, invite):
self._list.remove(invite)
self.emit('invite-removed', invite)
def __iter__(self):
return self._list.__iter__()
+7
View File
@@ -0,0 +1,7 @@
sugardir = $(pkgdatadir)/shell/model
sugar_PYTHON = \
__init__.py \
Friends.py \
Invites.py \
Owner.py \
ShellModel.py
+53
View File
@@ -0,0 +1,53 @@
import os
import random
import base64
import conf
from sugar import env
from sugar.p2p import Stream
from sugar.presence import PresenceService
PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp"
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):
profile = conf.get_profile()
self._nick = profile.get_nick_name()
user_dir = profile.get_path()
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
self._pservice = PresenceService.get_instance()
def announce(self):
# Create and announce our presence
color = conf.get_profile().get_color()
props = { 'color': color.to_string() }
self._service = self._pservice.register_service(self._nick,
PRESENCE_SERVICE_TYPE, properties=props)
print "Owner '%s' using port %d" % (self._nick, self._service.get_port())
self._icon_stream = Stream.Stream.new_from_service(self._service)
self._icon_stream.register_reader_handler(self._handle_buddy_icon_request, "get_buddy_icon")
self._icon_stream.register_reader_handler(self._handle_invite, "invite")
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 ""
def _handle_invite(self, issuer, bundle_id, activity_id):
"""XMLRPC method, called when the owner is invited to an activity."""
self._invites.add_invite(issuer, bundle_id, activity_id)
return ''
+86
View File
@@ -0,0 +1,86 @@
import gobject
from sugar.presence import PresenceService
from sugar.activity import ActivityFactory
from sugar.activity import Activity
from model.Friends import Friends
from model.Invites import Invites
from model.Owner import ShellOwner
class ShellModel(gobject.GObject):
__gsignals__ = {
'activity-opened': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
'activity-changed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT])),
'activity-closed': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, ([gobject.TYPE_PYOBJECT]))
}
def __init__(self):
gobject.GObject.__init__(self)
self._hosts = {}
self._current_activity = None
PresenceService.start()
self._pservice = PresenceService.get_instance()
self._owner = ShellOwner()
self._owner.announce()
self._friends = Friends()
self._invites = Invites()
def get_friends(self):
return self._friends
def get_invites(self):
return self._invites
def get_owner(self):
return self._owner
def add_activity(self, activity_host):
self._hosts[activity_host.get_xid()] = activity_host
self.emit('activity-opened', activity_host)
def set_current_activity(self, activity_xid):
activity_host = self._hosts[activity_xid]
if self._current_activity == activity_host:
return
self._current_activity = activity_host
self.emit('activity-changed', activity_host)
def remove_activity(self, activity_xid):
if self._hosts.has_key(activity_xid):
host = self._hosts[activity_xid]
self.emit('activity-closed', host)
del self._hosts[activity_xid]
def get_activity(self, activity_id):
for host in self._hosts.values():
if host.get_id() == activity_id:
return host
return None
def get_current_activity(self):
return self._current_activity
def join_activity(self, bundle_id, activity_id):
activity = self.get_activity(activity_id)
if activity:
activity.present()
else:
activity_ps = self._pservice.get_activity(activity_id)
if activity_ps:
activity = ActivityFactory.create(bundle_id)
activity.join(activity_ps.object_path())
else:
logging.error('Cannot start activity.')
def start_activity(self, activity_type):
activity = ActivityFactory.create(activity_type)
activity.execute('test', [])
return activity
View File