[gnome-books/wip/carlosg/tracker3: 5/7] changeMonitor: Rewrite to use TrackerNotifier
- From: Carlos Garnacho <carlosg src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-books/wip/carlosg/tracker3: 5/7] changeMonitor: Rewrite to use TrackerNotifier
- Date: Tue, 26 Jan 2021 13:27:47 +0000 (UTC)
commit 5158278245d2ead155876153488e605dbaa63e80
Author: Carlos Garnacho <carlosg gnome org>
Date: Sun Jan 17 17:02:04 2021 +0100
changeMonitor: Rewrite to use TrackerNotifier
The GraphUpdated DBus signal became superseded by TrackerNotifier
in 2.x, and completely removed on 3.x. Use TrackerNotifier here
for event tracking.
src/changeMonitor.js | 160 ++++++---------------------------------------------
1 file changed, 18 insertions(+), 142 deletions(-)
---
diff --git a/src/changeMonitor.js b/src/changeMonitor.js
index a9e3a5ba..ec9d8a02 100644
--- a/src/changeMonitor.js
+++ b/src/changeMonitor.js
@@ -23,176 +23,52 @@ const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Mainloop = imports.mainloop;
const Signals = imports.signals;
+const Tracker = imports.gi.Tracker;
const Application = imports.application;
-const TrackerResourcesServiceIface = '<node> \
-<interface name="org.freedesktop.Tracker1.Resources"> \
- <signal name="GraphUpdated"> \
- <arg name="className" type="s" /> \
- <arg name="deleteEvents" type="a(iiii)" /> \
- <arg name="insertEvents" type="a(iiii)" /> \
- </signal> \
-</interface> \
-</node>';
-
-var TrackerResourcesServiceProxy = Gio.DBusProxy.makeProxyWrapper(TrackerResourcesServiceIface);
-function TrackerResourcesService() {
- return new TrackerResourcesServiceProxy(Gio.DBus.session,
- 'org.freedesktop.Tracker1',
- '/org/freedesktop/Tracker1/Resources');
-}
-
-var ChangeEventType = {
- CHANGED: 0,
- CREATED: 1,
- DELETED: 2
-};
-
-const _RDF_TYPE = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
-
const ChangeEvent = new Lang.Class({
Name: 'ChangeEvent',
- _init: function(urnId, predicateId, isDelete) {
- this.urnId = urnId;
- this.predicateId = predicateId;
-
- if (isDelete)
- this.type = ChangeEventType.DELETED;
- else
- this.type = ChangeEventType.CREATED;
- },
-
- setResolvedValues: function(urn, predicate) {
+ _init: function(type, urn) {
this.urn = urn;
- this.predicate = predicate;
- if (predicate != _RDF_TYPE)
+ if (type == Tracker.NotifierEventType.CREATE)
+ this.type = ChangeEventType.CREATED;
+ else if (type == Tracker.NotifierEventType.DELETE)
+ this.type = ChangeEventType.DELETED;
+ else if (type == Tracker.NotifierEventType.UPDATE)
this.type = ChangeEventType.CHANGED;
- },
-
- merge: function(event) {
- // deletions or creations override the current type
- if (event.type == ChangeEventType.DELETED ||
- event.type == ChangeEventType.CREATED) {
- this.type = event.type;
- }
}
});
-const CHANGE_MONITOR_TIMEOUT = 500; // msecs
-const CHANGE_MONITOR_MAX_ITEMS = 500; // items
-
var TrackerChangeMonitor = new Lang.Class({
Name: 'TrackerChangeMonitor',
_init: function() {
this._pendingChanges = {};
- this._unresolvedIds = {};
this._pendingEvents = [];
this._pendingEventsId = 0;
- this._resourceService = new TrackerResourcesService();
- this._resourceService.connectSignal('GraphUpdated', Lang.bind(this, this._onGraphUpdated));
- },
-
- _onGraphUpdated: function(proxy, senderName, [className, deleteEvents, insertEvents]) {
- deleteEvents.forEach(Lang.bind(this,
- function(event) {
- this._addPendingEvent(event, true);
- }));
-
- insertEvents.forEach(Lang.bind(this,
- function(event) {
- this._addPendingEvent(event, false);
- }));
- },
-
- _addPendingEvent: function(event, isDelete) {
- if (this._pendingEventsId != 0)
- Mainloop.source_remove(this._pendingEventsId);
-
- this._unresolvedIds[event[1]] = event[1];
- this._unresolvedIds[event[2]] = event[2];
- this._pendingEvents.push(new ChangeEvent(event[1], event[2], isDelete));
-
- if (this._pendingEvents.length >= CHANGE_MONITOR_MAX_ITEMS)
- this._processEvents();
- else
- this._pendingEventsId =
- Mainloop.timeout_add(CHANGE_MONITOR_TIMEOUT, Lang.bind(this, this._processEvents));
- },
-
- _processEvents: function() {
- let events = this._pendingEvents;
- let idTable = this._unresolvedIds;
-
- this._pendingEventsId = 0;
- this._pendingEvents = [];
- this._unresolvedIds = {};
-
- let sparql = 'SELECT';
- Object.keys(idTable).forEach(Lang.bind(this,
- function(unresolvedId) {
- sparql += (' tracker:uri(%d)').format(unresolvedId);
- }));
- sparql += ' {}';
-
- // resolve all the unresolved IDs we got so far
- Application.connectionQueue.add(sparql, null, Lang.bind(this,
- function(object, res) {
- let cursor = object.query_finish(res);
-
- cursor.next_async(null, Lang.bind(this,
- function(object, res) {
- let valid = false;
- try {
- valid = cursor.next_finish(res);
- } catch(e) {
- logError(e, 'Unable to resolve item URNs for graph changes');
- }
-
- if (valid) {
- let idx = 0;
- Object.keys(idTable).forEach(Lang.bind(this,
- function(unresolvedId) {
- idTable[unresolvedId] = cursor.get_string(idx)[0];
- idx++;
- }));
-
- this._sendEvents(events, idTable);
- }
-
- cursor.close();
- }));
- }));
-
- return false;
+ this._notifier = Application.connection.create_notifier();
+ this._notifier.signal_subscribe(Gio.DBus.session,
+ 'org.freedesktop.Tracker3.Miner.Files',
+ null, 'tracker:Documents');
+ this._notifier.connect('events', Lang.bind(this, this._onNotifierEvents));
},
- _addEvent: function(event) {
- let urn = event.urn;
- let oldEvent = this._pendingChanges[urn];
+ _onNotifierEvents: function(notifier, service, graph, events) {
+ let pendingChanges = {};
- if (oldEvent != null) {
- oldEvent.merge(event);
- this._pendingChanges[urn] = oldEvent;
- } else {
- this._pendingChanges[urn] = event;
- }
- },
-
- _sendEvents: function(events, idTable) {
events.forEach(Lang.bind(this,
function(event) {
- event.setResolvedValues(idTable[event.urnId], idTable[event.predicateId]);
- this._addEvent(event);
+ let urn = event.get_urn();
+ let changeEvent = new ChangeEvent(urn, event.get_type());
+ pendingEvents[urn] = changeEvent;
}));
- this.emit('changes-pending', this._pendingChanges);
- this._pendingChanges = {};
+ this.emit('changes-pending', event);
}
});
Signals.addSignalMethods(TrackerChangeMonitor.prototype);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]