[gnome-documents] sources: move the source list into SourceManager



commit d8e42f0a3431a8839cb777698268868e195cffb1
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Wed Aug 24 12:08:05 2011 -0400

    sources: move the source list into SourceManager
    
    And turn the source in an object with id and name (for now).

 src/sidebar.js      |   74 ++++++++++++++++++--------------------------------
 src/sources.js      |   75 ++++++++++++++++++++++++++++++++++++++++++++++++---
 src/trackerModel.js |    8 +++--
 3 files changed, 103 insertions(+), 54 deletions(-)
---
diff --git a/src/sidebar.js b/src/sidebar.js
index 5f06bec..2f8d026 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -44,62 +44,39 @@ function SidebarModel() {
 
 SidebarModel.prototype = {
     _init: function() {
-        this.activeAccounts = [];
-
-        Goa.Client.new(null, Lang.bind(this, this._onGoaClientCreated));
-
         this.model = Gd.create_sidebar_store();
 
-        let iter = this.model.append();
-        Gd.sidebar_store_set(this.model, iter,
-                             '', _("Sources"), true);
-
-        iter = this.model.append();
-        Gd.sidebar_store_set(this.model, iter,
-                             'all', _("All"), false);
+        this._sourceManager = Main.sourceManager;
+        this._sourceManager.connect('sources-changed', Lang.bind(this, this._refreshModel));
 
-        iter = this.model.append();
-        Gd.sidebar_store_set(this.model, iter,
-                             'local', _("Local"), false);
+        this._refreshModel();
     },
 
-    _onGoaClientCreated: function(object, res) {
-        try {
-            this._client = Goa.Client.new_finish(res);
-        } catch (e) {
-            log('Unable to create the GOA client: ' + e.toString());
-            return;
-        }
+    _refreshModel: function() {
+        this.model.clear();
 
-        let accounts = this._client.get_accounts();
-        accounts.forEach(Lang.bind(this,
-            function(object) {
-                let account = object.get_account();
-                if (!account)
-                    return;
-
-                if (!object.get_documents())
-                    return;
-
-                let id = account.get_id();
-                let name = account.get_provider_name();
+        let iter = this.model.append();
+        Gd.sidebar_store_set(this.model, iter,
+                             '', _("Sources"), true);
 
-                let iter = this.model.append();
+        let sources = this._sourceManager.sources;
+        sources.forEach(Lang.bind(this,
+            function(source) {
+                iter = this.model.append();
                 Gd.sidebar_store_set(this.model, iter,
-                                     id, name, false);
-
-                this.activeAccounts.push(id);
+                                     source.id, source.name, false);
             }));
     }
 };
 
-function SourcesPage() {
+function SidebarView() {
     this._init();
 }
 
-SourcesPage.prototype = {
+SidebarView.prototype = {
     _init: function() {
         this._model = new SidebarModel();
+        this._sourceManager = Main.sourceManager;
 
         this._treeView = new Gtk.TreeView({ headers_visible: false,
                                             no_show_all: true });
@@ -116,7 +93,7 @@ SourcesPage.prototype = {
                 let id = this._model.model.get_value(iter, SidebarModelColumns.ID);
                 let name = this._model.model.get_value(iter, SidebarModelColumns.NAME);
 
-                Main.sourceManager.setActiveSource(id);
+                this._sourceManager.setActiveSourceId(id);
             }));
 
         let col = new Gtk.TreeViewColumn();
@@ -140,7 +117,7 @@ SourcesPage.prototype = {
                       Lang.bind(this,
                           function(col, cell, model, iter) {
                               let id = model.get_value(iter, SidebarModelColumns.ID);
-                              if (id == Main.sourceManager.activeSource)
+                              if (id == this._sourceManager.getActiveSourceId())
                                   cell.active = true;
                               else
                                   cell.active = false;
@@ -174,7 +151,7 @@ SourcesPage.prototype = {
             additionalFunc(col, cell, model, iter);
     }
 };
-Signals.addSignalMethods(SourcesPage.prototype);
+Signals.addSignalMethods(SidebarView.prototype);
 
 function Sidebar() {
     this._init();
@@ -182,6 +159,10 @@ function Sidebar() {
 
 Sidebar.prototype = {
     _init: function() {
+        this._sourceManager = Main.sourceManager;
+        this._sourceManager.connect('active-source-changed',
+                                    Lang.bind(this, this._onSourceFilterChanged));
+
         this.widget = new Gtk.ScrolledWindow({ hscrollbar_policy: Gtk.PolicyType.NEVER });
         this.widget.get_style_context().add_class(Gtk.STYLE_CLASS_SIDEBAR);
 
@@ -205,20 +186,19 @@ Sidebar.prototype = {
         this._grid.add(this._sourcesButton);
         this._sourcesButton.connect('clicked', Lang.bind(this, this._onSourcesButtonClicked));
 
-        this._sourcesPage = new SourcesPage();
-        this._grid.add(this._sourcesPage.widget);
-        Main.sourceManager.connect('active-source-changed', Lang.bind(this, this._onSourceFilterChanged));
+        this._sidebarView = new SidebarView();
+        this._grid.add(this._sidebarView.widget);
 
         this.widget.show_all();
     },
 
     _onSourcesButtonClicked: function() {
         this._sourcesButton.hide();
-        this._sourcesPage.widget.show();
+        this._sidebarView.widget.show();
     },
 
     _onSourceFilterChanged: function(sourcePage, id, name) {
-        this._sourcesPage.widget.hide();
+        this._sidebarView.widget.hide();
         this._sourcesButton.show();
     }
 };
diff --git a/src/sources.js b/src/sources.js
index 989dc7b..f2a3299 100644
--- a/src/sources.js
+++ b/src/sources.js
@@ -19,22 +19,89 @@
  *
  */
 
+const Lang = imports.lang;
 const Signals = imports.signals;
 
+const Goa = imports.gi.Goa;
+const _ = imports.gettext.gettext;
+
+function Source(id, name) {
+    this._init(id, name);
+};
+
+Source.prototype = {
+    _init: function(id, name) {
+        this.id = id;
+        this.name = name;
+    }
+};
+
 function SourceManager() {
     this._init();
 };
 
 SourceManager.prototype = {
     _init: function() {
-        this.activeSource = this._currentSourceId = Main.settings.get_string('active-source');
+        this._client = null;
+        this.sources = [];
+
+        this.sources.push(new Source('all', _("All")));
+        this.sources.push(new Source('local', _("Local")));
+
+        Goa.Client.new(null, Lang.bind(this, this._onGoaClientCreated));
     },
 
-    setActiveSource: function(id) {
-        this.activeSource = id;
-        Main.settings.set_string('active-source', id);
+    _onGoaClientCreated: function(object, res) {
+        try {
+            this._client = Goa.Client.new_finish(res);
+        } catch (e) {
+            log('Unable to create the GOA client: ' + e.toString());
+            return;
+        }
+
+        let accounts = this._client.get_accounts();
+        let modified = false;
+
+        accounts.forEach(Lang.bind(this,
+            function(object) {
+                let account = object.get_account();
+                if (!account)
+                    return;
+
+                if (!object.get_documents())
+                    return;
+
+                let id = account.get_id();
+                let name = account.get_provider_name();
+
+                this.sources.push(new Source(id, name));
+                modified = true;
+            }));
+
+        if (modified)
+            this.emit('sources-changed');
+
+        let activeSourceId = Main.settings.get_string('active-source');
+        this.setActiveSourceId(activeSourceId);
+    },
+
+    setActiveSourceId: function(id) {
+        let matched = this.sources.filter(Lang.bind(this,
+            function(source) {
+                return (source.id == id);
+            }));
+
+        if (!matched.length)
+            return;
+
+        this.activeSource = matched[0];
+        Main.settings.set_string('active-source', this.activeSource.id);
 
         this.emit('active-source-changed');
+    },
+
+    getActiveSourceId: function() {
+        return this.activeSource.id;
     }
 };
 Signals.addSignalMethods(SourceManager.prototype);
diff --git a/src/trackerModel.js b/src/trackerModel.js
index fc73501..d0e3013 100644
--- a/src/trackerModel.js
+++ b/src/trackerModel.js
@@ -202,7 +202,9 @@ TrackerModel.prototype = {
         this._miner = new GDataMiner.GDataMiner();
         this._refreshMinerNow();
 
-        Main.sourceManager.connect('active-source-changed', Lang.bind(this, this._refreshAccountFilter));
+        this._sourceManager = Main.sourceManager;
+        this._sourceManager.connect('active-source-changed',
+                                    Lang.bind(this, this._refreshAccountFilter));
     },
 
     _onSettingsChanged: function() {
@@ -304,7 +306,7 @@ TrackerModel.prototype = {
         this.offset = 0;
         this._filter = filter;
 
-        this._refreshAccountFilter(Main.sourceManager.activeSource);
+        this._refreshAccountFilter(this._sourceManager.getActiveSourceId());
     },
 
     loadMore: function() {
@@ -320,7 +322,7 @@ TrackerModel.prototype = {
     },
 
     _refreshAccountFilter: function() {
-        let id = Main.sourceManager.activeSource;
+        let id = this._sourceManager.getActiveSourceId();
 
         if (id == 'all' || id == 'local') {
             this._resourceUrn = id;



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