[gnome-documents] all: refactor document model/controller



commit f5b0e0a5b08529030638f7bbf251ce9cc4581538
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Fri Aug 26 15:33:58 2011 -0400

    all: refactor document model/controller
    
    Make also the main model follow the model/view/controller of the other
    components.

 src/Makefile-js.am                            |    4 +-
 src/application.js                            |    6 +-
 src/{docFactory.js => documents.js}           |   81 ++++++++++++++++++++----
 src/global.js                                 |    3 +-
 src/iconView.js                               |    8 +-
 src/listView.js                               |    8 +-
 src/query.js                                  |   13 ++++
 src/{trackerModel.js => trackerController.js} |   62 ++-----------------
 src/utils.js                                  |    4 +-
 src/view.js                                   |   17 +++---
 10 files changed, 113 insertions(+), 93 deletions(-)
---
diff --git a/src/Makefile-js.am b/src/Makefile-js.am
index 09af567..7768ad1 100644
--- a/src/Makefile-js.am
+++ b/src/Makefile-js.am
@@ -3,7 +3,7 @@ dist_js_DATA = \
     application.js \
     categories.js \
     changeMonitor.js \
-    docFactory.js \
+    documents.js \
     filterController.js \
     gDataMiner.js \
     global.js \
@@ -20,7 +20,7 @@ dist_js_DATA = \
     sidebar.js \
     sources.js \
     spinnerBox.js \
-    trackerModel.js \
+    trackerController.js \
     trackerUtils.js \
     utils.js \
     view.js \
diff --git a/src/application.js b/src/application.js
index a7d8660..add7223 100644
--- a/src/application.js
+++ b/src/application.js
@@ -32,6 +32,7 @@ const Tracker = imports.gi.Tracker;
 
 const Categories = imports.categories;
 const ChangeMonitor = imports.changeMonitor;
+const Documents = imports.documents;
 const FilterController = imports.filterController;
 const Format = imports.format;
 const Global = imports.global;
@@ -42,7 +43,7 @@ const Path = imports.path;
 const Query = imports.query;
 const SelectionController = imports.selectionController;
 const Sources = imports.sources;
-const TrackerModel = imports.trackerModel;
+const TrackerController = imports.trackerController;
 
 const _GD_DBUS_PATH = '/org/gnome/Documents';
 
