[gnome-documents/rhel-7.4: 8/16] Optionally load the document when determining if it can be printed



commit dd1343e7869882d632d75307b396de0c3fde4447
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Apr 18 19:01:26 2017 +0200

    Optionally load the document when determining if it can be printed
    
    https://bugzilla.gnome.org/show_bug.cgi?id=781533

 src/documents.js  |   99 ++++++++++++++++++++++++++++++++++------------------
 src/evinceview.js |    8 ++++-
 2 files changed, 72 insertions(+), 35 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 4db7d07..fb0eee9 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -36,6 +36,7 @@ const Zpj = imports.gi.Zpj;
 const _ = imports.gettext.gettext;
 
 const Lang = imports.lang;
+const Mainloop = imports.mainloop;
 const Signals = imports.signals;
 
 const Application = imports.application;
@@ -416,11 +417,37 @@ const DocCommon = new Lang.Class({
         log('Error: DocCommon implementations must override canTrash');
     },
 
-    canPrint: function(docModel) {
-        if (!docModel)
-            return false;
+    canPrint: function(docModel, cancellable, callback) {
+        if (docModel) {
+            Mainloop.idle_add(Lang.bind(this,
+                function() {
+                    this._canPrint(docModel, callback);
+                    return GLib.SOURCE_REMOVE;
+                }));
+
+            return;
+        }
+
+        this.load(null, cancellable, Lang.bind(this,
+            function(doc, docModel, error) {
+                if (error) {
+                    callback(this, false);
+                    return;
+                }
+
+                this._canPrint(docModel, callback);
+            }));
+    },
+
+    _canPrint: function(docModel, callback) {
+        if (!docModel) {
+            callback(this, false);
+            return;
+        }
 
-        return EvView.PrintOperation.exists_for_document(docModel.get_document());
+        let evDoc = docModel.get_document();
+        let supported = EvView.PrintOperation.exists_for_document(evDoc);
+        callback(this, supported);
     },
 
     trash: function() {
@@ -694,41 +721,45 @@ const DocCommon = new Lang.Class({
                     return;
                 }
 
-                if (!this.canPrint(docModel))
-                    return;
+                this.canPrint(docModel, null, Lang.bind(this,
+                    function(doc, supported) {
+                        if (!supported)
+                            return;
 
-                let printOp = EvView.PrintOperation.new(docModel.get_document());
+                        let printOp = EvView.PrintOperation.new(docModel.get_document());
 
-                printOp.connect('begin-print', Lang.bind(this,
-                    function() {
-                        Application.selectionController.setSelectionMode(false);
-                    }));
 
-                printOp.connect('done', Lang.bind(this,
-                    function(op, res) {
-                        if (res == Gtk.PrintOperationResult.ERROR) {
-                            try {
-                                printOp.get_error();
-                            } catch (e) {
-                                let errorDialog = new Gtk.MessageDialog ({ transient_for: toplevel,
-                                                                           modal: true,
-                                                                           destroy_with_parent: true,
-                                                                           buttons: Gtk.ButtonsType.OK,
-                                                                           message_type: 
Gtk.MessageType.ERROR,
-                                                                           text: _("Failed to print 
document"),
-                                                                           secondary_text: e.message });
-                                errorDialog.connect ('response', Lang.bind(this,
-                                    function() {
-                                        errorDialog.destroy();
-                                    }));
-                                errorDialog.show();
-                            }
-                        }
-                    }));
+                        printOp.connect('begin-print', Lang.bind(this,
+                            function() {
+                                Application.selectionController.setSelectionMode(false);
+                            }));
 
-                let printNotification = new Notifications.PrintNotification(printOp, doc);
+                        printOp.connect('done', Lang.bind(this,
+                            function(op, res) {
+                                if (res == Gtk.PrintOperationResult.ERROR) {
+                                    try {
+                                        printOp.get_error();
+                                    } catch (e) {
+                                        let errorDialog = new Gtk.MessageDialog ({ transient_for: toplevel,
+                                                                                   modal: true,
+                                                                                   destroy_with_parent: true,
+                                                                                   buttons: 
Gtk.ButtonsType.OK,
+                                                                                   message_type: 
Gtk.MessageType.ERROR,
+                                                                                   text: _("Failed to print 
document"),
+                                                                                   secondary_text: e.message 
});
+                                        errorDialog.connect ('response', Lang.bind(this,
+                                            function() {
+                                                errorDialog.destroy();
+                                            }));
+                                        errorDialog.show();
+                                    }
+                                }
+                            }));
+
+                        let printNotification = new Notifications.PrintNotification(printOp, doc);
 
-                printOp.run(toplevel);
+                        printOp.run(toplevel);
+                    }));
             }));
     },
 
diff --git a/src/evinceview.js b/src/evinceview.js
index d4ea883..3e0070b 100644
--- a/src/evinceview.js
+++ b/src/evinceview.js
@@ -264,7 +264,13 @@ const EvinceView = new Lang.Class({
 
         this.getAction('copy').enabled = false;
         this.getAction('edit-current').enabled = doc.canEdit();
-        this.getAction('print-current').enabled = doc.canPrint(docModel);
+
+        this.getAction('print-current').enabled = false;
+        doc.canPrint(docModel, null, Lang.bind(this,
+            function(doc, supported) {
+                this.getAction('print-current').enabled = supported;
+            }));
+
         let presentCurrent = this.getAction('present-current');
         if (presentCurrent)
             presentCurrent.enabled = true;


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