2011-11-16 23:11:05 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2013-01-03 01:48:22 +00:00
|
|
|
const GdkPixbuf = imports.gi.GdkPixbuf;
|
2011-11-16 23:11:05 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
2011-12-15 13:46:12 +00:00
|
|
|
const GLib = imports.gi.GLib;
|
2011-11-16 23:11:05 +00:00
|
|
|
const Lang = imports.lang;
|
|
|
|
const St = imports.gi.St;
|
2013-01-02 14:31:50 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
2011-11-16 23:11:05 +00:00
|
|
|
|
|
|
|
const Search = imports.ui.search;
|
|
|
|
|
2011-12-15 13:46:12 +00:00
|
|
|
const KEY_FILE_GROUP = 'Shell Search Provider';
|
|
|
|
|
2013-10-24 21:51:58 +00:00
|
|
|
const SearchProviderIface = '<node> \
|
|
|
|
<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> \
|
|
|
|
</node>';
|
2011-11-16 23:11:05 +00:00
|
|
|
|
2013-10-24 21:51:58 +00:00
|
|
|
const SearchProvider2Iface = '<node> \
|
|
|
|
<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" /> \
|
|
|
|
<arg type="u" direction="in" /> \
|
|
|
|
</method> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
2012-12-05 22:20:22 +00:00
|
|
|
|
2013-09-26 11:22:22 +00:00
|
|
|
var SearchProviderProxyInfo = Gio.DBusInterfaceInfo.new_for_xml(SearchProviderIface);
|
|
|
|
var SearchProvider2ProxyInfo = Gio.DBusInterfaceInfo.new_for_xml(SearchProvider2Iface);
|
2011-11-16 23:11:05 +00:00
|
|
|
|
2011-12-15 13:46:12 +00:00
|
|
|
function loadRemoteSearchProviders(addProviderCallback) {
|
2013-07-26 22:36:31 +00:00
|
|
|
let objectPaths = {};
|
|
|
|
let loadedProviders = [];
|
2012-06-22 15:44:15 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
function loadRemoteSearchProvider(file) {
|
|
|
|
let keyfile = new GLib.KeyFile();
|
|
|
|
let path = file.get_path();
|
2012-11-30 15:36:12 +00:00
|
|
|
|
|
|
|
try {
|
2013-07-26 22:36:31 +00:00
|
|
|
keyfile.load_from_file(path, 0);
|
|
|
|
} catch(e) {
|
2012-11-30 15:36:12 +00:00
|
|
|
return;
|
2011-12-15 13:46:12 +00:00
|
|
|
}
|
2012-11-01 19:55:41 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
if (!keyfile.has_group(KEY_FILE_GROUP))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let remoteProvider;
|
2012-12-05 22:20:22 +00:00
|
|
|
try {
|
2013-07-26 22:36:31 +00:00
|
|
|
let group = KEY_FILE_GROUP;
|
|
|
|
let busName = keyfile.get_string(group, 'BusName');
|
|
|
|
let objectPath = keyfile.get_string(group, 'ObjectPath');
|
|
|
|
|
|
|
|
if (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;
|
|
|
|
}
|
2012-12-05 22:20:22 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
let version = '1';
|
|
|
|
try {
|
|
|
|
version = keyfile.get_string(group, 'Version');
|
|
|
|
} catch (e) {
|
|
|
|
// ignore error
|
|
|
|
}
|
2012-12-05 22:20:22 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
if (version >= 2)
|
|
|
|
remoteProvider = new RemoteSearchProvider2(appInfo, busName, objectPath);
|
|
|
|
else
|
|
|
|
remoteProvider = new RemoteSearchProvider(appInfo, busName, objectPath);
|
|
|
|
|
|
|
|
objectPaths[objectPath] = remoteProvider;
|
|
|
|
loadedProviders.push(remoteProvider);
|
|
|
|
} catch(e) {
|
|
|
|
log('Failed to add search provider %s: %s'.format(path, e.toString()));
|
|
|
|
}
|
2012-11-30 15:36:12 +00:00
|
|
|
}
|
2012-11-01 19:55:41 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
let dataDirs = GLib.get_system_data_dirs();
|
|
|
|
dataDirs.forEach(function(dataDir) {
|
|
|
|
let path = GLib.build_filenamev([dataDir, 'gnome-shell', 'search-providers']);
|
|
|
|
let dir = Gio.File.new_for_path(path);
|
2013-07-27 14:55:57 +00:00
|
|
|
let fileEnum;
|
|
|
|
try {
|
|
|
|
fileEnum = dir.enumerate_children('standard::name,standard::type',
|
2013-07-26 22:36:31 +00:00
|
|
|
Gio.FileQueryInfoFlags.NONE, null);
|
2013-07-27 14:55:57 +00:00
|
|
|
} catch (e) {
|
|
|
|
fileEnum = null;
|
|
|
|
}
|
|
|
|
if (fileEnum != null) {
|
|
|
|
let info;
|
|
|
|
while ((info = fileEnum.next_file(null)))
|
|
|
|
loadRemoteSearchProvider(fileEnum.get_child(info));
|
|
|
|
}
|
2013-07-26 22:36:31 +00:00
|
|
|
});
|
|
|
|
|
2012-11-01 19:57:18 +00:00
|
|
|
let searchSettings = new Gio.Settings({ schema: Search.SEARCH_PROVIDERS_SCHEMA });
|
|
|
|
let sortOrder = searchSettings.get_strv('sort-order');
|
2012-12-28 17:03:13 +00:00
|
|
|
|
|
|
|
// Special case gnome-control-center to be always active and always first
|
|
|
|
sortOrder.unshift('gnome-control-center.desktop');
|
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
loadedProviders.sort(function(providerA, providerB) {
|
|
|
|
let idxA, idxB;
|
|
|
|
let appIdA, appIdB;
|
2012-11-01 19:57:18 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
appIdA = providerA.appInfo.get_id();
|
|
|
|
appIdB = providerB.appInfo.get_id();
|
2012-11-01 19:57:18 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
idxA = sortOrder.indexOf(appIdA);
|
|
|
|
idxB = sortOrder.indexOf(appIdB);
|
2012-11-01 19:57:18 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
// if no provider is found in the order, use alphabetical order
|
|
|
|
if ((idxA == -1) && (idxB == -1)) {
|
|
|
|
let nameA = providerA.appInfo.get_name();
|
|
|
|
let nameB = providerB.appInfo.get_name();
|
2013-01-28 17:04:59 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
return GLib.utf8_collate(nameA, nameB);
|
|
|
|
}
|
2012-11-01 19:57:18 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
// if providerA isn't found, it's sorted after providerB
|
|
|
|
if (idxA == -1)
|
|
|
|
return 1;
|
2012-11-01 19:57:18 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
// if providerB isn't found, it's sorted after providerA
|
|
|
|
if (idxB == -1)
|
|
|
|
return -1;
|
2012-11-01 19:57:18 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
// finally, if both providers are found, return their order in the list
|
|
|
|
return (idxA - idxB);
|
|
|
|
});
|
2012-11-01 19:57:18 +00:00
|
|
|
|
2013-07-26 22:36:31 +00:00
|
|
|
loadedProviders.forEach(addProviderCallback);
|
2012-11-01 19:55:41 +00:00
|
|
|
}
|
|
|
|
|
2011-11-16 23:11:05 +00:00
|
|
|
const RemoteSearchProvider = new Lang.Class({
|
|
|
|
Name: 'RemoteSearchProvider',
|
|
|
|
|
2013-09-26 11:22:22 +00:00
|
|
|
_init: function(appInfo, dbusName, dbusPath, proxyInfo) {
|
|
|
|
if (!proxyInfo)
|
|
|
|
proxyInfo = SearchProviderProxyInfo;
|
2012-12-05 22:20:22 +00:00
|
|
|
|
2013-09-26 11:22:22 +00:00
|
|
|
this.proxy = new Gio.DBusProxy({ g_bus_type: Gio.BusType.SESSION,
|
|
|
|
g_name: dbusName,
|
|
|
|
g_object_path: dbusPath,
|
|
|
|
g_interface_info: proxyInfo,
|
|
|
|
g_interface_name: proxyInfo.name,
|
|
|
|
g_flags: (Gio.DBusProxyFlags.DO_NOT_AUTO_START_AT_CONSTRUCTION |
|
|
|
|
Gio.DBusProxyFlags.DO_NOT_LOAD_PROPERTIES) });
|
|
|
|
this.proxy.init_async(GLib.PRIORITY_DEFAULT, null, null);
|
2011-11-16 23:11:05 +00:00
|
|
|
|
2012-12-06 19:10:44 +00:00
|
|
|
this.appInfo = appInfo;
|
|
|
|
this.id = appInfo.get_id();
|
|
|
|
this.isRemoteProvider = true;
|
|
|
|
|
2011-11-16 23:11:05 +00:00
|
|
|
this._cancellable = new Gio.Cancellable();
|
|
|
|
},
|
|
|
|
|
|
|
|
createIcon: function(size, meta) {
|
2013-01-02 14:31:50 +00:00
|
|
|
let gicon;
|
2013-04-24 16:05:22 +00:00
|
|
|
if (meta['icon']) {
|
|
|
|
gicon = Gio.icon_deserialize(meta['icon']);
|
|
|
|
} else if (meta['gicon']) {
|
2013-01-02 14:31:50 +00:00
|
|
|
gicon = Gio.icon_new_for_string(meta['gicon']);
|
2011-11-16 23:11:05 +00:00
|
|
|
} else if (meta['icon-data']) {
|
|
|
|
let [width, height, rowStride, hasAlpha,
|
|
|
|
bitsPerSample, nChannels, data] = meta['icon-data'];
|
2013-01-02 14:31:50 +00:00
|
|
|
gicon = Shell.util_create_pixbuf_from_data(data, GdkPixbuf.Colorspace.RGB, hasAlpha,
|
|
|
|
bitsPerSample, width, height, rowStride);
|
2011-11-16 23:11:05 +00:00
|
|
|
}
|
|
|
|
|
2013-01-15 00:36:18 +00:00
|
|
|
return new St.Icon({ gicon: gicon,
|
2013-01-02 14:31:50 +00:00
|
|
|
icon_size: size });
|
2011-11-16 23:11:05 +00:00
|
|
|
},
|
|
|
|
|
2013-05-29 20:48:30 +00:00
|
|
|
filterResults: function(results, maxNumber) {
|
|
|
|
if (results.length <= maxNumber)
|
|
|
|
return results;
|
|
|
|
|
|
|
|
let regularResults = results.filter(function(r) { return !r.startsWith('special:'); });
|
|
|
|
let specialResults = results.filter(function(r) { return r.startsWith('special:'); });
|
|
|
|
|
|
|
|
return regularResults.slice(0, maxNumber).concat(specialResults.slice(0, maxNumber));
|
|
|
|
},
|
|
|
|
|
2011-11-16 23:11:05 +00:00
|
|
|
_getResultsFinished: function(results, error) {
|
2013-09-26 11:22:22 +00:00
|
|
|
if (error) {
|
|
|
|
if (!error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
|
|
|
|
log('Received error from DBus search provider %s: %s'.format(this.id, String(error)));
|
|
|
|
} else {
|
|
|
|
this.searchSystem.setResults(this, results[0]);
|
|
|
|
}
|
2011-11-16 23:11:05 +00:00
|
|
|
},
|
|
|
|
|
2012-05-02 19:54:25 +00:00
|
|
|
getInitialResultSet: function(terms) {
|
2011-11-16 23:11:05 +00:00
|
|
|
this._cancellable.cancel();
|
|
|
|
this._cancellable.reset();
|
2013-09-26 11:22:22 +00:00
|
|
|
this.proxy.GetInitialResultSetRemote(terms,
|
|
|
|
Lang.bind(this, this._getResultsFinished),
|
|
|
|
this._cancellable);
|
2011-11-16 23:11:05 +00:00
|
|
|
},
|
|
|
|
|
2012-05-02 19:54:25 +00:00
|
|
|
getSubsearchResultSet: function(previousResults, newTerms) {
|
2011-11-16 23:11:05 +00:00
|
|
|
this._cancellable.cancel();
|
|
|
|
this._cancellable.reset();
|
2013-09-26 11:22:22 +00:00
|
|
|
this.proxy.GetSubsearchResultSetRemote(previousResults, newTerms,
|
|
|
|
Lang.bind(this, this._getResultsFinished),
|
|
|
|
this._cancellable);
|
2011-11-16 23:11:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_getResultMetasFinished: function(results, error, callback) {
|
|
|
|
if (error) {
|
2013-09-26 11:22:22 +00:00
|
|
|
if (!error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED))
|
|
|
|
log('Received error from DBus search provider %s during GetResultMetas: %s'.format(this.id, String(error)));
|
2011-11-16 23:11:05 +00:00
|
|
|
callback([]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let metas = results[0];
|
|
|
|
let resultMetas = [];
|
|
|
|
for (let i = 0; i < metas.length; i++) {
|
2013-04-24 16:05:22 +00:00
|
|
|
for (let prop in metas[i]) {
|
|
|
|
// we can use the serialized icon variant directly
|
|
|
|
if (prop != 'icon')
|
|
|
|
metas[i][prop] = metas[i][prop].deep_unpack();
|
|
|
|
}
|
|
|
|
|
2011-11-16 23:11:05 +00:00
|
|
|
resultMetas.push({ id: metas[i]['id'],
|
|
|
|
name: metas[i]['name'],
|
2013-02-27 16:54:09 +00:00
|
|
|
description: metas[i]['description'],
|
2011-11-16 23:11:05 +00:00
|
|
|
createIcon: Lang.bind(this,
|
|
|
|
this.createIcon, metas[i]) });
|
|
|
|
}
|
|
|
|
callback(resultMetas);
|
|
|
|
},
|
|
|
|
|
2012-05-02 19:54:25 +00:00
|
|
|
getResultMetas: function(ids, callback) {
|
2011-11-16 23:11:05 +00:00
|
|
|
this._cancellable.cancel();
|
|
|
|
this._cancellable.reset();
|
2013-09-26 11:22:22 +00:00
|
|
|
this.proxy.GetResultMetasRemote(ids,
|
|
|
|
Lang.bind(this, this._getResultMetasFinished, callback),
|
|
|
|
this._cancellable);
|
2011-11-16 23:11:05 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
activateResult: function(id) {
|
2012-12-05 22:20:22 +00:00
|
|
|
this.proxy.ActivateResultRemote(id);
|
2012-11-26 21:55:25 +00: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 23:11:05 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2012-12-05 22:20:22 +00:00
|
|
|
const RemoteSearchProvider2 = new Lang.Class({
|
|
|
|
Name: 'RemoteSearchProvider2',
|
|
|
|
Extends: RemoteSearchProvider,
|
|
|
|
|
|
|
|
_init: function(appInfo, dbusName, dbusPath) {
|
2013-09-26 11:22:22 +00:00
|
|
|
this.parent(appInfo, dbusName, dbusPath, SearchProvider2ProxyInfo);
|
2012-11-26 21:55:25 +00:00
|
|
|
|
|
|
|
this.canLaunchSearch = true;
|
2012-12-05 22:20:22 +00:00
|
|
|
},
|
2011-11-16 23:11:05 +00:00
|
|
|
|
2012-12-05 16:48:22 +00:00
|
|
|
activateResult: function(id, terms) {
|
|
|
|
this.proxy.ActivateResultRemote(id, terms, global.get_current_time());
|
2012-11-26 21:55:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
launchSearch: function(terms) {
|
2012-12-10 20:47:25 +00:00
|
|
|
this.proxy.LaunchSearchRemote(terms, global.get_current_time());
|
2012-12-05 22:20:22 +00:00
|
|
|
}
|
|
|
|
});
|