[gnome-documents] collections: port to be a BaseManager subclass



commit 3b14e402599c3a303382b54c0ec3702dfd80fea7
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Fri Oct 14 13:28:28 2011 -0400

    collections: port to be a BaseManager subclass

 src/collections.js |   76 ++++++++++++++++++++++++++++-----------------------
 src/sidebar.js     |   10 +++---
 2 files changed, 47 insertions(+), 39 deletions(-)
---
diff --git a/src/collections.js b/src/collections.js
index 88fc94c..cfaac35 100644
--- a/src/collections.js
+++ b/src/collections.js
@@ -23,6 +23,7 @@ const Lang = imports.lang;
 const Signals = imports.signals;
 
 const Global = imports.global;
+const Manager = imports.manager;
 
 const CollectionQueryColumns = {
     URN: 0,
@@ -35,15 +36,15 @@ function Collection(params) {
 
 Collection.prototype = {
     _init: function(params) {
-        this.urn = null;
-        this.name = null;
-
         if (params.cursor) {
             let cursor = params.cursor;
 
-            this.urn = cursor.get_string(CollectionQueryColumns.URN)[0];
+            this.id = cursor.get_string(CollectionQueryColumns.URN)[0];
             this.name = cursor.get_string(CollectionQueryColumns.NAME)[0];
         }
+
+        // TODO add icon for remote categories
+        this.icon = '';
     },
 
     getWhere: function() {
@@ -57,13 +58,28 @@ function CollectionManager() {
 };
 
 CollectionManager.prototype = {
+    __proto__: Manager.BaseManager.prototype,
+
     _init: function() {
-        this._collections = {};
+        Manager.BaseManager.prototype._init.call(this);
+        this._newItems = {};
+
+        // we want to only display collections for the current source,
+        // so refresh the list when the active source changes.
+        Global.sourceManager.connect('active-changed',
+                                     Lang.bind(this, this._refreshCollections));
+
+        this._refreshCollections();
 
+        // TODO: keep track changes from the tracker store
+    },
+
+    _refreshCollections: function() {
         let sparql = 'SELECT ?urn nie:title(?urn) WHERE { ' +
             '{ ?urn a nfo:DataContainer } ' +
             '{ ?doc nie:isPartOf ?urn } ' +
-            'FILTER ((fn:starts-with (nao:identifier(?urn), "gd:collection"))' +
+            'FILTER ((fn:starts-with (nao:identifier(?urn), "gd:collection")) &&' +
+            Global.sourceManager.getFilter() +
             ')}';
 
         Global.connection.query_async(sparql, null, Lang.bind(this,
@@ -77,10 +93,6 @@ CollectionManager.prototype = {
             }));
     },
 
-    _onChangesPending: function() {
-
-    },
-
     _onCursorNext: function(cursor, res) {
         try {
             let valid = cursor.next_finish(res);
@@ -91,41 +103,37 @@ CollectionManager.prototype = {
             } else {
                 // process all the items we collected
                 cursor.close();
+                this._refreshItems();
             }
         } catch (e) {
             log('Unable to query the collection list: ' + e.toString());
             cursor.close();
+            this._refreshItems();
         }
     },
 
-    _addCollectionFromCursor: function(cursor) {
-        let urn = cursor.get_string(CollectionQueryColumns.URN)[0];
-        let collection = this.getCollectionByUrn(urn);
-
-        if (collection != null) {
-            collection.updateFromCursor(cursor);
-        } else {
-            collection = new Collection({ cursor: cursor });
-            this._addCollection(collection);
+    _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]);
         }
-    },
 
-    _addCollection: function(collection) {
-        this._collections[collection.urn] = collection;
-        this.emit('collection-added', collection);
-    },
+        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]);
+        }
 
-    getCollections: function() {
-        return this._collections;
+        this._newItems = {};
     },
 
-    getCollectionByUrn: function(urn) {
-        let retval = this._collections[urn];
-
-        if (!retval)
-            retval = null;
-
-        return retval;
+    _addCollectionFromCursor: function(cursor) {
+        let collection = new Collection({ cursor: cursor });
+        this._newItems[collection.id] = collection;
     }
 };
-Signals.addSignalMethods(CollectionManager.prototype);
diff --git a/src/sidebar.js b/src/sidebar.js
index 0cc32f2..96faf15 100644
--- a/src/sidebar.js
+++ b/src/sidebar.js
@@ -62,7 +62,7 @@ SidebarModel.prototype = {
         this.model = Gd.create_sidebar_store();
         this.model.set_default_sort_func(Lang.bind(this, this._modelSortFunc));
 
-        this._collectionsId = Global.collectionManager.connect('collection-added', Lang.bind(this,
+        this._collectionsId = Global.collectionManager.connect('item-added', Lang.bind(this,
             this._addCollection));
 
         // insert categories
@@ -81,9 +81,9 @@ SidebarModel.prototype = {
                              '', '', '',
                              _('Collections'), SidebarModelSections.COLLECTIONS);
 
-        let collections = Global.collectionManager.getCollections();
-        for (idx in collections) {
-            let collection = collections[idx];
+        items = Global.collectionManager.getItems();
+        for (idx in items) {
+            let collection = items[idx];
             this._addCollection(Global.collectionManager, collection);
         }
     },
@@ -119,7 +119,7 @@ SidebarModel.prototype = {
     _addCollection: function(manager, collection) {
         let iter = this.model.append();
         Gd.sidebar_store_set(this.model, iter,
-                             collection.urn, collection.name, '',
+                             collection.id, collection.name, '',
                              '', SidebarModelSections.COLLECTIONS);
     }
 };



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