1
// -*- Mode: js; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*-
3
// Copyright (c) 2013 Giovanni Campagna <scampa.giovanni@gmail.com>
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.
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
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
19
const Gio = imports.gi.Gio;
20
const GLib = imports.gi.GLib;
21
const GWeather = imports.gi.GWeather;
22
const Lang = imports.lang;
24
const Util = imports.misc.util;
25
const World = imports.shared.world;
27
const SearchProviderInterface = Gio.resources_lookup_data('/org/gnome/shell/ShellSearchProvider2.xml', 0).toArray().toString();
29
function getCountryName(location) {
31
location.get_level() > GWeather.LocationLevel.COUNTRY)
32
location = location.get_parent();
34
return location.get_name();
37
const SearchProvider = new Lang.Class({
38
Name: 'WeatherSearchProvider',
40
_init: function(application) {
41
this._app = application;
43
this._impl = Gio.DBusExportedObject.wrapJSObject(SearchProviderInterface, this);
46
export: function(connection, path) {
47
return this._impl.export(connection, path);
50
unexport: function(connection) {
51
return this._impl.unexport_from_connection(connection);
54
GetInitialResultSetAsync: function(params, invocation) {
57
let terms = params[0];
58
let model = this._app.model;
61
let notifyId = model.connect('notify::loading', Lang.bind(this, function(model) {
63
model.disconnect(notifyId);
64
this._runQuery(terms, invocation);
68
this._runQuery(terms, invocation);
72
_runQuery: function(terms, invocation) {
77
let model = this._app.model;
80
for (let info of model.getAll()) {
81
let location = info.location;
83
let name = Util.normalizeCasefoldAndUnaccent(location.get_name());
84
let city = Util.normalizeCasefoldAndUnaccent(location.get_city_name());
85
let country = Util.normalizeCasefoldAndUnaccent(getCountryName(location));
87
let nameMatch = false;
88
let cityMatch = false;
89
let countryMatch = false;
91
for (let i = 0; i < terms.length && good; i++) {
92
terms[i] = Util.normalizeCasefoldAndUnaccent(terms[i]);
94
if (name.indexOf(terms[i]) >= 0) {
96
} else if (city.indexOf(terms[i]) >= 0) {
98
} else if (country.indexOf(terms[i]) >= 0) {
104
//log ('Comparing %s against (%s, %s, %s): %s'.format(terms[i],
105
// name, city, country, good));
109
let path = index.toString();
116
countryRet.push(path);
122
let result = nameRet.concat(cityRet).concat(countryRet);
124
invocation.return_value(new GLib.Variant('(as)', [result]));
127
GetSubsearchResultSet: function(previous, terms) {
130
let model = this._app.model;
133
for (let i = 0; i < previous.length; i++) {
134
let info = model.getAtIndex(parseInt(previous[i]));
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));
144
for (let j = 0; j < terms.length && good; j++) {
145
terms[j] = Util.normalizeCasefoldAndUnaccent(terms[j]);
147
good = (name.indexOf(terms[j]) >= 0) ||
148
(city.indexOf(terms[j]) >= 0) ||
149
(country.indexOf(terms[j]) >= 0);
151
//log ('Comparing %s against (%s, %s, %s): %s'.format(terms[i],
152
// name, city, country, good));
156
ret.push(previous[i]);
164
GetResultMetas: function(identifiers) {
167
let model = this._app.model;
170
for (let i = 0; i < identifiers.length; i++) {
171
let info = model.getAtIndex(parseInt(identifiers[i]));
175
let location = info.location;
176
let name = location.get_city_name();
177
let conditions = Util.getWeatherConditions(info);
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()
195
_getPlatformData: function(timestamp) {
196
return {'desktop-startup-id': new GLib.Variant('s', '_TIME' + timestamp) };
199
_activateAction: function(action, parameter, timestamp) {
202
wrappedParam = [parameter];
206
Gio.DBus.session.call('org.gnome.Weather.Application',
207
'/org/gnome/Weather/Application',
208
'org.freedesktop.Application',
210
new GLib.Variant('(sava{sv})', [action, wrappedParam,
211
this._getPlatformData(timestamp)]),
213
Gio.DBusCallFlags.NONE,
214
-1, null, Lang.bind(this, function(connection, result) {
216
connection.call_finish(result);
218
log('Failed to launch application: ' + e);
225
ActivateResult: function(id, terms, timestamp) {
228
log('Activating ' + id);
230
let model = this._app.model;
231
let info = model.getAtIndex(parseInt(id));
237
log('Activating ' + info.get_location_name());
239
let location = info.location.serialize();
240
this._activateAction('show-location', new GLib.Variant('v', location), timestamp);
243
LaunchSearch: function(terms, timestamp) {