From 705884f899440c20dc224a30ed3ef75ea9c7b9aa Mon Sep 17 00:00:00 2001
From: Tomeu Vizoso <[EMAIL PROTECTED]>
Date: Sun, 6 Apr 2008 19:31:17 +0200
Subject: [PATCH] Identify bundles also by their version number.

---
 sugar/activity/activityfactory.py   |   33 ++++++++++++++++++---------------
 sugar/activity/registry.py          |   16 ++++++++--------
 sugar/clipboard/clipboardservice.py |   19 +++++++++----------
 sugar/datastore/datastore.py        |   24 +++++++++++++++---------
 4 files changed, 50 insertions(+), 42 deletions(-)

diff --git a/sugar/activity/activityfactory.py b/sugar/activity/activityfactory.py
index 1638197..7cb843a 100644
--- a/sugar/activity/activityfactory.py
+++ b/sugar/activity/activityfactory.py
@@ -163,10 +163,11 @@ class ActivityCreationHandler(gobject.GObject):
     create call.
     """
 
-    def __init__(self, service_name, handle):
+    def __init__(self, bundle_id, version, handle):
         """Initialise the handler
 
-        service_name -- the service name of the bundle factory
+        bundle_id -- id of the bundle to be started
+        version -- version of the bundle to be started
         activity_handle -- stores the values which are to
             be passed to the service to uniquely identify
             the activity to be created and the sharing
