[gnome-documents] sources: add a source manager to consolidate active source handling



commit df0e4458ecf4d7fdfefead511b369732a26fde4a
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Aug 23 21:02:19 2011 -0400

    sources: add a source manager to consolidate active source handling
    
    SourceManager is a singleton that synchronizes the active source over
    the views and the model (and settings).

 src/Makefile-js.am  |    1 +
 src/main.js         |    5 ++++-
 src/mainWindow.js   |   12 +-----------
 src/sidebar.js      |   13 +++----------
 src/sources.js      |   40 ++++++++++++++++++++++++++++++++++++++++
 src/trackerModel.js |   10 +++++++---
 6 files changed, 56 insertions(+), 25 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 4f5a44c..2255985 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -10,6 +10,7 @@ dist_js_DATA = \
     mainWindow.js \
     preview.js \
     sidebar.js \
+    sources.js \
     spinnerBox.js \
     trackerModel.js \
     trackerUtils.js \
diff --git a/src/main.js b/src/main.js
index 18c49f2..309317a 100644
--- a/src/main.js
+++ b/src/main.js
@@ -23,12 +23,15 @@ 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() {
-    application = new Application.Application();
     settings = new Gio.Settings({ schema: 'org.gnome.documents' });
+    sourceManager = new Sources.SourceManager();
+    application = new Application.Application();
     Gtk.main();
 }
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 4f6ef7b..ed749fd 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -57,8 +57,6 @@ MainWindow.prototype = {
         this._loaderTimeout = 0;
         this._lastFilter = '';
 
-        this._currentSourceId = Main.settings.get_string('active-source');
-
         this.window = new Gtk.Window({ type: Gtk.WindowType.TOPLEVEL,
                                        window_position: Gtk.WindowPosition.CENTER,
                                        title: _("Documents") });
@@ -76,7 +74,6 @@ MainWindow.prototype = {
         this.window.add(this._grid);
 
         this._sidebar = new Sidebar.Sidebar();
-        this._sidebar.connect('source-filter-changed', Lang.bind(this, this._onSourceFilterChanged));
         this._grid.add(this._sidebar.widget);
 
         this._viewContainer = new Gtk.Grid({ orientation: Gtk.Orientation.VERTICAL });
@@ -185,7 +182,7 @@ MainWindow.prototype = {
 
         this._sidebar.widget.show();
         this._toolbar.setOverview(this._lastFilter);
-        this._model.populateForOverview(this._currentSourceId, this._lastFilter);
+        this._model.populateForOverview(this._lastFilter);
     },
 
     _onDeleteEvent: function() {
@@ -254,12 +251,5 @@ MainWindow.prototype = {
 
     _onModelUpdateDone: function(model, itemCount, offset) {
         this._refreshLoadMoreButton(itemCount, offset);
-    },
-
-    _onSourceFilterChanged: function(sidebar, id) {
-        this._currentSourceId = id;
-        Main.settings.set_string('active-source', id);
-
-        this._model.setAccountFilter(id);
     }
 };
diff --git a/src/sidebar.js b/src/sidebar.js
index 9c4fad9..5f06bec 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -100,7 +100,6 @@ function SourcesPage() {
 SourcesPage.prototype = {
     _init: function() {
         this._model = new SidebarModel();
-        this._currentSourceId = Main.settings.get_string('active-source');
 
         this._treeView = new Gtk.TreeView({ headers_visible: false,
                                             no_show_all: true });
@@ -117,9 +116,7 @@ SourcesPage.prototype = {
                 let id = this._model.model.get_value(iter, SidebarModelColumns.ID);
                 let name = this._model.model.get_value(iter, SidebarModelColumns.NAME);
 
-                this._currentSourceId = id;
-
-                this.emit('source-filter-changed', id, name);
+                Main.sourceManager.setActiveSource(id);
             }));
 
         let col = new Gtk.TreeViewColumn();
@@ -143,7 +140,7 @@ SourcesPage.prototype = {
                       Lang.bind(this,
                           function(col, cell, model, iter) {
                               let id = model.get_value(iter, SidebarModelColumns.ID);
-                              if (id == this._currentSourceId)
+                              if (id == Main.sourceManager.activeSource)
                                   cell.active = true;
                               else
                                   cell.active = false;
@@ -210,7 +207,7 @@ Sidebar.prototype = {
 
         this._sourcesPage = new SourcesPage();
         this._grid.add(this._sourcesPage.widget);
-        this._sourcesPage.connect('source-filter-changed', Lang.bind(this, this._onSourceFilterChanged));
+        Main.sourceManager.connect('active-source-changed', Lang.bind(this, this._onSourceFilterChanged));
 
         this.widget.show_all();
     },
@@ -223,9 +220,5 @@ Sidebar.prototype = {
     _onSourceFilterChanged: function(sourcePage, id, name) {
         this._sourcesPage.widget.hide();
         this._sourcesButton.show();
-
-        // forward the signal
-        this.emit('source-filter-changed', id);
     }
 };
-Signals.addSignalMethods(Sidebar.prototype);
diff --git a/src/sources.js b/src/sources.js
new file mode 100644
index 0000000..989dc7b
--- /dev/null
+++ b/src/sources.js
@@ -0,0 +1,40 @@
+/*
+ * 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>
+ *
+ */
+
+const Signals = imports.signals;
+
+function SourceManager() {
+    this._init();
+};
+
+SourceManager.prototype = {
+    _init: function() {
+        this.activeSource = this._currentSourceId = Main.settings.get_string('active-source');
+    },
+
+    setActiveSource: function(id) {
+        this.activeSource = id;
+        Main.settings.set_string('active-source', id);
+
+        this.emit('active-source-changed');
+    }
+};
+Signals.addSignalMethods(SourceManager.prototype);
diff --git a/src/trackerModel.js b/src/trackerModel.js
index 30507da..fc73501 100644
--- a/src/trackerModel.js
+++ b/src/trackerModel.js
@@ -201,6 +201,8 @@ TrackerModel.prototype = {
         // startup a refresh of the gdocs cache
         this._miner = new GDataMiner.GDataMiner();
         this._refreshMinerNow();
+
+        Main.sourceManager.connect('active-source-changed', Lang.bind(this, this._refreshAccountFilter));
     },
 
     _onSettingsChanged: function() {
@@ -298,11 +300,11 @@ TrackerModel.prototype = {
         this._performCurrentQuery();
     },
 
-    populateForOverview: function(resourceUrn, filter) {
+    populateForOverview: function(filter) {
         this.offset = 0;
         this._filter = filter;
 
-        this.setAccountFilter(resourceUrn);
+        this._refreshAccountFilter(Main.sourceManager.activeSource);
     },
 
     loadMore: function() {
@@ -317,7 +319,9 @@ TrackerModel.prototype = {
         this._refresh();
     },
 
-    setAccountFilter: function(id) {
+    _refreshAccountFilter: function() {
+        let id = Main.sourceManager.activeSource;
+
         if (id == 'all' || id == 'local') {
             this._resourceUrn = id;
             this._refresh();



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