~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
282
283
284
285
286
287
288
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <libnotify/notify.h>
#include <gio/gio.h>

#include "update-notifier.h"
#include "update.h"

static GtkBuilder *builder;

static gboolean
show_notification (TrayApplet *ta)
{
	NotifyNotification *n;

	// only show once the icon is realy availabe
	if(!gtk_status_icon_get_visible(ta->tray_icon))
	   return TRUE;

	/* Create and show the notification */
	n = notify_notification_new(
				     _("System restart required"),
				     _("To finish updating your system, "
				       "please restart it.\n\n"
				       "Click on the notification icon for "
				       "details."),
				     GTK_STOCK_DIALOG_WARNING);
	notify_notification_set_timeout (n, 60000);
	notify_notification_show (n, NULL);

	return FALSE;
}

static gboolean
gdm_action_reboot()
{
  GVariant *answer;
  GDBusProxy *proxy;

  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL, /* GDBusInterfaceInfo */
                                         "org.gnome.SessionManager",
                                         "/org/gnome/SessionManager",
                                         "org.gnome.SessionManager",
                                         NULL, /* GCancellable */
                                         NULL /* GError */);
  if (proxy == NULL)
     return FALSE;

  answer = g_dbus_proxy_call_sync (proxy, "RequestReboot", NULL,
                                   G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
  g_object_unref (proxy);

  if (answer == NULL)
    return FALSE;

  g_variant_unref (answer);
  return TRUE;
}

static gboolean
ck_action_reboot()
{
  GVariant *answer;
  GDBusProxy *proxy;

  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                         G_DBUS_PROXY_FLAGS_NONE,
                                         NULL, /* GDBusInterfaceInfo */
                                         "org.freedesktop.ConsoleKit",
                                         "/org/freedesktop/ConsoleKit/Manager",
                                         "org.freedesktop.ConsoleKit.Manager",
                                         NULL, /* GCancellable */
                                         NULL /* GError */);
  if (proxy == NULL)
     return FALSE;

  answer = g_dbus_proxy_call_sync (proxy, "Restart", NULL,
                                   G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
  g_object_unref (proxy);

  if (answer == NULL)
    return FALSE;

  g_variant_unref (answer);
  return TRUE;

}

static void
request_reboot (void)
{
   if(!gdm_action_reboot() && !ck_action_reboot()) {
      const char *fmt, *msg, *details;
      fmt = "<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n";
      msg = _("Reboot failed");
      details = _("Failed to request reboot, please shutdown manually");
      GtkWidget *dlg = gtk_message_dialog_new_with_markup(NULL, 0,
							  GTK_MESSAGE_ERROR,
							  GTK_BUTTONS_CLOSE,
							  fmt, msg, details);
      gtk_dialog_run(GTK_DIALOG(dlg));
      gtk_widget_destroy(dlg);
   }
}


static void
ask_reboot_required(TrayApplet *ta, gboolean focus_on_map)
{
   GtkWidget *dia;
   
   dia = GTK_WIDGET (gtk_builder_get_object (builder, "dialog_reboot"));
   gtk_window_set_focus_on_map(GTK_WINDOW(dia), focus_on_map);
   if (gtk_dialog_run (GTK_DIALOG(dia)) == GTK_RESPONSE_OK)
      request_reboot ();
   gtk_widget_hide (dia);
}

