Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/service/activityregistryservice.py
blob: 9c2dda7f809ab0afff1b7e191d545ff12bcd6cb0 (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
# Copyright (C) 2006-2007 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
# 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

import bundleregistry

_ACTIVITY_REGISTRY_SERVICE_NAME = 'org.laptop.ActivityRegistry'
_ACTIVITY_REGISTRY_IFACE = 'org.laptop.ActivityRegistry'
_ACTIVITY_REGISTRY_PATH = '/org/laptop/ActivityRegistry'

class ActivityRegistry(dbus.service.Object):
    def __init__(self):
        bus = dbus.SessionBus()
        bus_name = dbus.service.BusName(_ACTIVITY_REGISTRY_SERVICE_NAME, bus=bus)
        dbus.service.Object.__init__(self, bus_name, _ACTIVITY_REGISTRY_PATH)

        bundle_registry = bundleregistry.get_registry()
        bundle_registry.connect('bundle-added', self._bundle_added_cb)
        bundle_registry.connect('bundle-removed', self._bundle_removed_cb)

    @dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
                         in_signature='s', out_signature='b')
    def AddBundle(self, bundle_path):
        '''Register the activity bundle with the global registry 
        
        bundle_path -- path to the root directory of the activity bundle,
            that is, the directory with activity/activity.info as a 
            child of the directory.
        
        The bundleregistry.BundleRegistry is responsible for setting 
        up a set of d-bus service mappings for each available activity.
        '''
        registry = bundleregistry.get_registry()
        return registry.add_bundle(bundle_path)

    @dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
                         in_signature='s', out_signature='b')
    def RemoveBundle(self, bundle_path):
        '''Unregister the activity bundle with the global registry 
        
        bundle_path -- path to the activity bundle root directory
        '''
        registry = bundleregistry.get_registry()
        return registry.remove_bundle(bundle_path)

    @dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
                         in_signature='', out_signature='aa{sv}')
    def GetActivities(self):
        result = []
        registry = bundleregistry.get_registry()
        for bundle in registry:
            result.append(self._bundle_to_dict(bundle))
        return result

    @dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
                         in_signature='s', out_signature='a{sv}')
    def GetActivity(self, bundle_id):
        registry = bundleregistry.get_registry()
        bundle = registry.get_bundle(bundle_id)
        if not bundle:
            return {}
        
        return self._bundle_to_dict(bundle)

    @dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
                         in_signature='s', out_signature='aa{sv}')
    def FindActivity(self, name):
        result = []
        key = name.lower()

        for bundle in bundleregistry.get_registry():
            name = bundle.get_name().lower()
            bundle_id = bundle.get_bundle_id().lower()
            if name.find(key) != -1 or bundle_id.find(key) != -1:
                result.append(self._bundle_to_dict(bundle))

        return result

    @dbus.service.method(_ACTIVITY_REGISTRY_IFACE,
                         in_signature='s', out_signature='aa{sv}')
    def GetActivitiesForType(self, mime_type):
        result = []
        registry = bundleregistry.get_registry()
        for bundle in registry.get_activities_for_type(mime_type):
            result.append(self._bundle_to_dict(bundle))
        return result

    @dbus.service.signal(_ACTIVITY_REGISTRY_IFACE, signature='a{sv}')
    def ActivityAdded(self, activity_info):
        pass

    @dbus.service.signal(_ACTIVITY_REGISTRY_IFACE, signature='a{sv}')
    def ActivityRemoved(self, activity_info):
        pass

    def _bundle_to_dict(self, bundle):
        return {'name': bundle.get_name(),
                'icon': bundle.get_icon(),
                'bundle_id': bundle.get_bundle_id(),
                'version': bundle.get_activity_version(),
                'path': bundle.get_path(),
                'command': bundle.get_command(),
                'show_launcher': bundle.get_show_launcher()}

    def _bundle_added_cb(self, bundle_registry, bundle):
        self.ActivityAdded(self._bundle_to_dict(bundle))

    def _bundle_removed_cb(self, bundle_registry, bundle):
        self.ActivityRemoved(self._bundle_to_dict(bundle))

_instance = None

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