~strycore/ubuntu/vivid/gnome-weather/fix-for-1456400

« back to all changes in this revision

Viewing changes to src/service/searchProvider.js

  • Committer: Package Import Robot
  • Author(s): Jackson Doak
  • Date: 2014-12-20 18:32:08 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20141220183208-e2895ntfyv23x1m3
Tags: 3.14.1-0ubuntu1
* New upstream release.
* Fix a typo in watch file
* Change binary to arch: all
* Update d/copyright for this release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: js; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*-
 
2
//
 
3
// Copyright (c) 2013 Giovanni Campagna <scampa.giovanni@gmail.com>
 
4
//
 
5
// Gnome Weather is free software; you can redistribute it and/or modify
 
6
// it under the terms of the GNU General Public License as published by the
 
7
// Free Software Foundation; either version 2 of the License, or (at your
 
8
// option) any later version.
 
9
//
 
10
// Gnome Weather is distributed in the hope that it will be useful, but
 
11
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
12
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
13
// for more details.
 
14
//
 
15
// You should have received a copy of the GNU General Public License along
 
16
// with Gnome Weather; if not, write to the Free Software Foundation,
 
17
// Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 
 
19
const Gio = imports.gi.Gio;
 
20
const GLib = imports.gi.GLib;
 
21
const GWeather = imports.gi.GWeather;
 
22
const Lang = imports.lang;
 
23
 
 
24
const Util = imports.misc.util;
 
25
const World = imports.shared.world;
 
26
 
 
27
const SearchProviderInterface = Gio.resources_lookup_data('/org/gnome/shell/ShellSearchProvider2.xml', 0).toArray().toString();
 
28
 
 
29
function getCountryName(location) {
 
30
    while (location &&
 
31
           location.get_level() > GWeather.LocationLevel.COUNTRY)
 
32
        location = location.get_parent();
 
33
 
 
34
    return location.get_name();
 
35
}
 
