Move Cosmic Applets into new Dir & remove old applets
This commit is contained in:
parent
813e6c0aff
commit
a682b8deb0
134 changed files with 0 additions and 1354 deletions
3986
cosmic-applet-network/Cargo.lock
generated
Normal file
3986
cosmic-applet-network/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
32
cosmic-applet-network/Cargo.toml
Normal file
32
cosmic-applet-network/Cargo.toml
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
[package]
|
||||
name = "cosmic-applet-network"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
|
||||
[dependencies]
|
||||
once_cell = "1.16.0"
|
||||
cosmic-dbus-networkmanager = { git = "https://github.com/pop-os/dbus-settings-bindings" }
|
||||
futures-util = "0.3.21"
|
||||
libcosmic = { git = "https://github.com/pop-os/libcosmic/", branch = "master", default-features = false, features = ["wayland", "applet"] }
|
||||
cosmic-panel-config = {git = "https://github.com/pop-os/cosmic-panel", default-features = false }
|
||||
iced_sctk = { git = "https://github.com/pop-os/iced-sctk" }
|
||||
sctk = { package = "smithay-client-toolkit", git = "https://github.com/Smithay/client-toolkit", version = "0.16" }
|
||||
futures = "0.3"
|
||||
zbus = { version = "3.5", no-default-features = true }
|
||||
log = "0.4"
|
||||
pretty_env_logger = "0.4"
|
||||
# Application i18n
|
||||
i18n-embed = { version = "0.13.4", features = ["fluent-system", "desktop-requester"] }
|
||||
i18n-embed-fl = "0.6.4"
|
||||
rust-embed = "6.3.0"
|
||||
itertools = "0.10.3"
|
||||
slotmap = "1.0.6"
|
||||
tokio = { version = "1.15.0", features = ["full"] }
|
||||
|
||||
[dependencies.iced]
|
||||
git = "https://github.com/pop-os/iced.git"
|
||||
branch = "sctk-cosmic"
|
||||
# path = "../iced"
|
||||
default-features = false
|
||||
features = ["image", "svg", "tokio"]
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
[Desktop Entry]
|
||||
Name=Cosmic Applet Network
|
||||
Comment=Write a GTK + Rust application
|
||||
Type=Application
|
||||
Exec=cosmic-applet-network
|
||||
Terminal=false
|
||||
Categories=GNOME;GTK;
|
||||
Keywords=Gnome;GTK;
|
||||
# Translators: Do NOT translate or transliterate this text (this is an icon file name)!
|
||||
Icon=com.system76.CosmicAppletNetwork.svg
|
||||
StartupNotify=true
|
||||
NoDisplay=true
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="128px" height="128px" viewBox="0 0 128 128" version="1.1">
|
||||
<defs>
|
||||
<filter id="alpha" filterUnits="objectBoundingBox" x="0%" y="0%" width="100%" height="100%">
|
||||
<feColorMatrix type="matrix" in="SourceGraphic" values="0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0"/>
|
||||
</filter>
|
||||
<mask id="mask0">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip1">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10632" clip-path="url(#clip1)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 123.503906 236 C 123.503906 268.863281 96.863281 295.503906 64 295.503906 C 31.136719 295.503906 4.496094 268.863281 4.496094 236 C 4.496094 203.136719 31.136719 176.496094 64 176.496094 C 96.863281 176.496094 123.503906 203.136719 123.503906 236 Z M 123.503906 236 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
<mask id="mask1">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip2">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10635" clip-path="url(#clip2)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 29.195312 180.496094 L 98.804688 180.496094 C 103.609375 180.496094 107.503906 184.046875 107.503906 188.425781 L 107.503906 283.574219 C 107.503906 287.953125 103.609375 291.503906 98.804688 291.503906 L 29.195312 291.503906 C 24.390625 291.503906 20.496094 287.953125 20.496094 283.574219 L 20.496094 188.425781 C 20.496094 184.046875 24.390625 180.496094 29.195312 180.496094 Z M 29.195312 180.496094 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
<mask id="mask2">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip3">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10638" clip-path="url(#clip3)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 20.417969 184.496094 L 107.582031 184.496094 C 111.957031 184.496094 115.503906 188.042969 115.503906 192.417969 L 115.503906 279.582031 C 115.503906 283.957031 111.957031 287.503906 107.582031 287.503906 L 20.417969 287.503906 C 16.042969 287.503906 12.496094 283.957031 12.496094 279.582031 L 12.496094 192.417969 C 12.496094 188.042969 16.042969 184.496094 20.417969 184.496094 Z M 20.417969 184.496094 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
<mask id="mask3">
|
||||
<g filter="url(#alpha)">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(0%,0%,0%);fill-opacity:0.1;stroke:none;"/>
|
||||
</g>
|
||||
</mask>
|
||||
<clipPath id="clip4">
|
||||
<rect x="0" y="0" width="192" height="152"/>
|
||||
</clipPath>
|
||||
<g id="surface10641" clip-path="url(#clip4)">
|
||||
<path style="fill:none;stroke-width:0.99;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-dasharray:0.99,0.99;stroke-miterlimit:4;" d="M 16.425781 200.496094 L 111.574219 200.496094 C 115.953125 200.496094 119.503906 204.390625 119.503906 209.195312 L 119.503906 278.804688 C 119.503906 283.609375 115.953125 287.503906 111.574219 287.503906 L 16.425781 287.503906 C 12.046875 287.503906 8.496094 283.609375 8.496094 278.804688 L 8.496094 209.195312 C 8.496094 204.390625 12.046875 200.496094 16.425781 200.496094 Z M 16.425781 200.496094 " transform="matrix(1,0,0,1,8,-156)"/>
|
||||
</g>
|
||||
</defs>
|
||||
<g id="surface10578">
|
||||
<rect x="0" y="0" width="128" height="128" style="fill:rgb(94.117647%,94.117647%,94.117647%);fill-opacity:1;stroke:none;"/>
|
||||
<use xlink:href="#surface10632" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask0)"/>
|
||||
<use xlink:href="#surface10635" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask1)"/>
|
||||
<use xlink:href="#surface10638" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask2)"/>
|
||||
<use xlink:href="#surface10641" transform="matrix(1,0,0,1,-8,-16)" mask="url(#mask3)"/>
|
||||
<path style="fill:none;stroke-width:2;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(38.431373%,62.7451%,91.764706%);stroke-opacity:1;stroke-miterlimit:4;" d="M 0 289 L 128 289 " transform="matrix(1,0,0,1,0,-172)"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 4.5 KiB |
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gresources>
|
||||
<gresource prefix="/com/System76/CosmicDockAppList/">
|
||||
<!-- see https://gtk-rs.org/gtk4-rs/git/docs/gtk4/struct.Application.html#automatic-resources -->
|
||||
</gresource>
|
||||
</gresources>
|
||||
4
cosmic-applet-network/i18n.toml
Normal file
4
cosmic-applet-network/i18n.toml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
fallback_language = "en"
|
||||
|
||||
[fluent]
|
||||
assets_dir = "i18n"
|
||||
7
cosmic-applet-network/i18n/en/cosmic_applet_network.ftl
Normal file
7
cosmic-applet-network/i18n/en/cosmic_applet_network.ftl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
network = Network
|
||||
airplane-mode = Airplane mode
|
||||
wifi = Wi-Fi
|
||||
ipv4 = IPv4 Address
|
||||
ipv6 = IPv6 Address
|
||||
mac = MAC
|
||||
megabits-per-second = Mbps
|
||||
344
cosmic-applet-network/src/app.rs
Normal file
344
cosmic-applet-network/src/app.rs
Normal file
|
|
@ -0,0 +1,344 @@
|
|||
use cosmic::{
|
||||
applet::CosmicAppletHelper,
|
||||
iced::{
|
||||
executor,
|
||||
widget::{column, container, row, scrollable, text},
|
||||
Alignment, Application, Color, Command, Length, Subscription,
|
||||
},
|
||||
iced_native::window,
|
||||
iced_style::{application, svg},
|
||||
theme::{Button, Svg},
|
||||
widget::{button, horizontal_rule, icon, list_column, toggler},
|
||||
Element, Theme,
|
||||
};
|
||||
use futures::channel::mpsc::UnboundedSender;
|
||||
use iced_sctk::{
|
||||
application::SurfaceIdWrapper,
|
||||
commands::popup::{destroy_popup, get_popup},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
config, fl,
|
||||
network_manager::{
|
||||
available_wifi::AccessPoint, current_networks::ActiveConnectionInfo,
|
||||
network_manager_subscription, NetworkManagerEvent, NetworkManagerRequest,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn run() -> cosmic::iced::Result {
|
||||
let helper = CosmicAppletHelper::default();
|
||||
CosmicNetworkApplet::run(helper.window_settings())
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct CosmicNetworkApplet {
|
||||
icon_name: String,
|
||||
theme: Theme,
|
||||
popup: Option<window::Id>,
|
||||
id_ctr: u32,
|
||||
applet_helper: CosmicAppletHelper,
|
||||
// STATE
|
||||
airplane_mode: bool,
|
||||
wifi: bool,
|
||||
wireless_access_points: Vec<AccessPoint>,
|
||||
active_conns: Vec<ActiveConnectionInfo>,
|
||||
nm_sender: Option<UnboundedSender<NetworkManagerRequest>>,
|
||||
}
|
||||
|
||||
impl CosmicNetworkApplet {
|
||||
fn update_icon_name(&mut self) {
|
||||
self.icon_name = self
|
||||
.active_conns
|
||||
.iter()
|
||||
.fold("network-offline-symbolic", |icon_name, conn| {
|
||||
match (icon_name, conn) {
|
||||
("network-offline-symbolic", ActiveConnectionInfo::WiFi { .. }) => {
|
||||
"network-wireless-symbolic"
|
||||
}
|
||||
(
|
||||
"network-offline-symbolic",
|
||||
ActiveConnectionInfo::Wired { .. },
|
||||
)
|
||||
| (
|
||||
"network-wireless-symbolic",
|
||||
ActiveConnectionInfo::Wired { .. },
|
||||
) => "network-wired-symbolic",
|
||||
(_, ActiveConnectionInfo::Vpn { .. }) => "network-vpn-symbolic",
|
||||
_ => icon_name,
|
||||
}
|
||||
})
|
||||
.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
enum Message {
|
||||
TogglePopup,
|
||||
ToggleAirplaneMode(bool),
|
||||
ToggleWiFi(bool),
|
||||
Errored(String),
|
||||
Ignore,
|
||||
NetworkManagerEvent(NetworkManagerEvent),
|
||||
SelectWirelessAccessPoint(String),
|
||||
}
|
||||
|
||||
impl Application for CosmicNetworkApplet {
|
||||
type Message = Message;
|
||||
type Theme = Theme;
|
||||
type Executor = executor::Default;
|
||||
type Flags = ();
|
||||
|
||||
fn new(_flags: ()) -> (Self, Command<Message>) {
|
||||
(
|
||||
CosmicNetworkApplet {
|
||||
icon_name: "network-offline-symbolic".to_string(),
|
||||
..Default::default()
|
||||
},
|
||||
Command::none(),
|
||||
)
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
config::APP_ID.to_string()
|
||||
}
|
||||
|
||||
fn update(&mut self, message: Message) -> Command<Message> {
|
||||
match message {
|
||||
Message::TogglePopup => {
|
||||
if let Some(p) = self.popup.take() {
|
||||
return destroy_popup(p);
|
||||
} else {
|
||||
// TODO request update of state maybe
|
||||
self.id_ctr += 1;
|
||||
let new_id = window::Id::new(self.id_ctr);
|
||||
self.popup.replace(new_id);
|
||||
|
||||
let popup_settings = self.applet_helper.get_popup_settings(
|
||||
window::Id::new(0),
|
||||
new_id,
|
||||
(420, 600),
|
||||
None,
|
||||
None,
|
||||
);
|
||||
return get_popup(popup_settings);
|
||||
}
|
||||
}
|
||||
Message::Errored(_) => todo!(),
|
||||
Message::Ignore => {}
|
||||
Message::ToggleAirplaneMode(enabled) => {
|
||||
self.airplane_mode = enabled;
|
||||
// TODO apply changes
|
||||
}
|
||||
Message::ToggleWiFi(enabled) => {
|
||||
self.wifi = enabled;
|
||||
if let Some(tx) = self.nm_sender.as_mut() {
|
||||
let _ = tx.unbounded_send(NetworkManagerRequest::SetWiFi(enabled));
|
||||
}
|
||||
}
|
||||
Message::NetworkManagerEvent(event) => match event {
|
||||
NetworkManagerEvent::Init {
|
||||
sender,
|
||||
wireless_access_points,
|
||||
active_conns,
|
||||
wifi_enabled,
|
||||
airplane_mode,
|
||||
} => {
|
||||
self.nm_sender.replace(sender);
|
||||
self.wireless_access_points = wireless_access_points;
|
||||
self.active_conns = active_conns;
|
||||
self.wifi = wifi_enabled;
|
||||
self.airplane_mode = airplane_mode;
|
||||
self.update_icon_name();
|
||||
}
|
||||
NetworkManagerEvent::WiFiEnabled(enabled) => {
|
||||
self.wifi = enabled;
|
||||
}
|
||||
NetworkManagerEvent::WirelessAccessPoints(access_points) => {
|
||||
self.wireless_access_points = access_points;
|
||||
}
|
||||
NetworkManagerEvent::ActiveConns(conns) => {
|
||||
self.active_conns = conns;
|
||||
self.update_icon_name();
|
||||
}
|
||||
NetworkManagerEvent::RequestResponse { wireless_access_points, active_conns, wifi_enabled, success, ..} => {
|
||||
if success {
|
||||
self.wireless_access_points = wireless_access_points;
|
||||
self.active_conns = active_conns;
|
||||
self.wifi = wifi_enabled;
|
||||
self.update_icon_name();
|
||||
}
|
||||
},
|
||||
},
|
||||
Message::SelectWirelessAccessPoint(ssid) => {
|
||||
if let Some(tx) = self.nm_sender.as_ref() {
|
||||
let _ = tx.unbounded_send(NetworkManagerRequest::SelectAccessPoint(ssid));
|
||||
}
|
||||
}
|
||||
}
|
||||
Command::none()
|
||||
}
|
||||
fn view(&self, id: SurfaceIdWrapper) -> Element<Message> {
|
||||
match id {
|
||||
SurfaceIdWrapper::LayerSurface(_) => unimplemented!(),
|
||||
SurfaceIdWrapper::Window(_) => self
|
||||
.applet_helper
|
||||
.icon_button(&self.icon_name)
|
||||
.on_press(Message::TogglePopup)
|
||||
.into(),
|
||||
SurfaceIdWrapper::Popup(_) => {
|
||||
let name = text(fl!("network")).size(18);
|
||||
let icon = icon(&self.icon_name, 24)
|
||||
.style(Svg::Custom(|theme| svg::Appearance {
|
||||
fill: Some(theme.palette().text),
|
||||
}))
|
||||
.width(Length::Units(24))
|
||||
.height(Length::Units(24));
|
||||
let mut list_col = list_column();
|
||||
|
||||
for conn in &self.active_conns {
|
||||
let el = match conn {
|
||||
ActiveConnectionInfo::Vpn { name, ip_addresses } => {
|
||||
let mut ipv4 = column![];
|
||||
let mut ipv6 = column![];
|
||||
for addr in ip_addresses {
|
||||
match addr {
|
||||
std::net::IpAddr::V4(a) => {
|
||||
ipv4 = ipv4.push(text(format!(
|
||||
"{}: {}",
|
||||
fl!("ipv4"),
|
||||
a.to_string()
|
||||
)));
|
||||
}
|
||||
std::net::IpAddr::V6(a) => {
|
||||
ipv6 = ipv6.push(text(format!(
|
||||
"{}: {}",
|
||||
fl!("ipv6"),
|
||||
a.to_string()
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
column![text(name), ipv4, ipv6].spacing(4)
|
||||
}
|
||||
ActiveConnectionInfo::Wired {
|
||||
name,
|
||||
hw_address,
|
||||
speed,
|
||||
ip_addresses,
|
||||
} => {
|
||||
let mut ipv4 = column![];
|
||||
let mut ipv6 = column![];
|
||||
for addr in ip_addresses {
|
||||
match addr {
|
||||
std::net::IpAddr::V4(a) => {
|
||||
ipv4 = ipv4.push(text(format!(
|
||||
"{}: {}",
|
||||
fl!("ipv4"),
|
||||
a.to_string()
|
||||
)));
|
||||
}
|
||||
std::net::IpAddr::V6(a) => {
|
||||
ipv6 = ipv6.push(text(format!(
|
||||
"{}: {}",
|
||||
fl!("ipv6"),
|
||||
a.to_string()
|
||||
)));
|
||||
}
|
||||
}
|
||||
}
|
||||
column![
|
||||
row![
|
||||
text(name),
|
||||
text(format!("{speed} {}", fl!("megabits-per-second")))
|
||||
]
|
||||
.spacing(16),
|
||||
ipv4,
|
||||
ipv6,
|
||||
text(format!("{}: {hw_address}", fl!("mac"))),
|
||||
]
|
||||
.spacing(4)
|
||||
}
|
||||
ActiveConnectionInfo::WiFi {
|
||||
name, hw_address, ..
|
||||
} => column![row![
|
||||
text(name),
|
||||
text(format!("{}: {hw_address}", fl!("mac")))
|
||||
]
|
||||
.spacing(12)]
|
||||
.spacing(4),
|
||||
};
|
||||
list_col = list_col.add(el);
|
||||
}
|
||||
|
||||
let mut content = column![
|
||||
row![icon, name].spacing(8).width(Length::Fill),
|
||||
list_col,
|
||||
horizontal_rule(1),
|
||||
container(
|
||||
toggler(fl!("airplane-mode"), self.airplane_mode, |m| {
|
||||
Message::ToggleAirplaneMode(m)
|
||||
})
|
||||
.width(Length::Fill)
|
||||
)
|
||||
.padding([0, 12]),
|
||||
horizontal_rule(1),
|
||||
container(
|
||||
toggler(fl!("wifi"), self.wifi, |m| { Message::ToggleWiFi(m) })
|
||||
.width(Length::Fill)
|
||||
)
|
||||
.padding([0, 12]),
|
||||
]
|
||||
.align_items(Alignment::Center)
|
||||
.spacing(8)
|
||||
.padding(8);
|
||||
if self.wifi {
|
||||
let mut list_col = list_column();
|
||||
for ap in &self.wireless_access_points {
|
||||
let button = self
|
||||
.active_conns
|
||||
.iter()
|
||||
.find_map(|conn| match conn {
|
||||
ActiveConnectionInfo::WiFi { name, .. } if name == &ap.ssid => {
|
||||
Some(
|
||||
button(Button::Primary)
|
||||
.text(&ap.ssid)
|
||||
.on_press(Message::Ignore)
|
||||
.width(Length::Fill),
|
||||
)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
button(Button::Text)
|
||||
.text(&ap.ssid)
|
||||
.on_press(Message::SelectWirelessAccessPoint(ap.ssid.clone()))
|
||||
.width(Length::Fill)
|
||||
});
|
||||
list_col = list_col.add(button);
|
||||
}
|
||||
content = content.push(scrollable(list_col).height(Length::Fill));
|
||||
}
|
||||
self.applet_helper.popup_container(content).into()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn subscription(&self) -> Subscription<Message> {
|
||||
network_manager_subscription(0).map(|(_, event)| Message::NetworkManagerEvent(event))
|
||||
}
|
||||
|
||||
fn theme(&self) -> Theme {
|
||||
self.theme
|
||||
}
|
||||
|
||||
fn close_requested(&self, _id: iced_sctk::application::SurfaceIdWrapper) -> Self::Message {
|
||||
Message::Ignore
|
||||
}
|
||||
|
||||
fn style(&self) -> <Self::Theme as application::StyleSheet>::Style {
|
||||
<Self::Theme as application::StyleSheet>::Style::Custom(|theme| application::Appearance {
|
||||
background_color: Color::from_rgba(0.0, 0.0, 0.0, 0.0),
|
||||
text_color: theme.cosmic().on_bg_color().into(),
|
||||
})
|
||||
}
|
||||
}
|
||||
3
cosmic-applet-network/src/config.rs
Normal file
3
cosmic-applet-network/src/config.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub const APP_ID: &str = "com.system76.CosmicAppletNetwork";
|
||||
pub const PROFILE: &str = "";
|
||||
pub const VERSION: &str = "0.1.0";
|
||||
47
cosmic-applet-network/src/localize.rs
Normal file
47
cosmic-applet-network/src/localize.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// SPDX-License-Identifier: MPL-2.0-only
|
||||
|
||||
use i18n_embed::{
|
||||
fluent::{fluent_language_loader, FluentLanguageLoader},
|
||||
DefaultLocalizer, LanguageLoader, Localizer,
|
||||
};
|
||||
use once_cell::sync::Lazy;
|
||||
use rust_embed::RustEmbed;
|
||||
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "i18n/"]
|
||||
struct Localizations;
|
||||
|
||||
pub static LANGUAGE_LOADER: Lazy<FluentLanguageLoader> = Lazy::new(|| {
|
||||
let loader: FluentLanguageLoader = fluent_language_loader!();
|
||||
|
||||
loader
|
||||
.load_fallback_language(&Localizations)
|
||||
.expect("Error while loading fallback language");
|
||||
|
||||
loader
|
||||
});
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! fl {
|
||||
($message_id:literal) => {{
|
||||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id)
|
||||
}};
|
||||
|
||||
($message_id:literal, $($args:expr),*) => {{
|
||||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id, $($args), *)
|
||||
}};
|
||||
}
|
||||
|
||||
// Get the `Localizer` to be used for localizing this library.
|
||||
pub fn localizer() -> Box<dyn Localizer> {
|
||||
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
|
||||
}
|
||||
|
||||
pub fn localize() {
|
||||
let localizer = localizer();
|
||||
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
|
||||
|
||||
if let Err(error) = localizer.select(&requested_languages) {
|
||||
eprintln!("Error while loading language for App List {}", error);
|
||||
}
|
||||
}
|
||||
23
cosmic-applet-network/src/main.rs
Normal file
23
cosmic-applet-network/src/main.rs
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
mod app;
|
||||
mod config;
|
||||
mod localize;
|
||||
mod network_manager;
|
||||
|
||||
use log::info;
|
||||
|
||||
use crate::config::{APP_ID, PROFILE, VERSION};
|
||||
use crate::localize::localize;
|
||||
|
||||
fn main() -> cosmic::iced::Result {
|
||||
// Initialize logger
|
||||
pretty_env_logger::init();
|
||||
info!("Iced Workspaces Applet ({})", APP_ID);
|
||||
info!("Version: {} ({})", VERSION, PROFILE);
|
||||
|
||||
// Prepare i18n
|
||||
localize();
|
||||
|
||||
app::run()
|
||||
}
|
||||
43
cosmic-applet-network/src/network_manager/available_wifi.rs
Normal file
43
cosmic-applet-network/src/network_manager/available_wifi.rs
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
use cosmic_dbus_networkmanager::device::wireless::WirelessDevice;
|
||||
|
||||
use futures_util::StreamExt;
|
||||
use itertools::Itertools;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub async fn handle_wireless_device(device: WirelessDevice<'_>) -> zbus::Result<Vec<AccessPoint>> {
|
||||
device.request_scan(HashMap::new()).await?;
|
||||
let mut scan_changed = device.receive_last_scan_changed().await;
|
||||
if let Some(t) = scan_changed.next().await {
|
||||
if let Ok(-1) = t.get().await {
|
||||
eprintln!("scan errored");
|
||||
return Ok(Default::default());
|
||||
}
|
||||
}
|
||||
let access_points = device.get_access_points().await?;
|
||||
// Sort by strength and remove duplicates
|
||||
let mut aps = HashMap::<String, AccessPoint>::new();
|
||||
for ap in access_points {
|
||||
let ssid = String::from_utf8_lossy(&ap.ssid().await?.clone()).into_owned();
|
||||
let strength = ap.strength().await?;
|
||||
if let Some(access_point) = aps.get(&ssid) {
|
||||
if access_point.strength > strength {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
aps.insert(ssid.clone(), AccessPoint { ssid, strength });
|
||||
}
|
||||
let aps = aps
|
||||
.into_iter()
|
||||
.map(|(_, x)| x)
|
||||
.sorted_by(|a, b| b.strength.cmp(&a.strength))
|
||||
.collect();
|
||||
Ok(aps)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AccessPoint {
|
||||
pub ssid: String,
|
||||
pub strength: u8,
|
||||
}
|
||||
108
cosmic-applet-network/src/network_manager/current_networks.rs
Normal file
108
cosmic-applet-network/src/network_manager/current_networks.rs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
use cosmic_dbus_networkmanager::{
|
||||
active_connection::ActiveConnection,
|
||||
device::SpecificDevice,
|
||||
interface::enums::{ApFlags, ApSecurityFlags},
|
||||
};
|
||||
use std::net::IpAddr;
|
||||
|
||||
pub async fn active_connections(
|
||||
active_connections: Vec<ActiveConnection<'_>>,
|
||||
) -> zbus::Result<Vec<ActiveConnectionInfo>> {
|
||||
let mut info = Vec::<ActiveConnectionInfo>::with_capacity(active_connections.len());
|
||||
for connection in active_connections {
|
||||
if connection.vpn().await.unwrap_or_default() {
|
||||
let mut ip_addresses = Vec::new();
|
||||
for address_data in connection.ip4_config().await?.address_data().await.unwrap_or_default() {
|
||||
ip_addresses.push(IpAddr::V4(address_data.address));
|
||||
}
|
||||
for address_data in connection.ip6_config().await?.address_data().await.unwrap_or_default() {
|
||||
ip_addresses.push(IpAddr::V6(address_data.address));
|
||||
}
|
||||
info.push(ActiveConnectionInfo::Vpn {
|
||||
name: connection.id().await?,
|
||||
ip_addresses,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
for device in connection.devices().await.unwrap_or_default() {
|
||||
match device.downcast_to_device().await.ok().and_then(|inner| inner) {
|
||||
Some(SpecificDevice::Wired(wired_device)) => {
|
||||
let mut ip_addresses = Vec::new();
|
||||
for address_data in device.ip4_config().await?.address_data().await.unwrap_or_default() {
|
||||
ip_addresses.push(IpAddr::V4(address_data.address));
|
||||
}
|
||||
for address_data in device.ip6_config().await?.address_data().await.unwrap_or_default() {
|
||||
ip_addresses.push(IpAddr::V6(address_data.address));
|
||||
}
|
||||
info.push(ActiveConnectionInfo::Wired {
|
||||
name: connection.id().await?,
|
||||
hw_address: wired_device.hw_address().await?,
|
||||
speed: wired_device.speed().await?,
|
||||
ip_addresses,
|
||||
});
|
||||
}
|
||||
Some(SpecificDevice::Wireless(wireless_device)) => {
|
||||
if let Ok(access_point) = wireless_device.active_access_point().await {
|
||||
info.push(ActiveConnectionInfo::WiFi {
|
||||
name: String::from_utf8_lossy(&access_point.ssid().await?).into_owned(),
|
||||
hw_address: wireless_device.hw_address().await?,
|
||||
flags: access_point.flags().await?,
|
||||
rsn_flags: access_point.rsn_flags().await?,
|
||||
wpa_flags: access_point.wpa_flags().await?,
|
||||
});
|
||||
}
|
||||
}
|
||||
Some(SpecificDevice::WireGuard(_)) => {
|
||||
let mut ip_addresses = Vec::new();
|
||||
for address_data in connection.ip4_config().await?.address_data().await.unwrap_or_default() {
|
||||
ip_addresses.push(IpAddr::V4(address_data.address));
|
||||
}
|
||||
for address_data in connection.ip6_config().await?.address_data().await.unwrap_or_default() {
|
||||
ip_addresses.push(IpAddr::V6(address_data.address));
|
||||
}
|
||||
info.push(ActiveConnectionInfo::Vpn {
|
||||
name: connection.id().await?,
|
||||
ip_addresses,
|
||||
});
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
info.sort_by(|a, b| {
|
||||
let helper = |conn: &ActiveConnectionInfo| {
|
||||
match conn {
|
||||
ActiveConnectionInfo::Vpn { name, .. } => format!("0{name}"),
|
||||
ActiveConnectionInfo::Wired { name, .. } => format!("1{name}"),
|
||||
ActiveConnectionInfo::WiFi { name, .. } => format!("2{name}"),
|
||||
}
|
||||
};
|
||||
helper(a).cmp(&helper(b))
|
||||
});
|
||||
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ActiveConnectionInfo {
|
||||
Wired {
|
||||
name: String,
|
||||
hw_address: String,
|
||||
speed: u32,
|
||||
ip_addresses: Vec<IpAddr>,
|
||||
},
|
||||
WiFi {
|
||||
name: String,
|
||||
hw_address: String,
|
||||
flags: ApFlags,
|
||||
rsn_flags: ApSecurityFlags,
|
||||
wpa_flags: ApSecurityFlags,
|
||||
},
|
||||
Vpn {
|
||||
name: String,
|
||||
ip_addresses: Vec<IpAddr>,
|
||||
},
|
||||
}
|
||||
242
cosmic-applet-network/src/network_manager/mod.rs
Normal file
242
cosmic-applet-network/src/network_manager/mod.rs
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
pub mod available_wifi;
|
||||
pub mod current_networks;
|
||||
|
||||
use std::{fmt::Debug, hash::Hash, time::Duration};
|
||||
|
||||
use cosmic::iced::{self, subscription};
|
||||
use cosmic_dbus_networkmanager::{
|
||||
device::SpecificDevice, interface::enums::DeviceType, nm::NetworkManager,
|
||||
};
|
||||
use futures::{
|
||||
channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender},
|
||||
FutureExt, StreamExt,
|
||||
};
|
||||
use zbus::Connection;
|
||||
|
||||
use self::{
|
||||
available_wifi::{handle_wireless_device, AccessPoint},
|
||||
current_networks::{active_connections, ActiveConnectionInfo},
|
||||
};
|
||||
|
||||
// TODO subscription for wifi list & selection of wifi
|
||||
// TODO subscription & channel for enabling / disabling wifi
|
||||
// TODO subscription for displaying active connections & devices
|
||||
|
||||
pub fn network_manager_subscription<I: 'static + Hash + Copy + Send + Sync + Debug>(
|
||||
id: I,
|
||||
) -> iced::Subscription<(I, NetworkManagerEvent)> {
|
||||
subscription::unfold(id, State::Ready, move |state| start_listening(id, state))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum State {
|
||||
Ready,
|
||||
Waiting(Connection, UnboundedReceiver<NetworkManagerRequest>),
|
||||
Finished,
|
||||
}
|
||||
|
||||
async fn start_listening<I: Copy>(
|
||||
id: I,
|
||||
state: State,
|
||||
) -> (Option<(I, NetworkManagerEvent)>, State) {
|
||||
match state {
|
||||
State::Ready => {
|
||||
let conn = match Connection::system().await {
|
||||
Ok(c) => c,
|
||||
Err(_) => return (None, State::Finished),
|
||||
};
|
||||
let network_manager = match NetworkManager::new(&conn).await {
|
||||
Ok(n) => n,
|
||||
Err(_) => return (None, State::Finished),
|
||||
};
|
||||
let (tx, rx) = unbounded();
|
||||
let mut active_conns = active_connections(
|
||||
network_manager
|
||||
.active_connections()
|
||||
.await
|
||||
.unwrap_or_default(),
|
||||
)
|
||||
.await
|
||||
.unwrap_or_default();
|
||||
active_conns.sort_by(|a, b| {
|
||||
let helper = |conn: &ActiveConnectionInfo| match conn {
|
||||
ActiveConnectionInfo::Vpn { name, .. } => format!("0{name}"),
|
||||
ActiveConnectionInfo::Wired { name, .. } => format!("1{name}"),
|
||||
ActiveConnectionInfo::WiFi { name, .. } => format!("2{name}"),
|
||||
};
|
||||
helper(a).cmp(&helper(b))
|
||||
});
|
||||
let wifi_enabled = network_manager.wireless_enabled().await.unwrap_or_default();
|
||||
let devices = network_manager.devices().await.ok().unwrap_or_default();
|
||||
let wireless_access_point_futures: Vec<_> = devices
|
||||
.into_iter()
|
||||
.map(|device| async move {
|
||||
if let Ok(Some(SpecificDevice::Wireless(wireless_device))) =
|
||||
device.downcast_to_device().await
|
||||
{
|
||||
handle_wireless_device(wireless_device)
|
||||
.await
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let mut wireless_access_points =
|
||||
Vec::with_capacity(wireless_access_point_futures.len());
|
||||
for f in wireless_access_point_futures {
|
||||
wireless_access_points.append(&mut f.await);
|
||||
}
|
||||
wireless_access_points.sort_by(|a, b| b.strength.cmp(&a.strength));
|
||||
drop(network_manager);
|
||||
return (
|
||||
Some((
|
||||
id,
|
||||
NetworkManagerEvent::Init {
|
||||
sender: tx,
|
||||
wireless_access_points,
|
||||
wifi_enabled,
|
||||
airplane_mode: false,
|
||||
active_conns,
|
||||
},
|
||||
)),
|
||||
State::Waiting(conn, rx),
|
||||
);
|
||||
}
|
||||
State::Waiting(conn, mut rx) => {
|
||||
let network_manager = match NetworkManager::new(&conn).await {
|
||||
Ok(n) => n,
|
||||
Err(_) => return (None, State::Finished),
|
||||
};
|
||||
let mut active_conns_changed = tokio::time::sleep(Duration::from_secs(5))
|
||||
.then(|_| async { network_manager.receive_active_connections_changed().await })
|
||||
.await;
|
||||
let mut devices_changed = network_manager.receive_devices_changed().await;
|
||||
let mut wireless_enabled_changed =
|
||||
network_manager.receive_wireless_enabled_changed().await;
|
||||
let mut req = rx.next().boxed().fuse();
|
||||
|
||||
let (update, should_exit) = futures::select! {
|
||||
req = req => {
|
||||
match req {
|
||||
Some(NetworkManagerRequest::SetAirplaneMode(state)) => {
|
||||
// TODO set airplane mode
|
||||
let _ = network_manager.set_wireless_enabled(state).await;
|
||||
(None, false)
|
||||
}
|
||||
Some(NetworkManagerRequest::SetWiFi(enabled)) => {
|
||||
let success = network_manager.set_wireless_enabled(enabled).await.is_ok();
|
||||
let active_conns = active_connections(network_manager.active_connections().await.unwrap_or_default()).await.unwrap_or_default();
|
||||
let devices = network_manager.devices().await.ok().unwrap_or_default();
|
||||
let wireless_access_point_futures: Vec<_> = devices.into_iter().map(|device| async move {
|
||||
if let Ok(Some(SpecificDevice::Wireless(wireless_device))) =
|
||||
device.downcast_to_device().await
|
||||
{
|
||||
handle_wireless_device(wireless_device).await.unwrap_or_default()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}).collect();
|
||||
let mut wireless_access_points = Vec::with_capacity(wireless_access_point_futures.len());
|
||||
for f in wireless_access_point_futures {
|
||||
wireless_access_points.append(&mut f.await);
|
||||
}
|
||||
(Some((id, NetworkManagerEvent::RequestResponse {
|
||||
req: NetworkManagerRequest::SetWiFi(enabled),
|
||||
success,
|
||||
active_conns,
|
||||
wireless_access_points,
|
||||
wifi_enabled: enabled,
|
||||
airplane_mode: false,
|
||||
})), false)
|
||||
}
|
||||
Some(NetworkManagerRequest::SelectAccessPoint(ssid)) => {
|
||||
'device_loop: for device in network_manager.devices().await.ok().unwrap_or_default() {
|
||||
if matches!(device.device_type().await.unwrap_or(DeviceType::Other), DeviceType::Wifi) {
|
||||
for conn in device.available_connections().await.unwrap_or_default() {
|
||||
// dbg!(&conn.path());
|
||||
// TODO activate connection
|
||||
}
|
||||
}
|
||||
}
|
||||
(None, false)
|
||||
}
|
||||
None => {
|
||||
(None, true)
|
||||
}
|
||||
}}
|
||||
_ = active_conns_changed.next().boxed().fuse() => {
|
||||
let active_conns = active_connections(network_manager.active_connections().await.unwrap_or_default()).await.unwrap_or_default();
|
||||
|
||||
(Some((id, NetworkManagerEvent::ActiveConns(active_conns))), false)
|
||||
}
|
||||
_ = devices_changed.next().boxed().fuse() => {
|
||||
let devices = network_manager.devices().await.ok().unwrap_or_default();
|
||||
let wireless_access_point_futures: Vec<_> = devices.into_iter().map(|device| async move {
|
||||
if let Ok(Some(SpecificDevice::Wireless(wireless_device))) =
|
||||
device.downcast_to_device().await
|
||||
{
|
||||
handle_wireless_device(wireless_device).await.unwrap_or_default()
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}).collect();
|
||||
let mut wireless_access_points = Vec::with_capacity(wireless_access_point_futures.len());
|
||||
for f in wireless_access_point_futures {
|
||||
wireless_access_points.append(&mut f.await);
|
||||
}
|
||||
(Some((id, NetworkManagerEvent::WirelessAccessPoints(wireless_access_points))), false)
|
||||
}
|
||||
enabled = wireless_enabled_changed.next().boxed().fuse() => {
|
||||
let update = if let Some(update) = enabled {
|
||||
update.get().await.ok().map(|update| (id, NetworkManagerEvent::WiFiEnabled(update)))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
(update, false)
|
||||
}
|
||||
};
|
||||
drop(active_conns_changed);
|
||||
drop(wireless_enabled_changed);
|
||||
drop(req);
|
||||
(
|
||||
update,
|
||||
if should_exit {
|
||||
State::Finished
|
||||
} else {
|
||||
State::Waiting(conn, rx)
|
||||
},
|
||||
)
|
||||
}
|
||||
State::Finished => iced::futures::future::pending().await,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NetworkManagerRequest {
|
||||
SetAirplaneMode(bool),
|
||||
SetWiFi(bool),
|
||||
SelectAccessPoint(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum NetworkManagerEvent {
|
||||
Init {
|
||||
sender: UnboundedSender<NetworkManagerRequest>,
|
||||
wireless_access_points: Vec<AccessPoint>,
|
||||
active_conns: Vec<ActiveConnectionInfo>,
|
||||
wifi_enabled: bool,
|
||||
airplane_mode: bool,
|
||||
},
|
||||
RequestResponse {
|
||||
req: NetworkManagerRequest,
|
||||
wireless_access_points: Vec<AccessPoint>,
|
||||
active_conns: Vec<ActiveConnectionInfo>,
|
||||
wifi_enabled: bool,
|
||||
airplane_mode: bool,
|
||||
success: bool,
|
||||
},
|
||||
WiFiEnabled(bool),
|
||||
WirelessAccessPoints(Vec<AccessPoint>),
|
||||
ActiveConns(Vec<ActiveConnectionInfo>),
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue