From 22689ed1dac2c99b10c7de5d8a51789f3abd4f35 Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Tue, 12 Jun 2007 21:57:49 +0200 Subject: [PATCH] Several fixes for the object type registry and the activity registry. --- bin/sugar-activity | 2 +- services/clipboard/Makefile.am | 15 +++++++---- services/clipboard/clipboardservice.py | 16 ++--------- services/clipboard/objecttypeservice.py | 35 ++++++++++++++----------- services/clipboard/sugar-clipboard | 11 ++++++-- shell/shellservice.py | 31 +++++++++++++--------- sugar/activity/__init__.py | 2 -- sugar/activity/bundle.py | 2 +- sugar/activity/registry.py | 20 +++++++------- sugar/datastore/datastore.py | 14 +++++++--- sugar/objects/objecttype.py | 34 ++++++++++++++++-------- 11 files changed, 106 insertions(+), 76 deletions(-) diff --git a/bin/sugar-activity b/bin/sugar-activity index 31c48d07..9a970904 100755 --- a/bin/sugar-activity +++ b/bin/sugar-activity @@ -52,7 +52,7 @@ activity_info = None if len(sys.argv) > 1: registry = ActivityRegistry() - activities = registry.get_activities_for_name(sys.argv[1]) + activities = registry.find_activity(sys.argv[1]) if len(activities) > 0: activity_info = activities[0] diff --git a/services/clipboard/Makefile.am b/services/clipboard/Makefile.am index 7a957c24..d87276d8 100644 --- a/services/clipboard/Makefile.am +++ b/services/clipboard/Makefile.am @@ -4,18 +4,23 @@ service_in_files = \ org.laptop.Clipboard.service.in \ org.laptop.ObjectTypeRegistry.service.in -service_DATA = $(service_in_files:.service.in=.service) +service_DATA = \ + org.laptop.Clipboard.service \ + org.laptop.ObjectTypeRegistry.service -$(service_DATA): $(service_in_files) Makefile +org.laptop.Clipboard.service: org.laptop.Clipboard.service.in Makefile + @sed -e "s|\@bindir\@|$(bindir)|" $< > $@ + +org.laptop.ObjectTypeRegistry.service: org.laptop.ObjectTypeRegistry.service.in Makefile @sed -e "s|\@bindir\@|$(bindir)|" $< > $@ sugardir = $(pkgdatadir)/services/clipboard -sugar_PYTHON = \ - __init__.py \ +sugar_PYTHON = \ + __init__.py \ clipboardobject.py \ clipboardservice.py \ - objecttypeservice.py \ + objecttypeservice.py \ typeregistry.py bin_SCRIPTS = sugar-clipboard diff --git a/services/clipboard/clipboardservice.py b/services/clipboard/clipboardservice.py index 16e37b19..8b2d8364 100644 --- a/services/clipboard/clipboardservice.py +++ b/services/clipboard/clipboardservice.py @@ -15,7 +15,6 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import logging -import gobject import os import shutil import dbus @@ -32,14 +31,13 @@ PREVIEW_KEY = 'PREVIEW' ACTIVITY_KEY = 'ACTIVITY' FORMATS_KEY = 'FORMATS' -class ClipboardDBusServiceHelper(dbus.service.Object): +class ClipboardService(dbus.service.Object): _CLIPBOARD_DBUS_INTERFACE = "org.laptop.Clipboard" _CLIPBOARD_OBJECT_PATH = "/org/laptop/Clipboard" _CLIPBOARD_OBJECTS_PATH = _CLIPBOARD_OBJECT_PATH + "/Objects/" - def __init__(self, parent): - self._parent = parent + def __init__(self): self._objects = {} self._next_id = 0 @@ -175,13 +173,3 @@ class ClipboardDBusServiceHelper(dbus.service.Object): def object_state_changed(self, object_path, values): pass -class ClipboardService(object): - def __init__(self): - self._dbus_helper = ClipboardDBusServiceHelper(self) - - def run(self): - loop = gobject.MainLoop() - try: - loop.run() - except KeyboardInterrupt: - print 'Ctrl+C pressed, exiting...' diff --git a/services/clipboard/objecttypeservice.py b/services/clipboard/objecttypeservice.py index bf9083f6..d1bcd495 100644 --- a/services/clipboard/objecttypeservice.py +++ b/services/clipboard/objecttypeservice.py @@ -17,46 +17,49 @@ import dbus import dbus.service -from sugar.objects.objecttype import ObjectType - _REGISTRY_IFACE = "org.laptop.ObjectTypeRegistry" _REGISTRY_PATH = "/org/laptop/ObjectTypeRegistry" class ObjectTypeRegistry(dbus.service.Object): def __init__(self): bus = dbus.SessionBus() - bus_name = dbus.service.BusName(self._REGISTRY_IFACE, bus=bus) - dbus.service.Object.__init__(self, bus_name, self._REGISTRY_PATH) + bus_name = dbus.service.BusName(_REGISTRY_IFACE, bus=bus) + dbus.service.Object.__init__(self, bus_name, _REGISTRY_PATH) self._types = {} - self._add_primitive('Text', _('Text'), 'object-text', - [ 'text/rtf' ]) - self._add_primitive('Image', _('Image'), 'object-image', + from gettext import gettext as _ + self._add_primitive('Text', _('Text'), 'theme:object-text', + [ 'text/plain', 'text/rtf', 'application/pdf', + 'application/x-pdf' ]) + self._add_primitive('Image', _('Image'), 'theme:object-image', [ 'image/png' ]) def _add_primitive(self, type_id, name, icon, mime_types): - object_type = ObjectType(type_id, name, icon, mime_types) - self._types.add(object_type) + object_type = {'type_id': type_id, + 'name': name, + 'icon': icon, + 'mime_types': mime_types} + self._types[type_id] = object_type def _get_type_for_mime(self, mime_type): for object_type in self._types.values(): - if mime_type in object_type.mime_types: + if mime_type in object_type['mime_types']: return object_type - @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE, + @dbus.service.method(_REGISTRY_IFACE, in_signature="s", out_signature="a{sv}") def GetType(self, type_id): if self._types.has_key(type_id): - return self._types[type_id].to_dict() + return self._types[type_id] else: - return [] + return {} - @dbus.service.method(_CLIPBOARD_DBUS_INTERFACE, + @dbus.service.method(_REGISTRY_IFACE, in_signature="s", out_signature="a{sv}") def GetTypeForMIME(self, mime_type): object_type = self._get_type_for_mime(mime_type) if object_type: - return object_type.to_dict() + return object_type else: - return [] + return {} diff --git a/services/clipboard/sugar-clipboard b/services/clipboard/sugar-clipboard index f9dcfb3d..ddac295f 100755 --- a/services/clipboard/sugar-clipboard +++ b/services/clipboard/sugar-clipboard @@ -35,11 +35,18 @@ from sugar import env sys.path.append(env.get_service_path('clipboard')) from clipboardservice import ClipboardService +from objecttypeservice import ObjectTypeRegistry logging.info('Starting clipboard service.') gobject.threads_init() dbus.glib.threads_init() -app = ClipboardService() -app.run() +clipboard_service = ClipboardService() +object_type_registry = ObjectTypeRegistry() + +loop = gobject.MainLoop() +try: + loop.run() +except KeyboardInterrupt: + print 'Ctrl+C pressed, exiting...' diff --git a/shell/shellservice.py b/shell/shellservice.py index e29d9b03..c9323a09 100644 --- a/shell/shellservice.py +++ b/shell/shellservice.py @@ -17,9 +17,6 @@ """D-bus service providing access to the shell's functionality""" import dbus -from sugar.activity import ActivityRegistry -from sugar.activity import ActivityInfo - from model import bundleregistry _DBUS_SERVICE = "org.laptop.Shell" @@ -76,9 +73,19 @@ class ShellService(dbus.service.Object): registry = bundleregistry.get_registry() return registry.add_bundle(bundle_path) + @dbus.service.method(_DBUS_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(_DBUS_ACTIVITY_REGISTRY_IFACE, in_signature="s", out_signature="aa{sv}") - def GetActivitiesForName(self, name): + def FindActivity(self, name): result = [] key = name.lower() @@ -86,8 +93,7 @@ class ShellService(dbus.service.Object): name = bundle.get_name().lower() service_name = bundle.get_service_name().lower() if name.find(key) != -1 or service_name.find(key) != -1: - info = self._bundle_to_activity_info(bundle) - result.append(info.to_dict()) + result.append(self._bundle_to_dict(bundle)) return result @@ -97,9 +103,8 @@ class ShellService(dbus.service.Object): result = [] for bundle in bundleregistry.get_registry(): - if mime_type in bundle.get_mime_types(): - info = self._bundle_to_activity_info(bundle) - result.append(info.to_dict()) + if bundle.get_mime_types() and mime_type in bundle.get_mime_types(): + result.append(self._bundle_to_dict(bundle)) return result @@ -135,6 +140,8 @@ class ShellService(dbus.service.Object): if new_id: self.CurrentActivityChanged(new_id) - def _bundle_to_activity_info(self, bundle): - return ActivityInfo(bundle.get_name(), bundle.get_icon(), - bundle.get_service_name(), bundle.get_path()) + 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()} diff --git a/sugar/activity/__init__.py b/sugar/activity/__init__.py index 84539a3b..89bd1c3f 100644 --- a/sugar/activity/__init__.py +++ b/sugar/activity/__init__.py @@ -37,5 +37,3 @@ window, requesting sharing across the network, and basic "what type of application are you" queries. """ -from sugar.activity.registry import ActivityRegistry -from sugar.activity.registry import ActivityInfo diff --git a/sugar/activity/bundle.py b/sugar/activity/bundle.py index df67601c..cee64faf 100644 --- a/sugar/activity/bundle.py +++ b/sugar/activity/bundle.py @@ -93,7 +93,7 @@ class Bundle: logging.error('%s must specify exec or class' % self._path) if cp.has_option(section, 'mime_types'): - mime_list = cp.get(section, 'show_launcher') + mime_list = cp.get(section, 'mime_types') self._mime_types = mime_list.strip(';') if cp.has_option(section, 'show_launcher'): diff --git a/sugar/activity/registry.py b/sugar/activity/registry.py index 9eed4aa4..7e307b80 100644 --- a/sugar/activity/registry.py +++ b/sugar/activity/registry.py @@ -32,13 +32,6 @@ class ActivityInfo(object): self.service_name = service_name self.path = path - def to_dict(self): - return { 'name' : self.name, - 'icon' : self.icon, - 'service_name' : self.service_name, - 'path' : self.path - } - class ActivityRegistry(object): def __init__(self): bus = dbus.SessionBus() @@ -53,10 +46,19 @@ class ActivityRegistry(object): return result - def get_activities_for_name(self, name): - info_list = self._registry.GetActivitiesForName(name) + def get_activity(self, service_name): + info_dict = self._registry.GetActivity(service_name) + return _activity_info_from_dict(info_dict) + + def find_activity(self, name): + info_list = self._registry.FindActivity(name) return self._convert_info_list(info_list) def get_activities_for_type(self, mime_type): info_list = self._registry.GetActivitiesForType(mime_type) return self._convert_info_list(info_list) + +_registry = ActivityRegistry() + +def get_registry(): + return _registry diff --git a/sugar/datastore/datastore.py b/sugar/datastore/datastore.py index 52d735f9..4cac40be 100644 --- a/sugar/datastore/datastore.py +++ b/sugar/datastore/datastore.py @@ -25,9 +25,17 @@ class DSMetadata(gobject.GObject): ([])) } - def __init__(self, props={}): + def __init__(self, props=None): gobject.GObject.__init__(self) - self._props = props + if not props: + self._props = {} + else: + self._props = props + + default_keys = ['activity', 'mime_type'] + for key in default_keys: + if not self._props.has_key(key): + self._props[key] = '' def __getitem__(self, key): return self._props[key] @@ -88,7 +96,7 @@ def create(): return DSObject(object_id=None, metadata=DSMetadata(), file_path=None) def write(ds_object, reply_handler=None, error_handler=None): - logging.debug('datastore.write') + logging.debug('datastore.write: %r' % ds_object.metadata.get_dictionary()) if ds_object.object_id: dbus_helpers.update(ds_object.object_id, ds_object.metadata.get_dictionary(), diff --git a/sugar/objects/objecttype.py b/sugar/objects/objecttype.py index 81b8ec12..b515bd13 100644 --- a/sugar/objects/objecttype.py +++ b/sugar/objects/objecttype.py @@ -1,3 +1,21 @@ +# Copyright (C) 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 dbus + _SERVICE = "org.laptop.ObjectTypeRegistry" _PATH = "/org/laptop/ObjectTypeRegistry" _IFACE = "org.laptop.ObjectTypeRegistry" @@ -11,33 +29,27 @@ def _object_type_from_dict(info_dict): return None class ObjectType(object): - def __init__(self, type_id, name, icon, mime_types): + def __init__(self, type_id, name, icon): self.type_id = type_id self.name = name self.icon = icon self.mime_types = [] - def to_dict(self): - return { 'type_id' : self.type_id, - 'name' : self.name, - 'icon' : self.icon - } - class ObjectTypeRegistry(object): def __init__(self): bus = dbus.SessionBus() bus_object = bus.get_object(_SERVICE, _PATH) self._registry = dbus.Interface(bus_object, _IFACE) - def get_type(type_id): + def get_type(self, type_id): type_dict = self._registry.GetType(type_id) return _object_type_from_dict(type_dict) - def get_type_for_mime(mime_type): - type_dict = self._registry.GetTypeForMime(type_id) + def get_type_for_mime(self, mime_type): + type_dict = self._registry.GetTypeForMIME(mime_type) return _object_type_from_dict(type_dict) -_registry = ObjectRegistry() +_registry = ObjectTypeRegistry() def get_registry(): return _registry