[gnome-documents/gnome-3-8] documents: Show thumbnails for Google Documents



commit 128e8ba90167bcad3785bca78be0adeab132fea0
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Jun 6 14:20:11 2013 +0200

    documents: Show thumbnails for Google Documents
    
    Create a new createThumbnail virtual method, which is implemented by
    LocalDocument and GoogleDocument for actually creating the thumbnail.
    While LocalDocument calls out to GnomeDesktopThumbnailFactory, for
    GoogleDocument the thumbnail is downloaded using GData and stored in
    the appropriate location.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=698315

 src/documents.js |   72 ++++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 64 insertions(+), 8 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index c2a6861..30dbcba 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -357,6 +357,10 @@ const DocCommon = new Lang.Class({
         }
     },
 
+    createThumbnail: function(callback) {
+        log('Error: DocCommon implementations must override createThumbnail');
+    },
+
     refreshIcon: function() {
         if (this._thumbPath) {
             this._refreshThumbPath();
@@ -396,16 +400,11 @@ const DocCommon = new Lang.Class({
             this._refreshThumbPath();
         } else {
             this.thumbnailed = false;
-
-            // try to create the thumbnail
-            GdPrivate.queue_thumbnail_job_for_file_async(this._file,
-                                                         Lang.bind(this, this._onQueueThumbnailJob));
+            this.createThumbnail(Lang.bind(this, this._onCreateThumbnail));
         }
     },
 
-    _onQueueThumbnailJob: function(object, res) {
-        let thumbnailed = GdPrivate.queue_thumbnail_job_for_file_finish(res);
-
+    _onCreateThumbnail: function(thumbnailed) {
         if (!thumbnailed) {
             this._failedThumbnailing = true;
             return;
@@ -609,6 +608,14 @@ const LocalDocument = new Lang.Class({
         }
     },
 
+    createThumbnail: function(callback) {
+        GdPrivate.queue_thumbnail_job_for_file_async(this._file, Lang.bind(this,
+            function(object, res) {
+                let thumbnailed = GdPrivate.queue_thumbnail_job_for_file_finish(res);
+                callback(thumbnailed);
+            }));
+    },
+
     updateTypeDescription: function() {
         if (this.mimeType)
             this.typeDescription = Gio.content_type_get_description(this.mimeType);
@@ -646,7 +653,7 @@ const GoogleDocument = new Lang.Class({
     Extends: DocCommon,
 
     _init: function(cursor) {
-        this._failedThumbnailing = true;
+        this._failedThumbnailing = false;
 
         this.parent(cursor);
 
@@ -712,6 +719,55 @@ const GoogleDocument = new Lang.Class({
             }));
     },
 
+    createThumbnail: function(callback) {
+        this._createGDataEntry(null, Lang.bind(this,
+            function(entry, service, exception) {
+                if (exception) {
+                    callback(false);
+                    return;
+                }
+
+                let uri = entry.get_thumbnail_uri();
+                let authorizationDomain = service.get_primary_authorization_domain();
+                let inputStream = new GData.DownloadStream({ service: service,
+                                                             authorization_domain: authorizationDomain,
+                                                             download_uri: uri });
+
+                let checksum = new GLib.Checksum(GLib.ChecksumType.MD5);
+                checksum.update(this.uri, -1);
+                let basename = checksum.get_string() + '.png';
+                let path = GLib.build_filenamev([GLib.get_user_cache_dir(), "thumbnails", "normal", 
basename]);
+
+                let downloadFile = Gio.File.new_for_path(path);
+                downloadFile.replace_async
+                    (null, false, Gio.FileCreateFlags.PRIVATE, GLib.PRIORITY_DEFAULT, null, Lang.bind(this,
+                        function(source, res) {
+                            let outputStream;
+
+                            try {
+                                outputStream = downloadFile.replace_finish(res);
+                            } catch (e) {
+                                callback(false);
+                                return;
+                            }
+
+                            outputStream.splice_async(inputStream,
+                                Gio.OutputStreamSpliceFlags.CLOSE_SOURCE | 
Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
+                                GLib.PRIORITY_DEFAULT, null, Lang.bind(this,
+                                    function(source, res) {
+                                        try {
+                                            outputStream.splice_finish(res);
+                                        } catch (e) {
+                                            callback(false);
+                                            return;
+                                        }
+
+                                        callback(true);
+                                    }));
+                        }));
+            }));
+    },
+
     updateTypeDescription: function() {
         let description;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]