aboutsummaryrefslogtreecommitdiff
path: root/src/apt-watcher.c
blob: e5e1d0e471a0391c072701d7b7cc79b196a44f38 (plain)
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*
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 "apt-transaction.h"

static guint watcher_id;

struct _AptWatcher
{
	GObject parent_instance;
  guint reboot_query;
	GCancellable * proxy_cancel;
	GDBusProxy * proxy;  
  SessionDbus* session_dbus_interface;
  DbusmenuMenuitem* apt_item;
  AptState current_state;
  AptTransaction* current_transaction;
};

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 fetch_proxy_cb (GObject * object,
                            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,
                                         gpointer userdata);

static void apt_watcher_signal_cb (GDBusProxy* proxy,
                                   gchar* sender_name,
                                   gchar* signal_name,
                                   GVariant* parameters,
                                   gpointer user_data);
static void  apt_watcher_manage_transactions (AptWatcher* self,
                                              gchar* transaction_id);
static gboolean apt_watcher_query_reboot_status (gpointer self);
static void apt_watcher_transaction_state_simulation_update_cb (AptTransaction* trans,
                                                                gint update,
                                                                gpointer user_data);
static void apt_watcher_transaction_state_real_update_cb (AptTransaction* trans,
                                                          gint update,
                                                          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->reboot_query = 0;
  self->current_transaction = 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);  
  AptWatcher* self = APT_WATCHER (object);
           
  if (self->proxy != NULL)
    g_object_unref (self->proxy);

	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);
  
	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("(b)", TRUE),
                     G_DBUS_CALL_FLAGS_NONE,
                     -1,
                     NULL,
                     apt_watcher_upgrade_system_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));
}

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){
    g_warning ("apt_watcher_upgrade_system_cb - transaction id is null");
    return;
  }  
  
  apt_watcher_manage_transactions (self, transaction_id);
  
}

static void
apt_watcher_show_apt_dialog (DbusmenuMenuitem * mi,
                             guint timestamp,
                             gpointer userdata)
{
  GError * error = NULL;
  g_return_if_fail (APT_IS_WATCHER (userdata));
  AptWatcher* self = APT_WATCHER (userdata);
  const gchar* disposition = NULL;
  disposition = dbusmenu_menuitem_property_get (self->apt_item,
                                                DBUSMENU_MENUITEM_PROP_DISPOSITION);
                                    
  if (g_strcmp0 (disposition, DBUSMENU_MENUITEM_DISPOSITION_ALERT) == 0){  	
    gchar * helper = g_build_filename (LIBEXECDIR, "gtk-logout-helper", NULL);
	  gchar * dialog_line = g_strdup_printf ("%s --%s", helper, "restart");
  	g_free(helper);
	  if (!g_spawn_command_line_async(dialog_line, &error)) {
		  g_warning("Unable to show dialog: %s", error->message);
		  g_error_free(error);
	  }
	  g_free(dialog_line);
  } 
  else{
    if (!g_spawn_command_line_async("update-manager", &error))
    {
      g_warning("Unable to show update-manager: %s", error->message);
      g_error_free(error);
    }
  }   
}

static void
apt_watcher_transaction_state_real_update_cb (AptTransaction* trans,
                                              gint update,
                                              gpointer user_data)
{
  g_debug ("apt-watcher -transaction update %i", update);
  g_return_if_fail (APT_IS_WATCHER (user_data));
  AptWatcher* self = APT_WATCHER (user_data);
  
  AptState state = (AptState)update;
  
  if (state == UP_TO_DATE){
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Software Up to Date"));   
    self->current_state = state;                                                
  }
  else if (state == UPDATES_AVAILABLE){
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Updates Available…"));    
    self->current_state = state;                                    
  }
  else if (state == UPGRADE_IN_PROGRESS){
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Updates Installing…"));    
    self->current_state = state;                                    
  }  
  else if (state == FINISHED){
    gboolean query_again = FALSE;
    
    // Only query if the previous state was an upgrade.
    if (self->current_state == UPGRADE_IN_PROGRESS){
      if (self->reboot_query != 0){
        g_source_remove (self->reboot_query);
        self->reboot_query = 0;
      }
      // Wait a sec before querying for reboot status, 
      // race condition with Apt has been observed.
      self->reboot_query = g_timeout_add_seconds (2,
                                                  apt_watcher_query_reboot_status,
                                                  self); 
    }
    else{
      query_again = TRUE;      
    }    
    self->current_state = state;

    g_object_unref (G_OBJECT(self->current_transaction));
    self->current_transaction = NULL;

    // It is impossible to determine from a 'real' transaction whether
    // updates are available ?
    if (query_again){
      g_dbus_proxy_call (self->proxy,
                         "UpgradeSystem",
                         g_variant_new("(b)", TRUE),
                         G_DBUS_CALL_FLAGS_NONE,
                         -1,
                         NULL,
                         apt_watcher_upgrade_system_cb,
                         self);
    }      
  }
}                                              


