Skip to content

Commit

Permalink
Start implementing Network Fuse to manage network connections and set…
Browse files Browse the repository at this point in the history
…tings
  • Loading branch information
lainsce committed Sep 12, 2024
1 parent d89d112 commit 0c2fe43
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 0 deletions.
1 change: 1 addition & 0 deletions fuses/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Add each fuse subdir here, order matters!

# Network
subdir('network')
subdir('bluetooth')

# Personal
Expand Down
203 changes: 203 additions & 0 deletions fuses/network/NetworkFuse.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
public class Network.NetworkFuse : Fusebox.Fuse {
private Gtk.Box main_box;
private NM.Client? nm_client;

private He.MiniContentBlock wired_block;
private He.MiniContentBlock vpn_block;
private He.MiniContentBlock proxy_block;

private Gtk.Switch wired_switch;
private Gtk.Label wired_status_label;
private Gtk.Switch proxy_switch;

public NetworkFuse () {
Object (
category: Category.NETWORK,
code_name: "network-fuse",
display_name: _("Network"),
description: _("Manage network connections and settings"),
icon: "preferences-desktop-network-symbolic",
supported_settings: new GLib.HashTable<string, string?> (null, null)
);
supported_settings.set ("network", null);
}

construct {
try {
nm_client = new NM.Client();
} catch (Error e) {
warning("Failed to create NM.Client: %s", e.message);
}
}

public override Gtk.Widget get_widget () {
if (main_box == null) {
main_box = new Gtk.Box(Gtk.Orientation.VERTICAL, 12) {
margin_start = 18,
margin_end = 18,
margin_bottom = 18
};

create_wired_section();
create_vpn_section();
create_proxy_section();

var view_label = new Gtk.Label ("Network") {
halign = Gtk.Align.START
};
view_label.add_css_class ("view-title");

var appbar = new He.AppBar () {
viewtitle_widget = view_label,
show_back = false,
show_left_title_buttons = false,
show_right_title_buttons = true
};

main_box.append(appbar);
main_box.append(wired_block);
main_box.append(vpn_block);
main_box.append(proxy_block);
}

return main_box;
}

private void create_wired_section() {
wired_block = new He.MiniContentBlock();
wired_block.title = (_("Wired"));

var wired_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 12);
wired_status_label = new Gtk.Label("") {
hexpand = true,
halign = Gtk.Align.START
};
wired_switch = new Gtk.Switch() {
valign = Gtk.Align.CENTER
};

var settings_button = new Gtk.Button.from_icon_name("emblem-system-symbolic");
settings_button.add_css_class("circular");

wired_box.append(wired_status_label);
wired_box.append(wired_switch);
wired_box.append(settings_button);

wired_block.widget = wired_box;

wired_switch.notify["active"].connect(update_wired_status);
settings_button.clicked.connect(open_wired_settings);
}

private void create_vpn_section() {
vpn_block = new He.MiniContentBlock();
vpn_block.title = (_("VPN"));

var vpn_label = new Gtk.Label(_("Not set up")) {
hexpand = true,
halign = Gtk.Align.START
};

vpn_block.widget = vpn_label;
}

private void create_proxy_section() {
proxy_block = new He.MiniContentBlock();
proxy_block.title = (_("Proxy"));

var proxy_box = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 12);
proxy_switch = new Gtk.Switch() {
valign = Gtk.Align.CENTER
};

var proxy_settings_button = new Gtk.Button.with_label(_("Off")) {
valign = Gtk.Align.CENTER
};
proxy_settings_button.add_css_class("flat");

proxy_box.append(proxy_switch);
proxy_box.append(proxy_settings_button);

proxy_block.widget = proxy_box;

proxy_switch.notify["active"].connect(update_proxy_status);
proxy_settings_button.clicked.connect(open_proxy_settings);
}

private void update_wired_status() {
if (nm_client == null) return;

NM.Device? wired_device = null;
foreach (var device in nm_client.get_devices()) {
if (device.get_device_type() == NM.DeviceType.ETHERNET) {
wired_device = device;
break;
}
}

if (wired_device != null) {
var active_connection = wired_device.get_active_connection();
if (active_connection != null) {
wired_status_label.label = _("Connected - %u Mb/s").printf(((NM.DeviceEthernet)wired_device).get_speed());
wired_switch.active = true;
} else {
wired_status_label.label = _("Disconnected");
wired_switch.active = false;
}
} else {
wired_status_label.label = _("Not available");
wired_switch.sensitive = false;
}
}

private void update_proxy_status() {
if (proxy_switch.active) {
((Gtk.Label)((Gtk.Button)proxy_switch.get_next_sibling()).get_child()).label = _("On");
} else {
((Gtk.Label)((Gtk.Button)proxy_switch.get_next_sibling()).get_child()).label = _("Off");
}
}

private void open_wired_settings() {
try {
Process.spawn_command_line_async("nm-connection-editor --type=ethernet");
} catch (Error e) {
warning("Failed to open wired settings: %s", e.message);
}
}

private void open_proxy_settings() {
print("Opening proxy settings...\n");
}

public override void shown() {
update_wired_status();
update_proxy_status();

if (nm_client != null) {
nm_client.notify["active-connections"].connect(update_wired_status);
}
}

public override void hidden() {
if (nm_client != null) {
nm_client.notify["active-connections"].disconnect(update_wired_status);
}
}

public override void search_callback(string location) {

}

public override async GLib.HashTable<string, string> search(string search) {
var results = new GLib.HashTable<string, string>(null, null);

return results;
}
}

public Fusebox.Fuse get_fuse(Module module) {
debug("Activating Network fuse");
var fuse = new Network.NetworkFuse();
return fuse;
}
20 changes: 20 additions & 0 deletions fuses/network/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fuse_files = files(
'NetworkFuse.vala',
)

shared_module(
'fuse-network',
fuse_files,
dependencies: [
meson.get_compiler('vala').find_library('posix'),
dependency('glib-2.0', version: '>=2.34'),
dependency('gio-2.0', version: '>=2.34'),
dependency('gobject-2.0', version: '>=2.34'),
libfusebox_dep,
dependency('libhelium-1'),
dependency('gtk4', version: '>=4.12'),
dependency('libnm', version: '>=1.2'),
],
install: true,
install_dir: join_paths(fuses_dir, 'network'),
)

0 comments on commit 0c2fe43

Please sign in to comment.