Skip to content

Commit

Permalink
fix: Use ssid over path for events
Browse files Browse the repository at this point in the history
  • Loading branch information
DashieTM committed Dec 16, 2023
1 parent 4a9f8bf commit c860fcd
Show file tree
Hide file tree
Showing 7 changed files with 532 additions and 46 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ repository = "https://github.com/Xetibo/ReSet"
license = "GPL-3.0-only"

[dependencies]
reset_daemon = "0.6.3"
re_set-lib = "0.8.5"
reset_daemon = "0.6.4"
re_set-lib = "0.8.6"
adw = { version = "0.5.3", package = "libadwaita", features = ["v1_4"] }
dbus = "0.9.7"
gtk = { version = "0.7.3", package = "gtk4", features = ["v4_12"] }
Expand Down
25 changes: 21 additions & 4 deletions src/components/bluetooth/bluetooth_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,18 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
return;
}

let device_added_box = bluetooth_box.clone();
let device_removed_box = bluetooth_box.clone();
let device_changed_box = bluetooth_box.clone();
let loop_box = bluetooth_box.clone();

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
.set_description(Some("Scanning..."));
let device_added =
BluetoothDeviceAdded::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH)))
.static_clone();
Expand All @@ -233,10 +242,6 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
let device_changed =
BluetoothDeviceChanged::match_rule(Some(&BASE.into()), Some(&Path::from(DBUS_PATH)))
.static_clone();
let device_added_box = bluetooth_box.clone();
let device_removed_box = bluetooth_box.clone();
let device_changed_box = bluetooth_box.clone();
let loop_box = bluetooth_box.clone();

