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

« back to all changes in this revision

Viewing changes to src/reboot.c

  • Committer: Michael Terry
  • Date: 2011-07-12 21:12:26 UTC
  • mto: This revision was merged to the branch mainline in revision 631.
  • Revision ID: michael.terry@canonical.com-20110712211226-jo3e7ovsxovn7ue3
use gdbus instead of dbus-glib

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>
 
10
#include <gio/gio.h>
12
11
 
13
12
#include "update-notifier.h"
14
13
#include "update.h"
41
40
static gboolean
42
41
gdm_action_reboot()
43
42
{
44
 
   DBusGConnection *connection;
45
 
   GError *error;
46
 
   DBusGProxy *proxy;
47
 
 
48
 
   error = NULL;
49
 
   connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
50
 
   if (connection == NULL) {
51
 
      g_error_free (error);
52
 
      return FALSE;
53
 
   }
54
 
 
55
 
  proxy = dbus_g_proxy_new_for_name (connection,
56
 
                                     "org.gnome.SessionManager",
57
 
                                     "/org/gnome/SessionManager",
58
 
                                     "org.gnome.SessionManager");
 
43
  GVariant *answer;
 
44
  GDBusProxy *proxy;
 
45
 
 
46
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
 
47
                                         G_DBUS_PROXY_FLAGS_NONE,
 
48
                                         NULL, /* GDBusInterfaceInfo */
 
49
                                         "org.gnome.SessionManager",
 
50
                                         "/org/gnome/SessionManager",
 
51
                                         "org.gnome.SessionManager",
 
52
                                         NULL, /* GCancellable */
 
53
                                         NULL /* GError */);
59
54
  if (proxy == NULL)
60
55
     return FALSE;
61
56
 
62
 
  error = NULL;
63
 
  if (!dbus_g_proxy_call (proxy, "RequestReboot", &error, 
64
 
                          G_TYPE_INVALID, G_TYPE_INVALID)) {
65
 
     g_error_free (error);
66
 
     return FALSE;
67
 
  }
 
57
  answer = g_dbus_proxy_call_sync (proxy, "RequestReboot", NULL,
 
58
                                   G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
 
59
  g_object_unref (proxy);
 
60
 
 
61
  if (answer == NULL)
 
62
    return FALSE;
 
63
 
 
64
  g_variant_unref (answer);
68
65
  return TRUE;
69
66
}
70
67
 
71
68
static gboolean
72
69
ck_action_reboot()
73
70
{
74
 
   DBusGConnection *connection;
75
 
   GError *error;
76
 
   DBusGProxy *proxy;
77
 
 
78
 
   error = NULL;
79
 
   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
80
 
   if (connection == NULL) {
81
 
      g_error_free (error);
82
 
      return FALSE;
83
 
   }
84
 
 
85
 
  proxy = dbus_g_proxy_new_for_name (connection,
86
 
                                     "org.freedesktop.ConsoleKit",
87
 
                                     "/org/freedesktop/ConsoleKit/Manager",
88
 
                                     "org.freedesktop.ConsoleKit.Manager");
 
71
  GVariant *answer;
 
72
  GDBusProxy *proxy;
 
73
 
 
74
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
75
                                         G_DBUS_PROXY_FLAGS_NONE,
 
76
                                         NULL, /* GDBusInterfaceInfo */
 
77
                                         "org.freedesktop.ConsoleKit",
 
78
                                         "/org/freedesktop/ConsoleKit/Manager",
 
79
                                         "org.freedesktop.ConsoleKit.Manager",
 
80
                                         NULL, /* GCancellable */
 
81
                                         NULL /* GError */);
89
82
  if (proxy == NULL)
90
83
     return FALSE;
91
84
 
92
 
  error = NULL;
93
 
  if (!dbus_g_proxy_call (proxy, "Restart", &error,
94
 
                          G_TYPE_INVALID, G_TYPE_INVALID)) {
95
 
     g_error_free (error);
96
 
     return FALSE;
97
 
  }
 
85
  answer = g_dbus_proxy_call_sync (proxy, "Restart", NULL,
 
86
                                   G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
 
87
  g_object_unref (proxy);
 
88
 
 
89
  if (answer == NULL)
 
90
    return FALSE;
 
91
 
 
92
  g_variant_unref (answer);
98
93
  return TRUE;
99
94
 
100
95
}
130
125
}
131
126
 
132
127
static gboolean
133
 
is_aptdaemon_on_the_system_bus ()
134
 
