~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to src/livepatch.c

Tags: 3.192.14
releasing package update-notifier version 3.192.14

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#endif
4
4
 
5
5
#include <errno.h>
 
6
#include <gio/gdesktopappinfo.h>
6
7
#include <glib.h>
7
8
#include <glib/gstdio.h>
8
9
#include <libnotify/notify.h>
11
12
 
12
13
#include "update-notifier.h"
13
14
 
 
15
#define LIVEPATCH_DESKTOP_FILE "software-properties-livepatch.desktop"
14
16
#define STATUS_PATH "/var/snap/canonical-livepatch/current/status"
15
17
 
16
18
static void
29
31
}
30
32
 
31
33
static void
 
34
notify_action_cb (NotifyNotification *notification,
 
35
                  char *action,
 
36
                  gpointer user_data)
 
37
{
 
38
    g_autoptr(GDesktopAppInfo) info = NULL;
 
39
    g_autoptr(GdkAppLaunchContext) context = NULL;
 
40
    g_autoptr(GError) error = NULL;
 
41
 
 
42
    info = g_desktop_app_info_new (LIVEPATCH_DESKTOP_FILE);
 
43
    if (!info) {
 
44
        g_warning ("Could not find application '%s'", LIVEPATCH_DESKTOP_FILE);
 
45
        return;
 
46
    }
 
47
 
 
48
    context = gdk_display_get_app_launch_context (gdk_display_get_default ());
 
49
    if (!g_app_info_launch (G_APP_INFO (info), NULL, G_APP_LAUNCH_CONTEXT (context), &error)) {
 
50
        g_warning ("Could not launch application '%s'", LIVEPATCH_DESKTOP_FILE);
 
51
    }
 
52
}
 
53
 
 
54
static gboolean
32
55
show_notification (const char *summary, const char *body, const char *icon)
33
56
{
34
 
    NotifyNotification *n = notify_notification_new (summary, body, icon);
 
57
    NotifyNotification *n;
 
58
    g_autoptr(GDesktopAppInfo) info = NULL;
 
59
    g_autoptr(GError) error = NULL;
 
60
 
 
61
    n = notify_notification_new (summary, body, icon);
35
62
    notify_notification_set_timeout (n, 60000);
36
 
    notify_notification_show (n, NULL);
37
 
    g_object_unref (n);
 
63
 
 
64
    info = g_desktop_app_info_new (LIVEPATCH_DESKTOP_FILE);
 
65
    if (info) {
 
66
        notify_notification_add_action (n, "settings", _("Show Settings…"),
 
67
                                        notify_action_cb, NULL, NULL);
 
68
        notify_notification_add_action (n, "default", _("Show Settings…"),
 
69
                                        notify_action_cb, NULL, NULL);
 
70
    } else {
 
71
        g_warning ("Could not find application '%s'. The notification will not "
 
72
                   "have a 'Show Settings…' button.", LIVEPATCH_DESKTOP_FILE);
 
73
    }
 
74
 
 
75
    g_signal_connect (n, "closed", G_CALLBACK (gtk_main_quit), NULL);
 
76
 
 
77
    if (!notify_notification_show (n, &error)) {
 
78
        g_warning ("Could not show notification: '%s", error->message);
 
79
        return FALSE;
 
80
    }
 
81
 
 
82
    return TRUE;
38
83
}
39
84
 
40
85
static void
94
139
    return difftime (status_stat.st_mtim.tv_sec, boot_timestamp) >= 0;
95
140
}
96
141
 
97
 
static void
 
142
static gboolean
98
143
show_status_notification ()
99
144
{
100
145
    g_autofree gchar *event = NULL;
101
146
    g_autofree gchar *description = NULL;
102
147
 
103
148
    if (!g_file_test (STATUS_PATH, G_FILE_TEST_EXISTS))
104
 
        return;
 
149
        return FALSE;
105
150
 
106
151
    if (!file_modified_after_boot (STATUS_PATH))
107
 
        return;
 
152
        return FALSE;
108
153
 
109
154
    get_event_from_file (STATUS_PATH, &event, &description);
110
155
 
120
165
 
121
166
        if (is_overflow || conversion_failed) {
122
167
            g_warning ("Failed to parse the status file");
 
168
            return FALSE;
123
169
        } else if (num_updates != 0) {
124
170
            body = g_strdup_printf (
125
171
                ngettext ("%" G_GUINT64_FORMAT " Livepatch update has been successfully applied.",
127
173
                          num_updates),
128
174
                num_updates);
129
175
 
130
 
            show_notification (_("Canonical Livepatch"), body, NULL);
 
176
            return show_notification (_("Canonical Livepatch"), body, NULL);
131
177
        }
132
178
    }
 
179
 
 
180
    return FALSE;
133
181
}
134
182
 
135
183
int
136
184
main (int argc, char **argv)
137
185
{
 
186
    gtk_init (&argc, &argv);
138
187
    init_notification ();
139
188
    init_gettext ();
140
189
 
141
 
    show_status_notification ();
 
190
    if (show_status_notification ())
 
191
        gtk_main ();
142
192
 
143
193
    return EXIT_SUCCESS;
144
194
}