From 5be4a75d7be42f6f27e52cc2d1534119195eb5c6 Mon Sep 17 00:00:00 2001 From: Vladislav Nepogodin Date: Tue, 5 Jul 2022 01:38:59 +0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20add=20the=20actual=20instal/remo?= =?UTF-8?q?ve=20script?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- postinstall.sh | 1 + src/alpm_helper.rs | 12 ++++--- src/application_browser.rs | 1 + src/pages.rs | 61 ----------------------------------- src/scripts/rootshell.sh | 3 ++ src/scripts/terminal-helper | 64 +++++++++++++++++++++++++++++++++++++ src/utils.rs | 18 +++++++++++ 7 files changed, 94 insertions(+), 66 deletions(-) create mode 100755 src/scripts/rootshell.sh create mode 100755 src/scripts/terminal-helper diff --git a/postinstall.sh b/postinstall.sh index c5dd731..34aaf0c 100755 --- a/postinstall.sh +++ b/postinstall.sh @@ -1,6 +1,7 @@ #!/bin/sh mkdir -p "${DESTDIR}/${MESON_INSTALL_PREFIX}/share/cachyos-hello/" +cp -r "${MESON_SOURCE_ROOT}/src/scripts" "${DESTDIR}/${MESON_INSTALL_PREFIX}/share/cachyos-hello/" cp -r "${MESON_SOURCE_ROOT}/data" "${DESTDIR}/${MESON_INSTALL_PREFIX}/share/cachyos-hello/" cp -r "${MESON_SOURCE_ROOT}/ui" "${DESTDIR}/${MESON_INSTALL_PREFIX}/share/cachyos-hello/" diff --git a/src/alpm_helper.rs b/src/alpm_helper.rs index faa2230..80647c6 100644 --- a/src/alpm_helper.rs +++ b/src/alpm_helper.rs @@ -1,3 +1,5 @@ +use crate::utils; + #[derive(Clone, Debug)] #[repr(C)] pub struct AlpmHelper { @@ -76,18 +78,18 @@ impl AlpmHelper { } fn install_apps(&self, pkg_list: &Vec, install: bool) -> bool { - let mut install_arg: &str = ""; + let mut install_arg: &str = "-Sy"; if pkg_list.is_empty() { return false; } else if !install { install_arg = "-R"; } - println!("pacman {} {:?}", install_arg, pkg_list); - + let packages_do = pkg_list.iter().map(|s| s.to_string() + " ").collect::(); + let _ = utils::run_cmd_terminal(format!("pacman {} {}", install_arg, packages_do), true); match install { - true => !self.app_installed(&pkg_list[0]), - false => self.app_installed(&pkg_list[0]), + true => self.app_installed(&pkg_list[0]), + false => !self.app_installed(&pkg_list[0]), } } diff --git a/src/application_browser.rs b/src/application_browser.rs index d0b88c9..d30d807 100644 --- a/src/application_browser.rs +++ b/src/application_browser.rs @@ -193,6 +193,7 @@ impl ApplicationBrowser { self.app_store.clear(); if refresh { + self.alpm_handle = new_alpm().unwrap(); self.group_store = load_groups_data(&self.groups); } self.load_app_data(); diff --git a/src/pages.rs b/src/pages.rs index 26e2ad7..a0bf147 100644 --- a/src/pages.rs +++ b/src/pages.rs @@ -11,67 +11,6 @@ use gtk::prelude::*; use std::str; use subprocess::{Exec, Redirection}; -// static mut g_app_browser: Lazy> = -// Lazy::new(|| Mutex::new(ApplicationBrowser::new())); - -// pub fn create_appbrowser_page() -> gtk::Box { -// NOTE: we might not even need that here -// let app_browser_box = ApplicationBrowser::default_impl().create_page(); -// let app_browser_box = ApplicationBrowser::default_impl().lock().expect("Initialization -// failed!").create_page(); - -// let app_browser_box = gtk::Box::new(gtk::Orientation::Vertical, 10); -// app_browser_box.set_expand(true); - -// let button_box = gtk::Box::new(gtk::Orientation::Horizontal, 10); -// let advanced_button = gtk::Button::with_label("advanced"); -// advanced_button.set_tooltip_text(Some("Toggle an extended selection of packages")); -// advanced_button.connect_clicked(on_advanced_clicked); -// let download_button = gtk::Button::with_label("download"); -// download_button.set_tooltip_text(Some("Download the most recent selection of packages")); -// download_button.connect_clicked(on_download_clicked); -// let reset_button = gtk::Button::with_label("reset"); -// reset_button.set_tooltip_text(Some("Reset your current selections...")); -// reset_button.connect_clicked(on_reload_clicked); -// let update_system_button = unsafe { g_app_browser.lock().unwrap().update_system_btn }; - -// Group filter -// let data = fs::read_to_string(format!("{}/data/application_utility/default.json", -// PKGDATADIR)) .expect("Unable to read file"); -// let groups: serde_json::Value = serde_json::from_str(&data).expect("Unable to parse"); -// let group_store = load_groups_data(&groups); -// let group_combo = utils::create_combo_with_model(&group_store); - -// Packing button box -// button_box.pack_start(&advanced_button, false, false, 10); -// button_box.pack_start(&group_combo, false, false, 10); -// button_box.pack_end(&update_system_button, false, false, 10); - -// button_box.pack_end(&reset_button, false, false, 10); -// button_box.pack_end(&download_button, false, false, 10); -// app_browser_box.pack_start(&button_box, false, false, 10); - -// create view and app store -// let (tree_view, app_store_size) = create_view_tree(&groups); - -// create a scrollable window -// let app_window = gtk::ScrolledWindow::new(gtk::Adjustment::NONE, gtk::Adjustment::NONE); -// app_window.set_vexpand(true); -// app_window.set_policy(gtk::PolicyType::Never, gtk::PolicyType::Automatic); -// add window to tree view -// app_window.add(&tree_view); - -// // setup grid -// let grid_inter = gtk::Grid::new(); -// grid_inter.set_column_homogeneous(true); -// grid_inter.set_row_homogeneous(true); -// // add grid to app browser -// app_browser_box.add(&grid_inter); -// grid_inter.attach(&app_window, 0, 0, 5, app_store_size as i32); -// -// app_browser_box -// gtk::Box::new(gtk::Orientation::Vertical, 10) -//} static mut g_local_units: Lazy> = Lazy::new(|| Mutex::new(SystemdUnits::new())); static mut g_global_units: Lazy> = diff --git a/src/scripts/rootshell.sh b/src/scripts/rootshell.sh new file mode 100755 index 0000000..db3e162 --- /dev/null +++ b/src/scripts/rootshell.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +exec bash $@ diff --git a/src/scripts/terminal-helper b/src/scripts/terminal-helper new file mode 100755 index 0000000..50f3ac4 --- /dev/null +++ b/src/scripts/terminal-helper @@ -0,0 +1,64 @@ +#!/usr/bin/bash +# +### +# This code has been taken from Garuda +# Its is only temporal implementation +### +# +# This script tries to exec a terminal emulator by trying some known terminal +# emulators. +# +# Invariants: +# 1. $TERMINAL must come first +# 2. Distribution-specific mechanisms come next, e.g. x-terminal-emulator +# 3. The terminal emulator with best accessibility comes first. +# 4. No order is guaranteed/desired for the remaining terminal emulators. + +set -e +LAUNCHER_CMD=bash + +usage() { + echo "Usage: ${0##*/} [cmd]" + echo ' -s [shell] Change shell to [shell]' + echo ' -h This help' + exit 1 +} + +opts='s:h' + +while getopts "${opts}" arg; do + case "${arg}" in + s) LAUNCHER_CMD="$OPTARG" ;; + h|?) usage 0 ;; + *) echo "invalid argument '${arg}'"; usage 1 ;; + esac +done + +shift $(($OPTIND - 1)) + +file="$(mktemp)" +echo "$1" > "$file" +cmd="${LAUNCHER_CMD} \"$file\"" +echo $cmd + +#declare -a terminals=(x-terminal-emulator mate-terminal gnome-terminal terminator xfce4-terminal urxvt rxvt termit Eterm aterm uxterm xterm roxterm termite lxterminal terminology st qterminal lilyterm tilix terminix konsole kitty guake tilda alacritty) +terminal="" +declare -A terminals=( ["alacritty"]="alacritty -e $cmd || LIBGL_ALWAYS_SOFTWARE=1 alacritty -e $cmd" ["konsole"]="konsole -e $cmd" ["gnome-terminal"]="gnome-terminal --wait -- $cmd" ["xfce4-terminal"]="xfce4-terminal --disable-server --command '$cmd'" ["lxterminal"]="lxterminal -e $cmd" ["xterm"]="xterm -e $cmd" ["st"]="st $cmd") +declare -a term_order=( "alacritty" "konsole" "gnome-terminal" "xfce4-terminal" "lxterminal" "xterm" "st") + +if [ -z "$terminal" ] || ! command -v "$terminal" &> /dev/null; then +for entry in ${term_order[@]}; do + if command -v "$entry" > /dev/null 2>&1; then + terminal="$entry" + break; + fi +done +fi + +if [ -z "$terminal" ]; then + notify-send -t 1500 --app-name=CachyOS "No terminal installed" "Could not find a terminal emulator. Please install one." + exit 1 +fi + +eval "${terminals[${terminal}]}" || { rm "$file"; exit 2; } +rm "$file" diff --git a/src/utils.rs b/src/utils.rs index 0f09a80..c0a7118 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,6 +2,8 @@ use gtk::prelude::*; use std::fs::File; use std::{fs, slice, str}; +use subprocess::{Exec, Redirection}; + #[inline] pub fn fix_path(path: &str) -> String { if !path.starts_with('~') { @@ -65,6 +67,22 @@ pub fn create_combo_with_model(group_store: >k::ListStore) -> gtk::ComboBox { group_combo } +pub fn run_cmd_terminal(cmd: String, escalate: bool) -> bool { + let cmd_formated = format!("{}; read -p 'Press enter to exit'", cmd); + let mut args: Vec<&str> = vec![]; + if escalate { + args.extend_from_slice(&["-s", "pkexec /usr/share/cachyos-hello/scripts/rootshell.sh"]); + } + args.push(cmd_formated.as_str()); + + let exit_status = Exec::cmd("/usr/share/cachyos-hello/scripts/terminal-helper") + .args(args.as_slice()) + .stdout(Redirection::Pipe) + .join() + .unwrap(); + exit_status.success() +} + #[cfg(test)] mod test { use super::*;