From be37f671f65d035c34bb4db29f4849b533b87688 Mon Sep 17 00:00:00 2001 From: Wade Brainerd Date: Wed, 16 Sep 2009 03:04:35 +0000 Subject: Use sugar-port chooser. --- diff --git a/editlessonlistscreen.py b/editlessonlistscreen.py index 144c4f9..4388f24 100644 --- a/editlessonlistscreen.py +++ b/editlessonlistscreen.py @@ -20,6 +20,7 @@ import logging, os, math, time, copy, locale, datetime, random, re from gettext import gettext as _ from port import json +from port import chooser # Import PyGTK. import gobject, pygtk, gtk, pango @@ -27,7 +28,6 @@ import gobject, pygtk, gtk, pango # Import Sugar UI modules. import sugar.activity.activity import sugar.graphics.style -import sugar.graphics.objectchooser import sugar.mime import sugar.datastore.datastore @@ -255,36 +255,24 @@ class EditLessonListScreen(gtk.VBox): def import_clicked_cb(self, btn): - chooser = sugar.graphics.objectchooser.ObjectChooser( - None, self, None, - what_filter='text/x-typing-turtle-lessons') + jobject = chooser.pick(None, None, self, 'text/x-typing-turtle-lessons') - try: - result = chooser.run() + if jobject and jobject.file_path: + fd = open(jobject.file_path, 'r') - if result == gtk.RESPONSE_ACCEPT: - jobject = chooser.get_selected_object() + try: + data = json.loads(fd.read()) - if jobject and jobject.file_path: - fd = open(jobject.file_path, 'r') - - try: - data = json.loads(fd.read()) - - # Replace lessons without destroying the object. - while len(self.lessons): - self.lessons.pop() - for l in data['lessons']: - self.lessons.append(l) - self.build() - - finally: - fd.close() - - finally: - chooser.destroy() - del chooser - + # Replace lessons without destroying the object. + while len(self.lessons): + self.lessons.pop() + for l in data['lessons']: + self.lessons.append(l) + self.build() + + finally: + fd.close() + def export_clicked_cb(self, btn): # Create the new journal entry fileObject = sugar.datastore.datastore.create() @@ -309,4 +297,4 @@ class EditLessonListScreen(gtk.VBox): fileObject.destroy() del fileObject - \ No newline at end of file + \ No newline at end of file diff --git a/port/chooser.py b/port/chooser.py new file mode 100644 index 0000000..e2df259 --- /dev/null +++ b/port/chooser.py @@ -0,0 +1,66 @@ +# 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 + +"""Object chooser method""" + +import gtk +import logging + +from sugar import mime +from sugar.graphics.objectchooser import ObjectChooser + +TEXT = hasattr(mime, 'GENERIC_TYPE_TEXT') and mime.GENERIC_TYPE_TEXT or None +IMAGE = hasattr(mime, 'GENERIC_TYPE_IMAGE') and mime.GENERIC_TYPE_IMAGE or None +AUDIO = hasattr(mime, 'GENERIC_TYPE_AUDIO') and mime.GENERIC_TYPE_AUDIO or None +VIDEO = hasattr(mime, 'GENERIC_TYPE_VIDEO') and mime.GENERIC_TYPE_VIDEO or None +LINK = hasattr(mime, 'GENERIC_TYPE_LINK') and mime.GENERIC_TYPE_LINK or None + +def pick(cb=None, default=None, parent=None, what=None): + """ + Opens object chooser. + + Method returns: + + * cb(jobject), if object was choosen and cb is not None + * jobject, if object was choosen and cb is None + * default, otherwise + + NOTE: 'what' makes sense only for sugar >= 0.84 + """ + what = what and {'what_filter': what} or {} + chooser = ObjectChooser(parent=parent, **what) + + jobject = None + out = None + + try: + if chooser.run() == gtk.RESPONSE_ACCEPT: + jobject = chooser.get_selected_object() + logging.debug('ObjectChooser: %r' % jobject) + + if jobject and jobject.file_path: + if cb: + out = cb(jobject) + else: + out = jobject + finally: + if jobject and id(jobject) != id(out): + jobject.destroy() + chooser.destroy() + del chooser + + if out: + return out + else: + return default -- cgit v0.9.1