Ticket #1496: pippy_saveexample.patch

File pippy_saveexample.patch, 5.7 KB (added by geirea, 14 years ago)
  • pippy_app.py

    diff --git a/pippy_app.py b/pippy_app.py
    index abaf397..7a9ea6f 100644
    a b class PippyActivity(ViewSourceActivity): 
    7777        menu_item.connect('activate', self.makebutton_cb)
    7878        palette.menu.append(menu_item)
    7979        menu_item.show()
     80        menu_item = MenuItem(_('As Pippy Example'))
     81        menu_item.set_image(Icon(file=('%s/activity/activity-icon.svg' % get_bundle_path()), icon_size=gtk.ICON_SIZE_MENU))
     82        menu_item.connect('activate', self.saveexamplebutton_cb)
     83        palette.menu.append(menu_item)
     84        menu_item.show()
     85
    8086        self.set_toolbox(toolbox)
    8187        toolbox.show()
    8288
    class PippyActivity(ViewSourceActivity): 
    8894        sidebar = gtk.VBox()
    8995        self.model = gtk.TreeStore(gobject.TYPE_PYOBJECT, gobject.TYPE_STRING)
    9096        treeview = gtk.TreeView(self.model)
     97
    9198        cellrenderer = gtk.CellRendererText()
    9299        treecolumn = gtk.TreeViewColumn(_("Examples"), cellrenderer, text=1)
    93100        treeview.get_selection().connect("changed", self.selection_cb)
    class PippyActivity(ViewSourceActivity): 
    117124                self.model.set_value(_iter, 0, entry)
    118125                self.model.set_value(_iter, 1, entry["name"])
    119126
     127        # now add local examples
     128        d = os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'],'data')
     129        direntry = { "name": _("My examples"),
     130                     "path": d + "/" }
     131        self.example_iter = self.model.insert_before(None, None)
     132        self.model.set_value(self.example_iter, 0, direntry)
     133        self.model.set_value(self.example_iter, 1, direntry["name"])
     134               
     135        for _file in sorted(os.listdir(d)):
     136            if _file.endswith('~'): continue # skip emacs backups
     137            entry = { "name": _(_file.capitalize()),
     138                      "path": os.path.join(d, _file) }
     139            _iter = self.model.insert_before(self.example_iter, None)
     140            self.model.set_value(_iter, 0, entry)
     141            self.model.set_value(_iter, 1, entry["name"])
     142
    120143        treeview.expand_all()
    121144
    122145        # Source buffer
    class PippyActivity(ViewSourceActivity): 
    217240        hbox.pack_end(vbox)
    218241        self.set_canvas(hbox)
    219242        self.show_all()
    220 
    221243       
    222244        self.hellotube = None
    223245
    class PippyActivity(ViewSourceActivity): 
    252274    def vte_drop_cb(self, widget, context, x, y, selection, targetType, time):
    253275        if targetType == TARGET_TYPE_TEXT:
    254276            self._vte.feed_child(selection.data)
     277
    255278    def selection_cb(self, column):
    256279        self.save()
    257280        model, _iter = column.get_selected()
    class PippyActivity(ViewSourceActivity): 
    287310                f.write(PYTHON_PREFIX)
    288311            for line in text:
    289312                f.write(line)
     313
    290314    def _reset_vte(self):
    291315        self._vte.grab_focus()
    292316        self._vte.feed("\x1B[H\x1B[J\x1B[0;39m")
    class PippyActivity(ViewSourceActivity): 
    366390            rmtree(app_temp, ignore_errors=True) # clean up!
    367391            raise
    368392
     393    def saveexamplebutton_cb(self, __):
     394        # get the name of this pippy program.
     395        title = self.metadata['title']
     396        if title == _('Pippy Activity'):
     397            from sugar.graphics.alert import Alert
     398            from sugar.graphics.icon import Icon
     399            alert = Alert()
     400            alert.props.title =_ ('Save as Example Error')
     401            alert.props.msg = _('Please give your activity a meaningful name before attempting to save it as an example.')
     402            ok_icon = Icon(icon_name='dialog-ok')
     403            alert.add_button(gtk.RESPONSE_OK, _('Ok'), ok_icon)
     404            alert.connect('response', self.dismiss_alert_cb)
     405            self.add_alert(alert)
     406            return
     407        self.stopbutton_cb(None) # try stopping old code first.
     408        self._reset_vte()
     409        self._vte.feed(_("Creating example..."))
     410        self._vte.feed("\r\n")
     411        local_data = os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'],'data')
     412        local_file = os.path.join(local_data,title)
     413        if os.path.exists(local_file):
     414            from sugar.graphics.alert import ConfirmationAlert
     415            alert = ConfirmationAlert()
     416            alert.props.title =_ ('Save as Example Warning')
     417            alert.props.msg = _('This example already exists. Do you want to overwrite it?')
     418            alert.connect('response', self.confirmation_alert_cb, local_file)
     419            self.add_alert(alert)
     420        else:
     421            self.write_file(local_file)
     422            self._reset_vte()
     423            self._vte.feed(_("Saved as example."))
     424            self._vte.feed("\r\n")
     425            self.add_to_example_list(local_file)
     426
    369427    def child_exited_cb(self, *args):
    370428        """Called whenever a child exits.  If there's a handler, run it."""
    371429        h, self._child_exited_handler = self._child_exited_handler, None
    class PippyActivity(ViewSourceActivity): 
    408466    def dismiss_alert_cb(self, alert, response_id):
    409467        self.remove_alert(alert)
    410468
     469    def confirmation_alert_cb(self, alert, response_id, local_file):
     470        self.remove_alert(alert)
     471        if response_id is gtk.RESPONSE_OK:
     472            self.write_file(local_file)
     473            self._reset_vte()
     474            self._vte.feed(_("Saved as example."))
     475            self._vte.feed("\r\n")
     476        else:
     477            self._reset_vte()
     478
     479    def add_to_example_list(self,local_file):
     480        entry = { "name": _(os.path.basename(local_file)),
     481                  "path": local_file }
     482        _iter = self.model.insert_before(self.example_iter, None)
     483        self.model.set_value(_iter, 0, entry)
     484        self.model.set_value(_iter, 1, entry["name"])
     485 
    411486    def write_file(self, file_path):
    412487        self.metadata['mime_type'] = 'text/x-python'
    413488        global text_buffer