aboutsummaryrefslogtreecommitdiff
path: root/src/apt-watcher.c
blob: b62354a5289bb1cbf788cc20d1a30d80fa7c84cf (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
/*
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/>.
*/

// Conor - 6/2/2012
// Please pull in packagekit's client lib
// using apt-get install --no-install-recommends libpackagekit-glib2-dev
// make sure you don't install package-kit
#define I_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE

#include <glib/gi18n.h>
#include <packagekit-glib2/packagekit.h>
#include "apt-watcher.h"
#include "dbus-shared-names.h"

static guint watcher_id;

struct _AptWatcher
{
	GObject parent_instance;
  SessionDbus* session_dbus_interface;
  DbusmenuMenuitem* apt_item;
  AptState current_state;
  GCancellable * proxy_cancel;
  GDBusProxy * proxy;
};
                                                                
G_DEFINE_TYPE (AptWatcher, apt_watcher, G_TYPE_OBJECT);


static void
get_updates_complete (GObject *source_object,
                      GAsyncResult *res,
                      gpointer user_data)
{
  g_return_if_fail (APT_IS_WATCHER (user_data));
  AptWatcher* self = APT_WATCHER (user_data);

  PkResults *results;
  GError *error = NULL;
  results = pk_client_generic_finish (PK_CLIENT(source_object), res, &error);
  
  if (error != NULL){
    g_warning ("Unable to query for updates - error - %s", error->message);
    return;
  }

  GPtrArray *packages;
  packages = pk_results_get_package_array (results);

  const gchar* disposition;
  disposition = dbusmenu_menuitem_property_get (self->apt_item,
                                                DBUSMENU_MENUITEM_PROP_DISPOSITION);
  gboolean do_update;
  do_update = g_strcmp0 (disposition, DBUSMENU_MENUITEM_DISPOSITION_ALERT) != 0;                                       

  if (packages->len > 0){
    g_print ("Apparently we have updates available - change dbmitem %i", do_update);   
      if (do_update) 
        dbusmenu_menuitem_property_set (self->apt_item,
                                        DBUSMENU_MENUITEM_PROP_LABEL,
                                        _("Updates Available…"));    
  }
  else{
    g_print ("No updates available - change dbmitem - %i", do_update);   
      if (do_update)
        dbusmenu_menuitem_property_set (self->apt_item,
                                        DBUSMENU_MENUITEM_PROP_LABEL,
                                        _("Software Up to Date"));       
  }
  g_ptr_array_unref (packages);
  g_object_unref (results);
  g_object_unref (source_object);
}

static void
apt_watcher_check_for_updates (AptWatcher* self)
{
    PkClient* client;
    client = pk_client_new ();

    pk_client_get_updates_async (client, 
                                 PK_FILTER_ENUM_NONE,
                                 NULL, NULL, NULL,
                                 (GAsyncReadyCallback)get_updates_complete,
                                 self);  
}

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_debug ("apt-watcher-signal cb signal name - %s", signal_name);
  if (g_strcmp0(signal_name, "UpdatesChanged") == 0){
    g_debug ("updates changed signal received");
    apt_watcher_check_for_updates (self);
  }
  else if (g_strcmp0(signal_name, "RestartSchedule") == 0) {
    g_debug ("RestartScheduled signal received");
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_LABEL,
                                    _("Restart to Complete Updates…"));
    dbusmenu_menuitem_property_set (self->apt_item,
                                    DBUSMENU_MENUITEM_PROP_DISPOSITION,
                                    DBUSMENU_MENUITEM_DISPOSITION_ALERT);     
  } 
}

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);
}


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
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.freedesktop.PackageKit",
                                 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 gboolean 
apt_watcher_start_apt_interaction (gpointer data)
{
  g_return_val_if_fail (APT_IS_WATCHER (data), FALSE);
  AptWatcher* self = APT_WATCHER (data);
  g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                            G_DBUS_PROXY_FLAGS_NONE,
                            NULL,
                            "org.freedesktop.PackageKit",
                            "/org/freedesktop/PackageKit",
                            "org.freedesktop.PackageKit",
                            self->proxy_cancel,
                            fetch_proxy_cb,
                            self);
  apt_watcher_check_for_updates (self);
  return FALSE;    
}


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_init (AptWatcher *self)
{
  self->current_state = UP_TO_DATE;
  g_timeout_add_seconds (60,
                         apt_watcher_start_apt_interaction,
                         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;
}

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;
}