forked from sleidig/gnome_sample-search-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
335 lines (285 loc) · 11.4 KB
/
extension.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
* Sample Search Provider
* A sample and base project to integrate custom search providers in GNOME Shell.
* This code provides a simple outline for a GNOME Extension that adds a new search into
* the GNOME Shell search.
*
* Copyright (C) 2019
* Sebastian Leidig <[email protected]
*
* based on WordReference Search Provider by
* Lorenzo Carbonell <[email protected]>, https://www.atareao.es
*
* This file is part of Sample Search Provider
*
* Sample Search Provider is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sample Search Provider is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gnome-shell-extension-openweather.
* If not, see <http://www.gnu.org/licenses/>.
*/
/** IMPORTS **/
const St = imports.gi.St;
const Main = imports.ui.main;
const Gio = imports.gi.Gio;
const Gtk = imports.gi.Gtk;
const GLib = imports.gi.GLib;
const Clutter = imports.gi.Clutter;
const Util = imports.misc.util;
const Extension = imports.misc.extensionUtils.getCurrentExtension();
const Utils = Extension.imports.utils;
const Gettext = imports.gettext.domain(Extension.metadata.uuid);
const _ = Gettext.gettext;
/** APP CONFIG **/
const APP_NAME = 'Sample Search';
const ICON_NAME = 'sample-search';
const APP_COMMAND_FACTORY = (terms => { return 'xdg-open http://www.google.com/search?q=' + terms; });
const SEARCH_TERMS_FILTER = (terms => { return (terms[0].substring(0, 5) === 'demo:'); }); // display the search provider only if search term by user starts with "demo:"
const SEARCH_CLIENT = Extension.imports.search_client_sample_from_console; // see search_client_sample_from_web.js or search_client_sample_from_console.js
/**
* Wraps a "Search Client" implementation (see search_client_sample.js) and
* implements the methods required for GNOME Shell Extension to be used as a search provider.
* An instance of this class can be registered with GNOME using "Main.overview.viewSelector._searchResults._registerProvider(genericSearchProviderInstance)"
*/
class GenericSearchProvider {
/**
* Create a new GenericSearchProvider configured with the passed arguments:
* @param {string} appName The human-readable name displayed for the extension.
* @param {string} iconName The filename of the icon within the extensions "icons" subfolder
* @param {Function} appCommandFactory A function receiving the search terms generating a command to launch the search in the app.
* @param {Function} isRelevantSearchTermsFunction A function receiving the search terms as a parameter and evaluating to true if the SearchProvider should be displayed for this query.
* @param {SearchClient} clientApi An instance of a SearchClient managing the retrieval of actual results.
*/
constructor(appName, iconName, appCommandFactory, isRelevantSearchTermsFunction, clientApi) {
this._appName = appName;
this._iconName = iconName;
this._generateAppCommand = appCommandFactory;
this._isRelevantSearchTerms = isRelevantSearchTermsFunction;
this._api = clientApi;
Gtk.IconTheme.get_default().append_search_path(Extension.dir.get_child('icons').get_path());
this.appInfo = this._getAppInfoForOpeningFullSearch();
this._messages = this._getMessagesAsResults();
this.resultsMap = new Map(); // API results will be stored here; key:string, value:metaObject (see _getMessagesAsResults() for examples)
// Wait before making an API request
this._timeoutId = 0;
}
/**
* Get AppInfo of the app with which full search should be launched.
* @returns {Gio.AppInfo}
* @private
*/
_getAppInfoForOpeningFullSearch() {
// Use the default app for opening https links as the app for launching full search.
let app = Gio.AppInfo.get_default_for_uri_scheme('https');
// Fake the name and icon of the app
app.get_name = () => { return this._appName; };
app.get_icon = () => { return new Gio.ThemedIcon({name: this._iconName}); };
return app;
}
/**
* Return custom messages that will be shown as search results
*/
_getMessagesAsResults() {
return {
'__loading__': {
id: '__loading__',
name: _(this._appName),
description : _('Loading items from '+this._appName+', please wait...'),
// TODO: do these kinds of icon creations better
createIcon: this._createIconGenerator()
},
'__error__': {
id: '__error__',
name: _(this._appName),
description : _('Oops, an error occurred while searching.'),
createIcon: this._createIconGenerator()
}
};
}
_createIconGenerator() {
let iconName = this._iconName;
return (size) => {
let box = new Clutter.Box();
let icon = new St.Icon({gicon: new Gio.ThemedIcon({name: iconName}), icon_size: size});
box.add_child(icon);
return box;
};
}
/**
* Search API if the query is matches the SEARCH_TERMS_FILTER for the app.
* This function is called by GNOME Shell when the user starts a search.
* @param {Array} terms
* @param {Function} callback
* @param {Gio.Cancellable} cancellable
*/
getInitialResultSet(terms, callback, cancellable) {
if (terms != null && terms.length >= 1 && this._isRelevantSearchTerms(terms)) {
// show the loading message
this._showMessage('__loading__', callback);
// remove previous timeout
if (this._timeoutId > 0) {
GLib.source_remove(this._timeoutId);
this._timeoutId = 0;
}
this._timeoutId = GLib.timeout_add(GLib.PRIORITY_DEFAULT, 800, () => {
// now search
this._api.get(
terms.join(' '),
(error, result) => { this._getResultSet.bind(this)(error, result, callback, this._timeoutId); }
);
return false;
});
} else {
// return an emtpy result set
this._getResultSet(null, {}, callback, 0);
}
}
/**
* Show any message as a search item
* @param {String} identifier Message identifier
* @param {Function} callback Callback that pushes the result to search overview
*/
_showMessage(identifier, callback) {
callback([identifier]);
}
/**
* Parse results that we get from the API and save them in this.resultsMap.
* Inform the user if no results are found.
* @param {null|String} error
* @param {Object|null} result
* @param {Function} callback
* @private
*/
_getResultSet(error, result, callback, timeoutId) {
let results = [];
if (timeoutId === this._timeoutId && result && result.length > 0) {
result.forEach((aresult) => {
this.resultsMap.set(aresult.id, aresult);
results.push(aresult.id);
});
callback(results);
} else if (error) {
// Let the user know that an error has occurred.
this._showMessage('__error__', callback);
}
}
/**
* Run callback with results
* //TODO: what does this mean? When/How is this executed exactly?
* @param {Array} identifiers
* @param {Function} callback
*/
getResultMetas(identifiers, callback) {
let metas = [];
for (let i = 0; i < identifiers.length; i++) {
let result;
// return predefined message if it exists
if (identifiers[i] in this._messages) {
metas.push(this._messages[identifiers[i]]);
} else {
// TODO: check for messages that don't exist, show generic error message
let meta = this.resultsMap.get(identifiers[i]);
if (meta){
metas.push({
id: meta.id,
name: meta.name,
description: meta.description,
createIcon: this._createIconGenerator()
});
}
}
}
callback(metas);
}
/**
* Open the url in default app.
* This function is called by GNOME Shell when the user clicks on a result.
* @param {String} identifier
* @param {Array} terms
* @param timestamp
*/
activateResult(identifier, terms, timestamp) {
let result;
// only do something if the result is not a custom message
if (!(identifier in this._messages)) {
result = this.resultsMap.get(identifier);
if (result) {
Util.trySpawnCommandLine('xdg-open "' + result.url + '"');
}
}
}
/**
* Launch the search in the actual application.
* This function is called by GNOME Shell when the user clicks on the Extension icon & name displayed to the left of its results.
* @param {String[]} terms
*/
launchSearch(terms) {
Util.trySpawnCommandLine(this._generateAppCommand(terms));
}
/**
* Return subset of results
* @param {Array} results
* @param {number} max
* @returns {Array}
*/
filterResults(results, max) {
// override max for now
max = this._api.limit; //TODO: _api.limit is probably never defined! (at least WordReferenceClient never sets it)
return results.slice(0, max);
}
/**
* TODO: implement
* @param {Array} previousResults
* @param {Array} terms
* @returns {Array}
*/
getSubsetResultSearch(previousResults, terms) {
return [];
}
destroy() {
this._api.destroy();
}
}
let searchProvider = null;
/**
* Will be invoked by GNOME Shell at most once directly after your source JS file is loaded.
* Setup whatever needs to be setup here, such as labels, text, icons or actors, prior to enabling the extension. It is only called once per shell session.
* You should make user interfaces visible/hidden in the separate enable/disable functions.
*/
function init() {
Utils.initTranslations();
}
/**
* Will be invoked by GNOME Shell when the extension is enabled by the user in the OS.
* Make the Shell extension’s UI visible to the user here.
*/
function enable() {
if (!searchProvider) {
let client = SEARCH_CLIENT.getSearchClient();
searchProvider = new GenericSearchProvider(APP_NAME, ICON_NAME, APP_COMMAND_FACTORY, SEARCH_TERMS_FILTER, client);
Main.overview.viewSelector._searchResults._registerProvider(
searchProvider
);
}
}
/**
* Will be invoked by GNOME Shell when the extension is disabled by the user in the OS.
* Hide the Shell extension’s UI here.
*/
function disable() {
if (searchProvider){
searchProvider.destroy();
Main.overview.viewSelector._searchResults._unregisterProvider(
searchProvider
);
searchProvider = null;
}
}