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

« back to all changes in this revision

Viewing changes to src/reboot.c

  • Committer: seb128
  • Date: 2009-06-26 16:07:39 UTC
  • mto: This revision was merged to the branch mainline in revision 465.
  • Revision ID: seb128@seb128-laptop-20090626160739-hs300xxw3o75mz4s
clean libglade use

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
#include <unistd.h>
8
8
 
9
9
#include <libnotify/notify.h>
10
 
#include <dbus/dbus-glib.h>
11
 
#include <dbus/dbus.h>
12
10
 
13
11
#include "update-notifier.h"
14
12
#include "update.h"
 
13
#include "gdm-logout-action.h"
15
14
 
16
15
static GtkBuilder *builder;
17
16
 
47
46
        return FALSE;
48
47
}
49
48
 
50
 
static gboolean
51
 
gdm_action_reboot()
52
 
{
53
 
   DBusGConnection *connection;
54
 
   GError *error;
55
 
   DBusGProxy *proxy;
56
 
 
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
 
 
80
 
static gboolean
81
 
hal_action_reboot()
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,
95
 
                                     "org.freedesktop.Hal",
96
 
                                     "/org/freedesktop/Hal/devices/computer",
97
 
                                     "org.freedesktop.Hal.Device.SystemPowerManagement");
98
 
  if (proxy == NULL)
99
 
     return FALSE;
100
 
 
101
 
  error = NULL;
102
 
  if (!dbus_g_proxy_call (proxy, "Reboot", &error,
103
 
                          G_TYPE_INVALID, G_TYPE_INVALID)) {
104
 
     g_error_free (error);
105
 
     return FALSE;
106
 
  }
107
 
  return TRUE;
108
 
 
109
 
}
110
 
 
111
49
static void
112
50
request_reboot (void)
113
51
{
114
 
   if(!gdm_action_reboot() && !hal_action_reboot()) {
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
 
   }
 
52
        GnomeClient *client;
 
53
 
 
54
        client = gnome_master_client ();
 
55
        g_return_if_fail (client != NULL);
 
56
 
 
57
        /* set gdm logout action */
 
58
        gdm_set_logout_action(GDM_LOGOUT_ACTION_REBOOT);
 
59
 
 
60
        /* Tell gnome-session to save and exit the session without asking
 
61
         * the user. */
 
62
        gnome_client_request_save (client,
 
63
                                   GNOME_SAVE_GLOBAL,
 
64
                                   TRUE,
 
65
                                   GNOME_INTERACT_ANY,
 
66
                                   TRUE,
 
67
                                   TRUE);
126
68
}
127
69
 
128
70
 
138
80
   gtk_widget_hide (dia);
139
81
}
140
82
 
141
 
static gboolean
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
 
 
181
 
   g_debug("aptdaemon on bus: %i", res);
182
 
   return res;
183
 
}
184
 
 
185
 
static gboolean
186
 
aptdaemon_pending_transactions ()
187
 
{
188
 
   DBusGConnection *connection;
189
 
   GError *error;
190
 
   DBusGProxy *proxy;
191
 
   char *current = NULL;
192
 
   char **pending = NULL;
193
 
  
194
 
   if (!is_aptdaemon_on_the_system_bus())
195
 
      return FALSE;
196
 
 
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
 
 
232
 
static void
233
 
do_reboot_check (TrayApplet *ta)
234
 
{
 
83
gboolean
 
84
reboot_check (TrayApplet *ta)
 
85
{
 
86
        static gboolean already_asked_for_reboot = FALSE;
235
87
        struct stat statbuf;
236
88
 
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))
241
 
           return;
242
 
        // no auto-open of this dialog 
243
 
        if(gconf_client_get_bool((GConfClient*) ta->user_data,
244
 
                                 GCONF_KEY_AUTO_LAUNCH, NULL)) {
245
 
           g_debug ("Skipping reboot required");
246
 
           return;
247
 
        }
248
 
 
249
89
        /* If the file doesn't exist, we don't need to reboot */
250
90
        if (stat (REBOOT_FILE, &statbuf)) {
251
91
                NotifyNotification *n;
257
97
                        notify_notification_close (n, NULL);
258
98
                g_object_set_data (G_OBJECT(ta->tray_icon), "notification", NULL);
259
99
 
260
 
                return;
 
100
                return TRUE;
 
101
        }
 
102
 
 
103
        // check if we are in auto-launch dialog/apps mode
 
104
        if(gconf_client_get_bool((GConfClient*) ta->user_data,
 
105
                                 GCONF_KEY_AUTO_LAUNCH, NULL)) {
 
106
           if (!already_asked_for_reboot)
 
107
              ask_reboot_required(ta, FALSE);
 
108
           already_asked_for_reboot = TRUE;
 
109
           return TRUE;
261
110
        }
262
111
 
263
112
        /* Skip the rest if the icon is already visible */
264
113
        if (gtk_status_icon_get_visible (ta->tray_icon))
265
 
           return;
 
114
           return TRUE;
 
115
 
266
116
        gtk_status_icon_set_tooltip (ta->tray_icon,  
267
117
                                     _("System restart required"));
268
118
        gtk_status_icon_set_visible (ta->tray_icon, TRUE);
270
120
        /* Check whether the user doesn't like notifications */
271
121
        if (gconf_client_get_bool ((GConfClient*) ta->user_data,
272
122
                                   GCONF_KEY_NO_UPDATE_NOTIFICATIONS, NULL))
273
 
                return;
 
123
                return TRUE;
274
124
 
275
125
        /* Show the notification, after a delay so it doesn't look ugly
276
126
         * if we've just logged in */
277
127
        g_timeout_add(5000, (GSourceFunc)(show_notification), ta);
278
128
 
279
 
}
280
 
 
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;
 
129
        return TRUE;
289
130
}
290
131
 
291
132
static gboolean
306
147
 
307
148
        builder = gtk_builder_new ();
308
149
        if (!gtk_builder_add_from_file (builder, UIDIR"reboot-dialog.ui", &error)) {
309
 
                g_warning ("Couldn't load builder file: %s", error->message);
 
150
                g_warning ("Couldn't load builder file: s", error->message);
310
151
                g_error_free (error);
311
152
        }
312
153