From 0ba98dff62aca1e4d8e588d7d5d3ac26b3d92827 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Tue, 12 Dec 2023 15:39:44 -0500 Subject: [PATCH 1/2] fix(network): open a browser when connection is behind a captive portal --- cosmic-applet-network/src/app.rs | 18 +++++++++- .../src/network_manager/mod.rs | 34 ++++++++++++++----- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cosmic-applet-network/src/app.rs b/cosmic-applet-network/src/app.rs index 3dd47ee6..369164e3 100644 --- a/cosmic-applet-network/src/app.rs +++ b/cosmic-applet-network/src/app.rs @@ -21,7 +21,9 @@ use cosmic::{ widget::{button, divider, icon}, Element, Theme, }; -use cosmic_dbus_networkmanager::interface::enums::{ActiveConnectionState, DeviceState}; +use cosmic_dbus_networkmanager::interface::enums::{ + ActiveConnectionState, DeviceState, NmConnectivityState, +}; use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline}; use futures::channel::mpsc::UnboundedSender; @@ -306,6 +308,20 @@ impl cosmic::Application for CosmicNetworkApplet { } } } + + if self.nm_state.connectivity != state.connectivity + && !matches!(req, NetworkManagerRequest::Reload) + && matches!(state.connectivity, NmConnectivityState::Portal) + { + // spawn browser + if let Err(err) = std::process::Command::new("xdg-open") + .arg("http://pop.system76.com") + .spawn() + { + log::error!("Failed to open browser: {}", err); + } + } + self.update_nm_state(state); } }, diff --git a/cosmic-applet-network/src/network_manager/mod.rs b/cosmic-applet-network/src/network_manager/mod.rs index 9153a0fb..ef0a3920 100644 --- a/cosmic-applet-network/src/network_manager/mod.rs +++ b/cosmic-applet-network/src/network_manager/mod.rs @@ -11,8 +11,8 @@ use cosmic_dbus_networkmanager::{ device::SpecificDevice, interface::{ active_connection::ActiveConnectionProxy, - enums::DeviceType, enums::{self, ActiveConnectionState}, + enums::{DeviceType, NmConnectivityState}, }, nm::NetworkManager, settings::{connection::Settings, NetworkManagerSettings}, @@ -512,19 +512,33 @@ pub enum NetworkManagerEvent { ActiveConns(NetworkManagerState), } -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone)] pub struct NetworkManagerState { pub wireless_access_points: Vec, pub active_conns: Vec, pub known_access_points: Vec, pub wifi_enabled: bool, pub airplane_mode: bool, + pub connectivity: NmConnectivityState, +} + +impl Default for NetworkManagerState { + fn default() -> Self { + Self { + wireless_access_points: Vec::new(), + active_conns: Vec::new(), + known_access_points: Vec::new(), + wifi_enabled: false, + airplane_mode: false, + connectivity: NmConnectivityState::Unknown, + } + } } impl NetworkManagerState { pub async fn new(conn: &Connection) -> anyhow::Result { let network_manager = NetworkManager::new(conn).await?; - let mut _self = Self::default(); + let mut self_ = Self::default(); // airplane mode let airplaine_mode = Command::new("rfkill") .arg("list") @@ -532,8 +546,8 @@ impl NetworkManagerState { .output() .await?; let airplane_mode = std::str::from_utf8(&airplaine_mode.stdout).unwrap_or_default(); - _self.wifi_enabled = network_manager.wireless_enabled().await.unwrap_or_default(); - _self.airplane_mode = airplane_mode.contains("Soft blocked: yes") && !_self.wifi_enabled; + self_.wifi_enabled = network_manager.wireless_enabled().await.unwrap_or_default(); + self_.airplane_mode = airplane_mode.contains("Soft blocked: yes") && !self_.wifi_enabled; let s = NetworkManagerSettings::new(conn).await?; _ = s.load_connections(&[]).await; @@ -595,10 +609,12 @@ impl NetworkManagerState { .cloned() .collect(); wireless_access_points.sort_by(|a, b| b.strength.cmp(&a.strength)); - _self.wireless_access_points = wireless_access_points; - _self.active_conns = active_conns; - _self.known_access_points = known_access_points; - Ok(_self) + self_.wireless_access_points = wireless_access_points; + self_.active_conns = active_conns; + self_.known_access_points = known_access_points; + self_.connectivity = network_manager.connectivity().await?; + + Ok(self_) } #[allow(dead_code)] From d22a593237ded592e92d788b53da2cb7f6d133a1 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Fri, 2 Feb 2024 11:56:33 -0500 Subject: [PATCH 2/2] fix: use the correct url and use cosmic spawn --- cosmic-applet-network/src/app.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/cosmic-applet-network/src/app.rs b/cosmic-applet-network/src/app.rs index 369164e3..04842a35 100644 --- a/cosmic-applet-network/src/app.rs +++ b/cosmic-applet-network/src/app.rs @@ -313,13 +313,9 @@ impl cosmic::Application for CosmicNetworkApplet { && !matches!(req, NetworkManagerRequest::Reload) && matches!(state.connectivity, NmConnectivityState::Portal) { - // spawn browser - if let Err(err) = std::process::Command::new("xdg-open") - .arg("http://pop.system76.com") - .spawn() - { - log::error!("Failed to open browser: {}", err); - } + let mut browser = std::process::Command::new("xdg-open"); + browser.arg("http://204.pop-os.org/"); + cosmic::process::spawn(browser); } self.update_nm_state(state);