Ticket #2859: 0001-added-support-for-copying-bundle-fixed-bundle_name-p.patch

File 0001-added-support-for-copying-bundle-fixed-bundle_name-p.patch, 6.5 KB (added by walter, 13 years ago)

add copy source to view source toolbar

  • src/jarabe/view/viewsource.py

    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 b  
    11# Copyright (C) 2008 One Laptop Per Child
    22# Copyright (C) 2009 Tomeu Vizoso, Simon Schampijer
     3# Copyright (C) 2011 Walter Bender
    34#
    45# This program is free software; you can redistribute it and/or modify
    56# it under the terms of the GNU General Public License as published by
     
    1617# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1718
    1819import os
     20import subprocess
    1921import logging
    2022from gettext import gettext as _
    2123
    map_activity_to_window = {} 
    4446
    4547
    4648def setup_view_source(activity):
     49
    4750    service = activity.get_service()
     51
    4852    if service is not None:
    4953        try:
    5054            service.HandleViewSource()
    51             return
     55            # return
    5256        except dbus.DBusException, e:
    5357            expected_exceptions = ['org.freedesktop.DBus.Error.UnknownMethod',
    5458                    'org.freedesktop.DBus.Python.NotImplementedError']
    def setup_view_source(activity): 
    8286            logging.exception('Exception occured in GetDocumentPath():')
    8387
    8488    if bundle_path is None and document_path is None:
    85         _logger.debug('Activity without bundle_path nor document_path')
     89        _logger.debug('Activity with neither bundle_path nor document_path')
    8690        return
    8791
    8892    view_source = ViewSource(window_xid, bundle_path, document_path,
    class ViewSource(gtk.Window): 
    97101    def __init__(self, window_xid, bundle_path, document_path, title):
    98102        gtk.Window.__init__(self)
    99103
    100         logging.debug('ViewSource paths: %r %r', bundle_path, document_path)
    101 
    102104        self.set_decorated(False)
    103105        self.set_position(gtk.WIN_POS_CENTER_ALWAYS)
    104106        self.set_border_width(style.LINE_WIDTH)
    class ViewSource(gtk.Window): 
    132134
    133135        activity_bundle = ActivityBundle(bundle_path)
    134136        command = activity_bundle.get_command()
     137        file_path = ''
    135138        if len(command.split(' ')) > 1:
    136             name = command.split(' ')[1].split('.')[0]
    137             file_name = name + '.py'
     139            name = command.split(' ')[1].split('.')[-1]
     140            tmppath = command.split(' ')[1].replace('.', '/')
     141            file_name = tmppath[0:-(len(name) + 1)] + '.py'
    138142            path = os.path.join(activity_bundle.get_path(), file_name)
    139143            self._selected_file = path
    140144
    class ViewSource(gtk.Window): 
    201205class DocumentButton(RadioToolButton):
    202206    __gtype_name__ = 'SugarDocumentButton'
    203207
    204     def __init__(self, file_name, document_path, title):
     208    def __init__(self, file_name, document_path, title, bundle=False):
    205209        RadioToolButton.__init__(self)
    206210
    207211        self._document_path = document_path
    class DocumentButton(RadioToolButton): 
    218222        self.set_icon_widget(icon)
    219223        icon.show()
    220224
    221         menu_item = MenuItem(_('Keep'))
    222         icon = Icon(icon_name='document-save', icon_size=gtk.ICON_SIZE_MENU,
    223                     xo_color=XoColor(self._color))
    224         menu_item.set_image(icon)
     225        if bundle:
     226            menu_item = MenuItem(_('Copy'))
     227            icon = Icon(icon_name='edit-copy', icon_size=gtk.ICON_SIZE_MENU,
     228                        xo_color=XoColor(self._color))
     229            menu_item.connect('activate', self.__copy_to_home)
     230        else:
     231            menu_item = MenuItem(_('Keep'))
     232            icon = Icon(icon_name='document-save', icon_size=gtk.ICON_SIZE_MENU,
     233                        xo_color=XoColor(self._color))
     234            menu_item.connect('activate', self.__keep_in_journal_cb)
    225235
    226         menu_item.connect('activate', self.__keep_in_journal_cb)
     236        menu_item.set_image(icon)
    227237        self.props.palette.menu.append(menu_item)
    228238        menu_item.show()
    229239
     240    def __copy_to_home(self, menu_item):
     241        ''' Make a local copy of the activity bundle in
     242        $HOME/Activities as MyActivity and change the bundle_id to
     243        bundle_id_my_copy'''
     244        # TODO: Check to see if MyActivity already exisits
     245        home_activities = os.path.join(os.environ['HOME'], 'Activities')
     246        new_bundle_name = 'My' + self._document_path.split('/')[-1]
     247        command_line = ['cp', '-r', self._document_path,
     248                        os.path.join(home_activities, new_bundle_name)]
     249        _logger.debug(subprocess.call(command_line))
     250
     251        # Modify bundle_id in new activity.info file
     252        fold = open(os.path.join(home_activities, new_bundle_name, 'activity',
     253                                 'activity.info'), 'r')
     254        fnew = open(os.path.join(home_activities, new_bundle_name, 'activity',
     255                                 'new_activity.info'), 'w')
     256        for line in fold:
     257            tokens = line.split('=')
     258            if tokens[0].rstrip() == 'bundle_id':
     259                new_bundle_id = tokens[1].strip() + '_my_copy'
     260                fnew.write('%s = %s\n' % (tokens[0].rstrip(), new_bundle_id))
     261            else:
     262                fnew.write(line)
     263        fold.close
     264        fnew.close
     265        command_line = ['mv', os.path.join(home_activities, new_bundle_name,
     266                                           'activity', 'new_activity.info'),
     267                        os.path.join(home_activities, new_bundle_name,
     268                                     'activity', 'activity.info')]
     269        _logger.debug(subprocess.call(command_line))
     270
    230271    def __keep_in_journal_cb(self, menu_item):
    231272        mime_type = mime.get_from_file_name(self._document_path)
    232273        if mime_type == 'application/octet-stream':
    class Toolbar(gtk.Toolbar): 
    283324            self._add_separator()
    284325
    285326        if bundle_path is not None and os.path.exists(bundle_path):
    286             activity_button = RadioToolButton()
     327            # activity_button = RadioToolButton()
     328            activity_button = DocumentButton(file_name, bundle_path, title,
     329                                             bundle=True)
    287330            icon = Icon(file=file_name,
    288331                        icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR,
    289332                        fill_color=style.COLOR_TRANSPARENT.get_svg(),