| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2007-2009 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU Lesser General Public License Version 2.1 |
| 6 |
* |
| 7 |
* This library is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 2.1 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This library is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public |
| 18 |
* License along with this library; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* SECTION:pk-transaction-list |
| 24 |
* @short_description: A nice way to keep a list of the jobs being processed |
| 25 |
* |
| 26 |
* These provide a good way to keep a list of the jobs being processed so we |
| 27 |
* can see what type of jobs and thier status easily. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "config.h" |
| 31 |
|
| 32 |
#include <glib-object.h> |
| 33 |
#include <gio/gio.h> |
| 34 |
|
| 35 |
#include <packagekit-glib2/pk-transaction-list.h> |
| 36 |
#include <packagekit-glib2/pk-control.h> |
| 37 |
#include <packagekit-glib2/pk-common.h> |
| 38 |
|
| 39 |
static void pk_transaction_list_finalize (GObject *object); |
| 40 |
|
| 41 |
/** |
| 42 |
* PkTransactionListPrivate: |
| 43 |
* |
| 44 |
* Private #PkTransactionList data |
| 45 |
**/ |
| 46 |
struct _PkTransactionListPrivate |
| 47 |
{ |
| 48 |
GPtrArray *transaction_ids; |
| 49 |
PkControl *control; |
| 50 |
GCancellable *cancellable; |
| 51 |
}; |
| 52 |
|
| 53 |
typedef enum { |
| 54 |
SIGNAL_ADDED, |
| 55 |
SIGNAL_REMOVED, |
| 56 |
SIGNAL_LAST |
| 57 |
} PkSignals; |
| 58 |
|
| 59 |
static guint signals [SIGNAL_LAST] = { 0 }; |
| 60 |
|
| 61 |
G_DEFINE_TYPE_WITH_PRIVATE (PkTransactionList, pk_transaction_list, G_TYPE_OBJECT) |
| 62 |
#define GET_PRIVATE(o) (pk_transaction_list_get_instance_private (o)) |
| 63 |
|
| 64 |
/* |
| 65 |
* pk_transaction_list_process_transaction_list: |
| 66 |
**/ |
| 67 |
static void |
| 68 |
pk_transaction_list_process_transaction_list (PkTransactionList *tlist, gchar **transaction_ids) |
| 69 |
{ |
| 70 |
PkTransactionListPrivate *priv = GET_PRIVATE(tlist); |
| 71 |
guint i, j; |
| 72 |
gboolean ret; |
| 73 |
const gchar *tid; |
| 74 |
gchar *tid_tmp; |
| 75 |
GPtrArray *array = priv->transaction_ids; |
| 76 |
|
| 77 |
/* debug */ |
| 78 |
for (i = 0; i < array->len; i++) { |
| 79 |
tid = g_ptr_array_index (array, i); |
| 80 |
g_debug ("last:\t%s", tid); |
| 81 |
} |
| 82 |
for (i = 0; transaction_ids[i] != NULL; i++) |
| 83 |
g_debug ("current:\t%s", transaction_ids[i]); |
| 84 |
|
| 85 |
/* remove old entries */ |
| 86 |
for (i = 0; i < array->len; i++) { |
| 87 |
tid = g_ptr_array_index (array, i); |
| 88 |
|
| 89 |
/* is in new list */ |
| 90 |
ret = FALSE; |
| 91 |
for (j = 0; transaction_ids[j] != NULL; j++) { |
| 92 |
ret = (g_strcmp0 (tid, transaction_ids[j]) == 0); |
| 93 |
if (ret) |
| 94 |
break; |
| 95 |
} |
| 96 |
|
| 97 |
/* no, so remove from array */ |
| 98 |
if (!ret) { |
| 99 |
tid_tmp = g_strdup (tid); |
| 100 |
g_ptr_array_remove_index (array, i); |
| 101 |
g_debug ("emit removed: %s", tid_tmp); |
| 102 |
g_signal_emit (tlist, signals[SIGNAL_REMOVED], 0, tid_tmp); |
| 103 |
g_free (tid_tmp); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/* add new entries */ |
| 108 |
for (i = 0; transaction_ids[i] != NULL; i++) { |
| 109 |
|
| 110 |
/* check to see if tid is in array */ |
| 111 |
ret = FALSE; |
| 112 |
for (j = 0; j<array->len; j++) { |
| 113 |
tid = g_ptr_array_index (array, j); |
| 114 |
ret = (g_strcmp0 (tid, transaction_ids[i]) == 0); |
| 115 |
if (ret) |
| 116 |
break; |
| 117 |
} |
| 118 |
|
| 119 |
/* no, so add to array */ |
| 120 |
if (!ret) { |
| 121 |
g_ptr_array_add (array, g_strdup (transaction_ids[i])); |
| 122 |
g_debug ("emit added: %s", transaction_ids[i]); |
| 123 |
g_signal_emit (tlist, signals[SIGNAL_ADDED], 0, transaction_ids[i]); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/* |
| 129 |
* pk_transaction_list_get_transaction_list_cb: |
| 130 |
**/ |
| 131 |
static void |
| 132 |
pk_transaction_list_get_transaction_list_cb (PkControl *control, GAsyncResult *res, PkTransactionList *tlist) |
| 133 |
{ |
| 134 |
g_autoptr(GError) error = NULL; |
| 135 |
g_auto(GStrv) transaction_ids = NULL; |
| 136 |
|
| 137 |
/* get the result */ |
| 138 |
transaction_ids = pk_control_get_transaction_list_finish (control, res, &error); |
| 139 |
if (transaction_ids == NULL) { |
| 140 |
g_warning ("Failed to get transaction list: %s", error->message); |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
/* process */ |
| 145 |
pk_transaction_list_process_transaction_list (tlist, transaction_ids); |
| 146 |
} |
| 147 |
|
| 148 |
/* |
| 149 |
* pk_transaction_list_get_transaction_list: |
| 150 |
**/ |
| 151 |
static void |
| 152 |
pk_transaction_list_get_transaction_list (PkTransactionList *tlist) |
| 153 |
{ |
| 154 |
PkTransactionListPrivate *priv = GET_PRIVATE(tlist); |
| 155 |
|
| 156 |
g_debug ("refreshing task list"); |
| 157 |
pk_control_get_transaction_list_async (priv->control, priv->cancellable, |
| 158 |
(GAsyncReadyCallback) pk_transaction_list_get_transaction_list_cb, tlist); |
| 159 |
} |
| 160 |
|
| 161 |
/* |
| 162 |
* pk_transaction_list_task_list_changed_cb: |
| 163 |
**/ |
| 164 |
static void |
| 165 |
pk_transaction_list_task_list_changed_cb (PkControl *control, gchar **transaction_ids, PkTransactionList *tlist) |
| 166 |
{ |
| 167 |
/* process */ |
| 168 |
pk_transaction_list_process_transaction_list (tlist, transaction_ids); |
| 169 |
} |
| 170 |
|
| 171 |
/* |
| 172 |
* pk_transaction_list_notify_connected_cb: |
| 173 |
**/ |
| 174 |
static void |
| 175 |
pk_transaction_list_notify_connected_cb (PkControl *control, GParamSpec *pspec, PkTransactionList *tlist) |
| 176 |
{ |
| 177 |
gboolean connected; |
| 178 |
g_object_get (control, "connected", &connected, NULL); |
| 179 |
if (connected) |
| 180 |
pk_transaction_list_get_transaction_list (tlist); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* pk_transaction_list_get_ids: |
| 185 |
* @tlist: a valid #PkTransactionList instance |
| 186 |
* |
| 187 |
* Gets the string lists of transaction IDs recognised as pending, running or finished by the daemon. |
| 188 |
* |
| 189 |
* Return value: (transfer full): the array of strings, free with g_strfreev() |
| 190 |
* |
| 191 |
* Since: 0.5.3 |
| 192 |
**/ |
| 193 |
gchar ** |
| 194 |
pk_transaction_list_get_ids (PkTransactionList *tlist) |
| 195 |
{ |
| 196 |
PkTransactionListPrivate *priv = GET_PRIVATE(tlist); |
| 197 |
|
| 198 |
g_return_val_if_fail (PK_IS_TRANSACTION_LIST (tlist), NULL); |
| 199 |
|
| 200 |
return pk_ptr_array_to_strv (priv->transaction_ids); |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
* pk_transaction_list_class_init: |
| 205 |
**/ |
| 206 |
static void |
| 207 |
pk_transaction_list_class_init (PkTransactionListClass *klass) |
| 208 |
{ |
| 209 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 210 |
|
| 211 |
object_class->finalize = pk_transaction_list_finalize; |
| 212 |
|
| 213 |
/** |
| 214 |
* PkTransactionList::added: |
| 215 |
* @tlist: the #PkTransactionList instance that emitted the signal |
| 216 |
* @tid: the transaction id |
| 217 |
* |
| 218 |
* The ::added signal is emitted when a tid has been added to the transaction list |
| 219 |
**/ |
| 220 |
signals [SIGNAL_ADDED] = |
| 221 |
g_signal_new ("added", |
| 222 |
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 223 |
G_STRUCT_OFFSET (PkTransactionListClass, added), |
| 224 |
NULL, NULL, g_cclosure_marshal_VOID__STRING, |
| 225 |
G_TYPE_NONE, 1, G_TYPE_STRING); |
| 226 |
|
| 227 |
/** |
| 228 |
* PkTransactionList::removed: |
| 229 |
* @tlist: the #PkTransactionList instance that emitted the signal |
| 230 |
* @tid: the transaction id |
| 231 |
* |
| 232 |
* The ::removed signal is emitted when a tid has been removed from the transaction list |
| 233 |
**/ |
| 234 |
signals [SIGNAL_REMOVED] = |
| 235 |
g_signal_new ("removed", |
| 236 |
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, |
| 237 |
G_STRUCT_OFFSET (PkTransactionListClass, added), |
| 238 |
NULL, NULL, g_cclosure_marshal_VOID__STRING, |
| 239 |
G_TYPE_NONE, 1, G_TYPE_STRING); |
| 240 |
} |
| 241 |
|
| 242 |
/* |
| 243 |
* pk_transaction_list_init: |
| 244 |
**/ |
| 245 |
static void |
| 246 |
pk_transaction_list_init (PkTransactionList *tlist) |
| 247 |
{ |
| 248 |
PkTransactionListPrivate *priv = GET_PRIVATE(tlist); |
| 249 |
|
| 250 |
tlist->priv = priv; |
| 251 |
|
| 252 |
/* get the changing job list */ |
| 253 |
priv->cancellable = g_cancellable_new (); |
| 254 |
priv->control = pk_control_new (); |
| 255 |
g_signal_connect (priv->control, "transaction-list-changed", |
| 256 |
G_CALLBACK (pk_transaction_list_task_list_changed_cb), tlist); |
| 257 |
g_signal_connect (priv->control, "notify::connected", |
| 258 |
G_CALLBACK (pk_transaction_list_notify_connected_cb), tlist); |
| 259 |
|
| 260 |
/* we maintain a local copy */ |
| 261 |
priv->transaction_ids = g_ptr_array_new_with_free_func (g_free); |
| 262 |
|
| 263 |
/* force a refresh so we have valid data*/ |
| 264 |
pk_transaction_list_get_transaction_list (tlist); |
| 265 |
} |
| 266 |
|
| 267 |
/* |
| 268 |
* pk_transaction_list_finalize: |
| 269 |
**/ |
| 270 |
static void |
| 271 |
pk_transaction_list_finalize (GObject *object) |
| 272 |
{ |
| 273 |
PkTransactionList *tlist = PK_TRANSACTION_LIST (object); |
| 274 |
PkTransactionListPrivate *priv = GET_PRIVATE(tlist); |
| 275 |
|
| 276 |
/* cancel if we're in the act */ |
| 277 |
g_cancellable_cancel (priv->cancellable); |
| 278 |
|
| 279 |
/* unhook all signals */ |
| 280 |
g_signal_handlers_disconnect_by_func (priv->control, G_CALLBACK (pk_transaction_list_task_list_changed_cb), tlist); |
| 281 |
g_signal_handlers_disconnect_by_func (priv->control, G_CALLBACK (pk_transaction_list_notify_connected_cb), tlist); |
| 282 |
|
| 283 |
/* remove all watches */ |
| 284 |
g_clear_pointer (&priv->transaction_ids, g_ptr_array_unref); |
| 285 |
g_clear_object (&priv->control); |
| 286 |
g_clear_object (&priv->cancellable); |
| 287 |
|
| 288 |
G_OBJECT_CLASS (pk_transaction_list_parent_class)->finalize (object); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* pk_transaction_list_new: |
| 293 |
* |
| 294 |
* Since: 0.5.3 |
| 295 |
**/ |
| 296 |
PkTransactionList * |
| 297 |
pk_transaction_list_new (void) |
| 298 |
{ |
| 299 |
PkTransactionList *tlist; |
| 300 |
tlist = g_object_new (PK_TYPE_TRANSACTION_LIST, NULL); |
| 301 |
return PK_TRANSACTION_LIST (tlist); |
| 302 |
} |