From fa8bcd2ba58fca8605bc2fea72478a23f9bd9998 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Wed, 08 Aug 2007 16:08:07 +0000 Subject: Move the activity register to the clipboard service. --- (limited to 'services') diff --git a/services/clipboard/Makefile.am b/services/clipboard/Makefile.am index e5a03e4..6d2cd9c 100644 --- a/services/clipboard/Makefile.am +++ b/services/clipboard/Makefile.am @@ -1,13 +1,18 @@ servicedir = $(datadir)/dbus-1/services service_in_files = \ + org.laptop.ActivityRegistry.service.in \ org.laptop.Clipboard.service.in \ org.laptop.ObjectTypeRegistry.service.in service_DATA = \ + org.laptop.ActivityRegistry.service \ org.laptop.Clipboard.service \ org.laptop.ObjectTypeRegistry.service +org.laptop.ActivityRegistry.service: org.laptop.ActivityRegistry.service.in Makefile + @sed -e "s|\@bindir\@|$(bindir)|" $< > $@ + org.laptop.Clipboard.service: org.laptop.Clipboard.service.in Makefile @sed -e "s|\@bindir\@|$(bindir)|" $< > $@ @@ -16,10 +21,11 @@ org.laptop.ObjectTypeRegistry.service: org.laptop.ObjectTypeRegistry.service.in sugardir = $(pkgdatadir)/services/clipboard -sugar_PYTHON = \ - __init__.py \ - clipboardobject.py \ - clipboardservice.py \ +sugar_PYTHON = \ + __init__.py \ + activityregistryservice.py \ + clipboardobject.py \ + clipboardservice.py \ objecttypeservice.py bin_SCRIPTS = sugar-clipboard diff --git a/services/clipboard/activityregistryservice.py b/services/clipboard/activityregistryservice.py new file mode 100644 index 0000000..60293d4 --- /dev/null +++ b/services/clipboard/activityregistryservice.py @@ -0,0 +1,104 @@ +# Copyright (C) 2006-2007 Red Hat, Inc. +# Copyright (C) 2007 One Laptop Per Child +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import dbus +import dbus.service + +import bundleregistry + +_ACTIVITY_REGISTRY_SERVICE_NAME = 'org.laptop.ActivityRegistry' +_ACTIVITY_REGISTRY_IFACE = 'org.laptop.ActivityRegistry' +_ACTIVITY_REGISTRY_PATH = '/org/laptop/ActivityRegistry' + +class ActivityRegistry(dbus.service.Object): + def __init__(self): + bus = dbus.SessionBus() + bus_name = dbus.service.BusName(_ACTIVITY_REGISTRY_SERVICE_NAME, bus=bus) + dbus.service.Object.__init__(self, bus_name, _ACTIVITY_REGISTRY_PATH) + + bundle_registry = bundleregistry.get_registry() + bundle_registry.connect('bundle-added', self._bundle_added_cb) + + @dbus.service.method(_ACTIVITY_REGISTRY_IFACE, + in_signature='s', out_signature='b') + def AddBundle(self, bundle_path): + '''Register the activity bundle with the global registry + + bundle_path -- path to the activity bundle's root directory, + that is, the directory with activity/activity.info as a + child of the directory. + + The bundleregistry.BundleRegistry is responsible for setting + up a set of d-bus service mappings for each available activity. + ''' + registry = bundleregistry.get_registry() + return registry.add_bundle(bundle_path) + + @dbus.service.method(_ACTIVITY_REGISTRY_IFACE, + in_signature='s', out_signature='a{sv}') + def GetActivity(self, service_name): + registry = bundleregistry.get_registry() + bundle = registry.get_bundle(service_name) + if not bundle: + return {} + + return self._bundle_to_dict(bundle) + + @dbus.service.method(_ACTIVITY_REGISTRY_IFACE, + in_signature='s', out_signature='aa{sv}') + def FindActivity(self, name): + result = [] + key = name.lower() + + for bundle in bundleregistry.get_registry(): + name = bundle.get_name().lower() + service_name = bundle.get_service_name().lower() + if name.find(key) != -1 or service_name.find(key) != -1: + result.append(self._bundle_to_dict(bundle)) + + return result + + @dbus.service.method(_ACTIVITY_REGISTRY_IFACE, + in_signature='s', out_signature='aa{sv}') + def GetActivitiesForType(self, mime_type): + result = [] + registry = bundleregistry.get_registry() + for bundle in registry.get_activities_for_type(mime_type): + result.append(self._bundle_to_dict(bundle)) + return result + + @dbus.service.signal(_ACTIVITY_REGISTRY_IFACE, signature='a{sv}') + def ActivityAdded(self, activity_info): + pass + + def _bundle_to_dict(self, bundle): + return {'name': bundle.get_name(), + 'icon': bundle.get_icon(), + 'service_name': bundle.get_service_name(), + 'path': bundle.get_path()} + + def _bundle_added_cb(self, bundle_registry, bundle): + self.ActivityAdded(self._bundle_to_dict(bundle)) + +_instance = None + +def get_instance(): + global _instance + if not _instance: + _instance = ActivityRegistry() + return _instance + diff --git a/services/clipboard/bundleregistry.py b/services/clipboard/bundleregistry.py new file mode 100644 index 0000000..65a2348 --- /dev/null +++ b/services/clipboard/bundleregistry.py @@ -0,0 +1,125 @@ +# Copyright (C) 2006-2007 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +import os + +import gobject + +from sugar.activity.bundle import Bundle +from sugar import env +from sugar import util + +# http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html +def _get_data_dirs(): + if os.environ.has_key('XDG_DATA_DIRS'): + return os.environ['XDG_DATA_DIRS'].split(':') + else: + return [ '/usr/local/share/', '/usr/share/' ] + +class _ServiceManager(object): + """Internal class responsible for creating dbus service files + + DBUS services are defined in files which bind a service name + to the name of an executable which provides the service name. + + In Sugar, the service files are automatically generated from + the activity registry (by this class). When an activity's + dbus launch service is requested, dbus will launch the + specified executable in order to allow it to provide the + requested activity-launching service. + + In the case of activities which provide a "class", instead of + an "exec" attribute in their activity.info, the + sugar-activity-factory script is used with an appropriate + argument to service that bundle. + """ + SERVICE_DIRECTORY = '~/.local/share/dbus-1/services' + def __init__(self): + service_dir = os.path.expanduser(self.SERVICE_DIRECTORY) + if not os.path.isdir(service_dir): + os.makedirs(service_dir) + + self._path = service_dir + + def add(self, bundle): + util.write_service(bundle.get_service_name(), + bundle.get_exec(), self._path) + +class BundleRegistry(gobject.GObject): + """Service that tracks the available activity bundles""" + + __gsignals__ = { + 'bundle-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, + ([gobject.TYPE_PYOBJECT])) + } + + def __init__(self): + gobject.GObject.__init__(self) + + self._bundles = {} + self._search_path = [] + self._service_manager = _ServiceManager() + + def get_bundle(self, service_name): + """Returns an bundle given his service name""" + if self._bundles.has_key(service_name): + return self._bundles[service_name] + else: + return None + + def add_search_path(self, path): + """Add a directory to the bundles search path""" + self._search_path.append(path) + self._scan_directory(path) + + def __iter__(self): + return self._bundles.values().__iter__() + + def _scan_directory(self, path): + if os.path.isdir(path): + for f in os.listdir(path): + bundle_dir = os.path.join(path, f) + if os.path.isdir(bundle_dir) and \ + bundle_dir.endswith('.activity'): + self.add_bundle(bundle_dir) + + def add_bundle(self, bundle_path): + bundle = Bundle(bundle_path) + if bundle.is_valid(): + self._bundles[bundle.get_service_name()] = bundle + self._service_manager.add(bundle) + self.emit('bundle-added', bundle) + return True + else: + return False + + def get_activities_for_type(self, mime_type): + result = [] + for bundle in self._bundles.values(): + if bundle.get_mime_types() and mime_type in bundle.get_mime_types(): + result.append(bundle) + return result + +def get_registry(): + return _bundle_registry + +_bundle_registry = BundleRegistry() + +for path in _get_data_dirs(): + bundles_path = os.path.join(path, 'activities') + _bundle_registry.add_search_path(bundles_path) + +_bundle_registry.add_search_path(env.get_user_activities_path()) diff --git a/services/clipboard/clipboardobject.py b/services/clipboard/clipboardobject.py index d751274..bc51f47 100644 --- a/services/clipboard/clipboardobject.py +++ b/services/clipboard/clipboardobject.py @@ -19,9 +19,9 @@ import logging import urlparse from sugar.objects import mime -from sugar import activity import objecttypeservice +import bundleregistry class ClipboardObject: @@ -66,30 +66,15 @@ class ClipboardObject: return '' def get_activity(self): - logging.debug('get_activity') - mapping = {'text/html' : 'org.laptop.WebActivity', - 'image/jpeg' : 'org.laptop.WebActivity', - 'image/gif' : 'org.laptop.WebActivity', - 'image/png' : 'org.laptop.WebActivity', - 'text/plain' : 'org.laptop.AbiWordActivity', - 'text/rtf' : 'org.laptop.AbiWordActivity', - 'text/richtext' : 'org.laptop.AbiWordActivity', - 'application/pdf' : 'org.laptop.sugar.ReadActivity', - 'application/x-squeak-project' : 'org.vpri.EtoysActivity'} mime = self.get_mime_type() if not mime: return '' - """ - registry = activity.get_registry() + + registry = bundleregistry.get_registry() activities = registry.get_activities_for_type(self.get_mime_type()) # TODO: should we return several activities? if activities: - return activities[0] - else: - return '' - """ - if mapping.has_key(mime): - return mapping[mime] + return activities[0].get_service_name() else: return '' @@ -101,8 +86,6 @@ class ClipboardObject: def add_format(self, format): self._formats[format.get_type()] = format - # We want to get the activity early in order to prevent a DBus lockup. - activity = self.get_activity() def get_formats(self): return self._formats diff --git a/services/clipboard/clipboardservice.py b/services/clipboard/clipboardservice.py index 639f29c..19958a7 100644 --- a/services/clipboard/clipboardservice.py +++ b/services/clipboard/clipboardservice.py @@ -74,7 +74,7 @@ class ClipboardService(dbus.service.Object): def add_object_format(self, object_path, format_type, data, on_disk): logging.debug('ClipboardService.add_object_format') cb_object = self._objects[str(object_path)] - + if on_disk and cb_object.get_percent() == 100: new_uri = self._copy_file(data) cb_object.add_format(Format(format_type, new_uri, on_disk)) diff --git a/services/clipboard/org.laptop.ActivityRegistry.service.in b/services/clipboard/org.laptop.ActivityRegistry.service.in new file mode 100644 index 0000000..051ab00 --- /dev/null +++ b/services/clipboard/org.laptop.ActivityRegistry.service.in @@ -0,0 +1,4 @@ +[D-BUS Service] +Name = org.laptop.ActivityRegistry +Exec = @bindir@/sugar-clipboard + diff --git a/services/clipboard/sugar-clipboard b/services/clipboard/sugar-clipboard index 4cffa33..9531e94 100755 --- a/services/clipboard/sugar-clipboard +++ b/services/clipboard/sugar-clipboard @@ -34,6 +34,7 @@ sys.path.append(env.get_service_path('clipboard')) import clipboardservice import objecttypeservice +import activityregistryservice logging.info('Starting clipboard service.') @@ -42,9 +43,11 @@ dbus.glib.threads_init() clipboard_service = clipboardservice.get_instance() object_type_registry = objecttypeservice.get_instance() +activity_registry = activityregistryservice.get_instance() loop = gobject.MainLoop() try: loop.run() except KeyboardInterrupt: print 'Ctrl+C pressed, exiting...' + -- cgit v0.9.1