2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2019-01-31 09:07:06 -05:00
|
|
|
/* exported Indicator */
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2020-04-03 14:27:34 -04:00
|
|
|
const { Gio, GLib, GnomeBluetooth, GObject } = imports.gi;
|
2010-07-24 07:57:53 -04:00
|
|
|
|
|
|
|
const Main = imports.ui.main;
|
|
|
|
const PanelMenu = imports.ui.panelMenu;
|
|
|
|
const PopupMenu = imports.ui.popupMenu;
|
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const { loadInterfaceXML } = imports.misc.fileUtils;
|
|
|
|
|
2013-11-26 08:06:41 -05:00
|
|
|
const BUS_NAME = 'org.gnome.SettingsDaemon.Rfkill';
|
|
|
|
const OBJECT_PATH = '/org/gnome/SettingsDaemon/Rfkill';
|
|
|
|
|
2018-09-05 20:55:20 -04:00
|
|
|
const RfkillManagerInterface = loadInterfaceXML('org.gnome.SettingsDaemon.Rfkill');
|
2022-07-29 06:57:50 -04:00
|
|
|
const rfkillManagerInfo = Gio.DBusInterfaceInfo.new_for_xml(RfkillManagerInterface);
|
2013-11-26 08:06:41 -05:00
|
|
|
|
2014-11-13 04:58:35 -05:00
|
|
|
const HAD_BLUETOOTH_DEVICES_SETUP = 'had-bluetooth-devices-setup';
|
|
|
|
|
2019-10-28 14:35:33 -04:00
|
|
|
var Indicator = GObject.registerClass(
|
|
|
|
class Indicator extends PanelMenu.SystemIndicator {
|
2019-07-16 05:24:13 -04:00
|
|
|
_init() {
|
|
|
|
super._init();
|
2013-06-06 17:27:25 -04:00
|
|
|
|
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);
|
2022-01-18 05:14:47 -05:00
|
|
|
|
2022-01-18 05:14:47 -05:00
|
|
|
this._client = new GnomeBluetooth.Client();
|
2022-02-24 13:35:06 -05:00
|
|
|
this._client.connect('notify::default-adapter', () => {
|
|
|
|
const newAdapter = this._client.default_adapter ?? null;
|
|
|
|
|
|
|
|
if (newAdapter && this._adapter)
|
|
|
|
this._setHadSetupDevices(this._getDeviceInfos().length > 0);
|
|
|
|
|
|
|
|
this._adapter = newAdapter;
|
|
|
|
|
|
|
|
this._deviceNotifyConnected.clear();
|
|
|
|
this._sync();
|
|
|
|
});
|
2022-01-18 05:14:47 -05:00
|
|
|
this._client.connect('notify::default-adapter-powered', this._sync.bind(this));
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2022-07-29 06:57:50 -04:00
|
|
|
this._proxy = new Gio.DBusProxy({
|
|
|
|
g_connection: Gio.DBus.session,
|
|
|
|
g_name: BUS_NAME,
|
|
|
|
g_object_path: OBJECT_PATH,
|
|
|
|
g_interface_name: rfkillManagerInfo.name,
|
|
|
|
g_interface_info: rfkillManagerInfo,
|
|
|
|
});
|
|
|
|
this._proxy.connect('g-properties-changed', (p, properties) => {
|
|
|
|
if ('BluetoothHardwareAirplaneMode' in properties.unpack())
|
|
|
|
this._sync();
|
|
|
|
});
|
|
|
|
this._proxy.init_async(GLib.PRIORITY_DEFAULT, null)
|
|
|
|
.catch(e => console.error(e.message));
|
2013-11-26 08:06:41 -05:00
|
|
|
|
2014-11-13 04:58:35 -05:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem(_("Bluetooth"), true);
|
|
|
|
|
|
|
|
this._toggleItem = new PopupMenu.PopupMenuItem('');
|
2017-10-30 20:38:18 -04:00
|
|
|
this._toggleItem.connect('activate', () => {
|
2022-01-18 05:14:47 -05:00
|
|
|
if (!this._client.default_adapter_powered) {
|
|
|
|
this._proxy.BluetoothAirplaneMode = false;
|
2022-01-18 05:14:47 -05:00
|
|
|
this._client.default_adapter_powered = true;
|
2022-01-18 05:14:47 -05:00
|
|
|
} else {
|
|
|
|
this._proxy.BluetoothAirplaneMode = true;
|
|
|
|
}
|
2017-10-30 20:38:18 -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
|
|
|
|
2020-04-03 14:27:34 -04:00
|
|
|
this._syncId = 0;
|
2020-04-03 14:44:41 -04:00
|
|
|
this._adapter = null;
|
2020-04-03 14:27:34 -04:00
|
|
|
|
2021-12-02 05:44:47 -05:00
|
|
|
this._deviceNotifyConnected = new Set();
|
|
|
|
|
|
|
|
const deviceStore = this._client.get_devices();
|
|
|
|
for (let i = 0; i < deviceStore.get_n_items(); i++)
|
|
|
|
this._connectDeviceNotify(deviceStore.get_item(i));
|
|
|
|
|
2021-12-02 05:44:47 -05:00
|
|
|
this._client.connect('device-removed', (c, path) => {
|
2021-12-02 05:44:47 -05:00
|
|
|
this._deviceNotifyConnected.delete(path);
|
2021-12-02 05:44:47 -05:00
|
|
|
this._queueSync.bind(this);
|
|
|
|
});
|
|
|
|
this._client.connect('device-added', (c, device) => {
|
|
|
|
this._connectDeviceNotify(device);
|
|
|
|
this._sync();
|
|
|
|
});
|
2017-12-01 19:27:35 -05:00
|
|
|
Main.sessionMode.connect('updated', this._sync.bind(this));
|
2013-07-15 13:51:42 -04:00
|
|
|
this._sync();
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2010-07-24 07:57:53 -04:00
|
|
|
|
2020-04-03 14:44:41 -04:00
|
|
|
_setHadSetupDevices(value) {
|
|
|
|
if (this._hadSetupDevices === value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this._hadSetupDevices = value;
|
|
|
|
global.settings.set_boolean(
|
|
|
|
HAD_BLUETOOTH_DEVICES_SETUP, this._hadSetupDevices);
|
|
|
|
}
|
|
|
|
|
2021-12-02 05:44:47 -05:00
|
|
|
_connectDeviceNotify(device) {
|
2021-12-02 05:44:47 -05:00
|
|
|
const path = device.get_object_path();
|
|
|
|
|
|
|
|
if (this._deviceNotifyConnected.has(path))
|
2021-12-02 05:44:47 -05:00
|
|
|
return;
|
2021-12-02 05:44:47 -05:00
|
|
|
|
2021-12-02 05:44:47 -05:00
|
|
|
device.connect('notify::alias', this._queueSync.bind(this));
|
|
|
|
device.connect('notify::paired', this._queueSync.bind(this));
|
|
|
|
device.connect('notify::trusted', this._queueSync.bind(this));
|
|
|
|
device.connect('notify::connected', this._queueSync.bind(this));
|
2021-12-02 05:44:47 -05:00
|
|
|
|
|
|
|
this._deviceNotifyConnected.add(path);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-11-26 08:46:13 -05:00
|
|
|
|
2021-12-02 05:44:47 -05:00
|
|
|
_getDeviceInfos() {
|
2021-12-02 05:44:47 -05:00
|
|
|
const deviceStore = this._client.get_devices();
|
2020-03-30 04:48:06 -04:00
|
|
|
let deviceInfos = [];
|
2021-12-02 05:44:47 -05:00
|
|
|
|
2021-12-02 05:44:47 -05:00
|
|
|
for (let i = 0; i < deviceStore.get_n_items(); i++) {
|
|
|
|
const device = deviceStore.get_item(i);
|
2021-12-02 05:44:47 -05:00
|
|
|
|
|
|
|
if (device.paired || device.trusted) {
|
2020-04-02 06:42:41 -04:00
|
|
|
deviceInfos.push({
|
2021-12-02 05:44:47 -05:00
|
|
|
connected: device.connected,
|
|
|
|
name: device.alias,
|
2020-04-02 06:42:41 -04:00
|
|
|
});
|
|
|
|
}
|
2013-11-26 08:46:13 -05:00
|
|
|
}
|
2014-11-13 04:58:35 -05:00
|
|
|
|
2020-03-30 04:48:06 -04:00
|
|
|
return deviceInfos;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2013-11-26 08:46:13 -05:00
|
|
|
|
2020-04-03 14:27:34 -04: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-30 20:03:21 -04:00
|
|
|
_sync() {
|
2022-01-31 17:40:39 -05:00
|
|
|
const devices = this._getDeviceInfos();
|
2020-03-30 04:48:06 -04:00
|
|
|
const connectedDevices = devices.filter(dev => dev.connected);
|
|
|
|
const nConnectedDevices = connectedDevices.length;
|
|
|
|
|
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
|
|
|
|
2022-01-18 05:14:47 -05:00
|
|
|
const adapterPowered = this._client.default_adapter_powered;
|
|
|
|
|
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
|
2020-04-03 08:34:21 -04:00
|
|
|
if (this._hadSetupDevices)
|
2019-04-12 17:00:49 -04:00
|
|
|
this._item.visible = !this._proxy.BluetoothHardwareAirplaneMode;
|
2014-11-13 04:58:35 -05:00
|
|
|
else
|
2022-01-18 05:14:47 -05:00
|
|
|
this._item.visible = adapterPowered;
|
2014-11-13 04:58:35 -05:00
|
|
|
|
2022-02-18 15:02:20 -05:00
|
|
|
this._item.icon.icon_name = adapterPowered
|
|
|
|
? 'bluetooth-active-symbolic' : 'bluetooth-disabled-symbolic';
|
|
|
|
|
2020-03-28 14:56:20 -04:00
|
|
|
if (nConnectedDevices > 1)
|
2015-08-06 05:19:37 -04:00
|
|
|
/* Translators: this is the number of connected bluetooth devices */
|
2020-08-21 15:30:03 -04:00
|
|
|
this._item.label.text = ngettext('%d Connected', '%d Connected', nConnectedDevices).format(nConnectedDevices);
|
2020-03-28 14:56:20 -04:00
|
|
|
else if (nConnectedDevices === 1)
|
|
|
|
this._item.label.text = connectedDevices[0].name;
|
2022-01-18 05:14:47 -05:00
|
|
|
else if (adapterPowered)
|
2020-03-28 14:56:20 -04:00
|
|
|
this._item.label.text = _('Bluetooth On');
|
2022-01-18 05:14:47 -05:00
|
|
|
else
|
|
|
|
this._item.label.text = _('Bluetooth Off');
|
2014-11-13 04:58:35 -05:00
|
|
|
|
2022-01-18 05:14:47 -05:00
|
|
|
this._toggleItem.label.text = adapterPowered ? _('Turn Off') : _('Turn On');
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2019-07-16 05:24:13 -04:00
|
|
|
});
|