2007-06-24 14:43:48 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2007-08-08 18:08:07 +02:00
|
|
|
# Copyright (C) 2007 One Laptop Per Child
|
2007-05-27 20:24:10 +02:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2007-07-09 20:14:24 +02:00
|
|
|
import logging
|
|
|
|
|
2007-05-27 20:24:10 +02:00
|
|
|
import dbus
|
2007-08-09 18:10:16 +02:00
|
|
|
import gobject
|
2007-05-27 20:24:10 +02:00
|
|
|
|
2007-08-08 18:08:07 +02:00
|
|
|
_ACTIVITY_REGISTRY_SERVICE_NAME = 'org.laptop.ActivityRegistry'
|
|
|
|
_ACTIVITY_REGISTRY_IFACE = 'org.laptop.ActivityRegistry'
|
|
|
|
_ACTIVITY_REGISTRY_PATH = '/org/laptop/ActivityRegistry'
|
2007-05-27 20:24:10 +02:00
|
|
|
|
|
|
|
def _activity_info_from_dict(info_dict):
|
2007-06-15 11:36:08 +02:00
|
|
|
if not info_dict:
|
|
|
|
return None
|
2007-05-27 20:24:10 +02:00
|
|
|
return ActivityInfo(info_dict['name'], info_dict['icon'],
|
2007-12-18 13:43:34 +01:00
|
|
|
info_dict['bundle_id'], info_dict['version'],
|
|
|
|
info_dict['path'], info_dict['show_launcher'],
|
2008-03-10 17:17:09 +01:00
|
|
|
info_dict['command'], info_dict['favorite'])
|
2007-05-27 20:24:10 +02:00
|
|
|
|
|
|
|
class ActivityInfo(object):
|
2007-12-18 13:43:34 +01:00
|
|
|
def __init__(self, name, icon, bundle_id, version,
|
2008-03-10 17:17:09 +01:00
|
|
|
path, show_launcher, command, favorite):
|
2007-05-27 20:24:10 +02:00
|
|
|
self.name = name
|
|
|
|
self.icon = icon
|
2007-10-09 13:15:06 +02:00
|
|
|
self.bundle_id = bundle_id
|
2007-12-18 13:43:34 +01:00
|
|
|
self.version = version
|
2007-05-27 20:24:10 +02:00
|
|
|
self.path = path
|
2007-10-08 14:56:12 +02:00
|
|
|
self.command = command
|
2007-08-09 18:10:16 +02:00
|
|
|
self.show_launcher = show_launcher
|
2008-03-10 17:17:09 +01:00
|
|
|
self.favorite = favorite
|
2007-05-27 20:24:10 +02:00
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
class ActivityRegistry(gobject.GObject):
|
|
|
|
__gsignals__ = {
|
2008-03-10 17:17:09 +01:00
|
|
|
'activity-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
2007-10-04 21:59:23 +02:00
|
|
|
'activity-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2008-03-10 17:17:09 +01:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'activity-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
2007-08-09 18:10:16 +02:00
|
|
|
}
|
2007-05-27 20:24:10 +02:00
|
|
|
def __init__(self):
|
2007-08-09 18:10:16 +02:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2007-05-27 20:24:10 +02:00
|
|
|
bus = dbus.SessionBus()
|
2007-08-22 23:37:33 +02:00
|
|
|
|
2007-08-25 00:25:56 +02:00
|
|
|
# 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
|
2007-08-08 18:08:07 +02:00
|
|
|
bus_object = bus.get_object(_ACTIVITY_REGISTRY_SERVICE_NAME,
|
2007-08-22 23:37:33 +02:00
|
|
|
_ACTIVITY_REGISTRY_PATH,
|
|
|
|
follow_name_owner_changes = True)
|
2007-08-08 18:08:07 +02:00
|
|
|
self._registry = dbus.Interface(bus_object, _ACTIVITY_REGISTRY_IFACE)
|
2008-04-19 11:10:03 +02:00
|
|
|
self._registry.connect_to_signal('ActivityAdded',
|
|
|
|
self._activity_added_cb)
|
|
|
|
self._registry.connect_to_signal('ActivityRemoved',
|
|
|
|
self._activity_removed_cb)
|
|
|
|
self._registry.connect_to_signal('ActivityChanged',
|
|
|
|
self._activity_changed_cb)
|
2007-07-09 20:14:24 +02:00
|
|
|
|
|
|
|
# Two caches fo saving some travel across dbus.
|
|
|
|
self._service_name_to_activity_info = {}
|
|
|
|
self._mime_type_to_activities = {}
|
2007-05-27 20:24:10 +02:00
|
|
|
|
2007-05-27 20:43:31 +02:00
|
|
|
def _convert_info_list(self, info_list):
|
2007-05-27 20:24:10 +02:00
|
|
|
result = []
|
|
|
|
|
2007-05-27 20:43:31 +02:00
|
|
|
for info_dict in info_list:
|
2007-05-27 20:24:10 +02:00
|
|
|
result.append(_activity_info_from_dict(info_dict))
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def get_activities(self):
|
|
|
|
info_list = self._registry.GetActivities()
|
|
|
|
return self._convert_info_list(info_list)
|
|
|
|
|
2007-08-27 21:47:58 +02:00
|
|
|
def _get_activities_cb(self, reply_handler, info_list):
|
|
|
|
result = []
|
|
|
|
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:
|
2008-04-19 11:10:03 +02:00
|
|
|
logging.error('Function get_activities_async called' \
|
|
|
|
'without a reply handler. Can not run.')
|
2007-08-27 21:47:58 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
self._registry.GetActivities(
|
2008-04-19 11:10:03 +02:00
|
|
|
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))
|
2007-08-27 21:47:58 +02:00
|
|
|
|
2007-06-12 21:57:49 +02:00
|
|
|
def get_activity(self, service_name):
|
2007-07-09 20:14:24 +02:00
|
|
|
if self._service_name_to_activity_info.has_key(service_name):
|
|
|
|
return self._service_name_to_activity_info[service_name]
|
|
|
|
|
2007-06-12 21:57:49 +02:00
|
|
|
info_dict = self._registry.GetActivity(service_name)
|
2007-07-09 20:14:24 +02:00
|
|
|
activity_info = _activity_info_from_dict(info_dict)
|
|
|
|
|
|
|
|
self._service_name_to_activity_info[service_name] = activity_info
|
|
|
|
return activity_info
|
2007-06-12 21:57:49 +02:00
|
|
|
|
|
|
|
def find_activity(self, name):
|
|
|
|
info_list = self._registry.FindActivity(name)
|
2007-05-27 20:43:31 +02:00
|
|
|
return self._convert_info_list(info_list)
|
|
|
|
|
2007-05-27 20:24:10 +02:00
|
|
|
def get_activities_for_type(self, mime_type):
|
2007-07-09 20:14:24 +02:00
|
|
|
if self._mime_type_to_activities.has_key(mime_type):
|
|
|
|
return self._mime_type_to_activities[mime_type]
|
|
|
|
|
2007-05-27 20:43:31 +02:00
|
|
|
info_list = self._registry.GetActivitiesForType(mime_type)
|
2007-07-09 20:14:24 +02:00
|
|
|
activities = self._convert_info_list(info_list)
|
|
|
|
|
|
|
|
self._mime_type_to_activities[mime_type] = activities
|
|
|
|
return activities
|
|
|
|
|
2007-08-09 18:10:16 +02:00
|
|
|
def add_bundle(self, bundle_path):
|
2007-11-18 19:25:28 +01:00
|
|
|
result = self._registry.AddBundle(bundle_path)
|
|
|
|
# Need to invalidate here because get_activity could be called after
|
|
|
|
# add_bundle and before we receive activity-added, causing a race.
|
|
|
|
self._invalidate_cache()
|
|
|
|
return result
|
2007-08-09 18:10:16 +02:00
|
|
|
|
|
|
|
def _activity_added_cb(self, info_dict):
|
2007-11-18 19:25:28 +01:00
|
|
|
logging.debug('ActivityRegistry._activity_added_cb: invalidating cache')
|
|
|
|
self._invalidate_cache()
|
|
|
|
self.emit('activity-added', _activity_info_from_dict(info_dict))
|
|
|
|
|
|
|
|
def _invalidate_cache(self):
|
2007-07-09 20:14:24 +02:00
|
|
|
self._service_name_to_activity_info.clear()
|
|
|
|
self._mime_type_to_activities.clear()
|
2007-06-12 21:57:49 +02:00
|
|
|
|
2007-10-04 21:59:23 +02:00
|
|
|
def remove_bundle(self, bundle_path):
|
2008-03-10 17:17:09 +01:00
|
|
|
self._invalidate_cache()
|
2007-10-04 21:59:23 +02:00
|
|
|
return self._registry.RemoveBundle(bundle_path)
|
|
|
|
|
|
|
|
def _activity_removed_cb(self, info_dict):
|
|
|
|
logging.debug('ActivityRegistry._activity_removed_cb: flushing caches')
|
2008-03-10 17:17:09 +01:00
|
|
|
self._invalidate_cache()
|
2007-10-04 21:59:23 +02:00
|
|
|
self.emit('activity-removed', _activity_info_from_dict(info_dict))
|
|
|
|
|
2008-03-10 17:17:09 +01:00
|
|
|
def _activity_changed_cb(self, info_dict):
|
|
|
|
logging.debug('ActivityRegistry._activity_changed_cb: flushing caches')
|
|
|
|
self._invalidate_cache()
|
|
|
|
self.emit('activity-changed', _activity_info_from_dict(info_dict))
|
|
|
|
|
|
|
|
def set_activity_favorite(self, bundle_id, version, favorite):
|
|
|
|
self._registry.SetActivityFavorite(bundle_id, version, favorite)
|
|
|
|
|
2007-06-13 11:50:05 +02:00
|
|
|
_registry = None
|
2007-06-12 21:57:49 +02:00
|
|
|
|
|
|
|
def get_registry():
|
2007-06-13 11:50:05 +02:00
|
|
|
global _registry
|
|
|
|
if not _registry:
|
|
|
|
_registry = ActivityRegistry()
|
2007-06-12 21:57:49 +02:00
|
|
|
return _registry
|