[gnome-documents] all: cleanup the startup sequence to have a tracker connection singleton



commit 19725969ef527b093464d84607d5be2a52a0d3aa
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sun Aug 21 18:19:30 2011 +0200

    all: cleanup the startup sequence to have a tracker connection singleton
    
    We want a single tracker connection object, so always create it before
    building the model and the window.

 src/application.js  |   30 ++++++++++++++++--------------
 src/mainWindow.js   |   12 +++++-------
 src/trackerModel.js |   29 +++++++----------------------
 3 files changed, 28 insertions(+), 43 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 6bea45a..b6de7f2 100644
--- a/src/application.js
+++ b/src/application.js
@@ -27,10 +27,12 @@ const EvDoc = imports.gi.EvinceDocument;
 const Gdk = imports.gi.Gdk;
 const Gtk = imports.gi.Gtk;
 const GLib = imports.gi.GLib;
+const Tracker = imports.gi.Tracker;
 
 const Format = imports.format;
 const MainWindow = imports.mainWindow;
 const Path = imports.path;
+const TrackerModel = imports.trackerModel;
 
 const _GD_DBUS_PATH = '/org/gnome/Documents';
 
@@ -81,14 +83,6 @@ Application.prototype = {
     },
 
     _initReal: function() {
-        this._initLowlevel();
-        this._initGtkSettings();
-        this._initMainWindow();
-
-        this.activate();
-    },
-
-    _initLowlevel: function() {
         Gettext.bindtextdomain('gnome-documents', Path.LOCALE_DIR);
         Gettext.textdomain('gnome-documents');
         String.prototype.format = Format.format;
@@ -96,18 +90,26 @@ Application.prototype = {
         GLib.set_prgname('gnome-documents');
         Gtk.init(null, null);
         EvDoc.init();
-    },
 
-    _initGtkSettings: function() {
         let provider = new Gtk.CssProvider();
         provider.load_from_path(Path.STYLE_DIR + "gtk-style.css");
         Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
                                                  provider,
                                                  600);
-    },
 
-    _initMainWindow: function() {
-        this._mainWindow = new MainWindow.MainWindow();
+        // connect to tracker, then create the main window
+        Tracker.SparqlConnection.get_async(null, Lang.bind(this,
+            function(object, res) {
+                try {
+                    this.connection = Tracker.SparqlConnection.get_finish(res);
+                } catch (e) {
+                    log('Unable to connect to the tracker database: ' + e.toString());
+                    this.quit();
+                }
+
+                this._mainWindow = new MainWindow.MainWindow();
+                this.activate();
+            }));
     },
 
     activate: function() {
@@ -117,4 +119,4 @@ Application.prototype = {
     quit: function() {
         Gtk.main_quit();
     }
-}
\ No newline at end of file
+};
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 45bc186..d23a684 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -96,8 +96,10 @@ MainWindow.prototype = {
 
         this._grid.show_all();
 
-        this._model = new TrackerModel.TrackerModel(Lang.bind(this, this._onModelCreated));
+        this._model = new TrackerModel.TrackerModel(Main.application.connection);
         this._model.connect('count-updated', Lang.bind(this, this._onModelCountUpdated));
+
+        this._prepareForOverview();
     },
 
     _destroyView: function() {
@@ -186,10 +188,6 @@ MainWindow.prototype = {
         this._model.populateForOverview(this._currentSourceId, this._lastFilter);
     },
 
-    _onModelCreated: function() {
-        this._prepareForOverview();
-    },
-
     _onDeleteEvent: function() {
         Main.application.quit();
     },
@@ -200,7 +198,7 @@ MainWindow.prototype = {
             this._loaderTimeout = 0;
         }
 
-        TrackerUtils.sourceIdFromResourceUrn(this._model.connection, resource, Lang.bind(this,
+        TrackerUtils.sourceIdFromResourceUrn(Main.application.connection, resource, Lang.bind(this,
             function(sourceId) {
                 this._loaderCancellable = new Gio.Cancellable();
                 this._pdfLoader = new Gd.PdfLoader({ source_id: sourceId });
@@ -263,4 +261,4 @@ MainWindow.prototype = {
 
         this._model.setAccountFilter(id);
     }
-}
+};
diff --git a/src/trackerModel.js b/src/trackerModel.js
index 49b3ff6..3942c9e 100644
--- a/src/trackerModel.js
+++ b/src/trackerModel.js
@@ -183,33 +183,18 @@ QueryBuilder.prototype = {
     }
 };
 
-function TrackerModel(callback) {
-    this._init(callback);
+function TrackerModel(connection) {
+    this._init(connection);
 }
 
 TrackerModel.prototype = {
-    _init: function(callback) {
+    _init: function(connection) {
         this._builder = new QueryBuilder();
         this._factory = new DocFactory.DocFactory();
-        this._initCallback = callback;
         Main.settings.connect('changed::list-view', Lang.bind(this, this._onSettingsChanged));
 
         this.model = Gd.create_list_store();
-        this._initConnection();
-    },
-
-    _initConnection: function() {
-        Tracker.SparqlConnection.get_async(null, Lang.bind(this, function(object, res) {
-            try {
-                this.connection = Tracker.SparqlConnection.get_finish(res);
-            } catch (e) {
-                log('Unable to connect to the tracker database: ' + e.toString());
-                Main.application.quit();
-            }
-
-            if (this._initCallback)
-                this._initCallback();
-        }));
+        this._connection = connection;
     },
 
     _onSettingsChanged: function() {
@@ -269,8 +254,8 @@ TrackerModel.prototype = {
     },
 
     _performCurrentQuery: function() {
-        this.connection.query_async(this._builder.buildQuery(this.offset, this._filter, this._resourceUrn),
-                                    null, Lang.bind(this, this._onQueryExecuted));
+        this._connection.query_async(this._builder.buildQuery(this.offset, this._filter, this._resourceUrn),
+                                     null, Lang.bind(this, this._onQueryExecuted));
     },
 
     _emitCountUpdated: function() {
@@ -308,7 +293,7 @@ TrackerModel.prototype = {
             return;
         }
 
-        TrackerUtils.resourceUrnFromSourceId(this.connection, id, Lang.bind(this,
+        TrackerUtils.resourceUrnFromSourceId(this._connection, id, Lang.bind(this,
             function(resourceUrn) {
                 this.model.clear();
 



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