Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorManuel Quiñones <manuq@laptop.org>2013-04-11 17:28:25 (GMT)
committer Manuel Quiñones <manuq@laptop.org>2013-04-11 17:32:54 (GMT)
commit7e31cfa6371d18d9f1de02919644a7c26f6b141b (patch)
treeff3104645e58c8e7ff4139a05cbb59a27ead1e8a
parentfad672c7e2b4f38d8db88b36ba43dcdf6884e83b (diff)
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@laptop.org>
-rw-r--r--downloadmanager.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/downloadmanager.py b/downloadmanager.py
index 9e630ff..b2a4c35 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
@@ -186,6 +190,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,
@@ -299,6 +310,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)