[gnome-documents/wip/rishi/onedrive-lo: 8/10] documents: Let everybody provide their own download implementation



commit 0af15b8961ff99d734019e592beee4fc5179d329
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 |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 7ad4412..37d8517 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -365,6 +365,69 @@ 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) {
+            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);
+                }));
+        } else {
+            this.downloadImpl(localFile, cancellable, callback);
+        }
+    },
+
+    downloadImpl: function(localFile, cancellable, callback) {
+        throw(new Error('DocCommon implementations must override downloadImpl'));
+    },
+
+    setCacheAttributes: function(localFile, cancellable, callback) {
+        let info = new Gio.FileInfo();
+        let cacheMtime = this.mtime * 1000000;
+        info.set_attribute_uint64(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, cacheMtime);
+        localFile.set_attributes_async(info,
+                                       Gio.FileQueryInfoFlags.NONE,
+                                       GLib.PRIORITY_DEFAULT,
+                                       cancellable,
+                                       Lang.bind(this,
+            function(object, res) {
+                try {
+                    object.set_attributes_finish(res);
+                } catch (e) {
+                    logError(e,
+                             'Cannot set mtime on the cache file; cache will not be valid after the file has 
' +
+                             'been viewed.');
+                };
+
+                callback();
+            }));
+    },
+
     load: function() {
         log('Error: DocCommon implementations must override load');
     },


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