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



commit a9080451f0efd0ed6585efe8069773af59e202c6
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Fri Oct 14 13:29:30 2011 -0400

    documents: port to be a BaseManager subclass

 src/documents.js         |   89 +++++++++++++++++++--------------------------
 src/embed.js             |    6 ++--
 src/listView.js          |    4 +-
 src/mainToolbar.js       |    2 +-
 src/preview.js           |    4 +-
 src/trackerController.js |    2 +-
 src/view.js              |    2 +-
 7 files changed, 48 insertions(+), 61 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index aa66e1b..155fad3 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -34,6 +34,7 @@ const Signals = imports.signals;
 const Categories = imports.categories;
 const ChangeMonitor = imports.changeMonitor;
 const Global = imports.global;
+const Manager = imports.manager;
 const Path = imports.path;
 const Query = imports.query;
 const TrackerUtils = imports.trackerUtils;
@@ -47,7 +48,7 @@ function DocCommon(cursor) {
 
 DocCommon.prototype = {
     _init: function(cursor) {
-        this.urn = null;
+        this.id = null;
         this.uri = null;
         this.title = null;
         this.author = null;
@@ -72,13 +73,13 @@ DocCommon.prototype = {
         this._refreshIconId =
             Global.settings.connect('changed::list-view',
                                     Lang.bind(this, this.refreshIcon));
-        this._categoryId =
-            Global.categoryManager.connect('active-category-changed',
-                                           Lang.bind(this, this.refreshIcon));
+        this._filterId =
+            Global.sideFilterController.connect('changed',
+                                                Lang.bind(this, this.refreshIcon));
     },
 
     refresh: function() {
-        let sparql = Global.queryBuilder.buildSingleQuery(this.urn);
+        let sparql = Global.queryBuilder.buildSingleQuery(this.id);
 
         Global.connection.query_async(sparql, null, Lang.bind(this,
             function(object, res) {
@@ -107,7 +108,7 @@ DocCommon.prototype = {
 
     populateFromCursor: function(cursor) {
         this.uri = cursor.get_string(Query.QueryColumns.URI)[0];
-        this.urn = cursor.get_string(Query.QueryColumns.URN)[0];
+        this.id = cursor.get_string(Query.QueryColumns.URN)[0];
         this.author = cursor.get_string(Query.QueryColumns.AUTHOR)[0];
         this.resourceUrn = cursor.get_string(Query.QueryColumns.RESOURCE_URN)[0];
         this.favorite = cursor.get_boolean(Query.QueryColumns.FAVORITE);
@@ -178,11 +179,17 @@ DocCommon.prototype = {
     checkEffectsAndUpdateInfo: function() {
         let emblemIcons = [];
         let pixbuf = this.pixbuf;
+        let activeItem;
+
+        activeItem = Global.sideFilterController.getWhereItem();
 
         if (this.favorite &&
-            (Global.categoryManager.getActiveCategoryId() != Categories.StockCategories.FAVORITES))
+            (!activeItem ||
+             (activeItem.id != Categories.StockCategories.FAVORITES)))
             emblemIcons.push(this._createSymbolicEmblem('emblem-favorite'));
-        if (this.shared && (Global.categoryManager.getActiveCategoryId() != Categories.StockCategories.SHARED))
+        if (this.shared &&
+            (!activeItem ||
+             (activeItem.id != Categories.StockCategories.SHARED)))
             emblemIcons.push(this._createSymbolicEmblem('emblem-shared'));
 
         if (emblemIcons.length > 0) {
@@ -221,7 +228,7 @@ DocCommon.prototype = {
 
     destroy: function() {
         Global.settings.disconnect(this._refreshIconId);
-        Global.categoryManager.disconnect(this._categoryId);
+        Global.sideFilterController.disconnect(this._filterId);
     },
 
     open: function(screen, timestamp) {
@@ -229,7 +236,7 @@ DocCommon.prototype = {
     },
 
     setFavorite: function(favorite) {
-        TrackerUtils.setFavorite(this.urn, favorite, null);
+        TrackerUtils.setFavorite(this.id, favorite, null);
     }
 };
 Signals.addSignalMethods(DocCommon.prototype);
@@ -380,7 +387,7 @@ GoogleDocument.prototype = {
     },
 
     _createGDataEntry: function(cancellable, callback) {
-        let source = Global.sourceManager.getSourceByUrn(this.resourceUrn);
+        let source = Global.sourceManager.getItemById(this.resourceUrn);
 
         let authorizer = new Gd.GDataGoaAuthorizer({ goa_object: source.object });
         let service = new GData.DocumentsService({ authorizer: authorizer });
@@ -506,9 +513,10 @@ function DocumentManager() {
 }
 
 DocumentManager.prototype = {
+    __proto__: Manager.BaseManager.prototype,
+
     _init: function() {
-        this._docs = {};
-        this._activeDocument = null;
+        Manager.BaseManager.prototype._init.call(this);
 
         this._model = new DocumentModel();
 
@@ -523,20 +531,20 @@ DocumentManager.prototype = {
             let changeEvent = changes[idx];
 
             if (changeEvent.type == ChangeMonitor.ChangeEventType.CHANGED) {
-                let doc = this.lookupDocument(changeEvent.urn);
+                let doc = this.getItemById(changeEvent.urn);
 
                 if (doc)
                     doc.refresh();
             } else if (changeEvent.type == ChangeMonitor.ChangeEventType.CREATED) {
                 this._onDocumentCreated(changeEvent.urn);
             } else if (changeEvent.type == ChangeMonitor.ChangeEventType.DELETED) {
-                let doc = this.lookupDocument(changeEvent.urn);
+                let doc = this.getItemById(changeEvent.urn);
 
                 if (doc) {
                     this._model.documentRemoved(doc);
 
                     doc.destroy();
-                    delete this._docs[changeEvent.urn];
+                    this.removeItemById(changeEvent.urn);
                 }
             }
         }
@@ -555,7 +563,7 @@ DocumentManager.prototype = {
                         function(object, res) {
                             let valid = object.next_finish(res);
                             if (valid)
-                                this.addDocument(object);
+                                this.addDocumentFromCursor(object);
 
                             cursor.close();
                         }));
@@ -575,7 +583,7 @@ DocumentManager.prototype = {
         return this._pixbufFrame;
     },
 
-    addDocument: function(cursor) {
+    addDocumentFromCursor: function(cursor) {
         let identifier = cursor.get_string(Query.QueryColumns.IDENTIFIER)[0];
         let doc;
 
@@ -584,52 +592,31 @@ DocumentManager.prototype = {
         else
             doc = new LocalDocument(cursor);
 
-        this._docs[doc.urn] = doc;
+        this.addItem(doc);
         this._model.documentAdded(doc);
     },
 
     clear: function() {
-        for (idx in this._docs) {
-            this._docs[idx].destroy();
+        let items = this.getItems();
+        for (idx in items) {
+            items[idx].destroy();
         };
-        this._docs = {};
-        this._model.clear();
-    },
-
-    getDocuments: function() {
-        return this._docs;
-    },
-
-    lookupDocument: function(urn) {
-        let document = null;
 
-        if (this._docs[urn])
-            document = this._docs[urn];
-
-        return document;
+        Manager.BaseManager.prototype.clear.call(this);
+        this._model.clear();
     },
 
-    setActiveDocument: function(doc) {
-        if (doc == this._activeDocument)
-            return;
-
-        this._activeDocument = doc;
-
-        if (this._activeDocument != null) {
+    setActiveItem: function(doc) {
+        if (Manager.BaseManager.prototype.setActiveItem.call(this, doc)) {
             let recentManager = Gtk.RecentManager.get_default();
-            recentManager.add_item(this._activeDocument.uri);
+            recentManager.add_item(this.getActiveItem().uri);
         }
     },
 
-    getActiveDocument: function() {
-        return this._activeDocument;
-    },
-
     getModel: function() {
         return this._model;
     }
 };
-Signals.addSignalMethods(DocumentManager.prototype);
 
 const ModelColumns = {
     URN: 0,
@@ -659,7 +646,7 @@ DocumentModel.prototype = {
         let treePath = this.model.get_path(iter);
 
         Gd.store_set(this.model, iter,
-                     doc.urn,
+                     doc.id,
                      doc.title, doc.author,
                      doc.pixbuf, doc.mtime);
 
@@ -668,7 +655,7 @@ DocumentModel.prototype = {
                 let objectIter = this.model.get_iter(treePath)[1];
                 if (objectIter)
                     Gd.store_set(this.model, iter,
-                                 doc.urn,
+                                 doc.id,
                                  doc.title, doc.author,
                                  doc.pixbuf, doc.mtime);
             }));
@@ -679,7 +666,7 @@ DocumentModel.prototype = {
             function(model, path, iter) {
                 let urn = model.get_value(iter, ModelColumns.URN);
 
-                if (urn == doc.urn) {
+                if (urn == doc.id) {
                     this.model.remove(iter);
                     return true;
                 }
diff --git a/src/embed.js b/src/embed.js
index 09669b5..585317b 100644
--- a/src/embed.js
+++ b/src/embed.js
@@ -152,8 +152,8 @@ ViewEmbed.prototype  = {
             this._loaderTimeout = 0;
         }
 
-        let doc = Global.documentManager.lookupDocument(urn);
-        Global.documentManager.setActiveDocument(doc);
+        let doc = Global.documentManager.getItemById(urn);
+        Global.documentManager.setActiveItem(doc);
 
         this._loaderTimeout = Mainloop.timeout_add(_PDF_LOADER_TIMEOUT,
             Lang.bind(this, this._onPdfLoaderTimeout));
@@ -247,7 +247,7 @@ ViewEmbed.prototype  = {
         this._document = null;
         this._stage = null;
 
-        Global.documentManager.setActiveDocument(null);
+        Global.documentManager.setActiveItem(null);
 
         this._scrolledWin = new Gtk.ScrolledWindow({ hexpand: true,
                                                      vexpand: true,
diff --git a/src/listView.js b/src/listView.js
index dae6273..f291ad6 100644
--- a/src/listView.js
+++ b/src/listView.js
@@ -105,7 +105,7 @@ ListView.prototype = {
         col.set_cell_data_func(typeRenderer, Lang.bind(this,
             function(col, cell, model, iter) {
                 let urn = model.get_value(iter, Documents.ModelColumns.URN);
-                let doc = Global.documentManager.lookupDocument(urn);
+                let doc = Global.documentManager.getItemById(urn);
 
                 typeRenderer.text = doc.typeDescription;
             }));
@@ -116,7 +116,7 @@ ListView.prototype = {
         col.set_cell_data_func(whereRenderer, Lang.bind(this,
             function(col, cell, model, iter) {
                 let urn = model.get_value(iter, Documents.ModelColumns.URN);
-                let doc = Global.documentManager.lookupDocument(urn);
+                let doc = Global.documentManager.getItemById(urn);
 
                 whereRenderer.text = doc.sourceName;
             }));
diff --git a/src/mainToolbar.js b/src/mainToolbar.js
index dec0c06..83138fd 100644
--- a/src/mainToolbar.js
+++ b/src/mainToolbar.js
@@ -187,7 +187,7 @@ MainToolbar.prototype = {
 
     _updateModelLabels: function() {
         let pageLabel = null;
-        let doc = Global.documentManager.getActiveDocument();
+        let doc = Global.documentManager.getActiveItem();
 
         let titleLabel = ('<b>%s</b>').format(GLib.markup_escape_text(doc.title, -1));
         this._titleLabel.set_markup(titleLabel);
diff --git a/src/preview.js b/src/preview.js
index 9762f9d..49e0ff3 100644
--- a/src/preview.js
+++ b/src/preview.js
@@ -53,8 +53,8 @@ PreviewView.prototype = {
         if (button != 3)
             return false;
 
-        let doc = Global.documentManager.getActiveDocument();
-        let menu = new View.ContextMenu([ doc.urn ]);
+        let doc = Global.documentManager.getActiveItem();
+        let menu = new View.ContextMenu([ doc.id ]);
 
         menu.widget.popup_for_device(null, null, null, null, null, null, button, timestamp);
 
diff --git a/src/trackerController.js b/src/trackerController.js
index 946715d..bae43e4 100644
--- a/src/trackerController.js
+++ b/src/trackerController.js
@@ -96,7 +96,7 @@ TrackerController.prototype = {
             return;
         }
 
-        Global.documentManager.addDocument(cursor);
+        Global.documentManager.addDocumentFromCursor(cursor);
         cursor.next_async(null, Lang.bind(this, this._onCursorNext));
     },
 
diff --git a/src/view.js b/src/view.js
index d3a7acf..629cdd2 100644
--- a/src/view.js
+++ b/src/view.js
@@ -48,7 +48,7 @@ ContextMenu.prototype = {
 
         urns.forEach(Lang.bind(this,
             function(urn) {
-                let doc = Global.documentManager.lookupDocument(urn);
+                let doc = Global.documentManager.getItemById(urn);
                 if (doc.favorite)
                     favCount++;
 



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