From e6acdd1e188035cbb4d0a6c70d9cade0628282c3 Mon Sep 17 00:00:00 2001 From: Rodrigue Vande Capelle Date: Sat, 14 Dec 2024 10:48:53 +0100 Subject: [PATCH] Fixed notification on linux --- src/main.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index dab727a..a40eb70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,7 +76,6 @@ const LOGO: &str = "logo.png"; #[tokio::main] async fn main() { // Check if it's the first setup - show_notification("body"); if is_first_setup() { display_logo(); @@ -86,9 +85,10 @@ async fn main() { "Automate your TU Delft exam registrations. Let's get you set up!".bright_cyan() ); + download_logo().await; // Sets up the registry values to be able to display notifications with a logo #[cfg(target_os = "windows")] - setup_registry().await; + setup_registry(); } set_up_logging(); @@ -361,7 +361,8 @@ async fn run_loop(interval: &u32, manager: &CredentialManager, i Ok(_) => info!("Auto sign-up successful."), Err(err) if err == "Invalid credentials" => { error!("Invalid credentials detected."); - show_notification("Your credentials are invalid. Run tuenroll start again"); + show_notification("Your credentials are invalid. Run tuenroll start again") + .await; break; // !!! Stops the background process !!! } Err(_) => { @@ -553,7 +554,8 @@ async fn run_auto_sign_up( show_notification(&format!( "You have been successfully registered for the exam: {}", course_name - )); + )) + .await; } } @@ -563,7 +565,9 @@ async fn run_auto_sign_up( Ok(()) } -fn show_notification(body: &str) { +async fn show_notification(body: &str) { + download_logo().await; + let mut notification = Notification::new(); let notification = notification.body(body).timeout(5 * 1000); // 5 seconds @@ -571,6 +575,9 @@ fn show_notification(body: &str) { #[cfg(target_os = "windows")] let notification = notification.app_id(APP_NAME); + #[cfg(target_os = "linux")] + let notification = notification.image_path(get_config_path(CONFIG_DIR, LOGO).to_str().unwrap()); + if let Err(e) = notification.show() { error!("Failed to show notification: {}", e); } @@ -687,10 +694,23 @@ fn run_on_boot_linux(interval: &u32) -> Result<(), Box> { } #[cfg(target_os = "windows")] -async fn setup_registry() { - let logo_url = "https://raw.githubusercontent.com/dhruvan2006/tuenroll/main/logo.png"; +fn setup_registry() { + if registry::registry(logo_path.to_str().unwrap(), APP_NAME).is_ok() { + info!("Registry succesfully setup."); + } else { + error!("Registry setup was unsuccesful"); + } +} + +async fn download_logo() { let logo_path = get_config_path(CONFIG_DIR, LOGO); + if logo_path.exists() { + return; + } + + let logo_url = "https://raw.githubusercontent.com/dhruvan2006/tuenroll/main/logo.png"; + if let Some(parent) = logo_path.parent() { std::fs::create_dir_all(parent).expect("Failed to create log directory"); } @@ -717,10 +737,4 @@ async fn setup_registry() { .expect("Could not write to file."); info!("Writing logo to file"); - - if registry::registry(logo_path.to_str().unwrap(), APP_NAME).is_ok() { - info!("Registry succesfully setup."); - } else { - error!("Registry setup was unsuccesful"); - } }