[Pkg-sugar-commit] [sugar-browse-activity] 13/16: Get back preview in downloaded images - SL #1106

Jonas Smedegaard dr at jones.dk
Mon Apr 20 09:47:43 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag v149.2
in repository sugar-browse-activity.

commit 008cb9ea7c1c00551fbd97157fd7f5a81b7c16a6
Author: Manuel Quiñones <manuq at laptop.org>
Date:   Thu Apr 11 14:28:25 2013 -0300

    Get back preview in downloaded images - SL #1106
    
    This patch from Gonzalo for gtk2 was the main reference:
    http://bugs.sugarlabs.org/attachment/ticket/1106/0001-Fix-1106-No-preview-in-Journal-for-downloaded-image.patch
    
    Also borrows from sugar3.activity.Activity.get_preview to make the
    preview with cairo.  And uses GdkPixbuf to load any GdkPixbuf
    supported format.
    
    Signed-off-by: Manuel Quiñones <manuq at laptop.org>
---
 downloadmanager.py | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/downloadmanager.py b/downloadmanager.py
index d3f5874..1766819 100644
--- a/downloadmanager.py
+++ b/downloadmanager.py
@@ -20,9 +20,13 @@ import logging
 from gettext import gettext as _
 import tempfile
 import dbus
+import cairo
+import StringIO
 
 from gi.repository import Gtk
+from gi.repository import Gdk
 from gi.repository import WebKit
+from gi.repository import GdkPixbuf
 
 from sugar3.datastore import datastore
 from sugar3 import profile
@@ -181,6 +185,13 @@ class Download(object):
             sniffed_mime_type = mime.get_for_file(self._dest_path)
             self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
 
+            if sniffed_mime_type in ('image/bmp','image/gif','image/jpeg',
+                                     'image/png','image/tiff'):
+                preview = self._get_preview()
+                if preview is not None:
+                    self.dl_jobject.metadata['preview'] = \
+                        dbus.ByteArray(preview)
+
             datastore.write(self.dl_jobject,
                             transfer_ownership=True,
                             reply_handler=self.__internal_save_cb,
@@ -294,6 +305,39 @@ class Download(object):
             'Deleted', self.__datastore_deleted_cb,
             arg0=self.dl_jobject.object_id)
 
+    def _get_preview(self):
+        # This code borrows from sugar3.activity.Activity.get_preview
+        # to make the preview with cairo, and also uses GdkPixbuf to
+        # load any GdkPixbuf supported format.
+        pixbuf = GdkPixbuf.Pixbuf.new_from_file(self._dest_path)
+        image_width = pixbuf.get_width()
+        image_height = pixbuf.get_height()
+
+        preview_width, preview_height = activity.PREVIEW_SIZE
+        preview_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
+                                             preview_width, preview_height)
+        cr = cairo.Context(preview_surface)
+
+        scale_w = preview_width * 1.0 / image_width
+        scale_h = preview_height * 1.0 / image_height
+        scale = min(scale_w, scale_h)
+
+        translate_x = int((preview_width - (image_width * scale)) / 2)
+        translate_y = int((preview_height - (image_height * scale)) / 2)
+
+        cr.translate(translate_x, translate_y)
+        cr.scale(scale, scale)
+
+        cr.set_source_rgba(1, 1, 1, 0)
+        cr.set_operator(cairo.OPERATOR_SOURCE)
+        cr.paint()
+        Gdk.cairo_set_source_pixbuf(cr, pixbuf, 0, 0)
+        cr.paint()
+
+        preview_str = StringIO.StringIO()
+        preview_surface.write_to_png(preview_str)
+        return preview_str.getvalue()
+
     def __datastore_deleted_cb(self, uid):
         logging.debug('Downloaded entry has been deleted' \
                           ' from the datastore: %r', uid)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-sugar/sugar-browse-activity.git



More information about the pkg-sugar-commit mailing list