From f432801ebcd9f8587a55d81a4839190c16b517a9 Mon Sep 17 00:00:00 2001
From: Debarshi Ray <debarshir@gnome.org>
Date: Wed, 5 Apr 2017 17:17:59 +0200
Subject: [PATCH 2/2] documents: Implement downloadImpl for GoogleDocument

This will let us use the new JavaScript-based download and caching
implementation and reduce our reliance on GdPdfLoader.

https://bugzilla.gnome.org/show_bug.cgi?id=781032
---
 src/documents.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/src/documents.js b/src/documents.js
index 93c8e8fabb83..399b62d1bdf4 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -930,6 +930,59 @@ const GoogleDocument = new Lang.Class({
                  }));
     },
 
+    downloadImpl: function(localFile, cancellable, callback) {
+        this.createGDataEntry(cancellable, Lang.bind(this,
+            function(entry, service, error) {
+                if (error) {
+                    callback(false, error);
+                    return;
+                }
+
+                let inputStream;
+
+                try {
+                    inputStream = entry.download(service, "pdf", cancellable);
+                } catch(e) {
+                    callback(false, e);
+                    return;
+                }
+
+                localFile.replace_async(null,
+                                        false,
+                                        Gio.FileCreateFlags.PRIVATE,
+                                        GLib.PRIORITY_DEFAULT,
+                                        cancellable,
+                                        Lang.bind(this,
+                    function(object, res) {
+                        let outputStream;
+
+                        try {
+                            outputStream = object.replace_finish(res);
+                        } catch (e) {
+                            callback(false, e);
+                            return;
+                        }
+
+                        outputStream.splice_async(inputStream,
+                                                  Gio.OutputStreamSpliceFlags.CLOSE_SOURCE |
+                                                  Gio.OutputStreamSpliceFlags.CLOSE_TARGET,
+                                                  GLib.PRIORITY_DEFAULT,
+                                                  cancellable,
+                                                  Lang.bind(this,
+                            function(object, res) {
+                                try {
+                                    object.splice_finish(res);
+                                } catch (e) {
+                                    callback(false, e);
+                                    return;
+                                }
+
+                                callback(false, null);
+                            }));
+                    }));
+            }))
+    },
+
     load: function(passwd, cancellable, callback) {
         this.createGDataEntry(cancellable, Lang.bind(this,
             function(entry, service, exception) {
-- 
2.9.3

