sugar-toolkit-gtk3/sugar/simulator.py

106 lines
2.7 KiB
Python
Raw Normal View History

2006-09-10 13:31:08 +02:00
import os
import gtk
import gobject
2006-09-21 22:43:51 +02:00
import base64
import dbus
2006-09-10 13:31:08 +02:00
from sugar.session.TestSession import TestSession
from sugar.presence import PresenceService
from sugar.p2p import Stream
from sugar import util
_PRESENCE_SERVICE_TYPE = "_presence_olpc._tcp"
class _SimulatedActivity:
def __init__(self):
self._id = util.unique_id()
def get_id(self):
return self._id
2006-09-21 22:43:51 +02:00
class _SimulatedShellOwner(object):
def __init__(self, nick, color, icon_file=None):
2006-09-10 13:31:08 +02:00
self._pservice = PresenceService.get_instance()
self._color = color
self._nick = nick
2006-09-21 22:43:51 +02:00
self._icon = None
if icon_file:
fd = open(icon_file, "r")
self._icon = fd.read()
fd.close()
2006-09-10 13:31:08 +02:00
def announce(self):
props = { 'color': self._color.to_string() }
self._service = self._pservice.register_service(self._nick,
_PRESENCE_SERVICE_TYPE, properties=props)
self._stream = Stream.Stream.new_from_service(self._service)
self._stream.register_reader_handler(self._handle_buddy_icon_request, "get_buddy_icon")
self._stream.register_reader_handler(self._handle_invite, "invite")
def _handle_buddy_icon_request(self):
2006-09-21 22:43:51 +02:00
if self._icon:
return base64.b64encode(self._icon)
2006-09-10 13:31:08 +02:00
return ''
def _handle_invite(self, issuer, bundle_id, activity_id):
return ''
2006-09-21 22:43:51 +02:00
def set_current_activity(self, activity_id):
self._service.set_published_value('curact', dbus.String(activity_id))
2006-09-10 13:50:22 +02:00
class _Timeline:
def __init__(self, time_factor):
self._time_factor = time_factor
def add(self, action, minutes):
gobject.timeout_add(int(1000 * 60 * minutes * self._time_factor),
self._execute_action_cb, action)
def _execute_action_cb(self, action):
action.execute()
return False
class ShareActivityAction:
2006-09-21 22:43:51 +02:00
def __init__(self, title, activity_type, callback=None):
2006-09-10 13:50:22 +02:00
self._title = title
self._type = activity_type
2006-09-21 22:43:51 +02:00
self._callback = callback
2006-09-10 13:50:22 +02:00
def execute(self):
activity = _SimulatedActivity()
properties = { 'title' : self._title }
pservice = PresenceService.get_instance()
2006-09-21 22:43:51 +02:00
act_service = pservice.share_activity(activity, self._type, properties)
if self._callback is not None:
self._callback(activity, act_service)
2006-09-10 13:50:22 +02:00
2006-09-10 13:31:08 +02:00
class Bot:
def __init__(self, nick, color):
self._nick = nick
self._color = color
2006-09-10 13:50:22 +02:00
self._timeline = _Timeline(0.01)
2006-09-21 22:43:51 +02:00
self._owner = None
self._icon_file = None
2006-09-10 13:31:08 +02:00
os.environ['SUGAR_NICK_NAME'] = nick
os.environ['SUGAR_COLOR'] = color.to_string()
2006-09-10 13:31:08 +02:00
def start(self):
session = TestSession()
session.start()
PresenceService.start()
2006-09-21 22:43:51 +02:00
self._owner = _SimulatedShellOwner(self._nick, self._color, self._icon_file)
self._owner.announce()
2006-09-10 13:31:08 +02:00
2006-09-21 22:43:51 +02:00
self._pservice = PresenceService.get_instance()
2006-09-10 13:31:08 +02:00
gtk.main()
2006-09-10 13:50:22 +02:00
def add_action(self, action, minutes):
self._timeline.add(action, minutes)