2007-01-05 21:13:46 +01:00
|
|
|
import logging
|
2007-01-07 23:38:33 +01:00
|
|
|
from gettext import gettext as _
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_activity(self):
|
|
|
|
raise NotImplementedError
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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):
|
2007-01-07 23:38:33 +01:00
|
|
|
return _('Text snippet')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-02-22 13:21:09 +01:00
|
|
|
return 'theme:object-text'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_preview(self):
|
|
|
|
for format, data in self._formats.iteritems():
|
|
|
|
if format in TextFileType._types:
|
2007-03-14 13:32:05 +01:00
|
|
|
text = data.get_data()
|
2007-01-07 23:38:33 +01:00
|
|
|
if len(text) < 50:
|
|
|
|
return text
|
|
|
|
else:
|
|
|
|
return text[0:49] + "..."
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
return ''
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_activity(self):
|
2007-03-14 13:32:05 +01:00
|
|
|
return 'org.laptop.AbiWordActivity'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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):
|
2007-01-07 23:38:33 +01:00
|
|
|
return _('Image')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-02-22 13:21:09 +01:00
|
|
|
return 'theme:object-image'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_preview(self):
|
|
|
|
return ''
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_activity(self):
|
|
|
|
return ''
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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):
|
2007-01-07 23:38:33 +01:00
|
|
|
return _('Web Page')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-02-22 13:21:09 +01:00
|
|
|
return 'theme:object-link'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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 ''
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_activity(self):
|
|
|
|
return ''
|
|
|
|
|
2007-01-05 21:13:46 +01:00
|
|
|
def matches_mime_type(cls, mime_type):
|
|
|
|
return mime_type in cls._types
|
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
|
|
|
class PdfFileType(FileType):
|
|
|
|
|
2007-03-29 12:25:47 +02:00
|
|
|
_types = set(['application/pdf', 'application/x-pdf'])
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_name(self):
|
2007-01-07 23:38:33 +01:00
|
|
|
return _('PDF file')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-02-22 13:21:09 +01:00
|
|
|
return 'theme:object-text'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_preview(self):
|
|
|
|
return ''
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_activity(self):
|
|
|
|
return 'org.laptop.sugar.Xbook'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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):
|
2007-01-07 23:38:33 +01:00
|
|
|
return _('MS Word file')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-02-22 13:21:09 +01:00
|
|
|
return 'theme:object-text'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_preview(self):
|
|
|
|
return ''
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_activity(self):
|
|
|
|
return 'org.laptop.AbiWordActivity'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
|
|
|
return mime_type in cls._types
|
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-03-14 13:32:05 +01:00
|
|
|
class RtfFileType(TextFileType):
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
_types = set(['application/rtf', 'text/rtf'])
|
|
|
|
|
|
|
|
def get_name(self):
|
2007-01-07 23:38:33 +01:00
|
|
|
return _('RTF file')
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def matches_mime_type(cls, mime_type):
|
|
|
|
return mime_type in cls._types
|
|
|
|
matches_mime_type = classmethod(matches_mime_type)
|
|
|
|
|
2007-03-22 18:22:37 +01:00
|
|
|
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)
|
|
|
|
|
2007-03-22 15:34:05 +01:00
|
|
|
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)
|
|
|
|
|
2007-01-11 23:57:06 +01:00
|
|
|
class OOTextFileType(FileType):
|
|
|
|
|
|
|
|
_types = set(['application/vnd.oasis.opendocument.text'])
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
return _('OpenOffice text file')
|
|
|
|
|
|
|
|
def get_icon(self):
|
2007-02-22 13:21:09 +01:00
|
|
|
return 'theme:object-text'
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_preview(self):
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def get_activity(self):
|
|
|
|
return 'org.laptop.AbiWordActivity'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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):
|
2007-01-07 23:38:33 +01:00
|
|
|
return _('Object')
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_icon(self):
|
2007-01-16 19:22:50 +01:00
|
|
|
return 'theme:stock-missing'
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
def get_preview(self):
|
|
|
|
return ''
|
2007-01-11 23:57:06 +01:00
|
|
|
|
|
|
|
def get_activity(self):
|
|
|
|
return ''
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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)
|
2007-01-11 23:57:06 +01:00
|
|
|
self._types.append(OOTextFileType)
|
2007-01-05 21:13:46 +01:00
|
|
|
self._types.append(UriFileType)
|
|
|
|
self._types.append(ImageFileType)
|
2007-03-22 18:22:37 +01:00
|
|
|
self._types.append(AbiwordFileType)
|
2007-01-05 21:13:46 +01:00
|
|
|
self._types.append(TextFileType)
|
2007-03-22 15:34:05 +01:00
|
|
|
self._types.append(SqueakProjectFileType)
|
2007-01-05 21:13:46 +01:00
|
|
|
|
|
|
|
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
|