[gnome-documents] all: move global state singletons to a Global module



commit 746f3af1b0049072d3d5f0089fa68b9b4a17093e
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Aug 24 15:12:28 2011 -0400

    all: move global state singletons to a Global module
    
    This allows further separation between the Sources model and the view
    code.

 src/Makefile-js.am  |    1 +
 src/application.js  |   20 ++++++++++++---
 src/global.js       |   25 +++++++++++++++++++
 src/main.js         |   11 +--------
 src/mainToolbar.js  |   14 ++++++----
 src/mainWindow.js   |   12 ++++----
 src/sidebar.js      |   14 +++--------
 src/sources.js      |   65 +++++++++++++++++++++++++++++++++++++++-----------
 src/trackerModel.js |   30 ++++-------------------
 src/trackerUtils.js |   11 +++++++-
 src/utils.js        |    4 +-
 src/view.js         |    1 -
 12 files changed, 127 insertions(+), 81 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 2255985..58a448d 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -3,6 +3,7 @@ dist_js_DATA = \
     application.js \
     docFactory.js \
     gDataMiner.js \
+    global.js \
     iconView.js \
     listView.js \
     main.js \
diff --git a/src/application.js b/src/application.js
index b6de7f2..15f1863 100644
--- a/src/application.js
+++ b/src/application.js
@@ -25,13 +25,17 @@ const Gettext = imports.gettext;
 
 const EvDoc = imports.gi.EvinceDocument;
 const Gdk = imports.gi.Gdk;
+const Gio = imports.gi.Gio;
 const Gtk = imports.gi.Gtk;
 const GLib = imports.gi.GLib;
 const Tracker = imports.gi.Tracker;
 
 const Format = imports.format;
+const Global = imports.global;
+const Main = imports.main;
 const MainWindow = imports.mainWindow;
 const Path = imports.path;
+const Sources = imports.sources;
 const TrackerModel = imports.trackerModel;
 
 const _GD_DBUS_PATH = '/org/gnome/Documents';
@@ -97,21 +101,29 @@ Application.prototype = {
                                                  provider,
                                                  600);
 
-        // connect to tracker, then create the main window
+        Global.application = this;
+        Global.settings = new Gio.Settings({ schema: 'org.gnome.documents' });
+
+        // connect to tracker
         Tracker.SparqlConnection.get_async(null, Lang.bind(this,
             function(object, res) {
                 try {
-                    this.connection = Tracker.SparqlConnection.get_finish(res);
+                    Global.connection = Tracker.SparqlConnection.get_finish(res);
                 } catch (e) {
                     log('Unable to connect to the tracker database: ' + e.toString());
                     this.quit();
                 }
 
-                this._mainWindow = new MainWindow.MainWindow();
-                this.activate();
+                // now create the source manager
+                Global.sourceManager = new Sources.SourceManager(Lang.bind(this, this._onSourceManagerCreated));
             }));
     },
 
+    _onSourceManagerCreated: function() {
+        this._mainWindow = new MainWindow.MainWindow();
+        this.activate();
+    },
+
     activate: function() {
         this._mainWindow.window.present();
     },
diff --git a/src/global.js b/src/global.js
new file mode 100644
index 0000000..6387a03
--- /dev/null
+++ b/src/global.js
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2011 Red Hat, Inc.
+ *
+ * Gnome Documents is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * Gnome Documents is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with Gnome Documents; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Cosimo Cecchi <cosimoc redhat com>
+ *
+ */
+
+let application = null;
+let sourceManager = null;
+let connection = null;
+let settings = null;
diff --git a/src/main.js b/src/main.js
index 309317a..c03d1ef 100644
--- a/src/main.js
+++ b/src/main.js
@@ -20,18 +20,9 @@
  */
 
 const Gtk = imports.gi.Gtk;
-const Gio = imports.gi.Gio;
-const GLib = imports.gi.GLib;
 const Application = imports.application;
-const Sources = imports.sources;
-
-let application = null;
-let settings = null;
-let sourceManager = null;
 
 function start() {
-    settings = new Gio.Settings({ schema: 'org.gnome.documents' });
-    sourceManager = new Sources.SourceManager();
-    application = new Application.Application();
+    let application = new Application.Application();
     Gtk.main();
 }
