`cosmic-applet-network` listed every connection from `nm.list_saved_connections()` in the VPN dropdown, including the in-memory-only profiles NetworkManager auto-generates for externally-managed interfaces (e.g. a `wg-quick@wg0.service` tunnel). Toggling such an "assumed" connection off deletes it from NM — it was never persisted — leaving a dead toggle with no way back through the applet or `nmcli con up`. Skip connections flagged `unsaved` (in-memory only) at the `load_vpns()` site so they no longer get a togglable entry. The active-connections section still shows the interface as connected (read-only). One-line change in `cosmic-applet-network/src/app.rs`. ## Test plan - Built on Pop!_OS and ran the patched `cosmic-applet-network`: with `wg-quick@wg0` up and NM tracking `wg0` as connected-externally, the VPN dropdown no longer shows the destructive `wg0` toggle; the active-connections section still shows `wg0`; Wi-Fi toggling is unaffected. Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2036 lines
81 KiB
Rust
2036 lines
81 KiB
Rust
use anyhow::Context;
|
|
use cosmic_settings_network_manager_subscription::{
|
|
self as network_manager, NetworkManagerState, UUID,
|
|
available_wifi::{AccessPoint, NetworkType},
|
|
current_networks::ActiveConnectionInfo,
|
|
hw_address::HwAddress,
|
|
};
|
|
use indexmap::IndexMap;
|
|
use nmrs::{
|
|
NetworkManager as NmrsManager, SettingsSummary,
|
|
agent::{SecretAgent, SecretAgentCapabilities, SecretRequest, SecretResponder, SecretSetting},
|
|
};
|
|
use rustc_hash::FxHashSet;
|
|
use secure_string::SecureString;
|
|
use std::{
|
|
borrow::Cow,
|
|
collections::{BTreeMap, HashMap},
|
|
sync::{Arc, LazyLock},
|
|
};
|
|
|
|
use cosmic::{
|
|
Apply, Element, Task, app,
|
|
applet::{
|
|
menu_button, menu_control_padding, padded_control,
|
|
token::subscription::{TokenRequest, TokenUpdate, activation_token_subscription},
|
|
},
|
|
cctk::sctk::reexports::calloop,
|
|
cosmic_theme::Spacing,
|
|
iced::core::window,
|
|
iced::{
|
|
Alignment, Length, Subscription,
|
|
platform_specific::shell::wayland::commands::popup::{destroy_popup, get_popup},
|
|
},
|
|
surface, theme,
|
|
widget::{
|
|
Id, button, column, container, divider,
|
|
icon::{self, from_name},
|
|
row, scrollable, secure_input, text, text_input, toggler,
|
|
},
|
|
};
|
|
use cosmic_dbus_networkmanager::interface::enums::{
|
|
ActiveConnectionState, DeviceState, NmConnectivityState,
|
|
};
|
|
|
|
use futures::{StreamExt, lock::Mutex as AsyncMutex};
|
|
use zbus::Connection;
|
|
|
|
use crate::{config, fl};
|
|
|
|
pub fn run() -> cosmic::iced::Result {
|
|
cosmic::applet::run::<CosmicNetworkApplet>(())
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
enum NewConnectionState {
|
|
EnterPassword {
|
|
access_point: AccessPoint,
|
|
description: Option<String>,
|
|
identity: String,
|
|
password: SecureString,
|
|
password_hidden: bool,
|
|
},
|
|
Waiting(AccessPoint),
|
|
Failure(AccessPoint),
|
|
}
|
|
|
|
impl NewConnectionState {
|
|
pub fn ssid(&self) -> &str {
|
|
&match self {
|
|
Self::EnterPassword { access_point, .. } => access_point,
|
|
Self::Waiting(ap) => ap,
|
|
Self::Failure(ap) => ap,
|
|
}
|
|
.ssid
|
|
}
|
|
}
|
|
|
|
impl From<NewConnectionState> for AccessPoint {
|
|
fn from(connection_state: NewConnectionState) -> Self {
|
|
match connection_state {
|
|
NewConnectionState::EnterPassword { access_point, .. } => access_point,
|
|
NewConnectionState::Waiting(access_point) => access_point,
|
|
NewConnectionState::Failure(access_point) => access_point,
|
|
}
|
|
}
|
|
}
|
|
|
|
pub static SECURE_INPUT_WIFI: LazyLock<Id> = LazyLock::new(Id::unique);
|
|
|
|
#[derive(Default, Debug, Clone)]
|
|
pub struct MyNetworkState {
|
|
pub known_vpns: IndexMap<UUID, ConnectionSettings>,
|
|
pub ssid_to_uuid: BTreeMap<Box<str>, Box<str>>,
|
|
pub devices: Vec<Arc<network_manager::devices::DeviceInfo>>,
|
|
pub nm_state: NetworkManagerState,
|
|
pub requested_vpn: Option<RequestedVpn>,
|
|
}
|
|
|
|
/// Shared, take-once handle to an `nmrs` [`SecretResponder`]. Cloned freely
|
|
/// across `Message` boundaries; the first consumer to `lock().take()` it owns
|
|
/// the reply to NetworkManager.
|
|
pub type SecretResponderHandle = Arc<AsyncMutex<Option<SecretResponder>>>;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct RequestedVpn {
|
|
uuid: Arc<str>,
|
|
description: Option<String>,
|
|
password: SecureString,
|
|
password_hidden: bool,
|
|
responder: SecretResponderHandle,
|
|
/// VPN secret keys NM hinted as needed (e.g. `["password"]`). When empty,
|
|
/// `"password"` is used as a fallback.
|
|
secret_keys: Vec<String>,
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub enum ConnectionSettings {
|
|
Vpn { id: String },
|
|
Wireguard { id: String },
|
|
}
|
|
|
|
/// Local mirror of the secret-agent events the applet cares about. Sourced
|
|
/// from `nmrs::agent` instead of the previous `nm_secret_agent` subscription.
|
|
#[derive(Debug, Clone)]
|
|
pub enum NmAgentEvent {
|
|
RequestSecret {
|
|
connection_uuid: String,
|
|
connection_id: String,
|
|
setting: AgentSetting,
|
|
responder: SecretResponderHandle,
|
|
},
|
|
CancelGetSecrets,
|
|
Failed(String),
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum AgentSetting {
|
|
WifiPsk { ssid: String },
|
|
WifiEap,
|
|
Vpn { secret_keys: Vec<String> },
|
|
Other,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct CosmicNetworkApplet {
|
|
core: cosmic::app::Core,
|
|
icon_name: String,
|
|
popup: Option<window::Id>,
|
|
|
|
// NM state
|
|
nm_sender: Option<futures::channel::mpsc::UnboundedSender<network_manager::Request>>,
|
|
nm_task: Option<tokio::sync::oneshot::Sender<()>>,
|
|
nm_state: MyNetworkState,
|
|
|
|
// UI state
|
|
show_visible_networks: bool,
|
|
show_available_vpns: bool,
|
|
new_connection: Option<NewConnectionState>,
|
|
conn: Option<Connection>,
|
|
toggle_wifi_ctr: u128,
|
|
token_tx: Option<calloop::channel::Sender<TokenRequest>>,
|
|
failed_known_ssids: FxHashSet<Arc<str>>,
|
|
|
|
/// When defined, displays connections for the specific device.
|
|
active_device: Option<Arc<network_manager::devices::DeviceInfo>>,
|
|
}
|
|
|
|
fn wifi_icon(strength: u8) -> &'static str {
|
|
if strength < 25 {
|
|
"network-wireless-signal-weak-symbolic"
|
|
} else if strength < 50 {
|
|
"network-wireless-signal-ok-symbolic"
|
|
} else if strength < 75 {
|
|
"network-wireless-signal-good-symbolic"
|
|
} else {
|
|
"network-wireless-signal-excellent-symbolic"
|
|
}
|
|
}
|
|
|
|
fn vpn_section<'a>(
|
|
nm_state: &'a MyNetworkState,
|
|
show_available_vpns: bool,
|
|
space_xxs: u16,
|
|
space_s: u16,
|
|
) -> cosmic::iced::widget::Column<'a, Message, cosmic::Theme> {
|
|
let mut vpn_col = cosmic::widget::column::with_capacity::<'_, Message, cosmic::Theme, _>(4);
|
|
|
|
if !nm_state.known_vpns.is_empty() {
|
|
let dropdown_icon = if show_available_vpns {
|
|
"go-up-symbolic"
|
|
} else {
|
|
"go-down-symbolic"
|
|
};
|
|
|
|
vpn_col = vpn_col
|
|
.push(padded_control(divider::horizontal::default()).padding([space_xxs, space_s]));
|
|
|
|
if let Some(requested_vpn) = nm_state.requested_vpn.as_ref() {
|
|
let column_content = vec![
|
|
text::body(
|
|
requested_vpn
|
|
.description
|
|
.as_deref()
|
|
.unwrap_or(requested_vpn.uuid.as_ref()),
|
|
)
|
|
.width(Length::Fill)
|
|
.into(),
|
|
secure_input(
|
|
"",
|
|
Cow::Borrowed(requested_vpn.password.unsecure()),
|
|
Some(Message::ToggleVPNPasswordVisibility),
|
|
requested_vpn.password_hidden,
|
|
)
|
|
.on_input(|s| Message::VPNPasswordUpdate(s.into()))
|
|
.on_paste(|s| Message::VPNPasswordUpdate(s.into()))
|
|
.on_submit(|_| Message::ConnectVPNWithPassword)
|
|
.width(Length::Fill)
|
|
.into(),
|
|
row::with_children([
|
|
button::standard(fl!("cancel"))
|
|
.on_press(Message::CancelVPNConnection)
|
|
.into(),
|
|
button::suggested(fl!("connect"))
|
|
.on_press(Message::ConnectVPNWithPassword)
|
|
.into(),
|
|
])
|
|
.spacing(24)
|
|
.into(),
|
|
];
|
|
let col: Element<'a, Message> = Element::from(
|
|
padded_control(
|
|
column::with_children(column_content)
|
|
.spacing(8)
|
|
.align_x(Alignment::Center),
|
|
)
|
|
.align_x(Alignment::Center),
|
|
);
|
|
vpn_col = vpn_col.push(col);
|
|
}
|
|
|
|
let vpn_toggle_btn = menu_button(row::with_children([
|
|
Element::from(
|
|
text::body(fl!("vpn-connections"))
|
|
.width(Length::Fill)
|
|
.height(Length::Fixed(24.0))
|
|
.align_y(Alignment::Center),
|
|
),
|
|
container(icon::from_name(dropdown_icon).size(16).symbolic(true))
|
|
.center(Length::Fixed(24.0))
|
|
.into(),
|
|
]))
|
|
.on_press(Message::ToggleVpnList);
|
|
|
|
vpn_col = vpn_col.push(vpn_toggle_btn);
|
|
|
|
if show_available_vpns {
|
|
for (uuid, connection) in &nm_state.known_vpns {
|
|
let id = match connection {
|
|
ConnectionSettings::Vpn { id } | ConnectionSettings::Wireguard { id } => {
|
|
id.as_str()
|
|
}
|
|
};
|
|
// Check if this VPN is currently active
|
|
let is_active = nm_state.nm_state.active_conns.iter().any(
|
|
|conn| matches!(conn, ActiveConnectionInfo::Vpn { name, .. } if name == id),
|
|
);
|
|
|
|
let mut btn_content = vec![
|
|
icon::from_name("network-vpn-symbolic")
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
text::body(id).width(Length::Fill).into(),
|
|
];
|
|
|
|
if is_active {
|
|
btn_content.push(text::body(fl!("connected")).align_x(Alignment::End).into());
|
|
}
|
|
|
|
let mut btn = menu_button(
|
|
row::with_children(btn_content)
|
|
.align_y(Alignment::Center)
|
|
.spacing(8),
|
|
);
|
|
|
|
btn = if is_active {
|
|
btn.on_press(Message::DeactivateVpn(uuid.clone()))
|
|
} else {
|
|
btn.on_press(Message::ActivateVpn(uuid.clone()))
|
|
};
|
|
|
|
vpn_col = vpn_col.push(btn);
|
|
}
|
|
}
|
|
}
|
|
|
|
vpn_col
|
|
}
|
|
|
|
impl CosmicNetworkApplet {
|
|
fn update_nm_state(&mut self, mut new_state: NetworkManagerState) {
|
|
self.update_togglers(&new_state);
|
|
// check for failed conns that can be reset
|
|
for new_s in &mut new_state.active_conns {
|
|
let ActiveConnectionInfo::WiFi { state, .. } = new_s else {
|
|
continue;
|
|
};
|
|
|
|
if matches!(state, ActiveConnectionState::Activated) {
|
|
self.failed_known_ssids.remove(new_s.name().as_str());
|
|
continue;
|
|
}
|
|
if matches!(
|
|
state,
|
|
ActiveConnectionState::Activating | ActiveConnectionState::Deactivating
|
|
) {
|
|
continue;
|
|
}
|
|
|
|
if self.nm_state.nm_state.active_conns.iter().any(|old_s| {
|
|
matches!(
|
|
old_s,
|
|
ActiveConnectionInfo::WiFi {
|
|
state: ActiveConnectionState::Activating,
|
|
..
|
|
} if new_s.name() == old_s.name()
|
|
)
|
|
}) {
|
|
self.failed_known_ssids.insert(new_s.name().into());
|
|
}
|
|
}
|
|
self.nm_state.nm_state = new_state;
|
|
self.update_icon_name();
|
|
}
|
|
|
|
fn update_icon_name(&mut self) {
|
|
self.icon_name = self
|
|
.nm_state
|
|
.nm_state
|
|
.active_conns
|
|
.iter()
|
|
.fold(
|
|
"network-wired-disconnected-symbolic",
|
|
|icon_name, conn| match (icon_name, conn) {
|
|
(
|
|
"network-wired-disconnected-symbolic",
|
|
ActiveConnectionInfo::WiFi { strength, .. },
|
|
) => wifi_icon(*strength),
|
|
(_, ActiveConnectionInfo::Wired { .. })
|
|
if icon_name != "network-vpn-symbolic" =>
|
|
{
|
|
"network-wired-symbolic"
|
|
}
|
|
(_, ActiveConnectionInfo::Vpn { .. }) => "network-vpn-symbolic",
|
|
_ => icon_name,
|
|
},
|
|
)
|
|
.to_string();
|
|
}
|
|
|
|
fn update_togglers(&mut self, state: &NetworkManagerState) {
|
|
if self.nm_state.nm_state.wifi_enabled != state.wifi_enabled {
|
|
self.nm_state.nm_state.wifi_enabled = state.wifi_enabled;
|
|
}
|
|
|
|
if self.nm_state.nm_state.airplane_mode != state.airplane_mode {
|
|
self.nm_state.nm_state.airplane_mode = state.airplane_mode;
|
|
}
|
|
}
|
|
|
|
fn view_window_return<'a>(
|
|
&self,
|
|
mut content: cosmic::iced::widget::Column<'a, Message, cosmic::Theme>,
|
|
) -> Element<'a, Message> {
|
|
let Spacing {
|
|
space_xxs, space_s, ..
|
|
} = theme::active().cosmic().spacing;
|
|
|
|
content = content
|
|
.push(padded_control(divider::horizontal::default()).padding([space_xxs, space_s]))
|
|
.push(menu_button(text::body(fl!("settings"))).on_press(Message::OpenSettings));
|
|
|
|
self.core
|
|
.applet
|
|
.popup_container(content.padding([8, 0, 8, 0]))
|
|
.into()
|
|
}
|
|
|
|
fn connect_vpn(&mut self, uuid: Arc<str>) -> Task<cosmic::Action<Message>> {
|
|
cosmic::task::future(async move {
|
|
match NmrsManager::new().await {
|
|
Ok(nm) => match nm.connect_vpn_by_uuid(&uuid).await {
|
|
Ok(()) => Message::Refresh,
|
|
Err(e) => Message::Error(format!("activate VPN {uuid}: {e}")),
|
|
},
|
|
Err(e) => Message::Error(format!("nmrs init: {e}")),
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
/// Registers an `nmrs` secret agent on the system bus and yields its
|
|
/// requests + cancellations as [`NmAgentEvent`] for the applet to handle.
|
|
fn secret_agent_task(identifier: String) -> Task<NmAgentEvent> {
|
|
cosmic::Task::stream(async_fn_stream::fn_stream(move |emitter| async move {
|
|
let registration = SecretAgent::builder()
|
|
.with_identifier(identifier)
|
|
.with_capabilities(SecretAgentCapabilities::VPN_HINTS)
|
|
.register()
|
|
.await;
|
|
|
|
let (mut handle, mut requests) = match registration {
|
|
Ok(pair) => pair,
|
|
Err(e) => {
|
|
let _ = emitter.emit(NmAgentEvent::Failed(e.to_string())).await;
|
|
return;
|
|
}
|
|
};
|
|
|
|
loop {
|
|
tokio::select! {
|
|
req = requests.next() => match req {
|
|
Some(req) => {
|
|
let event = secret_request_to_event(req);
|
|
let _ = emitter.emit(event).await;
|
|
}
|
|
None => break,
|
|
},
|
|
cancel = handle.cancellations().next() => match cancel {
|
|
Some(_reason) => {
|
|
let _ = emitter.emit(NmAgentEvent::CancelGetSecrets).await;
|
|
}
|
|
None => break,
|
|
},
|
|
}
|
|
}
|
|
|
|
if let Err(e) = handle.unregister().await {
|
|
tracing::warn!("failed to unregister secret agent: {e}");
|
|
}
|
|
}))
|
|
}
|
|
|
|
fn secret_request_to_event(req: SecretRequest) -> NmAgentEvent {
|
|
let setting = match req.setting {
|
|
SecretSetting::WifiPsk { ssid } => AgentSetting::WifiPsk { ssid },
|
|
SecretSetting::WifiEap { .. } => AgentSetting::WifiEap,
|
|
SecretSetting::Vpn { .. } => AgentSetting::Vpn {
|
|
secret_keys: req.hints.clone(),
|
|
},
|
|
_ => AgentSetting::Other,
|
|
};
|
|
|
|
NmAgentEvent::RequestSecret {
|
|
connection_uuid: req.connection_uuid,
|
|
connection_id: req.connection_id,
|
|
setting,
|
|
responder: Arc::new(AsyncMutex::new(Some(req.responder))),
|
|
}
|
|
}
|
|
|
|
/// Reply with [`NoSecrets`](nmrs::agent::SecretResponder::no_secrets) to free
|
|
/// NetworkManager when the applet decides not to use the responder. Dropping
|
|
/// it would also auto-reply, but doing it explicitly keeps the log clean.
|
|
fn release_responder(responder: SecretResponderHandle) -> Task<cosmic::Action<Message>> {
|
|
cosmic::task::future(async move {
|
|
if let Some(r) = responder.lock().await.take() {
|
|
let _ = r.no_secrets().await;
|
|
}
|
|
Message::NoOp
|
|
})
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub(crate) enum Message {
|
|
TogglePopup,
|
|
CloseRequested(window::Id),
|
|
ToggleAirplaneMode(bool),
|
|
ToggleVisibleNetworks,
|
|
SelectWirelessAccessPoint(AccessPoint),
|
|
CancelNewConnection,
|
|
Token(TokenUpdate),
|
|
OpenSettings,
|
|
ResetFailedKnownSsid(String, HwAddress),
|
|
TogglePasswordVisibility,
|
|
FocusSecureInput,
|
|
NoOp,
|
|
#[allow(dead_code)] // required by `cosmic::applet` surface path; not always emitted
|
|
Surface(surface::Action),
|
|
ActivateVpn(Arc<str>), // UUID of VPN to activate
|
|
DeactivateVpn(Arc<str>), // UUID of VPN to deactivate
|
|
ToggleVpnList, // Show/hide available VPNs
|
|
/// An update from the secret agent
|
|
SecretAgent(NmAgentEvent),
|
|
/// Connect to a WiFi network access point.
|
|
Connect(network_manager::SSID, HwAddress),
|
|
/// Connect with a password
|
|
ConnectWithPassword,
|
|
KnownConnections(IndexMap<UUID, ConnectionSettings>),
|
|
/// Settings for known connections.
|
|
ConnectionSettings(BTreeMap<Box<str>, Box<str>>),
|
|
/// Disconnect from an access point.
|
|
Disconnect(network_manager::SSID, HwAddress),
|
|
/// An error occurred.
|
|
Error(String),
|
|
/// Identity update from the dialog
|
|
IdentityUpdate(String),
|
|
/// An update from the network manager daemon
|
|
NetworkManager(network_manager::Event),
|
|
/// Successfully connected to the system dbus.
|
|
NetworkManagerConnect(zbus::Connection),
|
|
/// Update the password from the dialog
|
|
PasswordUpdate(SecureString),
|
|
/// Update NetworkManagerState
|
|
UpdateState(NetworkManagerState),
|
|
/// Update the devices lists
|
|
UpdateDevices(Vec<network_manager::devices::DeviceInfo>),
|
|
/// Toggle WiFi access
|
|
WiFiEnable(bool),
|
|
/// Refresh state
|
|
Refresh,
|
|
ToggleVPNPasswordVisibility,
|
|
ConnectVPNWithPassword,
|
|
VPNPasswordUpdate(SecureString),
|
|
CancelVPNConnection,
|
|
/// Selects a device to display connections from
|
|
SelectDevice(Option<Arc<network_manager::devices::DeviceInfo>>),
|
|
}
|
|
|
|
fn connection_settings(conn: zbus::Connection) -> Task<Message> {
|
|
let settings = async move {
|
|
let settings = network_manager::dbus::settings::NetworkManagerSettings::new(&conn).await?;
|
|
|
|
_ = settings.load_connections(&[]).await;
|
|
|
|
let settings = settings
|
|
// Get a list of known connections.
|
|
.list_connections()
|
|
.await?
|
|
// Prepare for wrapping in a concurrent stream.
|
|
.into_iter()
|
|
.map(|conn| async move { conn })
|
|
// Create a concurrent stream for each connection.
|
|
.apply(futures::stream::FuturesOrdered::from_iter)
|
|
// Concurrently fetch settings for each connection.
|
|
.filter_map(|conn| async move {
|
|
conn.get_settings()
|
|
.await
|
|
.map(network_manager::Settings::new)
|
|
.ok()
|
|
})
|
|
// Reduce the settings list into a SSID->UUID map.
|
|
.fold(BTreeMap::new(), |mut set, settings| async move {
|
|
if let Some(ref wifi) = settings.wifi
|
|
&& let Some(ssid) = wifi
|
|
.ssid
|
|
.clone()
|
|
.and_then(|ssid| String::from_utf8(ssid).ok())
|
|
&& let Some(ref connection) = settings.connection
|
|
&& let Some(uuid) = connection.uuid.clone()
|
|
{
|
|
set.insert(ssid.into(), uuid.into());
|
|
return set;
|
|
}
|
|
|
|
set
|
|
})
|
|
.await;
|
|
|
|
Ok::<_, zbus::Error>(settings)
|
|
};
|
|
|
|
cosmic::task::future(async move {
|
|
settings
|
|
.await
|
|
.context("failed to get connection settings")
|
|
.map_or_else(
|
|
|why| Message::Error(why.to_string()),
|
|
Message::ConnectionSettings,
|
|
)
|
|
})
|
|
}
|
|
|
|
pub fn update_state(conn: zbus::Connection) -> Task<Message> {
|
|
cosmic::task::future(async move {
|
|
match NetworkManagerState::new(&conn).await {
|
|
Ok(state) => Message::UpdateState(state),
|
|
Err(why) => Message::Error(why.to_string()),
|
|
}
|
|
})
|
|
}
|
|
|
|
pub fn update_devices(conn: zbus::Connection) -> Task<Message> {
|
|
cosmic::task::future(async move {
|
|
let filter =
|
|
|device_type| matches!(device_type, network_manager::devices::DeviceType::Wifi);
|
|
match network_manager::devices::list(&conn, filter).await {
|
|
Ok(devices) => Message::UpdateDevices(devices),
|
|
Err(why) => Message::Error(why.to_string()),
|
|
}
|
|
})
|
|
}
|
|
|
|
impl CosmicNetworkApplet {
|
|
fn connect(&mut self, conn: zbus::Connection) -> Task<Message> {
|
|
if self.nm_task.is_none() {
|
|
let popup = self.popup;
|
|
let (canceller, task) = crate::utils::forward_event_loop(move |emitter| async move {
|
|
let (tx, mut rx) = futures::channel::mpsc::channel(1);
|
|
|
|
if popup.is_some() {
|
|
let watchers = std::pin::pin!(async move {
|
|
futures::join!(
|
|
network_manager::watch(conn.clone(), tx.clone()),
|
|
network_manager::active_conns::watch(conn.clone(), tx.clone(),),
|
|
network_manager::wireless_enabled::watch(conn.clone(), tx.clone()),
|
|
network_manager::watch_connections_changed(conn, tx,)
|
|
);
|
|
});
|
|
let forwarder = std::pin::pin!(async move {
|
|
while let Some(message) = rx.next().await {
|
|
_ = emitter.emit(Message::NetworkManager(message)).await;
|
|
}
|
|
});
|
|
|
|
futures::future::select(watchers, forwarder).await;
|
|
} else {
|
|
let watchers = std::pin::pin!(async move {
|
|
futures::join!(
|
|
network_manager::watch(conn.clone(), tx.clone()),
|
|
network_manager::active_conns::watch(conn.clone(), tx.clone(),),
|
|
network_manager::wireless_enabled::watch(conn.clone(), tx.clone()),
|
|
);
|
|
});
|
|
let forwarder = std::pin::pin!(async move {
|
|
while let Some(message) = rx.next().await {
|
|
_ = emitter.emit(Message::NetworkManager(message)).await;
|
|
}
|
|
});
|
|
|
|
futures::future::select(watchers, forwarder).await;
|
|
};
|
|
});
|
|
|
|
self.nm_task = Some(canceller);
|
|
return task.map(Message::from);
|
|
}
|
|
|
|
Task::none()
|
|
}
|
|
}
|
|
|
|
fn load_vpns(_conn: zbus::Connection) -> Task<crate::app::Message> {
|
|
cosmic::task::future(async move {
|
|
let nm = match NmrsManager::new().await {
|
|
Ok(nm) => nm,
|
|
Err(e) => return Message::Error(format!("nmrs init: {e}")),
|
|
};
|
|
|
|
let saved = match nm.list_saved_connections().await {
|
|
Ok(saved) => saved,
|
|
Err(e) => return Message::Error(format!("list saved connections: {e}")),
|
|
};
|
|
|
|
let mut map: IndexMap<UUID, ConnectionSettings> = IndexMap::new();
|
|
for c in saved {
|
|
// Skip in-memory-only NM connections — assumed connections that NM
|
|
// auto-generated from externally-managed interfaces (e.g. one
|
|
// brought up by wg-quick@wg0.service) report unsaved=true and
|
|
// evaporate on deactivate, leaving the applet's toggle dead.
|
|
if c.unsaved {
|
|
continue;
|
|
}
|
|
let uuid: UUID = Arc::from(c.uuid.as_str());
|
|
let entry = match c.summary {
|
|
SettingsSummary::WireGuard { .. } => ConnectionSettings::Wireguard { id: c.id },
|
|
SettingsSummary::Vpn { .. } => ConnectionSettings::Vpn { id: c.id },
|
|
_ => continue,
|
|
};
|
|
map.insert(uuid, entry);
|
|
}
|
|
|
|
Message::KnownConnections(map)
|
|
})
|
|
}
|
|
|
|
fn system_conn() -> Task<Message> {
|
|
cosmic::Task::future(async move {
|
|
zbus::Connection::system()
|
|
.await
|
|
.context("failed to create system dbus connection")
|
|
.map_or_else(
|
|
|why| Message::Error(why.to_string()),
|
|
Message::NetworkManagerConnect,
|
|
)
|
|
})
|
|
}
|
|
|
|
impl cosmic::Application for CosmicNetworkApplet {
|
|
type Message = Message;
|
|
type Executor = cosmic::SingleThreadExecutor;
|
|
type Flags = ();
|
|
const APP_ID: &'static str = config::APP_ID;
|
|
|
|
fn init(core: cosmic::app::Core, _flags: ()) -> (Self, app::Task<Message>) {
|
|
let applet = Self {
|
|
core,
|
|
icon_name: "network-wired-disconnected-symbolic".to_string(),
|
|
token_tx: None,
|
|
..Default::default()
|
|
};
|
|
|
|
(applet, system_conn().map(cosmic::Action::App))
|
|
}
|
|
|
|
fn core(&self) -> &cosmic::app::Core {
|
|
&self.core
|
|
}
|
|
|
|
fn core_mut(&mut self) -> &mut cosmic::app::Core {
|
|
&mut self.core
|
|
}
|
|
|
|
fn update(&mut self, message: Message) -> app::Task<Message> {
|
|
match message {
|
|
Message::TogglePopup => {
|
|
if let Some(p) = self.popup.take() {
|
|
self.show_visible_networks = false;
|
|
return destroy_popup(p);
|
|
} else {
|
|
let mut tasks = Vec::with_capacity(2);
|
|
if let Some(conn) = self.conn.clone() {
|
|
tasks.push(update_state(conn.clone()));
|
|
tasks.push(update_devices(conn.clone()));
|
|
tasks.push(load_vpns(conn));
|
|
let uuid = uuid::Uuid::new_v4().to_string().replace("-", "_");
|
|
|
|
let my_id = format!(
|
|
"com.system76.CosmicSettings.Applet._{uuid}.NetworkManager.SecretAgent",
|
|
);
|
|
tasks.push(secret_agent_task(my_id).map(Message::SecretAgent));
|
|
}
|
|
// TODO request update of state maybe
|
|
let new_id = window::Id::unique();
|
|
self.popup.replace(new_id);
|
|
|
|
let popup_settings = self.core.applet.get_popup_settings(
|
|
self.core.main_window_id().unwrap(),
|
|
new_id,
|
|
None,
|
|
None,
|
|
None,
|
|
);
|
|
|
|
tasks.push(system_conn());
|
|
tasks.push(get_popup(popup_settings));
|
|
|
|
return Task::batch(tasks).map(cosmic::Action::App);
|
|
}
|
|
}
|
|
Message::ToggleAirplaneMode(enabled) => {
|
|
self.toggle_wifi_ctr += 1;
|
|
self.nm_state.nm_state.airplane_mode = enabled;
|
|
return cosmic::task::future(async move {
|
|
match NmrsManager::new().await {
|
|
Ok(nm) => match nm.set_airplane_mode(enabled).await {
|
|
Ok(()) => Message::Refresh,
|
|
Err(e) => {
|
|
tracing::warn!("set_airplane_mode partial failure: {e}");
|
|
Message::Refresh
|
|
}
|
|
},
|
|
Err(e) => Message::Error(format!("nmrs init: {e}")),
|
|
}
|
|
})
|
|
.map(cosmic::Action::App);
|
|
}
|
|
Message::SelectWirelessAccessPoint(access_point) => {
|
|
let Some(tx) = self.nm_sender.as_ref() else {
|
|
return Task::none();
|
|
};
|
|
|
|
if matches!(access_point.network_type, NetworkType::Open) {
|
|
if let Err(err) =
|
|
tx.unbounded_send(network_manager::Request::SelectAccessPoint(
|
|
access_point.ssid.clone(),
|
|
access_point.network_type,
|
|
None,
|
|
self.active_device.as_ref().map(|d| d.interface.clone()),
|
|
))
|
|
{
|
|
if err.is_disconnected() {
|
|
return system_conn().map(cosmic::Action::App);
|
|
}
|
|
|
|
tracing::error!("{err:?}");
|
|
}
|
|
self.new_connection = Some(NewConnectionState::Waiting(access_point));
|
|
} else {
|
|
if self
|
|
.nm_state
|
|
.nm_state
|
|
.known_access_points
|
|
.contains(&access_point)
|
|
&& let Err(err) =
|
|
tx.unbounded_send(network_manager::Request::SelectAccessPoint(
|
|
access_point.ssid.clone(),
|
|
access_point.network_type,
|
|
None,
|
|
self.active_device.as_ref().map(|d| d.interface.clone()),
|
|
))
|
|
{
|
|
if err.is_disconnected() {
|
|
return system_conn().map(cosmic::Action::App);
|
|
}
|
|
|
|
tracing::error!("{err:?}");
|
|
}
|
|
self.new_connection = Some(NewConnectionState::EnterPassword {
|
|
access_point,
|
|
description: None,
|
|
identity: String::new(),
|
|
password: String::new().into(),
|
|
password_hidden: true,
|
|
});
|
|
return cosmic::task::message(cosmic::Action::App(Message::FocusSecureInput));
|
|
}
|
|
}
|
|
Message::ToggleVisibleNetworks => {
|
|
self.new_connection = None;
|
|
self.show_visible_networks = !self.show_visible_networks;
|
|
}
|
|
Message::TogglePasswordVisibility => {
|
|
if let Some(NewConnectionState::EnterPassword {
|
|
password_hidden, ..
|
|
}) = &mut self.new_connection
|
|
{
|
|
*password_hidden = !*password_hidden;
|
|
}
|
|
}
|
|
Message::FocusSecureInput => {
|
|
return text_input::focus(SECURE_INPUT_WIFI.clone())
|
|
.map(|_: ()| cosmic::Action::App(Message::NoOp));
|
|
}
|
|
Message::NoOp => {}
|
|
Message::CancelNewConnection => {
|
|
self.new_connection = None;
|
|
}
|
|
Message::CloseRequested(id) => {
|
|
if Some(id) == self.popup {
|
|
self.popup = None;
|
|
if let Some(cancel) = self.nm_task.take() {
|
|
_ = cancel.send(());
|
|
}
|
|
|
|
return system_conn().map(cosmic::Action::App);
|
|
}
|
|
}
|
|
Message::OpenSettings => {
|
|
let exec = "cosmic-settings network".to_string();
|
|
if let Some(tx) = self.token_tx.as_ref() {
|
|
let _ = tx.send(TokenRequest {
|
|
app_id: Self::APP_ID.to_string(),
|
|
exec,
|
|
});
|
|
}
|
|
}
|
|
Message::Token(u) => match u {
|
|
TokenUpdate::Init(tx) => {
|
|
self.token_tx = Some(tx);
|
|
}
|
|
TokenUpdate::Finished => {
|
|
self.token_tx = None;
|
|
}
|
|
TokenUpdate::ActivationToken { token, .. } => {
|
|
let mut cmd = std::process::Command::new("cosmic-settings");
|
|
cmd.arg("network");
|
|
if let Some(token) = token {
|
|
cmd.env("XDG_ACTIVATION_TOKEN", &token);
|
|
cmd.env("DESKTOP_STARTUP_ID", &token);
|
|
}
|
|
tokio::spawn(cosmic::process::spawn(cmd));
|
|
}
|
|
},
|
|
Message::SelectDevice(device) => {
|
|
self.active_device = device;
|
|
}
|
|
Message::ResetFailedKnownSsid(ssid, hw_address) => {
|
|
let ap = if let Some(pos) = self
|
|
.nm_state
|
|
.nm_state
|
|
.known_access_points
|
|
.iter()
|
|
.position(|ap| ap.ssid.as_ref() == ssid.as_str() && ap.hw_address == hw_address)
|
|
{
|
|
self.nm_state.nm_state.known_access_points.remove(pos)
|
|
} else if let Some((pos, ap)) = self
|
|
.nm_state
|
|
.nm_state
|
|
.active_conns
|
|
.iter()
|
|
.position(|conn| {
|
|
conn.name() == ssid && active_conn_hw_address(conn) == hw_address
|
|
})
|
|
.zip(
|
|
self.nm_state
|
|
.nm_state
|
|
.wireless_access_points
|
|
.iter()
|
|
.find(|ap| {
|
|
ap.ssid.as_ref() == ssid.as_str() && ap.hw_address == hw_address
|
|
}),
|
|
)
|
|
{
|
|
self.nm_state.nm_state.active_conns.remove(pos);
|
|
ap.clone()
|
|
} else {
|
|
tracing::warn!("Failed to find known access point with ssid: {}", ssid);
|
|
return Task::none();
|
|
};
|
|
self.show_visible_networks = true;
|
|
let ssid_for_task = ssid.clone();
|
|
let forget_task = cosmic::task::future(async move {
|
|
match NmrsManager::new().await {
|
|
Ok(nm) => match nm.forget(&ssid_for_task).await {
|
|
Ok(()) => Message::Refresh,
|
|
Err(e) => {
|
|
tracing::warn!("forget {ssid_for_task} failed: {e}");
|
|
Message::Refresh
|
|
}
|
|
},
|
|
Err(e) => Message::Error(format!("nmrs init: {e}")),
|
|
}
|
|
})
|
|
.map(cosmic::Action::App);
|
|
let reconnect_task = self.update(Message::SelectWirelessAccessPoint(ap));
|
|
return Task::batch(vec![forget_task, reconnect_task]);
|
|
}
|
|
Message::Surface(a) => {
|
|
return cosmic::task::message(cosmic::Action::Cosmic(
|
|
cosmic::app::Action::Surface(a),
|
|
));
|
|
}
|
|
Message::ActivateVpn(uuid) => {
|
|
return self.connect_vpn(uuid.clone());
|
|
}
|
|
Message::DeactivateVpn(uuid) => {
|
|
return cosmic::task::future(async move {
|
|
match NmrsManager::new().await {
|
|
Ok(nm) => match nm.disconnect_vpn_by_uuid(&uuid).await {
|
|
Ok(()) => Message::Refresh,
|
|
Err(e) => Message::Error(format!("disconnect VPN {uuid}: {e}")),
|
|
},
|
|
Err(e) => Message::Error(format!("nmrs init: {e}")),
|
|
}
|
|
})
|
|
.map(cosmic::Action::App);
|
|
}
|
|
Message::ToggleVpnList => {
|
|
self.show_available_vpns = !self.show_available_vpns;
|
|
}
|
|
Message::Connect(ssid, hw_address) => {
|
|
let mut network_type = NetworkType::Open;
|
|
let tx = if let Some(tx) = self.nm_sender.as_ref() {
|
|
if let Some(ap) = self
|
|
.nm_state
|
|
.nm_state
|
|
.known_access_points
|
|
.iter_mut()
|
|
.find(|c| c.ssid == ssid && c.hw_address == hw_address)
|
|
{
|
|
network_type = ap.network_type;
|
|
ap.working = true;
|
|
}
|
|
tx
|
|
} else {
|
|
return Task::none();
|
|
};
|
|
if let Err(err) = tx.unbounded_send(network_manager::Request::SelectAccessPoint(
|
|
ssid,
|
|
network_type,
|
|
None,
|
|
self.active_device.as_ref().map(|d| d.interface.clone()),
|
|
)) {
|
|
if err.is_disconnected() {
|
|
return system_conn().map(cosmic::Action::App);
|
|
}
|
|
|
|
tracing::error!("{err:?}");
|
|
}
|
|
}
|
|
Message::ConnectWithPassword => {
|
|
// save password
|
|
let Some(tx) = self.nm_sender.as_ref() else {
|
|
return Task::none();
|
|
};
|
|
|
|
if let Some(NewConnectionState::EnterPassword {
|
|
password,
|
|
access_point,
|
|
identity,
|
|
..
|
|
}) = self.new_connection.take()
|
|
{
|
|
let is_enterprise: bool = matches!(access_point.network_type, NetworkType::EAP);
|
|
|
|
if let Err(err) = tx.unbounded_send(network_manager::Request::Authenticate {
|
|
ssid: access_point.ssid.to_string(),
|
|
identity: is_enterprise.then(|| identity.clone()),
|
|
password,
|
|
secret_tx: None,
|
|
interface: self.active_device.as_ref().map(|d| d.interface.clone()),
|
|
}) {
|
|
if err.is_disconnected() {
|
|
return system_conn().map(cosmic::Action::App);
|
|
}
|
|
tracing::error!("Failed to authenticate with network manager");
|
|
}
|
|
self.new_connection
|
|
.replace(NewConnectionState::Waiting(access_point));
|
|
}
|
|
}
|
|
Message::ConnectionSettings(btree_map) => {
|
|
self.nm_state.ssid_to_uuid = btree_map;
|
|
}
|
|
Message::Disconnect(ssid, hw_address) => {
|
|
self.new_connection = None;
|
|
let tx = if let Some(tx) = self.nm_sender.as_ref() {
|
|
if let Some(ActiveConnectionInfo::WiFi { state, .. }) =
|
|
self.nm_state.nm_state.active_conns.iter_mut().find(|c| {
|
|
let c_hw_address = match c {
|
|
ActiveConnectionInfo::Wired { hw_address, .. }
|
|
| ActiveConnectionInfo::WiFi { hw_address, .. } => {
|
|
HwAddress::from_str(hw_address).unwrap()
|
|
}
|
|
ActiveConnectionInfo::Vpn { .. } => HwAddress::default(),
|
|
};
|
|
c.name().as_str() == ssid.as_ref() && c_hw_address == hw_address
|
|
})
|
|
{
|
|
*state = ActiveConnectionState::Deactivating;
|
|
}
|
|
tx
|
|
} else {
|
|
return Task::none();
|
|
};
|
|
if let Err(err) = tx.unbounded_send(network_manager::Request::Disconnect(ssid)) {
|
|
if err.is_disconnected() {
|
|
return system_conn().map(cosmic::Action::App);
|
|
}
|
|
|
|
tracing::error!("{err:?}");
|
|
}
|
|
}
|
|
Message::Error(error) => {
|
|
tracing::error!("error: {error:?}")
|
|
}
|
|
Message::IdentityUpdate(new_identity) => {
|
|
if let Some(NewConnectionState::EnterPassword { identity, .. }) =
|
|
&mut self.new_connection
|
|
{
|
|
*identity = new_identity;
|
|
}
|
|
}
|
|
Message::NetworkManager(event) => match event {
|
|
network_manager::Event::Init {
|
|
conn,
|
|
sender,
|
|
state,
|
|
} => {
|
|
self.nm_sender = Some(sender);
|
|
self.update_nm_state(state);
|
|
self.conn = Some(conn);
|
|
}
|
|
network_manager::Event::WiFiEnabled(_)
|
|
| network_manager::Event::WirelessAccessPoints
|
|
| network_manager::Event::ActiveConns => {
|
|
if let Some(conn) = self.conn.clone() {
|
|
return Task::future(async move {
|
|
let conn = conn.clone();
|
|
NetworkManagerState::new(&conn).await
|
|
})
|
|
.map(|res| match res {
|
|
Ok(s) => Message::UpdateState(s),
|
|
Err(err) => Message::Error(err.to_string()),
|
|
})
|
|
.map(cosmic::Action::App);
|
|
}
|
|
}
|
|
network_manager::Event::RequestResponse {
|
|
mut state,
|
|
success,
|
|
req,
|
|
} => {
|
|
if let network_manager::Request::SelectAccessPoint(
|
|
ssid,
|
|
_hw_address,
|
|
_network_type,
|
|
_secret_tx,
|
|
) = &req
|
|
{
|
|
let conn_match = self
|
|
.new_connection
|
|
.as_ref()
|
|
.is_some_and(|c| c.ssid() == ssid.as_ref() );
|
|
|
|
if conn_match && success {
|
|
if let Some(ActiveConnectionInfo::WiFi { state, .. }) = state
|
|
.active_conns
|
|
.iter_mut()
|
|
.find(|ap| ap.name().as_str() == ssid.as_ref())
|
|
{
|
|
*state = ActiveConnectionState::Activated;
|
|
}
|
|
self.failed_known_ssids.remove(ssid);
|
|
self.new_connection = None;
|
|
self.show_visible_networks = false;
|
|
} else if !matches!(
|
|
&self.new_connection,
|
|
Some(NewConnectionState::EnterPassword { .. })
|
|
) && !success {
|
|
self.failed_known_ssids.insert(ssid.clone());
|
|
}
|
|
} else if let network_manager::Request::Authenticate {
|
|
ssid,
|
|
identity: _,
|
|
password: _,
|
|
secret_tx: _,
|
|
interface: _,
|
|
} = &req
|
|
{
|
|
if let Some(NewConnectionState::Waiting(access_point)) =
|
|
self.new_connection.as_ref()
|
|
{
|
|
if !success
|
|
&& ssid.as_str() == access_point.ssid.as_ref()
|
|
{
|
|
self.new_connection =
|
|
Some(NewConnectionState::Failure(access_point.clone()));
|
|
} else {
|
|
self.show_visible_networks = false;
|
|
}
|
|
} else if let Some(NewConnectionState::EnterPassword { access_point, .. }) =
|
|
self.new_connection.as_ref()
|
|
&& success
|
|
&& ssid.as_str() == access_point.ssid.as_ref()
|
|
{
|
|
self.new_connection = None;
|
|
self.show_visible_networks = false;
|
|
}
|
|
} else if self
|
|
.new_connection
|
|
.as_ref()
|
|
.map(NewConnectionState::ssid).is_some_and(|ssid| {
|
|
state.active_conns.iter().any(|c|
|
|
matches!(c, ActiveConnectionInfo::WiFi { name, state: ActiveConnectionState::Activated, .. } if ssid == name)
|
|
)
|
|
}) {
|
|
self.new_connection = None;
|
|
self.show_visible_networks = false;
|
|
}
|
|
|
|
if !matches!(req, network_manager::Request::Reload)
|
|
&& matches!(state.connectivity, NmConnectivityState::Portal)
|
|
{
|
|
let mut browser = std::process::Command::new("xdg-open");
|
|
browser.arg("http://204.pop-os.org/");
|
|
|
|
tokio::spawn(cosmic::process::spawn(browser));
|
|
}
|
|
|
|
self.update_nm_state(state);
|
|
}
|
|
|
|
cosmic_settings_network_manager_subscription::Event::Devices => {
|
|
if let Some(conn) = self.conn.clone() {
|
|
return update_devices(conn).map(cosmic::Action::App);
|
|
}
|
|
}
|
|
cosmic_settings_network_manager_subscription::Event::WiFiCredentials {
|
|
ssid: _,
|
|
password: _,
|
|
security_type: _,
|
|
} => {}
|
|
},
|
|
Message::NetworkManagerConnect(connection) => {
|
|
return cosmic::task::batch(vec![
|
|
self.connect(connection.clone()),
|
|
connection_settings(connection),
|
|
]);
|
|
}
|
|
Message::PasswordUpdate(entered_pw) => {
|
|
if let Some(NewConnectionState::EnterPassword { password, .. }) =
|
|
&mut self.new_connection
|
|
{
|
|
*password = entered_pw;
|
|
}
|
|
}
|
|
Message::UpdateState(network_manager_state) => {
|
|
self.update_nm_state(network_manager_state);
|
|
}
|
|
Message::UpdateDevices(device_infos) => {
|
|
self.nm_state.devices = device_infos.into_iter().map(Arc::new).collect();
|
|
}
|
|
Message::WiFiEnable(enable) => {
|
|
self.nm_state.nm_state.wifi_enabled = enable;
|
|
return cosmic::task::future(async move {
|
|
match NmrsManager::new().await {
|
|
Ok(nm) => match nm.set_wireless_enabled(enable).await {
|
|
Ok(()) => Message::Refresh,
|
|
Err(e) => Message::Error(format!("set_wireless_enabled: {e}")),
|
|
},
|
|
Err(e) => Message::Error(format!("nmrs init: {e}")),
|
|
}
|
|
})
|
|
.map(cosmic::Action::App);
|
|
}
|
|
Message::SecretAgent(agent_event) => match agent_event {
|
|
NmAgentEvent::RequestSecret {
|
|
connection_uuid,
|
|
connection_id,
|
|
setting,
|
|
responder,
|
|
} => {
|
|
let description = (!connection_id.is_empty()).then_some(connection_id);
|
|
let known_vpn = self
|
|
.nm_state
|
|
.known_vpns
|
|
.contains_key(connection_uuid.as_str());
|
|
|
|
let mut consumed = false;
|
|
|
|
if let Some(state) = self.new_connection.as_mut() {
|
|
match state {
|
|
NewConnectionState::EnterPassword { access_point, .. }
|
|
| NewConnectionState::Waiting(access_point)
|
|
| NewConnectionState::Failure(access_point) => {
|
|
let matches_ssid = matches!(
|
|
&setting,
|
|
AgentSetting::WifiPsk { ssid }
|
|
if ssid == access_point.ssid.as_ref()
|
|
);
|
|
let matches_uuid = self
|
|
.nm_state
|
|
.ssid_to_uuid
|
|
.get(access_point.ssid.as_ref())
|
|
.is_some_and(|ap_uuid| {
|
|
ap_uuid.as_ref() == connection_uuid.as_str()
|
|
});
|
|
|
|
if matches_ssid || matches_uuid {
|
|
*state = NewConnectionState::EnterPassword {
|
|
access_point: access_point.clone(),
|
|
description: description.clone(),
|
|
identity: String::new(),
|
|
password: String::new().into(),
|
|
password_hidden: true,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
} else if known_vpn {
|
|
let secret_keys = match &setting {
|
|
AgentSetting::Vpn { secret_keys } => secret_keys.clone(),
|
|
_ => Vec::new(),
|
|
};
|
|
self.nm_state.requested_vpn = Some(RequestedVpn {
|
|
uuid: connection_uuid.into(),
|
|
description,
|
|
password: SecureString::from(String::new()),
|
|
password_hidden: true,
|
|
responder: responder.clone(),
|
|
secret_keys,
|
|
});
|
|
consumed = true;
|
|
}
|
|
|
|
// The applet's Wi-Fi flow re-issues the password through
|
|
// `Authenticate` rather than the agent. Free NM with
|
|
// `NoSecrets` so it doesn't sit on a stalled GetSecrets call.
|
|
if !consumed {
|
|
return release_responder(responder);
|
|
}
|
|
}
|
|
NmAgentEvent::CancelGetSecrets => {
|
|
self.new_connection = None;
|
|
self.nm_state.requested_vpn = None;
|
|
}
|
|
NmAgentEvent::Failed(error) => {
|
|
tracing::error!("Error from secret agent: {error}");
|
|
}
|
|
},
|
|
Message::KnownConnections(index_map) => {
|
|
self.nm_state.known_vpns = index_map;
|
|
}
|
|
Message::Refresh => {
|
|
if let Some(conn) = self.conn.clone() {
|
|
return Task::batch(vec![
|
|
update_state(conn.clone()),
|
|
update_devices(conn.clone()),
|
|
load_vpns(conn),
|
|
])
|
|
.map(cosmic::Action::App);
|
|
}
|
|
}
|
|
Message::ToggleVPNPasswordVisibility => {
|
|
if let Some(requested_vpn) = self.nm_state.requested_vpn.as_mut() {
|
|
requested_vpn.password_hidden = !requested_vpn.password_hidden;
|
|
}
|
|
}
|
|
Message::ConnectVPNWithPassword => {
|
|
if let Some(RequestedVpn {
|
|
password,
|
|
responder,
|
|
secret_keys,
|
|
..
|
|
}) = self.nm_state.requested_vpn.take()
|
|
{
|
|
return Task::future(async move {
|
|
let Some(responder) = responder.lock().await.take() else {
|
|
return Message::Refresh;
|
|
};
|
|
|
|
let mut secrets: HashMap<String, String> = HashMap::new();
|
|
let value = password.unsecure().to_owned();
|
|
if secret_keys.is_empty() {
|
|
secrets.insert("password".to_owned(), value);
|
|
} else {
|
|
for key in secret_keys {
|
|
secrets.insert(key, value.clone());
|
|
}
|
|
}
|
|
|
|
if let Err(e) = responder.vpn_secrets(secrets).await {
|
|
tracing::error!("vpn secret reply failed: {e}");
|
|
}
|
|
Message::Refresh
|
|
})
|
|
.map(cosmic::Action::App);
|
|
}
|
|
}
|
|
Message::VPNPasswordUpdate(secure_string) => {
|
|
if let Some(requested_vpn) = self.nm_state.requested_vpn.as_mut() {
|
|
requested_vpn.password = secure_string;
|
|
}
|
|
}
|
|
Message::CancelVPNConnection => {
|
|
if let Some(req) = self.nm_state.requested_vpn.take() {
|
|
return Task::future(async move {
|
|
if let Some(responder) = req.responder.lock().await.take() {
|
|
let _ = responder.cancel().await;
|
|
}
|
|
Message::NoOp
|
|
})
|
|
.map(cosmic::Action::App);
|
|
}
|
|
}
|
|
}
|
|
Task::none()
|
|
}
|
|
|
|
fn view(&self) -> Element<'_, Message> {
|
|
self.core
|
|
.applet
|
|
.icon_button(&self.icon_name)
|
|
.on_press_down(Message::TogglePopup)
|
|
.into()
|
|
}
|
|
|
|
fn view_window(&self, _id: window::Id) -> Element<'_, Message> {
|
|
let Spacing {
|
|
space_xxs, space_s, ..
|
|
} = theme::active().cosmic().spacing;
|
|
|
|
let mut vpn_ethernet_col: cosmic::iced::widget::Column<Message, cosmic::Theme> =
|
|
cosmic::widget::column::with_capacity(1);
|
|
let mut known_wifi = Vec::new();
|
|
for conn in &self.nm_state.nm_state.active_conns {
|
|
match conn {
|
|
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
|
|
if self.active_device.as_ref().is_some_and(|d| {
|
|
d.active_connection.as_ref().is_none_or(|a| a.0.id != *name)
|
|
}) {
|
|
continue;
|
|
}
|
|
let mut ipv4 = Vec::with_capacity(ip_addresses.len() + 1);
|
|
ipv4.push(text::body(name).into());
|
|
for addr in ip_addresses {
|
|
ipv4.push(text::caption(format!("{}: {}", fl!("ipv4"), addr)).into());
|
|
}
|
|
vpn_ethernet_col = vpn_ethernet_col.push(
|
|
column::with_capacity::<Message, cosmic::Theme, _>(2)
|
|
.push(
|
|
row::with_children([
|
|
Element::from(
|
|
icon::icon(
|
|
icon::from_name(self.icon_name.clone())
|
|
.symbolic(true)
|
|
.into(),
|
|
)
|
|
.size(40),
|
|
),
|
|
column::with_children(ipv4).into(),
|
|
text::body(fl!("connected"))
|
|
.width(Length::Fill)
|
|
.align_x(Alignment::End)
|
|
.into(),
|
|
])
|
|
.align_y(Alignment::Center)
|
|
.spacing(8)
|
|
.padding(menu_control_padding()),
|
|
)
|
|
.push(
|
|
padded_control(divider::horizontal::default())
|
|
.padding([space_xxs, space_s]),
|
|
),
|
|
);
|
|
}
|
|
ActiveConnectionInfo::Wired {
|
|
name,
|
|
hw_address: _,
|
|
speed,
|
|
ip_addresses,
|
|
} => {
|
|
if self.active_device.as_ref().is_some_and(|d| {
|
|
d.active_connection.as_ref().is_none_or(|a| a.0.id != *name)
|
|
}) {
|
|
continue;
|
|
}
|
|
let mut ipv4 = Vec::with_capacity(ip_addresses.len() + 1);
|
|
ipv4.push(text::body(name).into());
|
|
for addr in ip_addresses {
|
|
ipv4.push(text(format!("{}: {}", fl!("ipv4"), addr)).size(12).into());
|
|
}
|
|
|
|
let mut right_column = vec![text::body(fl!("connected")).into()];
|
|
|
|
// Only show speed if it's greater than 0
|
|
if *speed > 0 {
|
|
let speed_text = if *speed >= 1_000_000 {
|
|
let tbps = *speed as f64 / 1_000_000.0;
|
|
if tbps.fract() == 0.0 {
|
|
format!("{} {}", tbps as u32, fl!("terabits-per-second"))
|
|
} else {
|
|
format!("{:.1} {}", tbps, fl!("terabits-per-second"))
|
|
}
|
|
} else if *speed >= 1_000 {
|
|
let gbps = *speed as f64 / 1_000.0;
|
|
if gbps.fract() == 0.0 {
|
|
format!("{} {}", gbps as u32, fl!("gigabits-per-second"))
|
|
} else {
|
|
format!("{:.1} {}", gbps, fl!("gigabits-per-second"))
|
|
}
|
|
} else {
|
|
format!("{speed} {}", fl!("megabits-per-second"))
|
|
};
|
|
right_column.push(text(speed_text).size(12).into());
|
|
}
|
|
|
|
vpn_ethernet_col = vpn_ethernet_col
|
|
.push(
|
|
column::with_capacity(2).push(
|
|
row::with_children([
|
|
Element::from(
|
|
icon::icon(
|
|
icon::from_name(self.icon_name.clone())
|
|
.symbolic(true)
|
|
.into(),
|
|
)
|
|
.size(40),
|
|
),
|
|
column::with_children(ipv4).into(),
|
|
column::with_children(right_column)
|
|
.width(Length::Fill)
|
|
.align_x(Alignment::End)
|
|
.into(),
|
|
])
|
|
.align_y(Alignment::Center)
|
|
.spacing(8)
|
|
.padding(menu_control_padding()),
|
|
),
|
|
)
|
|
.push(
|
|
padded_control(divider::horizontal::default())
|
|
.padding([space_xxs, space_s]),
|
|
);
|
|
}
|
|
ActiveConnectionInfo::WiFi {
|
|
name,
|
|
ip_addresses,
|
|
state,
|
|
strength,
|
|
hw_address,
|
|
} => {
|
|
if self.active_device.as_ref().is_some_and(|d| {
|
|
d.active_connection.as_ref().is_none_or(|a| a.0.id != *name)
|
|
}) {
|
|
continue;
|
|
}
|
|
let mut ipv4 = Vec::with_capacity(ip_addresses.len());
|
|
for addr in ip_addresses {
|
|
ipv4.push(text(format!("{}: {}", fl!("ipv4"), addr)).size(12).into());
|
|
}
|
|
let mut btn_content = vec![
|
|
icon::from_name(wifi_icon(*strength))
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
column::with_children([
|
|
text::body(name).into(),
|
|
column::with_children(ipv4).into(),
|
|
])
|
|
.width(Length::Fill)
|
|
.into(),
|
|
];
|
|
match state {
|
|
ActiveConnectionState::Activating | ActiveConnectionState::Deactivating => {
|
|
btn_content.push(
|
|
icon::from_name("process-working-symbolic")
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
);
|
|
}
|
|
ActiveConnectionState::Activated => btn_content.push(
|
|
text::body(fl!("connected"))
|
|
.align_x(Alignment::End)
|
|
.align_y(Alignment::Center)
|
|
.into(),
|
|
),
|
|
_ => {}
|
|
}
|
|
if self.failed_known_ssids.contains(name.as_str()) {
|
|
btn_content.push(
|
|
cosmic::widget::button::icon(
|
|
from_name("view-refresh-symbolic").size(16),
|
|
)
|
|
.icon_size(16)
|
|
.on_press(Message::ResetFailedKnownSsid(
|
|
name.clone(),
|
|
HwAddress::from_str(hw_address).unwrap(),
|
|
))
|
|
.into(),
|
|
);
|
|
}
|
|
|
|
known_wifi.push(Element::from(
|
|
column::with_children([Element::from(
|
|
menu_button(
|
|
row::with_children(btn_content)
|
|
.align_y(Alignment::Center)
|
|
.spacing(8),
|
|
)
|
|
.on_press(Message::Disconnect(
|
|
Arc::from(name.as_str()),
|
|
HwAddress::from_str(hw_address).unwrap(),
|
|
)),
|
|
)])
|
|
.align_x(Alignment::Center),
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut content = cosmic::widget::column::with_capacity(known_wifi.len() + 1);
|
|
|
|
if let Some(active_device) = self.active_device.as_ref() {
|
|
let menu_row = row::with_children::<'_, Message, cosmic::Theme, _>([
|
|
Element::<'_, Message>::from(
|
|
container(
|
|
icon::from_name("go-previous-symbolic")
|
|
.size(16)
|
|
.symbolic(true),
|
|
)
|
|
.align_x(Alignment::Start)
|
|
.align_y(Alignment::Center)
|
|
.width(Length::Fixed(24.0))
|
|
.height(Length::Fixed(24.0)),
|
|
),
|
|
text::body(&active_device.interface)
|
|
.width(Length::Fill)
|
|
.height(Length::Fixed(24.0))
|
|
.align_y(Alignment::Center)
|
|
.into(),
|
|
]);
|
|
content = content
|
|
.push(vpn_ethernet_col)
|
|
.push(menu_button(menu_row).on_press(Message::SelectDevice(None)));
|
|
} else {
|
|
let menu_column = column::with_children([
|
|
Element::from(vpn_ethernet_col),
|
|
padded_control(
|
|
toggler(self.nm_state.nm_state.airplane_mode)
|
|
.label(fl!("airplane-mode"))
|
|
.on_toggle(Message::ToggleAirplaneMode)
|
|
.text_size(14)
|
|
.width(Length::Fill),
|
|
)
|
|
.into(),
|
|
padded_control(divider::horizontal::default())
|
|
.padding([space_xxs, space_s])
|
|
.into(),
|
|
])
|
|
.align_x(Alignment::Center);
|
|
content = content
|
|
.push(Element::from(menu_column))
|
|
.push(padded_control(
|
|
toggler(self.nm_state.nm_state.wifi_enabled)
|
|
.label(fl!("wifi"))
|
|
.on_toggle(Message::WiFiEnable)
|
|
.text_size(14)
|
|
.width(Length::Fill),
|
|
))
|
|
.align_x(Alignment::Center);
|
|
}
|
|
if self.nm_state.nm_state.airplane_mode {
|
|
content = content.push(
|
|
column::with_children([
|
|
Element::from(
|
|
padded_control(divider::horizontal::default())
|
|
.padding([space_xxs, space_s]),
|
|
),
|
|
icon::from_name("airplane-mode-symbolic")
|
|
.size(48)
|
|
.symbolic(true)
|
|
.into(),
|
|
text::body(fl!("airplane-mode-on")).into(),
|
|
text(fl!("turn-off-airplane-mode")).size(12).into(),
|
|
])
|
|
.spacing(8)
|
|
.padding([0, 0, 8, 0])
|
|
.align_x(Alignment::Center)
|
|
.width(Length::Fill),
|
|
);
|
|
|
|
// Show VPN connections even in airplane mode
|
|
if !self.nm_state.known_vpns.is_empty() {
|
|
content = content.push(vpn_section(
|
|
&self.nm_state,
|
|
self.show_available_vpns,
|
|
space_xxs,
|
|
space_s,
|
|
));
|
|
}
|
|
|
|
return self.view_window_return(content);
|
|
}
|
|
|
|
if !self.nm_state.nm_state.wifi_enabled && !self.nm_state.known_vpns.is_empty() {
|
|
// Add VPN connections section when WiFi is disabled
|
|
content = content.push(vpn_section(
|
|
&self.nm_state,
|
|
self.show_available_vpns,
|
|
space_xxs,
|
|
space_s,
|
|
));
|
|
|
|
return self.view_window_return(content);
|
|
}
|
|
|
|
content = content
|
|
.push(padded_control(divider::horizontal::default()).padding([space_xxs, space_s]));
|
|
|
|
// TODO sorting?
|
|
let wireless_hw_devices = self
|
|
.nm_state
|
|
.devices
|
|
.iter()
|
|
.filter(|d| matches!(d.device_type, network_manager::devices::DeviceType::Wifi))
|
|
.collect::<Vec<_>>();
|
|
|
|
if wireless_hw_devices.len() > 1 && self.active_device.is_none() {
|
|
for interface in wireless_hw_devices {
|
|
let display_name = interface.interface.to_string();
|
|
|
|
let is_connected = interface.active_connection.is_some();
|
|
let mut btn_content = vec![
|
|
column::with_children([
|
|
text::body(display_name).into(),
|
|
column::with_children([text("Adapter").size(10).into()]).into(),
|
|
])
|
|
.width(Length::Fill)
|
|
.into(),
|
|
];
|
|
if is_connected {
|
|
btn_content.push(
|
|
text::body(fl!("connected"))
|
|
.width(Length::Fill)
|
|
.align_x(Alignment::End)
|
|
.into(),
|
|
);
|
|
}
|
|
btn_content.push(
|
|
icon::from_name("go-next-symbolic")
|
|
.size(16)
|
|
.symbolic(true)
|
|
.into(),
|
|
);
|
|
content = content.push(Element::from(
|
|
menu_button(
|
|
row::with_children(btn_content)
|
|
.align_y(Alignment::Center)
|
|
.spacing(8),
|
|
)
|
|
.on_press(Message::SelectDevice(Some(interface.clone()))),
|
|
));
|
|
}
|
|
|
|
return self.view_window_return(content);
|
|
}
|
|
|
|
for known in &self.nm_state.nm_state.known_access_points {
|
|
if let Some(active_device) = self.active_device.as_ref()
|
|
&& active_device
|
|
.known_connections
|
|
.iter()
|
|
.all(|c| c.id != *known.ssid)
|
|
{
|
|
continue;
|
|
}
|
|
let mut btn_content = Vec::with_capacity(2);
|
|
let ssid = text::body(known.ssid.as_ref()).width(Length::Fill);
|
|
if known.working {
|
|
btn_content.push(
|
|
icon::from_name("network-wireless-acquiring-symbolic")
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
);
|
|
btn_content.push(ssid.into());
|
|
btn_content.push(
|
|
icon::from_name("process-working-symbolic")
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
);
|
|
} else if matches!(known.state, DeviceState::Unavailable) {
|
|
btn_content.push(
|
|
icon::from_name("network-wireless-disconnected-symbolic")
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
);
|
|
btn_content.push(ssid.into());
|
|
} else {
|
|
btn_content.push(
|
|
icon::from_name(wifi_icon(known.strength))
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
);
|
|
btn_content.push(ssid.into());
|
|
}
|
|
|
|
if self.failed_known_ssids.contains(known.ssid.as_ref()) {
|
|
btn_content.push(
|
|
cosmic::widget::button::icon(from_name("view-refresh-symbolic").size(16))
|
|
.icon_size(16)
|
|
.on_press(Message::ResetFailedKnownSsid(
|
|
known.ssid.to_string(),
|
|
known.hw_address,
|
|
))
|
|
.into(),
|
|
);
|
|
}
|
|
|
|
let mut btn = menu_button(
|
|
row::with_children(btn_content)
|
|
.align_y(Alignment::Center)
|
|
.spacing(8),
|
|
);
|
|
btn = match known.state {
|
|
DeviceState::Failed
|
|
| DeviceState::Unknown
|
|
| DeviceState::Unmanaged
|
|
| DeviceState::Disconnected
|
|
| DeviceState::NeedAuth => {
|
|
btn.on_press(Message::Connect(known.ssid.clone(), known.hw_address))
|
|
}
|
|
DeviceState::Activated => {
|
|
btn.on_press(Message::Disconnect(known.ssid.clone(), known.hw_address))
|
|
}
|
|
_ => btn,
|
|
};
|
|
known_wifi.push(Element::from(
|
|
row::with_capacity(1).push(btn).align_y(Alignment::Center),
|
|
));
|
|
}
|
|
let has_known_wifi = !known_wifi.is_empty();
|
|
content = content.push(column::with_children(known_wifi));
|
|
if has_known_wifi {
|
|
content = content
|
|
.push(padded_control(divider::horizontal::default()).padding([space_xxs, space_s]));
|
|
}
|
|
|
|
let dropdown_icon = if self.show_visible_networks {
|
|
"go-up-symbolic"
|
|
} else {
|
|
"go-down-symbolic"
|
|
};
|
|
let available_connections_btn = menu_button(row::with_children([
|
|
Element::from(
|
|
text::body(fl!("visible-wireless-networks"))
|
|
.width(Length::Fill)
|
|
.height(Length::Fixed(24.0))
|
|
.align_y(Alignment::Center),
|
|
),
|
|
container(icon::from_name(dropdown_icon).size(16).symbolic(true))
|
|
.center(Length::Fixed(24.0))
|
|
.into(),
|
|
]))
|
|
.on_press(Message::ToggleVisibleNetworks);
|
|
content = content.push(available_connections_btn);
|
|
|
|
if !self.show_visible_networks {
|
|
if !self.nm_state.known_vpns.is_empty() {
|
|
content = content.push(vpn_section(
|
|
&self.nm_state,
|
|
self.show_available_vpns,
|
|
space_xxs,
|
|
space_s,
|
|
));
|
|
}
|
|
return self.view_window_return(content);
|
|
}
|
|
|
|
if let Some(new_conn_state) = self.new_connection.as_ref() {
|
|
match new_conn_state {
|
|
NewConnectionState::EnterPassword {
|
|
access_point,
|
|
description,
|
|
identity,
|
|
password,
|
|
password_hidden,
|
|
} => {
|
|
let id = padded_control(
|
|
row::with_children([
|
|
Element::from(
|
|
icon::from_name("network-wireless-acquiring-symbolic")
|
|
.size(24)
|
|
.symbolic(true),
|
|
),
|
|
text::body(access_point.ssid.as_ref()).into(),
|
|
])
|
|
.align_y(Alignment::Center)
|
|
.spacing(12),
|
|
);
|
|
content = content.push(id);
|
|
|
|
let is_enterprise = matches!(access_point.network_type, NetworkType::EAP);
|
|
let enter_password_col = cosmic::widget::column::with_capacity(4)
|
|
.push_maybe(is_enterprise.then(|| text::body(fl!("identity"))))
|
|
.push_maybe(is_enterprise.then(|| {
|
|
text_input::text_input("", identity).on_input(Message::IdentityUpdate)
|
|
}))
|
|
.push(text::body(fl!("enter-password")))
|
|
.push_maybe(description.as_ref().map(|d| text::body(d.clone())))
|
|
.push(
|
|
text_input::secure_input(
|
|
"",
|
|
password.unsecure(),
|
|
Some(Message::TogglePasswordVisibility),
|
|
*password_hidden,
|
|
)
|
|
.id(SECURE_INPUT_WIFI.clone())
|
|
.on_input(|s| Message::PasswordUpdate(SecureString::from(s)))
|
|
.on_paste(|s| Message::PasswordUpdate(SecureString::from(s)))
|
|
.on_submit(|_| Message::ConnectWithPassword),
|
|
)
|
|
.push_maybe(
|
|
access_point.wps_push.then(|| {
|
|
container(text::body(fl!("router-wps-button"))).padding(8)
|
|
}),
|
|
)
|
|
.push(
|
|
row::with_children([
|
|
Element::from(
|
|
button::standard(fl!("cancel"))
|
|
.on_press(Message::CancelNewConnection),
|
|
),
|
|
Element::from(
|
|
button::suggested(fl!("connect"))
|
|
.on_press(Message::ConnectWithPassword),
|
|
),
|
|
])
|
|
.spacing(24),
|
|
);
|
|
let col =
|
|
padded_control(enter_password_col.spacing(8).align_x(Alignment::Center))
|
|
.align_x(Alignment::Center);
|
|
content = content.push(col);
|
|
}
|
|
NewConnectionState::Waiting(access_point) => {
|
|
let id = row::with_children([
|
|
Element::from(
|
|
icon::from_name("network-wireless-acquiring-symbolic")
|
|
.size(24)
|
|
.symbolic(true),
|
|
),
|
|
text::body(access_point.ssid.as_ref()).into(),
|
|
])
|
|
.align_y(Alignment::Center)
|
|
.width(Length::Fill)
|
|
.spacing(12);
|
|
let connecting = padded_control(
|
|
row::with_children([
|
|
Element::from(id),
|
|
icon::from_name("process-working-symbolic")
|
|
.size(24)
|
|
.symbolic(true)
|
|
.into(),
|
|
])
|
|
.spacing(8),
|
|
);
|
|
content = content.push(connecting);
|
|
}
|
|
NewConnectionState::Failure(access_point) => {
|
|
let id = padded_control(
|
|
row::with_children([
|
|
Element::from(
|
|
icon::from_name("network-wireless-error-symbolic")
|
|
.size(24)
|
|
.symbolic(true),
|
|
),
|
|
text::body(access_point.ssid.as_ref()).into(),
|
|
])
|
|
.align_y(Alignment::Center)
|
|
.spacing(12),
|
|
)
|
|
.align_x(Alignment::Center);
|
|
content = content.push(id);
|
|
let col = padded_control(
|
|
column::with_children([
|
|
Element::from(text(fl!("unable-to-connect"))),
|
|
text(fl!("check-wifi-connection")).into(),
|
|
row::with_children([
|
|
Element::from(
|
|
button::standard(fl!("cancel"))
|
|
.on_press(Message::CancelNewConnection),
|
|
),
|
|
button::suggested(fl!("connect"))
|
|
.on_press(Message::SelectWirelessAccessPoint(
|
|
access_point.clone(),
|
|
))
|
|
.into(),
|
|
])
|
|
.spacing(24)
|
|
.into(),
|
|
])
|
|
.spacing(16)
|
|
.align_x(Alignment::Center),
|
|
)
|
|
.align_x(Alignment::Center);
|
|
content = content.push(col);
|
|
}
|
|
}
|
|
} else {
|
|
let list_col = self
|
|
.nm_state
|
|
.nm_state
|
|
.wireless_access_points
|
|
.iter()
|
|
.filter(|ap| {
|
|
let among_active = self.nm_state.nm_state.active_conns.iter().any(|a| {
|
|
let hw_address = active_conn_hw_address(a);
|
|
ap.ssid.as_ref() == a.name() && ap.hw_address == hw_address
|
|
});
|
|
let among_known =
|
|
self.nm_state
|
|
.nm_state
|
|
.known_access_points
|
|
.iter()
|
|
.any(|known_ap| {
|
|
ap.network_type == known_ap.network_type
|
|
&& ap.hw_address == known_ap.hw_address
|
|
&& ap.ssid == known_ap.ssid
|
|
});
|
|
!among_active && !among_known
|
|
})
|
|
.map(|ap| {
|
|
let button = menu_button(
|
|
row::with_children([
|
|
Element::from(
|
|
icon::from_name(wifi_icon(ap.strength))
|
|
.size(16)
|
|
.symbolic(true),
|
|
),
|
|
text::body(ap.ssid.as_ref())
|
|
.align_y(Alignment::Center)
|
|
.into(),
|
|
])
|
|
.align_y(Alignment::Center)
|
|
.spacing(12),
|
|
)
|
|
.on_press(Message::SelectWirelessAccessPoint(ap.clone()));
|
|
button.into()
|
|
});
|
|
content = content.push(
|
|
scrollable::<'_, Message>(column::with_children(list_col))
|
|
.height(Length::Fixed(300.0)),
|
|
);
|
|
}
|
|
|
|
// Add VPN connections section after wireless networks when they are expanded
|
|
if !self.nm_state.known_vpns.is_empty() && self.nm_state.nm_state.wifi_enabled {
|
|
content = content.push(vpn_section(
|
|
&self.nm_state,
|
|
self.show_available_vpns,
|
|
space_xxs,
|
|
space_s,
|
|
));
|
|
}
|
|
|
|
self.view_window_return(content)
|
|
}
|
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
|
activation_token_subscription(0).map(Message::Token)
|
|
}
|
|
|
|
fn style(&self) -> Option<cosmic::iced::theme::Style> {
|
|
Some(cosmic::applet::style())
|
|
}
|
|
|
|
fn on_close_requested(&self, id: window::Id) -> Option<Message> {
|
|
Some(Message::CloseRequested(id))
|
|
}
|
|
}
|
|
|
|
fn active_conn_hw_address(conn: &ActiveConnectionInfo) -> HwAddress {
|
|
match conn {
|
|
ActiveConnectionInfo::Wired { hw_address, .. }
|
|
| ActiveConnectionInfo::WiFi { hw_address, .. } => HwAddress::from_str(hw_address).unwrap(),
|
|
ActiveConnectionInfo::Vpn { .. } => HwAddress::default(),
|
|
}
|
|
}
|