static void
apt_watcher_transaction_state_simulation_update_cb (AptTransaction* trans,
                                                    gint update,
                                                    gpointer user_data)
{
  g_debug ("apt-watcher -transaction update %i", update);
  g_return_if_fail (APT_IS_WATCHER (user_data));
  AptWatcher* self = APT_WATCHER (user_data);
  
  AptState state = (AptState)update;
  
  if (state == UP_TO_DATE){
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Software Up to Date"));   
    if (self->reboot_query != 0){
      g_source_remove (self->reboot_query);
      self->reboot_query = 0;
    }
    self->reboot_query = g_timeout_add_seconds (2,
                                                apt_watcher_query_reboot_status,
                                                self); 
  }
  else if (state == UPDATES_AVAILABLE){
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Updates Available…"));    
  }
  else if (state == UPGRADE_IN_PROGRESS){
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Updates Installing…"));    
  }  
  
  self->current_state = state;
  
  if (self->current_state != UPGRADE_IN_PROGRESS){
    g_object_unref (G_OBJECT(self->current_transaction));
    self->current_transaction = NULL;
  }
} 
 
static void
apt_watcher_manage_transactions (AptWatcher* self, gchar* transaction_id)
{
    if (self->current_transaction == NULL){
      self->current_transaction = apt_transaction_new (transaction_id, SIMULATION);
      g_signal_connect (G_OBJECT(self->current_transaction),
                        "state-update",
                        G_CALLBACK(apt_watcher_transaction_state_simulation_update_cb), self);
    }
}

static gboolean
apt_watcher_query_reboot_status (gpointer data)
{
  g_return_val_if_fail (APT_IS_WATCHER (data), FALSE);
  AptWatcher* self = APT_WATCHER (data);
  
  GVariant* reboot_result = g_dbus_proxy_get_cached_property (self->proxy,
                                                             "RebootRequired");
  gboolean reboot;
  g_variant_get (reboot_result, "b", &reboot);
  g_debug ("apt_watcher_query_reboot_status: reboot prop = %i", reboot);
  
  if (reboot == FALSE){
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Software Up to Date"));
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_DISPOSITION,
                                    DBUSMENU_MENUITEM_DISPOSITION_NORMAL);
                                    
  }
  else{
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Reboot Required"));
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_DISPOSITION,
                                    DBUSMENU_MENUITEM_DISPOSITION_ALERT);
    session_dbus_restart_required (self->session_dbus_interface);
    self->current_state = RESTART_NEEDED;
  }
  self->reboot_query = 0;
  return FALSE;
}

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){
    gchar* current = NULL;
    g_debug ("ActiveTransactionsChanged");

    g_variant_get(value, "s", &current);

    if (g_str_has_prefix (current, "/org/debian/apt/transaction/") == TRUE){
      g_debug ("ActiveTransactionsChanged - current is %s", current);
     
      // Cancel all existing operations.
      if (self->reboot_query != 0){
        g_source_remove (self->reboot_query);
        self->reboot_query = 0;
      }

      if (self->current_transaction != NULL)
      {
        g_object_unref (G_OBJECT(self->current_transaction));
        self->current_transaction = NULL;
      }

      self->current_transaction = apt_transaction_new (current, REAL);
      g_signal_connect (G_OBJECT(self->current_transaction),
                        "state-update",
                        G_CALLBACK(apt_watcher_transaction_state_real_update_cb), self);              
    }
  }
  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;
}