[gnome-documents] view: move DocumentsModel to ViewModel



commit cdbf09f1dd6c8bb4d14e3e0f2ff289fd80986f0d
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue Jul 17 15:12:15 2012 -0400

    view: move DocumentsModel to ViewModel
    
    There's no need for DocumentManager to own the view GtkTreeModel; have
    the view create it, and let it update automatically when the underlying
    Manager changes. For this, add a 'clear' signal to it.

 src/documents.js |   68 -----------------------------------------
 src/manager.js   |    1 +
 src/view.js      |   89 +++++++++++++++++++++++++++++++++++++++++++++++++----
 3 files changed, 83 insertions(+), 75 deletions(-)
---
diff --git a/src/documents.js b/src/documents.js
index 738e89e..4f2531e 100644
--- a/src/documents.js
+++ b/src/documents.js
@@ -913,7 +913,6 @@ const DocumentManager = new Lang.Class({
     _init: function() {
         this.parent();
 
-        this._model = new DocumentModel();
 
         Global.changeMonitor.connect('changes-pending',
                                      Lang.bind(this, this._onChangesPending));
@@ -934,8 +933,6 @@ const DocumentManager = new Lang.Class({
                 let doc = this.getItemById(changeEvent.urn);
 
                 if (doc) {
-                    this._model.documentRemoved(doc);
-
                     doc.destroy();
                     this.removeItemById(changeEvent.urn);
 
@@ -984,7 +981,6 @@ const DocumentManager = new Lang.Class({
     addDocumentFromCursor: function(cursor) {
         let doc = this.createDocumentFromCursor(cursor);
         this.addItem(doc);
-        this._model.documentAdded(doc);
 
         if (doc.collection)
             Global.collectionManager.addItem(doc);
@@ -999,7 +995,6 @@ const DocumentManager = new Lang.Class({
         };
 
         this.parent();
-        this._model.clear();
     },
 
     setActiveItem: function(doc) {
@@ -1016,69 +1011,6 @@ const DocumentManager = new Lang.Class({
 
         let recentManager = Gtk.RecentManager.get_default();
         recentManager.add_item(doc.uri);
-    },
-
-    getModel: function() {
-        return this._model;
-    }
-});
-
-const DocumentModel = new Lang.Class({
-    Name: 'DocumentModel',
-
-    _init: function() {
-        this.model = Gtk.ListStore.new(
-            [ GObject.TYPE_STRING,
-              GObject.TYPE_STRING,
-              GObject.TYPE_STRING,
-              GObject.TYPE_STRING,
-              GdkPixbuf.Pixbuf,
-              GObject.TYPE_LONG,
-              GObject.TYPE_BOOLEAN ]);
-        this.model.set_sort_column_id(Gd.MainColumns.MTIME,
-                                      Gtk.SortType.DESCENDING);
-    },
-
-    clear: function() {
-        this.model.clear();
-    },
 
-    documentAdded: function(doc) {
-        let iter = this.model.append();
-        this.model.set(iter,
-            [ 0, 1, 2, 3, 4, 5 ],
-            [ doc.id, doc.uri, doc.name,
-              doc.author, doc.pixbuf, doc.mtime ]);
-
-        let treePath = this.model.get_path(iter);
-        let treeRowRef = Gtk.TreeRowReference.new(this.model, treePath);
-
-        doc.connect('info-updated', Lang.bind(this,
-            function() {
-                let objectPath = treeRowRef.get_path();
-                if (!objectPath)
-                    return;
-
-                let objectIter = this.model.get_iter(objectPath)[1];
-                if (objectIter)
-                    this.model.set(objectIter,
-                        [ 0, 1, 2, 3, 4, 5 ],
-                        [ doc.id, doc.uri, doc.name,
-                          doc.author, doc.pixbuf, doc.mtime ]);
-            }));
-    },
-
-    documentRemoved: function(doc) {
-        this.model.foreach(Lang.bind(this,
-            function(model, path, iter) {
-                let id = model.get_value(iter, Gd.MainColumns.ID);
-
-                if (id == doc.id) {
-                    this.model.remove(iter);
-                    return true;
-                }
-
-                return false;
-            }));
     }
 });
diff --git a/src/manager.js b/src/manager.js
index 5ecffe8..2a3e75d 100644
--- a/src/manager.js
+++ b/src/manager.js
@@ -102,6 +102,7 @@ const BaseManager = new Lang.Class({
     clear: function() {
         this._items = {};
         this._activeItem = null;
+        this.emit('clear');
     },
 
     getFilter: function() {
diff --git a/src/view.js b/src/view.js
index 7523bda..2eb7375 100644
--- a/src/view.js
+++ b/src/view.js
@@ -21,8 +21,10 @@
 
 const Gd = imports.gi.Gd;
 const Gdk = imports.gi.Gdk;
+const GdkPixbuf = imports.gi.GdkPixbuf;
 const Gettext = imports.gettext;
 const GLib = imports.gi.GLib;
+const GObject = imports.gi.GObject;
 const Gtk = imports.gi.Gtk;
 const _ = imports.gettext.gettext;
 
@@ -156,6 +158,79 @@ const ContextMenu = new Lang.Class({
     }
 });
 
+const ViewModel = new Lang.Class({
+    Name: 'ViewModel',
+
+    _init: function() {
+        this.model = Gtk.ListStore.new(
+            [ GObject.TYPE_STRING,
+              GObject.TYPE_STRING,
+              GObject.TYPE_STRING,
+              GObject.TYPE_STRING,
+              GdkPixbuf.Pixbuf,
+              GObject.TYPE_LONG,
+              GObject.TYPE_BOOLEAN ]);
+        this.model.set_sort_column_id(Gd.MainColumns.MTIME,
+                                      Gtk.SortType.DESCENDING);
+
+        Global.documentManager.connect('item-added',
+            Lang.bind(this, this._onItemAdded));
+        Global.documentManager.connect('item-removed',
+            Lang.bind(this, this._onItemRemoved));
+        Global.documentManager.connect('clear',
+            Lang.bind(this, this._onClear));
+
+        // populate with the intial items
+        let items = Global.documentManager.getItems();
+        for (let idx in items) {
+            this._onItemAdded(null, items[idx]);
+        }
+    },
+
+    _onClear: function() {
+        this.model.clear();
+    },
+
+    _onItemAdded: function(source, doc) {
+        let iter = this.model.append();
+        this.model.set(iter,
+            [ 0, 1, 2, 3, 4, 5 ],
+            [ doc.id, doc.uri, doc.name,
+              doc.author, doc.pixbuf, doc.mtime ]);
+
+        let treePath = this.model.get_path(iter);
+        let treeRowRef = Gtk.TreeRowReference.new(this.model, treePath);
+
+        doc.connect('info-updated', Lang.bind(this,
+            function() {
+                let objectPath = treeRowRef.get_path();
+                if (!objectPath)
+                    return;
+
+                let objectIter = this.model.get_iter(objectPath)[1];
+                if (objectIter)
+                    this.model.set(objectIter,
+                        [ 0, 1, 2, 3, 4, 5 ],
+                        [ doc.id, doc.uri, doc.name,
+                          doc.author, doc.pixbuf, doc.mtime ]);
+            }));
+    },
+
+    _onItemRemoved: function(source, doc) {
+        this.model.foreach(Lang.bind(this,
+            function(model, path, iter) {
+                let id = model.get_value(iter, Gd.MainColumns.ID);
+
+                if (id == doc.id) {
+                    this.model.remove(iter);
+                    return true;
+                }
+
+                return false;
+            }));
+    }
+});
+
 const ViewContainer = new Lang.Class({
     Name: 'ViewContainer',
 
@@ -164,6 +239,8 @@ const ViewContainer = new Lang.Class({
         this._adjustmentChangedId = 0;
         this._scrollbarVisibleId = 0;
 
+        this._model = new ViewModel();
+
         this.widget = new Gtk.Grid({ orientation: Gtk.Orientation.VERTICAL });
         this.view = new Gd.MainView();
         this.widget.add(this.view);
@@ -298,8 +375,7 @@ const ViewContainer = new Lang.Class({
 
         if (!status) {
             // setup a model if we're not querying
-            this._treeModel = Global.documentManager.getModel().model;
-            this.view.set_model(this._treeModel);
+            this.view.set_model(this._model.model);
 
             // unfreeze selection
             Global.selectionController.freezeSelection(false);
@@ -310,7 +386,6 @@ const ViewContainer = new Lang.Class({
 
             // if we're querying, clear the model from the view,
             // so that we don't uselessly refresh the rows
-            this._treeModel = null;
             this.view.set_model(null);
         }
     },
@@ -324,13 +399,13 @@ const ViewContainer = new Lang.Class({
 
         let generic = this.view.get_generic_view();
         let first = true;
-        this._treeModel.foreach(Lang.bind(this,
+        this._model.model.foreach(Lang.bind(this,
             function(model, path, iter) {
-                let id = this._treeModel.get_value(iter, Gd.MainColumns.ID);
+                let id = this._model.model.get_value(iter, Gd.MainColumns.ID);
                 let idIndex = selected.indexOf(id);
 
                 if (idIndex != -1) {
-                    this._treeModel.set_value(iter, Gd.MainColumns.SELECTED, true);
+                    this._model.model.set_value(iter, Gd.MainColumns.SELECTED, true);
                     newSelection.push(id);
 
                     if (first) {
@@ -356,7 +431,7 @@ const ViewContainer = new Lang.Class({
     _onViewSelectionChanged: function() {
         // update the selection on the controller when the view signals a change
         let selectedURNs = Utils.getURNsFromPaths(this.view.get_selection(),
-                                                  this._treeModel);
+                                                  this._model.model);
         Global.selectionController.setSelection(selectedURNs);
     },
 



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