Skip to content

Commit

Permalink
fix(network): open a browser when connection is behind a captive portal
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Feb 2, 2024
1 parent 1707362 commit e6ec592
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
18 changes: 17 additions & 1 deletion cosmic-applet-network/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
},
Expand Down
34 changes: 25 additions & 9 deletions cosmic-applet-network/src/network_manager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -512,28 +512,42 @@ pub enum NetworkManagerEvent {
ActiveConns(NetworkManagerState),
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct NetworkManagerState {
pub wireless_access_points: Vec<AccessPoint>,
pub active_conns: Vec<ActiveConnectionInfo>,
pub known_access_points: Vec<AccessPoint>,
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<Self> {
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")
.arg("bluetooth")
.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;
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit e6ec592

Please sign in to comment.