Start reworking the cliboard services to use the types registry.
This commit is contained in:
@@ -7,11 +7,9 @@ $(service_DATA): $(service_in_files) Makefile
|
||||
|
||||
sugardir = $(pkgdatadir)/services/clipboard
|
||||
sugar_PYTHON = \
|
||||
__init__.py \
|
||||
clipboardobject.py \
|
||||
clipboardservice.py \
|
||||
typeregistry.py
|
||||
|
||||
__init__.py \
|
||||
clipboardobject.py \
|
||||
clipboardservice.py
|
||||
|
||||
bin_SCRIPTS = sugar-clipboard
|
||||
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import typeregistry
|
||||
|
||||
class ClipboardObject:
|
||||
|
||||
def __init__(self, object_path, name):
|
||||
self._id = object_path
|
||||
self._name = name
|
||||
@@ -11,24 +8,8 @@ class ClipboardObject:
|
||||
def get_id(self):
|
||||
return self._id
|
||||
|
||||
def _get_type_info(self):
|
||||
type_registry = typeregistry.get_instance()
|
||||
return type_registry.get_type(self._formats)
|
||||
|
||||
def get_name(self):
|
||||
if self._name:
|
||||
return self._name
|
||||
else:
|
||||
return self._get_type_info().get_name()
|
||||
|
||||
def get_icon(self):
|
||||
return self._get_type_info().get_icon()
|
||||
|
||||
def get_preview(self):
|
||||
return self._get_type_info().get_preview()
|
||||
|
||||
def get_activity(self):
|
||||
return self._get_type_info().get_activity()
|
||||
return self._name
|
||||
|
||||
def get_percent(self):
|
||||
return self._percent
|
||||
@@ -43,7 +24,6 @@ class ClipboardObject:
|
||||
return self._formats
|
||||
|
||||
class Format:
|
||||
|
||||
def __init__(self, type, data, on_disk):
|
||||
self._type = type
|
||||
self._data = data
|
||||
|
||||
@@ -27,9 +27,6 @@ import typeregistry
|
||||
|
||||
NAME_KEY = 'NAME'
|
||||
PERCENT_KEY = 'PERCENT'
|
||||
ICON_KEY = 'ICON'
|
||||
PREVIEW_KEY = 'PREVIEW'
|
||||
ACTIVITY_KEY = 'ACTIVITY'
|
||||
FORMATS_KEY = 'FORMATS'
|
||||
|
||||
class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
@@ -51,6 +48,18 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
self._next_id += 1
|
||||
return self._next_id
|
||||
|
||||
def _get_object_dict(self, object_path):
|
||||
cb_object = self._objects[str(object_path)]
|
||||
formats = cb_object.get_formats()
|
||||
format_types = dbus.Array([], 's')
|
||||
|
||||
for type, format in formats.iteritems():
|
||||
format_types.append(type)
|
||||
|
||||
return { NAME_KEY: cb_object.get_name(),
|
||||
PERCENT_KEY: cb_object.get_percent(),
|
||||
FORMATS_KEY: format_types }
|
||||
|
||||
def _handle_file_completed(self, cb_object):
|
||||
"""If the object is an on-disk file, and it's at 100%, and we care about
|
||||
it's file type, copy that file to $HOME and upate the clipboard object's
|
||||
@@ -63,10 +72,6 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
if not format.get_on_disk():
|
||||
return
|
||||
|
||||
if not len(cb_object.get_activity()):
|
||||
# no activity to handle this, don't autosave it
|
||||
return
|
||||
|
||||
# copy to homedir
|
||||
src = format.get_data()
|
||||
if not os.path.exists(src):
|
||||
@@ -100,11 +105,7 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
else:
|
||||
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()})
|
||||
self.object_changed(object_path, self._get_object_dict(object_path))
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="o", out_signature="")
|
||||
@@ -130,29 +131,12 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
if percent == 100:
|
||||
self._handle_file_completed(cb_object)
|
||||
|
||||
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()})
|
||||
self.object_state_changed(object_path, { PERCENT_KEY: percent })
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="o", out_signature="a{sv}")
|
||||
def get_object(self, object_path):
|
||||
cb_object = self._objects[str(object_path)]
|
||||
formats = cb_object.get_formats()
|
||||
format_types = dbus.Array([], '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)
|
||||
return dbus.Dictionary(self._get_object_dict(object_path))
|
||||
|
||||
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
|
||||
in_signature="os", out_signature="ay")
|
||||
@@ -166,6 +150,10 @@ class ClipboardDBusServiceHelper(dbus.service.Object):
|
||||
def object_added(self, object_path, name):
|
||||
pass
|
||||
|
||||
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="oa{sv}")
|
||||
def object_changed(self, object_path, values):
|
||||
pass
|
||||
|
||||
@dbus.service.signal(_CLIPBOARD_DBUS_INTERFACE, signature="o")
|
||||
def object_deleted(self, object_path):
|
||||
pass
|
||||
|
||||
@@ -1,243 +0,0 @@
|
||||
import logging
|
||||
from gettext import gettext as _
|
||||
|
||||
class FileType:
|
||||
def __init__(self, formats):
|
||||
self._formats = formats
|
||||
|
||||
def get_name(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_icon(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_preview(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_activity(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
raise NotImplementedError
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class TextFileType(FileType):
|
||||
|
||||
_types = set(['text/plain', 'UTF8_STRING', 'STRING'])
|
||||
|
||||
def get_name(self):
|
||||
return _('Text snippet')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:object-text'
|
||||
|
||||
def get_preview(self):
|
||||
for format, data in self._formats.iteritems():
|
||||
if format in TextFileType._types:
|
||||
text = data.get_data()
|
||||
if len(text) < 50:
|
||||
return text
|
||||
else:
|
||||
return text[0:49] + "..."
|
||||
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return 'org.laptop.AbiWordActivity'
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class ImageFileType(FileType):
|
||||
|
||||
_types = set(['image/jpeg', 'image/gif', 'image/png', 'image/tiff'])
|
||||
|
||||
def get_name(self):
|
||||
return _('Image')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:object-image'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class UriFileType(FileType):
|
||||
|
||||
_types = set(['_NETSCAPE_URL'])
|
||||
|
||||
def get_name(self):
|
||||
return _('Web Page')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:object-link'
|
||||
|
||||
def get_preview(self):
|
||||
for format, data in self._formats.iteritems():
|
||||
if format in UriFileType._types:
|
||||
string = data.get_data()
|
||||
title = string.split("\n")[1]
|
||||
return title
|
||||
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class PdfFileType(FileType):
|
||||
|
||||
_types = set(['application/pdf', 'application/x-pdf'])
|
||||
|
||||
def get_name(self):
|
||||
return _('PDF file')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:object-text'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return 'org.laptop.sugar.Xbook'
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class MsWordFileType(FileType):
|
||||
|
||||
_types = set(['application/msword'])
|
||||
|
||||
def get_name(self):
|
||||
return _('MS Word file')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:object-text'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return 'org.laptop.AbiWordActivity'
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class RtfFileType(TextFileType):
|
||||
|
||||
_types = set(['application/rtf', 'text/rtf'])
|
||||
|
||||
def get_name(self):
|
||||
return _('RTF file')
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class AbiwordFileType(TextFileType):
|
||||
|
||||
_types = set(['application/x-abiword'])
|
||||
|
||||
def get_name(self):
|
||||
return _('Abiword file')
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class SqueakProjectFileType(FileType):
|
||||
|
||||
_types = set(['application/x-squeak-project'])
|
||||
|
||||
def get_name(self):
|
||||
return _('Squeak project')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:object-squeak-project'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return 'org.vpri.EtoysActivity'
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class OOTextFileType(FileType):
|
||||
|
||||
_types = set(['application/vnd.oasis.opendocument.text'])
|
||||
|
||||
def get_name(self):
|
||||
return _('OpenOffice text file')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:object-text'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return 'org.laptop.AbiWordActivity'
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return mime_type in cls._types
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class UnknownFileType(FileType):
|
||||
def get_name(self):
|
||||
return _('Object')
|
||||
|
||||
def get_icon(self):
|
||||
return 'theme:stock-missing'
|
||||
|
||||
def get_preview(self):
|
||||
return ''
|
||||
|
||||
def get_activity(self):
|
||||
return ''
|
||||
|
||||
def matches_mime_type(cls, mime_type):
|
||||
return true
|
||||
matches_mime_type = classmethod(matches_mime_type)
|
||||
|
||||
class TypeRegistry:
|
||||
def __init__(self):
|
||||
self._types = []
|
||||
self._types.append(PdfFileType)
|
||||
self._types.append(MsWordFileType)
|
||||
self._types.append(RtfFileType)
|
||||
self._types.append(OOTextFileType)
|
||||
self._types.append(UriFileType)
|
||||
self._types.append(ImageFileType)
|
||||
self._types.append(AbiwordFileType)
|
||||
self._types.append(TextFileType)
|
||||
self._types.append(SqueakProjectFileType)
|
||||
|
||||
def get_type(self, formats):
|
||||
for file_type in self._types:
|
||||
for format, data in formats.iteritems():
|
||||
if file_type.matches_mime_type(format):
|
||||
return file_type(formats)
|
||||
|
||||
return UnknownFileType(formats)
|
||||
|
||||
_type_registry = None
|
||||
def get_instance():
|
||||
global _type_registry
|
||||
if not _type_registry:
|
||||
_type_registry = TypeRegistry()
|
||||
return _type_registry
|
||||
Reference in New Issue
Block a user