Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): open a browser when connection is behind a captive portal #170

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 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,16 @@ impl cosmic::Application for CosmicNetworkApplet {
}
}
}

if self.nm_state.connectivity != state.connectivity
&& !matches!(req, NetworkManagerRequest::Reload)
&& matches!(state.connectivity, NmConnectivityState::Portal)
{
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);
}
},
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
Loading