File: search.js

package info (click to toggle)
cinnamon 3.2.7-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,624 kB
  • sloc: ansic: 33,269; python: 18,048; xml: 1,504; makefile: 780; sh: 90; cpp: 54
file content (437 lines) | stat: -rw-r--r-- 12,924 bytes parent folder | download
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-

const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Lang = imports.lang;
const Signals = imports.signals;
const Cinnamon = imports.gi.Cinnamon;
const Util = imports.misc.util;

const FileUtils = imports.misc.fileUtils;
const Main = imports.ui.main;

const DISABLED_OPEN_SEARCH_PROVIDERS_KEY = 'disabled-open-search-providers';

// Not currently referenced by the search API, but
// this enumeration can be useful for provider
// implementations.
const MatchType = {
    NONE: 0,
    SUBSTRING: 1,
    MULTIPLE_SUBSTRING: 2,
    PREFIX: 3,
    MULTIPLE_PREFIX: 4
};

function SearchResultDisplay(provider) {
    this._init(provider);
}

SearchResultDisplay.prototype = {
    _init: function(provider) {
        this.provider = provider;
        this.actor = null;
        this.selectionIndex = -1;
    },

    /**
     * renderResults:
     * @results: List of identifier strings
     * @terms: List of search term strings
     *
     * Display the given search matches which resulted
     * from the given terms.  It's expected that not
     * all results will fit in the space for the container
     * actor; in this case, show as many as makes sense
     * for your result type.
     *
     * The terms are useful for search match highlighting.
     */
    renderResults: function(results, terms) {
        throw new Error('Not implemented');
    },

    /**
     * clear:
     * Remove all results from this display and reset the selection index.
     */
    clear: function() {
        this.actor.destroy_all_children();
        this.selectionIndex = -1;
    },

    /**
     * getSelectionIndex:
     *
     * Returns the index of the selected actor, or -1 if none.
     */
    getSelectionIndex: function() {
        return this.selectionIndex;
    },

    /**
     * getVisibleResultCount:
     *
     * Returns: The number of actors visible.
     */
    getVisibleResultCount: function() {
        throw new Error('Not implemented');
    },

    /**
     * selectIndex:
     * @index: Integer index
     *
     * Move selection to the given index.
     * Return true if successful, false if no more results
     * available.
     */
    selectIndex: function() {
        throw new Error('Not implemented');
    },

    /**
     * Activate the currently selected search result.
     */
    activateSelected: function() {
        throw new Error('Not implemented');
    }
};

/**
 * SearchProvider:
 *
 * Subclass this object to add a new result type
 * to the search system, then call registerProvider()
 * in SearchSystem with an instance.
 */
function SearchProvider(title) {
    this._init(title);
}

SearchProvider.prototype = {
    _init: function(title) {
        this.title = title;
        this.searchSystem = null;
        this.searchAsync  = false;
    },

    _asyncCancelled: function() {
    },

    startAsync: function() {
        this.searchAsync = true;
    },

    tryCancelAsync: function() {
        if (!this.searchAsync)
            return;
        this._asyncCancelled();
        this.searchAsync = false;
    },

    /**
     * addItems:
     * @items: an array of result identifier strings representing
     * items which match the last given search terms.
     *
     * This should be used for something that requires a bit more
     * logic; it's designed to be an asyncronous way to add a result
     * to the current search.
     */
    addItems: function(items) {
        if (!this.searchSystem)
            throw new Error('Search provider not registered');

        if (!items.length)
            return;

        this.tryCancelAsync();

        this.searchSystem.addProviderItems(this, items);
    },

    /**
     * getInitialResultSet:
     * @terms: Array of search terms, treated as logical AND
     *
     * Called when the user first begins a search (most likely
     * therefore a single term of length one or two), or when
     * a new term is added.
     *
     * Should return an array of result identifier strings representing
     * items which match the given search terms.  This
     * is expected to be a substring match on the metadata for a given
     * item.  Ordering of returned results is up to the discretion of the provider,
     * but you should follow these heruistics:
     *
     *  * Put items where the term matches multiple criteria (e.g. name and
     *    description) before single matches
     *  * Put items which match on a prefix before non-prefix substring matches
     *
     * This function should be fast; do not perform unindexed full-text searches
     * or network queries.
     */
    getInitialResultSet: function(terms) {
        throw new Error('Not implemented');
    },

    /**
     * getSubsearchResultSet:
     * @previousResults: Array of item identifiers
     * @newTerms: Updated search terms
     *
     * Called when a search is performed which is a "subsearch" of
     * the previous search; i.e. when every search term has exactly
     * one corresponding term in the previous search which is a prefix
     * of the new term.
     *
     * This allows search providers to only search through the previous
     * result set, rather than possibly performing a full re-query.
     */
    getSubsearchResultSet: function(previousResults, newTerms) {
        throw new Error('Not implemented');
    },

    /**
     * getResultMeta:
     * @id: Result identifier string
     *
     * Return an object with 'id', 'name', (both strings) and 'createIcon'
     * (function(size) returning a Clutter.Texture) properties which describe
     * the given search result.
     */
    getResultMeta: function(id) {
        throw new Error('Not implemented');
    },

    /**
     * createResultContainer:
     *
     * Search providers may optionally override this to render their
     * results in a custom fashion.  The default implementation
     * will create a vertical list.
     *
     * Returns: An instance of SearchResultDisplay.
     */
    createResultContainerActor: function() {
        return null;
    },

    /**
     * createResultActor:
     * @resultMeta: Object with result metadata
     * @terms: Array of search terms, should be used for highlighting
     *
     * Search providers may optionally override this to render a
     * particular serch result in a custom fashion.  The default
     * implementation will show the icon next to the name.
     *
     * The actor should be an instance of St.Widget, with the style class
     * 'search-result-content'.
     */
    createResultActor: function(resultMeta, terms) {
        return null;
    },

    /**
     * activateResult:
     * @id: Result identifier string
     *
     * Called when the user chooses a given result.
     */
    activateResult: function(id) {
        throw new Error('Not implemented');
    }
};
Signals.addSignalMethods(SearchProvider.prototype);

