comparison pkui-icon.c @ 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 7de92a24e86c
children
comparison
equal deleted inserted replaced
54:74da2bf0907b 55:b2f6b6b25703
37 PkuiBackend *backend; 37 PkuiBackend *backend;
38 38
39 GtkStatusIcon *status_icon; 39 GtkStatusIcon *status_icon;
40 GtkWidget *status_icon_popup_menu; 40 GtkWidget *status_icon_popup_menu;
41 NotifyNotification *notification; 41 NotifyNotification *notification;
42 gchar *update_viewer_command; 42 gchar **update_viewer_argv;
43 }; 43 };
44 44
45 static GtkWidget* icon_popup_menu_create(PkuiIcon *self); 45 static GtkWidget* icon_popup_menu_create(PkuiIcon *self);
46 static void icon_popup_menu_popup(GtkStatusIcon *status_icon, guint button, 46 static void icon_popup_menu_popup(GtkStatusIcon *status_icon, guint button,
47 guint activate_time, gpointer user_data); 47 guint activate_time, gpointer user_data);
61 if (self->priv->notification != NULL) { 61 if (self->priv->notification != NULL) {
62 notify_notification_close(self->priv->notification, NULL); 62 notify_notification_close(self->priv->notification, NULL);
63 g_object_unref(self->priv->notification); 63 g_object_unref(self->priv->notification);
64 } 64 }
65 g_object_unref(self->priv->backend); 65 g_object_unref(self->priv->backend);
66 g_free(self->priv->update_viewer_command); 66 g_strfreev(self->priv->update_viewer_argv);
67 67
68 G_OBJECT_CLASS(pkui_icon_parent_class)->finalize(gobject); 68 G_OBJECT_CLASS(pkui_icon_parent_class)->finalize(gobject);
69 } 69 }
70 70
71 static void 71 static void
97 G_CALLBACK(icon_activated), self); 97 G_CALLBACK(icon_activated), self);
98 g_signal_connect(G_OBJECT(self->priv->status_icon), "popup-menu", 98 g_signal_connect(G_OBJECT(self->priv->status_icon), "popup-menu",
99 G_CALLBACK(icon_popup_menu_popup), self); 99 G_CALLBACK(icon_popup_menu_popup), self);
100 100
101 self->priv->notification = NULL; 101 self->priv->notification = NULL;
102
103 self->priv->backend = NULL; 102 self->priv->backend = NULL;
104 103 self->priv->update_viewer_argv = NULL;
105 self->priv->update_viewer_command = NULL;
106 } 104 }
107 105
108 static void 106 static void
109 exec_update_viewer(PkuiIcon *self) 107 exec_update_viewer(PkuiIcon *self)
110 { 108 {
111 gchar **argv = NULL; 109 GError *error = NULL;
112 GError *error = NULL; 110 GPid pid;
113 GPid pid;
114 111
115 g_return_if_fail(PKUI_IS_BACKEND(self->priv->backend)); 112 g_return_if_fail(PKUI_IS_BACKEND(self->priv->backend));
116 g_return_if_fail(self->priv->update_viewer_command != NULL); 113 g_return_if_fail(self->priv->update_viewer_argv != NULL);
117 114
118 g_debug("executing command %s", self->priv->update_viewer_command); 115 g_debug("executing update viewer command");
119 116
120 if (!g_shell_parse_argv(self->priv->update_viewer_command, NULL, 117 if (!g_spawn_async(NULL, self->priv->update_viewer_argv, NULL,
121 &argv, &error)) {
122 g_warning("Could not parse command: %s", error->message);
123 g_error_free(error);
124 return;
125 }
126
127 if (!g_spawn_async(NULL, argv, NULL,
128 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &pid, 118 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, &pid,
129 &error)) { 119 &error)) {
130 g_warning("Could not execute command: %s", error->message); 120 g_warning("Could not execute command: %s", error->message);
131 g_error_free(error); 121 g_error_free(error);
132 return; 122 return;
249 /* TRANSLATORS: This is a message without number mentioned */ 239 /* TRANSLATORS: This is a message without number mentioned */
250 ngettext("Software Update", "Software Updates", updates_important + 240 ngettext("Software Update", "Software Updates", updates_important +
251 updates_normal); 241 updates_normal);
252 gchar *icon = updates_important ? "software-update-urgent" : 242 gchar *icon = updates_important ? "software-update-urgent" :
253 "software-update-available"; 243 "software-update-available";
244 gchar *program = NULL;
254 245
255 if (updates_important > 0) { 246 if (updates_important > 0) {
256 if (updates_normal > 0) { 247 if (updates_normal > 0) {
257 message_a = g_strdup_printf( 248 message_a = g_strdup_printf(
258 /* 249 /*
306 message, icon 297 message, icon
307 #if (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7) 298 #if (NOTIFY_VERSION_MAJOR == 0 && NOTIFY_VERSION_MINOR < 7)
308 , NULL 299 , NULL
309 #endif 300 #endif
310 ); 301 );
311 if (self->priv->update_viewer_command != NULL) { 302 } else {
312 notify_notification_add_action(self->priv->notification,
313 "install-updates", ngettext("Install Update",
314 "Install Updates",
315 updates_normal + updates_important),
316 notification_handle_action, self, NULL);
317 }
318 } else
319 notify_notification_update(self->priv->notification, title, 303 notify_notification_update(self->priv->notification, title,
320 message, icon); 304 message, icon);
305 }
306
307 notify_notification_clear_actions(self->priv->notification);
308 program = (self->priv->update_viewer_argv != NULL) ?
309 g_find_program_in_path(self->priv->update_viewer_argv[0]) : NULL;
310 if (program != NULL) {
311 notify_notification_add_action(self->priv->notification,
312 "install-updates", ngettext("Install Update",
313 "Install Updates", updates_normal + updates_important),
314 notification_handle_action, self, NULL);
315 }
321 316
322 notify_notification_set_timeout(self->priv->notification, 317 notify_notification_set_timeout(self->priv->notification,
323 NOTIFY_EXPIRES_NEVER); 318 NOTIFY_EXPIRES_NEVER);
324 notify_notification_set_urgency(self->priv->notification, 319 notify_notification_set_urgency(self->priv->notification,
325 updates_important ? NOTIFY_URGENCY_CRITICAL : 320 updates_important ? NOTIFY_URGENCY_CRITICAL :
326 NOTIFY_URGENCY_NORMAL); 321 NOTIFY_URGENCY_NORMAL);
327 notify_notification_show(self->priv->notification, NULL); 322 notify_notification_show(self->priv->notification, NULL);
328 323
324 g_free(program);
329 g_free(message); 325 g_free(message);
330 } 326 }
331 327
332 static void 328 static void
333 hide_notification(PkuiIcon *self) 329 hide_notification(PkuiIcon *self)
369 365
370 static void 366 static void
371 icon_activated(GtkStatusIcon *status_icon, gpointer user_data) 367 icon_activated(GtkStatusIcon *status_icon, gpointer user_data)
372 { 368 {
373 PkuiIcon *self = PKUI_ICON(user_data); 369 PkuiIcon *self = PKUI_ICON(user_data);
374 370 gchar *program = NULL;
375 if (self->priv->update_viewer_command != NULL) { 371
372 program = (self->priv->update_viewer_argv != NULL) ?
373 g_find_program_in_path(self->priv->update_viewer_argv[0]) :
374 NULL;
375 if (program != NULL) {
376 exec_update_viewer(self); 376 exec_update_viewer(self);
377 } 377 }
378
379 g_free(program);
378 } 380 }
379 381
380 PkuiIcon * 382 PkuiIcon *
381 pkui_icon_new(guint startup_delay, guint check_interval, 383 pkui_icon_new(guint startup_delay, guint check_interval,
382 const gchar *update_viewer_command) 384 const gchar *update_viewer_command)
383 { 385 {
386 GError *error = NULL;
384 PkuiIcon *icon = g_object_new(PKUI_TYPE_ICON, NULL); 387 PkuiIcon *icon = g_object_new(PKUI_TYPE_ICON, NULL);
385 388
386 icon->priv->update_viewer_command = g_strdup((update_viewer_command != 389 if (update_viewer_command == NULL) {
387 NULL) ? update_viewer_command : UPDATE_VIEWER_COMMAND); 390 update_viewer_command = UPDATE_VIEWER_COMMAND;
391 }
392
393 if (!g_shell_parse_argv(update_viewer_command, NULL,
394 &icon->priv->update_viewer_argv, &error)) {
395 g_warning("Could not parse command: %s", error->message);
396 g_error_free(error);
397 }
398
388 icon->priv->backend = pkui_backend_new(startup_delay, check_interval); 399 icon->priv->backend = pkui_backend_new(startup_delay, check_interval);
389 g_signal_connect(icon->priv->backend, "state-changed", 400 g_signal_connect(icon->priv->backend, "state-changed",
390 G_CALLBACK(backend_state_changed), icon); 401 G_CALLBACK(backend_state_changed), icon);
391 402
392 return (icon); 403 return (icon);