Skip to content

Commit

Permalink
Fixed notification on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Rodrigue Vande Capelle committed Dec 14, 2024
1 parent bdfcbe3 commit e6acdd1
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -361,7 +361,8 @@ async fn run_loop<T: ApiTrait>(interval: &u32, manager: &CredentialManager<T>, 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(_) => {
Expand Down Expand Up @@ -553,7 +554,8 @@ async fn run_auto_sign_up<T: ApiTrait>(
show_notification(&format!(
"You have been successfully registered for the exam: {}",
course_name
));
))
.await;
}
}

Expand All @@ -563,14 +565,19 @@ async fn run_auto_sign_up<T: ApiTrait>(
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

#[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);
}
Expand Down Expand Up @@ -687,10 +694,23 @@ fn run_on_boot_linux(interval: &u32) -> Result<(), Box<dyn std::error::Error>> {
}

#[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");
}
Expand All @@ -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");
}
}

0 comments on commit e6acdd1

Please sign in to comment.