mirror of
https://github.com/Xetibo/ReSet.git
synced 2025-07-07 18:47:45 +02:00
chore: Code cleanup
This commit is contained in:
parent
f8c27fd498
commit
921d5e7fe6
27 changed files with 304 additions and 234 deletions
|
@ -1,6 +1,7 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use crate::components::wifi::savedWifiEntryImpl;
|
||||
use crate::components::wifi::wifiBoxImpl::WifiBox;
|
||||
use adw::glib;
|
||||
use adw::glib::Object;
|
||||
use adw::prelude::{ButtonExt, WidgetExt};
|
||||
|
@ -10,7 +11,6 @@ use glib::subclass::types::ObjectSubclassIsExt;
|
|||
use glib::{clone, PropertySet};
|
||||
use gtk::gio;
|
||||
use gtk::prelude::ListBoxRowExt;
|
||||
use crate::components::wifi::wifiBoxImpl::WifiBox;
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct SavedWifiEntry(ObjectSubclass<savedWifiEntryImpl::SavedWifiEntry>)
|
||||
|
@ -19,16 +19,18 @@ glib::wrapper! {
|
|||
}
|
||||
|
||||
impl SavedWifiEntry {
|
||||
pub fn new(name: &String, path: Path<'static>, wifiBox: &WifiBox) -> Self {
|
||||
pub fn new(name: &str, path: Path<'static>, wifiBox: &WifiBox) -> Self {
|
||||
let entry: SavedWifiEntry = Object::builder().build();
|
||||
entry.set_activatable(false);
|
||||
let entryImp = entry.imp();
|
||||
|
||||
entryImp.resetEditSavedWifiButton.connect_clicked(clone!(@ weak entryImp, @ weak wifiBox => move |_| {
|
||||
// TODO accesspoint has to be saved somewhere i guess
|
||||
// let _option = getConnectionSettings(entryImp.accessPoint.borrow().associated_connection.clone());
|
||||
// wifiBox.resetWifiNavigation.push(&*WifiOptions::new(_option));
|
||||
}));
|
||||
entryImp.resetEditSavedWifiButton.connect_clicked(
|
||||
clone!(@ weak entryImp, @ weak wifiBox => move |_| {
|
||||
// TODO accesspoint has to be saved somewhere i guess
|
||||
// let _option = getConnectionSettings(entryImp.accessPoint.borrow().associated_connection.clone());
|
||||
// wifiBox.resetWifiNavigation.push(&*WifiOptions::new(_option));
|
||||
}),
|
||||
);
|
||||
|
||||
entryImp.resetSavedWifiLabel.set_text(name);
|
||||
entryImp.resetConnectionPath.set(path);
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
use dbus::arg::{RefArg};
|
||||
use dbus::arg::RefArg;
|
||||
use dbus::blocking::Connection;
|
||||
use dbus::Error;
|
||||
use dbus::Path;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
use ReSet_Lib::network::connection::Connection as ResetConnection;
|
||||
|
||||
type ResultType =
|
||||
Result<(HashMap<String, HashMap<String, dbus::arg::Variant<Box<dyn RefArg>>>>,), Error>;
|
||||
|
||||
pub fn getConnectionSettings(path: Path<'static>) -> ResetConnection {
|
||||
let conn = Connection::new_session().unwrap();
|
||||
let proxy = conn.with_proxy(
|
||||
|
@ -13,10 +16,8 @@ pub fn getConnectionSettings(path: Path<'static>) -> ResetConnection {
|
|||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(1000),
|
||||
);
|
||||
let res: Result<
|
||||
(HashMap<String, HashMap<String, dbus::arg::Variant<Box<dyn RefArg>>>>,),
|
||||
Error,
|
||||
> = proxy.method_call("org.xetibo.ReSetWireless", "GetConnectionSettings", (path,));
|
||||
let res: ResultType =
|
||||
proxy.method_call("org.xetibo.ReSetWireless", "GetConnectionSettings", (path,));
|
||||
if res.is_err() {
|
||||
ResetConnection::default();
|
||||
}
|
||||
|
|
|
@ -27,7 +27,9 @@ impl WifiAddressEntry {
|
|||
|
||||
entryImp.resetAddressAddress.set_text(&addr);
|
||||
entryImp.resetAddressNetmask.set_text(&prefix);
|
||||
entryImp.resetAddressRow.set_title(&format!("{}, {}", addr, prefix));
|
||||
entryImp
|
||||
.resetAddressRow
|
||||
.set_title(&format!("{}, {}", addr, prefix));
|
||||
}
|
||||
entry
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::components::wifi::wifiAddressEntry;
|
||||
use adw::{EntryRow, ExpanderRow};
|
||||
use crate::components::wifi::{wifiAddressEntry};
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{glib, CompositeTemplate, Button};
|
||||
use gtk::{glib, Button, CompositeTemplate};
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[derive(Default, CompositeTemplate)]
|
||||
|
|
|
@ -4,39 +4,37 @@ use std::sync::Arc;
|
|||
|
||||
use std::time::Duration;
|
||||
|
||||
|
||||
use crate::components::base::utils::Listeners;
|
||||
use crate::components::utils::setComboRowEllipsis;
|
||||
use adw::glib;
|
||||
use adw::glib::Object;
|
||||
use adw::prelude::{ListBoxRowExt, PreferencesGroupExt};
|
||||
use adw::subclass::prelude::ObjectSubclassIsExt;
|
||||
use dbus::arg::{RefArg};
|
||||
use dbus::blocking::Connection;
|
||||
use dbus::message::SignalArgs;
|
||||
use dbus::Error;
|
||||
use dbus::Path;
|
||||
use glib::{ObjectExt, PropertySet};
|
||||
use glib::PropertySet;
|
||||
use gtk::gio;
|
||||
use gtk::glib::Variant;
|
||||
use gtk::prelude::{ActionableExt, WidgetExt};
|
||||
use ReSet_Lib::network::network::{AccessPoint, WifiStrength};
|
||||
use ReSet_Lib::signals::{AccessPointAdded};
|
||||
use ReSet_Lib::signals::AccessPointAdded;
|
||||
use ReSet_Lib::signals::{AccessPointChanged, AccessPointRemoved};
|
||||
use crate::components::utils::setComboRowEllipsis;
|
||||
|
||||
|
||||
use crate::components::wifi::wifiBoxImpl;
|
||||
use crate::components::wifi::wifiEntry::WifiEntry;
|
||||
|
||||
use super::savedWifiEntry::SavedWifiEntry;
|
||||
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct WifiBox(ObjectSubclass<wifiBoxImpl::WifiBox>)
|
||||
@extends gtk::Box, gtk::Widget,
|
||||
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
|
||||
}
|
||||
|
||||
type ResultMap = Result<(Vec<(Path<'static>, Vec<u8>)>,), Error>;
|
||||
|
||||
unsafe impl Send for WifiBox {}
|
||||
unsafe impl Sync for WifiBox {}
|
||||
|
||||
|
@ -56,11 +54,19 @@ impl WifiBox {
|
|||
.set_action_target_value(Some(&Variant::from("saved")));
|
||||
|
||||
selfImp.resetAvailableNetworks.set_activatable(true);
|
||||
selfImp.resetAvailableNetworks.set_action_name(Some("navigation.pop"));
|
||||
selfImp
|
||||
.resetAvailableNetworks
|
||||
.set_action_name(Some("navigation.pop"));
|
||||
setComboRowEllipsis(selfImp.resetWiFiDevice.get());
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for WifiBox {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scanForWifi(_listeners: Arc<Listeners>, wifiBox: Arc<WifiBox>) {
|
||||
let wifibox_ref = wifiBox.clone();
|
||||
let _wifibox_ref_listener = wifiBox.clone();
|
||||
|
@ -116,7 +122,8 @@ pub fn dbus_start_network_events() {
|
|||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(1000),
|
||||
);
|
||||
let _: Result<(), Error> = proxy.method_call("org.Xetibo.ReSetWireless", "StartNetworkListener", ());
|
||||
let _: Result<(), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetWireless", "StartNetworkListener", ());
|
||||
}
|
||||
|
||||
pub fn get_access_points() -> Vec<AccessPoint> {
|
||||
|
@ -142,8 +149,7 @@ pub fn get_stored_connections() -> Vec<(Path<'static>, Vec<u8>)> {
|
|||
"/org/Xetibo/ReSetDaemon",
|
||||
Duration::from_millis(1000),
|
||||
);
|
||||
let res: Result<(Vec<(Path<'static>, Vec<u8>)>,), Error> =
|
||||
proxy.method_call("org.Xetibo.ReSetWireless", "ListStoredConnections", ());
|
||||
let res: ResultMap = proxy.method_call("org.Xetibo.ReSetWireless", "ListStoredConnections", ());
|
||||
if res.is_err() {
|
||||
println!("we got error...");
|
||||
return Vec::new();
|
||||
|
@ -162,7 +168,7 @@ pub fn start_event_listener(listeners: Arc<Listeners>, wifi_box: Arc<WifiBox>) {
|
|||
let conn = Connection::new_session().unwrap();
|
||||
let added_ref = wifi_box.clone();
|
||||
let removed_ref = wifi_box.clone();
|
||||
let changed_ref = wifi_box.clone(); // TODO implement changed
|
||||
let changed_ref = wifi_box.clone();
|
||||
let access_point_added = AccessPointAdded::match_rule(
|
||||
Some(&"org.Xetibo.ReSetDaemon".into()),
|
||||
Some(&Path::from("/org/Xetibo/ReSetDaemon")),
|
||||
|
|
|
@ -20,6 +20,9 @@ glib::wrapper! {
|
|||
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
|
||||
}
|
||||
|
||||
unsafe impl Send for WifiOptions {}
|
||||
unsafe impl Sync for WifiOptions {}
|
||||
|
||||
impl WifiOptions {
|
||||
pub fn new(connection: Connection) -> Arc<Self> {
|
||||
let wifiOption: Arc<WifiOptions> = Arc::new(Object::builder().build());
|
||||
|
@ -34,8 +37,12 @@ impl WifiOptions {
|
|||
let conn = selfImp.connection.borrow();
|
||||
// General
|
||||
selfImp.resetWifiName.set_subtitle(&conn.settings.name);
|
||||
selfImp.resetWifiAutoConnect.set_active(conn.settings.autoconnect);
|
||||
selfImp.resetWifiMetered.set_active(conn.settings.metered != -1);
|
||||
selfImp
|
||||
.resetWifiAutoConnect
|
||||
.set_active(conn.settings.autoconnect);
|
||||
selfImp
|
||||
.resetWifiMetered
|
||||
.set_active(conn.settings.metered != -1);
|
||||
match &conn.device {
|
||||
TypeSettings::WIFI(wifi) => {
|
||||
selfImp.resetWifiLinkSpeed.set_visible(false);
|
||||
|
@ -44,7 +51,7 @@ impl WifiOptions {
|
|||
selfImp.resetWifiDNS.set_visible(false);
|
||||
selfImp.resetWifiGateway.set_visible(false);
|
||||
selfImp.resetWifiLastUsed.set_visible(true);
|
||||
selfImp.resetWifiMac.set_subtitle(&*wifi.cloned_mac_address);
|
||||
selfImp.resetWifiMac.set_subtitle(&wifi.cloned_mac_address);
|
||||
}
|
||||
TypeSettings::ETHERNET(ethernet) => {
|
||||
selfImp.resetWifiLinkSpeed.set_visible(true);
|
||||
|
@ -53,17 +60,26 @@ impl WifiOptions {
|
|||
selfImp.resetWifiDNS.set_visible(true);
|
||||
selfImp.resetWifiGateway.set_visible(true);
|
||||
selfImp.resetWifiLastUsed.set_visible(false);
|
||||
selfImp.resetWifiMac.set_subtitle(&*ethernet.cloned_mac_address);
|
||||
selfImp.resetWifiLinkSpeed.set_subtitle(&*ethernet.speed.to_string());
|
||||
selfImp
|
||||
.resetWifiMac
|
||||
.set_subtitle(ðernet.cloned_mac_address);
|
||||
selfImp
|
||||
.resetWifiLinkSpeed
|
||||
.set_subtitle(ðernet.speed.to_string());
|
||||
}
|
||||
TypeSettings::VPN(_vpn) => {}
|
||||
TypeSettings::None => {}
|
||||
};
|
||||
// IPv4
|
||||
selfImp.resetIP4Method.set_selected(conn.ipv4.dns_method.to_i32() as u32);
|
||||
selfImp
|
||||
.resetIP4Method
|
||||
.set_selected(conn.ipv4.dns_method.to_i32() as u32);
|
||||
self.setIP4Visibility(conn.ipv4.dns_method.to_i32() as u32);
|
||||
|
||||
let ipv4Dns: Vec<String> = conn.ipv4.dns.iter()
|
||||
let ipv4Dns: Vec<String> = conn
|
||||
.ipv4
|
||||
.dns
|
||||
.iter()
|
||||
.map(|addr| {
|
||||
addr.iter()
|
||||
.map(|octet| octet.to_string())
|
||||
|
@ -75,10 +91,14 @@ impl WifiOptions {
|
|||
selfImp.resetIP4Gateway.set_text(&conn.ipv4.gateway);
|
||||
|
||||
if conn.ipv4.address_data.is_empty() {
|
||||
selfImp.resetIP4AddressGroup.add(&WifiAddressEntry::new(None))
|
||||
selfImp
|
||||
.resetIP4AddressGroup
|
||||
.add(&WifiAddressEntry::new(None))
|
||||
} else {
|
||||
for address in conn.ipv4.address_data.iter() {
|
||||
selfImp.resetIP4AddressGroup.add(&WifiAddressEntry::new(Some(address)))
|
||||
selfImp
|
||||
.resetIP4AddressGroup
|
||||
.add(&WifiAddressEntry::new(Some(address)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -86,14 +106,21 @@ impl WifiOptions {
|
|||
selfImp.resetIP4RoutesGroup.add(&WifiRouteEntry::new(None))
|
||||
} else {
|
||||
for address in conn.ipv4.route_data.iter() {
|
||||
selfImp.resetIP4RoutesGroup.add(&WifiRouteEntry::new(Some(address)))
|
||||
selfImp
|
||||
.resetIP4RoutesGroup
|
||||
.add(&WifiRouteEntry::new(Some(address)))
|
||||
}
|
||||
}
|
||||
// IPv6
|
||||
selfImp.resetIP6Method.set_selected(conn.ipv6.dns_method.to_i32() as u32);
|
||||
selfImp
|
||||
.resetIP6Method
|
||||
.set_selected(conn.ipv6.dns_method.to_i32() as u32);
|
||||
self.setIP6Visibility(conn.ipv6.dns_method.to_i32() as u32);
|
||||
|
||||
let ipv6Dns: Vec<String> = conn.ipv6.dns.iter()
|
||||
let ipv6Dns: Vec<String> = conn
|
||||
.ipv6
|
||||
.dns
|
||||
.iter()
|
||||
.map(|addr| {
|
||||
addr.iter()
|
||||
.map(|octet| octet.to_string())
|
||||
|
@ -105,10 +132,14 @@ impl WifiOptions {
|
|||
selfImp.resetIP6Gateway.set_text(&conn.ipv6.gateway);
|
||||
|
||||
if conn.ipv6.address_data.is_empty() {
|
||||
selfImp.resetIP6AddressGroup.add(&WifiAddressEntry::new(None))
|
||||
selfImp
|
||||
.resetIP6AddressGroup
|
||||
.add(&WifiAddressEntry::new(None))
|
||||
} else {
|
||||
for address in conn.ipv6.address_data.iter() {
|
||||
selfImp.resetIP6AddressGroup.add(&WifiAddressEntry::new(Some(address)))
|
||||
selfImp
|
||||
.resetIP6AddressGroup
|
||||
.add(&WifiAddressEntry::new(Some(address)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,7 +147,9 @@ impl WifiOptions {
|
|||
selfImp.resetIP6RoutesGroup.add(&WifiRouteEntry::new(None))
|
||||
} else {
|
||||
for address in conn.ipv6.route_data.iter() {
|
||||
selfImp.resetIP6RoutesGroup.add(&WifiRouteEntry::new(Some(address)))
|
||||
selfImp
|
||||
.resetIP6RoutesGroup
|
||||
.add(&WifiRouteEntry::new(Some(address)))
|
||||
}
|
||||
}
|
||||
// Security
|
||||
|
@ -126,12 +159,14 @@ impl WifiOptions {
|
|||
pub fn setIP4Visibility(&self, method: u32) {
|
||||
let selfImp = self.imp();
|
||||
match method {
|
||||
0 => { // auto
|
||||
0 => {
|
||||
// auto
|
||||
selfImp.resetIP4AddressGroup.set_visible(false);
|
||||
selfImp.resetIP4RoutesGroup.set_visible(true);
|
||||
selfImp.resetIP4Gateway.set_visible(false);
|
||||
}
|
||||
1 => { // manual
|
||||
1 => {
|
||||
// manual
|
||||
selfImp.resetIP4AddressGroup.set_visible(true);
|
||||
selfImp.resetIP4RoutesGroup.set_visible(true);
|
||||
selfImp.resetIP4Gateway.set_visible(true);
|
||||
|
@ -147,12 +182,14 @@ impl WifiOptions {
|
|||
pub fn setIP6Visibility(&self, method: u32) {
|
||||
let selfImp = self.imp();
|
||||
match method {
|
||||
0 | 1 => { // auto, dhcp
|
||||
0 | 1 => {
|
||||
// auto, dhcp
|
||||
selfImp.resetIP6AddressGroup.set_visible(false);
|
||||
selfImp.resetIP6RoutesGroup.set_visible(true);
|
||||
selfImp.resetIP6Gateway.set_visible(false);
|
||||
}
|
||||
2 => { // manual
|
||||
2 => {
|
||||
// manual
|
||||
selfImp.resetIP6AddressGroup.set_visible(true);
|
||||
selfImp.resetIP6RoutesGroup.set_visible(true);
|
||||
selfImp.resetIP6Gateway.set_visible(true);
|
||||
|
@ -170,55 +207,57 @@ fn setupCallbacks(wifiOptions: &Arc<WifiOptions>) {
|
|||
let imp = wifiOptions.imp();
|
||||
|
||||
// General
|
||||
imp.resetWifiAutoConnect.connect_active_notify(clone!(@weak imp => move |x| {
|
||||
imp.connection.borrow_mut().settings.autoconnect = x.is_active();
|
||||
}));
|
||||
imp.resetWifiMetered.connect_active_notify(clone!(@weak imp => move |x| {
|
||||
imp.connection.borrow_mut().settings.metered = if x.is_active() { 1 } else { 2 };
|
||||
}));
|
||||
imp.resetWifiAutoConnect
|
||||
.connect_active_notify(clone!(@weak imp => move |x| {
|
||||
imp.connection.borrow_mut().settings.autoconnect = x.is_active();
|
||||
}));
|
||||
imp.resetWifiMetered
|
||||
.connect_active_notify(clone!(@weak imp => move |x| {
|
||||
imp.connection.borrow_mut().settings.metered = if x.is_active() { 1 } else { 2 };
|
||||
}));
|
||||
// IPv4
|
||||
let wifiOptionsIP4 = wifiOptions.clone();
|
||||
imp.resetIP4Method.connect_selected_notify(clone!(@weak imp => move |dropdown| {
|
||||
let selected = dropdown.selected();
|
||||
wifiOptionsIP4.setIP4Visibility(selected);
|
||||
}));
|
||||
imp.resetIP4Method
|
||||
.connect_selected_notify(clone!(@weak imp => move |dropdown| {
|
||||
let selected = dropdown.selected();
|
||||
wifiOptionsIP4.setIP4Visibility(selected);
|
||||
}));
|
||||
|
||||
let dnsRegex = Regex::new(r"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$").unwrap();
|
||||
imp.resetIP4DNS.connect_changed(clone!(@weak imp => move |entry| {
|
||||
let dnsInput = entry.text();
|
||||
let mut conn = imp.connection.borrow_mut();
|
||||
conn.ipv4.dns.clear();
|
||||
if dnsInput.as_str().is_empty() {
|
||||
imp.resetIP4DNS.remove_css_class("error");
|
||||
return;
|
||||
}
|
||||
for dnsEntry in dnsInput.as_str().split(',').collect::<Vec<&str>>() {
|
||||
if dnsRegex.is_match(dnsEntry) {
|
||||
imp.resetIP4DNS
|
||||
.connect_changed(clone!(@weak imp => move |entry| {
|
||||
let dnsInput = entry.text();
|
||||
let mut conn = imp.connection.borrow_mut();
|
||||
conn.ipv4.dns.clear();
|
||||
if dnsInput.as_str().is_empty() {
|
||||
imp.resetIP4DNS.remove_css_class("error");
|
||||
let dnsParts = dnsEntry.split('.')
|
||||
.map(|s| s.parse::<u8>().unwrap())
|
||||
.collect::<Vec<u8>>();
|
||||
conn.ipv4.dns.push(dnsParts);
|
||||
} else {
|
||||
imp.resetIP4DNS.add_css_class("error");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}));
|
||||
for dnsEntry in dnsInput.as_str().split(',').collect::<Vec<&str>>() {
|
||||
if dnsRegex.is_match(dnsEntry) {
|
||||
imp.resetIP4DNS.remove_css_class("error");
|
||||
let dnsParts = dnsEntry.split('.')
|
||||
.map(|s| s.parse::<u8>().unwrap())
|
||||
.collect::<Vec<u8>>();
|
||||
conn.ipv4.dns.push(dnsParts);
|
||||
} else {
|
||||
imp.resetIP4DNS.add_css_class("error");
|
||||
}
|
||||
}
|
||||
}));
|
||||
// IPv6
|
||||
let wifiOptionsIP6 = wifiOptions.clone();
|
||||
imp.resetIP6Method.connect_selected_notify(clone!(@weak imp => move |dropdown| {
|
||||
let selected = dropdown.selected();
|
||||
wifiOptionsIP6.setIP6Visibility(selected);
|
||||
}));
|
||||
imp.resetIP6Method
|
||||
.connect_selected_notify(clone!(@weak imp => move |dropdown| {
|
||||
let selected = dropdown.selected();
|
||||
wifiOptionsIP6.setIP6Visibility(selected);
|
||||
}));
|
||||
// Security
|
||||
}
|
||||
|
||||
pub fn getValueFromKey(map: &PropMap, key: &str) -> String {
|
||||
map.get(key)
|
||||
.map_or_else(|| "".to_string(),
|
||||
|value| value.0
|
||||
.as_str()
|
||||
.unwrap_or_default()
|
||||
.trim()
|
||||
.to_string())
|
||||
map.get(key).map_or_else(
|
||||
|| "".to_string(),
|
||||
|value| value.0.as_str().unwrap_or_default().trim().to_string(),
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue