2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2009-11-29 22:45:30 +00:00
|
|
|
|
2011-01-17 21:38:47 +00:00
|
|
|
const Lang = imports.lang;
|
2009-11-29 22:45:30 +00:00
|
|
|
const Signals = imports.signals;
|
2011-01-17 21:38:47 +00:00
|
|
|
|
2012-11-01 14:33:40 +00:00
|
|
|
const SEARCH_PROVIDERS_SCHEMA = 'org.gnome.desktop.search-providers';
|
|
|
|
|
2011-11-20 17:56:27 +00:00
|
|
|
const SearchSystem = new Lang.Class({
|
|
|
|
Name: 'SearchSystem',
|
2009-11-29 22:45:30 +00:00
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this._providers = [];
|
2012-11-06 23:42:33 +00:00
|
|
|
this._remoteProviders = [];
|
2009-11-29 22:45:30 +00:00
|
|
|
this.reset();
|
|
|
|
},
|
|
|
|
|
|
|
|
registerProvider: function (provider) {
|
2011-07-24 10:29:36 +00:00
|
|
|
provider.searchSystem = this;
|
2009-11-29 22:45:30 +00:00
|
|
|
this._providers.push(provider);
|
2012-11-06 23:42:33 +00:00
|
|
|
|
|
|
|
if (provider.isRemoteProvider)
|
|
|
|
this._remoteProviders.push(provider);
|
2009-11-29 22:45:30 +00:00
|
|
|
},
|
|
|
|
|
2011-08-28 11:20:37 +00:00
|
|
|
unregisterProvider: function (provider) {
|
|
|
|
let index = this._providers.indexOf(provider);
|
|
|
|
if (index == -1)
|
|
|
|
return;
|
|
|
|
provider.searchSystem = null;
|
|
|
|
this._providers.splice(index, 1);
|
2012-11-06 23:42:33 +00:00
|
|
|
|
|
|
|
let remoteIndex = this._remoteProviders.indexOf(provider);
|
|
|
|
if (remoteIndex != -1)
|
|
|
|
this._remoteProviders.splice(index, 1);
|
2011-08-28 11:20:37 +00:00
|
|
|
},
|
|
|
|
|
2009-11-29 22:45:30 +00:00
|
|
|
getProviders: function() {
|
|
|
|
return this._providers;
|
|
|
|
},
|
|
|
|
|
2012-11-06 23:42:33 +00:00
|
|
|
getRemoteProviders: function() {
|
|
|
|
return this._remoteProviders;
|
|
|
|
},
|
|
|
|
|
2009-11-29 22:45:30 +00:00
|
|
|
getTerms: function() {
|
|
|
|
return this._previousTerms;
|
|
|
|
},
|
|
|
|
|
|
|
|
reset: function() {
|
|
|
|
this._previousTerms = [];
|
|
|
|
this._previousResults = [];
|
|
|
|
},
|
|
|
|
|
2012-02-21 18:53:25 +00:00
|
|
|
pushResults: function(provider, results) {
|
|
|
|
let i = this._providers.indexOf(provider);
|
|
|
|
if (i == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._previousResults[i] = [provider, results];
|
|
|
|
this.emit('search-updated', this._previousResults[i]);
|
2011-07-24 10:29:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
updateSearchResults: function(terms) {
|
|
|
|
if (!terms)
|
|
|
|
return;
|
|
|
|
|
2009-11-29 22:45:30 +00:00
|
|
|
let isSubSearch = terms.length == this._previousTerms.length;
|
|
|
|
if (isSubSearch) {
|
|
|
|
for (let i = 0; i < terms.length; i++) {
|
|
|
|
if (terms[i].indexOf(this._previousTerms[i]) != 0) {
|
|
|
|
isSubSearch = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-02 19:26:54 +00:00
|
|
|
let previousResultsArr = this._previousResults;
|
|
|
|
|
2009-11-29 22:45:30 +00:00
|
|
|
let results = [];
|
2012-05-02 19:26:54 +00:00
|
|
|
this._previousTerms = terms;
|
|
|
|
this._previousResults = results;
|
|
|
|
|
2009-11-29 22:45:30 +00:00
|
|
|
if (isSubSearch) {
|
2011-07-24 10:29:36 +00:00
|
|
|
for (let i = 0; i < this._providers.length; i++) {
|
2012-05-02 19:26:54 +00:00
|
|
|
let [provider, previousResults] = previousResultsArr[i];
|
2010-09-21 09:36:24 +00:00
|
|
|
try {
|
2012-05-02 19:54:25 +00:00
|
|
|
results.push([provider, []]);
|
|
|
|
provider.getSubsearchResultSet(previousResults, terms);
|
2010-09-21 09:36:24 +00:00
|
|
|
} catch (error) {
|
2012-08-09 21:52:36 +00:00
|
|
|
log('A ' + error.name + ' has occured in ' + provider.id + ': ' + error.message);
|
2010-09-21 09:36:24 +00:00
|
|
|
}
|
2009-11-29 22:45:30 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let i = 0; i < this._providers.length; i++) {
|
|
|
|
let provider = this._providers[i];
|
2010-09-21 09:36:24 +00:00
|
|
|
try {
|
2012-05-02 19:54:25 +00:00
|
|
|
results.push([provider, []]);
|
|
|
|
provider.getInitialResultSet(terms);
|
2010-09-21 09:36:24 +00:00
|
|
|
} catch (error) {
|
2012-08-09 21:52:36 +00:00
|
|
|
log('A ' + error.name + ' has occured in ' + provider.id + ': ' + error.message);
|
2010-09-21 09:36:24 +00:00
|
|
|
}
|
2009-11-29 22:45:30 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-09 01:48:02 +00:00
|
|
|
}
|
2011-11-20 17:56:27 +00:00
|
|
|
});
|
2009-11-29 22:45:30 +00:00
|
|
|
Signals.addSignalMethods(SearchSystem.prototype);
|