-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
357 lines (323 loc) · 10.8 KB
/
extension.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import St from 'gi://St';
import Clutter from 'gi://Clutter';
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import {
Extension,
gettext as _
} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PanelMenu from 'resource:///org/gnome/shell/ui/panelMenu.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import * as MumblePing from './mumblePing.js';
import {ExtensionMetadata} from '@girs/gnome-shell/extensions/extension';
interface MumblePingConstructorParams {
settings: Gio.Settings;
dir: Gio.File;
metadata: ExtensionMetadata;
}
const enum Status {
DISABLED = 0,
NEUTRAL = 1,
ERROR = 2,
WAITING = 3,
}
const enum Icon {
NEUTRAL = 'icon_neutral.svg',
ERROR = 'icon_red.svg',
}
const enum Settings {
MUMBLE_PORT = 'mumble-port',
MUMBLE_HOST = 'mumble-host',
REFRESH_TIMEOUT = 'refresh-timeout',
}
interface IndicatorStatus {
lastResponse: MumblePing.MumblePingResult;
status: Status;
}
class MumbleIndicatorButton extends PanelMenu.Button {
#settings: Gio.Settings | null;
#mumbleIcon: St.Icon | undefined;
#numUsersLabel: St.Label | undefined;
#settingsSignalHandlers: number[] | null;
#indicatorStatus: IndicatorStatus | null;
#isDebugModeEnabled: boolean;
#autoCancel: Gio.Cancellable | undefined | null;
#connection: Gio.SocketConnection | undefined | null;
/**
* Timeout ID
*/
#mainLoopTimeout: number | undefined | null;
#metadata: ExtensionMetadata;
#dir: Gio.File;
constructor(params: MumblePingConstructorParams) {
super(0.0, _('Mumble Ping'), false);
this.#settings = params.settings;
this.#metadata = params.metadata;
this.#dir = params.dir;
this.#settingsSignalHandlers = [];
this.#indicatorStatus = {
lastResponse: {
users: 0,
maxUsers: 0,
},
status: Status.NEUTRAL,
};
this.#isDebugModeEnabled = this.#settings.get_boolean('debug');
this.#setupWidgets();
this.#attachSettingsSignalHandlers();
if (this.#settings.get_boolean('enabled')) {
// Refresh indicator on extension start immediately
this.#mainLoop();
this.#startMainLoop();
}
}
#setupWidgets() {
const menuLayout = new St.BoxLayout();
this.#mumbleIcon = new St.Icon({
styleClass: 'system-status-icon',
});
this.#setIndicatorIcon(Icon.NEUTRAL);
this.#numUsersLabel = new St.Label({
text: '',
yExpand: true,
yAlign: Clutter.ActorAlign.CENTER,
});
menuLayout.add_child(this.#mumbleIcon);
menuLayout.add_child(this.#numUsersLabel);
this.add_child(menuLayout);
}
#attachSignalHandler(signalName: string) {
if (this.#settings === null)
return;
const restart = () => {
this.#stopMainLoop();
if (this.#settings!.get_boolean('enabled')) {
this.#setIndicatorToWaiting();
this.#startMainLoop();
}
};
this.#settingsSignalHandlers?.push(
this.#settings.connect(`changed::${signalName}`, () => {
this.#log(`Changed ${signalName}`);
restart();
})
);
}
#setIndicatorToWaiting() {
if (this.#indicatorStatus!.status !== Status.WAITING) {
this.#numUsersLabel?.set_text('...');
this.#indicatorStatus!.status = Status.WAITING;
}
}
#attachSettingsSignalHandlers() {
this.#attachSignalHandler(Settings.MUMBLE_PORT);
this.#attachSignalHandler(Settings.MUMBLE_HOST);
this.#attachSignalHandler(Settings.REFRESH_TIMEOUT);
this.#settingsSignalHandlers?.push(
this.#settings!.connect('changed::debug', () => {
this.#log('Changed debug mode setting');
this.#isDebugModeEnabled = this.#settings!.get_boolean('debug');
})
);
}
/**
* Log a message if the extension is currently in debug mode
*
* @param msg Message to log
*/
#log(msg: string) {
if (this.#isDebugModeEnabled)
console.log(`${this.#metadata.name}: ${msg}`);
}
#startMainLoop() {
this.#mainLoopTimeout = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
this.#settings!.get_int(Settings.REFRESH_TIMEOUT),
() => {
this.#mainLoop();
return true;
}
);
}
/**
* Stop the main loop and clear the connection
*/
#stopMainLoop() {
if (this.#mainLoopTimeout) {
GLib.source_remove(this.#mainLoopTimeout);
this.#mainLoopTimeout = null;
}
this.#connection = null;
}
toggleEnableDisable() {
const enabledNow = !this.#settings!.get_boolean('enabled');
this.#log(
`Setting status of indicator to ${
enabledNow ? 'enabled' : 'disabled'
}`
);
this.#stopMainLoop();
this.#settings!.set_boolean('enabled', enabledNow);
if (enabledNow) {
this.#setIndicatorToWaiting();
this.#mainLoop();
this.#startMainLoop();
} else {
this.#cancelPendingRequests();
this.#numUsersLabel!.set_text('');
this.#indicatorStatus!.status = Status.DISABLED;
}
}
#cancelPendingRequests() {
this.#autoCancel?.cancel();
this.#autoCancel = null;
}
async #mainLoop() {
try {
this.#cancelPendingRequests();
this.#autoCancel = new Gio.Cancellable();
if (!this.#connection) {
const port = this.#settings!.get_int(Settings.MUMBLE_PORT);
const host = this.#settings!.get_string(Settings.MUMBLE_HOST);
this.#log(`Connecting to ${host} on port ${port}`);
this.#connection = await MumblePing.createClient(
host!,
port,
this.#autoCancel
);
}
this.#log('Sending Ping');
const pingResponse = await MumblePing.pingMumble(
this.#connection,
this.#autoCancel
);
this.#autoCancel = null;
this.#updateIndicator(pingResponse);
} catch (error: any) {
if (error.matches(Gio.IOErrorEnum, Gio.IOErrorEnum.CANCELLED)) {
this.#log('Cancelled previous operation');
this.#setIndicatorToError();
} else {
this.#connection = null;
this.#handleError(error, '_mainLoop');
}
}
}
/**
* Set mumble status indicator icon
*
* @param {string} iconFileName
*/
#setIndicatorIcon(iconFileName: string) {
const iconPath = this.#dir
.get_child('icons')
.get_child(iconFileName)
.get_path();
this.#mumbleIcon?.set_gicon(Gio.Icon.new_for_string(iconPath!));
}
/**
* Update the indicator based on the ping response
*
* @param pingResponse
*/
#updateIndicator(pingResponse: MumblePing.MumblePingResult) {
if (this.#hasStatusChanged(pingResponse)) {
this.#numUsersLabel?.set_text(
`${pingResponse.users}/${pingResponse.maxUsers}`
);
if (this.#indicatorStatus!.status !== Status.NEUTRAL)
this.#setIndicatorIcon(Icon.NEUTRAL);
this.#indicatorStatus!.lastResponse = pingResponse;
this.#indicatorStatus!.status = Status.NEUTRAL;
}
}
#setIndicatorToError() {
if (!this.#indicatorStatus?.status)
return;
if (this.#indicatorStatus.status !== Status.ERROR) {
this.#setIndicatorIcon(Icon.ERROR);
this.#numUsersLabel?.set_text('');
this.#indicatorStatus.status = Status.ERROR;
}
}
#handleError(error: object | string, method = '') {
this.#log(`${method}: ${error}`);
this.#setIndicatorToError();
}
#hasStatusChanged(result: MumblePing.MumblePingResult): boolean {
if (!result)
return false;
const lastNumUsers = this.#indicatorStatus!.lastResponse?.users;
const lastMaxUsers = this.#indicatorStatus!.lastResponse?.maxUsers;
const lastStatus = this.#indicatorStatus!.status;
const updateNeeded =
result.users !== lastNumUsers ||
result.maxUsers !== lastMaxUsers ||
lastStatus !== Status.NEUTRAL;
return updateNeeded;
}
destroy() {
this.#stopMainLoop();
this.#cancelPendingRequests();
this.#settingsSignalHandlers?.forEach(handle => {
this.#settings!.disconnect(handle);
});
this.#settingsSignalHandlers = null;
this.#indicatorStatus = null;
this.#settings = null;
super.destroy();
}
}
const MumblePingIndicator = GObject.registerClass(MumbleIndicatorButton);
export default class MumblePingExtension extends Extension {
#indicator: MumbleIndicatorButton | undefined | null;
#settings?: Gio.Settings | null;
enable() {
this.#settings = this.getSettings();
this.#indicator = new MumblePingIndicator({
metadata: this.metadata,
settings: this.#settings,
dir: this.dir,
});
this.#setupPopupMenu();
Main.panel.addToStatusArea(this.uuid, this.#indicator);
}
disable() {
this.#log('disabling extension.');
this.#indicator?.destroy();
this.#indicator = null;
this.#settings = null;
}
#setupPopupMenu() {
const enableDisableMenuItem = new PopupMenu.PopupSwitchMenuItem(
_('Enable/Disable'),
this.#settings!.get_boolean('enabled')
);
enableDisableMenuItem.connect('activate', () => {
this.#indicator?.toggleEnableDisable();
});
(this.#indicator!.menu as PopupMenu.PopupMenu).addMenuItem(
enableDisableMenuItem
);
(this.#indicator!.menu as PopupMenu.PopupMenu).addMenuItem(
new PopupMenu.PopupSeparatorMenuItem()
);
(this.#indicator!.menu as PopupMenu.PopupMenu).addAction(
_('Settings'),
() => {
this.openPreferences();
}
);
}
/**
* Log a message if the extension is currently in debug mode
*
* @param msg Message to log
*/
#log(msg: string) {
if (this.#settings!.get_boolean('debug'))
console.debug(`${this.metadata.name}: ${msg}`);
}
}