use std::time::Duration; use adw::glib; use adw::glib::Object; use adw::prelude::{ComboRowExt, PreferencesRowExt}; use dbus::blocking::Connection; use dbus::Error; use glib::{Cast, clone}; use glib::subclass::types::ObjectSubclassIsExt; use gtk::{gio, StringList, StringObject}; use ReSet_Lib::audio::audio::Card; use components::utils::createDropdownLabelFactory; use crate::components; use super::cardEntryImpl; glib::wrapper! { pub struct CardEntry(ObjectSubclass) @extends adw::ComboRow, gtk::Widget, @implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable, adw::PreferencesRow; } impl CardEntry { pub fn new(card: Card) -> Self { let entry: CardEntry = Object::builder().build(); { let imp = entry.imp(); let mut map = imp.resetCardMap.borrow_mut(); entry.set_title(&card.name); let mut i: u32 = 0; let mut index: u32 = 0; let list = StringList::new(&[]); for profile in card.profiles.iter() { if profile.name == card.active_profile { index = i; } list.append(&profile.description); map.insert( profile.description.clone(), (card.index, profile.name.clone()), ); i += 1; } entry.set_model(Some(&list)); entry.set_selected(index); entry.set_use_subtitle(true); entry.connect_selected_notify(clone!(@weak imp => move |dropdown| { let selected = dropdown.selected_item(); if selected.is_none() { return; } let selected = selected.unwrap(); let selected = selected.downcast_ref::().unwrap(); let selected = selected.string().to_string(); let map = imp.resetCardMap.borrow(); let (device_index, profile_name) = map.get(&selected).unwrap(); set_card_profile_of_device(*device_index, profile_name.clone()); })); entry.set_factory(Some(&createDropdownLabelFactory())); } entry } } fn set_card_profile_of_device(device_index: u32, profile_name: String) -> bool { gio::spawn_blocking(move || { let conn = Connection::new_session().unwrap(); let proxy = conn.with_proxy( "org.Xetibo.ReSetDaemon", "/org/Xetibo/ReSetDaemon", Duration::from_millis(1000), ); let _: Result<(), Error> = proxy.method_call( "org.Xetibo.ReSetAudio", "SetCardProfileOfDevice", (device_index, profile_name), ); }); true }