diff --git a/src/mainToolbar.js b/src/mainToolbar.js
index 9abd340..4a33331 100644
--- a/src/mainToolbar.js
+++ b/src/mainToolbar.js
@@ -28,6 +28,8 @@ const Lang = imports.lang;
 const Mainloop = imports.mainloop;
 const Signals = imports.signals;
 
+const Global = imports.global;
+
 const _SEARCH_ENTRY_TIMEOUT = 200;
 
 function MainToolbar() {
@@ -58,12 +60,12 @@ MainToolbar.prototype = {
         listView.get_style_context().add_class('linked');
         listView.get_style_context().add_class('raised');
 
-        Main.settings.bind('list-view',
-                           iconView, 'active',
-                           Gio.SettingsBindFlags.INVERT_BOOLEAN);
-        Main.settings.bind('list-view',
-                           listView, 'active',
-                           Gio.SettingsBindFlags.DEFAULT);
+        Global.settings.bind('list-view',
+                             iconView, 'active',
+                             Gio.SettingsBindFlags.INVERT_BOOLEAN);
+        Global.settings.bind('list-view',
+                             listView, 'active',
+                             Gio.SettingsBindFlags.DEFAULT);
 
         let box = new Gtk.Box({ orientation: Gtk.Orientation.HORIZONTAL,
                                 spacing: 0,
diff --git a/src/mainWindow.js b/src/mainWindow.js
index ed749fd..641a33f 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -29,7 +29,7 @@ const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
 const Mainloop = imports.mainloop;
 
-const Main = imports.main;
+const Global = imports.global;
 const MainToolbar = imports.mainToolbar;
 const Sidebar = imports.sidebar;
 const TrackerModel = imports.trackerModel;
@@ -66,7 +66,7 @@ MainWindow.prototype = {
         this.window.connect('delete-event',
                             Lang.bind(this, this._onDeleteEvent));
 
-        Main.settings.connect('changed::list-view', Lang.bind(this, function() {
+        Global.settings.connect('changed::list-view', Lang.bind(this, function() {
             this._refreshViewSettings(true);
         }));
 
@@ -93,7 +93,7 @@ MainWindow.prototype = {
 
         this._grid.show_all();
 
-        this._model = new TrackerModel.TrackerModel(Main.application.connection);
+        this._model = new TrackerModel.TrackerModel(Global.connection);
         this._model.connect('model-update-done', Lang.bind(this, this._onModelUpdateDone));
 
         this._prepareForOverview();
@@ -106,7 +106,7 @@ MainWindow.prototype = {
     },
 
     _initView: function() {
-        let isList = Main.settings.get_boolean('list-view');
+        let isList = Global.settings.get_boolean('list-view');
 
         this._destroyView();
 
@@ -186,7 +186,7 @@ MainWindow.prototype = {
     },
 
     _onDeleteEvent: function() {
-        Main.application.quit();
+        Global.application.quit();
     },
 
     _onViewItemActivated: function(view, uri, resource) {
@@ -195,7 +195,7 @@ MainWindow.prototype = {
             this._loaderTimeout = 0;
         }
 
-        TrackerUtils.sourceIdFromResourceUrn(Main.application.connection, resource, Lang.bind(this,
+        TrackerUtils.sourceIdFromResourceUrn(Global.connection, resource, Lang.bind(this,
             function(sourceId) {
                 this._loaderCancellable = new Gio.Cancellable();
                 this._pdfLoader = new Gd.PdfLoader({ source_id: sourceId });
diff --git a/src/sidebar.js b/src/sidebar.js
index 2f8d026..105196d 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -28,7 +28,7 @@ const _ = imports.gettext.gettext;
 const Lang = imports.lang;
 const Signals = imports.signals;
 
-const Main = imports.main;
+const Global = imports.global;
 
 const _SIDEBAR_WIDTH_REQUEST = 240;
 
@@ -46,13 +46,7 @@ SidebarModel.prototype = {
     _init: function() {
         this.model = Gd.create_sidebar_store();
 
-        this._sourceManager = Main.sourceManager;
-        this._sourceManager.connect('sources-changed', Lang.bind(this, this._refreshModel));
-
-        this._refreshModel();
-    },
-
-    _refreshModel: function() {
+        this._sourceManager = Global.sourceManager;
         this.model.clear();
 
         let iter = this.model.append();
@@ -76,7 +70,7 @@ function SidebarView() {
 SidebarView.prototype = {
     _init: function() {
         this._model = new SidebarModel();
-        this._sourceManager = Main.sourceManager;
+        this._sourceManager = Global.sourceManager;
 
         this._treeView = new Gtk.TreeView({ headers_visible: false,
                                             no_show_all: true });
@@ -159,7 +153,7 @@ function Sidebar() {
 
 Sidebar.prototype = {
     _init: function() {
-        this._sourceManager = Main.sourceManager;
+        this._sourceManager = Global.sourceManager;
         this._sourceManager.connect('active-source-changed',
                                     Lang.bind(this, this._onSourceFilterChanged));
 
diff --git a/src/sources.js b/src/sources.js
index f2a3299..8e29e39 100644
--- a/src/sources.js
+++ b/src/sources.js
@@ -20,33 +20,58 @@
  */
 
 const Lang = imports.lang;
+const Mainloop = imports.mainloop;
 const Signals = imports.signals;
 
 const Goa = imports.gi.Goa;
 const _ = imports.gettext.gettext;
 
-function Source(id, name) {
-    this._init(id, name);
+const Global = imports.global;
+const TrackerUtils = imports.trackerUtils;
+
+function Source(id, name, initCallback) {
+    this._init(id, name, initCallback);
 };
 
 Source.prototype = {
-    _init: function(id, name) {
+    _init: function(id, name, initCallback) {
         this.id = id;
         this.name = name;
+
+        this._initCallback = initCallback;
+
+        if (id == 'all' || id == 'local') {
+            this.resourceUrn = id;
+            Mainloop.idle_add(Lang.bind(this,
+                function() {
+                    this._initCallback();
+                    return false;
+                }));
+        } else {
+            TrackerUtils.resourceUrnFromSourceId(Global.connection, id, Lang.bind(this,
+                function(resourceUrn) {
+                    this.resourceUrn = resourceUrn;
+                    this._initCallback();
+                }));
+        }
     }
 };
 
-function SourceManager() {
-    this._init();
+function SourceManager(initCallback) {
+    this._init(initCallback);
 };
 
 SourceManager.prototype = {
-    _init: function() {
+    _init: function(initCallback) {
         this._client = null;
         this.sources = [];
 
-        this.sources.push(new Source('all', _("All")));
-        this.sources.push(new Source('local', _("Local")));
+        this._initCallback = initCallback;
+
+        // two outstanding ops for the local sources, and one for the GOA client
+        this._outstandingOps = 3;
+        this.sources.push(new Source('all', _("All"), Lang.bind(this, this._initSourceCollector)));
+        this.sources.push(new Source('local', _("Local"), Lang.bind(this, this._initSourceCollector)));
 
         Goa.Client.new(null, Lang.bind(this, this._onGoaClientCreated));
     },
@@ -74,15 +99,21 @@ SourceManager.prototype = {
                 let id = account.get_id();
                 let name = account.get_provider_name();
 
-                this.sources.push(new Source(id, name));
-                modified = true;
+                this._outstandingOps++;
+                this.sources.push(new Source(id, name, Lang.bind(this, this._initSourceCollector)));
             }));
 
-        if (modified)
-            this.emit('sources-changed');
-
-        let activeSourceId = Main.settings.get_string('active-source');
+        let activeSourceId = Global.settings.get_string('active-source');
         this.setActiveSourceId(activeSourceId);
+
+        this._initSourceCollector();
+    },
+
+    _initSourceCollector: function() {
+        this._outstandingOps--;
+
+        if (this._outstandingOps == 0)
+            this._initCallback();
     },
 
     setActiveSourceId: function(id) {
@@ -95,13 +126,17 @@ SourceManager.prototype = {
             return;
 
         this.activeSource = matched[0];
-        Main.settings.set_string('active-source', this.activeSource.id);
+        Global.settings.set_string('active-source', this.activeSource.id);
 
         this.emit('active-source-changed');
     },
 
     getActiveSourceId: function() {
         return this.activeSource.id;
+    },
+
+    getActiveSourceUrn: function() {
+        return this.activeSource.resourceUrn;
     }
 };
 Signals.addSignalMethods(SourceManager.prototype);
diff --git a/src/trackerModel.js b/src/trackerModel.js
index d0e3013..56d92fc 100644
--- a/src/trackerModel.js
+++ b/src/trackerModel.js
@@ -31,7 +31,7 @@ const Gd = imports.gi.Gd;
 
 const DocFactory = imports.docFactory;
 const GDataMiner = imports.gDataMiner;
-const Main = imports.main;
+const Global = imports.global;
 const TrackerUtils = imports.trackerUtils;
 const Utils = imports.utils;
 
@@ -193,7 +193,7 @@ TrackerModel.prototype = {
     _init: function(connection) {
         this._builder = new QueryBuilder();
         this._factory = new DocFactory.DocFactory();
-        Main.settings.connect('changed::list-view', Lang.bind(this, this._onSettingsChanged));
+        Global.settings.connect('changed::list-view', Lang.bind(this, this._onSettingsChanged));
 
         this.model = Gd.create_list_store();
         this._connection = connection;
@@ -202,7 +202,7 @@ TrackerModel.prototype = {
         this._miner = new GDataMiner.GDataMiner();
         this._refreshMinerNow();
 
-        this._sourceManager = Main.sourceManager;
+        this._sourceManager = Global.sourceManager;
         this._sourceManager.connect('active-source-changed',
                                     Lang.bind(this, this._refreshAccountFilter));
     },
@@ -322,28 +322,8 @@ TrackerModel.prototype = {
     },
 
     _refreshAccountFilter: function() {
-        let id = this._sourceManager.getActiveSourceId();
-
-        if (id == 'all' || id == 'local') {
-            this._resourceUrn = id;
-            this._refresh();
-
-            return;
-        }
-
-        TrackerUtils.resourceUrnFromSourceId(this._connection, id, Lang.bind(this,
-            function(resourceUrn) {
-                this.model.clear();
-
-                if (resourceUrn) {
-                    this._resourceUrn = resourceUrn;
-                    this._performCurrentQuery();
-                } else {
-                    this.offset = 0;
-                    this.itemCount = 0;
-                    this._emitModelUpdateDone();
-                }
-            }));
+        this._resourceUrn = this._sourceManager.getActiveSourceUrn();
+        this._refresh();
     }
 };
 Signals.addSignalMethods(TrackerModel.prototype);
diff --git a/src/trackerUtils.js b/src/trackerUtils.js
index 846f5e8..8a1f829 100644
--- a/src/trackerUtils.js
+++ b/src/trackerUtils.js
@@ -59,10 +59,15 @@ function resourceUrnFromSourceId(connection, sourceId, callback) {
         (('SELECT ?urn WHERE { ?urn a nie:DataSource; nao:identifier \"goa:documents:%s\" }').format(sourceId), null,
          function(object, res) {
              let cursor = null;
+             let urn = '';
+
              try {
                  cursor = object.query_finish(res);
              } catch (e) {
                  log('Unable to resolve account ID -> resource URN: ' + e.toString());
+
+                 callback(urn);
+                 return;
              }
 
              cursor.next_async(null,
@@ -71,14 +76,16 @@ function resourceUrnFromSourceId(connection, sourceId, callback) {
                          let valid = cursor.next_finish(res);
 
                          if (!valid) {
-                             callback(null);
+                             callback(urn);
                              return;
                          }
                      } catch (e) {
                          log('Unable to resolve account ID -> resource URN: ' + e.toString());
+                         callback(urn);
+                         return;
                      }
 
-                     let urn = cursor.get_string(0)[0];
+                     urn = cursor.get_string(0)[0];
                      callback(urn);
                  });
          });
diff --git a/src/utils.js b/src/utils.js
index 0330fee..56e3ae0 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -21,13 +21,13 @@
 
 const Gtk = imports.gi.Gtk;
 
-const Main = imports.main;
+const Global = imports.global;
 
 const _ICON_VIEW_SIZE = 128;
 const _LIST_VIEW_SIZE = 48;
 
 function getIconSize() {
-    return Main.settings.get_boolean('list-view') ? _LIST_VIEW_SIZE : _ICON_VIEW_SIZE;
+    return Global.settings.get_boolean('list-view') ? _LIST_VIEW_SIZE : _ICON_VIEW_SIZE;
 }
 
 function pixbufFromRdfType(type) {
diff --git a/src/view.js b/src/view.js
index 94224b3..0c25dd7 100644
--- a/src/view.js
+++ b/src/view.js
@@ -24,7 +24,6 @@ const Gtk = imports.gi.Gtk;
 const Lang = imports.lang;
 const Signals = imports.signals;
 
-const Main = imports.main;
 const TrackerModel = imports.trackerModel;
 
 function View(window) {



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