[Dextrose] [PATCH] Downgrading activities not allowed. (SL #2164)

Martin Abente martin.abente.lahaye at gmail.com
Fri Dec 10 09:56:39 EST 2010


From: shanjit at seeta.in <shanjit at seeta.in>

Activity can be downgraded on the availability of an older .xo version of an
activity. An alert pops up when trying to install an older .xo file of an
activity, which asks the user to make a selection on whether to move to an
older activity version or not.

Co-authored-by: Shanjit Singh Jajmann <shanjit at seeta.in>
Co-authored-by: Anubhav Aggarwal <anubhav at seeta.in>
Dextrose backport by: Martin Abente <tch at activitycentral.com>
---
 src/jarabe/journal/Makefile.am        |    1 +
 src/jarabe/journal/journalactivity.py |    5 ++-
 src/jarabe/journal/journalwindow.py   |   34 +++++++++++++++++++++++
 src/jarabe/journal/misc.py            |   47 ++++++++++++++++++++++++++------
 src/jarabe/model/bundleregistry.py    |    7 +++-
 5 files changed, 81 insertions(+), 13 deletions(-)
 create mode 100644 src/jarabe/journal/journalwindow.py

diff --git a/src/jarabe/journal/Makefile.am b/src/jarabe/journal/Makefile.am
index a760869..f24dcfe 100644
--- a/src/jarabe/journal/Makefile.am
+++ b/src/jarabe/journal/Makefile.am
@@ -6,6 +6,7 @@ sugar_PYTHON =				\
 	journalactivity.py		\
 	journalentrybundle.py		\
 	journaltoolbox.py		\
+	journalwindow.py		\
 	keepicon.py			\
 	listmodel.py			\
 	listview.py			\
diff --git a/src/jarabe/journal/journalactivity.py b/src/jarabe/journal/journalactivity.py
index eab292b..52a677e 100644
--- a/src/jarabe/journal/journalactivity.py
+++ b/src/jarabe/journal/journalactivity.py
@@ -45,6 +45,7 @@ from jarabe.journal.journalentrybundle import JournalEntryBundle
 from jarabe.journal.objectchooser import ObjectChooser
 from jarabe.journal.modalalert import ModalAlert
 from jarabe.journal import model
+from jarabe.journal.journalwindow import JournalWindow
 
 J_DBUS_SERVICE = 'org.laptop.Journal'
 J_DBUS_INTERFACE = 'org.laptop.Journal'
@@ -103,10 +104,10 @@ class JournalActivityDBusService(dbus.service.Object):
     def ObjectChooserCancelled(self, chooser_id):
         pass
 
-class JournalActivity(Window):
+class JournalActivity(JournalWindow):
     def __init__(self):
         logging.debug("STARTUP: Loading the journal")
-        Window.__init__(self)
+        JournalWindow.__init__(self)
 
         self.set_title(_('Journal'))
 
diff --git a/src/jarabe/journal/journalwindow.py b/src/jarabe/journal/journalwindow.py
new file mode 100644
index 0000000..3c718c2
--- /dev/null
+++ b/src/jarabe/journal/journalwindow.py
@@ -0,0 +1,34 @@
+#Copyright (C) 2010 Software for Education, Entertainment and Training
+#Activities
+#
+# 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 gtk
+from sugar.graphics.window import Window
+
+_journal_window = None
+
+
+class JournalWindow(Window):
+
+    def __init__(self):
+
+        global _journal_window
+        Window.__init__(self)
+        _journal_window = self
+
+
+def get_journal_window():
+    return _journal_window
diff --git a/src/jarabe/journal/misc.py b/src/jarabe/journal/misc.py
index 6e3cb95..ec1f53d 100644
--- a/src/jarabe/journal/misc.py
+++ b/src/jarabe/journal/misc.py
@@ -28,8 +28,10 @@ from sugar.activity import activityfactory
 from sugar.activity.activityhandle import ActivityHandle
 from sugar.graphics.icon import get_icon_file_name
 from sugar.graphics.xocolor import XoColor
+from sugar.graphics.alert import ConfirmationAlert
 from sugar import mime
 from sugar.bundle.activitybundle import ActivityBundle
+from sugar.bundle.bundle import AlreadyInstalledException
 from sugar.bundle.contentbundle import ContentBundle
 from sugar import util
 
@@ -37,6 +39,7 @@ from jarabe.view import launcher
 from jarabe.model import bundleregistry, shell
 from jarabe.journal.journalentrybundle import JournalEntryBundle
 from jarabe.journal import model
+from jarabe.journal import journalwindow
 
 def _get_icon_for_mime(mime_type):
     generic_types = mime.get_all_generic_types()
@@ -160,19 +163,16 @@ def resume(metadata, bundle_id=None):
         bundle = ActivityBundle(file_path)
         if not registry.is_installed(bundle):
             logging.debug('Installing activity bundle')
-            registry.install(bundle)
+            try:
+                registry.install(bundle)
+            except AlreadyInstalledException:
+                _downgrade_option_alert(bundle)
+                return
         else:
             logging.debug('Upgrading activity bundle')
             registry.upgrade(bundle)
 
-        logging.debug('activityfactory.creating bundle with id %r',
-                        bundle.get_bundle_id())
-        installed_bundle = registry.get_bundle(bundle.get_bundle_id())
-        if installed_bundle:
-            activityfactory.create(installed_bundle)
-        else:
-            logging.error('Bundle %r is not installed.',
-                          bundle.get_bundle_id())
+        _launch_bundle(bundle)
 
     elif is_content_bundle(metadata) and bundle_id is None:
 
@@ -229,6 +229,35 @@ def resume(metadata, bundle_id=None):
         else:
             activityfactory.create_with_object_id(bundle, object_id)
 
+def _launch_bundle(bundle):
+    registry = bundleregistry.get_registry()
+    logging.debug('activityfactory.creating bundle with id %r',
+                       bundle.get_bundle_id())
+    installed_bundle = registry.get_bundle(bundle.get_bundle_id())
+    if installed_bundle:
+        activityfactory.create(installed_bundle)
+    else:
+        logging.error('Bundle %r is not installed.',
+                    bundle.get_bundle_id())
+
+def _downgrade_option_alert(bundle):
+    alert = ConfirmationAlert()
+    alert.props.title = _('Older Version Of %s Activity') % (bundle.get_name())
+    alert.props.msg = _('Do you want to downgrade to version %s\
+    ') % (bundle.get_activity_version())
+    alert.connect('response', _downgrade_alert_response_cb, bundle)
+    journalwindow.get_journal_window().add_alert(alert)
+    alert.show()
+
+def _downgrade_alert_response_cb(alert, response_id, bundle):
+    if response_id is gtk.RESPONSE_OK:
+        journalwindow.get_journal_window().remove_alert(alert)
+        registry = bundleregistry.get_registry()
+        registry.install(bundle, force_downgrade=True)
+        _launch_bundle(bundle)
+    elif response_id is gtk.RESPONSE_CANCEL:
+        journalwindow.get_journal_window().remove_alert(alert)
+
 def is_activity_bundle(metadata):
     mime_type = metadata.get('mime_type', '')
     return mime_type == ActivityBundle.MIME_TYPE or \
diff --git a/src/jarabe/model/bundleregistry.py b/src/jarabe/model/bundleregistry.py
index 090dddc..f2c2a52 100644
--- a/src/jarabe/model/bundleregistry.py
+++ b/src/jarabe/model/bundleregistry.py
@@ -376,14 +376,17 @@ class BundleRegistry(gobject.GObject):
                 return True
         return False
 
-    def install(self, bundle, uid=None):
+    def install(self, bundle, uid=None, force_downgrade=False):
         activities_path = env.get_user_activities_path()
 
         for installed_bundle in self._bundles:
             if bundle.get_bundle_id() == installed_bundle.get_bundle_id() and \
                     bundle.get_activity_version() <= \
                         installed_bundle.get_activity_version():
-                raise AlreadyInstalledException
+                if not force_downgrade:
+                    raise AlreadyInstalledException
+                else:
+                    self.uninstall(installed_bundle, force=True)
             elif bundle.get_bundle_id() == installed_bundle.get_bundle_id():
                 self.uninstall(installed_bundle, force=True)
 
-- 
1.7.1



More information about the Dextrose mailing list