fix: Properly handle wifi and bluetooth turned on and off

This commit is contained in:
Fabio Lenherr / DashieTM 2023-12-19 21:38:35 +01:00
parent e1be42a55d
commit 8796abb08f
9 changed files with 47 additions and 27 deletions

View file

@ -1,13 +1,13 @@
[package]
name = "reset"
version = "0.1.6"
version = "0.1.8"
edition = "2021"
description = "A wip universal Linux settings application."
repository = "https://github.com/Xetibo/ReSet"
license = "GPL-3.0-only"
[dependencies]
reset_daemon = "0.7.1"
reset_daemon = "0.7.2"
re_set-lib = "0.8.7"
adw = { version = "0.5.3", package = "libadwaita", features = ["v1_4"] }
dbus = "0.9.7"

View file

@ -1,7 +1,7 @@
# Maintainer: Fabio Lenherr <dashie@dashie.org>
pkgname=reset
pkgver=0.1.6
pkgver=0.1.8
pkgrel=0
arch=('x86_64')
pkgdir="/usr/bin/${pkgname}"

View file

@ -9,7 +9,7 @@ use adw::subclass::prelude::ObjectSubclassIsExt;
use dbus::blocking::Connection;
use dbus::message::SignalArgs;
use dbus::{Error, Path};
use glib::{clone, Cast};
use glib::{clone, Cast, PropertySet};
use gtk::glib::Variant;
use gtk::prelude::{ActionableExt, ButtonExt, ListBoxRowExt, WidgetExt};
use gtk::{gio, StringObject};
@ -48,6 +48,7 @@ fn setup_callbacks(
let bluetooth_box_ref = bluetooth_box.clone();
let listeners_ref = listeners.clone();
let imp = bluetooth_box.imp();
imp.reset_switch_initial.set(true);
imp.reset_visibility.set_activatable(true);
imp.reset_visibility
.set_action_name(Some("navigation.push"));
@ -77,11 +78,15 @@ fn setup_callbacks(
set_bluetooth_adapter_pairability(imp.reset_current_bluetooth_adapter.borrow().path.clone(), state.is_active());
}));
imp.reset_bluetooth_switch
.connect_state_set(move |_, state| {
imp.reset_bluetooth_switch.connect_state_set(
clone!(@weak imp => @default-return glib::Propagation::Proceed, move |_, state| {
if imp.reset_switch_initial.load(Ordering::SeqCst) {
return glib::Propagation::Proceed;
}
if !state {
let imp = bluetooth_box_ref.imp();
let mut available_devices = imp.available_devices.borrow_mut();
let mut current_adapter = imp.reset_current_bluetooth_adapter.borrow_mut();
for entry in available_devices.iter() {
imp.reset_bluetooth_available_devices.remove(&**entry.1);
}
@ -102,10 +107,13 @@ fn setup_callbacks(
listeners_ref
.bluetooth_listener
.store(false, Ordering::SeqCst);
set_adapter_enabled(
imp.reset_current_bluetooth_adapter.borrow().path.clone(),
let res = set_adapter_enabled(
current_adapter.path.clone(),
false,
);
if res {
current_adapter.powered = false;
}
} else {
let restart_ref = bluetooth_box_ref.clone();
let restart_listener_ref = listeners_ref.clone();
@ -115,21 +123,21 @@ fn setup_callbacks(
imp.reset_bluetooth_pairable_switch.set_sensitive(true);
}
gio::spawn_blocking(move || {
let mut current_adapter = restart_ref.imp().reset_current_bluetooth_adapter.borrow_mut();
if set_adapter_enabled(
restart_ref
.imp()
.reset_current_bluetooth_adapter
.borrow()
current_adapter
.path
.clone(),
true,
) {
current_adapter.powered = true;
start_bluetooth_listener(restart_listener_ref.clone(), restart_ref.clone());
}
});
}
glib::Propagation::Proceed
});
}),
);
bluetooth_box
}
@ -169,12 +177,14 @@ pub fn populate_conntected_bluetooth_devices(bluetooth_box: Arc<BluetoothBox>) {
{
let current_adapter = imp.reset_current_bluetooth_adapter.borrow();
imp.reset_bluetooth_switch
.set_state(current_adapter.powered);
let powered = current_adapter.powered;
imp.reset_bluetooth_switch.set_state(powered);
imp.reset_bluetooth_switch.set_active(powered);
imp.reset_bluetooth_discoverable_switch
.set_active(current_adapter.discoverable);
imp.reset_bluetooth_pairable_switch
.set_active(current_adapter.pairable);
imp.reset_switch_initial.set(false);
}
imp.reset_bluetooth_adapter.connect_selected_notify(
@ -219,6 +229,11 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
if listeners.bluetooth_listener.load(Ordering::SeqCst) {
return;
}
let imp = bluetooth_box.imp();
if !imp.reset_current_bluetooth_adapter.borrow().powered {
return;
}
let device_added_box = bluetooth_box.clone();
let device_removed_box = bluetooth_box.clone();
@ -228,9 +243,7 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(1000));
let _: Result<(), Error> = proxy.method_call(BLUETOOTH, "StartBluetoothListener", ());
loop_box
.imp()
.reset_bluetooth_available_devices
imp.reset_bluetooth_available_devices
.set_description(Some("Scanning..."));
let device_added =
BluetoothDeviceAdded::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH)))

