aboutsummaryrefslogtreecommitdiff
path: root/src/apt-transaction.c
blob: cd9e131c7ba6512388ae08d1289d0d98faf186ad (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
/*
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 "apt-transaction.h"
#include "dbus-shared-names.h"

static void apt_transaction_investigate (AptTransaction* self);
static void apt_transaction_simulate_transaction_cb (GObject * obj,
                                                     GAsyncResult * res,
                                                     gpointer user_data);
static void apt_transaction_receive_signal (GDBusProxy * proxy,
                                            gchar * sender_name,
                                            gchar * signal_name,
                                            GVariant * parameters,
                                            gpointer user_data);
static void apt_transaction_finish_proxy_setup (GObject *source_object,
                                                GAsyncResult *res,
                                                gpointer user_data);

struct _AptTransaction
{
	GObject       parent_instance;
	GDBusProxy *  proxy;    
	GCancellable * proxy_cancel;  
  gchar*        id;
  TransactionType type;
};

enum {
  UPDATE,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = { 0 };

G_DEFINE_TYPE (AptTransaction, apt_transaction, G_TYPE_OBJECT);

static void
apt_transaction_init (AptTransaction *self)
{
  self->proxy = NULL;
  self->id = NULL;
  self->proxy_cancel = g_cancellable_new();
}

static void
apt_transaction_finalize (GObject *object)
{
  AptTransaction* self = APT_TRANSACTION(object);
  g_signal_handlers_disconnect_by_func (G_OBJECT (self->proxy),
                                        G_CALLBACK (apt_transaction_receive_signal),
                                        self);
  if (self->proxy != NULL){
    g_object_unref (self->proxy);
    self->proxy = NULL;
  }
  g_free (self->id);
	G_OBJECT_CLASS (apt_transaction_parent_class)->finalize (object);
}

static void
apt_transaction_class_init (AptTransactionClass *klass)
{
	GObjectClass* object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = apt_transaction_finalize;

  signals[UPDATE] =  g_signal_new("state-update",
                                   G_TYPE_FROM_CLASS (klass),
                                   G_SIGNAL_RUN_LAST,
                                   0,
                                   NULL, NULL,
                                   g_cclosure_marshal_VOID__INT,
                                   G_TYPE_NONE, 1, G_TYPE_INT);  
}

// TODO: you don't need this additional helper
// Just GObject properties properly
static void
apt_transaction_investigate (AptTransaction* self)
{
  g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
                            G_DBUS_PROXY_FLAGS_NONE,
                            NULL,
                            "org.debian.apt",
                            self->id,
                            "org.debian.apt.transaction",
                            self->proxy_cancel,
                            apt_transaction_finish_proxy_setup,
                            self);
}

static void
apt_transaction_finish_proxy_setup (GObject *source_object,
                                    GAsyncResult *res,
                                    gpointer user_data)
{
  g_return_if_fail (APT_IS_TRANSACTION (user_data));
  AptTransaction* self = APT_TRANSACTION(user_data);      
	GError * error = 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;

  g_signal_connect (G_OBJECT(self->proxy),
                    "g-signal",
                    G_CALLBACK (apt_transaction_receive_signal),
                    self);    

  if (self->type == SIMULATION){
    g_dbus_proxy_call (self->proxy,
                       "Simulate",
                       NULL,
                       G_DBUS_CALL_FLAGS_NONE,
                       -1,
                       NULL,
                       apt_transaction_simulate_transaction_cb,
                       self);                                                                                        
  }                       
}

static void
apt_transaction_receive_signal (GDBusProxy * proxy,
                                gchar * sender_name,
                                gchar * signal_name,
                                GVariant * parameters,
                                gpointer user_data)
{
  g_return_if_fail (APT_IS_TRANSACTION (user_data));
  AptTransaction* self = APT_TRANSACTION(user_data);
  AptState current_state = DONT_KNOW;
  
  if (g_strcmp0(signal_name, "PropertyChanged") == 0 && self->type == SIMULATION) 
  {
    gchar* prop_name= NULL;
    GVariant* value = NULL;
    g_variant_get (parameters, "(sv)", &prop_name, &value);    
    g_debug ("transaction prop update - prop = %s", prop_name);
    
    if (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING) == TRUE){
      gchar* key = NULL;
      g_variant_get (value, "s", &key);
      g_debug ("transaction prop update - value = %s", key);
    }
    
    if (g_strcmp0 (prop_name, "Dependencies") == 0){
            
      gchar** install = NULL;
      gchar** reinstall = NULL;
      gchar** remove = NULL;
      gchar** purge = NULL;
      gchar** upgrade = NULL;
      gchar** downgrade = NULL;
      gchar** keep = NULL;
      g_variant_get (value, "(asasasasasasas)", &install, 
                     &reinstall, &remove, &purge, &upgrade, &downgrade,
                     &keep);      
      /*                     
      g_debug ("upgrade package length %i", g_strv_length(upgrade));                
      g_debug ("install package length %i", g_strv_length(install));
      g_debug ("reinstall package length %i", g_strv_length(reinstall));
      g_debug ("remove package length %i", g_strv_length(remove));
      g_debug ("purge package length %i", g_strv_length(purge));
      */
      gboolean upgrade_needed = (g_strv_length(upgrade) > 1) ||
                                (g_strv_length(install) > 1) ||
                                (g_strv_length(reinstall) > 1) ||
                                (g_strv_length(remove) > 1) ||
                                (g_strv_length(purge) > 1);
      if (upgrade_needed == TRUE){
        current_state = UPDATES_AVAILABLE;        
      }
      else{
        current_state = UP_TO_DATE;
      }
    }
  }
  else if (g_strcmp0(signal_name, "PropertyChanged") == 0 &&
           self->type == REAL)
  {
    GVariant* role = g_dbus_proxy_get_cached_property (self->proxy,
                                                       "Role");
    if (g_variant_is_of_type (role, G_VARIANT_TYPE_STRING) == TRUE){
      gchar* current_role = NULL;
      g_variant_get (role, "s", &current_role);
      //g_debug ("Current transaction role = %s", current_role);
      if (g_strcmp0 (current_role, "role-commit-packages") == 0 ||
          g_strcmp0 (current_role, "role-upgrade-system") == 0){
        g_debug ("UPGRADE IN PROGRESS");
        current_state = UPGRADE_IN_PROGRESS;                        
      }
    }
  } 
  else if (g_strcmp0(signal_name, "Finished") == 0) 
  {
    g_debug ("TRANSACTION Finished");
    current_state = FINISHED;
  }
  // Finally send out the state update  
  if (current_state != DONT_KNOW){
    g_signal_emit (self,
                   signals[UPDATE],
                   0,
                   current_state);
  }
  g_variant_unref (parameters);
}

static void
apt_transaction_simulate_transaction_cb (GObject * obj,
                                         GAsyncResult * res,
                                         gpointer user_data)
{
	GError * error = NULL;
	if (error != NULL) {
    g_warning ("unable to complete the simulate call");
    g_error_free (error);
		return;
	}
}
TransactionType 
apt_transaction_get_transaction_type (AptTransaction* self)
{
  return self->type;
}

AptTransaction* apt_transaction_new (gchar* transaction_id, TransactionType t)
{
  AptTransaction* tr = g_object_new (APT_TYPE_TRANSACTION, NULL);
  tr->id = transaction_id;
  tr->type = t;
  g_debug ("Apt transaction new id = %s", tr->id);
  apt_transaction_investigate (tr);
  return tr;
}