Rename the clipboard service as shell service.

This commit is contained in:
Tomeu Vizoso
2007-08-08 18:33:10 +02:00
parent fa8bcd2ba5
commit cac6038665
13 changed files with 12 additions and 10 deletions
+37
View File
@@ -0,0 +1,37 @@
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)|" $< > $@
org.laptop.ObjectTypeRegistry.service: org.laptop.ObjectTypeRegistry.service.in Makefile
@sed -e "s|\@bindir\@|$(bindir)|" $< > $@
sugardir = $(pkgdatadir)/services/shell
sugar_PYTHON = \
__init__.py \
activityregistryservice.py \
bundleregistry.py \
clipboardobject.py \
clipboardservice.py \
objecttypeservice.py
bin_SCRIPTS = sugar-shell-service
DISTCLEANFILES = $(service_DATA)
EXTRA_DIST = $(service_in_files) $(bin_SCRIPTS)
+16
View File
@@ -0,0 +1,16 @@
# 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
+104
View File
@@ -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
+125
View File
@@ -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())
+133
View File
@@ -0,0 +1,133 @@
# 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 os
import logging
import urlparse
from sugar.objects import mime
import objecttypeservice
import bundleregistry
class ClipboardObject:
def __init__(self, object_path, name):
self._id = object_path
self._name = name
self._percent = 0
self._formats = {}
def destroy(self):
for type, format in self._formats.iteritems():
format.destroy()
def get_id(self):
return self._id
def _get_type_info(self):
logging.debug('_get_type_info')
type_registry = objecttypeservice.get_instance()
return type_registry.GetTypeForMIME(self.get_mime_type())
def get_name(self):
if self._name:
return self._name
else:
type_info = self._get_type_info()
if type_info:
return type_info['name']
else:
return ''
def get_icon(self):
type_info = self._get_type_info()
if type_info:
return type_info['icon']
else:
return ''
def get_preview(self):
# TODO: should previews really be here?
#return self._get_type_info().get_preview()
return ''
def get_activity(self):
mime = self.get_mime_type()
if not mime:
return ''
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].get_service_name()
else:
return ''
def get_percent(self):
return self._percent
def set_percent(self, percent):
self._percent = percent
def add_format(self, format):
self._formats[format.get_type()] = format
def get_formats(self):
return self._formats
def get_mime_type(self):
if not self._formats:
return ''
format = mime.choose_most_significant(self._formats.keys())
if format == 'text/uri-list':
data = self._formats['text/uri-list'].get_data()
uris = data.split('\n')
if len(uris) == 1 or not uris[1]:
uri = urlparse.urlparse(uris[0], 'file')
if uri.scheme == 'file':
logging.debug('Choosed %r!' % mime.get_for_file(uri.path))
format = mime.get_for_file(uri.path)
return format
class Format:
def __init__(self, type, data, on_disk):
self._type = type
self._data = data
self._on_disk = on_disk
def destroy(self):
if self._on_disk:
uri = urlparse.urlparse(self._data)
if os.path.isfile(uri.path):
os.remove(uri.path)
def get_type(self):
return self._type
def get_data(self):
return self._data
def set_data(self, data):
self._data = data
def is_on_disk(self):
return self._on_disk
+194
View File
@@ -0,0 +1,194 @@
# Copyright (C) 2006, 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 logging
import os
import shutil
import urlparse
import tempfile
import dbus
import dbus.service
from sugar import env
from sugar import util
from sugar.objects import mime
from clipboardobject import ClipboardObject, Format
NAME_KEY = 'NAME'
PERCENT_KEY = 'PERCENT'
ICON_KEY = 'ICON'
PREVIEW_KEY = 'PREVIEW'
ACTIVITY_KEY = 'ACTIVITY'
FORMATS_KEY = 'FORMATS'
TYPE_KEY = 'TYPE'
DATA_KEY = 'DATA'
ON_DISK_KEY = 'ON_DISK'
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):
self._objects = {}
self._next_id = 0
bus = dbus.SessionBus()
bus_name = dbus.service.BusName(self._CLIPBOARD_DBUS_INTERFACE, bus=bus)
dbus.service.Object.__init__(self, bus_name, self._CLIPBOARD_OBJECT_PATH)
def _get_next_object_id(self):
self._next_id += 1
return self._next_id
# dbus methods
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="s", out_signature="o")
def add_object(self, name):
logging.debug('ClipboardService.add_object')
op = self._CLIPBOARD_OBJECTS_PATH + "%d" % self._get_next_object_id()
self._objects[op] = ClipboardObject(op, name)
self.object_added(dbus.ObjectPath(op), name)
logging.debug('Added object ' + op + ' with name ' + name)
return dbus.ObjectPath(op)
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="ssayb", out_signature="", byte_arrays=True)
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))
logging.debug('Added format of type ' + format_type + ' with path at ' + new_uri)
else:
cb_object.add_format(Format(format_type, data, on_disk))
logging.debug('Added in-memory format of type ' + format_type + '.')
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
PERCENT_KEY: cb_object.get_percent(),
ICON_KEY: cb_object.get_icon(),
PREVIEW_KEY: cb_object.get_preview(),
ACTIVITY_KEY: cb_object.get_activity()})
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="o", out_signature="")
def delete_object(self, object_path):
cb_object = self._objects.pop(str(object_path))
cb_object.destroy()
self.object_deleted(object_path)
logging.debug('Deleted object with object_id ' + object_path)
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="oi", out_signature="")
def set_object_percent(self, object_path, percent):
cb_object = self._objects[str(object_path)]
if percent < 0 or percent > 100:
raise ValueError("invalid percentage")
if cb_object.get_percent() > percent:
raise ValueError("invalid percentage; less than current percent")
if cb_object.get_percent() == percent:
# ignore setting same percentage
return
cb_object.set_percent(percent)
if percent == 100:
for format_name, format in cb_object.get_formats().iteritems():
if format.is_on_disk():
new_uri = self._copy_file(format.get_data())
format.set_data(new_uri)
self.object_state_changed(object_path, {NAME_KEY: cb_object.get_name(),
PERCENT_KEY: percent,
ICON_KEY: cb_object.get_icon(),
PREVIEW_KEY: cb_object.get_preview(),
ACTIVITY_KEY: cb_object.get_activity()})
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="o", out_signature="a{sv}")
def get_object(self, object_path):
logging.debug('ClipboardService.get_object')
if not self._objects.has_key(str(object_path)):
return dbus.Dictionary({}, signature='sv')
cb_object = self._objects[str(object_path)]
formats = cb_object.get_formats()
format_types = dbus.Array([], signature='s')
for type, format in formats.iteritems():
format_types.append(type)
result_dict = {NAME_KEY: cb_object.get_name(),
PERCENT_KEY: cb_object.get_percent(),
ICON_KEY: cb_object.get_icon(),
PREVIEW_KEY: cb_object.get_preview(),
ACTIVITY_KEY: cb_object.get_activity(),
FORMATS_KEY: format_types}
return dbus.Dictionary(result_dict)
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="os", out_signature="a{sv}")
def get_object_data(self, object_path, format_type):
logging.debug('ClipboardService.get_object_data')
cb_object = self._objects[str(object_path)]
format = cb_object.get_formats()[format_type]
result_dict = {TYPE_KEY: format.get_type(),
DATA_KEY: dbus.ByteArray(format.get_data()),
ON_DISK_KEY: format.is_on_disk()}
return dbus.Dictionary(result_dict)
# dbus signals
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="os")
def object_added(self, object_path, name):
pass
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="o")
def object_deleted(self, object_path):
pass
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="oa{sv}")
def object_state_changed(self, object_path, values):
pass
def _copy_file(self, original_uri):
uri = urlparse.urlparse(original_uri)
path, file_name = os.path.split(uri.path)
root, ext = os.path.splitext(file_name)
if not ext or ext == '.':
mime_type = mime.get_for_file(uri.path)
ext = '.' + mime.get_primary_extension(mime_type)
f, new_file_path = tempfile.mkstemp(ext, root)
del f
shutil.copyfile(uri.path, new_file_path)
return 'file://' + new_file_path
_instance = None
def get_instance():
global _instance
if not _instance:
_instance = ClipboardService()
return _instance
+74
View File
@@ -0,0 +1,74 @@
# 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
import dbus.service
_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(_REGISTRY_IFACE, bus=bus)
dbus.service.Object.__init__(self, bus_name, _REGISTRY_PATH)
self._types = {}
from gettext import gettext as _
self._add_primitive('Text', _('Text'), 'theme:object-text',
[ 'text/plain', 'text/rtf', 'application/pdf',
'application/x-pdf', 'text/html',
'application/vnd.oasis.opendocument.text' ])
self._add_primitive('Image', _('Image'), 'theme:object-image',
[ 'image/png', 'image/gif', 'image/jpeg' ])
def _add_primitive(self, type_id, name, icon, mime_types):
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']:
return object_type
@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]
else:
return {}
@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
else:
return {}
_instance = None
def get_instance():
global _instance
if not _instance:
_instance = ObjectTypeRegistry()
return _instance
@@ -0,0 +1,4 @@
[D-BUS Service]
Name = org.laptop.ActivityRegistry
Exec = @bindir@/sugar-shell-service
@@ -0,0 +1,4 @@
[D-BUS Service]
Name = org.laptop.Clipboard
Exec = @bindir@/sugar-shell-service
@@ -0,0 +1,4 @@
[D-BUS Service]
Name = org.laptop.ObjectTypeRegistry
Exec = @bindir@/sugar-shell-service
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# vi: ts=4 ai noet
#
# Copyright (C) 2006, 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 sys
import os
import logging
from sugar import logger
logger.start('shellservice')
import gobject
import dbus.glib
from sugar import env
sys.path.append(env.get_service_path('shell'))
import clipboardservice
import objecttypeservice
import activityregistryservice
logging.info('Starting shell service.')
gobject.threads_init()
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...'