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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/* livepatch-tray.c
 * Copyright (C) 2019 Canonical Ltd
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
 * Boston, MA  02110-1301 USA.
 */
#include <gio/gdesktopappinfo.h>
#include <glib.h>

#include "update-notifier.h"
#include "livepatch-tray.h"
#include "livepatch-utils.h"
#include "trayappletui.h"

#define LIVEPATCH_COMMON_DIRECTORY "/var/snap/canonical-livepatch/common"
#define LIVEPATCH_SNAP_DIRECTORY "/snap/canonical-livepatch"
#define TIMEOUT_SECONDS_DELAY 2

typedef struct _LivepatchTrayAppletPriv LivepatchTrayAppletPriv;
struct _LivepatchTrayAppletPriv
{
  GtkWidget    *menuitem_enabled;
  GtkWidget    *menuitem_desc;

  GFileMonitor *common_dir_monitor;
  GFileMonitor *snap_dir_monitor;

  guint         timeout_id;
};

static void
on_settings_menuitem_activated(GObject *self, gpointer user_data)
{
  g_autoptr(GDesktopAppInfo) info = NULL;
  g_autoptr(GdkAppLaunchContext) context = NULL;
  g_autoptr(GError) error = NULL;

  info = g_desktop_app_info_new(LIVEPATCH_DESKTOP_FILE);
  if (info == NULL)
    {
      g_warning("Could not find application '%s'", LIVEPATCH_DESKTOP_FILE);
      return;
    }

  context = gdk_display_get_app_launch_context(gdk_display_get_default());
  if (!g_app_info_launch(G_APP_INFO(info), NULL,
                         G_APP_LAUNCH_CONTEXT(context), &error))
    {
      g_warning("Could not launch application '%s'", LIVEPATCH_DESKTOP_FILE);
    }
}


static void
livepatch_trayicon_create_menu(TrayApplet *ta)
{
  LivepatchTrayAppletPriv *priv = (LivepatchTrayAppletPriv *) ta->user_data;
  GtkWidget *menuitem;

  ta->menu = gtk_menu_new();

  priv->menuitem_enabled = gtk_menu_item_new();
  gtk_widget_set_sensitive(priv->menuitem_enabled, FALSE);
  gtk_menu_shell_append(GTK_MENU_SHELL(ta->menu), priv->menuitem_enabled);

  priv->menuitem_desc = gtk_menu_item_new();
  gtk_widget_set_sensitive(priv->menuitem_desc, FALSE);
  gtk_menu_shell_append(GTK_MENU_SHELL(ta->menu), priv->menuitem_desc);

  menuitem = gtk_separator_menu_item_new();
  gtk_menu_shell_append(GTK_MENU_SHELL(ta->menu), menuitem);

  menuitem = gtk_menu_item_new_with_label(_("Livepatch Settings…"));
  gtk_menu_shell_append(GTK_MENU_SHELL(ta->menu), menuitem);
  g_signal_connect(G_OBJECT(menuitem), "activate",
                   G_CALLBACK(on_settings_menuitem_activated), ta);

  gtk_widget_show_all(ta->menu);
}

static gboolean
check_livepatch(TrayApplet *ta)
{
  LivepatchTrayAppletPriv *priv = (LivepatchTrayAppletPriv *) ta->user_data;
  gboolean show_status_icon;
  g_autofree gchar *check_state = NULL;
  g_autofree gchar *state = NULL;
  g_autoptr(GError) error = NULL;

  show_status_icon = g_settings_get_boolean(ta->un->settings,
                                            SETTINGS_KEY_SHOW_LIVEPATCH_ICON);

  if (!show_status_icon ||
      !livepatch_is_supported() || 
      !livepatch_is_running ())
    {
      tray_applet_ui_set_visible(ta, FALSE);

      priv->timeout_id = 0;
      return G_SOURCE_REMOVE;
    }

  tray_applet_ui_set_visible(ta, TRUE);

  gtk_menu_item_set_label(GTK_MENU_ITEM(priv->menuitem_enabled),
                          _("Livepatch is on"));

  check_state = livepatch_get_check_state(&error);
  if (check_state == NULL)
    g_warning("Cannot get Livepatch check-state: %s", error->message);

  state = livepatch_get_state(&error);
  if (state == NULL)
    g_warning("Cannot get Livepatch state: %s", error->message);

  if (!g_strcmp0(check_state, "checked") &&
      (!g_strcmp0(state, "applied") || !g_strcmp0(state, "nothing-to-apply")))
    {
      gssize num_fixes;
      g_autofree gchar *label = NULL;

      tray_applet_ui_set_icon(ta, "livepatch-on");

      num_fixes = livepatch_get_num_fixes(&error);
      if (num_fixes == -1)
        {
          g_warning("Cannot get applied Livepatch fixes: %s", error->message);
          num_fixes = 0;
        }

      gtk_widget_set_visible(priv->menuitem_desc, TRUE);

      if (num_fixes == 0)
        label = g_strdup(_("No current updates"));
      else
        label = g_strdup_printf(ngettext("%" G_GSSIZE_FORMAT " current update",
                                         "%" G_GSSIZE_FORMAT " current updates",
                                         num_fixes), num_fixes);

      gtk_menu_item_set_label(GTK_MENU_ITEM(priv->menuitem_desc), label);
    }
  else if (!g_strcmp0(check_state, "needs-check") ||
           !g_strcmp0(state, "unapplied") ||
           !g_strcmp0(state, "applying"))
    {
      /* Check livepatch status again */
      return G_SOURCE_CONTINUE;
    }
  else
    {
      tray_applet_ui_set_icon(ta, "livepatch-warning");

      if (check_state == NULL || !g_strcmp0(check_state, "check-failed"))
        {
          gtk_menu_item_set_label(GTK_MENU_ITEM(priv->menuitem_desc),
            _("An error occured when checking for Livepatch updates."));
        }
      else
        {
          gtk_menu_item_set_label(GTK_MENU_ITEM(priv->menuitem_desc),
            _("An error occured when applying Livepatch updates."));
        }
    }

    priv->timeout_id = 0;
    return G_SOURCE_REMOVE;
}

