[gnome-documents] documents: move preview loading to DocumentsManager



commit 2aaaafebb850b8c34b12f145f1b79a46b11b324f
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Jul 17 15:25:05 2012 -0400

    documents: move preview loading to DocumentsManager
    
    This also gives us a convenient place to store the currently active
    EvDocumentModel, and is a prerequisite for other things we want to do
    with such object.

 src/documents.js   |   62 ++++++++++++++++++++++++++++++++++++----------------
 src/embed.js       |   59 +++++++++++++++++++------------------------------
 src/error.js       |    9 -------
 src/mainToolbar.js |    1 -
 4 files changed, 66 insertions(+), 65 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 4f2531e..af7d4a6 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -19,14 +19,12 @@
  *
  */
 
-const EvView = imports.gi.EvinceView;
 const GdkPixbuf = imports.gi.GdkPixbuf;
 const Gio = imports.gi.Gio;
 const Gd = imports.gi.Gd;
 const Gdk = imports.gi.Gdk;
 const GData = imports.gi.GData;
 const GLib = imports.gi.GLib;
-const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 const Zpj = imports.gi.Zpj;
 const _ = imports.gettext.gettext;
@@ -609,13 +607,6 @@ const DocCommon = new Lang.Class({
             retval = '{ ?urn nie:isPartOf <' + this.id + '> }';
 
         return retval;
-    },
-
-    _finishLoad: function(docModel, callback, exception) {
-        if (exception)
-            Global.errorHandler.addLoadError(this, exception);
-
-        callback(this, docModel, exception);
     }
 });
 Signals.addSignalMethods(DocCommon.prototype);
@@ -650,9 +641,9 @@ const LocalDocument = new Lang.Class({
             function(source, res) {
                 try {
                     let docModel = Gd.pdf_loader_load_uri_finish(res);
-                    this._finishLoad(docModel, callback, null);
+                    callback(this, docModel, null);
                 } catch (e) {
-                    this._finishLoad(null, callback, e);
+                    callback(this, null, e);
                 }
             }));
     },
@@ -721,10 +712,10 @@ const GoogleDocument = new Lang.Class({
                         function(source, res) {
                             try {
                                 let docModel = Gd.pdf_loader_load_uri_finish(res);
-                                this._finishLoad(docModel, callback, null);
+                                callback(this, docModel, null);
                             } catch (e) {
                                 // report the outmost error only
-                                this._finishLoad(null, callback, exception);
+                                callback(this, null, exception);
                             }
                         }));
 
@@ -736,9 +727,9 @@ const GoogleDocument = new Lang.Class({
                         function(source, res) {
                             try {
                                 let docModel = Gd.pdf_loader_load_uri_finish(res);
-                                this._finishLoad(docModel, callback, null);
+                                callback(this, docModel, null);
                             } catch (e) {
-                                this._finishLoad(null, callback, e);
+                                callback(this, null, e);
                             }
                         }));
             }));
@@ -860,10 +851,10 @@ const SkydriveDocument = new Lang.Class({
                         function(source, res) {
                             try {
                                 let docModel = Gd.pdf_loader_load_uri_finish(res);
-                                this._finishLoad(docModel, callback, null);
+                                callback(this, docModel, null);
                             } catch (e) {
                                 // report the outmost error only
-                                this._finishLoad(null, callback, exception);
+                                callback(this, null, exception);
                             }
                         }));
 
@@ -875,9 +866,9 @@ const SkydriveDocument = new Lang.Class({
                         function(source, res) {
                             try {
                                 let docModel = Gd.pdf_loader_load_zpj_entry_finish(res);
-                                this._finishLoad(docModel, callback, null);
+                                callback(this, docModel, null);
                             } catch (e) {
-                                this._finishLoad(null, callback, e);
+                                callback(this, null, e);
                             }
                         }));
             }));
