Read service type from the .activity file

This commit is contained in:
Marco Pesenti Gritti 2006-07-14 16:40:45 +02:00
parent 5ff09a10f7
commit db08c3795f
7 changed files with 59 additions and 32 deletions

View File

@ -14,7 +14,6 @@ from sugar.p2p.model.RemoteModel import RemoteModel
from NotificationBar import NotificationBar
from NavigationToolbar import NavigationToolbar
_BROWSER_ACTIVITY_TYPE = "_web_olpc._udp"
_SERVICE_URI_TAG = "URI"
_SERVICE_TITLE_TAG = "Title"
@ -23,8 +22,8 @@ class BrowserActivity(Activity):
FOLLOWING = 2
LEADING = 3
def __init__(self, args):
Activity.__init__(self, _BROWSER_ACTIVITY_TYPE)
def __init__(self, service, args):
Activity.__init__(self, service)
if len(args) > 0:
self.uri = args[0]
@ -65,7 +64,6 @@ class BrowserActivity(Activity):
logging.debug('Track browser activities')
self._pservice.connect('service-appeared', self._service_appeared_cb)
self._pservice.track_service_type(_BROWSER_ACTIVITY_TYPE)
self._pservice.track_service_type(LocalModel.SERVICE_TYPE)
# Join the shared activity if we were started from one
@ -89,8 +87,9 @@ class BrowserActivity(Activity):
self._model = RemoteModel(self._model_service, self._notif_service)
self._model.add_listener(self.__shared_location_changed_cb)
def get_default_type(self):
return _BROWSER_ACTIVITY_TYPE
def set_default_type(self, default_type):
Activity.set_default_type(self, default_type)
self._pservice.track_service_type(default_type)
def _update_shared_location(self):
address = self.embed.get_address()
@ -125,7 +124,7 @@ class BrowserActivity(Activity):
# Publish ourselves on the network
properties = {_SERVICE_URI_TAG: escaped_url, _SERVICE_TITLE_TAG: escaped_title}
self._share_service = self._pservice.share_activity(self,
stype=_BROWSER_ACTIVITY_TYPE, properties=properties)
stype=self._default_type, properties=properties)
# Create our activity-specific browser sharing service
self._model = LocalModel(self, self._pservice, self._share_service)

View File

@ -2,3 +2,4 @@
name = Web
id = com.redhat.Sugar.BrowserActivity
python_module = BrowserActivity.BrowserActivity
default_type = _web_olpc._udp

View File

@ -1,5 +1,7 @@
import gobject
from sugar.presence.PresenceService import PresenceService
class ActivityInfo:
def __init__(self, service):
self._service = service
@ -15,10 +17,12 @@ class ActivitiesModel(gobject.GObject):
__gsignals__ = {
'activity-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT])),
'activity-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
}
def __init__(self):
gobject.GObject(self)
gobject.GObject.__init__(self)
self._activities = []
@ -33,7 +37,7 @@ class ActivitiesModel(gobject.GObject):
self.emit('activity-added', activity_info)
def __iter__(self):
return activities.__iter__()
return self._activities.__iter__()
def _on_new_service_adv_cb(self, pservice, activity_id, short_stype):
if activity_id:

View File

@ -30,6 +30,14 @@ class ActivityModule:
"""Get the path to activity directory."""
return self._directory
def get_default_type(self):
"""Get the the type of the default activity service."""
return self._default_type
def set_default_type(self, default_type):
"""Set the the type of the default activity service."""
self._default_type = default_type
class ActivityRegistry:
"""Service that tracks the available activities"""
@ -65,12 +73,20 @@ class ActivityRegistry:
logging.error('%s miss the required name option' % (path))
return False
if cp.has_option('Activity', 'default_type'):
default_type = cp.get('Activity', 'default_type')
else:
default_type = None
if cp.has_option('Activity', 'exec'):
activity_exec = cp.get('Activity', 'exec')
elif cp.has_option('Activity', 'python_module'):
python_module = cp.get('Activity', 'python_module')
python_module = cp.get('Activity', 'python_module')
activity_exec = '%s %s %s' % (env.get_activity_runner(),
activity_id, python_module)
if default_type:
activity_exec += ' ' + default_type
env.add_to_python_path(directory)
else:
logging.error('%s must specifiy exec or python_module' % (path))
@ -79,6 +95,8 @@ class ActivityRegistry:
module = ActivityModule(name, activity_id, activity_exec, directory)
self._activities.append(module)
module.set_default_type(default_type)
return True
def list_activities(self):

