[gnome-documents/gnome-3-22] documents: Let everybody provide their own download implementation



commit e7a1465fdcff00e3d62864822d9fe390c98e7377
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Mar 30 18:18:16 2017 +0200

    documents: Let everybody provide their own download implementation
    
    This is meant to reduce our reliance on GdPdfLoader for loading remote
    documents. We can open ODFs and OOXMLs in LOKDocView, so we don't need
    to convert everything to PDFs. In the future, remote sub-classes will
    provider their own implementation to download a document, and this will
    help unify the loading across the various sub-classes.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=774937

 src/documents.js |   42 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 42 insertions(+), 0 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 938554a..864ce9c 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -359,6 +359,48 @@ const DocCommon = new Lang.Class({
         }
     },
 
+    download: function(useCache, cancellable, callback) {
+        let localFile = Gio.File.new_for_uri(this.uriToLoad);
+        let localPath = localFile.get_path();
+        let localDir = GLib.path_get_dirname(localPath);
+        GLib.mkdir_with_parents(localDir, 448);
+
+        if (!useCache) {
+            this.downloadImpl(localFile, cancellable, callback);
+            return;
+        }
+
+        localFile.query_info_async(Gio.FILE_ATTRIBUTE_TIME_MODIFIED,
+                                   Gio.FileQueryInfoFlags.NONE,
+                                   GLib.PRIORITY_DEFAULT,
+                                   cancellable,
+                                   Lang.bind(this,
+            function(object, res) {
+                let info;
+
+                try {
+                    info = object.query_info_finish(res);
+                } catch (e) {
+                    this.downloadImpl(localFile, cancellable, callback);
+                    return;
+                }
+
+                let cacheMtime = info.get_attribute_uint64(Gio.FILE_ATTRIBUTE_TIME_MODIFIED);
+                cacheMtime /= 1000000;
+
+                if (this.mtime <= cacheMtime) {
+                    callback(true, null);
+                    return;
+                }
+
+                this.downloadImpl(localFile, cancellable, callback);
+            }));
+    },
+
+    downloadImpl: function(localFile, cancellable, callback) {
+        throw(new Error('DocCommon implementations must override downloadImpl'));
+    },
+
     load: function() {
         log('Error: DocCommon implementations must override load');
     },


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