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');
|
2022-07-29 10:57:50 +00:00
|
|
|
const rfkillManagerInfo = Gio.DBusInterfaceInfo.new_for_xml(RfkillManagerInterface);
|
2013-11-26 13:06:41 +00:00
|
|
|
|
2014-11-13 09:58:35 +00:00
|
|
|
const HAD_BLUETOOTH_DEVICES_SETUP = 'had-bluetooth-devices-setup';
|
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
const BtClient = GObject.registerClass({
|
|
|
|
Properties: {
|
|
|
|
'available': GObject.ParamSpec.boolean('available', '', '',
|
|
|
|
GObject.ParamFlags.READABLE,
|
|
|
|
false),
|
|
|
|
'active': GObject.ParamSpec.boolean('active', '', '',
|
|
|
|
GObject.ParamFlags.READABLE,
|
|
|
|
false),
|
|
|
|
},
|
|
|
|
Signals: {
|
|
|
|
'devices-changed': {},
|
|
|
|
},
|
|
|
|
}, class BtClient extends GObject.Object {
|
2019-07-16 09:24:13 +00:00
|
|
|
_init() {
|
|
|
|
super._init();
|
2013-06-06 21:27:25 +00:00
|
|
|
|
2014-11-13 09:58:35 +00:00
|
|
|
this._hadSetupDevices = global.settings.get_boolean(HAD_BLUETOOTH_DEVICES_SETUP);
|
2022-01-18 10:14:47 +00:00
|
|
|
|
2022-01-18 10:14:47 +00:00
|
|
|
this._client = new GnomeBluetooth.Client();
|
2022-07-28 13:00:45 +00:00
|
|
|
this._client.connect('notify::default-adapter-powered', () => {
|
|
|
|
this.notify('active');
|
|
|
|
this.notify('available');
|
|
|
|
});
|
2022-02-24 18:35:06 +00:00
|
|
|
this._client.connect('notify::default-adapter', () => {
|
|
|
|
const newAdapter = this._client.default_adapter ?? null;
|
|
|
|
|
|
|
|
if (newAdapter && this._adapter)
|
2022-07-28 13:00:45 +00:00
|
|
|
this._setHadSetupDevices([...this._getDevices()].length > 0);
|
2022-02-24 18:35:06 +00:00
|
|
|
|
|
|
|
this._adapter = newAdapter;
|
|
|
|
this._deviceNotifyConnected.clear();
|
2022-07-28 13:00:45 +00:00
|
|
|
this.emit('devices-changed');
|
|
|
|
|
|
|
|
this.notify('active');
|
|
|
|
this.notify('available');
|
2022-02-24 18:35:06 +00:00
|
|
|
});
|
2010-07-24 11:57:53 +00:00
|
|
|
|
2022-07-29 10:57:50 +00: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())
|
2022-07-28 13:00:45 +00:00
|
|
|
this.notify('available');
|
2022-07-29 10:57:50 +00:00
|
|
|
});
|
|
|
|
this._proxy.init_async(GLib.PRIORITY_DEFAULT, null)
|
|
|
|
.catch(e => console.error(e.message));
|
2013-11-26 13:06:41 +00:00
|
|
|
|
2020-04-03 18:44:41 +00:00
|
|
|
this._adapter = null;
|
2020-04-03 18:27:34 +00:00
|
|
|
|
2021-12-02 10:44:47 +00: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 10:44:47 +00:00
|
|
|
this._client.connect('device-removed', (c, path) => {
|
2021-12-02 10:44:47 +00:00
|
|
|
this._deviceNotifyConnected.delete(path);
|
2022-07-28 13:00:45 +00:00
|
|
|
this.emit('devices-changed');
|
2021-12-02 10:44:47 +00:00
|
|
|
});
|
|
|
|
this._client.connect('device-added', (c, device) => {
|
|
|
|
this._connectDeviceNotify(device);
|
2022-07-28 13:00:45 +00:00
|
|
|
this.emit('devices-changed');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
get available() {
|
|
|
|
// If there were set up devices, assume there is an adapter
|
|
|
|
// that can be powered on as long as we're not hard blocked
|
|
|
|
return this._hadSetupDevices
|
|
|
|
? !this._proxy.BluetoothHardwareAirplaneMode
|
|
|
|
: this.active;
|
|
|
|
}
|
|
|
|
|
|
|
|
get active() {
|
|
|
|
return this._client.default_adapter_powered;
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleActive() {
|
|
|
|
this._proxy.BluetoothAirplaneMode = this.active;
|
|
|
|
if (!this._client.default_adapter_powered)
|
|
|
|
this._client.default_adapter_powered = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*getDevices() {
|
|
|
|
const deviceStore = this._client.get_devices();
|
|
|
|
|
|
|
|
for (let i = 0; i < deviceStore.get_n_items(); i++) {
|
|
|
|
const device = deviceStore.get_item(i);
|
|
|
|
|
|
|
|
if (device.paired || device.trusted)
|
|
|
|
yield device;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_queueDevicesChanged() {
|
|
|
|
if (this._devicesChangedId)
|
|
|
|
return;
|
|
|
|
this._devicesChangedId = GLib.idle_add(GLib.PRIORITY_DEFAULT, () => {
|
|
|
|
delete this._devicesChangedId;
|
|
|
|
this.emit('devices-changed');
|
|
|
|
return GLib.SOURCE_REMOVE;
|
2021-12-02 10:44:47 +00:00
|
|
|
});
|
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);
|
|
|
|
}
|
|
|
|
|
2021-12-02 10:44:47 +00:00
|
|
|
_connectDeviceNotify(device) {
|
2021-12-02 10:44:47 +00:00
|
|
|
const path = device.get_object_path();
|
|
|
|
|
|
|
|
if (this._deviceNotifyConnected.has(path))
|
2021-12-02 10:44:47 +00:00
|
|
|
return;
|
2021-12-02 10:44:47 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
device.connect('notify::alias', () => this._queueDevicesChanged());
|
|
|
|
device.connect('notify::paired', () => this._queueDevicesChanged());
|
|
|
|
device.connect('notify::trusted', () => this._queueDevicesChanged());
|
|
|
|
device.connect('notify::connected', () => this._queueDevicesChanged());
|
2021-12-02 10:44:47 +00:00
|
|
|
|
|
|
|
this._deviceNotifyConnected.add(path);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2022-07-28 13:00:45 +00:00
|
|
|
});
|
2013-11-26 13:46:13 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
var Indicator = GObject.registerClass(
|
|
|
|
class Indicator extends PanelMenu.SystemIndicator {
|
|
|
|
_init() {
|
|
|
|
super._init();
|
2021-12-02 10:44:47 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
this._indicator = this._addIndicator();
|
|
|
|
this._indicator.icon_name = 'bluetooth-active-symbolic';
|
2021-12-02 10:44:47 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
this._client = new BtClient();
|
|
|
|
this._client.connectObject(
|
|
|
|
'notify::active', () => this._sync(),
|
|
|
|
'devices-changed', () => this._sync(), this);
|
2014-11-13 09:58:35 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
this._item = new PopupMenu.PopupSubMenuMenuItem(_('Bluetooth'), true);
|
|
|
|
this._client.bind_property('available',
|
|
|
|
this._item, 'visible',
|
|
|
|
GObject.BindingFlags.SYNC_CREATE);
|
2013-11-26 13:46:13 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
this._toggleItem = new PopupMenu.PopupMenuItem('');
|
|
|
|
this._toggleItem.connect('activate', () => this._client.toggleActive());
|
|
|
|
this._item.menu.addMenuItem(this._toggleItem);
|
|
|
|
|
|
|
|
this._item.menu.addSettingsAction(_('Bluetooth Settings'), 'gnome-bluetooth-panel.desktop');
|
|
|
|
this.menu.addMenuItem(this._item);
|
|
|
|
|
|
|
|
Main.sessionMode.connect('updated', this._sync.bind(this));
|
|
|
|
this._sync();
|
2020-04-03 18:27:34 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_sync() {
|
2022-07-28 13:00:45 +00:00
|
|
|
const devices = [...this._client.getDevices()];
|
2020-03-30 08:48:06 +00:00
|
|
|
const connectedDevices = devices.filter(dev => dev.connected);
|
|
|
|
const nConnectedDevices = connectedDevices.length;
|
|
|
|
|
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
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
this._item.icon.icon_name = this._client.active
|
2022-02-18 20:02:20 +00:00
|
|
|
? 'bluetooth-active-symbolic' : 'bluetooth-disabled-symbolic';
|
|
|
|
|
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)
|
2022-07-28 13:00:45 +00:00
|
|
|
this._item.label.text = connectedDevices[0].alias;
|
|
|
|
else if (this._client.active)
|
2020-03-28 18:56:20 +00:00
|
|
|
this._item.label.text = _('Bluetooth On');
|
2022-01-18 10:14:47 +00:00
|
|
|
else
|
|
|
|
this._item.label.text = _('Bluetooth Off');
|
2014-11-13 09:58:35 +00:00
|
|
|
|
2022-07-28 13:00:45 +00:00
|
|
|
this._toggleItem.label.text = this._client.active ? _('Turn Off') : _('Turn On');
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2019-07-16 09:24:13 +00:00
|
|
|
});
|