From 064e7939488830c79cf1f8c0ef793c7c443cfd50 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Tue, 6 Aug 2024 01:58:58 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20pages:=20refactor=20on=5Fservbtn?= =?UTF-8?q?=5Fclicked=20into=20function=20with=20callback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages.rs | 82 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 53 insertions(+), 29 deletions(-) diff --git a/src/pages.rs b/src/pages.rs index f5a0b60..3d13d6d 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -2,17 +2,20 @@ use crate::application_browser::ApplicationBrowser; use crate::data_types::*; use crate::utils::PacmanWrapper; use crate::{fl, utils}; + +use std::boxed::Box; +use std::fmt::Write; +use std::path::Path; +use std::str; +use std::sync::Mutex; + use glib::translate::FromGlib; use gtk::{glib, Builder}; use once_cell::sync::Lazy; use phf::phf_ordered_map; -use std::fmt::Write; -use std::path::Path; -use std::sync::Mutex; use gtk::prelude::*; -use std::str; use subprocess::{Exec, Redirection}; static G_LOCAL_UNITS: Lazy> = Lazy::new(|| Mutex::new(SystemdUnits::new())); @@ -937,19 +940,13 @@ pub fn create_appbrowser_page(builder: &Builder) { stack.add_named(&viewport, child_name); } -fn on_servbtn_clicked(button: >k::CheckButton) { - // Get action data/type. - let action_type: &str; - let action_data: &str; - let alpm_package_name: &str; - let signal_handler: u64; - unsafe { - action_type = *button.data("actionType").unwrap().as_ptr(); - action_data = *button.data("actionData").unwrap().as_ptr(); - alpm_package_name = *button.data("alpmPackage").unwrap().as_ptr(); - signal_handler = *button.data("signalHandle").unwrap().as_ptr(); - } - +fn toggle_service( + action_type: &str, + action_data: &str, + alpm_package_name: &str, + widget_window: gtk::Window, + callback: std::boxed::Box, +) { let units_handle = if action_type == "user_service" { &G_GLOBAL_UNITS } else { &G_LOCAL_UNITS } .lock() .unwrap(); @@ -968,13 +965,17 @@ fn on_servbtn_clicked(button: >k::CheckButton) { // Create context channel. let (tx, rx) = glib::MainContext::channel(glib::Priority::default()); + let dialog_text = fl!("package-not-installed", package_name = alpm_package_name); + + let action_type = action_type.to_owned(); + let alpm_package_name = alpm_package_name.to_owned(); // Spawn child process in separate thread. std::thread::spawn(move || { if !alpm_package_name.is_empty() { - if !utils::is_alpm_pkg_installed(alpm_package_name) { + if !utils::is_alpm_pkg_installed(&alpm_package_name) { let _ = utils::run_cmd_terminal(format!("pacman -S {alpm_package_name}"), true); } - if !utils::is_alpm_pkg_installed(alpm_package_name) { + if !utils::is_alpm_pkg_installed(&alpm_package_name) { tx.send(false).expect("Couldn't send data to channel"); return; } @@ -988,22 +989,14 @@ fn on_servbtn_clicked(button: >k::CheckButton) { } }); - let button_sh = button.clone(); rx.attach(None, move |msg| { if !msg { - let widget_window = - utils::get_window_from_widget(&button_sh).expect("Failed to retrieve window"); - - let sighandle_id_obj = - unsafe { glib::signal::SignalHandlerId::from_glib(signal_handler) }; - button_sh.block_signal(&sighandle_id_obj); - button_sh.set_active(msg); - button_sh.unblock_signal(&sighandle_id_obj); + callback(msg); let dialog = gtk::MessageDialog::builder() .transient_for(&widget_window) .message_type(gtk::MessageType::Error) - .text(fl!("package-not-installed", package_name = alpm_package_name)) + .text(&dialog_text) .title("Error") .modal(true) .buttons(gtk::ButtonsType::Ok) @@ -1016,6 +1009,37 @@ fn on_servbtn_clicked(button: >k::CheckButton) { }); } +fn on_servbtn_clicked(button: >k::CheckButton) { + // Get action data/type. + let action_type: &str; + let action_data: &str; + let alpm_package_name: &str; + let signal_handler: u64; + unsafe { + action_type = *button.data("actionType").unwrap().as_ptr(); + action_data = *button.data("actionData").unwrap().as_ptr(); + alpm_package_name = *button.data("alpmPackage").unwrap().as_ptr(); + signal_handler = *button.data("signalHandle").unwrap().as_ptr(); + } + + let widget_window = utils::get_window_from_widget(button).expect("Failed to retrieve window"); + + let button_sh = button.clone(); + toggle_service( + action_type, + action_data, + alpm_package_name, + widget_window, + Box::new(move |msg| { + let sighandle_id_obj = + unsafe { glib::signal::SignalHandlerId::from_glib(signal_handler) }; + button_sh.block_signal(&sighandle_id_obj); + button_sh.set_active(msg); + button_sh.unblock_signal(&sighandle_id_obj); + }), + ); +} + fn on_refreshkeyring_btn_clicked(_: >k::Button) { let pacman = pacmanconf::Config::with_opts(None, Some("/etc/pacman.conf"), Some("/")).unwrap(); let alpm = alpm_utils::alpm_with_conf(&pacman).unwrap();