Skip to content
Commits on Source (10)
/.idea
/schemas/gschemas.compiled
/.gnome-shell
/gnome-bluetooth-quick-connect.zip
# Bluetooth quick connect
# Bluetooth Quick Connect
This extension allows paired Bluetooth devices to be connected and
disconnected via the GNOME system menu, without need to enter the
......
......@@ -24,40 +24,46 @@ const Utils = Me.imports.utils;
var BluetoothController = class {
constructor() {
this._client = new GnomeBluetooth.Client();
this._model = this._client.get_model();
this._deviceNotifyConnected = new Set();
this._store = this._client.get_devices();
}
enable() {
this._connectSignal(this._model, 'row-changed', (arg0, arg1, iter) => {
if (iter) {
let device = this._buildDevice(iter);
this.emit('device-changed', device);
}
this._client.connect('notify::default-adapter', () => {
this._deviceNotifyConnected.clear();
this.emit('default-adapter-changed');
});
this._client.connect('notify::default-adapter-powered', () => {
this._deviceNotifyConnected.clear();
this.emit('default-adapter-changed');
});
this._connectSignal(this._model, 'row-deleted', () => {
this._client.connect('device-removed', (c, path) => {
this._deviceNotifyConnected.delete(path);
this.emit('device-deleted');
});
this._connectSignal(this._model, 'row-inserted', (arg0, arg1, iter) => {
if (iter) {
let device = this._buildDevice(iter);
this.emit('device-inserted', device);
}
this._client.connect('device-added', (c, device) => {
this._connectDeviceNotify(device);
this.emit('device-inserted', new BluetoothDevice(device));
});
}
getDevices() {
let adapter = this._getDefaultAdapter();
if (!adapter)
return [];
_connectDeviceNotify(device) {
const path = device.get_object_path();
if (this._deviceNotifyConnected.has(path))
return;
device.connect('notify', (device) => {
this.emit('device-changed', new BluetoothDevice(device));
});
}
getDevices() {
let devices = [];
let [ret, iter] = this._model.iter_children(adapter);
while (ret) {
let device = this._buildDevice(iter);
for (let i = 0; i < this._store.get_n_items(); i++) {
let device = new BluetoothDevice(this._store.get_item(i));
devices.push(device);
ret = this._model.iter_next(iter);
}
return devices;
......@@ -72,39 +78,21 @@ var BluetoothController = class {
destroy() {
this._disconnectSignals();
}
_getDefaultAdapter() {
let [ret, iter] = this._model.get_iter_first();
while (ret) {
let isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
let isPowered = this._model.get_value(iter, GnomeBluetooth.Column.POWERED);
if (isDefault && isPowered)
return iter;
ret = this._model.iter_next(iter);
}
return null;
}
_buildDevice(iter) {
return new BluetoothDevice(this._model, iter);
}
}
Signals.addSignalMethods(BluetoothController.prototype);
Utils.addSignalsHelperMethods(BluetoothController.prototype);
var BluetoothDevice = class {
constructor(model, iter) {
this._model = model;
this.update(iter);
constructor(dev) {
this.update(dev);
}
update(iter) {
this.name = this._model.get_value(iter, GnomeBluetooth.Column.ALIAS) || this._model.get_value(iter, GnomeBluetooth.Column.NAME);
this.isConnected = this._model.get_value(iter, GnomeBluetooth.Column.CONNECTED);
this.isPaired = this._model.get_value(iter, GnomeBluetooth.Column.PAIRED);
this.mac = this._model.get_value(iter, GnomeBluetooth.Column.ADDRESS);
this.isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
update(dev) {
this.name = dev.alias || dev.name;
this.isConnected = dev.connected;
this.isPaired = dev.paired;
this.mac = dev.address;
}
disconnect() {
......
// Copyright 2018 Bartosz Jaroszewski
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const GnomeBluetooth = imports.gi.GnomeBluetooth;
const Signals = imports.signals;
const GLib = imports.gi.GLib;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
var BluetoothController = class {
constructor() {
this._client = new GnomeBluetooth.Client();
this._model = this._client.get_model();
}
enable() {
this._connectSignal(this._model, 'row-changed', (arg0, arg1, iter) => {
if (iter) {
let device = this._buildDevice(iter);
if (device.isDefault)
this.emit('default-adapter-changed', device);
else
this.emit('device-changed', device);
}
});
this._connectSignal(this._model, 'row-deleted', () => {
this.emit('device-deleted');
});
this._connectSignal(this._model, 'row-inserted', (arg0, arg1, iter) => {
if (iter) {
let device = this._buildDevice(iter);
this.emit('device-inserted', device);
}
});
}
getDevices() {
let adapter = this._getDefaultAdapter();
if (!adapter)
return [];
let devices = [];
let [ret, iter] = this._model.iter_children(adapter);
while (ret) {
let device = this._buildDevice(iter);
devices.push(device);
ret = this._model.iter_next(iter);
}
return devices;
}
getConnectedDevices() {
return this.getDevices().filter((device) => {
return device.isConnected;
});
}
destroy() {
this._disconnectSignals();
}
_getDefaultAdapter() {
let [ret, iter] = this._model.get_iter_first();
while (ret) {
let isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
let isPowered = this._model.get_value(iter, GnomeBluetooth.Column.POWERED);
if (isDefault && isPowered)
return iter;
ret = this._model.iter_next(iter);
}
return null;
}
_buildDevice(iter) {
return new BluetoothDevice(this._model, iter);
}
}
Signals.addSignalMethods(BluetoothController.prototype);
Utils.addSignalsHelperMethods(BluetoothController.prototype);
var BluetoothDevice = class {
constructor(model, iter) {
this._model = model;
this.update(iter);
}
update(iter) {
this.name = this._model.get_value(iter, GnomeBluetooth.Column.ALIAS) || this._model.get_value(iter, GnomeBluetooth.Column.NAME);
this.isConnected = this._model.get_value(iter, GnomeBluetooth.Column.CONNECTED);
this.isPaired = this._model.get_value(iter, GnomeBluetooth.Column.PAIRED);
this.mac = this._model.get_value(iter, GnomeBluetooth.Column.ADDRESS);
this.isDefault = this._model.get_value(iter, GnomeBluetooth.Column.DEFAULT);
}
disconnect() {
Utils.spawn(`bluetoothctl -- disconnect ${this.mac}`)
}
connect() {
Utils.spawn(`bluetoothctl -- connect ${this.mac}`)
}
reconnect() {
Utils.spawn(`bluetoothctl -- disconnect ${this.mac} && sleep 7 && bluetoothctl -- connect ${this.mac}`)
}
}
......@@ -20,7 +20,11 @@ const GLib = imports.gi.GLib;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const UiExtension = Me.imports.ui;
const Bluetooth = Me.imports.bluetooth;
const Bluetooth = imports.gi.GnomeBluetooth.Client.prototype.get_devices === undefined ?
Me.imports.bluetooth_legacy :
Me.imports.bluetooth;
const Utils = Me.imports.utils;
const Settings = Me.imports.settings.Settings;
const BatteryProvider = Me.imports.power.UPowerBatteryProvider;
......@@ -74,7 +78,7 @@ class BluetoothQuickConnect {
GLib.spawn_command_line_sync("bluetoothctl --version");
this._logger.info('Test succeeded');
} catch (error) {
Main.notifyError(_('Bluetooth quick connect'), _(`Error trying to execute "bluetoothctl"`));
Main.notifyError(_('Bluetooth Quick Connect'), _(`Error trying to execute "bluetoothctl"`));
this._logger.info('Test failed');
}
}
......@@ -82,6 +86,11 @@ class BluetoothQuickConnect {
_connectControllerSignals() {
this._logger.info('Connecting bluetooth controller signals');
this._connectSignal(this._controller, 'default-adapter-changed', (ctrl) => {
this._logger.info('Default adapter changed event');
this._refresh();
});
this._connectSignal(this._controller, 'device-inserted', (ctrl, device) => {
this._logger.info(`Device inserted event: ${device.name}`);
if (device.isPaired) {
......@@ -93,9 +102,7 @@ class BluetoothQuickConnect {
this._connectSignal(this._controller, 'device-changed', (ctrl, device) => {
this._logger.info(`Device changed event: ${device.name}`);
if (device.isDefault)
this._refresh();
else if (device.isPaired)
if (device.isPaired)
this._syncMenuItem(device);
else
this._logger.info(`Skipping change event for unpaired device ${device.name}`);
......@@ -221,4 +228,5 @@ function enable() {
function disable() {
bluetoothQuickConnect.disable();
bluetoothQuickConnect = null;
}
{
"name": "Bluetooth quick connect",
"name": "Bluetooth Quick Connect",
"description": "Allow to connect to paired devices from gnome control panel.\n",
"uuid": "bluetooth-quick-connect@bjarosze.gmail.com",
"url": "https://github.com/bjarosze/gnome-bluetooth-quick-connect",
......@@ -7,6 +7,7 @@
"gettext-domain": "bluetooth-quick-connect",
"shell-version": [
"40",
"41"
"41",
"42"
]
}
......@@ -3,7 +3,7 @@ const UPower = imports.gi.UPowerGlib;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
class UPowerBatteryProvider {
var UPowerBatteryProvider = class {
constructor(logger) {
this._upower_client = UPower.Client.new();
this._logger = logger;
......