projects/pk-update-icon

changeset 55:b2f6b6b25703

Only show notification action if the update viewer executable exists
author Guido Berhoerster <guido+pk-update-icon@berhoerster.name>
date Wed Apr 18 13:20:52 2018 +0200 (2018-04-18)
parents 74da2bf0907b
children 63347002d746
files pkui-icon.c
line diff
     1.1 --- a/pkui-icon.c	Tue Sep 13 16:26:37 2016 +0200
     1.2 +++ b/pkui-icon.c	Wed Apr 18 13:20:52 2018 +0200
     1.3 @@ -39,7 +39,7 @@
     1.4  	GtkStatusIcon	*status_icon;
     1.5  	GtkWidget	*status_icon_popup_menu;
     1.6  	NotifyNotification *notification;
     1.7 -	gchar		*update_viewer_command;
     1.8 +	gchar		**update_viewer_argv;
     1.9  };
    1.10  
    1.11  static GtkWidget*	icon_popup_menu_create(PkuiIcon *self);
    1.12 @@ -63,7 +63,7 @@
    1.13  		g_object_unref(self->priv->notification);
    1.14  	}
    1.15  	g_object_unref(self->priv->backend);
    1.16 -	g_free(self->priv->update_viewer_command);
    1.17 +	g_strfreev(self->priv->update_viewer_argv);
    1.18  
    1.19  	G_OBJECT_CLASS(pkui_icon_parent_class)->finalize(gobject);
    1.20  }
    1.21 @@ -99,32 +99,22 @@
    1.22  	    G_CALLBACK(icon_popup_menu_popup), self);
    1.23  
    1.24  	self->priv->notification = NULL;
    1.25 -
    1.26  	self->priv->backend = NULL;
    1.27 -
    1.28 -	self->priv->update_viewer_command = NULL;
    1.29 +	self->priv->update_viewer_argv = NULL;
    1.30  }
    1.31  
    1.32  static void
    1.33  exec_update_viewer(PkuiIcon *self)
    1.34  {
    1.35 -	gchar		**argv = NULL;
    1.36 -	GError		*error = NULL;
    1.37 -	GPid		pid;
    1.38 +	GError	*error = NULL;
    1.39 +	GPid	pid;
    1.40  
    1.41  	g_return_if_fail(PKUI_IS_BACKEND(self->priv->backend));
    1.42 -	g_return_if_fail(self->priv->update_viewer_command != NULL);
    1.43 +	g_return_if_fail(self->priv->update_viewer_argv != NULL);
    1.44  
    1.45 -	g_debug("executing command %s", self->priv->update_viewer_command);
    1.46 +	g_debug("executing update viewer command");
    1.47  
    1.48 -	if (!g_shell_parse_argv(self->priv->update_viewer_command, NULL,
    1.49 -	    &argv, &error)) {
    1.50 -		g_warning("Could not parse command: %s", error->message);
    1.51 -		g_error_free(error);
    1.52 -		return;
    1.53 -	}
    1.54 -
    1.55 -	if (!g_spawn_async(NULL, argv, NULL,
    1.56 +	if (!g_spawn_async(NULL, self->priv->update_viewer_argv, NULL,
    1.57  	    G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &pid,
    1.58  	    &error)) {
    1.59  		g_warning("Could not execute command: %s", error->message);
    1.60 @@ -251,6 +241,7 @@
    1.61  	    updates_normal);
    1.62  	gchar	*icon = updates_important ? "software-update-urgent" :
    1.63  	    "software-update-available";
    1.64 +	gchar	*program = NULL;
    1.65  
    1.66  	if (updates_important > 0) {
    1.67  		if (updates_normal > 0) {
    1.68 @@ -308,16 +299,20 @@
    1.69  		    , NULL
    1.70  #endif
    1.71  		    );
    1.72 -		if (self->priv->update_viewer_command != NULL) {
    1.73 -			notify_notification_add_action(self->priv->notification,
    1.74 -			     "install-updates", ngettext("Install Update",
    1.75 -			     "Install Updates",
    1.76 -			     updates_normal + updates_important),
    1.77 -			     notification_handle_action, self, NULL);
    1.78 -		}
    1.79 -	} else
    1.80 +	} else {
    1.81  		notify_notification_update(self->priv->notification, title,
    1.82  		    message, icon);
    1.83 +	}
    1.84 +
    1.85 +	notify_notification_clear_actions(self->priv->notification);
    1.86 +	program = (self->priv->update_viewer_argv != NULL) ?
    1.87 +	    g_find_program_in_path(self->priv->update_viewer_argv[0]) : NULL;
    1.88 +	if (program != NULL) {
    1.89 +		notify_notification_add_action(self->priv->notification,
    1.90 +		     "install-updates", ngettext("Install Update",
    1.91 +		     "Install Updates", updates_normal + updates_important),
    1.92 +		     notification_handle_action, self, NULL);
    1.93 +	}
    1.94  
    1.95  	notify_notification_set_timeout(self->priv->notification,
    1.96  	    NOTIFY_EXPIRES_NEVER);
    1.97 @@ -326,6 +321,7 @@
    1.98  	    NOTIFY_URGENCY_NORMAL);
    1.99  	notify_notification_show(self->priv->notification, NULL);
   1.100  
   1.101 +	g_free(program);
   1.102  	g_free(message);
   1.103  }
   1.104  
   1.105 @@ -371,20 +367,35 @@
   1.106  icon_activated(GtkStatusIcon *status_icon, gpointer user_data)
   1.107  {
   1.108  	PkuiIcon	*self = PKUI_ICON(user_data);
   1.109 +	gchar		*program = NULL;
   1.110  
   1.111 -	if (self->priv->update_viewer_command != NULL) {
   1.112 +	program = (self->priv->update_viewer_argv != NULL) ?
   1.113 +	    g_find_program_in_path(self->priv->update_viewer_argv[0]) :
   1.114 +	    NULL;
   1.115 +	if (program != NULL) {
   1.116  		exec_update_viewer(self);
   1.117  	}
   1.118 +
   1.119 +	g_free(program);
   1.120  }
   1.121  
   1.122  PkuiIcon *
   1.123  pkui_icon_new(guint startup_delay, guint check_interval,
   1.124      const gchar *update_viewer_command)
   1.125  {
   1.126 +	GError		*error = NULL;
   1.127  	PkuiIcon	*icon = g_object_new(PKUI_TYPE_ICON, NULL);
   1.128  
   1.129 -	icon->priv->update_viewer_command = g_strdup((update_viewer_command !=
   1.130 -	    NULL) ? update_viewer_command : UPDATE_VIEWER_COMMAND);
   1.131 +	if (update_viewer_command == NULL) {
   1.132 +		update_viewer_command = UPDATE_VIEWER_COMMAND;
   1.133 +	}
   1.134 +
   1.135 +	if (!g_shell_parse_argv(update_viewer_command, NULL,
   1.136 +	    &icon->priv->update_viewer_argv, &error)) {
   1.137 +		g_warning("Could not parse command: %s", error->message);
   1.138 +		g_error_free(error);
   1.139 +	}
   1.140 +
   1.141  	icon->priv->backend = pkui_backend_new(startup_delay, check_interval);
   1.142  	g_signal_connect(icon->priv->backend, "state-changed",
   1.143  	    G_CALLBACK(backend_state_changed), icon);