[gnome-documents] all: move processNewItems to BaseManager and use it in SourceManager



commit e201dfef26948baeb9a7faa499c378488a1fcb89
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Oct 18 18:05:31 2011 -0400

    all: move processNewItems to BaseManager and use it in SourceManager

 src/collections.js |   17 +----------------
 src/manager.js     |   30 ++++++++++++++++++++++--------
 src/query.js       |    2 +-
 src/sources.js     |   26 ++++++++++++--------------
 4 files changed, 36 insertions(+), 39 deletions(-)
---
diff --git a/src/collections.js b/src/collections.js
index 169b4b7..2356dcb 100644
--- a/src/collections.js
+++ b/src/collections.js
@@ -108,22 +108,7 @@ CollectionManager.prototype = {
     },
 
     _refreshItems: function() {
-        let oldItems = this.getItems();
-
-        for (idx in oldItems) {
-            // if old items are not found in the new array,
-            // remove them
-            if (!this._newItems[idx])
-                this.removeItem(oldItems[idx]);
-        }
-
-        for (idx in this._newItems) {
-            // if new items are not found in the old array,
-            // add them
-            if (!oldItems[idx])
-                this.addItem(this._newItems[idx]);
-        }
-
+        this.processNewItems(this._newItems);
         this._newItems = {};
     },
 
diff --git a/src/manager.js b/src/manager.js
index 39ba11a..3a4cad4 100644
--- a/src/manager.js
+++ b/src/manager.js
@@ -80,14 +80,6 @@ BaseManager.prototype = {
         this.removeItemById(item.id);
     },
 
-    removeMatchingItems: function(filterFunc) {
-        for (idx in this._items) {
-            let item = this._items[idx];
-            if (!filterFunc(item))
-                this.removeItemById(idx);
-        }
-    },
-
     removeItemById: function(id) {
         let item = this._items[id];
 
@@ -108,6 +100,28 @@ BaseManager.prototype = {
             return item.getFilter();
 
         return '';
+    },
+
+    processNewItems: function(newItems) {
+        let oldItems = this.getItems();
+
+        for (idx in oldItems) {
+            let item = oldItems[idx];
+
+            // if old items are not found in the new array,
+            // remove them
+            if (!newItems[idx] && !item.builtin)
+                this.removeItem(oldItems[idx]);
+        }
+
+        for (idx in newItems) {
+            // if new items are not found in the old array,
+            // add them
+            if (!oldItems[idx])
+                this.addItem(newItems[idx]);
+        }
+
+        // TODO: merge existing item properties with new values
     }
 };
 Signals.addSignalMethods(BaseManager.prototype);
diff --git a/src/query.js b/src/query.js
index cb2f05d..40f1179 100644
--- a/src/query.js
+++ b/src/query.js
@@ -96,7 +96,7 @@ QueryBuilder.prototype = {
 
         for (idx in sources) {
             let source = sources[idx];
-            if (source.isGoa)
+            if (!source.builtin)
                 sparql += source.getFilter() + ' || ';
         }
 
diff --git a/src/sources.js b/src/sources.js
index d5bfbd7..cb94003 100644
--- a/src/sources.js
+++ b/src/sources.js
@@ -43,18 +43,17 @@ Source.prototype = {
     _init: function(params) {
         this.id = null;
         this.name = null;
-        this.isGoa = false;
 
         if (params.object) {
             let account = params.object.get_account();
             this.id = 'gd:goa-account:' + account.id;
             this.name = account.provider_name;
-
-            this.isGoa = true;
         } else {
             this.id = params.id;
             this.name = params.name;
         }
+
+        this.builtin = params.builtin;
     },
 
     getFilter: function() {
@@ -71,7 +70,7 @@ Source.prototype = {
     _buildFilterResource: function() {
         let filter = '(false)';
 
-        if (this.isGoa)
+        if (!this.builtin)
             filter = ('(nie:dataSource(?urn) = "%s")').format(this.id);
 
         return filter;
@@ -90,29 +89,26 @@ SourceManager.prototype = {
 
         // Translators: this refers to documents
         let source = new Source({ id: 'all',
-                                  name: _("All") });
+                                  name: _("All"),
+                                  builtin: true });
         this.addItem(source);
 
         // Translators: this refers to local documents
         source = new Source({ id: 'local',
-                              name: _("Local") });
+                              name: _("Local"),
+                              builtin: true });
         this.addItem(source);
 
         Global.goaClient.connect('account-added', Lang.bind(this, this._refreshGoaAccounts));
         Global.goaClient.connect('account-changed', Lang.bind(this, this._refreshGoaAccounts));
         Global.goaClient.connect('account-removed', Lang.bind(this, this._refreshGoaAccounts));
+
         this._refreshGoaAccounts();
     },
 
     _refreshGoaAccounts: function() {
-        this.removeMatchingItems(
-            function(item) {
-                // if it's a Goa object, remove it
-                return !item.isGoa;
-            });
-
+        let newItems = {};
         let accounts = Global.goaClient.get_accounts();
-        let modified = false;
 
         accounts.forEach(Lang.bind(this,
             function(object) {
@@ -123,9 +119,11 @@ SourceManager.prototype = {
                     return;
 
                 let source = new Source({ object: object });
-                this.addItem(source);
+                newItems[source.id] = source;
             }));
 
+        this.processNewItems(newItems);
+
         let activeItemId = Global.settings.get_string('active-source');
 
         // fallback to 'all' if we never saved any source, or if the saved



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