36
 
 
37
const SearchProvider = new Lang.Class({
 
38
    Name: 'WeatherSearchProvider',
 
39
 
 
40
    _init: function(application) {
 
41
        this._app = application;
 
42
 
 
43
        this._impl = Gio.DBusExportedObject.wrapJSObject(SearchProviderInterface, this);
 
44
    },
 
45
 
 
46
    export: function(connection, path) {
 
47
        return this._impl.export(connection, path);
 
48
    },
 
49
 
 
50
    unexport: function(connection) {
 
51
        return this._impl.unexport_from_connection(connection);
 
52
    },
 
53
 
 
54
    GetInitialResultSetAsync: function(params, invocation) {
 
55
        this._app.hold();
 
56
 
 
57
        let terms = params[0];
 
58
        let model = this._app.model;
 
59
 
 
60
        if (model.loading) {
 
61
            let notifyId = model.connect('notify::loading', Lang.bind(this, function(model) {
 
62
                if (!model.loading) {
 
63
                    model.disconnect(notifyId);
 
64
                    this._runQuery(terms, invocation);
 
65
                }
 
66
            }));
 
67
        } else {
 
68
            this._runQuery(terms, invocation);
 
69
        }
 
70
    },
 
71
 
 
72
    _runQuery: function(terms, invocation) {
 
73
        let nameRet = [];
 
74
        let cityRet = [];
 
75
        let countryRet = [];
 
76
 
 
77
        let model = this._app.model;
 
78
 
 
79
        let index = 0;
 
80
        for (let info of model.getAll()) {
 
81
            let location = info.location;
 
82
 
 
83
            let name = Util.normalizeCasefoldAndUnaccent(location.get_name());
 
84
            let city = Util.normalizeCasefoldAndUnaccent(location.get_city_name());
 
85
            let country = Util.normalizeCasefoldAndUnaccent(getCountryName(location));
 
86
 
 
87
            let nameMatch = false;
 
88
            let cityMatch = false;
 
89
            let countryMatch = false;
 
90
            let good = true;
 
91
            for (let i = 0; i < terms.length && good; i++) {
 
92
                terms[i] = Util.normalizeCasefoldAndUnaccent(terms[i]);
 
93
 
 
94
                if (name.indexOf(terms[i]) >= 0) {
 
95
                    nameMatch = true;
 
96
                } else if (city.indexOf(terms[i]) >= 0) {
 
97
                    cityMatch = true;
 
98
                } else if (country.indexOf(terms[i]) >= 0) {
 
99
                    countryMatch = true;
 
100
                } else {
 
101
                    good = false;
 
102
                }
 
103
 
 
104
                //log ('Comparing %s against (%s, %s, %s): %s'.format(terms[i],
 
105
                //                                                    name, city, country, good));
 
106
            }
 
107
 
 
108
            if (good) {
 
109
                let path = index.toString();
 
110
 
 
111
                if (nameMatch)
 
112
                    nameRet.push(path);
 
113
                else if (cityMatch)
 
114
                    cityRet.push(path);
 
115
                else
 
116
                    countryRet.push(path);
 
117
            }
 
118
        }
 
119
 
 
120
        this._app.release();
 
121
 
 
122
        let result = nameRet.concat(cityRet).concat(countryRet);
 
123
        log(result);
 
124
        invocation.return_value(new GLib.Variant('(as)', [result]));
 
125
    },
 
126
 
 
127
    GetSubsearchResultSet: function(previous, terms) {
 
128
        this._app.hold();
 
129
 
 
130
        let model = this._app.model;
 
131
        let ret = [];
 
132
 
 
133
        for (let i = 0; i < previous.length; i++) {
 
134
            let info = model.getAtIndex(parseInt(previous[i]));
 
135
            if (!info)
 
136
                continue;
 
137
 
 
138
            let location = info.location;
 
139
            let name = Util.normalizeCasefoldAndUnaccent(location.get_name());
 
140
            let city = Util.normalizeCasefoldAndUnaccent(location.get_city_name());
 
141
            let country = Util.normalizeCasefoldAndUnaccent(getCountryName(location));
 
142
            let good = true;
 
143
 
 
144
            for (let j = 0; j < terms.length && good; j++) {
 
145
                terms[j] = Util.normalizeCasefoldAndUnaccent(terms[j]);
 
146
 
 
147
                good = (name.indexOf(terms[j]) >= 0) ||
 
148
                    (city.indexOf(terms[j]) >= 0) ||
 
149
                    (country.indexOf(terms[j]) >= 0);
 
150
 
 
151
                //log ('Comparing %s against (%s, %s, %s): %s'.format(terms[i],
 
152
                //                                                    name, city, country, good));
 
153
            }
 
154
 
 
155
            if (good)
 
156
                ret.push(previous[i]);
 
157
        }
 
158
 
 
159
        this._app.release();
 
160
 
 
161
        return ret;
 
162
    },
 
163
 
 
164
    GetResultMetas: function(identifiers) {
 
165
        this._app.hold();
 
166
 
 
167
        let model = this._app.model;
 
168
        let ret = [];
 
169
 
 
170
        for (let i = 0; i < identifiers.length; i++) {
 
171
            let info = model.getAtIndex(parseInt(identifiers[i]));
 
172
            if (!info)
 
173
                continue;
 
174
 
 
175
            let location = info.location;
 
176
            let name = location.get_city_name();
 
177
            let conditions = Util.getWeatherConditions(info);
 
178
 
 
179
            // TRANSLATORS: this is the description shown in the overview search
 
180
            // It's the current weather conditions followed by the temperature,
 
181
            // like "Clear sky, 14 °C"
 
182
            let summary = _("%s, %s").format(conditions, info.get_temp());
 
183
            ret.push({ name: new GLib.Variant('s', name),
 
184
                       id: new GLib.Variant('s', identifiers[i]),
 
185
                       description: new GLib.Variant('s', summary),
 
186
                       icon: (new Gio.ThemedIcon({ name: info.get_icon_name() })).serialize()
 
187
                     });
 
188
        }
 
189
 
 
190
        this._app.release();
 
191
 
 
192
        return ret;
 
193
    },
 
194
 
 
195
    _getPlatformData: function(timestamp) {
 
196
        return {'desktop-startup-id': new GLib.Variant('s', '_TIME' + timestamp) };
 
197
    },
 
198
 
 
199
    _activateAction: function(action, parameter, timestamp) {
 
200
        let wrappedParam;
 
201
        if (parameter)
 
202
            wrappedParam = [parameter];
 
203
        else
 
204
            wrappedParam = [];
 
205
 
 
206
        Gio.DBus.session.call('org.gnome.Weather.Application',
 
207
                              '/org/gnome/Weather/Application',
 
208
                              'org.freedesktop.Application',
 
209
                              'ActivateAction',
 
210
                              new GLib.Variant('(sava{sv})', [action, wrappedParam,
 
211
                                                              this._getPlatformData(timestamp)]),
 
212
                              null,
 
213
                              Gio.DBusCallFlags.NONE,
 
214
                              -1, null, Lang.bind(this, function(connection, result) {
 
215
                                  try {
 
216
                                      connection.call_finish(result);
 
217
                                  } catch(e) {
 
218
                                      log('Failed to launch application: ' + e);
 
219
                                  }
 
220
 
 
221
                                  this._app.release();
 
222
                              }));
 
223
    },
 
224
 
 
225
    ActivateResult: function(id, terms, timestamp) {
 
226
        this._app.hold();
 
227
 
 
228
        log('Activating ' + id);
 
229
 
 
230
        let model = this._app.model;
 
231
        let info = model.getAtIndex(parseInt(id));
 
232
        if (!info) {
 
233
            this._app.release();
 
234
            return;
 
235
        }
 
236
 
 
237
        log('Activating ' + info.get_location_name());
 
238
 
 
239
        let location = info.location.serialize();
 
240
        this._activateAction('show-location', new GLib.Variant('v', location), timestamp);
 
241
    },
 
242
 
 
243
    LaunchSearch: function(terms, timestamp) {
 
244
        // not implemented
 
245
    },
 
246
});