View File

@ -4,6 +4,7 @@ import gtk
import wnck
from sugar.activity import Activity
from ActivitiesModel import ActivitiesModel
class NewActivityButton(gtk.MenuToolButton):
def __init__(self, home):
@ -45,13 +46,13 @@ class ActivitiesGrid(gtk.VBox):
for activity in model:
self._add(activity)
screen.connect('activity-added', self.__activity_added_cb)
screen.connect('activity-removed', self.__activity_removed_cb)
model.connect('activity-added', self.__activity_added_cb)
model.connect('activity-removed', self.__activity_removed_cb)
def __activity_added_cb(self, model, activity):
self._add(activity)
def __activity_closed_cb(self, model, activity):
def __activity_removed_cb(self, model, activity):
self._remove(window)
def _remove(self, activity):

View File

@ -13,4 +13,7 @@ theme.setup()
lw = LogWriter(sys.argv[1])
lw.start()
Activity.register_factory(sys.argv[1], sys.argv[2])
if len(sys.argv) == 4:
Activity.register_factory(sys.argv[1], sys.argv[2], sys.argv[3])
else:
Activity.register_factory(sys.argv[1], sys.argv[2])

View File

@ -31,7 +31,8 @@ def get_factory(activity_name):
class ActivityFactory(dbus.service.Object):
"""Dbus service that takes care of creating new instances of an activity"""
def __init__(self, activity_name, activity_class):
def __init__(self, name, activity_class, default_type):
self._default_type = default_type
splitted_module = activity_class.rsplit('.', 1)
module_name = splitted_module[0]
class_name = splitted_module[1]
@ -50,21 +51,19 @@ class ActivityFactory(dbus.service.Object):
self._class = getattr(module, class_name)
bus = dbus.SessionBus()
factory = get_factory(activity_name)
factory = get_factory(name)
bus_name = dbus.service.BusName(factory, bus = bus)
dbus.service.Object.__init__(self, bus_name, get_path(factory))
@dbus.service.method("com.redhat.Sugar.ActivityFactory")
def create_with_service(self, serialized_service, args):
service = None
if serialized_service is not None:
service = Service.deserialize(serialized_service)
activity = self._class(args)
activity = self._class(service, args)
@dbus.service.method("com.redhat.Sugar.ActivityFactory")
def create(self):
self.create_with_service(None, [])
activity = self._class(None, [])
activity.set_default_type(self._default_type)
def create(activity_name, service = None, args = None):
"""Create a new activity from his name."""
@ -82,9 +81,9 @@ def create(activity_name, service = None, args = None):
else:
factory.create()
def register_factory(activity_name, activity_class):
def register_factory(name, activity_class, default_type=None):
"""Register the activity factory."""
factory = ActivityFactory(activity_name, activity_class)
factory = ActivityFactory(name, activity_class, default_type)
gtk.main()
@ -143,21 +142,20 @@ class ActivityDbusService(dbus.service.Object):
class Activity(gtk.Window):
"""Base Activity class that all other Activities derive from."""
def __init__(self, default_type, activity_id = None):
def __init__(self, service = None):
gtk.Window.__init__(self)
if activity_id is None:
self._activity_id = sugar.util.unique_id()
if service and service.has_key('activity_id'):
self._activity_id = service['activity_id']
self._shared = True
else:
self._activity_id = activity_id
self._activity_id = sugar.util.unique_id()
self._shared = False
self._dbus_service = None
self._initial_service = None
self._activity_object = None
self._shared = False
if type(default_type) != type("") or not len(default_type):
raise ValueError("Default type must be a valid string.")
self._default_type = default_type
self._default_type = None
keybindings.setup_global_keys(self)
@ -186,7 +184,10 @@ class Activity(gtk.Window):
Allows subclasses to use their own dbus service object if they choose."""
return ActivityDbusService(self.window.xid, self)
def default_type(self):
def set_default_type(self, default_type):
self._default_type = default_type
def get_default_type(self):
return self._default_type
def set_shared(self):