@@ -189,11 +190,12 @@ class ActivityCreationHandler(gobject.GObject):
         """
         gobject.GObject.__init__(self)
 
-        self._service_name = service_name
+        self._bundle_id = bundle_id
+        self._version = version
         self._handle = handle
 
         self._use_rainbow = os.path.exists('/etc/olpc-security')
-        if service_name in [ 'org.laptop.JournalActivity',
+        if bundle_id in [ 'org.laptop.JournalActivity',
                              'org.laptop.Terminal',
                              'org.laptop.LogViewer',
                              'org.laptop.Analyze'
@@ -228,12 +230,13 @@ class ActivityCreationHandler(gobject.GObject):
             self._handle.activity_id = create_activity_id()
 
         self._shell.NotifyLaunch(
-                    self._service_name, self._handle.activity_id,
+                    self._bundle_id, self._handle.activity_id,
                     reply_handler=self._no_reply_handler,
                     error_handler=self._notify_launch_error_handler)
 
         activity_registry = registry.get_registry()
-        activity = activity_registry.get_activity(self._service_name)
+        activity = activity_registry.get_activity(self._bundle_id,
+                                                  self._version)
         if activity:
             environ = get_environment(activity)
             (log_path, log_file) = open_log_file(activity)
@@ -279,11 +282,11 @@ class ActivityCreationHandler(gobject.GObject):
 
     def _create_reply_handler(self):
         logging.debug("Activity created %s (%s)." %
-            (self._handle.activity_id, self._service_name))
+            (self._handle.activity_id, self._bundle_id))
 
     def _create_error_handler(self, err):
         logging.error("Couldn't create activity %s (%s): %s" %
-            (self._handle.activity_id, self._service_name, err))
+            (self._handle.activity_id, self._bundle_id, err))
         self._shell.NotifyLaunchFailure(
             self._handle.activity_id, reply_handler=self._no_reply_handler,
             error_handler=self._notify_launch_failure_error_handler)
@@ -299,18 +302,18 @@ class ActivityCreationHandler(gobject.GObject):
         logging.error("Datastore find failed %s" % err)
         self._create_activity()
 
-def create(service_name, activity_handle=None):
-    """Create a new activity from its name."""
+def create(bundle_id, version, activity_handle=None):
+    """Create a new activity from its bundle id."""
     if not activity_handle:
         activity_handle = ActivityHandle()
-    return ActivityCreationHandler(service_name, activity_handle)
+    return ActivityCreationHandler(bundle_id, version, activity_handle)
 
-def create_with_uri(service_name, uri):
+def create_with_uri(bundle_id, version, uri):
     """Create a new activity and pass the uri as handle."""
     activity_handle = ActivityHandle(uri=uri)
-    return ActivityCreationHandler(service_name, activity_handle)
+    return ActivityCreationHandler(bundle_id, version, activity_handle)
 
-def create_with_object_id(service_name, object_id):
+def create_with_object_id(bundle_id, version, object_id):
     """Create a new activity and pass the object id as handle."""
     activity_handle = ActivityHandle(object_id=object_id)
-    return ActivityCreationHandler(service_name, activity_handle)
+    return ActivityCreationHandler(bundle_id, version, activity_handle)
diff --git a/sugar/activity/registry.py b/sugar/activity/registry.py
index e327cf0..f3a3129 100644
--- a/sugar/activity/registry.py
+++ b/sugar/activity/registry.py
@@ -72,8 +72,8 @@ class ActivityRegistry(gobject.GObject):
         self._registry.connect_to_signal('ActivityRemoved', self._activity_removed_cb)
         self._registry.connect_to_signal('ActivityChanged', self._activity_changed_cb)
 
-        # Two caches fo saving some travel across dbus.
-        self._service_name_to_activity_info = {}
+        # Two caches for saving some travel across dbus.
+        self._activity_cache = {}
         self._mime_type_to_activities = {}
 
     def _convert_info_list(self, info_list):
@@ -111,14 +111,14 @@ class ActivityRegistry(gobject.GObject):
              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]
+    def get_activity(self, bundle_id, version):
+        if self._activity_cache.has_key((bundle_id, version)):
+            return self._activity_cache[(bundle_id, version)]
 
-        info_dict = self._registry.GetActivity(service_name)
+        info_dict = self._registry.GetActivity(bundle_id, version)
         activity_info = _activity_info_from_dict(info_dict)
 
-        self._service_name_to_activity_info[service_name] = activity_info
+        self._activity_cache[(bundle_id, version)] = activity_info
         return activity_info
 
     def find_activity(self, name):
@@ -148,7 +148,7 @@ class ActivityRegistry(gobject.GObject):
         self.emit('activity-added', _activity_info_from_dict(info_dict))
 
     def _invalidate_cache(self):
-        self._service_name_to_activity_info.clear()
+        self._activity_cache.clear()
         self._mime_type_to_activities.clear()
 
     def remove_bundle(self, bundle_path):
diff --git a/sugar/clipboard/clipboardservice.py b/sugar/clipboard/clipboardservice.py
index d975330..dc123fd 100644
--- a/sugar/clipboard/clipboardservice.py
+++ b/sugar/clipboard/clipboardservice.py
@@ -23,8 +23,8 @@ NAME_KEY = 'NAME'
 PERCENT_KEY = 'PERCENT'
 ICON_KEY = 'ICON'
 PREVIEW_KEY = 'PREVIEW'
-ACTIVITIES_KEY = 'ACTIVITIES'
 FORMATS_KEY = 'FORMATS'
+MIME_TYPE_KEY = 'MIME_TYPE'
 
 TYPE_KEY = 'TYPE'
 DATA_KEY = 'DATA'
@@ -51,7 +51,7 @@ class ClipboardService(gobject.GObject):
         'object-deleted': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
                         ([str])),
         'object-state-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
-                        ([str, str, int, str, str, object])),
+                        ([str, str, int, str, str, str])),
     }
     
     def __init__(self):
@@ -125,13 +125,13 @@ class ClipboardService(gobject.GObject):
             percent 
             icon 
             preview
-            activities
+            mime_type
         
         From the ClipboardObject instance which is being described.
         """
         self.emit('object-state-changed', str(object_id), values[NAME_KEY],
                   values[PERCENT_KEY], values[ICON_KEY], values[PREVIEW_KEY],
-                  values[ACTIVITIES_KEY])
+                  values[MIME_TYPE_KEY])
 
     def add_object(self, name):
         """Add a new object to the path
@@ -145,11 +145,11 @@ class ClipboardService(gobject.GObject):
         """
         return str(self._dbus_service.add_object(name))
 
