changeset 55:b2f6b6b25703

Only show notification action if the update viewer executable exists
author Guido Berhoerster <guido+pk-update-icon@berhoerster.name>
date Wed, 18 Apr 2018 13:20:52 +0200
parents 74da2bf0907b
children 63347002d746
files pkui-icon.c
diffstat 1 files changed, 40 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/pkui-icon.c	Tue Sep 13 16:26:37 2016 +0200
+++ b/pkui-icon.c	Wed Apr 18 13:20:52 2018 +0200
@@ -39,7 +39,7 @@
 	GtkStatusIcon	*status_icon;
 	GtkWidget	*status_icon_popup_menu;
 	NotifyNotification *notification;
-	gchar		*update_viewer_command;
+	gchar		**update_viewer_argv;
 };
 
 static GtkWidget*	icon_popup_menu_create(PkuiIcon *self);
@@ -63,7 +63,7 @@
 		g_object_unref(self->priv->notification);
 	}
 	g_object_unref(self->priv->backend);
-	g_free(self->priv->update_viewer_command);
+	g_strfreev(self->priv->update_viewer_argv);
 
 	G_OBJECT_CLASS(pkui_icon_parent_class)->finalize(gobject);
 }
@@ -99,32 +99,22 @@
 	    G_CALLBACK(icon_popup_menu_popup), self);
 
 	self->priv->notification = NULL;
-
 	self->priv->backend = NULL;
-
-	self->priv->update_viewer_command = NULL;
+	self->priv->update_viewer_argv = NULL;
 }
 
 static void
 exec_update_viewer(PkuiIcon *self)
 {
-	gchar		**argv = NULL;
-	GError		*error = NULL;
-	GPid		pid;
+	GError	*error = NULL;
+	GPid	pid;
 
 	g_return_if_fail(PKUI_IS_BACKEND(self->priv->backend));
-	g_return_if_fail(self->priv->update_viewer_command != NULL);
-
-	g_debug("executing command %s", self->priv->update_viewer_command);
+	g_return_if_fail(self->priv->update_viewer_argv != NULL);
 
-	if (!g_shell_parse_argv(self->priv->update_viewer_command, NULL,
-	    &argv, &error)) {
-		g_warning("Could not parse command: %s", error->message);
-		g_error_free(error);
-		return;
-	}
+	g_debug("executing update viewer command");
 
-	if (!g_spawn_async(NULL, argv, NULL,
+	if (!g_spawn_async(NULL, self->priv->update_viewer_argv, NULL,
 	    G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &pid,
 	    &error)) {
 		g_warning("Could not execute command: %s", error->message);
@@ -251,6 +241,7 @@
 	    updates_normal);
 	gchar	*icon = updates_important ? "software-update-urgent" :
 	    "software-update-available";
+	gchar	*program = NULL;
 
 	if (updates_important > 0) {
 		if (updates_normal > 0) {
@@ -308,16 +299,20 @@
 		    , NULL
 #endif
 		    );
-		if (self->priv->update_viewer_command != NULL) {
-			notify_notification_add_action(self->priv->notification,
-			     "install-updates", ngettext("Install Update",
-			     "Install Updates",
-			     updates_normal + updates_important),
-			     notification_handle_action, self, NULL);
-		}
-	} else
+	} else {
 		notify_notification_update(self->priv->notification, title,
 		    message, icon);
+	}
+
+	notify_notification_clear_actions(self->priv->notification);
+	program = (self->priv->update_viewer_argv != NULL) ?
+	    g_find_program_in_path(self->priv->update_viewer_argv[0]) : NULL;
+	if (program != NULL) {
+		notify_notification_add_action(self->priv->notification,
+		     "install-updates", ngettext("Install Update",
+		     "Install Updates", updates_normal + updates_important),
+		     notification_handle_action, self, NULL);
+	}
 
 	notify_notification_set_timeout(self->priv->notification,
 	    NOTIFY_EXPIRES_NEVER);
@@ -326,6 +321,7 @@
 	    NOTIFY_URGENCY_NORMAL);
 	notify_notification_show(self->priv->notification, NULL);
 
+	g_free(program);
 	g_free(message);
 }
 
@@ -371,20 +367,35 @@
 icon_activated(GtkStatusIcon *status_icon, gpointer user_data)
 {
 	PkuiIcon	*self = PKUI_ICON(user_data);
+	gchar		*program = NULL;
 
-	if (self->priv->update_viewer_command != NULL) {
+	program = (self->priv->update_viewer_argv != NULL) ?
+	    g_find_program_in_path(self->priv->update_viewer_argv[0]) :
+	    NULL;
+	if (program != NULL) {
 		exec_update_viewer(self);
 	}
+
+	g_free(program);
 }
 
 PkuiIcon *
 pkui_icon_new(guint startup_delay, guint check_interval,
     const gchar *update_viewer_command)
 {
+	GError		*error = NULL;
 	PkuiIcon	*icon = g_object_new(PKUI_TYPE_ICON, NULL);
 
-	icon->priv->update_viewer_command = g_strdup((update_viewer_command !=
-	    NULL) ? update_viewer_command : UPDATE_VIEWER_COMMAND);
+	if (update_viewer_command == NULL) {
+		update_viewer_command = UPDATE_VIEWER_COMMAND;
+	}
+
+	if (!g_shell_parse_argv(update_viewer_command, NULL,
+	    &icon->priv->update_viewer_argv, &error)) {
+		g_warning("Could not parse command: %s", error->message);
+		g_error_free(error);
+	}
+
 	icon->priv->backend = pkui_backend_new(startup_delay, check_interval);
 	g_signal_connect(icon->priv->backend, "state-changed",
 	    G_CALLBACK(backend_state_changed), icon);