@@ -131,7 +132,8 @@ Application.prototype = {
     _onSourceManagerCreated: function() {
         Global.selectionController = new SelectionController.SelectionController();
         Global.queryBuilder = new Query.QueryBuilder();
-        Global.model = new TrackerModel.TrackerModel();
+        Global.documentManager = new Documents.DocumentManager();
+        Global.trackerController = new TrackerController.TrackerController();
         Global.changeMonitor = new ChangeMonitor.TrackerChangeMonitor();
 
         this._mainWindow = new MainWindow.MainWindow();
diff --git a/src/docFactory.js b/src/documents.js
similarity index 78%
rename from src/docFactory.js
rename to src/documents.js
index 0b63a66..a1d59ff 100644
--- a/src/docFactory.js
+++ b/src/documents.js
@@ -29,7 +29,7 @@ const Signals = imports.signals;
 
 const ChangeMonitor = imports.changeMonitor;
 const Global = imports.global;
-const TrackerModel = imports.trackerModel;
+const Query = imports.query;
 const Utils = imports.utils;
 
 function DocCommon(cursor) {
@@ -86,14 +86,14 @@ DocCommon.prototype = {
     },
 
     _populateFromCursor: function(cursor) {
-        this.urn = cursor.get_string(TrackerModel.TrackerColumns.URN)[0];
-        this.title = cursor.get_string(TrackerModel.TrackerColumns.TITLE)[0];
-        this.author = cursor.get_string(TrackerModel.TrackerColumns.AUTHOR)[0];
-        this.mtime = cursor.get_string(TrackerModel.TrackerColumns.MTIME)[0];
-        this.resourceUrn = cursor.get_string(TrackerModel.TrackerColumns.RESOURCE_URN)[0];
-        this.favorite = cursor.get_boolean(TrackerModel.TrackerColumns.FAVORITE);
-
-        this._type = cursor.get_string(TrackerModel.TrackerColumns.TYPE)[0];
+        this.urn = cursor.get_string(Query.QueryColumns.URN)[0];
+        this.title = cursor.get_string(Query.QueryColumns.TITLE)[0];
+        this.author = cursor.get_string(Query.QueryColumns.AUTHOR)[0];
+        this.mtime = cursor.get_string(Query.QueryColumns.MTIME)[0];
+        this.resourceUrn = cursor.get_string(Query.QueryColumns.RESOURCE_URN)[0];
+        this.favorite = cursor.get_boolean(Query.QueryColumns.FAVORITE);
+
+        this._type = cursor.get_string(Query.QueryColumns.TYPE)[0];
         this.pixbuf = Utils.pixbufFromRdfType(this._type);
 
         // sanitize
@@ -149,7 +149,7 @@ LocalDocument.prototype = {
 
     _init: function(cursor) {
         // overridden
-        this.uri = cursor.get_string(TrackerModel.TrackerColumns.URI)[0];
+        this.uri = cursor.get_string(Query.QueryColumns.URI)[0];
 
         DocCommon.prototype._init.call(this, cursor);
     },
@@ -248,17 +248,17 @@ GoogleDocument.prototype = {
 
     _init: function(cursor) {
         // overridden
-        this.uri = cursor.get_string(TrackerModel.TrackerColumns.IDENTIFIER)[0];
+        this.uri = cursor.get_string(Query.QueryColumns.IDENTIFIER)[0];
 
         DocCommon.prototype._init.call(this, cursor);
     }
 };
 
-function DocFactory() {
+function DocumentManager() {
     this._init();
 }
 
-DocFactory.prototype = {
+DocumentManager.prototype = {
     _init: function() {
         this._docs = [];
     },
@@ -269,7 +269,7 @@ DocFactory.prototype = {
     },
 
     newDocument: function(cursor) {
-        let identifier = cursor.get_string(TrackerModel.TrackerColumns.IDENTIFIER)[0];
+        let identifier = cursor.get_string(Query.QueryColumns.IDENTIFIER)[0];
         let doc;
 
         if (this._identifierIsGoogle(identifier))
@@ -278,6 +278,7 @@ DocFactory.prototype = {
             doc = new LocalDocument(cursor);
 
         this._docs.push(doc);
+        this.emit('new-document', doc);
 
         return doc;
     },
@@ -287,5 +288,57 @@ DocFactory.prototype = {
             doc.destroy();
         });
         this._docs = [];
+        this.emit('clear');
     }
 };
+Signals.addSignalMethods(DocumentManager.prototype);
+
+const ModelColumns = {
+    URN: 0,
+    URI: 1,
+    TITLE: 2,
+    AUTHOR: 3,
+    MTIME: 4,
+    ICON: 5,
+    RESOURCE_URN: 6,
+    FAVORITE: 7
+};
+
+function DocumentModel() {
+    this._init();
+}
+
+DocumentModel.prototype = {
+    _init: function() {
+        this.model = Gd.create_list_store();
+        this._documentManager = Global.documentManager;
+        this._documentManager.connect('clear', Lang.bind(this, this._onManagerClear));
+        this._documentManager.connect('new-document', Lang.bind(this, this._onNewDocument));
+    },
+
+    _onManagerClear: function() {
+        this.model.clear();
+    },
+
+    _onNewDocument: function(manager, doc) {
+        let iter = this.model.append();
+        let treePath = this.model.get_path(iter);
+
+        Gd.store_set(this.model, iter,
+                     doc.urn, doc.uri,
+                     doc.title, doc.author,
+                     doc.mtime, doc.pixbuf,
+                     doc.resourceUrn, doc.favorite);
+
+        doc.connect('info-updated', Lang.bind(this,
+            function() {
+                let objectIter = this.model.get_iter(treePath)[1];
+                if (objectIter)
+                    Gd.store_set(this.model, iter,
+                                 doc.urn, doc.uri,
+                                 doc.title, doc.author,
+                                 doc.mtime, doc.pixbuf,
+                                 doc.resourceUrn, doc.favorite);
+            }));
+    }
+};
\ No newline at end of file
diff --git a/src/global.js b/src/global.js
index 5e0450c..a5a2c03 100644
--- a/src/global.js
+++ b/src/global.js
@@ -22,9 +22,10 @@
 let application = null;
 let categoryManager = null;
 let connection = null;
-let model = null;
+let documentManager = null;
 let offsetController = null;
 let queryBuilder = null;
 let selectionController = null;
 let settings = null;
 let sourceManager = null;
+let trackerController = null;
diff --git a/src/iconView.js b/src/iconView.js
index 2d51a1e..ff18513 100644
--- a/src/iconView.js
+++ b/src/iconView.js
@@ -23,7 +23,7 @@ const Gtk = imports.gi.Gtk;
 const Gd = imports.gi.Gd;
 const Pango = imports.gi.Pango;
 
-const TrackerModel = imports.trackerModel;
+const Documents = imports.documents;
 const View = imports.view;
 const Lang = imports.lang;
 
@@ -80,7 +80,7 @@ IconView.prototype = {
 
         this.widget.pack_start(pixbufRenderer, false);
         this.widget.add_attribute(pixbufRenderer,
-                                'pixbuf', TrackerModel.ModelColumns.ICON);
+                                'pixbuf', Documents.ModelColumns.ICON);
 
         let textRenderer =
             new Gd.TwoLinesRenderer({ alignment: Pango.Alignment.CENTER,
@@ -91,9 +91,9 @@ IconView.prototype = {
                                       text_lines: 3 });
         this.widget.pack_start(textRenderer, false);
         this.widget.add_attribute(textRenderer,
-                                  'text', TrackerModel.ModelColumns.TITLE);
+                                  'text', Documents.ModelColumns.TITLE);
         this.widget.add_attribute(textRenderer,
-                                  'line-two', TrackerModel.ModelColumns.AUTHOR);
+                                  'line-two', Documents.ModelColumns.AUTHOR);
     },
 
     _onItemActivated: function(view, path, column) {
diff --git a/src/listView.js b/src/listView.js
index 7b20eaa..e4e7af6 100644
--- a/src/listView.js
+++ b/src/listView.js
@@ -23,7 +23,7 @@ const Gtk = imports.gi.Gtk;
 const Gd = imports.gi.Gd;
 const Pango = imports.gi.Pango;
 
-const TrackerModel = imports.trackerModel;
+const Documents = imports.documents;
 const View = imports.view;
 const Lang = imports.lang;
 
@@ -81,7 +81,7 @@ ListView.prototype = {
 
         col.pack_start(pixbufRenderer, false);
         col.add_attribute(pixbufRenderer,
-                          'pixbuf', TrackerModel.ModelColumns.ICON);
+                          'pixbuf', Documents.ModelColumns.ICON);
 
         let textRenderer =
             new Gd.TwoLinesRenderer({ alignment: Pango.Alignment.CENTER,
@@ -92,8 +92,8 @@ ListView.prototype = {
                                       text_lines: 3 });
         col.pack_start(textRenderer, false);
         col.add_attribute(textRenderer,
-                          'text', TrackerModel.ModelColumns.TITLE);
+                          'text', Documents.ModelColumns.TITLE);
         col.add_attribute(textRenderer,
-                          'line-two', TrackerModel.ModelColumns.AUTHOR);
+                          'line-two', Documents.ModelColumns.AUTHOR);
     }
 };
diff --git a/src/query.js b/src/query.js
index d25f9e3..40dbbcb 100644
--- a/src/query.js
+++ b/src/query.js
@@ -21,6 +21,19 @@
 
 const Global = imports.global;
 
+const QueryColumns = {
+    URN: 0,
+    URI: 1,
+    TITLE: 2,
+    AUTHOR: 3,
+    MTIME: 4,
+    IDENTIFIER: 5,
+    TYPE: 6,
+    RESOURCE_URN: 7,
+    FAVORITE: 8,
+    TOTAL_COUNT: 9 // only in global query
+};
+
 function QueryBuilder() {
     this._init();
 }
diff --git a/src/trackerModel.js b/src/trackerController.js
similarity index 74%
rename from src/trackerModel.js
rename to src/trackerController.js
index 015703e..8432ab1 100644
--- a/src/trackerModel.js
+++ b/src/trackerController.js
@@ -30,49 +30,20 @@ const Tracker = imports.gi.Tracker;
 const Gd = imports.gi.Gd;
 const Gtk = imports.gi.Gtk;
 
-const DocFactory = imports.docFactory;
 const GDataMiner = imports.gDataMiner;
 const Global = imports.global;
 const Query = imports.query;
 const TrackerUtils = imports.trackerUtils;
 const Utils = imports.utils;
 
-const ModelColumns = {
-    URN: 0,
-    URI: 1,
-    TITLE: 2,
-    AUTHOR: 3,
-    MTIME: 4,
-    ICON: 5,
-    RESOURCE_URN: 6,
-    FAVORITE: 7
-};
-
 const MINER_REFRESH_TIMEOUT = 60; /* seconds */
 
-const TrackerColumns = {
-    URN: 0,
-    URI: 1,
-    TITLE: 2,
-    AUTHOR: 3,
-    MTIME: 4,
-    IDENTIFIER: 5,
-    TYPE: 6,
-    RESOURCE_URN: 7,
-    FAVORITE: 8,
-    TOTAL_COUNT: 9
-};
-
-function TrackerModel() {
+function TrackerController() {
     this._init();
 }
 
-TrackerModel.prototype = {
+TrackerController.prototype = {
     _init: function() {
-        this._factory = new DocFactory.DocFactory();
-
-        this.model = Gd.create_list_store();
-
         // startup a refresh of the gdocs cache
         this._miner = new GDataMiner.GDataMiner();
         this._refreshMinerNow();
@@ -118,28 +89,8 @@ TrackerModel.prototype = {
     },
 
     _addRowFromCursor: function(cursor) {
-        this._offsetController.setItemCount(cursor.get_integer(TrackerColumns.TOTAL_COUNT));
-
-        let newDoc = this._factory.newDocument(cursor);
-        let iter = this.model.append();
-        let treePath = this.model.get_path(iter);
-
-        Gd.store_set(this.model, iter,
-                     newDoc.urn, newDoc.uri,
-                     newDoc.title, newDoc.author,
-                     newDoc.mtime, newDoc.pixbuf,
-                     newDoc.resourceUrn, newDoc.favorite);
-
-        newDoc.connect('info-updated', Lang.bind(this,
-            function() {
-                let objectIter = this.model.get_iter(treePath)[1];
-                if (objectIter)
-                    Gd.store_set(this.model, iter,
-                                 newDoc.urn, newDoc.uri,
-                                 newDoc.title, newDoc.author,
-                                 newDoc.mtime, newDoc.pixbuf,
-                                 newDoc.resourceUrn, newDoc.favorite);
-            }));
+        this._offsetController.setItemCount(cursor.get_integer(Query.QueryColumns.TOTAL_COUNT));
+        Global.documentManager.newDocument(cursor);
     },
 
     _onQueryFinished: function() {
@@ -189,8 +140,7 @@ TrackerModel.prototype = {
 
     _refresh: function() {
         Global.selectionController.freezeSelection(true);
-        this.model.clear();
-        this._factory.clear();
+        Global.documentManager.clear();
 
         this._performCurrentQuery();
     },
@@ -200,4 +150,4 @@ TrackerModel.prototype = {
         this._refresh();
     }
 };
-Signals.addSignalMethods(TrackerModel.prototype);
+Signals.addSignalMethods(TrackerController.prototype);
diff --git a/src/utils.js b/src/utils.js
index 0299b20..74afe81 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -21,8 +21,8 @@
 
 const Gtk = imports.gi.Gtk;
 
+const Documents = imports.documents;
 const Global = imports.global;
-const TrackerModel = imports.trackerModel;
 
 const Lang = imports.lang;
 
@@ -65,7 +65,7 @@ function getURNsFromPaths(paths, model) {
     return paths.map(Lang.bind(this,
             function(path) {
                 let iter = model.get_iter(path)[1];
-                let urn = model.get_value(iter, TrackerModel.ModelColumns.URN);
+                let urn = model.get_value(iter, Documents.ModelColumns.URN);
 
                 return urn;
             }));
diff --git a/src/view.js b/src/view.js
index 8522971..a567269 100644
--- a/src/view.js
+++ b/src/view.js
@@ -26,8 +26,8 @@ const _ = imports.gettext.gettext;
 const Lang = imports.lang;
 const Signals = imports.signals;
 
+const Documents = imports.documents;
 const Global = imports.global;
-const TrackerModel = imports.trackerModel;
 const TrackerUtils = imports.trackerUtils;
 const Utils = imports.utils;
 
@@ -64,9 +64,10 @@ View.prototype = {
     _init: function() {
         this._selectedURNs = null;
 
-        this.model = Global.model;
-        this._treeModel = Global.model.model;
+        this._model = new Documents.DocumentModel();
+        this._treeModel = this._model.model;
         this.widget.set_model(this._treeModel);
+
         this.widget.connect('destroy', Lang.bind(this,
             function() {
                 Global.selectionController.disconnect(this._selectionControllerId);
@@ -94,7 +95,7 @@ View.prototype = {
 
         this._treeModel.foreach(Lang.bind(this,
             function(model, path, iter) {
-                let urn = this._treeModel.get_value(iter, TrackerModel.ModelColumns.URN);
+                let urn = this._treeModel.get_value(iter, Documents.ModelColumns.URN);
                 let urnIndex = selected.indexOf(urn);
 
                 if (urnIndex != -1) {
@@ -126,8 +127,8 @@ View.prototype = {
         let path = this.getPathAtPos(position);
         let iter = this._treeModel.get_iter(path)[1];
 
-        let urn = this._treeModel.get_value(iter, TrackerModel.ModelColumns.URN);
-        let isFavorite = this._treeModel.get_value(iter, TrackerModel.ModelColumns.FAVORITE);
+        let urn = this._treeModel.get_value(iter, Documents.ModelColumns.URN);
+        let isFavorite = this._treeModel.get_value(iter, Documents.ModelColumns.FAVORITE);
 
         let menu = new ContextMenu(urn, isFavorite);
         menu.widget.popup_for_device(null, null, null, null, null, null, button, timestamp);
@@ -142,8 +143,8 @@ View.prototype = {
 
     activateItem: function(path) {
         let iter = this._treeModel.get_iter(path)[1];
-        let uri = this._treeModel.get_value(iter, TrackerModel.ModelColumns.URI);
-        let resource = this._treeModel.get_value(iter, TrackerModel.ModelColumns.RESOURCE_URN);
+        let uri = this._treeModel.get_value(iter, Documents.ModelColumns.URI);
+        let resource = this._treeModel.get_value(iter, Documents.ModelColumns.RESOURCE_URN);
 
         this.emit('item-activated', uri, resource);
     }



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