{
135
 
   DBusConnection *connection;
136
 
   DBusError *dbus_error = NULL;
137
 
   DBusMessage *message, *reply;
138
 
   const char *aptdaemon_bus_name = "org.debian.apt";
139
 
   gboolean res;
140
 
 
141
 
   connection = dbus_bus_get(DBUS_BUS_SYSTEM, dbus_error);
142
 
   if (dbus_error != NULL) {
143
 
      g_warning("failed to connect to the system bus");
144
 
      return FALSE;
145
 
   }
146
 
   message = dbus_message_new_method_call(DBUS_SERVICE_DBUS, 
147
 
                                          DBUS_PATH_DBUS,
148
 
                                          DBUS_INTERFACE_DBUS,
149
 
                                          "GetNameOwner");
150
 
   if (message == NULL) {
151
 
      g_warning ("failed to create dbus message");
152
 
      return FALSE;
153
 
   }
154
 
   
155
 
   dbus_message_append_args(message,
156
 
                            DBUS_TYPE_STRING, &aptdaemon_bus_name,
157
 
                            DBUS_TYPE_INVALID);
158
 
 
159
 
   reply = dbus_connection_send_with_reply_and_block(connection,
160
 
                                                     message,
161
 
                                                     -1,
162
 
                                                     dbus_error);
163
 
   dbus_message_unref(message);
164
 
 
165
 
   if (reply) {
166
 
      dbus_message_unref(reply);
167
 
      res = TRUE;
168
 
   } else {
169
 
      res =  FALSE;
170
 
   }
171
 
 
172
 
   g_debug("aptdaemon on bus: %i", res);
173
 
   return res;
174
 
}
175
 
 
176
 
static gboolean
177
128
aptdaemon_pending_transactions ()
178
129
{
179
 
   DBusGConnection *connection;
180
 
   GError *error;
181
 
   DBusGProxy *proxy;
182
 
   char *current = NULL;
183
 
   char **pending = NULL;
184
 
  
185
 
   if (!is_aptdaemon_on_the_system_bus())
186
 
      return FALSE;
187
 
 
188
 
   error = NULL;
189
 
   connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
190
 
   if (connection == NULL) {
191
 
      g_debug ("Failed to open connection to bus: %s\n", error->message);
192
 
      g_error_free (error);
193
 
      return FALSE;
194
 
   }
195
 
 
196
 
  proxy = dbus_g_proxy_new_for_name (connection,
197
 
                                     "org.debian.apt",
198
 
                                     "/org/debian/apt",
199
 
                                     "org.debian.apt");
200
 
  error = NULL;
201
 
  if (!dbus_g_proxy_call (proxy, "GetActiveTransactions", &error, 
202
 
                          G_TYPE_INVALID,
203
 
                          G_TYPE_STRING, &current, 
204
 
                          G_TYPE_STRV, &pending,
205
 
                          G_TYPE_INVALID)) {
206
 
     g_debug ("error during dbus call: %s\n", error->message);
207
 
     g_error_free (error);
208
 
     g_object_unref (proxy);
209
 
     return FALSE;
210
 
  }
 
130
  GError *error;
 
131
  GVariant *answer;
 
132
  GDBusProxy *proxy;
 
133
  char *owner = NULL;
 
134
  const char *current = NULL;
 
135
  char **pending = NULL;
 
136
 
 
137
  error = NULL;
 
138
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
 
139
                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
 
140
                                         NULL, /* GDBusInterfaceInfo */
 
141
                                         "org.debian.apt",
 
142
                                         "/org/debian/apt",
 
143
                                         "org.debian.apt",
 
144
                                         NULL, /* GCancellable */
 
145
                                         &error);
 
146
  if (proxy == NULL) {
 
147
    g_debug ("Failed to open connection to bus: %s\n", error->message);
 
148
    g_error_free (error);
 
149
    return FALSE;
 
150
  }
 
151
 
 
152
  owner = g_dbus_proxy_get_name_owner (proxy);
 
153
  g_debug("aptdaemon on bus: %i", (owner != NULL));
 
154
  if (owner == NULL) {
 
155
    g_object_unref (proxy);
 
156
    g_free (owner);
 
157
    return FALSE;
 
158
  }
 
159
  g_free (owner);
 
160
 
 
161
  error = NULL;
 
162
  answer = g_dbus_proxy_call_sync (proxy, "GetActiveTransactions", NULL,
 
163
                                   G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
 
164
  g_object_unref (proxy);
 
165
 
 
166
  if (answer == NULL) {
 
167
    g_debug ("error during dbus call: %s\n", error->message);
 
168
    g_error_free (error);
 
169
    return FALSE;
 
170
  }
 
171
 
 
172
  if (g_strcmp0 (g_variant_get_type_string (answer), "(sas)") != 0) {
 
173
    g_debug ("aptd answer in unexpected format: %s\n",
 
174
             g_variant_get_type_string (answer));
 
175
    g_variant_unref (answer);
 
176
    return FALSE;
 
177
  }
 
178
 
 
179
  g_variant_get (answer, "(&s^a&s)", &current, &pending);
211
180
 
212
181
  gboolean has_pending = FALSE;
213
182
  if ((current && strcmp(current,"") != 0) || g_strv_length(pending) > 0)
214
183
     has_pending = TRUE;
215
184
 
216
 
  g_object_unref (proxy);
217
 
  g_free (current);
218
 
  g_strfreev (pending);
 
185
  g_free (pending);
 
186
  g_variant_unref (answer);
219
187
 
220
188
  return has_pending;
221
189
}