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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
/*
Copyright 2011 Canonical Ltd.
Authors:
Conor Curran <conor.curran@canonical.com>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gio/gio.h>
#include <glib/gi18n.h>
#include "apt-watcher.h"
#include "dbus-shared-names.h"
static guint watcher_id;
struct _AptWatcher
{
GObject parent_instance;
GCancellable * proxy_cancel;
GDBusProxy * proxy;
GDBusProxy * transaction_proxy;
SessionDbus* session_dbus_interface;
DbusmenuMenuitem* apt_item;
gint current_state;
};
static void
apt_watcher_on_name_appeared (GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data);
static void
apt_watcher_on_name_vanished (GDBusConnection *connection,
const gchar *name,
gpointer user_data);
/*static void
apt_watcher_get_active_transactions_cb (GObject * obj,
GAsyncResult * res,
gpointer user_data);
* */
static void fetch_proxy_cb (GObject * object,
GAsyncResult * res,
gpointer user_data);
static void apt_watcher_simulate_transaction_cb (GObject * obj,
GAsyncResult * res,
gpointer user_data);
static void apt_watcher_upgrade_system_cb (GObject * obj,
GAsyncResult * res,
gpointer user_data);
static void apt_watcher_show_apt_dialog (DbusmenuMenuitem* mi,
guint timestamp,
gchar * type);
static void apt_watcher_signal_cb (GDBusProxy* proxy,
gchar* sender_name,
gchar* signal_name,
GVariant* parameters,
gpointer user_data);
static void apt_watcher_determine_state (AptWatcher* self,
GVariant* update);
static void transaction_on_properties_changed (GDBusProxy *proxy,
GVariant *changed_properties,
const gchar* const *invalidated_properties,
gpointer user_data);
G_DEFINE_TYPE (AptWatcher, apt_watcher, G_TYPE_OBJECT);
static void
apt_watcher_init (AptWatcher *self)
{
self->current_state = UP_TO_DATE;
self->proxy_cancel = g_cancellable_new();
self->proxy = NULL;
self->transaction_proxy = NULL;
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL,
"org.debian.apt",
"/org/debian/apt",
"org.debian.apt",
self->proxy_cancel,
fetch_proxy_cb,
self);
}
static void
apt_watcher_finalize (GObject *object)
{
g_bus_unwatch_name (watcher_id);
G_OBJECT_CLASS (apt_watcher_parent_class)->finalize (object);
}
static void
apt_watcher_class_init (AptWatcherClass *klass)
{
GObjectClass* object_class = G_OBJECT_CLASS (klass);
object_class->finalize = apt_watcher_finalize;
}
static void
fetch_proxy_cb (GObject * object, GAsyncResult * res, gpointer user_data)
{
GError * error = NULL;
AptWatcher* self = APT_WATCHER(user_data);
g_return_if_fail(self != NULL);
GDBusProxy * proxy = g_dbus_proxy_new_for_bus_finish(res, &error);
if (self->proxy_cancel != NULL) {
g_object_unref(self->proxy_cancel);
self->proxy_cancel = NULL;
}
if (error != NULL) {
g_warning("Could not grab DBus proxy for %s: %s",
"org.debian.apt", error->message);
g_error_free(error);
return;
}
self->proxy = proxy;
// Set up the watch.
watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
"org.debian.apt",
G_BUS_NAME_WATCHER_FLAGS_NONE,
apt_watcher_on_name_appeared,
apt_watcher_on_name_vanished,
self,
NULL);
//We'll need to connect to the state changed signal
g_signal_connect (self->proxy,
"g-signal",
G_CALLBACK(apt_watcher_signal_cb),
self);
}
static void
apt_watcher_on_name_appeared (GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data)
{
g_return_if_fail (APT_IS_WATCHER (user_data));
AptWatcher* watcher = APT_WATCHER (user_data);
g_print ("Name %s on %s is owned by %s\n",
name,
"the system bus",
name_owner);
g_dbus_proxy_call (watcher->proxy,
"UpgradeSystem",
g_variant_new_boolean (TRUE),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
apt_watcher_upgrade_system_cb,
user_data);
/*g_dbus_proxy_call (watcher->proxy,
"GetActiveTransactions",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
apt_watcher_get_active_transactions_cb,
user_data);*/
}
static void
apt_watcher_on_name_vanished (GDBusConnection *connection,
const gchar *name,
gpointer user_data)
{
g_debug ("Name %s does not exist or has just vanished",
name);
g_return_if_fail (APT_IS_WATCHER (user_data));
AptWatcher* self = APT_WATCHER (user_data);
if (self->proxy != NULL)
g_object_unref (self->proxy);
}
/*static void
apt_watcher_get_active_transactions_cb (GObject * obj,
GAsyncResult * res,
gpointer user_data)
{
g_return_if_fail (APT_IS_WATCHER (user_data));
AptWatcher* self = APT_WATCHER (user_data);
GError * error = NULL;
GVariant * result;
result = g_dbus_proxy_call_finish(self->proxy, res, &error);
if (error != NULL) {
g_warning ("unable to complete the fetching of active transactions");
g_error_free (error);
return;
}
apt_watcher_determine_state (self, result);
}*/
static void
apt_watcher_upgrade_system_cb (GObject * obj,
GAsyncResult * res,
gpointer user_data)
{
g_return_if_fail (APT_IS_WATCHER (user_data));
AptWatcher* self = APT_WATCHER (user_data);
GError * error = NULL;
GVariant * result;
result = g_dbus_proxy_call_finish(self->proxy, res, &error);
if (error != NULL) {
g_warning ("unable to complete the UpgradeSystem apt call");
g_error_free (error);
return;
}
gchar* transaction_id = NULL;
g_variant_get (result, "(s)", &transaction_id);
if (transaction_id != NULL){
if (self->transaction_proxy != NULL){
g_object_unref (self->transaction_proxy);
self->transaction_proxy = NULL;
}
self->transaction_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_NONE,
NULL, /* GDBusInterfaceInfo */
"org.debian.apt",
transaction_id,
"org.debian.apt",
NULL, /* GCancellable */
&error);
if (error != NULL) {
g_warning ("unable to fetch proxy for transaction object path");
g_error_free (error);
return;
}
g_signal_connect (self->transaction_proxy,
"g-properties-changed",
G_CALLBACK (transaction_on_properties_changed),
self);
g_dbus_proxy_call (self->transaction_proxy,
"Simulate",
NULL,
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
apt_watcher_simulate_transaction_cb,
self);
}
}
static void
apt_watcher_simulate_transaction_cb (GObject * obj,
GAsyncResult * res,
gpointer user_data)
{
}
static void
transaction_on_properties_changed (GDBusProxy *proxy,
GVariant *changed_properties,
const gchar* const *invalidated_properties,
gpointer user_data)
{
if (g_variant_n_children (changed_properties) > 0)
{
GVariantIter *iter;
const gchar *key;
GVariant *value;
g_print (" *** Properties Changed:\n");
g_variant_get (changed_properties,
"a{sv}",
&iter);
while (g_variant_iter_loop (iter, "{&sv}", &key, &value))
{
gchar *value_str;
value_str = g_variant_print (value, TRUE);
g_print (" %s -> %s\n", key, value_str);
g_free (value_str);
}
g_variant_iter_free (iter);
}
}
static void
apt_watcher_determine_state (AptWatcher* self,
GVariant* update)
{
g_debug ("WE GOT SOME ACTIVE TRANSACTIONS TO EXAMINE, type is %s",
g_variant_get_type_string (update));
gchar* first_param = NULL;
gchar ** transactions = NULL;
g_variant_get (update, "(sas)", &first_param, &transactions);
g_debug ("apt_watcher_determine_state - the size is the string array %u",
g_strv_length (transactions));
g_debug ("apt_watcher_determine_state - first param = %s", first_param);
if (first_param == NULL){
if (self->current_state != UP_TO_DATE){
dbusmenu_menuitem_property_set (self->apt_item,
DBUSMENU_MENUITEM_PROP_LABEL,
_("Software Up to Date"));
}
}
else{
gboolean updating = g_str_has_prefix (first_param,
"/org/debian/apt/transaction/");
if (updating == TRUE){
self->current_state = UPDATES_IN_PROGRESS;
dbusmenu_menuitem_property_set (self->apt_item,
DBUSMENU_MENUITEM_PROP_LABEL,
_("Updates Installing..."));
}
}
}
static void
apt_watcher_show_apt_dialog (DbusmenuMenuitem * mi,
guint timestamp,
gchar * type)
{
}
// TODO signal is of type s not sas which is on d-feet !!!
static void apt_watcher_signal_cb ( GDBusProxy* proxy,
gchar* sender_name,
gchar* signal_name,
GVariant* parameters,
gpointer user_data)
{
g_return_if_fail (APT_IS_WATCHER (user_data));
AptWatcher* self = APT_WATCHER (user_data);
g_variant_ref (parameters);
GVariant *value = g_variant_get_child_value (parameters, 0);
if (g_strcmp0(signal_name, "ActiveTransactionsChanged") == 0){
g_debug ("Active Transactions signal received");
apt_watcher_determine_state (self, value);
}
g_variant_unref (parameters);
}
AptWatcher* apt_watcher_new (SessionDbus* session_dbus,
DbusmenuMenuitem* item)
{
AptWatcher* watcher = g_object_new (APT_TYPE_WATCHER, NULL);
watcher->session_dbus_interface = session_dbus;
watcher->apt_item = item;
g_signal_connect (G_OBJECT(watcher->apt_item),
DBUSMENU_MENUITEM_SIGNAL_ITEM_ACTIVATED,
G_CALLBACK(apt_watcher_show_apt_dialog), watcher);
return watcher;
}
|