diff options
author | Marco Pesenti Gritti <marco@localhost.localdomain> | 2006-09-15 10:52:37 (GMT) |
---|---|---|
committer | Marco Pesenti Gritti <marco@localhost.localdomain> | 2006-09-15 10:52:37 (GMT) |
commit | ca19f0f25100006c0f6c84e405e2e6a0cecdd15b (patch) | |
tree | aaab967408d64b21a07ba85a9cb13fc86c411608 /shell/model | |
parent | 14383f4fc7c6e182fc8a79e5f9d56c740b2bc7e7 (diff) |
Move the model to his own module
Diffstat (limited to 'shell/model')
-rw-r--r-- | shell/model/Friends.py | 78 | ||||
-rw-r--r-- | shell/model/Invites.py | 54 | ||||
-rw-r--r-- | shell/model/Makefile.am | 7 | ||||
-rw-r--r-- | shell/model/Owner.py | 53 | ||||
-rw-r--r-- | shell/model/ShellModel.py | 86 | ||||
-rw-r--r-- | shell/model/__init__.py | 0 |
6 files changed, 278 insertions, 0 deletions
diff --git a/shell/model/Friends.py b/shell/model/Friends.py new file mode 100644 index 0000000..6beca66 --- /dev/null +++ b/shell/model/Friends.py @@ -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() diff --git a/shell/model/Invites.py b/shell/model/Invites.py new file mode 100644 index 0000000..24f9913 --- /dev/null +++ b/shell/model/Invites.py @@ -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__() diff --git a/shell/model/Makefile.am b/shell/model/Makefile.am new file mode 100644 index 0000000..2ce494b --- /dev/null +++ b/shell/model/Makefile.am @@ -0,0 +1,7 @@ +sugardir = $(pkgdatadir)/shell/model +sugar_PYTHON = \ + __init__.py \ + Friends.py \ + Invites.py \ + Owner.py \ + ShellModel.py diff --git a/shell/model/Owner.py b/shell/model/Owner.py new file mode 100644 index 0000000..7dec04a --- /dev/null +++ b/shell/model/Owner.py @@ -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 '' diff --git a/shell/model/ShellModel.py b/shell/model/ShellModel.py new file mode 100644 index 0000000..2f9dd25 --- /dev/null +++ b/shell/model/ShellModel.py @@ -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 diff --git a/shell/model/__init__.py b/shell/model/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/shell/model/__init__.py |