2007-06-24 14:45:05 +02:00
|
|
|
# Copyright (C) 2006-2007 Red Hat, Inc.
|
2007-04-11 14:06:27 +02:00
|
|
|
#
|
2007-05-27 20:24:10 +02:00
|
|
|
# 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.
|
2007-04-11 14:06:27 +02:00
|
|
|
#
|
2007-05-27 20:24:10 +02:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
2007-04-11 14:06:27 +02:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2007-05-27 20:24:10 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2007-04-11 14:06:27 +02:00
|
|
|
#
|
2007-05-27 20:24:10 +02:00
|
|
|
# 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
|
2007-04-11 14:06:27 +02:00
|
|
|
|
2006-10-29 19:05:09 +01:00
|
|
|
import os
|
2007-03-09 14:57:54 +01:00
|
|
|
|
2007-01-27 12:54:56 +01:00
|
|
|
import gobject
|
2006-10-29 19:05:09 +01:00
|
|
|
|
2007-09-20 18:20:21 +02:00
|
|
|
from sugar.bundle.activitybundle import ActivityBundle
|
|
|
|
from sugar.bundle.bundle import MalformedBundleException
|
2006-10-31 12:06:28 +01:00
|
|
|
from sugar import env
|
2006-12-01 23:23:58 +01:00
|
|
|
from sugar import util
|
2006-10-31 10:48:45 +01:00
|
|
|
|
2007-03-09 14:57:54 +01:00
|
|
|
# http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
|
|
|
|
def _get_data_dirs():
|
|
|
|
if os.environ.has_key('XDG_DATA_DIRS'):
|
|
|
|
return os.environ['XDG_DATA_DIRS'].split(':')
|
|
|
|
else:
|
|
|
|
return [ '/usr/local/share/', '/usr/share/' ]
|
|
|
|
|
2007-09-11 21:53:26 +02:00
|
|
|
def _load_mime_defaults():
|
|
|
|
defaults = {}
|
|
|
|
|
|
|
|
f = open(env.get_data_path('mime.defaults'), 'r')
|
|
|
|
for line in f.readlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line and not line.startswith('#'):
|
|
|
|
mime = line[:line.find(' ')]
|
|
|
|
handler = line[line.rfind(' ') + 1:]
|
|
|
|
defaults[mime] = handler
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
return defaults
|
|
|
|
|
2006-10-31 10:48:45 +01:00
|
|
|
class _ServiceManager(object):
|
2007-04-10 04:47:37 +02:00
|
|
|
"""Internal class responsible for creating dbus service files
|
|
|
|
|
|
|
|
DBUS services are defined in files which bind a service name
|
|
|
|
to the name of an executable which provides the service name.
|
|
|
|
|
|
|
|
In Sugar, the service files are automatically generated from
|
|
|
|
the activity registry (by this class). When an activity's
|
|
|
|
dbus launch service is requested, dbus will launch the
|
|
|
|
specified executable in order to allow it to provide the
|
|
|
|
requested activity-launching service.
|
|
|
|
|
|
|
|
In the case of activities which provide a "class", instead of
|
|
|
|
an "exec" attribute in their activity.info, the
|
|
|
|
sugar-activity-factory script is used with an appropriate
|
|
|
|
argument to service that bundle.
|
|
|
|
"""
|
|
|
|
SERVICE_DIRECTORY = '~/.local/share/dbus-1/services'
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self):
|
2007-04-10 04:47:37 +02:00
|
|
|
service_dir = os.path.expanduser(self.SERVICE_DIRECTORY)
|
2007-03-09 14:57:54 +01:00
|
|
|
if not os.path.isdir(service_dir):
|
|
|
|
os.makedirs(service_dir)
|
|
|
|
|
|
|
|
self._path = service_dir
|
2006-10-31 10:48:45 +01:00
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def add(self, bundle):
|
2007-02-22 15:46:13 +01:00
|
|
|
util.write_service(bundle.get_service_name(),
|
2007-09-03 23:08:49 +02:00
|
|
|
bundle.get_command(), self._path)
|
2006-10-31 10:48:45 +01:00
|
|
|
|
2007-10-04 21:59:23 +02:00
|
|
|
def remove(self, bundle):
|
|
|
|
util.delete_service(bundle.get_service_name(), self._path)
|
|
|
|
|
2007-01-27 12:54:56 +01:00
|
|
|
class BundleRegistry(gobject.GObject):
|
2006-12-04 20:12:24 +01:00
|
|
|
"""Service that tracks the available activity bundles"""
|
|
|
|
|
2007-01-27 12:54:56 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'bundle-added': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-10-04 21:59:23 +02:00
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'bundle-removed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
2007-01-27 12:54:56 +01:00
|
|
|
}
|
|
|
|
|
2006-12-04 20:12:24 +01:00
|
|
|
def __init__(self):
|
2007-01-27 12:54:56 +01:00
|
|
|
gobject.GObject.__init__(self)
|
|
|
|
|
2007-08-16 16:36:20 +02:00
|
|
|
self._bundles = []
|
2006-12-04 20:12:24 +01:00
|
|
|
self._search_path = []
|
|
|
|
self._service_manager = _ServiceManager()
|
2007-09-11 21:53:26 +02:00
|
|
|
self._mime_defaults = _load_mime_defaults()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def get_bundle(self, service_name):
|
|
|
|
"""Returns an bundle given his service name"""
|
2007-08-16 16:36:20 +02:00
|
|
|
for bundle in self._bundles:
|
|
|
|
if bundle.get_service_name() == service_name:
|
|
|
|
return bundle
|
|
|
|
return None
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def add_search_path(self, path):
|
|
|
|
"""Add a directory to the bundles search path"""
|
|
|
|
self._search_path.append(path)
|
|
|
|
self._scan_directory(path)
|
|
|
|
|
|
|
|
def __iter__(self):
|
2007-08-16 16:36:20 +02:00
|
|
|
return self._bundles.__iter__()
|
2006-12-04 20:12:24 +01:00
|
|
|
|
|
|
|
def _scan_directory(self, path):
|
2007-08-16 16:36:20 +02:00
|
|
|
if not os.path.isdir(path):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Sort by mtime to ensure a stable activity order
|
|
|
|
bundles = {}
|
|
|
|
for f in os.listdir(path):
|
|
|
|
if not f.endswith('.activity'):
|
|
|
|
continue
|
|
|
|
bundle_dir = os.path.join(path, f)
|
|
|
|
if os.path.isdir(bundle_dir):
|
|
|
|
bundles[bundle_dir] = os.stat(bundle_dir).st_mtime
|
|
|
|
|
|
|
|
bundle_dirs = bundles.keys()
|
|
|
|
bundle_dirs.sort(lambda d1,d2: cmp(bundles[d1], bundles[d2]))
|
|
|
|
for dir in bundle_dirs:
|
|
|
|
self.add_bundle(dir)
|
2006-12-04 20:12:24 +01:00
|
|
|
|
2007-01-27 12:54:56 +01:00
|
|
|
def add_bundle(self, bundle_path):
|
2007-09-20 18:20:21 +02:00
|
|
|
try:
|
|
|
|
bundle = ActivityBundle(bundle_path)
|
|
|
|
except MalformedBundleException:
|
2007-01-27 12:54:56 +01:00
|
|
|
return False
|
2007-02-21 17:53:44 +01:00
|
|
|
|
2007-09-20 18:20:21 +02:00
|
|
|
self._bundles.append(bundle)
|
|
|
|
self._service_manager.add(bundle)
|
|
|
|
self.emit('bundle-added', bundle)
|
|
|
|
return True
|
|
|
|
|
2007-10-04 21:59:23 +02:00
|
|
|
def remove_bundle(self, bundle_path):
|
|
|
|
for bundle in self._bundles:
|
|
|
|
if bundle.get_path() == bundle_path:
|
|
|
|
self._bundles.remove(bundle)
|
|
|
|
self._service_manager.remove(bundle)
|
|
|
|
self.emit('bundle-removed', bundle)
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2007-08-08 18:08:07 +02:00
|
|
|
def get_activities_for_type(self, mime_type):
|
|
|
|
result = []
|
2007-08-16 16:36:20 +02:00
|
|
|
for bundle in self._bundles:
|
2007-08-08 18:08:07 +02:00
|
|
|
if bundle.get_mime_types() and mime_type in bundle.get_mime_types():
|
2007-09-15 03:14:30 +02:00
|
|
|
if self.get_default_for_type(mime_type) == bundle.get_service_name():
|
2007-09-11 21:53:26 +02:00
|
|
|
result.insert(0, bundle)
|
|
|
|
else:
|
|
|
|
result.append(bundle)
|
2007-08-08 18:08:07 +02:00
|
|
|
return result
|
|
|
|
|
2007-09-15 03:14:30 +02:00
|
|
|
def get_default_for_type(self, mime_type):
|
|
|
|
if self._mime_defaults.has_key(mime_type):
|
|
|
|
return self._mime_defaults[mime_type]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
2007-02-21 17:53:44 +01:00
|
|
|
def get_registry():
|
|
|
|
return _bundle_registry
|
|
|
|
|
|
|
|
_bundle_registry = BundleRegistry()
|
|
|
|
|
2007-03-09 14:57:54 +01:00
|
|
|
for path in _get_data_dirs():
|
2007-02-21 17:53:44 +01:00
|
|
|
bundles_path = os.path.join(path, 'activities')
|
|
|
|
_bundle_registry.add_search_path(bundles_path)
|
|
|
|
|
2007-03-09 14:57:54 +01:00
|
|
|
_bundle_registry.add_search_path(env.get_user_activities_path())
|