summaryrefslogtreecommitdiff
path: root/extension.js
blob: acd316c46445d6d8faa4995ed705ee3c7d1d0652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const Main = imports.ui.main;
const GnomeBluetooth = imports.gi.GnomeBluetooth;
const PopupMenu = imports.ui.popupMenu;
const GLib = imports.gi.GLib;
const Util = imports.misc.util;

class BluetoothDevice {
    constructor(model, device) {
        this._name = model.get_value(device, GnomeBluetooth.Column.NAME);
        this._isConnected = model.get_value(device, GnomeBluetooth.Column.CONNECTED);
        this._isPaired = model.get_value(device, GnomeBluetooth.Column.PAIRED);
        this._mac = model.get_value(device, GnomeBluetooth.Column.ADDRESS);
    }

    get name() {
        return this._name;
    }

    get isConnected() {
        return this._isConnected;
    }

    get isPaired() {
        return this._isPaired;
    }

    get mac() {
        return this._mac;
    }

    get item() {
        if (!this._item)
            this._buildMenuItem();

        return this._item;
    }

    _buildMenuItem() {
        this._item = new PopupMenu.PopupSwitchMenuItem(this.name, this.isConnected);
        this._item.isDeviceSwitcher = true;
        this._item.connect('toggled', (item, state) => {
            if (state)
                this._connect();
            else
                this._disconnect();
        });
    }

    _disconnect() {
        let command = `echo -e "disconnect ${this.mac}\\n" | bluetoothctl`;
        Util.spawn(['/bin/bash', '-c', command]);
    }

    _connect() {
        let command = `echo -e "connect ${this.mac}\\n" | bluetoothctl`;
        Util.spawn(['/bin/bash', '-c', command]);
    }
}

class BluetoothQuickConnect {
    constructor(bluetooth) {
        this._model = bluetooth._model;
        this._getDefaultAdapter = bluetooth._getDefaultAdapter;
        this._menu = bluetooth._item.menu;

        this._signals = [];
    }

    enable() {
        let signal = this._menu.connect('open-state-changed', (menu, isOpen) => {
            if (isOpen)
                this._sync();
        });

        this._signals.push(signal);
    }

    disable() {
        this._destroy();
    }

    _getPairedDevices() {
        let adapter = this._getDefaultAdapter();
        if (!adapter)
            return [];

        let pairedDevices = [];

        let [ret, iter] = this._model.iter_children(adapter);
        while (ret) {
            let bluetoothDevice = new BluetoothDevice(this._model, iter);
            if (bluetoothDevice.isPaired || bluetoothDevice.isConnected)
                pairedDevices.push(bluetoothDevice);

            ret = this._model.iter_next(iter);
        }

        return pairedDevices;
    }

    _sync() {
        this._removeDevicesFromMenu();
        this._addDevicesToMenu();
    }

    _addDevicesToMenu() {
        this._getPairedDevices().forEach((device) => {
            this._menu.addMenuItem(device.item, 1);
        });
    }

    _removeDevicesFromMenu() {
        this._menu._getMenuItems().forEach((item) => {
            if (item.isDeviceSwitcher) {
                item.destroy();
            }
        });
    }

    _destroy() {
        this._signals.forEach((signal) => {
            this._menu.disconnect(signal);
        });
        this._removeDevicesFromMenu();
    }
}


function init() {
    let bluetooth = Main.panel.statusArea.aggregateMenu._bluetooth;
    bluetoothQuickConnect = new BluetoothQuickConnect(bluetooth);
}

function enable() {
    bluetoothQuickConnect.enable();
}

function disable() {
    bluetoothQuickConnect.disable();
}