From f0e18ba785a3d57af54bb6f6a7e9993dee6e72fe Mon Sep 17 00:00:00 2001 From: Tomeu Vizoso Date: Fri, 15 Jun 2007 18:03:17 +0200 Subject: [PATCH] Support for previews in the journal. --- shell/view/Shell.py | 6 +++--- sugar/activity/activity.py | 23 ++++++++++++++++++++++- sugar/datastore/datastore.py | 2 +- sugar/graphics/window.py | 13 +++++++++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/shell/view/Shell.py b/shell/view/Shell.py index 626f8a2a..ab13269f 100644 --- a/shell/view/Shell.py +++ b/shell/view/Shell.py @@ -214,13 +214,13 @@ class Shell(gobject.GObject): file_path = os.path.join(tempfile.gettempdir(), '%i' % time.time()) window = gtk.gdk.get_default_root_window() - width, height = window.get_size(); - x_orig, y_orig = window.get_origin(); + width, height = window.get_size() + x_orig, y_orig = window.get_origin() screenshot = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=False, bits_per_sample=8, width=width, height=height) screenshot.get_from_drawable(window, window.get_colormap(), x_orig, y_orig, 0, 0, - width, height); + width, height) screenshot.save(file_path, "png") jobject = datastore.create() diff --git a/sugar/activity/activity.py b/sugar/activity/activity.py index b72b0b14..2e7641ca 100644 --- a/sugar/activity/activity.py +++ b/sugar/activity/activity.py @@ -27,9 +27,11 @@ import time import tempfile import gtk, gobject - +import dbus + from sugar.presence import presenceservice from sugar.activity.activityservice import ActivityService +from sugar.graphics import units from sugar.graphics.window import Window from sugar.graphics.toolbox import Toolbox from sugar.graphics.toolbutton import ToolButton @@ -276,6 +278,25 @@ class Activity(Window, gtk.Container): def save(self): """Request that the activity is saved to the Journal.""" + preview_pixbuf = self.get_canvas_screenshot() + preview_pixbuf = preview_pixbuf.scale_simple(units.grid_to_pixels(4), + units.grid_to_pixels(4), + gtk.gdk.INTERP_BILINEAR) + + # TODO: Find a way of taking a png out of the pixbuf without saving to a temp file. + fd, file_path = tempfile.mkstemp('.png') + del fd + preview_pixbuf.save(file_path, 'png') + f = open(file_path) + try: + preview_data = f.read() + finally: + f.close() + os.remove(file_path) + + # TODO: Take this out when the datastore accepts binary data. + import base64 + self.metadata['preview'] = base64.b64encode(preview_data) try: file_path = os.path.join(tempfile.gettempdir(), '%i' % time.time()) self.write_file(file_path) diff --git a/sugar/datastore/datastore.py b/sugar/datastore/datastore.py index 4cac40be..4b8b9030 100644 --- a/sugar/datastore/datastore.py +++ b/sugar/datastore/datastore.py @@ -96,7 +96,7 @@ def create(): return DSObject(object_id=None, metadata=DSMetadata(), file_path=None) def write(ds_object, reply_handler=None, error_handler=None): - logging.debug('datastore.write: %r' % ds_object.metadata.get_dictionary()) + logging.debug('datastore.write') if ds_object.object_id: dbus_helpers.update(ds_object.object_id, ds_object.metadata.get_dictionary(), diff --git a/sugar/graphics/window.py b/sugar/graphics/window.py index d2afaf81..28fdb357 100644 --- a/sugar/graphics/window.py +++ b/sugar/graphics/window.py @@ -53,3 +53,16 @@ class Window(gtk.Window): group = gtk.Window() group.realize() window.window.set_group(group.window) + + def get_canvas_screenshot(self): + if not self.canvas: + return None + + window = self.canvas.window + width, height = window.get_size() + + screenshot = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, has_alpha=False, + bits_per_sample=8, width=width, height=height) + screenshot.get_from_drawable(window, window.get_colormap(), 0, 0, 0, 0, + width, height) + return screenshot