2014-01-28 17:41:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
|
|
|
|
2014-02-13 17:50:32 +00:00
|
|
|
const GLib = imports.gi.GLib;
|
2014-01-28 17:41:26 +00:00
|
|
|
const Gio = imports.gi.Gio;
|
|
|
|
const Lang = imports.lang;
|
|
|
|
|
2014-03-15 08:01:50 +00:00
|
|
|
const Main = imports.ui.main;
|
2014-01-28 17:41:26 +00:00
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
2014-02-13 17:50:32 +00:00
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
2014-03-15 08:01:50 +00:00
|
|
|
const Shell = imports.gi.Shell;
|
2014-01-28 17:41:26 +00:00
|
|
|
|
2014-08-09 15:03:11 +00:00
|
|
|
const LOCATION_SCHEMA = 'org.gnome.system.location';
|
2014-02-16 15:06:55 +00:00
|
|
|
const MAX_ACCURACY_LEVEL = 'max-accuracy-level';
|
2014-08-06 16:04:00 +00:00
|
|
|
const ENABLED = 'enabled';
|
2014-02-16 15:06:55 +00:00
|
|
|
|
2014-09-11 13:57:15 +00:00
|
|
|
const GeoclueAccuracyLevel = {
|
|
|
|
NONE: 0,
|
|
|
|
COUNTRY: 1,
|
|
|
|
CITY: 4,
|
|
|
|
NEIGHBORHOOD: 5,
|
|
|
|
STREET: 6,
|
|
|
|
EXACT: 8
|
|
|
|
};
|
|
|
|
|
2014-01-28 17:41:26 +00:00
|
|
|
var GeoclueIface = '<node> \
|
|
|
|
<interface name="org.freedesktop.GeoClue2.Manager"> \
|
|
|
|
<property name="InUse" type="b" access="read"/> \
|
2014-02-13 17:50:32 +00:00
|
|
|
<property name="AvailableAccuracyLevel" type="u" access="read"/> \
|
|
|
|
<method name="AddAgent"> \
|
|
|
|
<arg name="id" type="s" direction="in"/> \
|
|
|
|
</method> \
|
2014-01-28 17:41:26 +00:00
|
|
|
</interface> \
|
|
|
|
</node>';
|
|
|
|
|
|
|
|
const GeoclueManager = Gio.DBusProxy.makeProxyWrapper(GeoclueIface);
|
|
|
|
|
2014-02-13 17:50:32 +00:00
|
|
|
var AgentIface = '<node> \
|
|
|
|
<interface name="org.freedesktop.GeoClue2.Agent"> \
|
2014-02-16 15:33:51 +00:00
|
|
|
<property name="MaxAccuracyLevel" type="u" access="read"/> \
|
2014-02-13 17:50:32 +00:00
|
|
|
<method name="AuthorizeApp"> \
|
|
|
|
<arg name="desktop_id" type="s" direction="in"/> \
|
|
|
|
<arg name="req_accuracy_level" type="u" direction="in"/> \
|
|
|
|
<arg name="authorized" type="b" direction="out"/> \
|
|
|
|
<arg name="allowed_accuracy_level" type="u" direction="out"/> \
|
|
|
|
</method> \
|
|
|
|
</interface> \
|
|
|
|
</node>';
|
|
|
|
|
2014-01-28 17:41:26 +00:00
|
|
|
const Indicator = new Lang.Class({
|
|
|
|
Name: 'LocationIndicator',
|
|
|
|
Extends: PanelMenu.SystemIndicator,
|
|
|
|
|
|
|
|
_init: function() {
|
|
|
|
this.parent();
|
|
|
|
|
2014-06-24 19:17:09 +00:00
|
|
|
this._settings = new Gio.Settings({ schema_id: LOCATION_SCHEMA });
|
2014-08-06 16:04:00 +00:00
|
|
|
this._settings.connect('changed::' + ENABLED,
|
|
|
|
Lang.bind(this, this._onMaxAccuracyLevelChanged));
|
2014-02-16 15:06:55 +00:00
|
|
|
this._settings.connect('changed::' + MAX_ACCURACY_LEVEL,
|
|
|
|
Lang.bind(this, this._onMaxAccuracyLevelChanged));
|
|
|
|
|
2014-01-28 17:41:26 +00:00
|
|
|
this._indicator = this._addIndicator();
|
|
|
|
this._indicator.icon_name = 'find-location-symbolic';
|
2014-02-13 17:50:32 +00:00
|
|
|
|
2015-08-06 09:19:37 +00:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem('', true);
|
2014-02-13 17:50:32 +00:00
|
|
|
this._item.icon.icon_name = 'find-location-symbolic';
|
|
|
|
|
|
|
|
this._agent = Gio.DBusExportedObject.wrapJSObject(AgentIface, this);
|
2014-02-24 18:26:46 +00:00
|
|
|
this._agent.export(Gio.DBus.system, '/org/freedesktop/GeoClue2/Agent');
|
2014-02-13 17:50:32 +00:00
|
|
|
|
2015-08-06 09:19:37 +00:00
|
|
|
this._item.label.text = _("Location Enabled");
|
2014-03-20 13:17:47 +00:00
|
|
|
this._onOffAction = this._item.menu.addAction(_("Disable"), Lang.bind(this, this._onOnOffAction));
|
2014-09-12 09:33:39 +00:00
|
|
|
this._item.menu.addSettingsAction(_("Privacy Settings"), 'gnome-privacy-panel.desktop');
|
2014-02-13 17:50:32 +00:00
|
|
|
|
|
|
|
this.menu.addMenuItem(this._item);
|
2014-01-28 17:41:26 +00:00
|
|
|
|
|
|
|
this._watchId = Gio.bus_watch_name(Gio.BusType.SYSTEM,
|
|
|
|
'org.freedesktop.GeoClue2',
|
|
|
|
0,
|
2014-02-15 13:58:06 +00:00
|
|
|
Lang.bind(this, this._connectToGeoclue),
|
2014-01-28 17:41:26 +00:00
|
|
|
Lang.bind(this, this._onGeoclueVanished));
|
2014-03-15 08:01:50 +00:00
|
|
|
Main.sessionMode.connect('updated', Lang.bind(this, this._onSessionUpdated));
|
|
|
|
this._onSessionUpdated();
|
2014-02-16 15:06:55 +00:00
|
|
|
this._onMaxAccuracyLevelChanged();
|
2014-02-13 17:50:32 +00:00
|
|
|
this._connectToGeoclue();
|
2014-01-28 17:41:26 +00:00
|
|
|
},
|
|
|
|
|
2014-02-13 17:50:32 +00:00
|
|
|
get MaxAccuracyLevel() {
|
2014-02-16 15:06:55 +00:00
|
|
|
return this._getMaxAccuracyLevel();
|
2014-02-13 17:50:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// We (and geoclue) have currently no way to reliably identifying apps so
|
|
|
|
// for now, lets just authorize all apps as long as they provide a valid
|
|
|
|
// desktop ID. We also ensure they don't get more accuracy than global max.
|
|
|
|
AuthorizeApp: function(desktop_id, reqAccuracyLevel) {
|
|
|
|
var appSystem = Shell.AppSystem.get_default();
|
|
|
|
var app = appSystem.lookup_app(desktop_id + ".desktop");
|
|
|
|
if (app == null) {
|
|
|
|
return [false, 0];
|
|
|
|
}
|
|
|
|
|
2014-02-16 15:06:55 +00:00
|
|
|
let allowedAccuracyLevel = clamp(reqAccuracyLevel, 0, this._getMaxAccuracyLevel());
|
2014-02-13 17:50:32 +00:00
|
|
|
return [true, allowedAccuracyLevel];
|
|
|
|
},
|
|
|
|
|
|
|
|
_syncIndicator: function() {
|
2014-01-28 17:41:26 +00:00
|
|
|
if (this._proxy == null) {
|
|
|
|
this._indicator.visible = false;
|
2014-08-09 15:38:20 +00:00
|
|
|
this._item.actor.visible = false;
|
2014-01-28 17:41:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._indicator.visible = this._proxy.InUse;
|
2014-08-09 15:38:20 +00:00
|
|
|
this._item.actor.visible = this._indicator.visible;
|
2014-03-20 13:17:47 +00:00
|
|
|
this._updateMenuLabels();
|
2014-01-28 17:41:26 +00:00
|
|
|
},
|
|
|
|
|
2014-02-15 13:58:06 +00:00
|
|
|
_connectToGeoclue: function() {
|
2014-02-15 15:17:58 +00:00
|
|
|
if (this._proxy != null || this._connecting)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
this._connecting = true;
|
2014-02-15 13:42:43 +00:00
|
|
|
new GeoclueManager(Gio.DBus.system,
|
|
|
|
'org.freedesktop.GeoClue2',
|
|
|
|
'/org/freedesktop/GeoClue2/Manager',
|
|
|
|
Lang.bind(this, this._onProxyReady));
|
2014-02-15 15:17:58 +00:00
|
|
|
return true;
|
2014-02-15 13:42:43 +00:00
|
|
|
},
|
|
|
|
|
2014-02-16 13:55:17 +00:00
|
|
|
_onProxyReady: function(proxy, error) {
|
2014-02-15 13:42:43 +00:00
|
|
|
if (error != null) {
|
2014-02-16 13:55:17 +00:00
|
|
|
log(error.message);
|
2014-02-13 17:50:32 +00:00
|
|
|
this._connecting = false;
|
2014-02-15 13:42:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._proxy = proxy;
|
2014-02-17 01:17:46 +00:00
|
|
|
this._propertiesChangedId = this._proxy.connect('g-properties-changed',
|
|
|
|
Lang.bind(this, this._onGeocluePropsChanged));
|
2014-01-28 17:41:26 +00:00
|
|
|
|
2014-02-13 17:50:32 +00:00
|
|
|
this._syncIndicator();
|
|
|
|
|
|
|
|
this._proxy.AddAgentRemote('gnome-shell', Lang.bind(this, this._onAgentRegistered));
|
|
|
|
},
|
|
|
|
|
|
|
|
_onAgentRegistered: function(result, error) {
|
|
|
|
this._connecting = false;
|
|
|
|
this._notifyMaxAccuracyLevel();
|
|
|
|
|
|
|
|
if (error != null)
|
2014-02-16 13:55:17 +00:00
|
|
|
log(error.message);
|
2014-01-28 17:41:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onGeoclueVanished: function() {
|
2014-02-17 01:17:46 +00:00
|
|
|
if (this._propertiesChangedId) {
|
|
|
|
this._proxy.disconnect(this._propertiesChangedId);
|
|
|
|
this._propertiesChangedId = 0;
|
|
|
|
}
|
2014-01-28 17:41:26 +00:00
|
|
|
this._proxy = null;
|
|
|
|
|
2014-02-13 17:50:32 +00:00
|
|
|
this._syncIndicator();
|
|
|
|
},
|
|
|
|
|
|
|
|
_onOnOffAction: function() {
|
2014-08-06 16:04:00 +00:00
|
|
|
let enabled = this._settings.get_boolean(ENABLED);
|
|
|
|
this._settings.set_boolean(ENABLED, !enabled);
|
2014-02-13 17:50:32 +00:00
|
|
|
},
|
|
|
|
|
2014-03-15 08:01:50 +00:00
|
|
|
_onSessionUpdated: function() {
|
|
|
|
let sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
|
|
|
|
this.menu.setSensitive(sensitive);
|
|
|
|
},
|
|
|
|
|
2014-03-20 13:17:47 +00:00
|
|
|
_updateMenuLabels: function() {
|
2014-08-06 16:04:00 +00:00
|
|
|
if (this._settings.get_boolean(ENABLED)) {
|
2015-08-06 09:19:37 +00:00
|
|
|
this._item.label.text = this._indicator.visible ? _("Location In Use")
|
|
|
|
: _("Location Enabled");
|
2014-03-20 13:17:47 +00:00
|
|
|
this._onOffAction.label.text = _("Disable");
|
2014-08-06 16:04:00 +00:00
|
|
|
} else {
|
2015-08-06 09:19:37 +00:00
|
|
|
this._item.label.text = _("Location Disabled");
|
2014-08-06 16:04:00 +00:00
|
|
|
this._onOffAction.label.text = _("Enable");
|
2014-02-13 17:50:32 +00:00
|
|
|
}
|
2014-03-20 13:17:47 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
_onMaxAccuracyLevelChanged: function() {
|
|
|
|
this._updateMenuLabels();
|
2014-02-13 17:50:32 +00:00
|
|
|
|
|
|
|
// Gotta ensure geoclue is up and we are registered as agent to it
|
|
|
|
// before we emit the notify for this property change.
|
|
|
|
if (!this._connectToGeoclue())
|
|
|
|
this._notifyMaxAccuracyLevel();
|
|
|
|
},
|
|
|
|
|
2014-02-16 15:06:55 +00:00
|
|
|
_getMaxAccuracyLevel: function() {
|
2014-09-11 13:57:15 +00:00
|
|
|
if (this._settings.get_boolean(ENABLED)) {
|
|
|
|
let level = this._settings.get_string(MAX_ACCURACY_LEVEL);
|
|
|
|
|
|
|
|
return GeoclueAccuracyLevel[level.toUpperCase()] ||
|
|
|
|
GeoclueAccuracyLevel.NONE;
|
|
|
|
} else {
|
|
|
|
return GeoclueAccuracyLevel.NONE;
|
|
|
|
}
|
2014-02-16 15:06:55 +00:00
|
|
|
},
|
2014-02-13 17:50:32 +00:00
|
|
|
|
2014-02-16 15:06:55 +00:00
|
|
|
_notifyMaxAccuracyLevel: function() {
|
|
|
|
let variant = new GLib.Variant('u', this._getMaxAccuracyLevel());
|
2014-02-16 13:55:17 +00:00
|
|
|
this._agent.emit_property_changed('MaxAccuracyLevel', variant);
|
2014-02-15 15:46:07 +00:00
|
|
|
},
|
|
|
|
|
2014-02-15 16:28:58 +00:00
|
|
|
_onGeocluePropsChanged: function(proxy, properties) {
|
|
|
|
let unpacked = properties.deep_unpack();
|
|
|
|
if ("InUse" in unpacked)
|
|
|
|
this._syncIndicator();
|
2014-01-28 17:41:26 +00:00
|
|
|
}
|
|
|
|
});
|
2014-02-13 17:50:32 +00:00
|
|
|
|
|
|
|
function clamp(value, min, max) {
|
|
|
|
return Math.max(min, Math.min(max, value));
|
|
|
|
}
|