feat: Add saved WifiList

This commit is contained in:
Fabio Lenherr / DashieTM 2023-11-12 03:41:29 +01:00
parent bbfd07688d
commit 9cad376a40
13 changed files with 372 additions and 126 deletions

View file

@ -9,7 +9,8 @@ use gtk::gdk_pixbuf::subclass::prelude::{
use gtk::prelude::PopupExt;
use gtk::subclass::prelude::*;
use gtk::{
gdk, glib, Button, CompositeTemplate, Entry, EntryBuffer, PasswordEntry, PasswordEntryBuffer, Popover,
gdk, glib, Button, CompositeTemplate, Entry, EntryBuffer, Label, PasswordEntry,
PasswordEntryBuffer, Popover,
};
use super::popup;
@ -18,6 +19,8 @@ use super::popup;
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/Xetibo/ReSet/resetPopup.ui")]
pub struct Popup {
#[template_child]
pub resetPopupLabel: TemplateChild<Label>,
#[template_child]
pub resetPopupEntry: TemplateChild<PasswordEntry>,
#[template_child]
@ -25,6 +28,9 @@ pub struct Popup {
pub resetPopupText: Arc<RefCell<PasswordEntryBuffer>>,
}
unsafe impl Send for Popup {}
unsafe impl Sync for Popup {}
#[glib::object_subclass]
impl ObjectSubclass for Popup {
const NAME: &'static str = "resetPopup";

View file

@ -1,5 +1,7 @@
#![allow(non_snake_case)]
pub mod savedWifiEntry;
pub mod wifiBox;
pub mod wifiBoxImpl;
pub mod wifiEntry;
pub mod wifiEntryImpl;
pub mod wifiEntryImpl;
pub mod savedWifiEntryImpl;

View file

@ -0,0 +1,46 @@
use std::sync::Arc;
use std::time::Duration;
use crate::components::wifi::savedWifiEntryImpl;
use adw::glib;
use adw::glib::Object;
use adw::prelude::{ButtonExt, WidgetExt};
use dbus::{Error, Path};
use dbus::blocking::Connection;
use glib::{clone, PropertySet};
use glib::subclass::types::ObjectSubclassIsExt;
glib::wrapper! {
pub struct SavedWifiEntry(ObjectSubclass<savedWifiEntryImpl::SavedWifiEntry>)
@extends gtk::Box, gtk::Widget,
@implements gtk::Accessible, gtk::Buildable, gtk::Actionable, gtk::ConstraintTarget;
}
impl SavedWifiEntry {
pub fn new(name: &String, path: Path<'static>) -> Self {
let entry: SavedWifiEntry = Object::builder().build();
let entryImp = entry.imp();
// TODO handle edit
entryImp.resetSavedWifiLabel.set_text(name);
entryImp.resetConnectionPath.set(path);
entryImp.resetDeleteSavedWifiButton.connect_clicked(clone!(@weak entry as entry => move |_| {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
"org.xetibo.ReSet",
"/org/xetibo/ReSet",
Duration::from_millis(1000),
);
let res: Result<(bool,), Error> = proxy.method_call("org.xetibo.ReSet", "DeleteConnection", (entry.imp().resetConnectionPath.take(),));
if res.is_err() || res.unwrap() == (false,) {
// TODO handle error -> inform user
println!("no worky");
return;
}
println!("worked, should be ded");
let parent = entry.parent().unwrap();
parent.set_visible(false);
parent.unparent();
}));
entry
}
}

View file

@ -0,0 +1,52 @@
use std::cell::RefCell;
use dbus::Path;
use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate, Label};
use super::savedWifiEntry;
#[allow(non_snake_case)]
#[derive(Default, CompositeTemplate)]
#[template(resource = "/org/Xetibo/ReSet/resetSavedWifiEntry.ui")]
pub struct SavedWifiEntry {
#[template_child]
pub resetDeleteSavedWifiButton: TemplateChild<Button>,
#[template_child]
pub resetEditSavedWifiButton: TemplateChild<Button>,
#[template_child]
pub resetSavedWifiLabel: TemplateChild<Label>,
pub resetConnectionPath: RefCell<Path<'static>>,
}
unsafe impl Send for SavedWifiEntry {}
unsafe impl Sync for SavedWifiEntry {}
#[glib::object_subclass]
impl ObjectSubclass for SavedWifiEntry {
const NAME: &'static str = "resetSavedWifiEntry";
type Type = savedWifiEntry::SavedWifiEntry;
type ParentType = gtk::Box;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
}
fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for SavedWifiEntry {
fn constructed(&self) {
self.parent_constructed();
}
}
impl BoxImpl for SavedWifiEntry {}
impl WidgetImpl for SavedWifiEntry {}
impl WindowImpl for SavedWifiEntry {}
impl ApplicationWindowImpl for SavedWifiEntry {}

View file

@ -6,12 +6,14 @@ use std::time::Duration;
use crate::components::base::listEntry::ListEntry;
use adw::glib;
use adw::glib::Object;
use adw::prelude::{BoxExt, ButtonExt, ListBoxRowExt};
use adw::subclass::prelude::ObjectSubclassIsExt;
use dbus::blocking::Connection;
use dbus::Error;
use dbus::Path;
use gtk::glib::{clone, Variant};
use gtk::prelude::ActionableExt;
use gtk::{Button, Label, Orientation};
use ReSet_Lib::network::network::{AccessPoint, WifiStrength};
use ReSet_Lib::signals::{
AccessPointAdded, AccessPointRemoved, BluetoothDeviceAdded, BluetoothDeviceRemoved,
@ -21,6 +23,8 @@ use ReSet_Lib::utils::Events;
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,
@ -46,18 +50,18 @@ impl WifiBox {
.set_action_target_value(Some(&Variant::from("saved")));
}
pub fn donotdisturb() {
thread::spawn(|| {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
"org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
Duration::from_millis(1000),
);
let _: Result<(), Error> =
proxy.method_call("org.freedesktop.Notifications", "DoNotDisturb", ());
});
}
// pub fn donotdisturb() {
// thread::spawn(|| {
// let conn = Connection::new_session().unwrap();
// let proxy = conn.with_proxy(
// "org.freedesktop.Notifications",
// "/org/freedesktop/Notifications",
// Duration::from_millis(1000),
// );
// let _: Result<(), Error> =
// proxy.method_call("org.freedesktop.Notifications", "DoNotDisturb", ());
// });
// }
}
pub fn scanForWifi(wifiBox: Arc<WifiBox>) {
@ -65,7 +69,7 @@ pub fn scanForWifi(wifiBox: Arc<WifiBox>) {
let wifiEntries = wifiBox.imp().wifiEntries.clone();
glib::spawn_future_local(async move {
let accessPoints = wat().await;
let accessPoints = get_access_points().await;
let wifiEntries = wifiEntries.clone();
{
let mut wifiEntries = wifiEntries.lock().unwrap();
@ -110,7 +114,36 @@ pub fn scanForWifi(wifiBox: Arc<WifiBox>) {
});
}
pub async fn wat() -> Vec<AccessPoint> {
pub fn show_stored_connections(wifiBox: Arc<WifiBox>) {
let wifibox_ref = wifiBox.clone();
let wifiEntries = wifiBox.imp().savedWifiEntries.clone();
glib::spawn_future_local(async move {
let connections = get_stored_connections().await;
let wifiEntries = wifiEntries.clone();
{
let mut wifiEntries = wifiEntries.lock().unwrap();
for connection in connections {
// TODO include button for settings
let name = &String::from_utf8(connection.1).unwrap_or_else(|_| String::from(""));
let entry = ListEntry::new(&SavedWifiEntry::new(name, connection.0));
entry.set_activatable(false);
wifiEntries.push(entry);
}
}
glib::MainContext::default().spawn_local(async move {
glib::idle_add_once(move || {
let wifiEntries = wifiEntries.lock().unwrap();
let selfImp = wifibox_ref.imp();
for wifiEntry in wifiEntries.iter() {
selfImp.resetStoredWifiList.append(wifiEntry);
}
});
});
});
}
pub async fn get_access_points() -> Vec<AccessPoint> {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
"org.xetibo.ReSet",
@ -125,3 +158,21 @@ pub async fn wat() -> Vec<AccessPoint> {
let (accessPoints,) = res.unwrap();
accessPoints
}
pub async fn get_stored_connections() -> Vec<(Path<'static>, Vec<u8>)> {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
"org.xetibo.ReSet",
"/org/xetibo/ReSet",
Duration::from_millis(1000),
);
let res: Result<(Vec<(Path<'static>, Vec<u8>)>,), Error> =
proxy.method_call("org.xetibo.ReSet", "ListStoredConnections", ());
if res.is_err() {
println!("we got error...");
return Vec::new();
}
let (connections,) = res.unwrap();
dbg!(connections.clone());
connections
}

View file

@ -26,7 +26,10 @@ pub struct WifiBox {
pub resetWifiList: TemplateChild<ListBox>,
#[template_child]
pub resetWifiAdvanced: TemplateChild<Button>,
#[template_child]
pub resetStoredWifiList: TemplateChild<ListBox>,
pub wifiEntries: Arc<Mutex<Vec<ListEntry>>>,
pub savedWifiEntries: Arc<Mutex<Vec<ListEntry>>>,
}
unsafe impl Send for WifiBox {}

View file

@ -141,38 +141,54 @@ pub fn click_stored_network(entry: Arc<WifiEntry>) {
pub fn click_new_network(entry: Arc<WifiEntry>) {
let connect_new_network =
|entry: Arc<WifiEntry>, access_point: AccessPoint, password: String| -> bool {
let alert = AlertDialog::builder().build();
let root = &entry.root().unwrap();
let root = root.downcast_ref::<gtk::Window>();
if root.is_none() {
println!("ERROR BRO");
return false;
}
let root = root.unwrap();
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
"org.xetibo.ReSet",
"/org/xetibo/ReSet",
Duration::from_millis(10000),
);
|result: Arc<AtomicBool>, entry: Arc<WifiEntry>, access_point: AccessPoint, password: String| {
let entry_ref = entry.clone();
let popup = entry.imp().resetWifiPopup.imp();
popup.resetPopupLabel.set_text("Connecting...");
popup.resetPopupLabel.set_visible(true);
popup.resetPopupEntry.set_sensitive(false);
popup.resetPopupButton.set_sensitive(false);
glib::spawn_future_local(async move {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
"org.xetibo.ReSet",
"/org/xetibo/ReSet",
Duration::from_millis(10000),
);
let res: Result<(bool,), Error> = proxy.method_call(
"org.xetibo.ReSet",
"ConnectToNewAccessPoint",
(access_point, password),
);
glib::MainContext::default().spawn_local(async move {
glib::idle_add_once(move || {
if res.is_err() {
entry_ref
.imp()
.resetWifiPopup
.imp()
.resetPopupLabel
.set_text("Could not connect to dbus.");
result.store(false, std::sync::atomic::Ordering::SeqCst);
return;
}
if res.unwrap() == (false,) {
entry_ref
.imp()
.resetWifiPopup
.imp()
.resetPopupLabel
.set_text("Could not connect to access point.");
result.store(false, std::sync::atomic::Ordering::SeqCst);
return;
}
entry_ref.imp().resetWifiPopup.popdown();
result.store(true, std::sync::atomic::Ordering::SeqCst);
});
});
});
// TODO crate spinner animation and block UI
let res: Result<(bool,), Error> = proxy.method_call(
"org.xetibo.ReSet",
"ConnectToNewAccessPoint",
(access_point, password),
);
if res.is_err() {
alert.set_message("Could not connect to dbus.");
alert.show(Some(root));
return false;
}
if res.unwrap() == (false,) {
alert.set_message("Could not connect to access point.");
alert.show(Some(root));
return false;
}
true
};
let result = Arc::new(AtomicBool::new(false));
@ -183,22 +199,14 @@ pub fn click_new_network(entry: Arc<WifiEntry>) {
popupImp
.resetPopupEntry
.connect_activate(clone!(@weak entry as origEntry, @weak entryImp => move |entry| {
result_ref.store(
connect_new_network(origEntry, entryImp.accessPoint.clone().take(), entry.text().to_string()),
std::sync::atomic::Ordering::SeqCst,
);
entryImp.resetWifiPopup.popdown();
connect_new_network(result_ref.clone(), origEntry, entryImp.accessPoint.clone().take(), entry.text().to_string());
}));
popupImp
.resetPopupButton
.connect_clicked(clone!(@weak entry as origEntry,@weak entryImp, @weak popupImp => move |_| {
popupImp.resetPopupButton.connect_clicked(
clone!(@weak entry as origEntry,@weak entryImp, @weak popupImp => move |_| {
let entry = entryImp.resetWifiPopup.imp().resetPopupEntry.text().to_string();
result_ref_button.store(
connect_new_network(origEntry, entryImp.accessPoint.clone().take(), entry),
std::sync::atomic::Ordering::SeqCst,
);
entryImp.resetWifiPopup.popdown();
}));
connect_new_network(result_ref_button.clone(), origEntry, entryImp.accessPoint.clone().take(), entry);
}),
);
entryImp.resetWifiPopup.popup();
println!(
"result is {}",

View file

@ -1,13 +1,17 @@
use std::sync::Arc;
use gtk::{FlowBox, Label};
use crate::components::audio::audioBox::AudioBox;
use crate::components::bluetooth::bluetoothBox::BluetoothBox;
use crate::components::base::settingBox::SettingBox;
use crate::components::wifi::wifiBox::{WifiBox, scanForWifi};
use crate::components::bluetooth::bluetoothBox::BluetoothBox;
use crate::components::wifi::wifiBox::{scanForWifi, show_stored_connections, WifiBox};
use adw::prelude::ButtonExt;
use glib::clone;
use glib::subclass::types::ObjectSubclassIsExt;
use gtk::{FlowBox, Label};
pub const HANDLE_CONNECTIVITY_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_CONNECTIVITY_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let wifiBox = Arc::new(WifiBox::new());
show_stored_connections(wifiBox.clone());
scanForWifi(wifiBox.clone());
let wifiBox = SettingBox::new(&*wifiBox);
let bluetoothBox = SettingBox::new(&BluetoothBox::new());
@ -17,74 +21,76 @@ pub const HANDLE_CONNECTIVITY_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
resetMain.set_max_children_per_line(2);
};
pub const HANDLE_WIFI_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let wifiBox = SettingBox::new(&WifiBox::new());
pub const HANDLE_WIFI_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let wifiBox = Arc::new(WifiBox::new());
scanForWifi(wifiBox.clone());
let wifiBox = SettingBox::new(&*wifiBox);
resetMain.remove_all();
resetMain.insert(&wifiBox, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_BLUETOOTH_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_BLUETOOTH_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let bluetoothBox = SettingBox::new(&BluetoothBox::new());
resetMain.remove_all();
resetMain.insert(&bluetoothBox, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_VPN_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_VPN_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let label = Label::new(Some("not implemented yet"));
resetMain.remove_all();
resetMain.insert(&label, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_AUDIO_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_AUDIO_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let audioBox = AudioBox::new();
resetMain.remove_all();
resetMain.insert(&audioBox, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_VOLUME_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_VOLUME_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let audioBox = AudioBox::new();
resetMain.remove_all();
resetMain.insert(&audioBox, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_MICROPHONE_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_MICROPHONE_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let label = Label::new(Some("not implemented yet"));
resetMain.remove_all();
resetMain.insert(&label, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_HOME: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_HOME: fn(FlowBox) = |resetMain: FlowBox| {
resetMain.remove_all();
};
pub const HANDLE_PERIPHERALS_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_PERIPHERALS_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let label = Label::new(Some("not implemented yet"));
resetMain.remove_all();
resetMain.insert(&label, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_MONITOR_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_MONITOR_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let label = Label::new(Some("not implemented yet"));
resetMain.remove_all();
resetMain.insert(&label, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_MOUSE_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_MOUSE_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let label = Label::new(Some("not implemented yet"));
resetMain.remove_all();
resetMain.insert(&label, -1);
resetMain.set_max_children_per_line(1);
};
pub const HANDLE_KEYBOARD_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
pub const HANDLE_KEYBOARD_CLICK: fn(FlowBox) = |resetMain: FlowBox| {
let label = Label::new(Some("not implemented yet"));
resetMain.remove_all();
resetMain.insert(&label, -1);