Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/clipboard/typeregistry.py
blob: 0a1aaa2f286f89dabc2929fe5576e58bd8c9fceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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 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 'activity-xbook'

    def get_preview(self):
        for format, data in self._formats.iteritems():
            if format in TextFileType._types:
                text = str(data.get_data())
                if len(text) < 50:
                    return text
                else:
                    return text[0:49] + "..."

        return ''
        
    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 'activity-sketch'

    def get_preview(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 'activity-web'

    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 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'])
    
    def get_name(self):
        return _('PDF file')

    def get_icon(self):
        return 'activity-xbook'

    def get_preview(self):
        return ''
        
    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 'activity-abiword'

    def get_preview(self):
        return ''
        
    def matches_mime_type(cls, mime_type):
        return mime_type in cls._types
    matches_mime_type = classmethod(matches_mime_type)

class RtfFileType(FileType):
    
    _types = set(['application/rtf', 'text/rtf'])
    
    def get_name(self):
        return _('RTF file')

    def get_icon(self):
        return 'activity-abiword'

    def get_preview(self):
        return ''
        
    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 'stock-missing'

    def get_preview(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(UriFileType)
        self._types.append(ImageFileType)
        self._types.append(TextFileType)
    
    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