Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar/activity/registry.py
blob: a279aa9c0d9e4bdaf49c93f392bd05475a924dc3 (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
# Copyright (C) 2006-2007 Red Hat, Inc.
# Copyright (C) 2007 One Laptop Per Child
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import logging

import dbus
import gobject

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

def _activity_info_from_dict(info_dict):
    if not info_dict:
        return None
    return ActivityInfo(info_dict['name'], info_dict['icon'],
                        info_dict['bundle_id'], info_dict['path'],
                        info_dict['show_launcher'], info_dict['command'])

class ActivityInfo(object):
    def __init__(self, name, icon, bundle_id,
                 path, show_launcher, command):
        self.name = name
        self.icon = icon
        self.bundle_id = bundle_id
        self.path = path
        self.command = command
        self.show_launcher = show_launcher

class ActivityRegistry(gobject.GObject):
    __gsignals__ = {
        'activity-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                           ([gobject.TYPE_PYOBJECT])),
        'activity-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                           ([gobject.TYPE_PYOBJECT]))
    }
    def __init__(self):
        gobject.GObject.__init__(self)

        bus = dbus.SessionBus()

        # NOTE: We need to follow_name_owner_changes here
        #       because we can not connect to a signal unless 
        #       we follow the changes or we start the service
        #       before we connect.  Starting the service here
        #       causes a major bottleneck during startup
        bus_object = bus.get_object(_ACTIVITY_REGISTRY_SERVICE_NAME,
                                    _ACTIVITY_REGISTRY_PATH,
                                    follow_name_owner_changes = True)
        self._registry = dbus.Interface(bus_object, _ACTIVITY_REGISTRY_IFACE)
        self._registry.connect_to_signal('ActivityAdded', self._activity_added_cb)
        self._registry.connect_to_signal('ActivityRemoved', self._activity_removed_cb)

        # Two caches fo saving some travel across dbus.
        self._service_name_to_activity_info = {}
        self._mime_type_to_activities = {}

    def _convert_info_list(self, info_list):
        result = []

        for info_dict in info_list:
            result.append(_activity_info_from_dict(info_dict))

        return result

    def get_activities(self):
        info_list = self._registry.GetActivities()
        return self._convert_info_list(info_list)

    def _get_activities_cb(self, reply_handler, info_list):
        result = []
        i = 0
        for info_dict in info_list:
            result.append(_activity_info_from_dict(info_dict))

        reply_handler(result)

    def _get_activities_error_cb(self, error_handler, e):
        if error_handler:
            error_handler(e)
        else:
            logging.error('Error getting activities async: %s' % str(e))

    def get_activities_async(self, reply_handler=None, error_handler=None):
        if not reply_handler:
            logging.error('Function get_activities_async called without a reply handler. Can not run.') 
            return

        self._registry.GetActivities(
             reply_handler=lambda info_list:self._get_activities_cb(reply_handler, info_list),
             error_handler=lambda e:self._get_activities_error_cb(error_handler, e))

    def get_activity(self, service_name):
        if self._service_name_to_activity_info.has_key(service_name):
            return self._service_name_to_activity_info[service_name]

        info_dict = self._registry.GetActivity(service_name)
        activity_info = _activity_info_from_dict(info_dict)

        self._service_name_to_activity_info[service_name] = activity_info
        return activity_info

    def find_activity(self, name):
        info_list = self._registry.FindActivity(name)
        return self._convert_info_list(info_list)

    def get_activities_for_type(self, mime_type):
        if self._mime_type_to_activities.has_key(mime_type):
            return self._mime_type_to_activities[mime_type]

        info_list = self._registry.GetActivitiesForType(mime_type)
        activities = self._convert_info_list(info_list)

        self._mime_type_to_activities[mime_type] = activities
        return activities

    def add_bundle(self, bundle_path):
        return self._registry.AddBundle(bundle_path)

    def _activity_added_cb(self, info_dict):
        logging.debug('ActivityRegistry._activity_added_cb: flushing caches')
        self._service_name_to_activity_info.clear()
        self._mime_type_to_activities.clear()
        self.emit('activity-added', _activity_info_from_dict(info_dict))

    def remove_bundle(self, bundle_path):
        return self._registry.RemoveBundle(bundle_path)

    def _activity_removed_cb(self, info_dict):
        logging.debug('ActivityRegistry._activity_removed_cb: flushing caches')
        self._service_name_to_activity_info.clear()
        self._mime_type_to_activities.clear()
        self.emit('activity-removed', _activity_info_from_dict(info_dict))

_registry = None

def get_registry():
    global _registry
    if not _registry:
        _registry = ActivityRegistry()
    return _registry