function OpenSearchSystem() {
    this._init();
}

OpenSearchSystem.prototype = {
    _init: function() {
        this._providers = [];
        global.settings.connect('changed::' + DISABLED_OPEN_SEARCH_PROVIDERS_KEY, Lang.bind(this, this._refresh));
        this._refresh();
    },

    getProviders: function() {
        let res = [];
        for (let i = 0; i < this._providers.length; i++)
            res.push({ id: i, name: this._providers[i].name });

        return res;
    },

    setSearchTerms: function(terms) {
        this._terms = terms;
    },

    _checkSupportedProviderLanguage: function(provider) {
        if (provider.url.search(/{language}/) == -1)
            return true;

        let langs = GLib.get_language_names();

        langs.push('en');
        let lang = null;
        for (let i = 0; i < langs.length; i++) {
            for (let k = 0; k < provider.langs.length; k++) {
                if (langs[i] == provider.langs[k])
                    lang = langs[i];
            }
            if (lang)
                break;
        }
        provider.lang = lang;
        return lang != null;
    },

    activateResult: function(id, params) {
        let searchTerms = this._terms.join(' ');

        let url = this._providers[id].url.replace('{searchTerms}', encodeURIComponent(searchTerms));
        if (url.match('{language}'))
            url = url.replace('{language}', this._providers[id].lang);

        try {
            Gio.app_info_launch_default_for_uri(url, global.create_app_launch_context());
        } catch (e) {
            // TODO: remove this after glib will be removed from moduleset
            // In the default jhbuild, gio is in our prefix but gvfs is not
            Util.spawn(['gvfs-open', url])
        }

        Main.overview.hide();
    },

    _addProvider: function(fileName) {
        let path = global.datadir + '/search_providers/' + fileName;
        let source = Cinnamon.get_file_contents_utf8_sync(path);
        let [success, name, url, langs, icon_uri] = Cinnamon.parse_search_provider(source);
        let provider ={ name: name,
                        url: url,
                        id: this._providers.length,
                        icon_uri: icon_uri,
                        langs: langs };
        if (this._checkSupportedProviderLanguage(provider)) {
            this._providers.push(provider);
            this.emit('changed');
        }
    },

    _refresh: function() {
        this._providers = [];
        let names = global.settings.get_strv(DISABLED_OPEN_SEARCH_PROVIDERS_KEY);
        let file = Gio.file_new_for_path(global.datadir + '/search_providers');
        FileUtils.listDirAsync(file, Lang.bind(this, function(files) {
            for (let i = 0; i < files.length; i++) {
                let enabled = true;
                let name = files[i].get_name();
                for (let k = 0; k < names.length; k++)
                    if (names[k] == name)
                        enabled = false;
                if (enabled)
                    this._addProvider(name);
            }
        }));
    }
}
Signals.addSignalMethods(OpenSearchSystem.prototype);

function SearchSystem() {
    this._init();
}

SearchSystem.prototype = {
    _init: function() {
        this._providers = [];
        this.reset();
    },

    registerProvider: function (provider) {
        provider.searchSystem = this;
        this._providers.push(provider);
    },

    unregisterProvider: function (provider) {
        let index = this._providers.indexOf(provider);
        if (index == -1)
            return;
        provider.searchSystem = null;
        this._providers.splice(index, 1);
    },

    getProviders: function() {
        return this._providers;
    },

    getTerms: function() {
        return this._previousTerms;
    },

    reset: function() {
        this._previousTerms = [];
        this._previousResults = [];
    },

    addProviderItems: function(provider, items) {
        this.emit('search-updated', provider, items);
    },

    updateSearch: function(searchString) {
        searchString = searchString.replace(/^\s+/g, '').replace(/\s+$/g, '');
        if (searchString == '')
            return;

        let terms = searchString.split(/\s+/);
        this.updateSearchResults(terms);
    },

    updateSearchResults: function(terms) {
        if (!terms)
            return;

        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;
                }
            }
        }

        let results = [];
        if (isSubSearch) {
            for (let i = 0; i < this._providers.length; i++) {
                let [provider, previousResults] = this._previousResults[i];
                provider.tryCancelAsync();
                try {
                    let providerResults = provider.getSubsearchResultSet(previousResults, terms);
                    results.push([provider, providerResults]);
                } catch (error) {
                    global.logError('A ' + error.name + ' has occurred in ' + provider.title, error);
                }
            }
        } else {
            for (let i = 0; i < this._providers.length; i++) {
                let provider = this._providers[i];
                provider.tryCancelAsync();
                try {
                    let providerResults = provider.getInitialResultSet(terms);
                    results.push([provider, providerResults]);
                } catch (error) {
                    global.logError('A ' + error.name + ' has occurred in ' + provider.title, error);
                }
            }
        }

        this._previousTerms = terms;
        this._previousResults = results;
        this.emit('search-completed', results);
    },
};
Signals.addSignalMethods(SearchSystem.prototype);