From 259bd3d0e475eb1996e6aca2a726ee9f6ad63846 Mon Sep 17 00:00:00 2001 From: C. Scott Ananian Date: Tue, 18 Dec 2007 20:09:54 +0000 Subject: Make the 'view source' key work in Pippy and generated activities. Also register Pippy as a handler for text/x-python. --- (limited to 'activity.py') diff --git a/activity.py b/activity.py index 67da456..6f6c9d1 100644 --- a/activity.py +++ b/activity.py @@ -1,17 +1,56 @@ from sugar.activity import activity -import os, sys -import gtk, pango, vte +class ViewSourceActivity(activity.Activity): + """Activity subclass which handles the 'view source' key.""" + def __init__(self, handle): + super(ViewSourceActivity, self).__init__(handle) + self.__source_object_id = None # XXX: persist this across invocations? + self.connect('key-press-event', self._key_press_cb) + def _key_press_cb(self, widget, event): + import gtk + if gtk.gdk.keyval_name(event.keyval) == 'XF86Start': + self.view_source() + return True + return False + def view_source(self): + """Implement the 'view source' key by saving pippy_app.py to the + datastore, and then telling the Journal to view it.""" + if self.__source_object_id is None: + from sugar import profile + from sugar.datastore import datastore + from sugar.activity.activity import get_bundle_name, get_bundle_path + from gettext import gettext as _ + import os.path + jobject = datastore.create() + metadata = { + 'title': _('%s Source') % get_bundle_name(), + 'title_set_by_user': '1', + 'suggested_filename': 'pippy_app.py', + 'icon-color': profile.get_color().to_string(), + 'mime_type': 'text/x-python', + } + for k,v in metadata.items(): + jobject.metadata[k] = v # dict.update method is missing =( + jobject.file_path = os.path.join(get_bundle_path(), 'pippy_app.py') + datastore.write(jobject) + self.__source_object_id = jobject.object_id + jobject.destroy() + self.journal_show_object(self.__source_object_id) + def journal_show_object(self, object_id): + """Invoke parent class' journal_show_object if it exists.""" + s = super(ViewSourceActivity, self) + if hasattr(s, 'journal_show_object'): + s.journal_show_object(object_id) + -class VteActivity(activity.Activity): +class VteActivity(ViewSourceActivity): def __init__(self, handle): - activity.Activity.__init__(self, handle) + import gtk, pango, vte + super(VteActivity, self).__init__(handle) toolbox = activity.ActivityToolbox(self) self.set_toolbox(toolbox) toolbox.show() - # XXX: NEED SHOW SOURCE BUTTON / KEYBINDING - # creates vte widget self._vte = vte.Terminal() self._vte.set_size(30,5) -- cgit v0.9.1