Ticket #1106: 0001-Fix-1106-No-preview-in-Journal-for-downloaded-image.patch

File 0001-Fix-1106-No-preview-in-Journal-for-downloaded-image.patch, 2.9 KB (added by godiard, 14 years ago)
  • downloadmanager.py

    From 45e053104de9671c06a732a8e4dc172db2df8a56 Mon Sep 17 00:00:00 2001
    From: Gonzalo Odiard <godiard@gmail.com>
    Date: Mon, 16 Aug 2010 18:48:20 -0300
    Subject: [PATCH] Fix #1106 No preview in Journal for downloaded image
    
    ---
     downloadmanager.py |   35 +++++++++++++++++++++++++++++++++++
     1 files changed, 35 insertions(+), 0 deletions(-)
    
    diff --git a/downloadmanager.py b/downloadmanager.py
    index 3eec649..0538049 100644
    a b from sugar import profile 
    3636from sugar import mime
    3737from sugar.graphics.alert import Alert, TimeoutAlert
    3838from sugar.graphics.icon import Icon
     39from sugar.graphics import style
    3940from sugar.activity import activity
    4041
    4142# #3903 - this constant can be removed and assumed to be 1 when dbus-python
    class Download: 
    192193                sniffed_mime_type = mime.get_for_file(self._target_file.path)
    193194                self.dl_jobject.metadata['mime_type'] = sniffed_mime_type
    194195
     196            if self._mime_type in ('image/bmp','image/gif','image/jpeg','image/png','image/tiff'):
     197                self.dl_jobject.metadata['preview'] = self.__get_preview_image()
     198            else:
     199                self.dl_jobject.metadata['preview'] = ''
     200
    195201            datastore.write(self.dl_jobject,
    196202                            transfer_ownership=True,
    197203                            reply_handler=self._internal_save_cb,
    198204                            error_handler=self._internal_save_error_cb,
    199205                            timeout=360 * DBUS_PYTHON_TIMEOUT_UNITS_PER_SECOND)
    200206
     207    def __get_preview_image(self):
     208        pixbuf = gtk.gdk.pixbuf_new_from_file(self._target_file.path)
     209        width, height = pixbuf.get_width(), pixbuf.get_height()
     210
     211        preview_width = style.zoom(300)
     212        preview_height =  style.zoom(225)
     213
     214        if (width > preview_width) or (height > preview_height):
     215            scale_x = float(width) / preview_width
     216            scale_y = float(height) / preview_height
     217            scale = max(scale_x,scale_y)
     218       
     219            pixbuf = pixbuf.scale_simple(float(width) / scale, height / scale,
     220                                     gtk.gdk.INTERP_BILINEAR)
     221        pixbuf2 = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB,pixbuf.get_has_alpha(),8,preview_width,preview_height)
     222        pixbuf2.fill(0xffffffff)
     223        margin_x = (preview_width - pixbuf.get_width()) / 2
     224        margin_y = (preview_height - pixbuf.get_height()) / 2
     225
     226        pixbuf.copy_area(0,0,pixbuf.get_width(),pixbuf.get_height(),pixbuf2,margin_x,margin_y)
     227
     228        preview_data = []
     229        def save_func(buf, data):
     230            data.append(buf)
     231
     232        pixbuf2.save_to_callback(save_func, 'png', user_data=preview_data)
     233        preview_data = ''.join(preview_data)
     234        return dbus.ByteArray(preview_data)
     235
    201236    def __start_response_cb(self, alert, response_id):
    202237        global _active_downloads
    203238        if response_id is gtk.RESPONSE_CANCEL: