From 5edd70ac27c3091076c8dd396e9d59623acd58bd Mon Sep 17 00:00:00 2001
From: Walter Bender <walter.bender@gmail.com>
Date: Wed, 25 May 2011 17:59:36 -0400
Subject: [PATCH] added support for copying bundle; fixed bundle_name parsing
 error
Organization: Sugar Labs Foundation

---
 src/jarabe/view/viewsource.py |   69 +++++++++++++++++++++++++++++++++--------
 1 files changed, 56 insertions(+), 13 deletions(-)

diff --git a/src/jarabe/view/viewsource.py b/src/jarabe/view/viewsource.py
index a1c0be3..120b3c7 100644
--- a/src/jarabe/view/viewsource.py
+++ b/src/jarabe/view/viewsource.py
@@ -1,5 +1,6 @@
 # Copyright (C) 2008 One Laptop Per Child
 # Copyright (C) 2009 Tomeu Vizoso, Simon Schampijer
+# Copyright (C) 2011 Walter Bender
 #
 # 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
@@ -16,6 +17,7 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 import os
+import subprocess
 import logging
 from gettext import gettext as _
 
@@ -44,11 +46,13 @@ map_activity_to_window = {}
 
 
 def setup_view_source(activity):
+
     service = activity.get_service()
+
     if service is not None:
         try:
             service.HandleViewSource()
-            return
+            # return
         except dbus.DBusException, e:
             expected_exceptions = ['org.freedesktop.DBus.Error.UnknownMethod',
                     'org.freedesktop.DBus.Python.NotImplementedError']
@@ -82,7 +86,7 @@ def setup_view_source(activity):
             logging.exception('Exception occured in GetDocumentPath():')
 
     if bundle_path is None and document_path is None:
-        _logger.debug('Activity without bundle_path nor document_path')
+        _logger.debug('Activity with neither bundle_path nor document_path')
         return
 
     view_source = ViewSource(window_xid, bundle_path, document_path,
@@ -97,8 +101,6 @@ class ViewSource(gtk.Window):
     def __init__(self, window_xid, bundle_path, document_path, title):
         gtk.Window.__init__(self)
 
-        logging.debug('ViewSource paths: %r %r', bundle_path, document_path)
-
         self.set_decorated(False)
         self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
         self.set_border_width(style.LINE_WIDTH)
@@ -132,9 +134,11 @@ class ViewSource(gtk.Window):
 
         activity_bundle = ActivityBundle(bundle_path)
         command = activity_bundle.get_command()
+        file_path = ''
         if len(command.split(' ')) > 1:
-            name = command.split(' ')[1].split('.')[0]
-            file_name = name + '.py'
+            name = command.split(' ')[1].split('.')[-1]
+            tmppath = command.split(' ')[1].replace('.', '/')
+            file_name = tmppath[0:-(len(name) + 1)] + '.py'
             path = os.path.join(activity_bundle.get_path(), file_name)
             self._selected_file = path
 
@@ -201,7 +205,7 @@ class ViewSource(gtk.Window):
 class DocumentButton(RadioToolButton):
     __gtype_name__ = 'SugarDocumentButton'
 
-    def __init__(self, file_name, document_path, title):
+    def __init__(self, file_name, document_path, title, bundle=False):
         RadioToolButton.__init__(self)
 
         self._document_path = document_path
@@ -218,15 +222,52 @@ class DocumentButton(RadioToolButton):
         self.set_icon_widget(icon)
         icon.show()
 
-        menu_item = MenuItem(_('Keep'))
-        icon = Icon(icon_name='document-save', icon_size=gtk.ICON_SIZE_MENU,
-                    xo_color=XoColor(self._color))
-        menu_item.set_image(icon)
+        if bundle:
+            menu_item = MenuItem(_('Copy'))
+            icon = Icon(icon_name='edit-copy', icon_size=gtk.ICON_SIZE_MENU,
+                        xo_color=XoColor(self._color))
+            menu_item.connect('activate', self.__copy_to_home)
+        else:
+            menu_item = MenuItem(_('Keep'))
+            icon = Icon(icon_name='document-save', icon_size=gtk.ICON_SIZE_MENU,
+                        xo_color=XoColor(self._color))
+            menu_item.connect('activate', self.__keep_in_journal_cb)
 
-        menu_item.connect('activate', self.__keep_in_journal_cb)
+        menu_item.set_image(icon)
         self.props.palette.menu.append(menu_item)
         menu_item.show()
 
+    def __copy_to_home(self, menu_item):
+        ''' Make a local copy of the activity bundle in
+        $HOME/Activities as MyActivity and change the bundle_id to
+        bundle_id_my_copy'''
+        # TODO: Check to see if MyActivity already exisits
+        home_activities = os.path.join(os.environ['HOME'], 'Activities')
+        new_bundle_name = 'My' + self._document_path.split('/')[-1]
+        command_line = ['cp', '-r', self._document_path,
+                        os.path.join(home_activities, new_bundle_name)]
+        _logger.debug(subprocess.call(command_line))
+
+        # Modify bundle_id in new activity.info file
+        fold = open(os.path.join(home_activities, new_bundle_name, 'activity',
+                                 'activity.info'), 'r')
+        fnew = open(os.path.join(home_activities, new_bundle_name, 'activity',
+                                 'new_activity.info'), 'w')
+        for line in fold:
+            tokens = line.split('=')
+            if tokens[0].rstrip() == 'bundle_id':
+                new_bundle_id = tokens[1].strip() + '_my_copy'
+                fnew.write('%s = %s\n' % (tokens[0].rstrip(), new_bundle_id))
+            else:
+                fnew.write(line)
+        fold.close
+        fnew.close
+        command_line = ['mv', os.path.join(home_activities, new_bundle_name,
+                                           'activity', 'new_activity.info'),
+                        os.path.join(home_activities, new_bundle_name,
+                                     'activity', 'activity.info')]
+        _logger.debug(subprocess.call(command_line))
+
     def __keep_in_journal_cb(self, menu_item):
         mime_type = mime.get_from_file_name(self._document_path)
         if mime_type == 'application/octet-stream':
@@ -283,7 +324,9 @@ class Toolbar(gtk.Toolbar):
             self._add_separator()
 
         if bundle_path is not None and os.path.exists(bundle_path):
-            activity_button = RadioToolButton()
+            # activity_button = RadioToolButton()
+            activity_button = DocumentButton(file_name, bundle_path, title,
+                                             bundle=True)
             icon = Icon(file=file_name,
                         icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR,
                         fill_color=style.COLOR_TRANSPARENT.get_svg(),
-- 
1.7.4.4

