Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--services/clipboard/Makefile.am3
-rw-r--r--services/clipboard/clipboardobject.py81
-rw-r--r--services/clipboard/clipboardservice.py13
-rw-r--r--services/clipboard/objecttypeservice.py8
-rwxr-xr-xservices/clipboard/sugar-clipboard9
-rw-r--r--services/clipboard/typeregistry.py844
6 files changed, 98 insertions, 860 deletions
diff --git a/services/clipboard/Makefile.am b/services/clipboard/Makefile.am
index d87276d..e5a03e4 100644
--- a/services/clipboard/Makefile.am
+++ b/services/clipboard/Makefile.am
@@ -20,8 +20,7 @@ sugar_PYTHON = \
__init__.py \
clipboardobject.py \
clipboardservice.py \
- objecttypeservice.py \
- typeregistry.py
+ objecttypeservice.py
bin_SCRIPTS = sugar-clipboard
diff --git a/services/clipboard/clipboardobject.py b/services/clipboard/clipboardobject.py
index 6505f1d..b7f5805 100644
--- a/services/clipboard/clipboardobject.py
+++ b/services/clipboard/clipboardobject.py
@@ -2,7 +2,10 @@ import os
import logging
import urlparse
-import typeregistry
+from sugar.objects import mime
+from sugar import activity
+
+import objecttypeservice
class ClipboardObject:
@@ -20,24 +23,58 @@ class ClipboardObject:
return self._id
def _get_type_info(self):
- type_registry = typeregistry.get_instance()
- return type_registry.get_type(self._formats)
+ 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:
- return self._get_type_info().get_name()
+ type_info = self._get_type_info()
+ if type_info:
+ return type_info['name']
+ else:
+ return ''
def get_icon(self):
- return self._get_type_info().get_icon()
+ type_info = self._get_type_info()
+ if type_info:
+ return type_info['icon']
+ else:
+ return ''
def get_preview(self):
- return self._get_type_info().get_preview()
+ # TODO: should previews really be here?
+ #return self._get_type_info().get_preview()
+ return ''
def get_activity(self):
- return self._get_type_info().get_activity()
-
+ 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'}
+ mime = self.get_mime_type()
+ if not mime:
+ return ''
+ """
+ registry = activity.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]
+ else:
+ return ''
+
def get_percent(self):
return self._percent
@@ -46,10 +83,36 @@ 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
-
+
+ def get_mime_type(self):
+ logging.debug('Choosing between %r.' % self._formats.keys())
+ if not self._formats:
+ return ''
+
+ if 'text/uri-list' in self._formats.keys():
+ data = self._formats['text/uri-list'].get_data()
+ uris = data.split('\n')
+ # TODO: could we do better when there are several uris?
+ uri = urlparse.urlparse(uris[0], 'file')
+ if uri.scheme == 'file':
+ logging.debug('Choosed %r!' % mime.get_for_file(uri.path))
+ return mime.get_for_file(uri.path)
+
+ for mime_category in ['image/', 'text/', 'application/']:
+ for mime_type in self._formats.keys():
+ if mime_type.startswith(mime_category):
+ mime_type = mime_type.split(';')[0]
+ logging.debug('Choosed %r!' % mime_type)
+ return mime_type
+
+ logging.debug('Returning first: %r.' % self._formats.keys()[0])
+ return self._formats.keys()[0]
+
class Format:
def __init__(self, type, data, on_disk):
diff --git a/services/clipboard/clipboardservice.py b/services/clipboard/clipboardservice.py
index aab97de..3dbec16 100644
--- a/services/clipboard/clipboardservice.py
+++ b/services/clipboard/clipboardservice.py
@@ -85,6 +85,7 @@ class ClipboardService(dbus.service.Object):
@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)
@@ -94,6 +95,7 @@ class ClipboardService(dbus.service.Object):
@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)]
cb_object.add_format(Format(format_type, data, on_disk))
@@ -142,6 +144,7 @@ class ClipboardService(dbus.service.Object):
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="o", out_signature="a{sv}")
def get_object(self, object_path):
+ logging.debug('ClipboardService.get_object')
cb_object = self._objects[str(object_path)]
formats = cb_object.get_formats()
format_types = dbus.Array([], 's')
@@ -159,7 +162,8 @@ class ClipboardService(dbus.service.Object):
@dbus.service.method(_CLIPBOARD_DBUS_INTERFACE,
in_signature="os", out_signature="a{sv}")
- def get_object_data(self, object_path, format_type):
+ 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(),
@@ -180,3 +184,10 @@ class ClipboardService(dbus.service.Object):
def object_state_changed(self, object_path, values):
pass
+_instance = None
+
+def get_instance():
+ global _instance
+ if not _instance:
+ _instance = ClipboardService()
+ return _instance
diff --git a/services/clipboard/objecttypeservice.py b/services/clipboard/objecttypeservice.py
index f24640a..70cf24f 100644
--- a/services/clipboard/objecttypeservice.py
+++ b/services/clipboard/objecttypeservice.py
@@ -63,3 +63,11 @@ class ObjectTypeRegistry(dbus.service.Object):
return object_type
else:
return {}
+
+_instance = None
+
+def get_instance():
+ global _instance
+ if not _instance:
+ _instance = ObjectTypeRegistry()
+ return _instance
diff --git a/services/clipboard/sugar-clipboard b/services/clipboard/sugar-clipboard
index ddac295..6ce1e86 100755
--- a/services/clipboard/sugar-clipboard
+++ b/services/clipboard/sugar-clipboard
@@ -2,6 +2,7 @@
# 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
@@ -34,16 +35,16 @@ from sugar import env
sys.path.append(env.get_service_path('clipboard'))
-from clipboardservice import ClipboardService
-from objecttypeservice import ObjectTypeRegistry
+import clipboardservice
+import objecttypeservice
logging.info('Starting clipboard service.')
gobject.threads_init()
dbus.glib.threads_init()
-clipboard_service = ClipboardService()
-object_type_registry = ObjectTypeRegistry()
+clipboard_service = clipboardservice.get_instance()
+object_type_registry = objecttypeservice.get_instance()
loop = gobject.MainLoop()
try:
diff --git a/services/clipboard/typeregistry.py b/services/clipboard/typeregistry.py
deleted file mode 100644
index eaaf6cb..0000000
--- a/services/clipboard/typeregistry.py
+++ /dev/null
@@ -1,844 +0,0 @@
-import logging
-from gettext import gettext as _
-import urlparse
-import posixpath
-
-class FileType:
- """Generic base class for all classes representing clipboard item formats.
-
- XXX This class's name is misleading; it represents a clipboard item
- format and not a file type per se. Perhaps it should be renamed to
- ClipboardItemFormat?
- """
-
- def __init__(self, formats):
- """Initializer for this class (and all subclasses).
-
- formats -- A dictionary of key-value pairs where the keys are MIME type
- strings and the data values are the clipboard data in each
- respective format. A reference to this dictionary is stored within
- the object for possible use later.
-
- This initializer is invoked when the clipboard item format object is
- instantiated by the TypeRegistry class's get_type method.
-
- """
- self._formats = formats
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- This base-class implementation raises a NotImplementedError exception.
-
- returns A localized string containing the clipboard item format name.
- """
- raise NotImplementedError
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- This base-class implementation raises a NotImplementedError exception.
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- raise NotImplementedError
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- This base-class implementation raises a NotImplementedError exception.
-
- returns A string containing the item preview.
- """
- raise NotImplementedError
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- This base-class implementation raises a NotImplementedError exception.
-
- returns A string containing the activity identifier.
- """
- raise NotImplementedError
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This base-class implementation currently raises a
- NotImplementedError exception, but could (and should) be rewritten to
- handle this functionality for *all* subclasses, since the code is
- identical for all or most subclasses anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- raise NotImplementedError
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class TextFileType(FileType):
- """Represents the text clipboard item format.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['text/plain', 'UTF8_STRING', 'STRING'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('Text snippet')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:object-text'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- For the text file format, this returns a text string containing up to
- 49 characters of the actual item's text.
-
- returns A preview string containing up to 49 characters of the item's
- text.
- """
- 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):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return 'org.laptop.AbiWordActivity'
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class ImageFileType(FileType):
- """Represents the "image" clipboard item format.
-
- This clipboard item format represents *any* image format, including JPEG,
- GIF, PNG and TIFF formats.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['image/jpeg', 'image/gif', 'image/png', 'image/tiff'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('Image')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:object-image'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- XXX Currently returns an empty string.
-
- returns A string containing the item preview.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return ''
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class UriFileType(FileType):
- """Represents the URI clipboard item format.
-
- Not to be confused with the multiple-URI UriListFileType class.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['_NETSCAPE_URL'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('Web Page')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:object-link'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- For the URI clipboard item format, this is the URI itself.
-
- returns A string containing the item preview, in this case the URI itself.
- """
- 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):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return ''
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class PdfFileType(FileType):
- """Represents the PDF clipboard item format.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['application/pdf', 'application/x-pdf'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('PDF file')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:object-text'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- XXX Currently returns an empty string.
-
- returns A string containing the item preview.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return 'org.laptop.sugar.Xbook'
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class MsWordFileType(FileType):
- """Represents the MS Word clipboard item format (*cringe*).
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['application/msword'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('MS Word file')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:object-text'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- XXX Currently returns an empty string.
-
- returns A string containing the item preview.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return 'org.laptop.AbiWordActivity'
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class RtfFileType(TextFileType):
- """Represents the RTF clipboard item format (a subclass of TextFileType).
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['application/rtf', 'text/rtf'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('RTF file')
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class AbiwordFileType(TextFileType):
- """Represents the AbiWord clipboard item format.
-
- (XXX This class's name is misleading; it represents a clipboard item format,
- not a file type per se.)
- (XXX AbiWord format is a full word processing format, like the OO Text and MS Word
- formats, and should not be a subclass of TextFileType! Otherwise the OO Text and
- MS Word format types should also be made subclasses of TextFileType.)
- """
-
- _types = set(['application/x-abiword'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('Abiword file')
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class SqueakProjectFileType(FileType):
- """Represents the Squeak Project clipboard item format.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['application/x-squeak-project'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('Squeak project')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:object-squeak-project'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- XXX Currently returns an empty string.
-
- returns A string containing the item preview.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return 'org.vpri.EtoysActivity'
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class OOTextFileType(FileType):
- """Represents the OpenDocument Text (OpenOffice.org Writer) clipboard item format.
-
- Note to the uninitiated: OpenDocument Text is a full word processing format,
- not a simple text file.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['application/vnd.oasis.opendocument.text'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('OpenOffice text file')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:object-text'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- XXX Currently returns an empty string.
-
- returns A string containing the item preview.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return 'org.laptop.AbiWordActivity'
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class UriListFileType(FileType):
- """Represents the URI-list clipboard item format.
-
- Not to be confused with the single-URI UriFileType class.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['text/uri-list'])
-
- def _is_image(self):
- """Determines whether this URI list represents an image.
-
- Called by the get_name and get_icon methods. For this URI list to be
- considered to represent an image, it must fulfil two criteria: (1) it
- must have only one URI in it, and (2) the URI must end with an extension
- indicating an image type (currently ".jpg", ".jpeg", ".gif", ".png", or
- ".svg").
-
- returns True if there is one URI in the list and it represents an image,
- False otherwise.
- """
- uris = self._formats['text/uri-list'].get_data().split('\n')
- if len(uris) == 1:
- uri = urlparse.urlparse(uris[0])
- ext = posixpath.splitext(uri[2])[1]
- # FIXME: Bad hack, the type registry should treat text/uri-list as a special case.
- if ext in ['.jpg', '.jpeg', '.gif', '.png', '.svg']:
- return True
-
- return False
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- if self._is_image():
- return _('Image')
- else:
- return _('File')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- if self._is_image():
- return 'theme:object-image'
- else:
- return 'theme:stock-missing'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- XXX Currently returns an empty string.
-
- returns A string containing the item preview.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return ''
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class XoFileType(FileType):
- """Represents the "xo" (OLPC package) clipboard item format.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
-
- _types = set(['application/vnd.olpc-x-sugar'])
-
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('Activity package')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:stock-missing'
-
- def get_preview(self):
- """Returns an appropriate preview of the clipboard item data for this item format.
-
- XXX Currently returns an empty string.
-
- returns A string containing the item preview.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- returns A string containing the activity identifier.
- """
- return ''
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- XXX This subclass method is unnecessary; a single base class method
- in the FileType class would be sufficient to handle all or most
- subclasses, since the code is identical for all or most subclasses
- anyway.
-
- returns True if this class handles the given MIME type, False otherwise.
- """
- return mime_type in cls._types
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class UnknownFileType(FileType):
- """Represents an unknown clipboard item format.
-
- XXX This class's name is misleading; it represents a clipboard item format
- and not a file type per se.
- """
- def get_name(self):
- """Returns a localized human-readable name for this clipboard item format.
-
- Since this clipboard item format is unknown, the format name will be
- simply "Object" or something to that effect.
-
- returns A localized string containing the clipboard item format name.
- """
- return _('Object')
-
- def get_icon(self):
- """XXX Returns a "tag" to be used to get an icon for this clipboard item format (I think).
-
- returns XXX A string "tag" to be used to get the icon for this clipboard
- item format (I think).
- """
- return 'theme:stock-missing'
-
- def get_preview(self):
- """Returns a "preview" for this clipboard item format.
-
- Since this clipboard item format is unknown, there can be no preview for
- it, so the "preview" will be an empty string.
-
- returns An empty string, since there can be no preview for an unknown
- clipboard item format.
- """
- return ''
-
- def get_activity(self):
- """Returns the activity identifier associated with this clipboard item format.
-
- Since this clipboard item format is unknown, there can be no activity
- associated with it, so the activity identifier will be an empty string.
-
- returns A string containing the activity identifier (an empty string in
- this case).
- """
- return ''
-
- def matches_mime_type(cls, mime_type):
- """Class method to determine whether this class handles a given MIME type.
-
- mime_type -- A string containing the MIME type.
-
- Always returns true, since UnknownFileType is the clipboard item format
- class of last resort and must be used if no other class handles the
- given MIME type.
-
- returns True if this class handles the given MIME type (always true in
- this case).
- """
- return true
-
- matches_mime_type = classmethod(matches_mime_type)
-
-
-class TypeRegistry:
- """Represents a registry of all clipboard item formats.
-
- There will be one single global instantiation of this object, accessible via
- the global typeregistry.get_instance method.
- """
- def __init__(self):
- """Initializes the registry.
- """
- self._types = []
- self._types.append(PdfFileType)
- self._types.append(MsWordFileType)
- self._types.append(RtfFileType)
- self._types.append(OOTextFileType)
- self._types.append(UriListFileType)
- self._types.append(UriFileType)
- self._types.append(ImageFileType)
- self._types.append(AbiwordFileType)
- self._types.append(TextFileType)
- self._types.append(SqueakProjectFileType)
- self._types.append(XoFileType)
-
- def get_type(self, formats):
- """Returns a matching clipboard item format object for the passed-in clipboard data.
-
- formats -- A dictionary of key-value pairs where the keys are MIME type
- strings and the data values are the clipboard data in each
- respective format.
-
- A reference to the given clipboard data format dictionary is stored
- within the returned object for possible use later.
-
- XXX The order of the clipboard item formats in the TypeRegistry object's
- internal list matters. For example, if clipboard item data is available
- in both MS Word and OO Text formats, the MS Word type will currently be
- matched first, and therefore an MsWordFileType object will be returned.
- Is this really what is desired? The ordering should definitely be given
- some thought.
-
- returns A clipboard item format object of a type matching one of the
- MIME type keys in the formats dictionary, or an UnknownFileType
- object if none of the other types is appropriate.
- """
- 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():
- """Returns the global clipboard item format registry object (global method).
-
- Returns a reference to the global clipboard item format registry object,
- instantiating it first if necessary.
-
- returns A TypeRegistry object representing the global clipboard item format
- registry.
- """
- global _type_registry
- if not _type_registry:
- _type_registry = TypeRegistry()
- return _type_registry