From 66946556908f926dd03fb248db34606437953b79 Mon Sep 17 00:00:00 2001 From: Gabriele Musco Date: Wed, 21 Aug 2024 23:53:25 +0200 Subject: [PATCH] feat: implemented basic update notification functionality --- src/ui/app.rs | 36 ++++++++++++++++++++++++++++++++++++ src/updater.rs | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/ui/app.rs b/src/ui/app.rs index 0643583..8e3907e 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -117,6 +117,8 @@ pub enum Msg { StartProber, OnProberExit(bool), NoOp, + #[cfg(feature = "appimage")] + CheckForUpdates, } impl App { @@ -740,6 +742,26 @@ impl AsyncComponent for App { } } } + #[cfg(feature = "appimage")] + Msg::CheckForUpdates => match crate::updater::check_updates_available().await { + Ok(true) => { + let notif = + gtk::gio::Notification::new(&format!("{APP_NAME}: update available")); + notif.set_body(Some(&format!( + "A new version of {APP_NAME} is available. Download it now." + ))); + notif.set_default_action("app.updateapp"); + notif.set_icon(>k::gio::ThemedIcon::from_names(&[ + crate::constants::APP_ID, + ])); + self.application.send_notification( + Some("org.gabmus.envision.update_available_notif"), + ¬if, + ); + } + Err(e) => eprintln!("Warning: failed to check for updates: {e}"), + _ => {} + }, } } @@ -944,6 +966,17 @@ impl AsyncComponent for App { root.insert_action_group(AppActionGroup::NAME, Some(&actions.into_action_group())); + #[cfg(feature = "appimage")] + { + let update_action = gtk::gio::SimpleAction::new("updateapp", None); + update_action.connect_activate(move |_, _| { + open_with_default_handler( + "https://gitlab.com/gabmus/envision/-/pipelines?ref=main&status=success", + ); + }); + model.application.add_action(&update_action); + } + { let app = &model.application; app.set_accelerators_for_action::(&["q"]); @@ -970,6 +1003,9 @@ impl AsyncComponent for App { model.split_view = Some(widgets.split_view.clone()); + #[cfg(feature = "appimage")] + sender.input(Msg::CheckForUpdates); + AsyncComponentParts { model, widgets } } } diff --git a/src/updater.rs b/src/updater.rs index 6706171..dd1563d 100644 --- a/src/updater.rs +++ b/src/updater.rs @@ -28,7 +28,7 @@ pub async fn check_updates_available() -> anyhow::Result { let text = res.text().await?; let commit_list: GitLabCommitList = serde_json::from_str(&text)?; if commit_list - .get(0) + .first() .is_some_and(|c| c.id.starts_with(shorthash)) { Ok(false)