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