2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2013-11-26 08:06:41 -05:00
|
|
|
const Gio = imports.gi.Gio;
|
2012-07-30 13:09:42 -04:00
|
|
|
const GnomeBluetooth = imports.gi.GnomeBluetooth;
|
2010-07-24 07:57:53 -04:00
|
|
|
const Lang = imports.lang;
|
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2013-11-26 08:06:41 -05:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon.Rfkill';
|
|
|
|
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Rfkill';
|
|
|
|
|
|
|
|
const RfkillManagerInterface = '<node> \
|
|
|
|
<interface name="org.gnome.SettingsDaemon.Rfkill"> \
|
|
|
|
<property name="BluetoothAirplaneMode" type="b" access="readwrite" /> \
|
2014-03-17 11:15:21 -04:00
|
|
|
<property name="BluetoothHasAirplaneMode" type="b" access="read" /> \
|
2017-10-17 12:51:48 -04:00
|
|
|
<property name="BluetoothHardwareAirplaneMode" type="b" access="readwrite" /> \
|
2013-11-26 08:06:41 -05:00
|
|
|
</interface> \
|
|
|
|
</node>';
|
|
|
|
|
|
|
|
const RfkillManagerProxy = Gio.DBusProxy.makeProxyWrapper(RfkillManagerInterface);
|
|
|
|
|
2014-11-13 04:58:35 -05:00
|
|
|
const HAD_BLUETOOTH_DEVICES_SETUP = 'had-bluetooth-devices-setup';
|
|
|
|
|
2017-07-18 13:41:25 -04:00
|
|
|
var Indicator = new Lang.Class({
|
2011-11-20 09:38:48 -05:00
|
|
|
Name: 'BTIndicator',
|
2013-06-06 17:27:25 -04:00
|
|
|
Extends: PanelMenu.SystemIndicator,
|
2010-07-24 07:57:53 -04:00
|
|
|
|
|
|
|
_init: function() {
|
2013-06-06 17:27:25 -04:00
|
|
|
this.parent();
|
|
|
|
|
2013-07-19 06:05:47 -04:00
|
|
|
this._indicator = this._addIndicator();
|
|
|
|
this._indicator.icon_name = 'bluetooth-active-symbolic';
|
2014-11-13 04:58:35 -05:00
|
|
|
this._hadSetupDevices = global.settings.get_boolean(HAD_BLUETOOTH_DEVICES_SETUP);
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2013-11-26 08:06:41 -05:00
|
|
|
this._proxy = new RfkillManagerProxy(Gio.DBus.session, BUS_NAME, OBJECT_PATH,
|
|
|
|
Lang.bind(this, function(proxy, error) {
|
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-28 15:47:37 -05:00
|
|
|
|
|
|
|
this._sync();
|
2013-11-26 08:06:41 -05:00
|
|
|
}));
|
2014-01-28 15:47:37 -05:00
|
|
|
this._proxy.connect('g-properties-changed', Lang.bind(this, this._sync));
|
2013-11-26 08:06:41 -05:00
|
|
|
|
2014-11-13 04:58:35 -05:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem(_("Bluetooth"), true);
|
2013-07-15 13:51:42 -04:00
|
|
|
this._item.icon.icon_name = 'bluetooth-active-symbolic';
|
2014-11-13 04:58:35 -05:00
|
|
|
|
|
|
|
this._toggleItem = new PopupMenu.PopupMenuItem('');
|
|
|
|
this._toggleItem.connect('activate', Lang.bind(this, function() {
|
|
|
|
this._proxy.BluetoothAirplaneMode = !this._proxy.BluetoothAirplaneMode;
|
2010-07-24 07:57:53 -04:00
|
|
|
}));
|
2014-11-13 04:58:35 -05:00
|
|
|
this._item.menu.addMenuItem(this._toggleItem);
|
|
|
|
|
2013-07-15 13:51:42 -04:00
|
|
|
this._item.menu.addSettingsAction(_("Bluetooth Settings"), 'gnome-bluetooth-panel.desktop');
|
2013-09-21 12:57:41 -04:00
|
|
|
this.menu.addMenuItem(this._item);
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2013-11-26 08:46:13 -05:00
|
|
|
this._client = new GnomeBluetooth.Client();
|
|
|
|
this._model = this._client.get_model();
|
|
|
|
this._model.connect('row-changed', Lang.bind(this, this._sync));
|
|
|
|
this._model.connect('row-deleted', Lang.bind(this, this._sync));
|
|
|
|
this._model.connect('row-inserted', Lang.bind(this, this._sync));
|
2014-03-15 03:53:54 -04:00
|
|
|
Main.sessionMode.connect('updated', Lang.bind(this, this._sync));
|
2013-07-15 13:51:42 -04:00
|
|
|
this._sync();
|
2010-07-24 07:57:53 -04:00
|
|
|
},
|
|
|
|
|
2013-11-26 08:46:13 -05:00
|
|
|
_getDefaultAdapter: function() {
|
|
|
|
let [ret, iter] = this._model.get_iter_first();
|
|
|
|
while (ret) {
|
|
|
|
let isDefault = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.DEFAULT);
|
2014-11-13 04:58:35 -05:00
|
|
|
let isPowered = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.POWERED);
|
|
|
|
if (isDefault && isPowered)
|
2013-11-26 08:46:13 -05:00
|
|
|
return iter;
|
|
|
|
ret = this._model.iter_next(iter);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2014-11-13 04:58:35 -05:00
|
|
|
// nDevices is the number of devices setup for the current default
|
|
|
|
// adapter if one exists and is powered. If unpowered or unavailable,
|
|
|
|
// nDevice is "1" if it had setup devices associated to it the last
|
|
|
|
// time it was seen, and "-1" if not.
|
|
|
|
//
|
|
|
|
// nConnectedDevices is the number of devices connected to the default
|
|
|
|
// adapter if one exists and is powered, or -1 if it's not available.
|
|
|
|
_getNDevices: function() {
|
2013-11-26 08:46:13 -05:00
|
|
|
let adapter = this._getDefaultAdapter();
|
|
|
|
if (!adapter)
|
2014-11-13 04:58:35 -05:00
|
|
|
return [ this._hadSetupDevices ? 1 : -1, -1 ];
|
2013-11-26 08:46:13 -05:00
|
|
|
|
2014-11-13 04:58:35 -05:00
|
|
|
let nConnectedDevices = 0;
|
2013-11-26 08:46:13 -05:00
|
|
|
let nDevices = 0;
|
|
|
|
let [ret, iter] = this._model.iter_children(adapter);
|
|
|
|
while (ret) {
|
|
|
|
let isConnected = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.CONNECTED);
|
|
|
|
if (isConnected)
|
2014-11-13 04:58:35 -05:00
|
|
|
nConnectedDevices++;
|
|
|
|
|
|
|
|
let isPaired = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.PAIRED);
|
|
|
|
let isTrusted = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.TRUSTED);
|
|
|
|
if (isPaired || isTrusted)
|
2013-11-26 08:46:13 -05:00
|
|
|
nDevices++;
|
|
|
|
ret = this._model.iter_next(iter);
|
|
|
|
}
|
2014-11-13 04:58:35 -05:00
|
|
|
|
|
|
|
if (this._hadSetupDevices != (nDevices > 0)) {
|
|
|
|
this._hadSetupDevices = !this._hadSetupDevices;
|
|
|
|
global.settings.set_boolean(HAD_BLUETOOTH_DEVICES_SETUP, this._hadSetupDevices);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [ nDevices, nConnectedDevices];
|
2013-11-26 08:46:13 -05:00
|
|
|
},
|
|
|
|
|
2013-07-15 13:51:42 -04:00
|
|
|
_sync: function() {
|
2014-11-13 04:58:35 -05:00
|
|
|
let [ nDevices, nConnectedDevices ] = this._getNDevices();
|
2014-03-15 03:53:54 -04:00
|
|
|
let sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2014-03-15 03:53:54 -04:00
|
|
|
this.menu.setSensitive(sensitive);
|
2014-11-13 04:58:35 -05:00
|
|
|
this._indicator.visible = nConnectedDevices > 0;
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2014-11-13 04:58:35 -05:00
|
|
|
// Remember if there were setup devices and show the menu
|
|
|
|
// if we've seen setup devices and we're not hard blocked
|
2014-01-28 15:47:37 -05:00
|
|
|
if (nDevices > 0)
|
2014-11-13 04:58:35 -05:00
|
|
|
this._item.actor.visible = !this._proxy.BluetoothHardwareAirplaneMode;
|
|
|
|
else
|
|
|
|
this._item.actor.visible = this._proxy.BluetoothHasAirplaneMode && !this._proxy.BluetoothAirplaneMode;
|
|
|
|
|
|
|
|
if (nConnectedDevices > 0)
|
2015-08-06 05:19:37 -04:00
|
|
|
/* Translators: this is the number of connected bluetooth devices */
|
2014-11-13 04:58:35 -05:00
|
|
|
this._item.label.text = ngettext("%d Connected", "%d Connected", nConnectedDevices).format(nConnectedDevices);
|
|
|
|
else if (nConnectedDevices == -1)
|
|
|
|
this._item.label.text = _("Off");
|
2014-01-28 15:47:37 -05:00
|
|
|
else
|
2016-10-12 01:33:21 -04:00
|
|
|
this._item.label.text = _("On");
|
2014-11-13 04:58:35 -05:00
|
|
|
|
|
|
|
this._toggleItem.label.text = this._proxy.BluetoothAirplaneMode ? _("Turn On") : _("Turn Off");
|
2010-07-24 07:57:53 -04:00
|
|
|
},
|
2011-11-20 10:12:02 -05:00
|
|
|
});
|