View file

@ -6,6 +6,7 @@ use gtk::{prelude::*, StringList};
use re_set_lib::bluetooth::bluetooth_structures::BluetoothAdapter;
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, RwLock};
use crate::components::base::list_entry::ListEntry;
@ -41,6 +42,7 @@ pub struct BluetoothBox {
pub reset_current_bluetooth_adapter: Arc<RefCell<BluetoothAdapter>>,
pub reset_model_list: Arc<RwLock<StringList>>,
pub reset_model_index: Arc<RwLock<u32>>,
pub reset_switch_initial: AtomicBool,
}
#[glib::object_subclass]

View file

@ -50,6 +50,7 @@ impl WifiBox {
fn setup_callbacks(listeners: Arc<Listeners>, wifi_box: Arc<WifiBox>) -> Arc<WifiBox> {
let imp = wifi_box.imp();
let wifibox_ref = wifi_box.clone();
imp.reset_switch_initial.set(true);
imp.reset_saved_networks.set_activatable(true);
imp.reset_saved_networks
.set_action_name(Some("navigation.push"));
@ -62,6 +63,9 @@ fn setup_callbacks(listeners: Arc<Listeners>, wifi_box: Arc<WifiBox>) -> Arc<Wif
set_combo_row_ellipsis(imp.reset_wifi_device.get());
imp.reset_wifi_switch.connect_state_set(
clone!(@weak imp => @default-return glib::Propagation::Proceed, move |_, value| {
if imp.reset_switch_initial.load(Ordering::SeqCst) {
return glib::Propagation::Proceed;
}
set_wifi_enabled(value);
if !value {
imp.reset_wifi_devices.write().unwrap().clear();
@ -93,17 +97,17 @@ pub fn scan_for_wifi(wifi_box: Arc<WifiBox>) {
let wifi_entries_path = wifi_box.imp().wifi_entries_path.clone();
gio::spawn_blocking(move || {
let devices = get_wifi_devices();
let access_points = get_access_points();
let wifi_status = get_wifi_status();
let devices = get_wifi_devices();
if devices.is_empty() {
return;
}
let access_points = get_access_points();
{
let imp = wifibox_ref.imp();
let list = imp.reset_model_list.write().unwrap();
let mut model_index = imp.reset_model_index.write().unwrap();
let mut map = imp.reset_wifi_devices.write().unwrap();
if devices.is_empty() {
return;
}
imp.reset_current_wifi_device
.replace(devices.last().unwrap().clone());
for (index, device) in devices.into_iter().enumerate() {
@ -121,8 +125,9 @@ pub fn scan_for_wifi(wifi_box: Arc<WifiBox>) {
let mut wifi_entries_path = wifi_entries_path.write().unwrap();
let imp = wifibox_ref.imp();
imp.reset_wifi_switch.set_active(wifi_status);
imp.reset_wifi_switch.set_state(wifi_status);
imp.reset_wifi_switch.set_active(wifi_status);
imp.reset_switch_initial.set(false);
let list = imp.reset_model_list.read().unwrap();
imp.reset_wifi_device.set_model(Some(&*list));

View file

@ -7,6 +7,7 @@ use gtk::{prelude::*, StringList};
use re_set_lib::network::network_structures::WifiDevice;
use std::cell::RefCell;
use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use std::sync::{Arc, RwLock};
use crate::components::base::list_entry::ListEntry;
@ -37,6 +38,7 @@ pub struct WifiBox {
pub reset_current_wifi_device: Arc<RefCell<WifiDevice>>,
pub reset_model_list: Arc<RwLock<StringList>>,
pub reset_model_index: Arc<RwLock<u32>>,
pub reset_switch_initial: AtomicBool,
}
unsafe impl Send for WifiBox {}

View file

@ -103,7 +103,7 @@ impl ReSetWindow {
.license_type(gtk::License::Gpl30)
.website("https://github.com/Xetibo/ReSet")
.issue_url("https://github.com/Xetibo/ReSet/issues")
.version("0.1.6")
.version("0.1.8")
.transient_for(window)
.modal(true)
.copyright("© 2022-2023 Xetibo")

View file

@ -373,7 +373,6 @@
(4,183,"GtkLabel","label","WiFi",None,None,None,None,None,None,None,None,None),
(4,183,"GtkWidget","css-classes","resetSettingLabel",None,None,None,None,None,None,None,None,None),
(4,183,"GtkWidget","margin-start","5",None,None,None,None,None,None,None,None,None),
(4,184,"GtkSwitch","active","True",None,None,None,None,None,None,None,None,None),
(4,184,"GtkWidget","halign","end",None,None,None,None,None,None,None,None,None),
(4,184,"GtkWidget","hexpand","True",None,None,None,None,None,None,None,None,None),
(4,184,"GtkWidget","margin-end","5",None,None,None,None,None,None,None,None,None),

View file

@ -21,7 +21,6 @@
</child>
<child>
<object class="GtkSwitch" id="reset_wifi_switch">
<property name="active">True</property>
<property name="halign">end</property>
<property name="hexpand">True</property>
<property name="margin-end">5</property>