Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/services/clipboard/objecttypeservice.py
blob: e12398ec089f8417c4fcf530f887ddcb0569d0bf (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
# Copyright (C) 2007, Red Hat, Inc.
#
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import dbus
import dbus.service

_REGISTRY_IFACE = "org.laptop.ObjectTypeRegistry"
_REGISTRY_PATH = "/org/laptop/ObjectTypeRegistry"

class ObjectTypeRegistry(dbus.service.Object):
    def __init__(self):
        bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(_REGISTRY_IFACE, bus=bus)
        dbus.service.Object.__init__(self, bus_name, _REGISTRY_PATH)

        self._types = {}

        from gettext import gettext as _
        self._add_primitive('Text', _('Text'), 'theme:object-text',
                            [ 'text/plain', 'text/rtf', 'application/pdf',
                              'application/x-pdf', 'text/html',
                              'application/vnd.oasis.opendocument.text' ])
        self._add_primitive('Image', _('Image'), 'theme:object-image',
                            [ 'image/png', 'image/gif', 'image/jpeg' ])

    def _add_primitive(self, type_id, name, icon, mime_types):
        object_type = {'type_id': type_id, 
                       'name': name,
                       'icon': icon,
                       'mime_types': mime_types}
        self._types[type_id] = object_type

    def _get_type_for_mime(self, mime_type):
        for object_type in self._types.values():
            if mime_type in object_type['mime_types']:
                return object_type

    @dbus.service.method(_REGISTRY_IFACE,
                         in_signature="s", out_signature="a{sv}")
    def GetType(self, type_id):
        if self._types.has_key(type_id):
            return self._types[type_id]
        else:
            return {}

    @dbus.service.method(_REGISTRY_IFACE,
                         in_signature="s", out_signature="a{sv}")
    def GetTypeForMIME(self, mime_type):
        object_type = self._get_type_for_mime(mime_type)
        if object_type:
            return object_type
        else:
            return {}

_instance = None

def get_instance():
    global _instance
    if not _instance:
        _instance = ObjectTypeRegistry()
    return _instance