projects/package-update-indicator

view pui-application.c @ 4:3d72ca76538d

Add setting to control whether to use a mobile connection
author Guido Berhoerster <guido+pui@berhoerster.name>
date Sun Jun 17 11:05:28 2018 +0200 (2018-06-17)
parents 2fa34d6272c6
children 2477a6151087
line source
1 /*
2 * Copyright (C) 2018 Guido Berhoerster <guido+pui@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
24 #include <gio/gdesktopappinfo.h>
25 #include <glib/gi18n.h>
26 #include <libappindicator/app-indicator.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include "pui-application.h"
31 #include "pui-backend.h"
32 #include "pui-common.h"
33 #include "pui-settings.h"
34 #include "pui-types.h"
36 struct _PuiApplication {
37 GApplication parent_instance;
38 GSettings *settings;
39 GCancellable *cancellable;
40 PuiBackend *backend;
41 AppIndicator *indicator;
42 GtkWidget *about_dialog;
43 GIcon *icons[PUI_STATE_LAST];
44 PuiState state;
45 gchar *update_command;
46 gchar *error_message;
47 };
49 G_DEFINE_TYPE(PuiApplication, pui_application, G_TYPE_APPLICATION)
51 enum {
52 PROP_0,
53 PROP_UPDATE_COMMAND,
54 PROP_LAST
55 };
57 extern gboolean restart;
59 static GParamSpec *properties[PROP_LAST] = { NULL };
61 static const gchar *icon_names[PUI_STATE_LAST] = {
62 [PUI_STATE_INITIAL] = "system-software-update",
63 [PUI_STATE_UP_TO_DATE] = "system-software-update",
64 [PUI_STATE_NORMAL_UPDATES_AVAILABLE] = "software-update-available",
65 [PUI_STATE_IMPORTANT_UPDATES_AVAILABLE] = "software-update-urgent",
66 [PUI_STATE_ERROR] = "dialog-warning"
67 };
69 static const GOptionEntry cmd_options[] = {
70 { "debug", '\0', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
71 N_("Enable debugging messages"), NULL },
72 { "quit", 'q', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
73 N_("Quit running instance of Package Update Indicator"), NULL },
74 { "version", 'V', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, NULL,
75 N_("Print the version number and quit"), NULL },
76 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, NULL, NULL, NULL },
77 { NULL }
78 };
80 static void pui_application_show_about_dialog(GSimpleAction *, GVariant *,
81 gpointer);
82 static void pui_application_open_preferences(GSimpleAction *, GVariant *,
83 gpointer);
84 static void pui_application_quit(GSimpleAction *, GVariant *, gpointer);
85 static void pui_application_install_updates(GSimpleAction *, GVariant *,
86 gpointer);
88 static const GActionEntry pui_application_actions[] = {
89 { "about", pui_application_show_about_dialog },
90 { "preferences", pui_application_open_preferences },
91 { "quit", pui_application_quit },
92 { "install-updates", pui_application_install_updates }
93 };
95 static gboolean
96 program_exists(const gchar *command_line)
97 {
98 gboolean is_program_in_path;
99 gchar **argv = NULL;
100 gchar *program_path;
102 if (!g_shell_parse_argv(command_line, NULL, &argv, NULL)) {
103 return (FALSE);
104 }
105 program_path = g_find_program_in_path(argv[0]);
106 is_program_in_path = (program_path != NULL) ? TRUE : FALSE;
107 g_free(program_path);
108 g_strfreev(argv);
110 return (is_program_in_path);
111 }
113 static void
114 pui_application_show_about_dialog(GSimpleAction *simple, GVariant *parameter,
115 gpointer user_data)
116 {
117 PuiApplication *self = user_data;
118 GtkBuilder *builder;
120 if (self->about_dialog == NULL) {
121 /* get dialog from builder */
122 builder = gtk_builder_new_from_resource("/org"
123 "/guido-berhoerster/code/package-update-indicator"
124 "/pui-about-dialog.ui");
126 self->about_dialog = GTK_WIDGET(gtk_builder_get_object(builder,
127 "about-dialog"));
129 g_object_unref(builder);
130 }
132 gtk_dialog_run(GTK_DIALOG(self->about_dialog));
133 gtk_widget_hide(self->about_dialog);
134 }
136 static void
137 pui_application_open_preferences(GSimpleAction *simple, GVariant *parameter,
138 gpointer user_data)
139 {
140 GDesktopAppInfo *prefs_app_info;
141 GError *error = NULL;
143 prefs_app_info = g_desktop_app_info_new("org.guido-berhoerster.code."
144 "package-update-indicator.preferences.desktop");
145 if (prefs_app_info == NULL) {
146 g_warning("desktop file \"org.guido-berhoerster.code."
147 "package-update-indicator.preferences.desktop\" not found");
148 return;
149 }
151 if (!g_app_info_launch(G_APP_INFO(prefs_app_info), NULL, NULL,
152 &error)) {
153 g_warning("failed to launch preferences: %s", error->message);
154 g_error_free(error);
155 }
156 }
158 static void
159 pui_application_quit(GSimpleAction *simple, GVariant *parameter,
160 gpointer user_data)
161 {
162 PuiApplication *self = user_data;
164 /* quit the GTK main loop if the about dialog is running */
165 if (self->about_dialog != NULL) {
166 gtk_widget_hide(self->about_dialog);
167 }
169 g_application_quit(G_APPLICATION(self));
170 }
172 static void
173 pui_application_install_updates(GSimpleAction *simple, GVariant *parameter,
174 gpointer user_data)
175 {
176 PuiApplication *self = user_data;
177 GError *error = NULL;
179 if (!g_spawn_command_line_async(self->update_command, &error)) {
180 g_warning("failed to run update command: %s", error->message);
181 g_error_free(error);
182 }
183 }
185 static void
186 update_ui(PuiApplication *self)
187 {
188 GSimpleAction *install_updates_action;
189 guint important_updates = 0;
190 guint normal_updates = 0;
191 gchar *title = NULL;
192 gchar *body = NULL;
193 GApplication *application = G_APPLICATION(self);
194 GNotification *notification = NULL;
196 install_updates_action =
197 G_SIMPLE_ACTION(g_action_map_lookup_action(G_ACTION_MAP(self),
198 "install-updates"));
200 if ((self->state == PUI_STATE_NORMAL_UPDATES_AVAILABLE) ||
201 (self->state == PUI_STATE_IMPORTANT_UPDATES_AVAILABLE)) {
202 g_object_get(self->backend,
203 "important-updates", &important_updates,
204 "normal-updates", &normal_updates, NULL);
205 }
207 /* actions */
208 switch (self->state) {
209 case PUI_STATE_INITIAL: /* FALLTHGROUGH */
210 case PUI_STATE_UP_TO_DATE: /* FALLTHGROUGH */
211 case PUI_STATE_ERROR:
212 g_simple_action_set_enabled(install_updates_action, FALSE);
213 break;
214 case PUI_STATE_NORMAL_UPDATES_AVAILABLE: /* FALLTHROUGH */
215 case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:
216 g_simple_action_set_enabled(install_updates_action,
217 program_exists(self->update_command));
218 break;
219 }
221 /* title and body for indicator and notification */
222 switch (self->state) {
223 case PUI_STATE_INITIAL:
224 title = g_strdup("");
225 body = g_strdup("");
226 break;
227 case PUI_STATE_UP_TO_DATE:
228 title = g_strdup(_("Up to Date"));
229 body = g_strdup(_("The system is up to date."));
230 break;
231 case PUI_STATE_NORMAL_UPDATES_AVAILABLE:
232 title = g_strdup(g_dngettext(NULL, "Software Update",
233 "Software Updates", (gulong)normal_updates));
234 if (normal_updates == 1) {
235 body = g_strdup(_("There is a software update "
236 "avaliable."));
237 } else {
238 body = g_strdup_printf(_("There are %u "
239 "software updates avaliable."), normal_updates);
240 }
241 break;
242 case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:
243 title = g_strdup(g_dngettext(NULL, "Important Software Update",
244 "Important Software Updates", (gulong)important_updates));
245 if ((normal_updates == 0) && (important_updates == 1)) {
246 body = g_strdup(_("There is an important "
247 "software update available."));
248 } else if ((normal_updates == 0) && (important_updates > 1)) {
249 body = g_strdup_printf(_("There are %u "
250 "important software updates available."),
251 important_updates);
252 } else if ((normal_updates > 0) && (important_updates == 1)) {
253 body = g_strdup_printf(_("There are %u "
254 "software updates available, "
255 "one of them is important."),
256 normal_updates + important_updates);
257 } else {
258 body = g_strdup_printf(_("There are %u "
259 "software updates available, "
260 "%u of them are important."),
261 normal_updates + important_updates,
262 important_updates);
263 }
264 break;
265 case PUI_STATE_ERROR:
266 title = g_strdup(self->error_message);
267 break;
268 }
270 /* indicator */
271 switch (self->state) {
272 case PUI_STATE_INITIAL:
273 app_indicator_set_status(self->indicator,
274 APP_INDICATOR_STATUS_PASSIVE);
275 break;
276 case PUI_STATE_UP_TO_DATE: /* FALLTHGROUGH */
277 case PUI_STATE_NORMAL_UPDATES_AVAILABLE: /* FALLTHGROUGH */
278 case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE: /* FALLTHGROUGH */
279 case PUI_STATE_ERROR:
280 app_indicator_set_status(self->indicator,
281 APP_INDICATOR_STATUS_ACTIVE);
282 break;
283 }
284 app_indicator_set_icon_full(self->indicator, icon_names[self->state],
285 title);
287 /* notification */
288 switch (self->state) {
289 case PUI_STATE_INITIAL: /* FALLTHGROUGH */
290 case PUI_STATE_UP_TO_DATE: /* FALLTHGROUGH */
291 case PUI_STATE_ERROR:
292 /* withdraw exisiting notification */
293 g_application_withdraw_notification(application,
294 "package-updates");
295 break;
296 case PUI_STATE_NORMAL_UPDATES_AVAILABLE: /* FALLTHGROUGH */
297 case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:
298 /* create notification */
299 notification = g_notification_new(title);
300 g_notification_set_body(notification, body);
301 g_notification_set_icon(notification, self->icons[self->state]);
302 g_notification_set_priority(notification,
303 G_NOTIFICATION_PRIORITY_NORMAL);
304 if (g_action_get_enabled(G_ACTION(install_updates_action))) {
305 g_notification_add_button(notification,
306 _("Install Updates"),
307 "app.install-updates");
308 }
309 g_application_send_notification(application, "package-updates",
310 notification);
311 break;
312 }
314 if (notification != NULL) {
315 g_object_unref(notification);
316 }
318 g_debug("indicator icon: %s, notification title: \"%s\", "
319 "notification body: \"%s\"", icon_names[self->state], title, body);
321 g_free(body);
322 g_free(title);
323 }
325 static void
326 transition_state(PuiApplication *self)
327 {
328 PuiState state = self->state;
329 guint important_updates;
330 guint normal_updates;
331 gchar *old_state;
332 gchar *new_state;
334 switch (self->state) {
335 case PUI_STATE_INITIAL: /* FALLTHROUGH */
336 case PUI_STATE_UP_TO_DATE: /* FALLTHROUGH */
337 case PUI_STATE_NORMAL_UPDATES_AVAILABLE: /* FALLTHROUGH */
338 case PUI_STATE_IMPORTANT_UPDATES_AVAILABLE:
339 if (self->error_message != NULL) {
340 state = PUI_STATE_ERROR;
341 break;
342 }
344 g_object_get(self->backend,
345 "important-updates", &important_updates,
346 "normal-updates", &normal_updates, NULL);
347 if (important_updates > 0) {
348 state = PUI_STATE_IMPORTANT_UPDATES_AVAILABLE;
349 } else if (normal_updates > 0) {
350 state = PUI_STATE_NORMAL_UPDATES_AVAILABLE;
351 } else {
352 state = PUI_STATE_UP_TO_DATE;
353 }
354 break;
355 case PUI_STATE_ERROR:
356 break;
357 }
359 if (state != self->state) {
360 old_state = pui_types_enum_to_string(PUI_TYPE_STATE,
361 self->state);
362 new_state = pui_types_enum_to_string(PUI_TYPE_STATE, state);
363 g_debug("state %s -> %s", old_state, new_state);
365 self->state = state;
366 update_ui(self);
368 g_free(new_state);
369 g_free(old_state);
370 }
371 }
373 static void
374 on_backend_restart_required(PuiBackend *backend, gpointer user_data)
375 {
376 PuiApplication *self = user_data;
378 restart = TRUE;
379 g_action_group_activate_action(G_ACTION_GROUP(G_APPLICATION(self)),
380 "quit", NULL);
381 }
383 static void
384 on_backend_state_changed(PuiBackend *backend, gpointer user_data)
385 {
386 PuiApplication *self = user_data;
388 transition_state(self);
389 }
391 static void
392 on_pui_backend_finished(GObject *source_object, GAsyncResult *result,
393 gpointer user_data)
394 {
395 PuiApplication *self = user_data;
396 GError *error = NULL;
398 self->backend = pui_backend_new_finish(result, &error);
399 if (self->backend == NULL) {
400 g_warning("failed to instantiate backend: %s", error->message);
401 g_free(self->error_message);
402 g_error_free(error);
403 self->error_message = g_strdup(_("Update notifications "
404 "are not supported."));
405 transition_state(self);
406 return;
407 }
409 g_settings_bind(self->settings, "refresh-interval", self->backend,
410 "refresh-interval", G_SETTINGS_BIND_GET);
411 g_settings_bind(self->settings, "use-mobile-connection", self->backend,
412 "use-mobile-connection", G_SETTINGS_BIND_GET);
414 transition_state(self);
416 g_signal_connect(self->backend, "restart-required",
417 G_CALLBACK(on_backend_restart_required), self);
418 g_signal_connect(self->backend, "state-changed",
419 G_CALLBACK(on_backend_state_changed), self);
420 }
422 static void
423 pui_application_startup(GApplication *application)
424 {
425 PuiApplication *self = PUI_APPLICATION(application);
426 gsize i;
427 GtkBuilder *builder;
428 GtkWidget *menu;
430 G_APPLICATION_CLASS(pui_application_parent_class)->startup(application);
432 /* create actions */
433 g_action_map_add_action_entries(G_ACTION_MAP(self),
434 pui_application_actions, G_N_ELEMENTS(pui_application_actions),
435 self);
437 /* load icons */
438 for (i = 0; i < G_N_ELEMENTS(self->icons); i++) {
439 self->icons[i] = g_themed_icon_new(icon_names[i]);
440 }
442 /* create settings */
443 self->settings = pui_settings_new();
444 g_settings_bind(self->settings, "update-command", self,
445 "update-command", G_SETTINGS_BIND_GET);
447 /* start instantiating backend */
448 pui_backend_new_async(self->cancellable, on_pui_backend_finished, self);
450 /* create indicator */
451 self->indicator = app_indicator_new(APPLICATION_ID,
452 "system-software-update",
453 APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
455 /* get menu from builder and add it to the indicator */
456 builder = gtk_builder_new_from_resource("/org/guido-berhoerster/code/"
457 "package-update-indicator/pui-menu.ui");
458 menu = GTK_WIDGET(gtk_builder_get_object(builder, "menu"));
459 gtk_widget_insert_action_group(menu, "app", G_ACTION_GROUP(self));
460 gtk_widget_show_all(menu);
461 app_indicator_set_menu(self->indicator, GTK_MENU(menu));
463 update_ui(self);
465 /* keep GApplication running */
466 g_application_hold(application);
468 g_object_unref(builder);
469 }
471 static void
472 pui_application_shutdown(GApplication *application)
473 {
474 GApplicationClass *application_class =
475 G_APPLICATION_CLASS(pui_application_parent_class);
477 application_class->shutdown(application);
478 }
480 static gint
481 pui_application_handle_local_options(GApplication *application,
482 GVariantDict *options)
483 {
484 gchar *messages_debug;
485 gchar **args = NULL;
486 GError *error = NULL;
488 /* filename arguments are not allowed */
489 if (g_variant_dict_lookup(options, G_OPTION_REMAINING, "^a&ay",
490 &args)) {
491 g_printerr("invalid argument: \"%s\"\n", args[0]);
492 g_free(args);
493 return (1);
494 }
496 if (g_variant_dict_contains(options, "version")) {
497 g_print("%s %s\n", PACKAGE, VERSION);
499 /* quit */
500 return (0);
501 }
503 if (g_variant_dict_contains(options, "debug")) {
504 /* enable debug logging */
505 messages_debug = g_strjoin(":", G_LOG_DOMAIN,
506 g_getenv("G_MESSAGES_DEBUG"), NULL);
507 g_setenv("G_MESSAGES_DEBUG", messages_debug, TRUE);
508 g_free(messages_debug);
509 }
511 /*
512 * register with the session bus so that it is possible to discern
513 * between remote and primary instance and that remote actions can be
514 * invoked, this causes the startup signal to be emitted which, in case
515 * of the primary instance, starts to instantiate the
516 * backend with the given values
517 */
518 if (!g_application_register(application, NULL, &error)) {
519 g_critical("g_application_register: %s", error->message);
520 g_error_free(error);
521 return (1);
522 }
524 if (g_variant_dict_contains(options, "quit")) {
525 /* only valid if a remote instance is running */
526 if (!g_application_get_is_remote(application)) {
527 g_printerr("%s is not running\n", g_get_prgname());
528 return (1);
529 }
531 /* signal remote instance to quit */
532 g_action_group_activate_action(G_ACTION_GROUP(application),
533 "quit", NULL);
535 /* quit local instance */
536 return (0);
537 }
539 /* proceed with default command line processing */
540 return (-1);
541 }
543 static void
544 pui_application_activate(GApplication *application) {
545 GApplicationClass *application_class =
546 G_APPLICATION_CLASS(pui_application_parent_class);
548 /* do nothing, implementation required by GApplication */
550 application_class->activate(application);
551 }
553 static void
554 pui_application_set_property(GObject *object, guint property_id,
555 const GValue *value, GParamSpec *pspec)
556 {
557 PuiApplication *self = PUI_APPLICATION(object);
559 switch (property_id) {
560 case PROP_UPDATE_COMMAND:
561 g_free(self->update_command);
562 self->update_command = g_value_dup_string(value);
563 g_debug("property \"update-command\" set to \"%s\"",
564 self->update_command);
565 break;
566 default:
567 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
568 break;
569 }
570 }
572 static void
573 pui_application_get_property(GObject *object, guint property_id, GValue *value,
574 GParamSpec *pspec)
575 {
576 PuiApplication *self = PUI_APPLICATION(object);
578 switch (property_id) {
579 case PROP_UPDATE_COMMAND:
580 g_value_set_string(value, self->update_command);
581 break;
582 default:
583 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
584 break;
585 }
586 }
588 static void
589 pui_application_dispose(GObject *object)
590 {
591 PuiApplication *self = PUI_APPLICATION(object);
592 gsize i;
594 if (self->settings != NULL) {
595 g_signal_handlers_disconnect_by_data(self->settings, self);
596 g_clear_object(&self->settings);
597 }
599 if (self->cancellable != NULL) {
600 g_cancellable_cancel(self->cancellable);
601 g_clear_object(&self->cancellable);
602 }
604 if (self->backend != NULL) {
605 g_clear_object(&self->backend);
606 }
608 if (self->indicator != NULL) {
609 g_clear_object(&self->indicator);
610 }
612 if (self->about_dialog != NULL) {
613 g_clear_pointer(&self->about_dialog,
614 (GDestroyNotify)(gtk_widget_destroy));
615 }
617 for (i = 0; i < G_N_ELEMENTS(self->icons); i++) {
618 if (self->icons[i] != NULL) {
619 g_clear_object(&self->icons[i]);
620 }
621 }
623 G_OBJECT_CLASS(pui_application_parent_class)->dispose(object);
624 }
626 static void
627 pui_application_finalize(GObject *object)
628 {
629 PuiApplication *self = PUI_APPLICATION(object);
631 g_free(self->update_command);
632 g_free(self->error_message);
634 G_OBJECT_CLASS(pui_application_parent_class)->finalize(object);
635 }
637 static void
638 pui_application_class_init(PuiApplicationClass *klass)
639 {
640 GObjectClass *object_class = G_OBJECT_CLASS(klass);
641 GApplicationClass *application_class = G_APPLICATION_CLASS(klass);
643 object_class->set_property = pui_application_set_property;
644 object_class->get_property = pui_application_get_property;
645 object_class->dispose = pui_application_dispose;
646 object_class->finalize = pui_application_finalize;
648 properties[PROP_UPDATE_COMMAND] = g_param_spec_string("update-command",
649 "Update command", "Command for installing updates", NULL,
650 G_PARAM_READWRITE);
652 g_object_class_install_properties(object_class, PROP_LAST, properties);
654 application_class->startup = pui_application_startup;
655 application_class->shutdown = pui_application_shutdown;
656 application_class->handle_local_options =
657 pui_application_handle_local_options;
658 application_class->activate = pui_application_activate;
659 }
661 static void
662 pui_application_init(PuiApplication *self)
663 {
664 g_application_add_main_option_entries(G_APPLICATION(self),
665 cmd_options);
667 self->cancellable = g_cancellable_new();
668 }
670 PuiApplication *
671 pui_application_new(void)
672 {
673 return (g_object_new(PUI_TYPE_APPLICATION, "application-id",
674 APPLICATION_ID, NULL));
675 }