2007-04-11 18:22:52 +02:00
|
|
|
import logging
|
|
|
|
from gettext import gettext as _
|
2007-04-18 23:52:46 +02:00
|
|
|
import urlparse
|
|
|
|
import posixpath
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
class FileType:
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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?
|
|
|
|
"""
|
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
def __init__(self, formats):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
self._formats = formats
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
raise NotImplementedError
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class TextFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['text/plain', 'UTF8_STRING', 'STRING'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('Text snippet')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:object-text'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
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):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'org.laptop.AbiWordActivity'
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class ImageFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['image/jpeg', 'image/gif', 'image/png', 'image/tiff'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('Image')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:object-image'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class UriFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['_NETSCAPE_URL'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('Web Page')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:object-link'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
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):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class PdfFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['application/pdf', 'application/x-pdf'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('PDF file')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:object-text'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'org.laptop.sugar.Xbook'
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class MsWordFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['application/msword'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('MS Word file')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:object-text'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'org.laptop.AbiWordActivity'
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class RtfFileType(TextFileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['application/rtf', 'text/rtf'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('RTF file')
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class AbiwordFileType(TextFileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.)
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['application/x-abiword'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('Abiword file')
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class SqueakProjectFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['application/x-squeak-project'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('Squeak project')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:object-squeak-project'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'org.vpri.EtoysActivity'
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class OOTextFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
_types = set(['application/vnd.oasis.opendocument.text'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('OpenOffice text file')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:object-text'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'org.laptop.AbiWordActivity'
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
class UriListFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-17 21:53:34 +02:00
|
|
|
|
|
|
|
_types = set(['text/uri-list'])
|
2007-04-18 23:52:46 +02:00
|
|
|
|
|
|
|
def _is_image(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-18 23:52:46 +02:00
|
|
|
uris = self._formats['text/uri-list'].get_data().split('\n')
|
|
|
|
if len(uris) == 1:
|
|
|
|
uri = urlparse.urlparse(uris[0])
|
|
|
|
ext = posixpath.splitext(uri.path)[1]
|
|
|
|
logging.debug(ext)
|
|
|
|
# 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
|
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-18 23:52:46 +02:00
|
|
|
if self._is_image():
|
|
|
|
return _('Image')
|
|
|
|
else:
|
|
|
|
return _('File')
|
2007-04-17 21:53:34 +02:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-18 23:52:46 +02:00
|
|
|
if self._is_image():
|
|
|
|
return 'theme:object-image'
|
|
|
|
else:
|
|
|
|
return 'theme:stock-missing'
|
2007-04-17 21:53:34 +02:00
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-18 23:52:46 +02:00
|
|
|
return ''
|
2007-04-17 21:53:34 +02:00
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-17 21:53:34 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-17 21:53:34 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-17 21:53:34 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-19 17:13:38 +02:00
|
|
|
class XoFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-19 17:13:38 +02:00
|
|
|
|
|
|
|
_types = set(['application/vnd.olpc-x-sugar'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns a localized human-readable name for this clipboard item format.
|
|
|
|
|
|
|
|
returns A localized string containing the clipboard item format name.
|
|
|
|
"""
|
2007-04-19 17:13:38 +02:00
|
|
|
return _('Activity package')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-19 17:13:38 +02:00
|
|
|
return 'theme:stock-missing'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-19 17:13:38 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Returns the activity identifier associated with this clipboard item format.
|
|
|
|
|
|
|
|
returns A string containing the activity identifier.
|
|
|
|
"""
|
2007-04-19 17:13:38 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-19 17:13:38 +02:00
|
|
|
return mime_type in cls._types
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-19 17:13:38 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class UnknownFileType(FileType):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
def get_name(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return _('Object')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return 'theme:stock-missing'
|
|
|
|
|
|
|
|
def get_preview(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return ''
|
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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).
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
return true
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-04-21 21:34:39 +02:00
|
|
|
|
2007-04-11 18:22:52 +02:00
|
|
|
class TypeRegistry:
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
def __init__(self):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""Initializes the registry.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
self._types = []
|
|
|
|
self._types.append(PdfFileType)
|
|
|
|
self._types.append(MsWordFileType)
|
|
|
|
self._types.append(RtfFileType)
|
|
|
|
self._types.append(OOTextFileType)
|
2007-04-17 21:53:34 +02:00
|
|
|
self._types.append(UriListFileType)
|
2007-04-11 18:22:52 +02:00
|
|
|
self._types.append(UriFileType)
|
|
|
|
self._types.append(ImageFileType)
|
|
|
|
self._types.append(AbiwordFileType)
|
|
|
|
self._types.append(TextFileType)
|
|
|
|
self._types.append(SqueakProjectFileType)
|
2007-04-19 17:13:38 +02:00
|
|
|
self._types.append(XoFileType)
|
2007-04-11 18:22:52 +02:00
|
|
|
|
|
|
|
def get_type(self, formats):
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
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():
|
2007-04-21 21:34:39 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2007-04-11 18:22:52 +02:00
|
|
|
global _type_registry
|
|
|
|
if not _type_registry:
|
|
|
|
_type_registry = TypeRegistry()
|
|
|
|
return _type_registry
|