2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 14:07:06 +00:00
|
|
|
/* exported Indicator */
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2020-04-03 18:27:34 +00:00
|
|
|
const { Gio, GLib, GnomeBluetooth, GObject } = imports.gi;
|
2010-07-24 11:57:53 +00:00
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
2013-11-26 13:06:41 +00:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon.Rfkill';
|
|
|
|
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Rfkill';
|
|
|
|
|
2018-09-06 00:55:20 +00:00
|
|
|
const RfkillManagerInterface = loadInterfaceXML('org.gnome.SettingsDaemon.Rfkill');
|
2013-11-26 13:06:41 +00:00
|
|
|
const RfkillManagerProxy = Gio.DBusProxy.makeProxyWrapper(RfkillManagerInterface);
|
|
|
|
|
2014-11-13 09:58:35 +00:00
|
|
|
const HAD_BLUETOOTH_DEVICES_SETUP = 'had-bluetooth-devices-setup';
|
|
|
|
|
2019-10-28 18:35:33 +00:00
|
|
|
var Indicator = GObject.registerClass(
|
|
|
|
class Indicator extends PanelMenu.SystemIndicator {
|
2019-07-16 09:24:13 +00:00
|
|
|
_init() {
|
|
|
|
super._init();
|
2013-06-06 21:27:25 +00:00
|
|
|
|
2013-07-19 10:05:47 +00:00
|
|
|
this._indicator = this._addIndicator();
|
|
|
|
this._indicator.icon_name = 'bluetooth-active-symbolic';
|
2014-11-13 09:58:35 +00:00
|
|
|
this._hadSetupDevices = global.settings.get_boolean(HAD_BLUETOOTH_DEVICES_SETUP);
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2013-11-26 13:06:41 +00:00
|
|
|
this._proxy = new RfkillManagerProxy(Gio.DBus.session, BUS_NAME, OBJECT_PATH,
|
2017-10-31 00:38:18 +00:00
|
|
|
(proxy, error) => {
|
2013-11-26 13:06:41 +00:00
|
|
|
if (error) {
|
|
|
|
log(error.message);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-28 20:47:37 +00:00
|
|
|
|
|
|
|
this._sync();
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2020-04-03 18:27:34 +00:00
|
|
|
this._proxy.connect('g-properties-changed', this._queueSync.bind(this));
|
2013-11-26 13:06:41 +00:00
|
|
|
|
2014-11-13 09:58:35 +00:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem(_("Bluetooth"), true);
|
2013-07-15 17:51:42 +00:00
|
|
|
this._item.icon.icon_name = 'bluetooth-active-symbolic';
|
2014-11-13 09:58:35 +00:00
|
|
|
|
|
|
|
this._toggleItem = new PopupMenu.PopupMenuItem('');
|
2017-10-31 00:38:18 +00:00
|
|
|
this._toggleItem.connect('activate', () => {
|
2014-11-13 09:58:35 +00:00
|
|
|
this._proxy.BluetoothAirplaneMode = !this._proxy.BluetoothAirplaneMode;
|
2017-10-31 00:38:18 +00:00
|
|
|
});
|
2014-11-13 09:58:35 +00:00
|
|
|
this._item.menu.addMenuItem(this._toggleItem);
|
|
|
|
|
2013-07-15 17:51:42 +00:00
|
|
|
this._item.menu.addSettingsAction(_("Bluetooth Settings"), 'gnome-bluetooth-panel.desktop');
|
2013-09-21 16:57:41 +00:00
|
|
|
this.menu.addMenuItem(this._item);
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2020-04-03 18:27:34 +00:00
|
|
|
this._syncId = 0;
|
2020-04-03 18:44:41 +00:00
|
|
|
this._adapter = null;
|
2020-04-03 18:27:34 +00:00
|
|
|
|
2013-11-26 13:46:13 +00:00
|
|
|
this._client = new GnomeBluetooth.Client();
|
|
|
|
this._model = this._client.get_model();
|
2020-04-03 18:27:34 +00:00
|
|
|
this._model.connect('row-deleted', this._queueSync.bind(this));
|
|
|
|
this._model.connect('row-changed', this._queueSync.bind(this));
|
2017-12-02 00:27:35 +00:00
|
|
|
this._model.connect('row-inserted', this._sync.bind(this));
|
|
|
|
Main.sessionMode.connect('updated', this._sync.bind(this));
|
2013-07-15 17:51:42 +00:00
|
|
|
this._sync();
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2020-04-03 18:44:41 +00:00
|
|
|
_setHadSetupDevices(value) {
|
|
|
|
if (this._hadSetupDevices === value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._hadSetupDevices = value;
|
|
|
|
global.settings.set_boolean(
|
|
|
|
HAD_BLUETOOTH_DEVICES_SETUP, this._hadSetupDevices);
|
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_getDefaultAdapter() {
|
2013-11-26 13:46:13 +00:00
|
|
|
let [ret, iter] = this._model.get_iter_first();
|
|
|
|
while (ret) {
|
|
|
|
let isDefault = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.DEFAULT);
|
2014-11-13 09:58:35 +00:00
|
|
|
let isPowered = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.POWERED);
|
|
|
|
if (isDefault && isPowered)
|
2013-11-26 13:46:13 +00:00
|
|
|
return iter;
|
|
|
|
ret = this._model.iter_next(iter);
|
|
|
|
}
|
|
|
|
return null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-11-26 13:46:13 +00:00
|
|
|
|
2020-03-30 08:48:06 +00:00
|
|
|
_getDeviceInfos(adapter) {
|
2013-11-26 13:46:13 +00:00
|
|
|
if (!adapter)
|
2020-03-30 08:48:06 +00:00
|
|
|
return [];
|
2013-11-26 13:46:13 +00:00
|
|
|
|
2020-03-30 08:48:06 +00:00
|
|
|
let deviceInfos = [];
|
2013-11-26 13:46:13 +00:00
|
|
|
let [ret, iter] = this._model.iter_children(adapter);
|
|
|
|
while (ret) {
|
2020-03-30 08:48:06 +00:00
|
|
|
const isPaired = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.PAIRED);
|
|
|
|
const isTrusted = this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.TRUSTED);
|
|
|
|
|
2020-04-02 10:42:41 +00:00
|
|
|
if (isPaired || isTrusted) {
|
|
|
|
deviceInfos.push({
|
|
|
|
connected: this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.CONNECTED),
|
|
|
|
name: this._model.get_value(iter,
|
|
|
|
GnomeBluetooth.Column.ALIAS),
|
|
|
|
});
|
|
|
|
}
|
2020-03-30 08:48:06 +00:00
|
|
|
|
2013-11-26 13:46:13 +00:00
|
|
|
ret = this._model.iter_next(iter);
|
|
|
|
}
|
2014-11-13 09:58:35 +00:00
|
|
|
|
2020-03-30 08:48:06 +00:00
|
|
|
return deviceInfos;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2013-11-26 13:46:13 +00:00
|
|
|
|
2020-04-03 18:27:34 +00:00
|
|
|
_queueSync() {
|
|
|
|
if (this._syncId)
|
|
|
|
return;
|
|
|
|
this._syncId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
|
|
this._syncId = 0;
|
|
|
|
this._sync();
|
|
|
|
return GLib.SOURCE_REMOVE;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_sync() {
|
2020-03-30 08:48:06 +00:00
|
|
|
let adapter = this._getDefaultAdapter();
|
|
|
|
let devices = this._getDeviceInfos(adapter);
|
|
|
|
const connectedDevices = devices.filter(dev => dev.connected);
|
|
|
|
const nConnectedDevices = connectedDevices.length;
|
|
|
|
|
2020-04-03 18:44:41 +00:00
|
|
|
if (adapter && this._adapter)
|
|
|
|
this._setHadSetupDevices(devices.length > 0);
|
|
|
|
this._adapter = adapter;
|
|
|
|
|
2014-03-15 07:53:54 +00:00
|
|
|
let sensitive = !Main.sessionMode.isLocked && !Main.sessionMode.isGreeter;
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2014-03-15 07:53:54 +00:00
|
|
|
this.menu.setSensitive(sensitive);
|
2014-11-13 09:58:35 +00:00
|
|
|
this._indicator.visible = nConnectedDevices > 0;
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2014-11-13 09:58:35 +00:00
|
|
|
// Remember if there were setup devices and show the menu
|
|
|
|
// if we've seen setup devices and we're not hard blocked
|
2020-04-03 12:34:21 +00:00
|
|
|
if (this._hadSetupDevices)
|
2019-04-12 21:00:49 +00:00
|
|
|
this._item.visible = !this._proxy.BluetoothHardwareAirplaneMode;
|
2014-11-13 09:58:35 +00:00
|
|
|
else
|
2019-04-12 21:00:49 +00:00
|
|
|
this._item.visible = this._proxy.BluetoothHasAirplaneMode && !this._proxy.BluetoothAirplaneMode;
|
2014-11-13 09:58:35 +00:00
|
|
|
|
2020-03-28 18:56:20 +00:00
|
|
|
if (nConnectedDevices > 1)
|
2015-08-06 09:19:37 +00:00
|
|
|
/* Translators: this is the number of connected bluetooth devices */
|
2020-08-21 19:30:03 +00:00
|
|
|
this._item.label.text = ngettext('%d Connected', '%d Connected', nConnectedDevices).format(nConnectedDevices);
|
2020-03-28 18:56:20 +00:00
|
|
|
else if (nConnectedDevices === 1)
|
|
|
|
this._item.label.text = connectedDevices[0].name;
|
2020-03-30 08:48:06 +00:00
|
|
|
else if (adapter === null)
|
2020-03-28 18:56:20 +00:00
|
|
|
this._item.label.text = _('Bluetooth Off');
|
2014-01-28 20:47:37 +00:00
|
|
|
else
|
2020-03-28 18:56:20 +00:00
|
|
|
this._item.label.text = _('Bluetooth On');
|
2014-11-13 09:58:35 +00:00
|
|
|
|
2020-03-28 18:56:20 +00:00
|
|
|
this._toggleItem.label.text = this._proxy.BluetoothAirplaneMode ? _('Turn On') : _('Turn Off');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|