Files

117 lines
4.1 KiB
JavaScript

import Adw from 'gi://Adw';
import Gio from 'gi://Gio';
import Gtk from 'gi://Gtk';
import {ExtensionPreferences, gettext as _} from
'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
export default class NetbirdHooksPreferences extends ExtensionPreferences {
fillPreferencesWindow(window) {
this._rows = [];
const settings = this.getSettings();
const page = new Adw.PreferencesPage();
const gen = new Adw.PreferencesGroup({title: _('Enforcement')});
const enforceRow = new Adw.SwitchRow({
title: _('Enforce netbird on open wifi'),
subtitle: _('Connect netbird (with its default exit selection) whenever the active wifi is unencrypted'),
});
settings.bind('enforce', enforceRow, 'active',
Gio.SettingsBindFlags.DEFAULT);
gen.add(enforceRow);
const indRow = new Adw.SwitchRow({
title: _('Show panel indicator'),
subtitle: _('Top-bar icon: green = netbird connected, red = down'),
});
settings.bind('show-indicator', indRow, 'active',
Gio.SettingsBindFlags.DEFAULT);
gen.add(indRow);
page.add(gen);
const tg = new Adw.PreferencesGroup({
title: _('Trusted networks'),
description: _('Netbird is never enforced on these SSIDs'),
});
this._group = tg;
this._rebuildSsidRows(settings);
settings.connect('changed::trusted-ssids',
() => this._rebuildSsidRows(settings));
const box = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 6,
margin_top: 6,
});
const entry = new Gtk.Entry({
hexpand: true,
placeholder_text: _('SSID'),
});
entry.connect('activate', () => this._addSsid(entry.get_text(), settings, entry));
box.append(entry);
const addBtn = new Gtk.Button({label: _('Add')});
addBtn.connect('clicked', () => this._addSsid(entry.get_text(), settings, entry));
box.append(addBtn);
const curBtn = new Gtk.Button({label: _('Add current network')});
curBtn.connect('clicked', () => this._addCurrent(settings));
box.append(curBtn);
tg.add(box);
page.add(tg);
window.add(page);
}
_rebuildSsidRows(settings) {
for (const row of this._rows)
this._group.remove(row);
this._rows = [];
for (const ssid of settings.get_strv('trusted-ssids')) {
const row = new Adw.ActionRow({title: ssid});
const btn = new Gtk.Button({
icon_name: 'user-trash-symbolic',
valign: Gtk.Align.CENTER,
});
btn.connect('clicked', () => {
settings.set_strv('trusted-ssids',
settings.get_strv('trusted-ssids').filter(x => x !== ssid));
});
row.add_suffix(btn);
this._group.add(row);
this._rows.push(row);
}
}
_addSsid(text, settings, entry) {
const ssid = (text ?? '').trim();
if (!ssid)
return;
const list = settings.get_strv('trusted-ssids');
if (!list.includes(ssid)) {
list.push(ssid);
settings.set_strv('trusted-ssids', list);
}
if (entry)
entry.set_text('');
}
_addCurrent(settings) {
const cmd = ['sh', '-c',
'd=$(nmcli -t -f DEVICE,TYPE,STATE device 2>/dev/null | ' +
"awk -F: '$2 == \"wifi\" && $3 == \"connected\" {print $1; exit}'); " +
'[ -n "$d" ] && iw dev "$d" link 2>/dev/null | ' +
"sed -n 's/^[[:space:]]*SSID: //p'"];
try {
const proc = Gio.Subprocess.new(cmd,
Gio.SubprocessFlags.STDOUT_PIPE);
proc.communicate_utf8_async(null, null, (p, res) => {
try {
const [, out] = p.communicate_utf8_finish(res);
this._addSsid((out ?? '').trim(), settings, null);
} catch {
}
});
} catch {
}
}
}