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

178 by mvo
* merged the changes from scott
1
#ifdef HAVE_CONFIG_H
2
#include "config.h"
3
#endif
4
5
#include <sys/types.h>
6
#include <sys/stat.h>
7
#include <unistd.h>
8
9
#include <libnotify/notify.h>
473 by Michael Vogt
* src/reboot.c:
10
#include <dbus/dbus-glib.h>
594 by Michael Vogt
* src/reboot.c:
11
#include <dbus/dbus.h>
178 by mvo
* merged the changes from scott
12
13
#include "update-notifier.h"
14
#include "update.h"
15
464.1.5 by seb128
reboot works using gtkbuilder now
16
static GtkBuilder *builder;
178 by mvo
* merged the changes from scott
17
334 by Michael Vogt
* fix crash in leftover from pre-GtkStatusIcon code (LP: #127965)
18
static gboolean
178 by mvo
* merged the changes from scott
19
show_notification (TrayApplet *ta)
20
{
21
	NotifyNotification *n;
22
333 by Michael Vogt
* fix leftover from pre-GtkStatusIcon code (LP: #127965)
23
	GdkRectangle area;
24
	gtk_status_icon_get_geometry(ta->tray_icon, NULL, &area, NULL);
334 by Michael Vogt
* fix crash in leftover from pre-GtkStatusIcon code (LP: #127965)
25
333 by Michael Vogt
* fix leftover from pre-GtkStatusIcon code (LP: #127965)
26
	// no usefull coordiante yet, do another timeout
27
	if(area.x <= 0 || area.y <= 0 || area.width <= 0 || area.height <= 0)
28
	   return TRUE;
178 by mvo
* merged the changes from scott
29
334 by Michael Vogt
* fix crash in leftover from pre-GtkStatusIcon code (LP: #127965)
30
	// only show once the icon is realy availabe
31
	if(!gtk_status_icon_get_visible(ta->tray_icon))
32
	   return TRUE;
33
178 by mvo
* merged the changes from scott
34
	/* Create and show the notification */
334 by Michael Vogt
* fix crash in leftover from pre-GtkStatusIcon code (LP: #127965)
35
	n = notify_notification_new_with_status_icon(
36
				     _("System restart required"),
207.1.4 by Sebastian Heinlein
* Wording fixes
37
				     _("To complete the update of your system, "
377 by Michael Vogt
better wording in the reboot dialog (LP: #211616)
38
				       "please restart it.\n\n"
39
				       "Click on the notification icon for "
40
				       "details."),
178 by mvo
* merged the changes from scott
41
				     GTK_STOCK_DIALOG_WARNING,
334 by Michael Vogt
* fix crash in leftover from pre-GtkStatusIcon code (LP: #127965)
42
				     ta->tray_icon);
178 by mvo
* merged the changes from scott
43
	notify_notification_set_timeout (n, 60000);
44
	notify_notification_show (n, NULL);
45
	g_object_set_data (G_OBJECT(ta->tray_icon), "notification", n);
46
47
	return FALSE;
48
}
49
473 by Michael Vogt
* src/reboot.c:
50
static gboolean
473.1.1 by Michael Vogt
dropped libgnome
51
gdm_action_reboot()
473 by Michael Vogt
* src/reboot.c:
52
{
53
   DBusGConnection *connection;
54
   GError *error;
55
   DBusGProxy *proxy;
475 by Michael Vogt
drop libgnome from the build-depends and switch to
56
473 by Michael Vogt
* src/reboot.c:
57
   error = NULL;
58
   connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
59
   if (connection == NULL) {
60
      g_error_free (error);
61
      return FALSE;
62
   }
63
64
  proxy = dbus_g_proxy_new_for_name (connection,
65
                                     "org.gnome.SessionManager",
66
				     "/org/gnome/SessionManager",
67
				     "org.gnome.SessionManager");
68
  if (proxy == NULL)
69
     return FALSE;
70
71
  error = NULL;
72
  if (!dbus_g_proxy_call (proxy, "RequestReboot", &error, 
73
			  G_TYPE_INVALID, G_TYPE_INVALID)) {
74
     g_error_free (error);
75
     return FALSE;
76
  }
77
  return TRUE;
78
}
79
499.1.1 by Mario Limonciello
* src/reboot.c:
80
static gboolean
619 by Lionel Le Folgoc
src/reboot.c: use consolekit instead of hal when gnome-session is
81
ck_action_reboot()
499.1.1 by Mario Limonciello
* src/reboot.c:
82
{
83
   DBusGConnection *connection;
84
   GError *error;
85
   DBusGProxy *proxy;
86
87
   error = NULL;
88
   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
89
   if (connection == NULL) {
90
      g_error_free (error);
91
      return FALSE;
92
   }
93
94
  proxy = dbus_g_proxy_new_for_name (connection,
619 by Lionel Le Folgoc
src/reboot.c: use consolekit instead of hal when gnome-session is
95
                                     "org.freedesktop.ConsoleKit",
96
                                     "/org/freedesktop/ConsoleKit/Manager",
97
                                     "org.freedesktop.ConsoleKit.Manager");
499.1.1 by Mario Limonciello
* src/reboot.c:
98
  if (proxy == NULL)
99
     return FALSE;
100
101
  error = NULL;
619 by Lionel Le Folgoc
src/reboot.c: use consolekit instead of hal when gnome-session is
102
  if (!dbus_g_proxy_call (proxy, "Restart", &error,
499.1.1 by Mario Limonciello
* src/reboot.c:
103
                          G_TYPE_INVALID, G_TYPE_INVALID)) {
104
     g_error_free (error);
105
     return FALSE;
106
  }
107
  return TRUE;
108
109
}
110
435 by Michael Vogt
make the reboot required dialog open up immediately if "auto_launch"
111
static void
112
request_reboot (void)
113
{
619 by Lionel Le Folgoc
src/reboot.c: use consolekit instead of hal when gnome-session is
114
   if(!gdm_action_reboot() && !ck_action_reboot()) {
475 by Michael Vogt
drop libgnome from the build-depends and switch to
115
      const char *fmt, *msg, *details;
116
      fmt = "<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s\n";
117
      msg = _("Reboot failed");
118
      details = _("Failed to request reboot, please shutdown manually");
119
      GtkWidget *dlg = gtk_message_dialog_new_with_markup(NULL, 0,
120
							  GTK_MESSAGE_ERROR,
121
							  GTK_BUTTONS_CLOSE,
122
							  fmt, msg, details);
123
      gtk_dialog_run(GTK_DIALOG(dlg));
124
      gtk_widget_destroy(dlg);
125
   }
435 by Michael Vogt
make the reboot required dialog open up immediately if "auto_launch"
126
}
127
128
129
static void
130
ask_reboot_required(TrayApplet *ta, gboolean focus_on_map)
131
{
132
   GtkWidget *dia;
133
   
464.1.5 by seb128
reboot works using gtkbuilder now
134
   dia = GTK_WIDGET (gtk_builder_get_object (builder, "dialog_reboot"));
435 by Michael Vogt
make the reboot required dialog open up immediately if "auto_launch"
135
   gtk_window_set_focus_on_map(GTK_WINDOW(dia), focus_on_map);
136
   if (gtk_dialog_run (GTK_DIALOG(dia)) == GTK_RESPONSE_OK)
137
      request_reboot ();
138
   gtk_widget_hide (dia);
139
}
140
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
141
static gboolean
594 by Michael Vogt
* src/reboot.c:
142
is_aptdaemon_on_the_system_bus ()
143
{
144
   DBusConnection *connection;
145
   DBusError *dbus_error = NULL;
146
   DBusMessage *message, *reply;
147
   const char *aptdaemon_bus_name = "org.debian.apt";
148
   gboolean res;
149
150
   connection = dbus_bus_get(DBUS_BUS_SYSTEM, dbus_error);
151
   if (dbus_error != NULL) {
152
      g_warning("failed to connect to the system bus");
153
      return FALSE;
154
   }
155
   message = dbus_message_new_method_call(DBUS_SERVICE_DBUS, 
156
                                          DBUS_PATH_DBUS,
157
                                          DBUS_INTERFACE_DBUS,
158
                                          "GetNameOwner");
159
   if (message == NULL) {
160
      g_warning ("failed to create dbus message");
161
      return FALSE;
162
   }
163
   
164
   dbus_message_append_args(message,
165
                            DBUS_TYPE_STRING, &aptdaemon_bus_name,
166
                            DBUS_TYPE_INVALID);
167
168
   reply = dbus_connection_send_with_reply_and_block(connection,
169
                                                     message,
170
                                                     -1,
171
                                                     dbus_error);
172
   dbus_message_unref(message);
173
174
   if (reply) {
175
      dbus_message_unref(reply);
176
      res = TRUE;
177
   } else {
178
      res =  FALSE;
179
   }
180
607 by Michael Vogt
merged from seb128, modified slightly to make the unity special case more obvious
181
   g_debug("aptdaemon on bus: %i", res);
594 by Michael Vogt
* src/reboot.c:
182
   return res;
183
}
184
185
static gboolean
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
186
aptdaemon_pending_transactions ()
187
{
188
   DBusGConnection *connection;
189
   GError *error;
190
   DBusGProxy *proxy;
191
   char *current = NULL;
192
   char **pending = NULL;
193
  
594 by Michael Vogt
* src/reboot.c:
194
   if (!is_aptdaemon_on_the_system_bus())
195
      return FALSE;
196
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
197
   error = NULL;
198
   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
199
   if (connection == NULL) {
200
      g_debug ("Failed to open connection to bus: %s\n", error->message);
201
      g_error_free (error);
202
      return FALSE;
203
   }
204
205
  proxy = dbus_g_proxy_new_for_name (connection,
206
                                     "org.debian.apt",
207
				     "/org/debian/apt",
208
				     "org.debian.apt");
209
  error = NULL;
210
  if (!dbus_g_proxy_call (proxy, "GetActiveTransactions", &error, 
211
			  G_TYPE_INVALID,
212
                          G_TYPE_STRING, &current, 
213
			  G_TYPE_STRV, &pending,
214
			  G_TYPE_INVALID)) {
215
     g_debug ("error during dbus call: %s\n", error->message);
216
     g_error_free (error);
217
     g_object_unref (proxy);
218
     return FALSE;
219
  }
220
221
  gboolean has_pending = FALSE;
222
  if ((current && strcmp(current,"") != 0) || g_strv_length(pending) > 0)
223
     has_pending = TRUE;
224
225
  g_object_unref (proxy);
226
  g_free (current);
227
  g_strfreev (pending);
228
229
  return has_pending;
230
}
231
573 by Michael Vogt
merged from debian
232
static void
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
233
do_reboot_check (TrayApplet *ta)
234
{
178 by mvo
* merged the changes from scott
235
	struct stat statbuf;
236
469 by Michael Vogt
add /apps/update-notifier/hide_reboot_notification
237
	// if we are not supposed to show the reboot notification
238
	// just skip it 
239
	if(gconf_client_get_bool((GConfClient*) ta->user_data,
240
				 GCONF_KEY_HIDE_REBOOT, NULL))
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
241
	   return;
538 by Michael Vogt
* src/reboot.c:
242
	// no auto-open of this dialog 
243
	if(gconf_client_get_bool((GConfClient*) ta->user_data,
244
				 GCONF_KEY_AUTO_LAUNCH, NULL)) {
607 by Michael Vogt
merged from seb128, modified slightly to make the unity special case more obvious
245
           g_debug ("Skipping reboot required");
538 by Michael Vogt
* src/reboot.c:
246
	   return;
247
	}
469 by Michael Vogt
add /apps/update-notifier/hide_reboot_notification
248
178 by mvo
* merged the changes from scott
249
	/* If the file doesn't exist, we don't need to reboot */
250
	if (stat (REBOOT_FILE, &statbuf)) {
251
		NotifyNotification *n;
252
329 by Michael Vogt
* use GtkStatusIcon instead of EggTrayIcon, that makes
253
		gtk_status_icon_set_visible (ta->tray_icon, FALSE);
178 by mvo
* merged the changes from scott
254
		/* Hide any notification popup */
255
		n = g_object_get_data (G_OBJECT(ta->tray_icon), "notification");
256
		if (n)
257
			notify_notification_close (n, NULL);
258
		g_object_set_data (G_OBJECT(ta->tray_icon), "notification", NULL);
259
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
260
		return;
178 by mvo
* merged the changes from scott
261
	}
262
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
263
	/* Skip the rest if the icon is already visible */
264
	if (gtk_status_icon_get_visible (ta->tray_icon))
265
	   return;
329 by Michael Vogt
* use GtkStatusIcon instead of EggTrayIcon, that makes
266
	gtk_status_icon_set_tooltip (ta->tray_icon,  
267
				     _("System restart required"));
268
	gtk_status_icon_set_visible (ta->tray_icon, TRUE);
178 by mvo
* merged the changes from scott
269
270
	/* Check whether the user doesn't like notifications */
271
	if (gconf_client_get_bool ((GConfClient*) ta->user_data,
272
				   GCONF_KEY_NO_UPDATE_NOTIFICATIONS, NULL))
501 by Michael Vogt
- Fallback to HAL if possible to reboot (LP: #437333)
273
		return;
178 by mvo
* merged the changes from scott
274
275
	/* Show the notification, after a delay so it doesn't look ugly
276
	 * if we've just logged in */
208 by Michael Vogt
* -Wall clean now
277
	g_timeout_add(5000, (GSourceFunc)(show_notification), ta);
178 by mvo
* merged the changes from scott
278
279
}
280
573 by Michael Vogt
merged from debian
281
gboolean
282
reboot_check (TrayApplet *ta)
283
{
284
   if (aptdaemon_pending_transactions())
285
      g_timeout_add_seconds (5, (GSourceFunc)reboot_check, ta);
286
   else
287
      do_reboot_check(ta);
288
   return FALSE;
289
}
290
178 by mvo
* merged the changes from scott
291
static gboolean
292
button_release_cb (GtkWidget *widget,
293
		   TrayApplet *ta)
294
{
435 by Michael Vogt
make the reboot required dialog open up immediately if "auto_launch"
295
   ask_reboot_required(ta, TRUE);
329 by Michael Vogt
* use GtkStatusIcon instead of EggTrayIcon, that makes
296
297
   return TRUE;
178 by mvo
* merged the changes from scott
298
}
299
300
301
void
302
reboot_tray_icon_init (TrayApplet *ta)
303
{
304
	GtkWidget *widget;
464.1.1 by seb128
use gtkbuilder for the reboot dialog
305
	GError* error = NULL;
178 by mvo
* merged the changes from scott
306
464.1.1 by seb128
use gtkbuilder for the reboot dialog
307
	builder = gtk_builder_new ();
308
	if (!gtk_builder_add_from_file (builder, UIDIR"reboot-dialog.ui", &error)) {
476 by Michael Vogt
fix -Wall errors
309
		g_warning ("Couldn't load builder file: %s", error->message);
464.1.1 by seb128
use gtkbuilder for the reboot dialog
310
		g_error_free (error);
311
	}
178 by mvo
* merged the changes from scott
312
464.1.5 by seb128
reboot works using gtkbuilder now
313
	widget = GTK_WIDGET (gtk_builder_get_object (builder, "image"));
250 by Michael Vogt
* switch the code to use GtkIconTheme and install the icons
314
	GtkIconTheme* icon_theme = gtk_icon_theme_get_default();
315
	GdkPixbuf *pixbuf = gtk_icon_theme_load_icon (icon_theme, "un-reboot",
316
						      48, 0,NULL);
367 by Michael Vogt
* src/reboot.c:
317
	gtk_status_icon_set_from_pixbuf (ta->tray_icon, pixbuf);
178 by mvo
* merged the changes from scott
318
	ta->user_data = gconf_client_get_default();
319
320
        g_signal_connect (G_OBJECT(ta->tray_icon),
329 by Michael Vogt
* use GtkStatusIcon instead of EggTrayIcon, that makes
321
			  "activate",
178 by mvo
* merged the changes from scott
322
			  G_CALLBACK (button_release_cb),
323
			  ta);
324
367 by Michael Vogt
* src/reboot.c:
325
	gtk_image_set_from_pixbuf(GTK_IMAGE(widget), pixbuf);
326
178 by mvo
* merged the changes from scott
327
	/* Check for updates for the first time */
328
	reboot_check (ta);
329
}