From 85ff44db1c49e688fa17e7759f7d785eed8a8755 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 06 Jul 2006 13:59:48 +0000 Subject: Forgot to commit changes... This breaks one-to-one chat, I'm going to fix it. --- (limited to 'sugar') diff --git a/sugar/activity/Activity.py b/sugar/activity/Activity.py index 6226380..8d86f7d 100644 --- a/sugar/activity/Activity.py +++ b/sugar/activity/Activity.py @@ -1,4 +1,5 @@ -# -*- tab-width: 4; indent-tabs-mode: t -*- +import sys +import imp import dbus import dbus.service @@ -21,6 +22,68 @@ ON_LOST_FOCUS_CB = "lost_focus" ON_GOT_FOCUS_CB = "got_focus" ON_PUBLISH_CB = "publish" +def get_path(activity_name): + """Returns the activity path""" + return '/' + activity_name.replace('.', '/') + +def get_factory(activity_name): + """Returns the activity factory""" + return activity_name + '.Factory' + +class ActivityFactory(dbus.service.Object): + """Dbus service that takes care of creating new instances of an activity""" + + def __init__(self, activity_name, activity_class): + splitted_module = activity_class.rsplit('.', 1) + module_name = splitted_module[0] + class_name = splitted_module[1] + + (fp, pathname, description) = imp.find_module(module_name) + module = imp.load_module(module_name, fp, pathname, description) + self._class = getattr(module, class_name) + + bus = dbus.SessionBus() + factory = get_factory(activity_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) + gobject.idle_add(self._start_activity_cb, activity, service) + + @dbus.service.method("com.redhat.Sugar.ActivityFactory") + def create(self, args): + self.create_with_service(None, args) + + def _start_activity_cb(self, activity, service): + activity.connect_to_shell(service) + +def create(activity_name, service = None, args = None): + """Create a new activity from his name.""" + bus = dbus.SessionBus() + + factory_name = get_factory(activity_name) + factory_path = get_path(factory_name) + + proxy_obj = bus.get_object(factory_name, factory_path) + factory = dbus.Interface(proxy_obj, "com.redhat.Sugar.ActivityFactory") + + if service: + serialized_service = service.serialize(service) + factory.create_with_service(serialized_service, args) + else: + factory.create(args) + +def main(activity_name, activity_class): + """Starts the activity main loop.""" + factory = ActivityFactory(activity_name, activity_class) + gtk.main() + class ActivityDbusService(dbus.service.Object): """Base dbus service object that each Activity uses to export dbus methods. @@ -141,7 +204,7 @@ class ActivityDbusService(dbus.service.Object): """Called by the shell to request the activity to publish itself on the network.""" self._call_callback(ON_PUBLISH_CB) - @dbus.service.signal(ACTIVITY_SERVICE_NAME) + @dbus.service.signal(ACTIVITY_SERVICE_NAME) def ActivityShared(self): pass @@ -335,3 +398,6 @@ class Activity(object): def on_close_from_user(self): """Triggered when this Activity is closed by the user.""" pass + +if __name__ == "__main__": + main(sys.argv[1], sys.argv[2]) -- cgit v0.9.1