static void
gsettings_visibility_changed_cb(GSettings *settings,
                                gchar     *key,
                                gpointer   user_data)
{
  TrayApplet *ta = (TrayApplet *) user_data;
  LivepatchTrayAppletPriv *priv = (LivepatchTrayAppletPriv *) ta->user_data;

  if (priv->timeout_id <= 0)
    priv->timeout_id = g_timeout_add_seconds(0, (GSourceFunc) check_livepatch, ta);
}

static void
livepatch_directory_changed_cb(GFileMonitor     *monitor,
                               GFile            *file,
                               GFile            *other_file,
                               GFileMonitorEvent event_type,
                               gpointer          user_data)
{
  TrayApplet *ta = (TrayApplet *) user_data;
  LivepatchTrayAppletPriv *priv = (LivepatchTrayAppletPriv *) ta->user_data;

  if (priv->timeout_id <= 0)
    priv->timeout_id = g_timeout_add_seconds(TIMEOUT_SECONDS_DELAY,
                                             (GSourceFunc) check_livepatch, ta);
}

void
livepatch_tray_icon_init(TrayApplet *ta)
{
  g_autoptr(GFile) livepatch_common_dir = NULL;
  g_autoptr(GFile) livepatch_snap_dir = NULL;
  g_autoptr(GFileMonitor) common_dir_monitor = NULL;
  g_autoptr(GFileMonitor) snap_dir_monitor = NULL;
  g_autoptr(GError) error = NULL;
  LivepatchTrayAppletPriv *priv;

  if (!livepatch_is_supported())
    return;

  if (!livepatch_has_settings_ui())
    {
      g_warning("There is no graphical application installed to manage "
                "Livepatch. The livepatch status icon will not be displayed.");
      return;
    }

  /* Monitor the directory LIVEPATCH_COMMON_DIRECTORY for changes to update
     the status of the indicator. */
  livepatch_common_dir = g_file_new_for_path(LIVEPATCH_COMMON_DIRECTORY);
  common_dir_monitor = g_file_monitor_directory(livepatch_common_dir,
                                                G_FILE_MONITOR_NONE,
                                                NULL, &error);
  if (common_dir_monitor == NULL)
    {
        g_warning("Couldn't create directory monitor on %s. Error: %s\n",
                  LIVEPATCH_COMMON_DIRECTORY, error->message);
        return;
    }

  /* We also need to monitor the directory LIVEPATCH_SNAP_DIRECTORY in order to
     detect if the snap was enabled/disabled. This consider the case
     canonical-livepatch is enabled but the snap is disabled. */
  livepatch_snap_dir = g_file_new_for_path(LIVEPATCH_SNAP_DIRECTORY);
  snap_dir_monitor = g_file_monitor_directory(livepatch_snap_dir,
                                              G_FILE_MONITOR_WATCH_MOUNTS,
                                              NULL, &error);
  if (snap_dir_monitor == NULL)
    {
        g_warning("Couldn't create directory monitor on %s. Error: %s\n",
                  LIVEPATCH_SNAP_DIRECTORY, error->message);
        return;
    }

  priv = g_new0(LivepatchTrayAppletPriv, 1);
  priv->common_dir_monitor = g_steal_pointer(&common_dir_monitor);
  priv->snap_dir_monitor = g_steal_pointer(&snap_dir_monitor);
  ta->user_data = priv;

  tray_applet_ui_ensure(ta);

  /* Menu initialization */
  livepatch_trayicon_create_menu(ta);
  tray_applet_ui_set_menu(ta, ta->menu);

  g_signal_connect(ta->un->settings,
                   "changed::"SETTINGS_KEY_SHOW_LIVEPATCH_ICON,
                   G_CALLBACK(gsettings_visibility_changed_cb), ta);
  g_signal_connect(priv->common_dir_monitor,
                   "changed",
                   G_CALLBACK(livepatch_directory_changed_cb), ta);
  g_signal_connect(priv->snap_dir_monitor,
                   "changed",
                   G_CALLBACK(livepatch_directory_changed_cb), ta);

  /* We always run check_livepatch in a timeout beacuse this allows us to easily
     check again the status of livepatch if it is in a transistion state (e.g.
     it is downloading the patches or applying them, etc.) */
  priv->timeout_id = g_timeout_add_seconds(TIMEOUT_SECONDS_DELAY,
                                           (GSourceFunc) check_livepatch, ta);
}