static gboolean
aptdaemon_pending_transactions ()
{
  GError *error;
  GVariant *answer;
  GDBusProxy *proxy;
  char *owner = NULL;
  const char *current = NULL;
  char **pending = NULL;

  error = NULL;
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
                                         NULL, /* GDBusInterfaceInfo */
                                         "org.debian.apt",
                                         "/org/debian/apt",
                                         "org.debian.apt",
                                         NULL, /* GCancellable */
                                         &error);
  if (proxy == NULL) {
    g_debug ("Failed to open connection to bus: %s\n", error->message);
    g_error_free (error);
    return FALSE;
  }

  owner = g_dbus_proxy_get_name_owner (proxy);
  g_debug("aptdaemon on bus: %i", (owner != NULL));
  if (owner == NULL) {
    g_object_unref (proxy);
    g_free (owner);
    return FALSE;
  }
  g_free (owner);

  error = NULL;
  answer = g_dbus_proxy_call_sync (proxy, "GetActiveTransactions", NULL,
                                   G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
  g_object_unref (proxy);

  if (answer == NULL) {
    g_debug ("error during dbus call: %s\n", error->message);
    g_error_free (error);
    return FALSE;
  }

  if (g_strcmp0 (g_variant_get_type_string (answer), "(sas)") != 0) {
    g_debug ("aptd answer in unexpected format: %s\n",
             g_variant_get_type_string (answer));
    g_variant_unref (answer);
    return FALSE;
  }

  g_variant_get (answer, "(&s^a&s)", &current, &pending);

  gboolean has_pending = FALSE;
  if ((current && strcmp(current,"") != 0) || g_strv_length(pending) > 0)
     has_pending = TRUE;

  g_free (pending);
  g_variant_unref (answer);

  return has_pending;
}

static void
do_reboot_check (TrayApplet *ta)
{
	struct stat statbuf;

	// if we are not supposed to show the reboot notification
	// just skip it 
	if(g_settings_get_boolean((GSettings*) ta->user_data,
	                          SETTINGS_KEY_HIDE_REBOOT))
	   return;
	// no auto-open of this dialog 
	if(g_settings_get_boolean((GSettings*) ta->user_data,
	                          SETTINGS_KEY_AUTO_LAUNCH)) {
	   g_debug ("Skipping reboot required");
	   return;
	}

	/* If the file doesn't exist, we don't need to reboot */
	if (stat (REBOOT_FILE, &statbuf)) {
		NotifyNotification *n;

		gtk_status_icon_set_visible (ta->tray_icon, FALSE);
		/* Hide any notification popup */
		n = g_object_get_data (G_OBJECT(ta->tray_icon), "notification");
		if (n)
			notify_notification_close (n, NULL);
		g_object_set_data (G_OBJECT(ta->tray_icon), "notification", NULL);

		return;
	}

	/* Skip the rest if the icon is already visible */
	if (gtk_status_icon_get_visible (ta->tray_icon))
	   return;
	gtk_status_icon_set_tooltip_text (ta->tray_icon,  
				     _("System restart required"));
	gtk_status_icon_set_visible (ta->tray_icon, TRUE);

	/* Check whether the user doesn't like notifications */
	if (g_settings_get_boolean ((GSettings*) ta->user_data,
	                            SETTINGS_KEY_NO_UPDATE_NOTIFICATIONS))
		return;

	/* Show the notification, after a delay so it doesn't look ugly
	 * if we've just logged in */
	g_timeout_add(5000, (GSourceFunc)(show_notification), ta);

}

gboolean
reboot_check (TrayApplet *ta)
{
   if (aptdaemon_pending_transactions())
      g_timeout_add_seconds (5, (GSourceFunc)reboot_check, ta);
   else
      do_reboot_check(ta);
   return FALSE;
}

static gboolean
button_release_cb (GtkWidget *widget,
		   TrayApplet *ta)
{
   ask_reboot_required(ta, TRUE);

   return TRUE;
}


void
reboot_tray_icon_init (TrayApplet *ta)
{
	GtkWidget *widget;
	GError* error = NULL;

	builder = gtk_builder_new ();
	if (!gtk_builder_add_from_file (builder, UIDIR"reboot-dialog.ui", &error)) {
		g_warning ("Couldn't load builder file: %s", error->message);
		g_error_free (error);
	}

	widget = GTK_WIDGET (gtk_builder_get_object (builder, "image"));
	GtkIconTheme* icon_theme = gtk_icon_theme_get_default();
	GdkPixbuf *pixbuf = gtk_icon_theme_load_icon (icon_theme, "un-reboot",
						      48, 0,NULL);
	gtk_status_icon_set_from_pixbuf (ta->tray_icon, pixbuf);
	ta->user_data = g_settings_new(SETTINGS_SCHEMA);

        g_signal_connect (G_OBJECT(ta->tray_icon),
			  "activate",
			  G_CALLBACK (button_release_cb),
			  ta);

	gtk_image_set_from_pixbuf(GTK_IMAGE(widget), pixbuf);

	/* Check for updates for the first time */
	reboot_check (ta);
}