2011-11-16 18:11:05 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
|
|
|
const Gio = imports.gi.Gio;
|
2011-12-15 08:46:12 -05:00
|
|
|
const GLib = imports.gi.GLib;
|
2011-11-16 18:11:05 -05:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
|
|
|
|
2011-12-15 08:46:12 -05:00
|
|
|
const FileUtils = imports.misc.fileUtils;
|
2011-11-16 18:11:05 -05:00
|
|
|
const Search = imports.ui.search;
|
|
|
|
|
2011-12-15 08:46:12 -05:00
|
|
|
const KEY_FILE_GROUP = 'Shell Search Provider';
|
|
|
|
|
2011-11-16 18:11:05 -05:00
|
|
|
const SearchProviderIface = <interface name="org.gnome.Shell.SearchProvider">
|
|
|
|
<method name="GetInitialResultSet">
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="as" direction="out" />
|
|
|
|
</method>
|
|
|
|
<method name="GetSubsearchResultSet">
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="as" direction="out" />
|
|
|
|
</method>
|
|
|
|
<method name="GetResultMetas">
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="aa{sv}" direction="out" />
|
|
|
|
</method>
|
|
|
|
<method name="ActivateResult">
|
|
|
|
<arg type="s" direction="in" />
|
|
|
|
</method>
|
|
|
|
</interface>;
|
|
|
|
|
2012-12-05 17:20:22 -05:00
|
|
|
const SearchProvider2Iface = <interface name="org.gnome.Shell.SearchProvider2">
|
|
|
|
<method name="GetInitialResultSet">
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="as" direction="out" />
|
|
|
|
</method>
|
|
|
|
<method name="GetSubsearchResultSet">
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="as" direction="out" />
|
|
|
|
</method>
|
|
|
|
<method name="GetResultMetas">
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="aa{sv}" direction="out" />
|
|
|
|
</method>
|
|
|
|
<method name="ActivateResult">
|
|
|
|
<arg type="s" direction="in" />
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
<arg type="u" direction="in" />
|
|
|
|
</method>
|
|
|
|
<method name="LaunchSearch">
|
|
|
|
<arg type="as" direction="in" />
|
|
|
|
</method>
|
|
|
|
</interface>;
|
|
|
|
|
2011-11-16 18:11:05 -05:00
|
|
|
var SearchProviderProxy = Gio.DBusProxy.makeProxyWrapper(SearchProviderIface);
|
2012-12-05 17:20:22 -05:00
|
|
|
var SearchProvider2Proxy = Gio.DBusProxy.makeProxyWrapper(SearchProvider2Iface);
|
2011-11-16 18:11:05 -05:00
|
|
|
|
2011-12-15 08:46:12 -05:00
|
|
|
function loadRemoteSearchProviders(addProviderCallback) {
|
2012-11-30 10:36:12 -05:00
|
|
|
let data = { loadedProviders: [],
|
|
|
|
objectPaths: {},
|
|
|
|
addProviderCallback: addProviderCallback };
|
|
|
|
FileUtils.collectFromDatadirsAsync('search-providers',
|
|
|
|
{ loadedCallback: remoteProvidersLoaded,
|
|
|
|
processFile: loadRemoteSearchProvider,
|
|
|
|
data: data
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadRemoteSearchProvider(file, info, data) {
|
|
|
|
let keyfile = new GLib.KeyFile();
|
|
|
|
let path = file.get_path();
|
|
|
|
|
|
|
|
try {
|
|
|
|
keyfile.load_from_file(path, 0);
|
|
|
|
} catch(e) {
|
|
|
|
return;
|
2011-12-15 08:46:12 -05:00
|
|
|
}
|
|
|
|
|
2012-11-30 10:36:12 -05:00
|
|
|
if (!keyfile.has_group(KEY_FILE_GROUP))
|
|
|
|
return;
|
2012-06-22 11:44:15 -04:00
|
|
|
|
2012-11-30 10:36:12 -05:00
|
|
|
let remoteProvider;
|
|
|
|
try {
|
|
|
|
let group = KEY_FILE_GROUP;
|
|
|
|
let busName = keyfile.get_string(group, 'BusName');
|
|
|
|
let objectPath = keyfile.get_string(group, 'ObjectPath');
|
|
|
|
|
|
|
|
if (data.objectPaths[objectPath])
|
|
|
|
return;
|
|
|
|
|
|
|
|
let appInfo = null;
|
|
|
|
try {
|
|
|
|
let desktopId = keyfile.get_string(group, 'DesktopId');
|
|
|
|
appInfo = Gio.DesktopAppInfo.new(desktopId);
|
|
|
|
} catch (e) {
|
|
|
|
log('Ignoring search provider ' + path + ': missing DesktopId');
|
|
|
|
return;
|
2011-12-15 08:46:12 -05:00
|
|
|
}
|
2012-11-01 15:55:41 -04:00
|
|
|
|
2012-12-05 17:20:22 -05:00
|
|
|
let version = '1';
|
|
|
|
try {
|
|
|
|
version = keyfile.get_string(group, 'Version');
|
|
|
|
} catch (e) {
|
|
|
|
// ignore error
|
|
|
|
}
|
|
|
|
|
|
|
|
if (version >= 2)
|
|
|
|
remoteProvider = new RemoteSearchProvider2(appInfo, busName, objectPath);
|
|
|
|
else
|
|
|
|
remoteProvider = new RemoteSearchProvider(appInfo, busName, objectPath);
|
|
|
|
|
2012-11-30 10:36:12 -05:00
|
|
|
data.objectPaths[objectPath] = remoteProvider;
|
|
|
|
data.loadedProviders.push(remoteProvider);
|
|
|
|
} catch(e) {
|
|
|
|
log('Failed to add search provider %s: %s'.format(path, e.toString()));
|
|
|
|
}
|
|
|
|
}
|
2012-11-01 15:55:41 -04:00
|
|
|
|
2012-11-30 10:36:12 -05:00
|
|
|
function remoteProvidersLoaded(loadState) {
|
2012-11-01 15:57:18 -04:00
|
|
|
let searchSettings = new Gio.Settings({ schema: Search.SEARCH_PROVIDERS_SCHEMA });
|
|
|
|
let sortOrder = searchSettings.get_strv('sort-order');
|
|
|
|
let numSorted = sortOrder.length;
|
|
|
|
|
|
|
|
loadState.loadedProviders.sort(
|
|
|
|
function(providerA, providerB) {
|
|
|
|
let idxA, idxB;
|
|
|
|
let appIdA, appIdB;
|
|
|
|
|
|
|
|
appIdA = providerA.appInfo.get_id();
|
|
|
|
appIdB = providerB.appInfo.get_id();
|
|
|
|
|
|
|
|
idxA = sortOrder.indexOf(appIdA);
|
|
|
|
idxB = sortOrder.indexOf(appIdB);
|
|
|
|
|
|
|
|
// if no provider is found in the order, use alphabetical order
|
|
|
|
if ((idxA == -1) && (idxB == -1))
|
|
|
|
return GLib.utf8_collate(providerA.title, providerB.title);
|
|
|
|
|
|
|
|
if (numSorted > 1) {
|
|
|
|
// if providerA is the last, it goes after everything
|
|
|
|
if ((idxA + 1) == numSorted)
|
|
|
|
return 1;
|
|
|
|
// if providerB is the last, it goes after everything
|
|
|
|
else if ((idxB + 1) == numSorted)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if providerA isn't found, it's sorted after providerB
|
|
|
|
if (idxA == -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
// if providerB isn't found, it's sorted after providerA
|
|
|
|
if (idxB == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// finally, if both providers are found, return their order in the list
|
|
|
|
return (idxA - idxB);
|
|
|
|
});
|
|
|
|
|
2012-11-01 15:55:41 -04:00
|
|
|
loadState.loadedProviders.forEach(
|
|
|
|
function(provider) {
|
|
|
|
loadState.addProviderCallback(provider);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-16 18:11:05 -05:00
|
|
|
const RemoteSearchProvider = new Lang.Class({
|
|
|
|
Name: 'RemoteSearchProvider',
|
|
|
|
Extends: Search.SearchProvider,
|
|
|
|
|
2012-12-05 17:20:22 -05:00
|
|
|
_init: function(appInfo, dbusName, dbusPath, proxyType) {
|
|
|
|
if (!proxyType)
|
|
|
|
proxyType = SearchProviderProxy;
|
|
|
|
|
|
|
|
this.proxy = new proxyType(Gio.DBus.session,
|
|
|
|
dbusName, dbusPath, Lang.bind(this, this._onProxyConstructed));
|
2011-11-16 18:11:05 -05:00
|
|
|
|
2012-11-06 18:42:33 -05:00
|
|
|
this.parent(appInfo.get_name().toUpperCase(), appInfo, true);
|
2011-11-16 18:11:05 -05:00
|
|
|
this._cancellable = new Gio.Cancellable();
|
|
|
|
},
|
|
|
|
|
2012-11-01 17:28:46 -04:00
|
|
|
_onProxyConstructed: function(proxy) {
|
|
|
|
// Do nothing
|
|
|
|
},
|
|
|
|
|
2011-11-16 18:11:05 -05:00
|
|
|
createIcon: function(size, meta) {
|
|
|
|
if (meta['gicon']) {
|
|
|
|
return new St.Icon({ gicon: Gio.icon_new_for_string(meta['gicon']),
|
2012-05-30 09:58:37 -04:00
|
|
|
icon_size: size });
|
2011-11-16 18:11:05 -05:00
|
|
|
} else if (meta['icon-data']) {
|
|
|
|
let [width, height, rowStride, hasAlpha,
|
|
|
|
bitsPerSample, nChannels, data] = meta['icon-data'];
|
|
|
|
let textureCache = St.TextureCache.get_default();
|
|
|
|
return textureCache.load_from_raw(data, hasAlpha,
|
|
|
|
width, height, rowStride, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ugh, but we want to fall back to something ...
|
|
|
|
return new St.Icon({ icon_name: 'text-x-generic',
|
2012-05-30 09:58:37 -04:00
|
|
|
icon_size: size });
|
2011-11-16 18:11:05 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
_getResultsFinished: function(results, error) {
|
|
|
|
if (error)
|
|
|
|
return;
|
|
|
|
this.searchSystem.pushResults(this, results[0]);
|
|
|
|
},
|
|
|
|
|
2012-05-02 15:54:25 -04:00
|
|
|
getInitialResultSet: function(terms) {
|
2011-11-16 18:11:05 -05:00
|
|
|
this._cancellable.cancel();
|
|
|
|
this._cancellable.reset();
|
|
|
|
try {
|
2012-12-05 17:20:22 -05:00
|
|
|
this.proxy.GetInitialResultSetRemote(terms,
|
|
|
|
Lang.bind(this, this._getResultsFinished),
|
|
|
|
this._cancellable);
|
2011-11-16 18:11:05 -05:00
|
|
|
} catch(e) {
|
|
|
|
log('Error calling GetInitialResultSet for provider %s: %s'.format( this.title, e.toString()));
|
|
|
|
this.searchSystem.pushResults(this, []);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-05-02 15:54:25 -04:00
|
|
|
getSubsearchResultSet: function(previousResults, newTerms) {
|
2011-11-16 18:11:05 -05:00
|
|
|
this._cancellable.cancel();
|
|
|
|
this._cancellable.reset();
|
|
|
|
try {
|
2012-12-05 17:20:22 -05:00
|
|
|
this.proxy.GetSubsearchResultSetRemote(previousResults, newTerms,
|
|
|
|
Lang.bind(this, this._getResultsFinished),
|
|
|
|
this._cancellable);
|
2011-11-16 18:11:05 -05:00
|
|
|
} catch(e) {
|
|
|
|
log('Error calling GetSubsearchResultSet for provider %s: %s'.format(this.title, e.toString()));
|
|
|
|
this.searchSystem.pushResults(this, []);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_getResultMetasFinished: function(results, error, callback) {
|
|
|
|
if (error) {
|
|
|
|
callback([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let metas = results[0];
|
|
|
|
let resultMetas = [];
|
|
|
|
for (let i = 0; i < metas.length; i++) {
|
|
|
|
for (let prop in metas[i])
|
|
|
|
metas[i][prop] = metas[i][prop].deep_unpack();
|
|
|
|
resultMetas.push({ id: metas[i]['id'],
|
|
|
|
name: metas[i]['name'],
|
|
|
|
createIcon: Lang.bind(this,
|
|
|
|
this.createIcon, metas[i]) });
|
|
|
|
}
|
|
|
|
callback(resultMetas);
|
|
|
|
},
|
|
|
|
|
2012-05-02 15:54:25 -04:00
|
|
|
getResultMetas: function(ids, callback) {
|
2011-11-16 18:11:05 -05:00
|
|
|
this._cancellable.cancel();
|
|
|
|
this._cancellable.reset();
|
|
|
|
try {
|
2012-12-05 17:20:22 -05:00
|
|
|
this.proxy.GetResultMetasRemote(ids,
|
|
|
|
Lang.bind(this, this._getResultMetasFinished, callback),
|
|
|
|
this._cancellable);
|
2011-11-16 18:11:05 -05:00
|
|
|
} catch(e) {
|
|
|
|
log('Error calling GetResultMetas for provider %s: %s'.format(this.title, e.toString()));
|
|
|
|
callback([]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
activateResult: function(id) {
|
2012-12-05 17:20:22 -05:00
|
|
|
this.proxy.ActivateResultRemote(id);
|
2012-11-26 16:55:25 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
launchSearch: function(terms) {
|
|
|
|
// the provider is not compatible with the new version of the interface, launch
|
|
|
|
// the app itself but warn so we can catch the error in logs
|
|
|
|
log('Search provider ' + this.appInfo.get_id() + ' does not implement LaunchSearch');
|
|
|
|
this.appInfo.launch([], global.create_app_launch_context());
|
2011-11-16 18:11:05 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-12-05 17:20:22 -05:00
|
|
|
const RemoteSearchProvider2 = new Lang.Class({
|
|
|
|
Name: 'RemoteSearchProvider2',
|
|
|
|
Extends: RemoteSearchProvider,
|
|
|
|
|
|
|
|
_init: function(appInfo, dbusName, dbusPath) {
|
|
|
|
this.parent(appInfo, dbusName, dbusPath, SearchProvider2Proxy);
|
2012-11-26 16:55:25 -05:00
|
|
|
|
|
|
|
this.canLaunchSearch = true;
|
2012-12-05 17:20:22 -05:00
|
|
|
},
|
2011-11-16 18:11:05 -05:00
|
|
|
|
2012-12-05 11:48:22 -05:00
|
|
|
activateResult: function(id, terms) {
|
|
|
|
this.proxy.ActivateResultRemote(id, terms, global.get_current_time());
|
2012-11-26 16:55:25 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
launchSearch: function(terms) {
|
|
|
|
this.proxy.LaunchSearchRemote(terms);
|
2012-12-05 17:20:22 -05:00
|
|
|
}
|
|
|
|
});
|