🧹 cleanup

This commit is contained in:
Vladislav Nepogodin 2023-02-17 22:17:57 +04:00
parent af13cf75ad
commit 1024bd53cc
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
5 changed files with 53 additions and 59 deletions

View File

@ -3,14 +3,14 @@ use crate::utils::PacmanWrapper;
use std::path::Path; use std::path::Path;
use subprocess::Exec; use subprocess::Exec;
#[derive(Clone, Debug)] #[derive(Debug)]
#[repr(C)] #[repr(C)]
pub struct AlpmHelper { pub struct AlpmHelper {
pub pkg_list_install: Vec<String>, pub pkg_list_install: Vec<String>,
pub pkg_list_removal: Vec<String>, pub pkg_list_removal: Vec<String>,
} }
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum AlpmHelperResult { pub enum AlpmHelperResult {
Nothing, Nothing,
Remove, Remove,
@ -87,7 +87,7 @@ impl AlpmHelper {
false => "--remove", false => "--remove",
_ => "", _ => "",
}; };
Exec::shell(format!("pamac-installer {} {}", arg, packages_do)).join().unwrap(); Exec::shell(format!("pamac-installer {arg} {packages_do}")).join().unwrap();
} else { } else {
let (cmd, escalate) = match install { let (cmd, escalate) = match install {
true => match utils::get_pacman_wrapper() { true => match utils::get_pacman_wrapper() {
@ -103,7 +103,7 @@ impl AlpmHelper {
_ => ("pacman -R", true), _ => ("pacman -R", true),
}, },
}; };
let _ = utils::run_cmd_terminal(format!("{} {}", cmd, packages_do), escalate); let _ = utils::run_cmd_terminal(format!("{cmd} {packages_do}"), escalate);
} }
match install { match install {

View File

@ -74,7 +74,7 @@ impl ApplicationBrowser {
// Group filter // Group filter
let data = let data =
fs::read_to_string(format!("{}/data/application_utility/default.json", PKGDATADIR)) fs::read_to_string(format!("{PKGDATADIR}/data/application_utility/default.json"))
.expect("Unable to read file"); .expect("Unable to read file");
let groups: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse"); let groups: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse");
let group_store = load_groups_data(&groups); let group_store = load_groups_data(&groups);
@ -389,12 +389,12 @@ fn on_button_press_event_tree_view(
let (path, ..) = path_info.unwrap(); let (path, ..) = path_info.unwrap();
let app_browser = unsafe { &mut G_APP_BROWSER.lock().unwrap() }; let app_browser = unsafe { &mut G_APP_BROWSER.lock().unwrap() };
let app_store = app_browser.app_store.clone(); let app_store = &app_browser.app_store;
let iter_a = app_store.iter(&path.clone().unwrap()).unwrap(); let iter_a = app_store.iter(path.as_ref().unwrap()).unwrap();
let value_gobj = app_store.value(&iter_a, PACKAGE as i32); let value_gobj = app_store.value(&iter_a, PACKAGE as i32);
if value_gobj.get::<&str>().is_err() { if value_gobj.get::<&str>().is_err() {
if treeview.row_expanded(&path.clone().unwrap()) { if treeview.row_expanded(path.as_ref().unwrap()) {
treeview.collapse_row(&path.unwrap()); treeview.collapse_row(&path.unwrap());
} else { } else {
treeview.expand_to_path(&path.unwrap()); treeview.expand_to_path(&path.unwrap());
@ -408,7 +408,7 @@ fn on_button_press_event_tree_view(
fn on_app_toggle(_cell: &gtk::CellRendererToggle, path: gtk::TreePath) { fn on_app_toggle(_cell: &gtk::CellRendererToggle, path: gtk::TreePath) {
let app_browser = unsafe { &mut G_APP_BROWSER.lock().unwrap() }; let app_browser = unsafe { &mut G_APP_BROWSER.lock().unwrap() };
let app_store = app_browser.app_store.clone(); let app_store = &app_browser.app_store;
let iter_a = app_store.iter(&path).unwrap(); let iter_a = app_store.iter(&path).unwrap();
let value_gobj = app_store.value(&iter_a, PACKAGE as i32); let value_gobj = app_store.value(&iter_a, PACKAGE as i32);

View File

@ -86,15 +86,12 @@ fn quick_message(message: &'static str) {
} }
fn show_about_dialog() { fn show_about_dialog() {
let main_window: Window; let main_window: &Window = unsafe { g_hello_window.as_ref().unwrap().window.as_ref() };
unsafe { let logo_path = format!("/usr/share/icons/hicolor/scalable/apps/{APP_ID}.svg");
main_window = g_hello_window.clone().unwrap().window.clone();
}
let logo_path = format!("/usr/share/icons/hicolor/scalable/apps/{}.svg", APP_ID);
let logo = Pixbuf::from_file(logo_path).unwrap(); let logo = Pixbuf::from_file(logo_path).unwrap();
let dialog = gtk::AboutDialog::builder() let dialog = gtk::AboutDialog::builder()
.transient_for(&main_window) .transient_for(main_window)
.modal(true) .modal(true)
.program_name(&gettextrs::gettext("CachyOS Hello")) .program_name(&gettextrs::gettext("CachyOS Hello"))
.comments(&gettextrs::gettext("Welcome screen for CachyOS")) .comments(&gettextrs::gettext("Welcome screen for CachyOS"))
@ -141,7 +138,7 @@ fn main() {
} }
fn build_ui(application: &gtk::Application) { fn build_ui(application: &gtk::Application) {
let data = fs::read_to_string(format!("{}/data/preferences.json", PKGDATADIR)) let data = fs::read_to_string(format!("{PKGDATADIR}/data/preferences.json"))
.expect("Unable to read file"); .expect("Unable to read file");
let preferences: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse"); let preferences: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse");
@ -190,7 +187,7 @@ fn build_ui(application: &gtk::Application) {
preferences: preferences.clone(), preferences: preferences.clone(),
})); }));
*g_save_json.lock().unwrap() = save.clone(); *g_save_json.lock().unwrap() = save;
}; };
// Subtitle of headerbar // Subtitle of headerbar
@ -208,7 +205,7 @@ fn build_ui(application: &gtk::Application) {
let social_box: gtk::Box = builder.object("social").unwrap(); let social_box: gtk::Box = builder.object("social").unwrap();
for btn in social_box.children() { for btn in social_box.children() {
let name = btn.widget_name(); let name = btn.widget_name();
let icon_path = format!("{}/data/img/{}.png", PKGDATADIR, name); let icon_path = format!("{PKGDATADIR}/data/img/{name}.png");
let image: gtk::Image = builder.object(name.as_str()).unwrap(); let image: gtk::Image = builder.object(name.as_str()).unwrap();
image.set_from_file(Some(&icon_path)); image.set_from_file(Some(&icon_path));
} }
@ -224,7 +221,7 @@ fn build_ui(application: &gtk::Application) {
if btn.image_position() != gtk::PositionType::Right { if btn.image_position() != gtk::PositionType::Right {
continue; continue;
} }
let image_path = format!("{}/data/img/external-link.png", PKGDATADIR); let image_path = format!("{PKGDATADIR}/data/img/external-link.png");
let image = gtk::Image::new(); let image = gtk::Image::new();
image.set_from_file(Some(&image_path)); image.set_from_file(Some(&image_path));
image.set_margin_start(2); image.set_margin_start(2);
@ -252,7 +249,7 @@ fn build_ui(application: &gtk::Application) {
back_btn.connect_clicked(glib::clone!(@weak builder => move |button| { back_btn.connect_clicked(glib::clone!(@weak builder => move |button| {
let name = button.widget_name(); let name = button.widget_name();
let stack: gtk::Stack = builder.object("stack").unwrap(); let stack: gtk::Stack = builder.object("stack").unwrap();
stack.set_visible_child_name(&format!("{}page", name)); stack.set_visible_child_name(&format!("{name}page"));
})); }));
let grid = gtk::Grid::new(); let grid = gtk::Grid::new();
@ -323,7 +320,7 @@ pub fn get_best_locale(preferences: &serde_json::Value, save: &serde_json::Value
let sys_locale = let sys_locale =
string_substr(locale_name.as_str(), 0, locale_name.find('.').unwrap_or(usize::MAX)) string_substr(locale_name.as_str(), 0, locale_name.find('.').unwrap_or(usize::MAX))
.unwrap(); .unwrap();
let user_locale = format!("{}/{}/LC_MESSAGES/cachyos-hello.mo", LOCALEDIR, sys_locale); let user_locale = format!("{LOCALEDIR}/{sys_locale}/LC_MESSAGES/cachyos-hello.mo");
let two_letters = string_substr(sys_locale, 0, 2).unwrap(); let two_letters = string_substr(sys_locale, 0, 2).unwrap();
// If user's locale is supported // If user's locale is supported
@ -335,7 +332,7 @@ pub fn get_best_locale(preferences: &serde_json::Value, save: &serde_json::Value
} }
// If two first letters of user's locale is supported (ex: en_US -> en) // If two first letters of user's locale is supported (ex: en_US -> en)
else if check_regular_file( else if check_regular_file(
format!("{}/{}/LC_MESSAGES/cachyos-hello.mo", LOCALEDIR, two_letters).as_str(), format!("{LOCALEDIR}/{two_letters}/LC_MESSAGES/cachyos-hello.mo").as_str(),
) { ) {
return String::from(two_letters); return String::from(two_letters);
} }
@ -349,7 +346,7 @@ fn set_locale(use_locale: &str) {
println!( println!(
"┌{0:─^40}┐\n│{1: ^40}│\n└{0:─^40}┘", "┌{0:─^40}┐\n│{1: ^40}│\n└{0:─^40}┘",
"", "",
format!("Locale changed to {}", use_locale) format!("Locale changed to {use_locale}")
); );
} }
@ -376,7 +373,7 @@ fn set_locale(use_locale: &str) {
let elt_value = elt.as_str().unwrap(); let elt_value = elt.as_str().unwrap();
unsafe { unsafe {
let item: gtk::Widget = let item: gtk::Widget =
g_hello_window.clone().unwrap().builder.object(elt_value).unwrap(); g_hello_window.as_ref().unwrap().builder.object(elt_value).unwrap();
if default_texts[method.0].get(elt_value).is_none() { if default_texts[method.0].get(elt_value).is_none() {
let item_buf = item.property::<String>(method.0.as_str()); let item_buf = item.property::<String>(method.0.as_str());
default_texts[method.0][elt_value] = json!(item_buf); default_texts[method.0][elt_value] = json!(item_buf);
@ -392,7 +389,7 @@ fn set_locale(use_locale: &str) {
} }
unsafe { unsafe {
let preferences = &g_hello_window.clone().unwrap().preferences; let preferences = &g_hello_window.as_ref().unwrap().preferences;
let save = &*g_save_json.lock().unwrap(); let save = &*g_save_json.lock().unwrap();
// Change content of pages // Change content of pages
@ -403,7 +400,7 @@ fn set_locale(use_locale: &str) {
); );
for page in fs::read_dir(pages).unwrap() { for page in fs::read_dir(pages).unwrap() {
let stack: gtk::Stack = let stack: gtk::Stack =
g_hello_window.clone().unwrap().builder.object("stack").unwrap(); g_hello_window.as_ref().unwrap().builder.object("stack").unwrap();
let child = stack.child_by_name(&format!( let child = stack.child_by_name(&format!(
"{}page", "{}page",
page.as_ref().unwrap().path().file_name().unwrap().to_str().unwrap() page.as_ref().unwrap().path().file_name().unwrap().to_str().unwrap()
@ -432,25 +429,20 @@ fn set_locale(use_locale: &str) {
} }
fn set_autostart(autostart: bool) { fn set_autostart(autostart: bool) {
let autostart_path: String; let autostart_path = unsafe {
let desktop_path: String; fix_path(g_hello_window.as_ref().unwrap().preferences["autostart_path"].as_str().unwrap())
unsafe { };
autostart_path = fix_path( let desktop_path = unsafe {
g_hello_window.clone().unwrap().preferences["autostart_path"].as_str().unwrap(), g_hello_window.as_ref().unwrap().preferences["desktop_path"].as_str().unwrap().to_owned()
); };
desktop_path = g_hello_window.clone().unwrap().preferences["desktop_path"]
.as_str()
.unwrap()
.to_string();
}
let config_dir = Path::new(&autostart_path).parent().unwrap(); let config_dir = Path::new(&autostart_path).parent().unwrap();
if !config_dir.exists() { if !config_dir.exists() {
fs::create_dir_all(config_dir).unwrap(); fs::create_dir_all(config_dir).unwrap();
} }
if autostart && !check_regular_file(autostart_path.as_str()) { if autostart && !check_regular_file(&autostart_path) {
std::os::unix::fs::symlink(desktop_path, autostart_path).unwrap(); std::os::unix::fs::symlink(desktop_path, &autostart_path).unwrap();
} else if !autostart && check_regular_file(autostart_path.as_str()) { } else if !autostart && check_regular_file(&autostart_path) {
std::fs::remove_file(autostart_path).unwrap(); std::fs::remove_file(&autostart_path).unwrap();
} }
} }
@ -504,8 +496,8 @@ fn on_btn_clicked(param: &[glib::Value]) -> Option<glib::Value> {
let name = widget.widget_name(); let name = widget.widget_name();
unsafe { unsafe {
let stack: gtk::Stack = g_hello_window.clone().unwrap().builder.object("stack").unwrap(); let stack: gtk::Stack = g_hello_window.as_ref().unwrap().builder.object("stack").unwrap();
stack.set_visible_child_name(&format!("{}page", name)); stack.set_visible_child_name(&format!("{name}page"));
}; };
None None
@ -516,10 +508,11 @@ fn on_link_clicked(param: &[glib::Value]) -> Option<glib::Value> {
let name = widget.widget_name(); let name = widget.widget_name();
unsafe { unsafe {
let preferences = &g_hello_window.clone().unwrap().preferences["urls"]; let window_ref = &g_hello_window.as_ref().unwrap().window;
let preferences = &g_hello_window.as_ref().unwrap().preferences["urls"];
let uri = preferences[name.as_str()].as_str().unwrap(); let uri = preferences[name.as_str()].as_str().unwrap();
let _ = gtk::show_uri_on_window(gtk::Window::NONE, uri, 0); let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
} }
None None
@ -530,10 +523,11 @@ fn on_link1_clicked(param: &[glib::Value]) -> Option<glib::Value> {
let name = widget.widget_name(); let name = widget.widget_name();
unsafe { unsafe {
let preferences = &g_hello_window.clone().unwrap().preferences["urls"]; let window_ref = &g_hello_window.as_ref().unwrap().window;
let preferences = &g_hello_window.as_ref().unwrap().preferences["urls"];
let uri = preferences[name.as_str()].as_str().unwrap(); let uri = preferences[name.as_str()].as_str().unwrap();
let _ = gtk::show_uri_on_window(gtk::Window::NONE, uri, 0); let _ = gtk::show_uri_on_window(Some(window_ref), uri, 0);
} }
Some(false.to_value()) Some(false.to_value())
@ -541,7 +535,7 @@ fn on_link1_clicked(param: &[glib::Value]) -> Option<glib::Value> {
fn on_delete_window(_param: &[glib::Value]) -> Option<glib::Value> { fn on_delete_window(_param: &[glib::Value]) -> Option<glib::Value> {
unsafe { unsafe {
let preferences = &g_hello_window.clone().unwrap().preferences["save_path"]; let preferences = &g_hello_window.as_ref().unwrap().preferences["save_path"];
let save = &*g_save_json.lock().unwrap(); let save = &*g_save_json.lock().unwrap();
write_json(preferences.as_str().unwrap(), save); write_json(preferences.as_str().unwrap(), save);
} }

View File

@ -171,7 +171,7 @@ fn create_options_section() -> gtk::Box {
let pkg_name = "cachyos-dnscrypt-proxy"; let pkg_name = "cachyos-dnscrypt-proxy";
let service_unit_name = "dnscrypt-proxy.service"; let service_unit_name = "dnscrypt-proxy.service";
if !check_is_pkg_installed(pkg_name) { if !check_is_pkg_installed(pkg_name) {
let _ = utils::run_cmd_terminal(format!("pacman -S {}", pkg_name), true); let _ = utils::run_cmd_terminal(format!("pacman -S {pkg_name}"), true);
load_enabled_units(); load_enabled_units();
return; return;
} }
@ -294,7 +294,7 @@ pub fn create_tweaks_page(builder: &Builder) {
back_btn.connect_clicked(glib::clone!(@weak builder => move |button| { back_btn.connect_clicked(glib::clone!(@weak builder => move |button| {
let name = button.widget_name(); let name = button.widget_name();
let stack: gtk::Stack = builder.object("stack").unwrap(); let stack: gtk::Stack = builder.object("stack").unwrap();
stack.set_visible_child_name(&format!("{}page", name)); stack.set_visible_child_name(&format!("{name}page"));
})); }));
let options_section_box = create_options_section(); let options_section_box = create_options_section();
@ -341,7 +341,7 @@ pub fn create_appbrowser_page(builder: &Builder) {
back_btn.connect_clicked(glib::clone!(@weak builder => move |button| { back_btn.connect_clicked(glib::clone!(@weak builder => move |button| {
let name = button.widget_name(); let name = button.widget_name();
let stack: gtk::Stack = builder.object("stack").unwrap(); let stack: gtk::Stack = builder.object("stack").unwrap();
stack.set_visible_child_name(&format!("{}page", name)); stack.set_visible_child_name(&format!("{name}page"));
})); }));
let grid = gtk::Grid::new(); let grid = gtk::Grid::new();
@ -383,13 +383,13 @@ fn on_servbtn_clicked(button: &gtk::CheckButton) {
let local_units = &g_local_units.lock().unwrap().enabled_units; let local_units = &g_local_units.lock().unwrap().enabled_units;
cmd = if !local_units.contains(&String::from(action_data)) { cmd = if !local_units.contains(&String::from(action_data)) {
format!( format!(
"/sbin/pkexec {} bash -c \"systemctl {} enable --now --force {}\"", "/sbin/pkexec {pkexec_only} bash -c \"systemctl {user_only} enable --now --force \
pkexec_only, user_only, action_data {action_data}\""
) )
} else { } else {
format!( format!(
"/sbin/pkexec {} bash -c \"systemctl {} disable --now {}\"", "/sbin/pkexec {pkexec_only} bash -c \"systemctl {user_only} disable --now \
pkexec_only, user_only, action_data {action_data}\""
) )
}; };
} }
@ -419,14 +419,14 @@ fn on_refreshkeyring_btn_clicked(_: &gtk::Button) {
.map(|pkg| { .map(|pkg| {
let mut pkgname = String::from(pkg.name()); let mut pkgname = String::from(pkg.name());
pkgname.remove_matches("-keyring"); pkgname.remove_matches("-keyring");
format!("{} ", pkgname) format!("{pkgname} ")
}) })
.collect::<String>(); .collect::<String>();
// Spawn child process in separate thread. // Spawn child process in separate thread.
std::thread::spawn(move || { std::thread::spawn(move || {
let _ = utils::run_cmd_terminal( let _ = utils::run_cmd_terminal(
format!("pacman-key --init && pacman-key --populate {}", needles), format!("pacman-key --init && pacman-key --populate {needles}"),
true, true,
); );
}); });
@ -510,7 +510,7 @@ fn on_appbtn_clicked(button: &gtk::Button) {
}); });
rx.attach(None, move |text| { rx.attach(None, move |text| {
println!("{}", text); println!("{text}");
glib::Continue(true) glib::Continue(true)
}); });
} }

View File

@ -77,7 +77,7 @@ pub fn create_combo_with_model(group_store: &gtk::ListStore) -> gtk::ComboBox {
} }
pub fn run_cmd_terminal(cmd: String, escalate: bool) -> bool { pub fn run_cmd_terminal(cmd: String, escalate: bool) -> bool {
let cmd_formated = format!("{}; read -p 'Press enter to exit'", cmd); let cmd_formated = format!("{cmd}; read -p 'Press enter to exit'");
let mut args: Vec<&str> = vec![]; let mut args: Vec<&str> = vec![];
if escalate { if escalate {
args.extend_from_slice(&["-s", "pkexec /usr/share/cachyos-hello/scripts/rootshell.sh"]); args.extend_from_slice(&["-s", "pkexec /usr/share/cachyos-hello/scripts/rootshell.sh"]);