Several fixes for the object type registry and the activity registry.

This commit is contained in:
Tomeu Vizoso 2007-06-12 21:57:49 +02:00
parent cc604e0815
commit 22689ed1da
11 changed files with 106 additions and 76 deletions

View File

@ -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]

View File

@ -4,9 +4,14 @@ 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

View File

@ -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...'

View File

@ -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 {}

View File

@ -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...'

View File

@ -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()}

View File

@ -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

View File

@ -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'):

View File

@ -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

View File

@ -25,10 +25,18 @@ class DSMetadata(gobject.GObject):
([]))
}
def __init__(self, props={}):
def __init__(self, props=None):
gobject.GObject.__init__(self)
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(),

View File

@ -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