diff --git a/Cargo.toml b/Cargo.toml index fe94ccb..eb8645e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,7 @@ edition = "2021" # tray-item = { path = "../../", features = ["libappindicator"] } tray-item = { git = "https://github.com/olback/tray-item-rs", features = ["libappindicator"] } gtk = "0.18" -subprocess = "0.2" +glib = "0.20" + +[build-dependencies] +glib-build-tools = "0.20" diff --git a/src/main.rs b/src/main.rs index 202281c..4c80e69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,46 @@ -use tray_item::{TrayItem, IconSource}; -use subprocess; +// #![cfg_attr(debug_assertions, allow(dead_code, unused_imports, unused_variables, unused_must_use))] + +use { + std::sync::mpsc, + std::thread, + std::time, + std::process::Command, + tray_item::IconSource, + tray_item::TrayItem, +}; + +// use subprocess; + +enum Message { + Quit, + NOP, + UpdateSystem, + UpdateFlatpak +} + +fn run_cmd(cmd_string: &str, lang_env_var: Option<&str>) { + if lang_env_var.is_some() { + let key: &str = "LANG"; + std::env::set_var(key, lang_env_var.unwrap_or("C.UTF-8")); + } + + let args_vec : Vec<&str> = cmd_string.split_whitespace().collect(); + + if args_vec.len() == 1 { + let cmd = args_vec[0]; + + let status = Command::new(cmd).output(); + println!("{:#?}", status); + } + + else { + let cmd = args_vec[0]; + let new_args_vec = &args_vec[1..].to_vec(); + + let status = Command::new(cmd).args(new_args_vec).output(); + println!("{:#?}", status); + } +} fn main() { gtk::init().unwrap(); @@ -7,16 +48,71 @@ fn main() { let mut tray = TrayItem::new("Melawy Arch Linux Updater", IconSource::Resource("software-store")).unwrap(); tray.add_label("Melawy Arch Linux Updater").unwrap(); + let (tx, rx) = mpsc::sync_channel::(2); - tray.add_menu_item("Update system", || { - println!("Update system"); - let exit_status = subprocess::Exec::cmd("sudo").arg("arch-linux-updater").join(); - println!("{:?}", exit_status); + let update_system_tx = tx.clone(); + tray.add_menu_item("Update system", move|| { + update_system_tx.send(Message::UpdateSystem).unwrap(); }).unwrap(); - tray.add_menu_item("Quit", || { - gtk::main_quit(); + + let update_flatpak_tx = tx.clone(); + tray.add_menu_item("Update flatpak applications", move || { + update_flatpak_tx.send(Message::UpdateFlatpak).unwrap(); }).unwrap(); + + let quit_tx = tx.clone(); + tray.add_menu_item("Quit", move || { + quit_tx.send(Message::Quit).unwrap(); + }).unwrap(); + + + glib::idle_add_local(move || match rx.recv() { + Ok(Message::Quit) => { + gtk::main_quit(); + println!("Quit!"); + glib::ControlFlow::Break + } + Ok(Message::UpdateSystem) => { + println!("Update system"); + let cmd = "sudo arch-linux-updater"; + let _ = run_cmd(cmd, None); + glib::ControlFlow::Continue + } + Ok(Message::UpdateFlatpak) => { + println!("Update flatpak applications"); + let cmd = "/usr/bin/konsole --noclose --separate -e flatpak update"; + let _ = run_cmd(cmd, None); + glib::ControlFlow::Continue + } + _ => { + // println!("Default!"); + glib::ControlFlow::Continue + } + }); + + + thread::spawn(move || { + let mut count = 0; + loop { + // Menu doesn't show up until after hitting enter a few times? + //let mut s = String::new(); + //std::io::stdin().read_line(&mut s).unwrap(); + //if s.as_bytes()[0] == b'q' { + // println!("stopping thread loop!"); + // break + //} + + // glib::idle_add_local doesn't loop without this? + count += 1; + thread::sleep(time::Duration::from_millis(10)); + if count % 100 == 0 { + tx.send(Message::NOP).unwrap(); + // println!("Idle loop, {}!", count); + } + } + }); + gtk::main(); }