[the-board/tracker: 4/14] [model] Add initial code for TrackerClient



commit 5b8ba4cbfa5142f645ce738b707209eaf4ab02f9
Author: Lucas Rocha <lucasr lucasr org>
Date:   Sun May 8 11:34:17 2011 +0100

    [model] Add initial code for TrackerClient

 src/js/model/trackerClient.js |  145 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 145 insertions(+), 0 deletions(-)
---
diff --git a/src/js/model/trackerClient.js b/src/js/model/trackerClient.js
new file mode 100644
index 0000000..880dadd
--- /dev/null
+++ b/src/js/model/trackerClient.js
@@ -0,0 +1,145 @@
+// standard imports
+const Lang = imports.lang;
+const Mainloop = imports.mainloop;
+const Signals = imports.signals;
+
+// gi imports
+const GIO = imports.gi.Gio;
+const Tracker = imports.gi.Tracker;
+
+// model imports
+const PageModel = imports.model.pageModel;
+
+let State = {
+    IDLE : 0,
+    CONNECTING : 1,
+    READY : 2,
+    ERROR : 3
+}
+
+function TrackerClient(args) {
+    this._init(args);
+}
+
+TrackerClient.prototype = {
+    _init : function(args) {
+        args = args || {};
+
+        this._connection = null;
+        this._state = State.IDLE;
+
+        this._connectToTracker();
+    },
+
+    _connectToTracker : function() {
+        if (this._connection) {
+            return;
+        }
+
+        // Now we're in connecting state
+        this._setState(State.CONNECTING);
+
+        // This will allow to cancel operation is necessary
+        this._getConnectionCancellable = new GIO.Cancellable();
+
+        Tracker.SparqlConnection.get_async(this._getConnectionCancellable,
+                                           Lang.bind(this, this._onGetConnectionAsync),
+                                           null);
+    },
+
+    _setState : function(state) {
+        if (this._state == state) {
+            return;
+        }
+
+        this._state = state;
+
+        this.emit("state-changed");
+    },
+
+    _onGetConnectionAsync : function(source, result, data) {
+        let cancellable = this._getConnectionCancellable;
+        delete this._getConnectionCancellable;
+
+        // If operation has been cancelled, we just stop and set
+        // state back to IDLE
+        if (cancellable.is_cancelled()) {
+            this._setState(State.IDLE);
+            return;
+        }
+
+        let newState = State.READY;
+
+        try {
+            this._connection = Tracker.SparqlConnection.get_finish(result);
+        } catch(e) {
+            log("Tracker: failed to connect to Tracker");
+            newState = State.ERROR;
+        }
+
+        this._setState(newState);
+    },
+
+    query : function(query, onReturn) {
+        if (this._state != State.READY) {
+            throw new Error("Tracker is not ready for queries");
+        }
+
+        this._connection.query_async(query, null,
+                                     Lang.bind(this,
+                                               this._onQueryAsync,
+                                               onReturn),
+                                     null);
+    },
+
+    update : function(query, onReturn) {
+        if (this._state != State.READY) {
+            throw new Error("Tracker is not ready for updates");
+        }
+
+        this._connection.update_async(query, null,
+                                      Lang.bind(this,
+                                                this._onUpdateAsync,
+                                                onReturn),
+                                     null);
+    },
+
+    _onQueryAsync : function(source, result, data, onReturn) {
+        let cursor = null;
+
+        try {
+            cursor = Tracker.SparqlConnection.query_finish(result);
+        } catch(e) {
+            log("Tracker: failed to run query in Tracker");
+        }
+
+        if (onReturn) {
+            onReturn(cursor);
+        }
+    },
+
+    _onUpdateAsync : function(source, result, data, onReturn) {
+        let success = true;
+
+        try {
+            Tracker.SparqlConnection.update_finish(result);
+        } catch(e) {
+            log("Tracker: failed to run update in Tracker");
+            success = false;
+        }
+
+        if (onReturn) {
+            onReturn(success);
+        }
+    },
+
+    destroy : function() {
+        delete this._connection;
+    },
+
+    get state() {
+        return this._state;
+    }
+}
+
+Signals.addSignalMethods(TrackerClient.prototype);



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