let res = conn.add_match(device_added, move |ir: BluetoothDeviceAdded, _, _| {
let bluetooth_box = device_added_box.clone();
Expand Down Expand Up @@ -338,6 +343,10 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
if !listeners.bluetooth_listener.load(Ordering::SeqCst) {
let _: Result<(), Error> =
proxy.method_call(BLUETOOTH, "StopBluetoothListener", ());
loop_box
.imp()
.reset_bluetooth_available_devices
.set_description(None);
break;
}
if listener_active && time.elapsed().unwrap() > Duration::from_millis(10000) {
Expand All @@ -352,6 +361,10 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
});
});
let _: Result<(), Error> = proxy.method_call(BLUETOOTH, "StopBluetoothScan", ());
loop_box
.imp()
.reset_bluetooth_available_devices
.set_description(None);
}
if !listener_active && listeners.bluetooth_scan_requested.load(Ordering::SeqCst) {
listeners
Expand All @@ -360,6 +373,10 @@ pub fn start_bluetooth_listener(listeners: Arc<Listeners>, bluetooth_box: Arc<Bl
listener_active = true;
let _: Result<(), Error> =
proxy.method_call(BLUETOOTH, "StartBluetoothListener", ());
loop_box
.imp()
.reset_bluetooth_available_devices
.set_description(Some("Scanning..."));
time = SystemTime::now();
}
thread::sleep(Duration::from_millis(100));
Expand Down
17 changes: 10 additions & 7 deletions src/components/input/source_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ pub fn populate_sources(input_box: Arc<SourceBox>) {
for entry in 0..*index {
if model_list.string(entry) == Some(name.alias.clone().into()) {
input_box_imp.reset_source_dropdown.set_selected(entry);
}
return;
}
}
input_box_imp.reset_source_dropdown.connect_selected_notify(
clone!(@weak input_box_imp => move |dropdown| {
Expand Down Expand Up @@ -260,6 +261,7 @@ pub fn refresh_default_source(new_source: Source, input_box: Arc<SourceBox>, ent
for entry in 0..*imp.reset_model_index.read().unwrap() {
if model_list.string(entry) == Some(new_source.alias.clone().into()) {
imp.reset_source_dropdown.set_selected(entry);
return;
}
}
}
Expand Down Expand Up @@ -419,15 +421,16 @@ pub fn start_input_box_listener(conn: Connection, source_box: Arc<SourceBox>) ->
let mut index = input_box_imp.reset_model_index.write().unwrap();
let model_list = input_box_imp.reset_model_list.write().unwrap();
if model_list.string(*index - 1) == Some("Monitor of Dummy Output".into()) {
if alias == "Monitor of Dummy Output" {
return;
}
model_list.append(&alias);
model_list.remove(*index - 1);
map.insert(alias, (source_index, name));
input_box_imp.reset_source_dropdown.set_selected(0);
} else {
model_list.append(&alias);
map.insert(alias, (source_index, name));
map.insert(alias.clone(), (source_index, name));
if alias == "Monitor of Dummy Output" {
input_box_imp.reset_source_dropdown.set_selected(0);
}
*index += 1;
}
});
Expand Down Expand Up @@ -464,11 +467,11 @@ pub fn start_input_box_listener(conn: Connection, source_box: Arc<SourceBox>) ->

if *index == 1 {
model_list.append("Monitor of Dummy Output");
map.insert(String::from("Monitor of Dummy Output"), (0, String::from("Monitor of Dummy Output")));
}
for entry in 0..*index {
if model_list.string(entry) == Some(alias.clone().into()) {
model_list.remove(entry);
return;
}
}
if *index > 1 {
Expand Down Expand Up @@ -609,7 +612,7 @@ pub fn start_input_box_listener(conn: Connection, source_box: Arc<SourceBox>) ->
for entry in 0..*index {
if model_list.string(entry) == Some(alias.clone().into()) {
imp.reset_source_selection.set_selected(entry);
}
}
}
});
});
Expand Down
6 changes: 5 additions & 1 deletion src/components/output/sink_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ pub fn populate_sinks(output_box: Arc<SinkBox>) {
for entry in 0..*index {
if model_list.string(entry) == Some(name.alias.clone().into()) {
output_box_imp.reset_sink_dropdown.set_selected(entry);
return;
}
}
output_box_imp.reset_sink_dropdown.connect_selected_notify(
Expand Down Expand Up @@ -264,6 +265,7 @@ pub fn refresh_default_sink(new_sink: Sink, output_box: Arc<SinkBox>, entry: boo
for entry in 0..*index {
if model_list.string(entry) == Some(new_sink.alias.clone().into()) {
imp.reset_sink_dropdown.set_selected(entry);
return;
}
}
}
Expand Down Expand Up @@ -423,7 +425,7 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
if model_list.string(*index - 1) == Some("Dummy Output".into()) {
model_list.append(&alias);
model_list.remove(*index - 1);
map.insert(alias.clone(), (sink_index, name));
map.insert(alias, (sink_index, name));
output_box_imp.reset_sink_dropdown.set_selected(0);
} else {
model_list.append(&alias);
Expand Down Expand Up @@ -472,6 +474,7 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
for entry in 0..*index {
if model_list.string(entry) == Some(alias.clone().into()) {
model_list.remove(entry);
return;
}
}
if *index > 1 {
Expand Down Expand Up @@ -608,6 +611,7 @@ pub fn start_output_box_listener(conn: Connection, sink_box: Arc<SinkBox>) -> Co
for entry in 0..*index {
if model_list.string(entry) == Some(alias.clone().into()) {
imp.reset_sink_selection.set_selected(entry);
return;
}
}
});
Expand Down
9 changes: 5 additions & 4 deletions src/components/wifi/wifi_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub fn scan_for_wifi(wifi_box: Arc<WifiBox>) {
let ssid = access_point.ssid.clone();
let path = access_point.dbus_path.clone();
let connected =
imp.reset_current_wifi_device.borrow().active_access_point == path;
imp.reset_current_wifi_device.borrow().active_access_point == ssid;
let entry = WifiEntry::new(connected, access_point, imp);
wifi_entries.insert(ssid, entry.clone());
wifi_entries_path.insert(path, entry.clone());
Expand Down Expand Up @@ -294,7 +294,7 @@ pub fn start_event_listener(listeners: Arc<Listeners>, wifi_box: Arc<WifiBox>) {
return;
}
let connected = imp.reset_current_wifi_device.borrow().active_access_point
== ir.access_point.dbus_path;
== ir.access_point.ssid;
let entry = WifiEntry::new(connected, ir.access_point, imp);
wifi_entries.insert(ssid, entry.clone());
wifi_entries_path.insert(path, entry.clone());
Expand Down Expand Up @@ -366,7 +366,7 @@ pub fn start_event_listener(listeners: Arc<Listeners>, wifi_box: Arc<WifiBox>) {
.borrow()
.set_sensitive(false);
}
if ir.access_point.dbus_path
if ir.access_point.ssid
== imp.reset_current_wifi_device.borrow().active_access_point
{
entry_imp
Expand Down Expand Up @@ -403,7 +403,8 @@ pub fn start_event_listener(listeners: Arc<Listeners>, wifi_box: Arc<WifiBox>) {
for entry in wifi_entries.iter_mut() {
let imp = entry.1.imp();
let mut connected = imp.connected.borrow_mut();
*connected = imp.access_point.borrow().dbus_path == current_device.path;
*connected =
imp.access_point.borrow().ssid == current_device.active_access_point;
if *connected {
imp.reset_wifi_connected.borrow().set_text("Connected");
} else {
Expand Down
49 changes: 21 additions & 28 deletions src/components/wifi/wifi_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;

use crate::components::utils::{WIRELESS, DBUS_PATH, BASE};
use crate::components::utils::{BASE, DBUS_PATH, WIRELESS};
use crate::components::wifi::utils::get_connection_settings;
use adw::glib;
use adw::glib::{Object, PropertySet};
Expand Down Expand Up @@ -119,20 +119,18 @@ impl WifiEntry {

pub fn click_disconnect(entry: Arc<WifiEntry>) {
let entry_ref = entry.clone();
entry_ref
.imp()
.reset_wifi_connected
.borrow()
.set_text("Disconnecting...");
entry.set_activatable(false);
gio::spawn_blocking(move || {
let imp = entry_ref.imp();
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
BASE,
DBUS_PATH,
Duration::from_millis(10000),
);
let res: Result<(bool,), Error> = proxy.method_call(
WIRELESS,
"DisconnectFromCurrentAccessPoint",
(),
);
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(10000));
let res: Result<(bool,), Error> =
proxy.method_call(WIRELESS, "DisconnectFromCurrentAccessPoint", ());
if res.is_err() {
imp.connected.replace(false);
return;
Expand All @@ -151,22 +149,19 @@ pub fn click_stored_network(entry: Arc<WifiEntry>) {
let entry_imp = entry.imp();
let access_point = entry_imp.access_point.borrow().clone();
let entry_ref = entry.clone();
entry.set_activatable(false);
entry.set_sensitive(false);
entry_imp
.reset_wifi_connected
.borrow()
.set_text("Connecting...");
gio::spawn_blocking(move || {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
BASE,
DBUS_PATH,
Duration::from_millis(10000),
);
let res: Result<(bool,), Error> = proxy.method_call(
WIRELESS,
"ConnectToKnownAccessPoint",
(access_point,),
);
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(10000));
let res: Result<(bool,), Error> =
proxy.method_call(WIRELESS, "ConnectToKnownAccessPoint", (access_point,));
glib::spawn_future(async move {
glib::idle_add_once(move || {
entry.set_activatable(true);
entry.set_sensitive(true);
let imp = entry_ref.imp();
if res.is_err() {
imp.connected.replace(false);
Expand Down Expand Up @@ -194,21 +189,19 @@ pub fn click_new_network(entry: Arc<WifiEntry>) {
popup.reset_popup_label.set_visible(true);
popup.reset_popup_entry.set_sensitive(false);
popup.reset_popup_button.set_sensitive(false);
entry.set_sensitive(false);

gio::spawn_blocking(move || {
let conn = Connection::new_session().unwrap();
let proxy = conn.with_proxy(
BASE,
DBUS_PATH,
Duration::from_millis(10000),
);
let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(10000));
let res: Result<(bool,), Error> = proxy.method_call(
WIRELESS,
"ConnectToNewAccessPoint",
(access_point, password),
);
glib::spawn_future(async move {
glib::idle_add_once(move || {
entry.set_sensitive(false);
if res.is_err() {
let imp = entry_ref.imp();
imp.reset_wifi_popup
Expand Down
Loading

0 comments on commit c860fcd

Please sign in to comment.