diff --git a/shell/sugar-activity b/shell/sugar-activity index 6f5c9741..7757a76b 100755 --- a/shell/sugar-activity +++ b/shell/sugar-activity @@ -18,30 +18,63 @@ import sys import os -import gobject +from ConfigParser import ConfigParser -from sugar.activity import activityfactory from sugar import env -from sugar import util -def _success_cb(handler, activity, loop): - activity.start(util.unique_id()) - loop.quit() +# Setup the environment so that we run inside the Sugar shell +cp = ConfigParser() +cp.read([env.get_profile_path("session.info")]) +os.environ['DBUS_SESSION_BUS_ADDRESS'] = cp.get('Session', 'dbus_address') +os.environ['DISPLAY'] = cp.get('Session', 'display') +del cp -def _error_cb(handler, err, loop): - loop.quit() +import gtk +import dbus +import dbus.glib -ppath = env.get_profile_path() -bus_file = os.path.join(ppath, "session_bus_address") -f = open(bus_file, "r") -bus_name = f.read() -f.close() -os.environ['DBUS_SESSION_BUS_ADDRESS'] = bus_name +from sugar.activity import bundleregistry +from sugar.activity import activityfactory +from sugar.activity import activityfactoryservice -loop = gobject.MainLoop() +def _success_cb(handler, exit): + if exit: + gtk.main_quit() -handler = activityfactory.create(sys.argv[1]) -handler.connect('success', _success_cb, loop) -handler.connect('error', _error_cb, loop) +def _error_cb(handler, err): + print err + gtk.main_quit() -loop.run() +def print_help(self): + sys.exit(0) + +bundle = None + +if len(sys.argv) > 1: + registry = bundleregistry.get_registry() + bundle = registry.find_bundle(sys.argv[1]) + +if bundle == None: + print 'Usage:\n\n' \ + 'sugar-activity [bundle]\n\n' \ + 'Bundle can be a part of the service name or of bundle name.' + sys.exit(0) + +bus = dbus.SessionBus() +bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus') +try: + name = bus_object.GetNameOwner( + bundle.get_service_name(), dbus_interface='org.freedesktop.DBus') +except dbus.DBusException: + name = None + +if name: + print '%s is already running, creating a new instance.' % service_name +else: + activityfactoryservice.run(bundle.get_path()) + +handler = activityfactory.create(bundle.get_service_name()) +handler.connect('success', _success_cb, name != None) +handler.connect('error', _error_cb) + +gtk.main() diff --git a/shell/sugar-activity-factory b/shell/sugar-activity-factory index 009b4609..a144f343 100755 --- a/shell/sugar-activity-factory +++ b/shell/sugar-activity-factory @@ -18,6 +18,12 @@ import sys +import pygtk +pygtk.require('2.0') +import gtk + from sugar.activity import activityfactoryservice -activityfactoryservice.run(sys.argv) +activityfactoryservice.run_with_args(sys.argv) + +gtk.main() diff --git a/shell/sugar-shell b/shell/sugar-shell index 4b67e963..24eb7516 100755 --- a/shell/sugar-shell +++ b/shell/sugar-shell @@ -18,9 +18,7 @@ import sys import os - -if len(sys.argv) == 2: - sys.path.insert(0, sys.argv[1]) +from ConfigParser import ConfigParser import pygtk pygtk.require('2.0') @@ -35,8 +33,7 @@ from sugar import TracebackUtils logger.cleanup() logger.start('shell') -if len(sys.argv) == 1: - sys.path.insert(0, env.get_shell_path()) +sys.path.insert(0, env.get_shell_path()) from view.Shell import Shell from model.ShellModel import ShellModel @@ -55,9 +52,15 @@ if not key or not len(key): # # WARNING!!! this is going away at some near future point, do not rely on it # -dsba_file = os.path.join(env.get_profile_path(), "session_bus_address") +dsba_file = os.path.join(env.get_profile_path(), "session.info") f = open(dsba_file, "w") -f.write(os.environ["DBUS_SESSION_BUS_ADDRESS"]) + +cp = ConfigParser() +cp.add_section('Session') +cp.set('Session', 'dbus_address', os.environ['DBUS_SESSION_BUS_ADDRESS']) +cp.set('Session', 'display', gtk.gdk.display_get_default().get_name()) +cp.write(f) + f.close() model = ShellModel() diff --git a/sugar-emulator b/sugar-emulator index d31010d0..09ebaa8a 100755 --- a/sugar-emulator +++ b/sugar-emulator @@ -25,24 +25,12 @@ import gtk os.environ['SUGAR_EMULATOR'] = 'yes' -sourcedir = os.path.abspath(os.path.dirname(__file__)) -if os.path.isfile(os.path.join(sourcedir, 'sugar/__uninstalled__.py')): - print 'Running sugar from ' + sourcedir + ' ...' - sys.path.insert(0, sourcedir) -else: - print 'Running the installed sugar...' - sourcedir = None - from sugar import env -from sugar import util from sugar.emulator import Emulator import _sugar if len(sys.argv) == 1: - if sourcedir: - program = os.path.join(sourcedir, 'shell/sugar-shell') - else: - program = 'sugar-shell' + program = 'sugar-shell' else: program = sys.argv[1] @@ -54,18 +42,10 @@ else: height = 900 dpi = min(_sugar.get_screen_dpi(), 96) - -if sourcedir: - kbd_config = os.path.join(sourcedir, 'emulator/kbdconfig') -else: - kbd_config = os.path.join(env.get_emulator_path('kbdconfig')) +kbd_config = os.path.join(env.get_emulator_path('kbdconfig')) emulator = Emulator(width, height, dpi) emulator.set_keyboard_config(kbd_config) emulator.start() -if sourcedir: - program = os.path.join(sourcedir, program) - os.execlp('dbus-launch', 'dbus-launch', '--exit-with-session', program, sourcedir) -else: - os.execlp('dbus-launch', 'dbus-launch', '--exit-with-session', program) +os.execlp('dbus-launch', 'dbus-launch', '--exit-with-session', program) diff --git a/sugar/activity/activityfactory.py b/sugar/activity/activityfactory.py index 695d149c..cd632ca1 100644 --- a/sugar/activity/activityfactory.py +++ b/sugar/activity/activityfactory.py @@ -53,16 +53,16 @@ def _find_activity_id(): return act_id class ActivityCreationHandler(gobject.GObject): - __gsignals__ = { - 'error': (gobject.SIGNAL_RUN_FIRST, - gobject.TYPE_NONE, - ([gobject.TYPE_PYOBJECT])), + 'success': (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, ([])), + 'error': (gobject.SIGNAL_RUN_FIRST, + gobject.TYPE_NONE, + ([gobject.TYPE_PYOBJECT])) } def __init__(self, service_name, activity_handle): gobject.GObject.__init__(self) - self._service_name = service_name self._activity_handle = activity_handle @@ -83,6 +83,7 @@ class ActivityCreationHandler(gobject.GObject): def _reply_handler(self, xid): logging.debug("Activity created %s (%s)." % (self._activity_handle.activity_id, self._service_name)) + self.emit('success') def _error_handler(self, err): logging.debug("Couldn't create activity %s (%s): %s" % diff --git a/sugar/activity/activityfactoryservice.py b/sugar/activity/activityfactoryservice.py index 548ff33f..723c31e1 100644 --- a/sugar/activity/activityfactoryservice.py +++ b/sugar/activity/activityfactoryservice.py @@ -77,22 +77,25 @@ class ActivityFactoryService(dbus.service.Object): if len(self._activities) == 0: gtk.main_quit() -def run(args): +def run_with_args(args): """Start the activity factory.""" parser = OptionParser() parser.add_option("-p", "--bundle-path", dest="bundle_path", help="path to the activity bundle") (options, args) = parser.parse_args() - sys.path.insert(0, options.bundle_path) + run(options.bundle_path) - bundle = Bundle(options.bundle_path) +def run(bundle_path): + sys.path.insert(0, bundle_path) + + bundle = Bundle(bundle_path) logger.start(bundle.get_name()) - os.environ['SUGAR_BUNDLE_PATH'] = options.bundle_path + os.environ['SUGAR_BUNDLE_PATH'] = bundle_path os.environ['SUGAR_BUNDLE_SERVICE_NAME'] = bundle.get_service_name() os.environ['SUGAR_BUNDLE_DEFAULT_TYPE'] = bundle.get_default_type() - factory = ActivityFactoryService(bundle.get_service_name(), args[0]) - gtk.main() + factory = ActivityFactoryService(bundle.get_service_name(), + bundle.get_class()) diff --git a/sugar/activity/bundle.py b/sugar/activity/bundle.py index 8980a115..c4f85030 100644 --- a/sugar/activity/bundle.py +++ b/sugar/activity/bundle.py @@ -43,8 +43,8 @@ class Bundle: if cp.has_option(section, 'class'): self._class = cp.get(section, 'class') - self._exec = '%s %s --bundle-path="%s"' % ( - env.get_bin_path(_PYTHON_FACTORY), self._class, self.get_path()) + self._exec = '%s --bundle-path="%s"' % ( + env.get_bin_path(_PYTHON_FACTORY), self.get_path()) elif cp.has_option(section, 'exec'): self._class = None self._exec = cp.get(section, 'exec') @@ -105,7 +105,7 @@ class Bundle: def get_class(self): """Get the main Activity class""" - return self._exec + return self._class def get_show_launcher(self): """Get whether there should be a visible launcher for the activity""" diff --git a/sugar/activity/bundleregistry.py b/sugar/activity/bundleregistry.py index 2ad68ed5..7b124921 100644 --- a/sugar/activity/bundleregistry.py +++ b/sugar/activity/bundleregistry.py @@ -41,6 +41,18 @@ class BundleRegistry(gobject.GObject): self._search_path = [] self._service_manager = _ServiceManager() + def find_bundle(self, key): + """Find a bundle in the registry""" + key = key.lower() + + for bundle in self._bundles.values(): + name = bundle.get_name().lower() + service_name = bundle.get_service_name().lower() + if name.find(key) != -1 or service_name.find(key) != -1: + return bundle + + return None + def get_bundle(self, service_name): """Returns an bundle given his service name""" if self._bundles.has_key(service_name): diff --git a/sugar/env.py b/sugar/env.py index 65fd1fe5..4cdaf2cc 100644 --- a/sugar/env.py +++ b/sugar/env.py @@ -34,20 +34,23 @@ def is_emulator(): return True return False -def get_profile_path(): +def get_profile_path(path=None): if os.environ.has_key('SUGAR_PROFILE'): profile_id = os.environ['SUGAR_PROFILE'] else: profile_id = 'default' - path = os.path.join(os.path.expanduser('~/.sugar'), profile_id) - if not os.path.isdir(path): + base = os.path.join(os.path.expanduser('~/.sugar'), profile_id) + if not os.path.isdir(base): try: - os.makedirs(path) + os.makedirs(base) except OSError, exc: print "Could not create user directory." - return path + if path != None: + return os.path.join(base, path) + else: + return base def get_user_activities_path(): path = os.path.expanduser('~/Activities')