2017-02-23 16:55:33 -05:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2019-07-23 06:49:40 -04:00
|
|
|
const { Geoclue, Gio, GLib, GWeather, Shell } = imports.gi;
|
2017-02-23 16:55:33 -05:00
|
|
|
const Signals = imports.signals;
|
|
|
|
|
2017-03-19 11:29:35 -04:00
|
|
|
const PermissionStore = imports.misc.permissionStore;
|
2019-07-23 06:49:40 -04:00
|
|
|
|
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
2022-02-10 18:09:54 -05:00
|
|
|
Gio._promisify(Geoclue.Simple, 'new');
|
2019-12-19 14:50:37 -05:00
|
|
|
|
2019-07-23 06:49:40 -04:00
|
|
|
const WeatherIntegrationIface = loadInterfaceXML('org.gnome.Shell.WeatherIntegration');
|
|
|
|
|
|
|
|
const WEATHER_BUS_NAME = 'org.gnome.Weather';
|
|
|
|
const WEATHER_OBJECT_PATH = '/org/gnome/Weather';
|
|
|
|
const WEATHER_INTEGRATION_IFACE = 'org.gnome.Shell.WeatherIntegration';
|
|
|
|
|
|
|
|
const WEATHER_APP_ID = 'org.gnome.Weather.desktop';
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-02-24 19:36:47 -05:00
|
|
|
// Minimum time between updates to show loading indication
|
2017-07-18 13:47:27 -04:00
|
|
|
var UPDATE_THRESHOLD = 10 * GLib.TIME_SPAN_MINUTE;
|
2017-02-24 19:36:47 -05:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var WeatherClient = class {
|
|
|
|
constructor() {
|
2017-02-23 16:55:33 -05:00
|
|
|
this._loading = false;
|
2017-03-19 09:42:35 -04:00
|
|
|
this._locationValid = false;
|
2017-02-24 19:36:47 -05:00
|
|
|
this._lastUpdate = GLib.DateTime.new_from_unix_local(0);
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-03-19 11:34:53 -04:00
|
|
|
this._autoLocationRequested = false;
|
2017-02-23 16:55:33 -05:00
|
|
|
this._mostRecentLocation = null;
|
|
|
|
|
|
|
|
this._gclueService = null;
|
|
|
|
this._gclueStarted = false;
|
2017-03-11 07:46:10 -05:00
|
|
|
this._gclueStarting = false;
|
2017-02-23 16:55:33 -05:00
|
|
|
this._gclueLocationChangedId = 0;
|
|
|
|
|
2019-11-27 17:01:15 -05:00
|
|
|
this._needsAuth = true;
|
2017-03-19 11:29:35 -04:00
|
|
|
this._weatherAuthorized = false;
|
|
|
|
this._permStore = new PermissionStore.PermissionStore((proxy, error) => {
|
|
|
|
if (error) {
|
2019-01-29 19:18:24 -05:00
|
|
|
log(`Failed to connect to permissionStore: ${error.message}`);
|
2017-03-19 11:29:35 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-03 06:35:03 -04:00
|
|
|
if (this._permStore.g_name_owner == null) {
|
|
|
|
// Failed to auto-start, likely because xdg-desktop-portal
|
|
|
|
// isn't installed; don't restrict access to location service
|
|
|
|
this._weatherAuthorized = true;
|
|
|
|
this._updateAutoLocation();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-08-19 20:20:08 -04:00
|
|
|
this._permStore.LookupRemote('gnome', 'geolocation', (res, err) => {
|
|
|
|
if (err)
|
|
|
|
log(`Error looking up permission: ${err.message}`);
|
2017-03-19 11:29:35 -04:00
|
|
|
|
2019-08-19 20:20:08 -04:00
|
|
|
let [perms, data] = err ? [{}, null] : res;
|
2017-03-19 11:29:35 -04:00
|
|
|
let params = ['gnome', 'geolocation', false, data, perms];
|
|
|
|
this._onPermStoreChanged(this._permStore, '', params);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this._permStore.connectSignal('Changed',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._onPermStoreChanged.bind(this));
|
2017-03-19 11:29:35 -04:00
|
|
|
|
2017-03-19 09:31:19 -04:00
|
|
|
this._locationSettings = new Gio.Settings({ schema_id: 'org.gnome.system.location' });
|
|
|
|
this._locationSettings.connect('changed::enabled',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._updateAutoLocation.bind(this));
|
2017-03-19 09:31:19 -04:00
|
|
|
|
2017-02-23 16:55:33 -05:00
|
|
|
this._world = GWeather.Location.get_world();
|
|
|
|
|
2021-01-12 11:30:16 -05:00
|
|
|
const providers =
|
|
|
|
GWeather.Provider.METAR |
|
|
|
|
GWeather.Provider.MET_NO |
|
|
|
|
GWeather.Provider.OWM;
|
2021-01-12 11:31:15 -05:00
|
|
|
this._weatherInfo = new GWeather.Info({
|
|
|
|
application_id: 'org.gnome.Shell',
|
2021-07-14 14:07:22 -04:00
|
|
|
contact_info: 'https://gitlab.gnome.org/GNOME/gnome-shell/-/raw/HEAD/gnome-shell.doap',
|
2021-01-14 14:47:52 -05:00
|
|
|
enabled_providers: providers,
|
2021-01-12 11:31:15 -05:00
|
|
|
});
|
2017-02-23 16:55:33 -05:00
|
|
|
this._weatherInfo.connect_after('updated', () => {
|
2017-02-24 19:36:47 -05:00
|
|
|
this._lastUpdate = GLib.DateTime.new_now_local();
|
2017-02-23 16:55:33 -05:00
|
|
|
this.emit('changed');
|
|
|
|
});
|
|
|
|
|
2019-07-23 06:49:40 -04:00
|
|
|
this._weatherApp = null;
|
|
|
|
this._weatherProxy = null;
|
|
|
|
|
2019-12-19 14:50:37 -05:00
|
|
|
this._createWeatherProxy();
|
2019-07-23 06:49:40 -04:00
|
|
|
|
|
|
|
this._settings = new Gio.Settings({
|
2019-08-20 17:43:54 -04:00
|
|
|
schema_id: 'org.gnome.shell.weather',
|
2019-07-23 06:49:40 -04:00
|
|
|
});
|
|
|
|
this._settings.connect('changed::automatic-location',
|
|
|
|
this._onAutomaticLocationChanged.bind(this));
|
2019-08-01 09:18:43 -04:00
|
|
|
this._onAutomaticLocationChanged();
|
2019-07-23 06:49:40 -04:00
|
|
|
this._settings.connect('changed::locations',
|
|
|
|
this._onLocationsChanged.bind(this));
|
2019-08-01 09:18:43 -04:00
|
|
|
this._onLocationsChanged();
|
2019-07-23 06:49:40 -04:00
|
|
|
|
|
|
|
this._appSystem = Shell.AppSystem.get_default();
|
|
|
|
this._appSystem.connect('installed-changed',
|
|
|
|
this._onInstalledChanged.bind(this));
|
|
|
|
this._onInstalledChanged();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
|
|
|
get available() {
|
2019-07-23 06:49:40 -04:00
|
|
|
return this._weatherApp != null;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
|
|
|
get loading() {
|
|
|
|
return this._loading;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-03-19 09:42:35 -04:00
|
|
|
get hasLocation() {
|
|
|
|
return this._locationValid;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-03-19 09:42:35 -04:00
|
|
|
|
2017-02-23 16:55:33 -05:00
|
|
|
get info() {
|
|
|
|
return this._weatherInfo;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
activateApp() {
|
2019-07-23 06:49:40 -04:00
|
|
|
if (this._weatherApp)
|
|
|
|
this._weatherApp.activate();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
update() {
|
2017-03-19 09:42:35 -04:00
|
|
|
if (!this._locationValid)
|
|
|
|
return;
|
|
|
|
|
2017-02-24 19:36:47 -05:00
|
|
|
let now = GLib.DateTime.new_now_local();
|
|
|
|
// Update without loading indication if the current info is recent enough
|
|
|
|
if (this._weatherInfo.is_valid() &&
|
|
|
|
now.difference(this._lastUpdate) < UPDATE_THRESHOLD)
|
|
|
|
this._weatherInfo.update();
|
|
|
|
else
|
|
|
|
this._loadInfo();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-03-19 11:34:53 -04:00
|
|
|
get _useAutoLocation() {
|
2017-03-19 09:31:19 -04:00
|
|
|
return this._autoLocationRequested &&
|
2017-03-19 11:29:35 -04:00
|
|
|
this._locationSettings.get_boolean('enabled') &&
|
2019-11-27 17:01:15 -05:00
|
|
|
(!this._needsAuth || this._weatherAuthorized);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-03-19 11:34:53 -04:00
|
|
|
|
2019-12-19 14:50:37 -05:00
|
|
|
async _createWeatherProxy() {
|
|
|
|
const nodeInfo = Gio.DBusNodeInfo.new_for_xml(WeatherIntegrationIface);
|
2019-07-23 06:49:40 -04:00
|
|
|
try {
|
2019-12-19 14:50:37 -05:00
|
|
|
this._weatherProxy = await Gio.DBusProxy.new(
|
|
|
|
Gio.DBus.session,
|
|
|
|
Gio.DBusProxyFlags.DO_NOT_AUTO_START | Gio.DBusProxyFlags.GET_INVALIDATED_PROPERTIES,
|
|
|
|
nodeInfo.lookup_interface(WEATHER_INTEGRATION_IFACE),
|
|
|
|
WEATHER_BUS_NAME,
|
|
|
|
WEATHER_OBJECT_PATH,
|
|
|
|
WEATHER_INTEGRATION_IFACE,
|
|
|
|
null);
|
2019-07-23 06:49:40 -04:00
|
|
|
} catch (e) {
|
|
|
|
log(`Failed to create GNOME Weather proxy: ${e}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._weatherProxy.connect('g-properties-changed',
|
|
|
|
this._onWeatherPropertiesChanged.bind(this));
|
2019-07-31 19:24:13 -04:00
|
|
|
this._onWeatherPropertiesChanged();
|
2019-07-23 06:49:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_onWeatherPropertiesChanged() {
|
2019-07-31 19:24:13 -04:00
|
|
|
if (this._weatherProxy.g_name_owner == null)
|
|
|
|
return;
|
|
|
|
|
2019-07-23 06:49:40 -04:00
|
|
|
this._settings.set_boolean('automatic-location',
|
|
|
|
this._weatherProxy.AutomaticLocation);
|
|
|
|
this._settings.set_value('locations',
|
|
|
|
new GLib.Variant('av', this._weatherProxy.Locations));
|
|
|
|
}
|
|
|
|
|
|
|
|
_onInstalledChanged() {
|
2019-08-19 15:38:51 -04:00
|
|
|
let hadApp = this._weatherApp != null;
|
2019-07-23 06:49:40 -04:00
|
|
|
this._weatherApp = this._appSystem.lookup_app(WEATHER_APP_ID);
|
2019-08-19 15:38:51 -04:00
|
|
|
let haveApp = this._weatherApp != null;
|
2019-07-23 06:49:40 -04:00
|
|
|
|
|
|
|
if (hadApp !== haveApp)
|
|
|
|
this.emit('changed');
|
2019-11-27 17:01:15 -05:00
|
|
|
|
|
|
|
let neededAuth = this._needsAuth;
|
|
|
|
this._needsAuth = this._weatherApp === null ||
|
|
|
|
this._weatherApp.app_info.has_key('X-Flatpak');
|
|
|
|
|
|
|
|
if (neededAuth !== this._needsAuth)
|
|
|
|
this._updateAutoLocation();
|
2019-07-23 06:49:40 -04:00
|
|
|
}
|
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_loadInfo() {
|
2017-02-23 16:55:33 -05:00
|
|
|
let id = this._weatherInfo.connect('updated', () => {
|
|
|
|
this._weatherInfo.disconnect(id);
|
|
|
|
this._loading = false;
|
|
|
|
});
|
|
|
|
|
|
|
|
this._loading = true;
|
|
|
|
this.emit('changed');
|
|
|
|
|
|
|
|
this._weatherInfo.update();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_locationsEqual(loc1, loc2) {
|
2017-02-23 16:55:33 -05:00
|
|
|
if (loc1 == loc2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (loc1 == null || loc2 == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return loc1.equal(loc2);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_setLocation(location) {
|
2017-02-23 16:55:33 -05:00
|
|
|
if (this._locationsEqual(this._weatherInfo.location, location))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._weatherInfo.abort();
|
|
|
|
this._weatherInfo.set_location(location);
|
2019-08-19 15:38:51 -04:00
|
|
|
this._locationValid = location != null;
|
2017-02-23 16:55:33 -05:00
|
|
|
|
|
|
|
if (location)
|
|
|
|
this._loadInfo();
|
|
|
|
else
|
|
|
|
this.emit('changed');
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateLocationMonitoring() {
|
2017-02-23 16:55:33 -05:00
|
|
|
if (this._useAutoLocation) {
|
|
|
|
if (this._gclueLocationChangedId != 0 || this._gclueService == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._gclueLocationChangedId =
|
|
|
|
this._gclueService.connect('notify::location',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._onGClueLocationChanged.bind(this));
|
2017-02-23 16:55:33 -05:00
|
|
|
this._onGClueLocationChanged();
|
|
|
|
} else {
|
|
|
|
if (this._gclueLocationChangedId)
|
|
|
|
this._gclueService.disconnect(this._gclueLocationChangedId);
|
|
|
|
this._gclueLocationChangedId = 0;
|
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2019-12-19 14:50:37 -05:00
|
|
|
async _startGClueService() {
|
2017-03-11 07:46:10 -05:00
|
|
|
if (this._gclueStarting)
|
2017-02-23 16:55:33 -05:00
|
|
|
return;
|
|
|
|
|
2017-03-11 07:46:10 -05:00
|
|
|
this._gclueStarting = true;
|
|
|
|
|
2019-12-19 14:50:37 -05:00
|
|
|
try {
|
|
|
|
this._gclueService = await Geoclue.Simple.new(
|
|
|
|
'org.gnome.Shell', Geoclue.AccuracyLevel.CITY, null);
|
|
|
|
} catch (e) {
|
|
|
|
log(`Failed to connect to Geoclue2 service: ${e.message}`);
|
|
|
|
this._setLocation(this._mostRecentLocation);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._gclueStarted = true;
|
|
|
|
this._gclueService.get_client().distance_threshold = 100;
|
|
|
|
this._updateLocationMonitoring();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onGClueLocationChanged() {
|
2017-02-23 16:55:33 -05:00
|
|
|
let geoLocation = this._gclueService.location;
|
|
|
|
let location = GWeather.Location.new_detached(geoLocation.description,
|
|
|
|
null,
|
|
|
|
geoLocation.latitude,
|
|
|
|
geoLocation.longitude);
|
|
|
|
this._setLocation(location);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2019-08-01 09:18:43 -04:00
|
|
|
_onAutomaticLocationChanged() {
|
|
|
|
let useAutoLocation = this._settings.get_boolean('automatic-location');
|
2017-03-19 11:34:53 -04:00
|
|
|
if (this._autoLocationRequested == useAutoLocation)
|
2017-02-23 16:55:33 -05:00
|
|
|
return;
|
|
|
|
|
2017-03-19 11:34:53 -04:00
|
|
|
this._autoLocationRequested = useAutoLocation;
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2017-03-19 09:31:19 -04:00
|
|
|
this._updateAutoLocation();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-03-19 09:31:19 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_updateAutoLocation() {
|
2017-02-23 16:55:33 -05:00
|
|
|
this._updateLocationMonitoring();
|
|
|
|
|
2017-03-11 07:46:10 -05:00
|
|
|
if (this._useAutoLocation)
|
|
|
|
this._startGClueService();
|
|
|
|
else
|
2017-02-23 16:55:33 -05:00
|
|
|
this._setLocation(this._mostRecentLocation);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-02-23 16:55:33 -05:00
|
|
|
|
2019-08-01 09:18:43 -04:00
|
|
|
_onLocationsChanged() {
|
|
|
|
let locations = this._settings.get_value('locations').deep_unpack();
|
|
|
|
let serialized = locations.shift();
|
2017-02-23 16:55:33 -05:00
|
|
|
let mostRecentLocation = null;
|
|
|
|
|
|
|
|
if (serialized)
|
|
|
|
mostRecentLocation = this._world.deserialize(serialized);
|
|
|
|
|
|
|
|
if (this._locationsEqual(this._mostRecentLocation, mostRecentLocation))
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._mostRecentLocation = mostRecentLocation;
|
|
|
|
|
2017-03-11 07:46:10 -05:00
|
|
|
if (!this._useAutoLocation || !this._gclueStarted)
|
2017-02-23 16:55:33 -05:00
|
|
|
this._setLocation(this._mostRecentLocation);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2017-03-19 11:29:35 -04:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onPermStoreChanged(proxy, sender, params) {
|
2019-01-31 09:08:00 -05:00
|
|
|
let [table, id, deleted_, data_, perms] = params;
|
2017-03-19 11:29:35 -04:00
|
|
|
|
|
|
|
if (table != 'gnome' || id != 'geolocation')
|
|
|
|
return;
|
|
|
|
|
2019-01-31 10:39:50 -05:00
|
|
|
let permission = perms['org.gnome.Weather'] || ['NONE'];
|
2017-03-19 11:29:35 -04:00
|
|
|
let [accuracy] = permission;
|
|
|
|
this._weatherAuthorized = accuracy != 'NONE';
|
|
|
|
|
|
|
|
this._updateAutoLocation();
|
2017-02-23 16:55:33 -05:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2017-02-23 16:55:33 -05:00
|
|
|
Signals.addSignalMethods(WeatherClient.prototype);
|