-    def add_object_format(self, object_id, formatType, data, on_disk):
+    def add_object_format(self, object_id, format_type, data, on_disk):
         """Annotate given object on the clipboard with new information
         
         object_id -- dbus path as returned from add_object
-        formatType -- XXX what should this be? mime type?
+        format_type -- XXX what should this be? mime type?
         data -- storage format for the clipped object?
         on_disk -- whether the data is on-disk (non-volatile) or in 
             memory (volatile)
@@ -160,7 +160,7 @@ class ClipboardService(gobject.GObject):
         returns None
         """
         self._dbus_service.add_object_format(dbus.ObjectPath(object_id),
-                formatType,
+                format_type,
                 data,
                 on_disk)
     
@@ -199,9 +199,8 @@ class ClipboardService(gobject.GObject):
             NAME_KEY: str,
             PERCENT_KEY: number,
             ICON_KEY: str,
-            PREVIEW_KEY: XXX what is it?,
-            ACTIVITIES_KEY: activities that can open this object,
-            FORMATS_KEY: list of XXX what is it?
+            PREVIEW_KEY: str,
+            MIME_TYPE_KEY: str
         """
         return self._dbus_service.get_object(dbus.ObjectPath(object_id),)
 
diff --git a/sugar/datastore/datastore.py b/sugar/datastore/datastore.py
index 334c866..38ef84c 100644
--- a/sugar/datastore/datastore.py
+++ b/sugar/datastore/datastore.py
@@ -25,6 +25,7 @@ import gobject
 from sugar.datastore import dbus_helpers
 from sugar import activity
 from sugar.activity.activityhandle import ActivityHandle
+from sugar.activity import activityfactory
 from sugar.bundle.contentbundle import ContentBundle
 from sugar.bundle.activitybundle import ActivityBundle
 from sugar.bundle.contentbundle import ContentBundle
@@ -130,7 +131,8 @@ class DSObject(object):
 
         bundle_id = self.metadata.get('activity', '')
         if bundle_id:
-            activity_info = activity.get_registry().get_activity(bundle_id)
+            # TODO: when the journal entries contain the bundle version, use it.
+            activity_info = activity.get_registry().get_activity(bundle_id, -1)
             if activity_info:
                 activities.append(activity_info)
 
@@ -151,11 +153,9 @@ class DSObject(object):
         return self.metadata['mime_type'] == ContentBundle.MIME_TYPE
 
     def is_bundle(self):
-        return self.is_activity_bundle() or self.is_content_bundle()
-
-    def resume(self, bundle_id=None):
-        from sugar.activity import activityfactory
+        return s22elf.is_activity_bundle() or self.is_content_bundle()
 
+    def resume(self, bundle_id=None, version=None):
         if self.is_activity_bundle():
             if bundle_id is not None:
                 raise ValueError('Object is a bundle, cannot be resumed as an activity.')
@@ -169,25 +169,31 @@ class DSObject(object):
                 logging.debug('Upgrading activity bundle')
                 bundle.upgrade()
 
-            logging.debug('activityfactory.creating bundle with id %r', bundle.get_bundle_id())
-            activityfactory.create(bundle.get_bundle_id())
+            activityfactory.create(bundle.get_bundle_id(),
+                                   bundle.get_activity_version())
         else:
             if not self.get_activities() and bundle_id is None:
                 logging.warning('No activity can open this object, %s.' %
                         self.metadata.get('mime_type', None))
                 return
+
             if bundle_id is None:
                 bundle_id = self.get_activities()[0].bundle_id
 
+            if version is None:
+                registry = activity.get_registry()
+                version = registry.get_activity(bundle_id, -1).version
+
             activity_id = self.metadata['activity_id']
             object_id = self.object_id
 
             if activity_id:
                 handle = ActivityHandle(object_id=object_id,
                                         activity_id=activity_id)
-                activityfactory.create(bundle_id, handle)
+                activityfactory.create(bundle_id, version, handle)
             else:
-                activityfactory.create_with_object_id(bundle_id, object_id)
+                activityfactory.create_with_object_id(bundle_id, version,
+                                                      object_id)
 
     def destroy(self):
         if self._destroyed:
-- 
1.5.2.5

_______________________________________________
Sugar mailing list
[email protected]
http://lists.laptop.org/listinfo/sugar

Reply via email to