@@ -913,6 +904,8 @@ const DocumentManager = new Lang.Class({
     _init: function() {
         this.parent();
 
+        this._activeDocModel = null;
+        this._loaderCancellable = null;
 
         Global.changeMonitor.connect('changes-pending',
                                      Lang.bind(this, this._onChangesPending));
@@ -997,10 +990,38 @@ const DocumentManager = new Lang.Class({
         this.parent();
     },
 
+    _onDocumentLoadError: function(doc, error) {
+        if (error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
+            return;
+
+        // Translators: %s is the title of a document
+        let message = _("Unable to load \"%s\" for preview").format(doc.name);
+        this.emit('load-error', doc, message, error);
+    },
+
+    _onDocumentLoaded: function(doc, docModel, error) {
+        if (error) {
+            this._onDocumentLoadError(doc, error);
+            return;
+        }
+
+        this._activeDocModel = docModel;
+        this.emit('load-finished', doc, docModel);
+    },
+
     setActiveItem: function(doc) {
         if (!this.parent(doc))
             return;
 
+        // cancel any pending load operation
+        if (this._loaderCancellable) {
+            this._loaderCancellable.cancel();
+            this._loaderCancellable = null;
+        }
+
+        // clear any previously loaded document model
+        this._activeDocModel = null;
+
         if (!doc)
             return;
 
@@ -1012,5 +1033,8 @@ const DocumentManager = new Lang.Class({
         let recentManager = Gtk.RecentManager.get_default();
         recentManager.add_item(doc.uri);
 
+        this._loaderCancellable = new Gio.Cancellable();
+        doc.load(this._loaderCancellable, Lang.bind(this, this._onDocumentLoaded));
+        this.emit('load-started', doc);
     }
 });
diff --git a/src/embed.js b/src/embed.js
index 21d80f1..3c69bf9 100644
--- a/src/embed.js
+++ b/src/embed.js
@@ -46,7 +46,6 @@ const Embed = new Lang.Class({
     Name: 'Embed',
 
     _init: function() {
-        this._loaderCancellable = null;
         this._queryErrorId = 0;
 
         this.widget = new GtkClutter.Embed({ use_layout_size: true });
@@ -126,17 +125,21 @@ const Embed = new Lang.Class({
         this._preview = new Preview.PreviewView();
         this._previewPage = this._notebook.append_page(this._preview.widget, null);
 
-        Global.errorHandler.connect('load-error',
-                                    Lang.bind(this, this._onLoadError));
-
         Global.modeController.connect('window-mode-changed',
                                       Lang.bind(this, this._onWindowModeChanged));
         Global.modeController.connect('fullscreen-changed',
                                       Lang.bind(this, this._onFullscreenChanged));
         Global.trackerController.connect('query-status-changed',
                                          Lang.bind(this, this._onQueryStatusChanged));
+
         Global.documentManager.connect('active-changed',
                                        Lang.bind(this, this._onActiveItemChanged));
+        Global.documentManager.connect('load-started',
+                                       Lang.bind(this, this._onLoadStarted));
+        Global.documentManager.connect('load-finished',
+                                       Lang.bind(this, this._onLoadFinished));
+        Global.documentManager.connect('load-error',
+                                       Lang.bind(this, this._onLoadError));
 
         this._onQueryStatusChanged();
     },
@@ -191,34 +194,25 @@ const Embed = new Lang.Class({
             this._windowModeChangeFlash();
     },
 
-    _onActiveItemChanged: function() {
-        let doc = Global.documentManager.getActiveItem();
-
-        if (!doc)
-            return;
+    _onActiveItemChanged: function(manager, doc) {
+        let newMode = WindowMode.WindowMode.OVERVIEW;
 
-        let collection = Global.collectionManager.getItemById(doc.id);
-        if (collection) {
-            Global.modeController.setWindowMode(WindowMode.WindowMode.OVERVIEW);
-            return;
+        if (doc) {
+            let collection = Global.collectionManager.getItemById(doc.id);
+            if (!collection)
+                newMode = WindowMode.WindowMode.PREVIEW;
         }
 
+        Global.modeController.setWindowMode(newMode);
+    },
+
+    _onLoadStarted: function() {
         // switch to preview mode, and schedule the spinnerbox to
         // move in if the document is not loaded by the timeout
-        Global.modeController.setWindowMode(WindowMode.WindowMode.PREVIEW);
         this._spinnerBox.moveInDelayed(_PDF_LOADER_TIMEOUT);
-
-        this._loaderCancellable = new Gio.Cancellable();
-        doc.load(this._loaderCancellable, Lang.bind(this, this._onDocumentLoaded));
     },
 
-    _onDocumentLoaded: function(doc, docModel, error) {
-        this._loaderCancellable = null;
-
-        if (!docModel) {
-            return;
-        }
-
+    _onLoadFinished: function(manager, doc, docModel) {
         this._toolbar.setModel(docModel);
         this._preview.setModel(docModel);
         this._preview.widget.grab_focus();
@@ -227,15 +221,15 @@ const Embed = new Lang.Class({
         Global.modeController.setCanFullscreen(true);
     },
 
+    _onLoadError: function(manager, doc, message, exception) {
+        this._spinnerBox.moveOut();
+        this._setError(message, exception.message);
+    },
+
     _prepareForOverview: function() {
         if (this._preview)
             this._preview.setModel(null);
 
-        if (this._loaderCancellable) {
-            this._loaderCancellable.cancel();
-            this._loaderCancellable = null;
-        }
-
         this._spinnerBox.moveOut();
         this._errorBox.moveOut();
 
@@ -262,12 +256,5 @@ const Embed = new Lang.Class({
     _setError: function(primary, secondary) {
         this._errorBox.update(primary, secondary);
         this._errorBox.moveIn();
-    },
-
-    _onLoadError: function(manager, message, exception) {
-        this._loaderCancellable = null;
-        this._spinnerBox.moveOut();
-
-        this._setError(message, exception.message);
     }
 });
diff --git a/src/error.js b/src/error.js
index 11f3e39..a96813b 100644
--- a/src/error.js
+++ b/src/error.js
@@ -31,15 +31,6 @@ const ErrorHandler = new Lang.Class({
     _init: function() {
     },
 
-    addLoadError: function(doc, exception) {
-        if (exception.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
-            return;
-
-        // Translators: %s is the title of a document
-        let message = _("Unable to load \"%s\" for preview").format(doc.name);
-        this.emit('load-error', message, exception);
-    },
-
     addQueryError: function(exception) {
         if (exception.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
             return;
diff --git a/src/mainToolbar.js b/src/mainToolbar.js
index 021032a..2aa1b04 100644
--- a/src/mainToolbar.js
+++ b/src/mainToolbar.js
@@ -240,7 +240,6 @@ const MainToolbar = new Lang.Class({
         backButton.connect('clicked', Lang.bind(this,
             function() {
                 Global.documentManager.setActiveItem(null);
-                Global.modeController.setWindowMode(WindowMode.WindowMode.OVERVIEW);
             }));
     },
 



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