Smarter sugar-activity which can make itself the factory and open

one instance.
This commit is contained in:
Marco Pesenti Gritti 2007-03-09 16:35:53 +01:00
parent 1587218e9e
commit d3493aea9e
5 changed files with 62 additions and 33 deletions

View File

@ -18,30 +18,53 @@
import sys import sys
import os import os
import gobject
import gtk
import dbus
import dbus.glib
from sugar.activity import bundleregistry
from sugar.activity import activityfactory from sugar.activity import activityfactory
from sugar.activity import activityfactoryservice
from sugar import env from sugar import env
from sugar import util
def _success_cb(handler, activity, loop): def _setup_bus_address():
activity.start(util.unique_id()) '''Use the bus address of the running Sugar'''
loop.quit() bus_file = os.path.join(env.get_profile_path(), "session_bus_address")
f = open(bus_file, "r")
bus_name = f.read()
f.close()
os.environ['DBUS_SESSION_BUS_ADDRESS'] = bus_name
def _error_cb(handler, err, loop): def _success_cb(handler, exit):
loop.quit() if exit:
gtk.main_quit()
ppath = env.get_profile_path() def _error_cb(handler, err):
bus_file = os.path.join(ppath, "session_bus_address") print err
f = open(bus_file, "r") gtk.main_quit()
bus_name = f.read()
f.close()
os.environ['DBUS_SESSION_BUS_ADDRESS'] = bus_name
loop = gobject.MainLoop() _setup_bus_address()
handler = activityfactory.create(sys.argv[1]) service_name = sys.argv[1]
handler.connect('success', _success_cb, loop) registry = bundleregistry.get_registry()
handler.connect('error', _error_cb, loop) bundle = registry.get_bundle(service_name)
loop.run() bus = dbus.SessionBus()
bus_object = bus.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
try:
name = bus_object.GetNameOwner(
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(service_name)
handler.connect('success', _success_cb, name != None)
handler.connect('error', _error_cb)
gtk.main()

View File

@ -20,4 +20,6 @@ import sys
from sugar.activity import activityfactoryservice from sugar.activity import activityfactoryservice
activityfactoryservice.run(sys.argv) activityfactoryservice.run_with_args(sys.argv)
gtk.main()

View File

@ -53,16 +53,16 @@ def _find_activity_id():
return act_id return act_id
class ActivityCreationHandler(gobject.GObject): class ActivityCreationHandler(gobject.GObject):
__gsignals__ = { __gsignals__ = {
'error': (gobject.SIGNAL_RUN_FIRST, 'success': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE, gobject.TYPE_NONE, ([])),
([gobject.TYPE_PYOBJECT])), 'error': (gobject.SIGNAL_RUN_FIRST,
gobject.TYPE_NONE,
([gobject.TYPE_PYOBJECT]))
} }
def __init__(self, service_name, activity_handle): def __init__(self, service_name, activity_handle):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
self._service_name = service_name self._service_name = service_name
self._activity_handle = activity_handle self._activity_handle = activity_handle
@ -83,6 +83,7 @@ class ActivityCreationHandler(gobject.GObject):
def _reply_handler(self, xid): def _reply_handler(self, xid):
logging.debug("Activity created %s (%s)." % logging.debug("Activity created %s (%s)." %
(self._activity_handle.activity_id, self._service_name)) (self._activity_handle.activity_id, self._service_name))
self.emit('success')
def _error_handler(self, err): def _error_handler(self, err):
logging.debug("Couldn't create activity %s (%s): %s" % logging.debug("Couldn't create activity %s (%s): %s" %

View File

@ -77,22 +77,25 @@ class ActivityFactoryService(dbus.service.Object):
if len(self._activities) == 0: if len(self._activities) == 0:
gtk.main_quit() gtk.main_quit()
def run(args): def run_with_args(args):
"""Start the activity factory.""" """Start the activity factory."""
parser = OptionParser() parser = OptionParser()
parser.add_option("-p", "--bundle-path", dest="bundle_path", parser.add_option("-p", "--bundle-path", dest="bundle_path",
help="path to the activity bundle") help="path to the activity bundle")
(options, args) = parser.parse_args() (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()) 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_SERVICE_NAME'] = bundle.get_service_name()
os.environ['SUGAR_BUNDLE_DEFAULT_TYPE'] = bundle.get_default_type() os.environ['SUGAR_BUNDLE_DEFAULT_TYPE'] = bundle.get_default_type()
factory = ActivityFactoryService(bundle.get_service_name(), args[0]) factory = ActivityFactoryService(bundle.get_service_name(),
gtk.main() bundle.get_class())

View File

@ -43,8 +43,8 @@ class Bundle:
if cp.has_option(section, 'class'): if cp.has_option(section, 'class'):
self._class = cp.get(section, 'class') self._class = cp.get(section, 'class')
self._exec = '%s %s --bundle-path="%s"' % ( self._exec = '%s --bundle-path="%s"' % (
env.get_bin_path(_PYTHON_FACTORY), self._class, self.get_path()) env.get_bin_path(_PYTHON_FACTORY), self.get_path())
elif cp.has_option(section, 'exec'): elif cp.has_option(section, 'exec'):
self._class = None self._class = None
self._exec = cp.get(section, 'exec') self._exec = cp.get(section, 'exec')
@ -105,7 +105,7 @@ class Bundle:
def get_class(self): def get_class(self):
"""Get the main Activity class""" """Get the main Activity class"""
return self._exec return self._class
def get_show_launcher(self): def get_show_launcher(self):
"""Get whether there should be a visible launcher for the activity""" """Get whether there should be a visible launcher for the activity"""