File: searchProviderManager.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 (103 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (2)
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
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-

const Extension = imports.ui.extension;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Lang = imports.lang;

// Maps uuid -> importer object (extension directory tree)
var extensions;
// Maps uuid -> metadata object
var extensionMeta;
// Maps uuid -> extension state object (returned from init())
const searchProviderObj = {};
// Arrays of uuids
var enabledSearchProviders;

const ENABLED_SEARCH_PROVIDERS_KEY = 'enabled-search-providers';

// Callback for extension.js
function prepareExtensionUnload(extension) {
    delete searchProviderObj[extension.uuid];
}

// Callback for extension.js
function finishExtensionLoad(extension) {
    searchProviderObj[extension.uuid] = extension.module;
    return true;
}

function onEnabledSearchProvidersChanged() {
    enabledSearchProviders = global.settings.get_strv(ENABLED_SEARCH_PROVIDERS_KEY);

    for(let uuid in Extension.Type.SEARCH_PROVIDER.maps.objects) {
        if(enabledSearchProviders.indexOf(uuid) == -1)
            Extension.unloadExtension(uuid, Extension.Type.SEARCH_PROVIDER);
    }
    
    for(let i=0; i<enabledSearchProviders.length; i++) {
        Extension.loadExtension(enabledSearchProviders[i], Extension.Type.SEARCH_PROVIDER);
    }
}

function init() {
    extensions = Extension.Type.SEARCH_PROVIDER.maps.importObjects;
    extensionMeta = Extension.Type.SEARCH_PROVIDER.maps.meta;

    global.settings.connect('changed::' + ENABLED_SEARCH_PROVIDERS_KEY, onEnabledSearchProvidersChanged);
    
    enabledSearchProviders = global.settings.get_strv(ENABLED_SEARCH_PROVIDERS_KEY);
    for (let i = 0; i < enabledSearchProviders.length; i++){
        Extension.loadExtension(enabledSearchProviders[i], Extension.Type.SEARCH_PROVIDER);
    }
}

function get_object_for_uuid(uuid){
    return searchProviderObj[uuid];
}

function launch_all(pattern, callback){
    var provider, supports_locale, language_names;
    for (var i in enabledSearchProviders){
        try{
            provider = get_object_for_uuid(enabledSearchProviders[i]);
            provider.uuid = enabledSearchProviders[i];
            if (provider)
            {
                if (extensionMeta[enabledSearchProviders[i]] && extensionMeta[enabledSearchProviders[i]].supported_locales){
                    supports_locale = false;
                    language_names = GLib.get_language_names();
                    for (var j in language_names){
                        if (extensionMeta[enabledSearchProviders[i]].supported_locales.indexOf(language_names[j]) != -1){
                            supports_locale = true;
                            break;
                        }
                    }
                }else{
                    supports_locale = true;
                }
                if (supports_locale){
                    provider.send_results = Lang.bind(this, function(results, p, cb){
                        cb(p, results);
                    }, provider, callback);
                    provider.get_locale_string = Lang.bind(this, function(key, providerData){
                        if (extensionMeta[providerData] && extensionMeta[providerData].locale_data && extensionMeta[providerData].locale_data[key]){
                            language_names = GLib.get_language_names();
                            for (var j in language_names){
                                if (extensionMeta[providerData].locale_data[key][language_names[j]]){
                                    return extensionMeta[providerData].locale_data[key][language_names[j]];
                                }
                            }
                        }
                        return "";
                    }, enabledSearchProviders[i]);
                    provider.perform_search(pattern);
                }
            }
        }
        catch(e)
        {
            global.logError(e);
        }
    }
}