Debian Codegraph @

lib/packagekit-glib2/pk-control.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2008-2011 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-control
24 * @short_description: For querying data about PackageKit
25 *
26 * A GObject to use for accessing PackageKit asynchronously.
27 */
28
29 #include "config.h"
30
31 #include <string.h>
32 #include <glib-object.h>
33 #include <gio/gio.h>
34
35 #include <packagekit-glib2/pk-bitfield.h>
36 #include <packagekit-glib2/pk-common.h>
37 #include <packagekit-glib2/pk-control.h>
38 #include <packagekit-glib2/pk-version.h>
39 #include <packagekit-glib2/pk-enum-types.h>
40
41 static void pk_control_finalize (GObject *object);
42
43 #define PK_CONTROL_DBUS_METHOD_TIMEOUT 1500 /* ms */
44
45 /**
46 * PkControlPrivate:
47 *
48 * Private #PkControl data
49 **/
50 struct _PkControlPrivate
51 {
52 GCancellable *cancellable;
53 GPtrArray *calls;
54 GDBusProxy *proxy;
55 guint version_major;
56 guint version_minor;
57 guint version_micro;
58 gchar *backend_name;
59 gchar *backend_description;
60 gchar *backend_author;
61 PkBitfield roles;
62 PkBitfield provides;
63 PkBitfield groups;
64 PkBitfield filters;
65 gchar **mime_types;
66 gboolean connected;
67 gboolean locked;
68 PkNetworkEnum network_state;
69 gchar *distro_id;
70 guint watch_id;
71 };
72
73 enum {
74 SIGNAL_TRANSACTION_LIST_CHANGED,
75 SIGNAL_RESTART_SCHEDULE,
76 SIGNAL_INSTALLED_CHANGED,
77 SIGNAL_UPDATES_CHANGED,
78 SIGNAL_REPO_LIST_CHANGED,
79 SIGNAL_LAST
80 };
81
82 enum {
83 PROP_0,
84 PROP_VERSION_MAJOR,
85 PROP_VERSION_MINOR,
86 PROP_VERSION_MICRO,
87 PROP_BACKEND_NAME,
88 PROP_BACKEND_DESCRIPTION,
89 PROP_BACKEND_AUTHOR,
90 PROP_ROLES,
91 PROP_GROUPS,
92 PROP_FILTERS,
93 PROP_PROVIDES,
94 PROP_MIME_TYPES,
95 PROP_LOCKED,
96 PROP_NETWORK_STATE,
97 PROP_CONNECTED,
98 PROP_DISTRO_ID,
99 PROP_LAST
100 };
101
102 static guint signals[SIGNAL_LAST] = { 0 };
103 static gpointer pk_control_object = NULL;
104
105 static GParamSpec *obj_properties[PROP_LAST] = { NULL, };
106
107 G_DEFINE_TYPE_WITH_PRIVATE (PkControl, pk_control, G_TYPE_OBJECT)
108 #define GET_PRIVATE(o) (pk_control_get_instance_private (o))
109
110 /**
111 * pk_control_error_quark:
112 *
113 * We are a GObject that sets errors
114 *
115 * Returns: Our personal error quark.
116 *
117 * Since: 0.5.2
118 **/
119 G_DEFINE_QUARK (pk-control-error-quark, pk_control_error)
120
121 /*
122 * pk_control_fixup_dbus_error:
123 **/
124 static void
125 pk_control_fixup_dbus_error (GError *error)
126 {
127 g_return_if_fail (error != NULL);
128
129 /* hardcode domain */
130 error->domain = PK_CONTROL_ERROR;
131
132 /* find a better failure code */
133 if (error->code == 0)
134 error->code = PK_CONTROL_ERROR_CANNOT_START_DAEMON;
135 else
136 error->code = PK_CONTROL_ERROR_FAILED;
137 }
138
139 static gboolean
140 _g_strvcmp0 (gchar **one, gchar **two)
141 {
142 guint i;
143 if (one == two)
144 return TRUE;
145 if (one == NULL && two != NULL)
146 return FALSE;
147 if (one != NULL && two == NULL)
148 return FALSE;
149 if (g_strv_length (one) != g_strv_length (two))
150 return FALSE;
151 for (i = 0; one[i] != NULL; i++) {
152 if (g_strcmp0 (one[i], two[i]) != 0)
153 return FALSE;
154 }
155 return TRUE;
156 }
157
158 /*
159 * pk_control_set_property_value:
160 **/
161 static void
162 pk_control_set_property_value (PkControl *control,
163 const gchar *key,
164 GVariant *value)
165 {
166 PkControlPrivate *priv = GET_PRIVATE(control);
167 const gchar *tmp_str;
168 gboolean tmp_bool;
169 guint tmp_uint;
170 PkBitfield tmp_bitfield;
171
172 if (g_strcmp0 (key, "VersionMajor") == 0) {
173 tmp_uint = g_variant_get_uint32 (value);
174 if (priv->version_major == tmp_uint)
175 return;
176 priv->version_major = tmp_uint;
177 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_VERSION_MAJOR]);
178 return;
179 }
180 if (g_strcmp0 (key, "VersionMinor") == 0) {
181 tmp_uint = g_variant_get_uint32 (value);
182 if (priv->version_minor == tmp_uint)
183 return;
184 priv->version_minor = tmp_uint;
185 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_VERSION_MINOR]);
186 return;
187 }
188 if (g_strcmp0 (key, "VersionMicro") == 0) {
189 tmp_uint = g_variant_get_uint32 (value);
190 if (priv->version_micro == tmp_uint)
191 return;
192 priv->version_micro = tmp_uint;
193 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_VERSION_MICRO]);
194 return;
195 }
196 if (g_strcmp0 (key, "BackendName") == 0) {
197 tmp_str = g_variant_get_string (value, NULL);
198 if (g_strcmp0 (priv->backend_name, tmp_str) == 0)
199 return;
200 g_free (priv->backend_name);
201 priv->backend_name = g_strdup (tmp_str);
202 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_BACKEND_NAME]);
203 return;
204 }
205 if (g_strcmp0 (key, "BackendDescription") == 0) {
206 tmp_str = g_variant_get_string (value, NULL);
207 if (g_strcmp0 (priv->backend_description, tmp_str) == 0)
208 return;
209 g_free (priv->backend_description);
210 priv->backend_description = g_strdup (tmp_str);
211 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_BACKEND_DESCRIPTION]);
212 return;
213 }
214 if (g_strcmp0 (key, "BackendAuthor") == 0) {
215 tmp_str = g_variant_get_string (value, NULL);
216 if (g_strcmp0 (priv->backend_author, tmp_str) == 0)
217 return;
218 g_free (priv->backend_author);
219 priv->backend_author = g_strdup (tmp_str);
220 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_BACKEND_AUTHOR]);
221 return;
222 }
223 if (g_strcmp0 (key, "MimeTypes") == 0) {
224 g_autofree gchar **tmp_strv = NULL;
225 tmp_strv = (gchar **) g_variant_get_strv (value, NULL);
226 if (_g_strvcmp0 (priv->mime_types, tmp_strv))
227 return;
228 g_strfreev (priv->mime_types);
229 priv->mime_types = g_strdupv (tmp_strv);
230 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_MIME_TYPES]);
231 return;
232 }
233 if (g_strcmp0 (key, "Roles") == 0) {
234 tmp_bitfield = g_variant_get_uint64 (value);
235 if (priv->roles == tmp_bitfield)
236 return;
237 priv->roles = tmp_bitfield;
238 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_ROLES]);
239 return;
240 }
241 if (g_strcmp0 (key, "Provides") == 0) {
242 tmp_bitfield = g_variant_get_uint64 (value);
243 if (priv->provides == tmp_bitfield)
244 return;
245 priv->provides = tmp_bitfield;
246 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_PROVIDES]);
247 return;
248 }
249 if (g_strcmp0 (key, "Groups") == 0) {
250 tmp_bitfield = g_variant_get_uint64 (value);
251 if (priv->groups == tmp_bitfield)
252 return;
253 priv->groups = tmp_bitfield;
254 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_GROUPS]);
255 return;
256 }
257 if (g_strcmp0 (key, "Filters") == 0) {
258 tmp_bitfield = g_variant_get_uint64 (value);
259 if (priv->filters == tmp_bitfield)
260 return;
261 priv->filters = tmp_bitfield;
262 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_FILTERS]);
263 return;
264 }
265 if (g_strcmp0 (key, "Locked") == 0) {
266 tmp_bool = g_variant_get_boolean (value);
267 if (priv->locked == tmp_bool)
268 return;
269 priv->locked = tmp_bool;
270 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_LOCKED]);
271 return;
272 }
273 if (g_strcmp0 (key, "NetworkState") == 0) {
274 tmp_uint = g_variant_get_uint32 (value);
275 if (priv->network_state == tmp_uint)
276 return;
277 priv->network_state = tmp_uint;
278 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_NETWORK_STATE]);
279 return;
280 }
281 if (g_strcmp0 (key, "DistroId") == 0) {
282 tmp_str = g_variant_get_string (value, NULL);
283 /* we don't want distro specific results in 'make check' */
284 if (g_getenv ("PK_SELF_TEST") != NULL)
285 tmp_str = "selftest;11.91;i686";
286 if (g_strcmp0 (priv->distro_id, tmp_str) == 0)
287 return;
288 g_free (priv->distro_id);
289 priv->distro_id = g_strdup (tmp_str);
290 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_DISTRO_ID]);
291 return;
292 }
293 g_warning ("unhandled property '%s'", key);
294 }
295
296 /*
297 * pk_control_properties_changed_cb:
298 **/
299 static void
300 pk_control_properties_changed_cb (GDBusProxy *proxy,
301 GVariant *changed_properties,
302 const gchar* const *invalidated_properties,
303 gpointer user_data)
304 {
305 const gchar *key;
306 GVariantIter *iter;
307 GVariant *value;
308 PkControl *control = PK_CONTROL (user_data);
309
310 if (g_variant_n_children (changed_properties) > 0) {
311 g_variant_get (changed_properties,
312 "a{sv}",
313 &iter);
314 while (g_variant_iter_loop (iter, "{&sv}", &key, &value))
315 pk_control_set_property_value (control, key, value);
316 g_variant_iter_free (iter);
317 }
318 }
319
320 /*
321 * pk_control_signal_cb:
322 **/
323 static void
324 pk_control_signal_cb (GDBusProxy *proxy,
325 const gchar *sender_name,
326 const gchar *signal_name,
327 GVariant *parameters,
328 gpointer user_data)
329 {
330 PkControl *control = PK_CONTROL (user_data);
331 g_auto(GStrv) ids = NULL;
332
333 if (g_strcmp0 (signal_name, "TransactionListChanged") == 0) {
334 g_autofree gchar **ids_tmp = NULL;
335 g_variant_get (parameters, "(^a&s)", &ids_tmp);
336 if (ids_tmp == NULL) {
337 ids = g_new0 (gchar *, 1);
338 } else {
339 ids = g_strdupv ((gchar **) ids_tmp);
340 }
341 g_debug ("emit transaction-list-changed");
342 g_signal_emit (control,
343 signals[SIGNAL_TRANSACTION_LIST_CHANGED], 0,
344 ids);
345 }
346 if (g_strcmp0 (signal_name, "InstalledChanged") == 0) {
347 g_debug ("emit installed-changed");
348 g_signal_emit (control, signals[SIGNAL_INSTALLED_CHANGED], 0);
349 return;
350 }
351 if (g_strcmp0 (signal_name, "UpdatesChanged") == 0) {
352 g_debug ("emit updates-changed");
353 g_signal_emit (control, signals[SIGNAL_UPDATES_CHANGED], 0);
354 return;
355 }
356 if (g_strcmp0 (signal_name, "RepoListChanged") == 0) {
357 g_debug ("emit repo-list-changed");
358 g_signal_emit (control, signals[SIGNAL_REPO_LIST_CHANGED], 0);
359 return;
360 }
361 if (g_strcmp0 (signal_name, "RestartSchedule") == 0) {
362 g_debug ("emit restart-schedule");
363 g_signal_emit (control, signals[SIGNAL_RESTART_SCHEDULE], 0);
364 return;
365 }
366 }
367
368 /*
369 * pk_control_proxy_connect:
370 **/
371 static void
372 pk_control_proxy_connect (PkControl *control,
373 GDBusProxy *proxy)
374 {
375 PkControlPrivate *priv = GET_PRIVATE(control);
376 guint i;
377 g_auto(GStrv) props = NULL;
378
379 /* coldplug properties */
380 props = g_dbus_proxy_get_cached_property_names (proxy);
381 for (i = 0; props != NULL && props[i] != NULL; i++) {
382 g_autoptr(GVariant) value_tmp = NULL;
383 value_tmp = g_dbus_proxy_get_cached_property (proxy, props[i]);
384 pk_control_set_property_value (control,
385 props[i],
386 value_tmp);
387 }
388
389 /* connect up signals */
390 g_signal_connect (proxy, "g-properties-changed",
391 G_CALLBACK (pk_control_properties_changed_cb),
392 control);
393 g_signal_connect (proxy, "g-signal",
394 G_CALLBACK (pk_control_signal_cb),
395 control);
396
397 /* if we have no generic system wide proxy, then use this */
398 if (priv->proxy == NULL)
399 priv->proxy = g_object_ref (proxy);
400 }
401
402 /**********************************************************************/
403
404 /*
405 * pk_control_get_tid_cb:
406 **/
407 static void
408 pk_control_get_tid_cb (GObject *source_object,
409 GAsyncResult *res,
410 gpointer user_data)
411 {
412 GDBusProxy *proxy = G_DBUS_PROXY (source_object);
413 g_autoptr(GTask) task = G_TASK (user_data);
414 g_autoptr(GError) error = NULL;
415 g_autoptr(GVariant) value = NULL;
416 gchar *tid = NULL;
417
418 /* get the result */
419 value = g_dbus_proxy_call_finish (proxy, res, &error);
420 if (value == NULL) {
421 /* fix up the D-Bus error */
422 pk_control_fixup_dbus_error (error);
423 g_task_return_error (task, g_steal_pointer (&error));
424 return;
425 }
426
427 /* save results */
428 g_variant_get (value, "(o)", &tid);
429
430 /* we're done */
431 g_task_return_pointer (task, g_steal_pointer (&tid), g_free);
432 }
433
434 /*
435 * pk_control_get_tid_internal:
436 **/
437 static void
438 pk_control_get_tid_internal (PkControl *control,
439 GTask *task)
440 {
441 PkControlPrivate *priv = GET_PRIVATE(control);
442 GCancellable *cancellable;
443
444 g_assert (PK_IS_CONTROL (control));
445 g_assert (G_IS_TASK (task));
446
447 cancellable = g_task_get_cancellable (task);
448 g_dbus_proxy_call (priv->proxy,
449 "CreateTransaction",
450 NULL,
451 G_DBUS_CALL_FLAGS_NONE,
452 PK_CONTROL_DBUS_METHOD_TIMEOUT,
453 cancellable,
454 pk_control_get_tid_cb,
455 g_steal_pointer (&task));
456 }
457
458 /*
459 * pk_control_get_tid_proxy_cb:
460 **/
461 static void
462 pk_control_get_tid_proxy_cb (GObject *source_object,
463 GAsyncResult *res,
464 gpointer user_data)
465 {
466 g_autoptr(GTask) task = G_TASK (user_data);
467 g_autoptr(GDBusProxy) proxy = NULL;
468 g_autoptr(GError) error = NULL;
469 PkControl *control = g_task_get_source_object (task);
470
471 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
472 if (proxy == NULL) {
473 g_task_return_error (task, g_steal_pointer (&error));
474 return;
475 }
476
477 pk_control_proxy_connect (control, proxy);
478 pk_control_get_tid_internal (control, g_steal_pointer (&task));
479 }
480
481 /**
482 * pk_control_get_tid_async:
483 * @control: a valid #PkControl instance
484 * @cancellable: a #GCancellable or %NULL
485 * @callback: the function to run on completion
486 * @user_data: the data to pass to @callback
487 *
488 * Gets a transacton ID from the daemon.
489 *
490 * Since: 0.5.2
491 **/
492 void
493 pk_control_get_tid_async (PkControl *control,
494 GCancellable *cancellable,
495 GAsyncReadyCallback callback,
496 gpointer user_data)
497 {
498 PkControlPrivate *priv = GET_PRIVATE(control);
499 g_autoptr(GError) error = NULL;
500 g_autoptr(GTask) task = NULL;
501
502 g_return_if_fail (PK_IS_CONTROL (control));
503 g_return_if_fail (callback != NULL);
504 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
505
506 /* check not already cancelled */
507 if (cancellable != NULL &&
508 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
509 g_task_report_error (control, callback, user_data,
510 pk_control_get_tid_async,
511 g_steal_pointer (&error));
512 return;
513 }
514
515 task = g_task_new (control, cancellable, callback, user_data);
516 g_task_set_source_tag (task, pk_control_get_tid_async);
517
518 /* skip straight to the D-Bus method if already connection */
519 if (priv->proxy != NULL) {
520 pk_control_get_tid_internal (control, g_steal_pointer (&task));
521 } else {
522 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
523 G_DBUS_PROXY_FLAGS_NONE,
524 NULL,
525 PK_DBUS_SERVICE,
526 PK_DBUS_PATH,
527 PK_DBUS_INTERFACE,
528 priv->cancellable,
529 pk_control_get_tid_proxy_cb,
530 g_steal_pointer (&task));
531 }
532 }
533
534 /**
535 * pk_control_get_tid_finish:
536 * @control: a valid #PkControl instance
537 * @res: the #GAsyncResult
538 * @error: A #GError or %NULL
539 *
540 * Gets the result from the asynchronous function.
541 *
542 * Returns: (nullable): the ID, or %NULL if unset, free with g_free()
543 *
544 * Since: 0.5.2
545 **/
546 gchar *
547 pk_control_get_tid_finish (PkControl *control,
548 GAsyncResult *res,
549 GError **error)
550 {
551 g_return_val_if_fail (PK_IS_CONTROL (control), NULL);
552 g_return_val_if_fail (g_task_is_valid (res, control), NULL);
553 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_get_tid_async), NULL);
554 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
555
556 return g_task_propagate_pointer (G_TASK (res), error);
557 }
558
559 /**********************************************************************/
560
561 /*
562 * pk_control_suggest_daemon_quit_cb:
563 **/
564 static void
565 pk_control_suggest_daemon_quit_cb (GObject *source_object,
566 GAsyncResult *res,
567 gpointer user_data)
568 {
569 GDBusProxy *proxy = G_DBUS_PROXY (source_object);
570 g_autoptr(GTask) task = G_TASK (user_data);
571 g_autoptr(GError) error = NULL;
572 g_autoptr(GVariant) value = NULL;
573
574 /* get the result */
575 value = g_dbus_proxy_call_finish (proxy, res, &error);
576 if (value == NULL) {
577 g_task_return_error (task, g_steal_pointer (&error));
578 return;
579 }
580
581 /* we're done */
582 g_task_return_boolean (task, TRUE);
583 }
584
585 /*
586 * pk_control_suggest_daemon_quit_internal:
587 **/
588 static void
589 pk_control_suggest_daemon_quit_internal (PkControl *control,
590 GTask *task)
591 {
592 PkControlPrivate *priv = GET_PRIVATE(control);
593 GCancellable *cancellable;
594
595 g_assert (PK_IS_CONTROL (control));
596 g_assert (G_IS_TASK (task));
597
598 cancellable = g_task_get_cancellable (task);
599 g_dbus_proxy_call (priv->proxy,
600 "SuggestDaemonQuit",
601 NULL,
602 G_DBUS_CALL_FLAGS_NONE,
603 PK_CONTROL_DBUS_METHOD_TIMEOUT,
604 cancellable,
605 pk_control_suggest_daemon_quit_cb,
606 g_steal_pointer (&task));
607 }
608
609 /*
610 * pk_control_suggest_daemon_quit_proxy_cb:
611 **/
612 static void
613 pk_control_suggest_daemon_quit_proxy_cb (GObject *source_object,
614 GAsyncResult *res,
615 gpointer user_data)
616 {
617 g_autoptr(GTask) task = G_TASK (user_data);
618 g_autoptr(GDBusProxy) proxy = NULL;
619 g_autoptr(GError) error = NULL;
620 PkControl *control = g_task_get_source_object (task);
621
622 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
623 if (proxy == NULL) {
624 g_task_return_error (task, g_steal_pointer (&error));
625 return;
626 }
627
628 pk_control_proxy_connect (control, proxy);
629 pk_control_suggest_daemon_quit_internal (control, g_steal_pointer (&task));
630 }
631
632 /**
633 * pk_control_suggest_daemon_quit_async:
634 * @control: a valid #PkControl instance
635 * @cancellable: a #GCancellable or %NULL
636 * @callback: the function to run on completion
637 * @user_data: the data to pass to @callback
638 *
639 * Suggests to the daemon that it should quit as soon as possible.
640 *
641 * Since: 0.6.2
642 **/
643 void
644 pk_control_suggest_daemon_quit_async (PkControl *control,
645 GCancellable *cancellable,
646 GAsyncReadyCallback callback,
647 gpointer user_data)
648 {
649 PkControlPrivate *priv = GET_PRIVATE(control);
650 g_autoptr(GTask) task = NULL;
651 g_autoptr(GError) error = NULL;
652
653 g_return_if_fail (PK_IS_CONTROL (control));
654 g_return_if_fail (callback != NULL);
655 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
656
657 /* check not already cancelled */
658 if (cancellable != NULL &&
659 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
660 g_task_report_error (control, callback, user_data,
661 pk_control_suggest_daemon_quit_async,
662 g_steal_pointer (&error));
663 return;
664 }
665
666 task = g_task_new (control, cancellable, callback, user_data);
667 g_task_set_source_tag (task, pk_control_suggest_daemon_quit_async);
668
669 /* skip straight to the D-Bus method if already connection */
670 if (priv->proxy != NULL) {
671 pk_control_suggest_daemon_quit_internal (control, g_steal_pointer (&task));
672 } else {
673 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
674 G_DBUS_PROXY_FLAGS_NONE,
675 NULL,
676 PK_DBUS_SERVICE,
677 PK_DBUS_PATH,
678 PK_DBUS_INTERFACE,
679 priv->cancellable,
680 pk_control_suggest_daemon_quit_proxy_cb,
681 g_steal_pointer (&task));
682 }
683 }
684
685 /**
686 * pk_control_suggest_daemon_quit_finish:
687 * @control: a valid #PkControl instance
688 * @res: the #GAsyncResult
689 * @error: A #GError or %NULL
690 *
691 * Gets the result from the asynchronous function.
692 *
693 * Returns: %TRUE if the suggestion was sent
694 *
695 * Since: 0.6.2
696 **/
697 gboolean
698 pk_control_suggest_daemon_quit_finish (PkControl *control, GAsyncResult *res, GError **error)
699 {
700 g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
701 g_return_val_if_fail (g_task_is_valid (res, control), FALSE);
702 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_suggest_daemon_quit_async), FALSE);
703 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
704
705 return g_task_propagate_boolean (G_TASK (res), error);
706 }
707
708 /**********************************************************************/
709
710 /*
711 * pk_control_get_daemon_state_cb:
712 **/
713 static void
714 pk_control_get_daemon_state_cb (GObject *source_object,
715 GAsyncResult *res,
716 gpointer user_data)
717 {
718 GDBusProxy *proxy = G_DBUS_PROXY (source_object);
719 g_autoptr (GTask) task = G_TASK (user_data);
720 g_autoptr(GError) error = NULL;
721 g_autoptr(GVariant) value = NULL;
722 gchar *daemon_state = NULL;
723
724 /* get the result */
725 value = g_dbus_proxy_call_finish (proxy, res, &error);
726 if (value == NULL) {
727 /* fix up the D-Bus error */
728 pk_control_fixup_dbus_error (error);
729 g_task_return_error (task, g_steal_pointer (&error));
730 return;
731 }
732
733 /* save results */
734 g_variant_get (value, "(s)", &daemon_state);
735
736 /* we're done */
737 g_task_return_pointer (task, g_steal_pointer (&daemon_state), g_free);
738 }
739
740 /*
741 * pk_control_get_daemon_state_internal:
742 **/
743 static void
744 pk_control_get_daemon_state_internal (PkControl *control,
745 GTask *task)
746 {
747 PkControlPrivate *priv = GET_PRIVATE(control);
748 GCancellable *cancellable;
749
750 g_assert (PK_IS_CONTROL (control));
751 g_assert (G_IS_TASK (task));
752
753 cancellable = g_task_get_cancellable (task);
754 g_dbus_proxy_call (priv->proxy,
755 "GetDaemonState",
756 NULL,
757 G_DBUS_CALL_FLAGS_NONE,
758 PK_CONTROL_DBUS_METHOD_TIMEOUT,
759 cancellable,
760 pk_control_get_daemon_state_cb,
761 g_steal_pointer (&task));
762 }
763
764 /*
765 * pk_control_get_daemon_state_proxy_cb:
766 **/
767 static void
768 pk_control_get_daemon_state_proxy_cb (GObject *source_object,
769 GAsyncResult *res,
770 gpointer user_data)
771 {
772 g_autoptr(GError) error = NULL;
773 g_autoptr(GTask) task = G_TASK (user_data);
774 g_autoptr(GDBusProxy) proxy = NULL;
775 PkControl *control = g_task_get_source_object (task);
776
777 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
778 if (proxy == NULL) {
779 g_task_return_error (task, g_steal_pointer (&error));
780 return;
781 }
782
783 pk_control_proxy_connect (control, proxy);
784 pk_control_get_daemon_state_internal (control, g_steal_pointer (&task));
785 }
786
787 /**
788 * pk_control_get_daemon_state_async:
789 * @control: a valid #PkControl instance
790 * @cancellable: a #GCancellable or %NULL
791 * @callback: the function to run on completion
792 * @user_data: the data to pass to @callback
793 *
794 * Gets the debugging state from the daemon.
795 *
796 * Since: 0.5.2
797 **/
798 void
799 pk_control_get_daemon_state_async (PkControl *control,
800 GCancellable *cancellable,
801 GAsyncReadyCallback callback,
802 gpointer user_data)
803 {
804 PkControlPrivate *priv = GET_PRIVATE(control);
805 g_autoptr(GTask) task = NULL;
806 g_autoptr(GError) error = NULL;
807
808 g_return_if_fail (PK_IS_CONTROL (control));
809 g_return_if_fail (callback != NULL);
810 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
811
812 /* check not already cancelled */
813 if (cancellable != NULL &&
814 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
815 g_task_report_error (control, callback, user_data,
816 pk_control_get_daemon_state_async,
817 g_steal_pointer (&error));
818 return;
819 }
820
821 task = g_task_new (control, cancellable, callback, user_data);
822 g_task_set_source_tag (task, pk_control_get_daemon_state_async);
823
824 /* skip straight to the D-Bus method if already connection */
825 if (priv->proxy != NULL) {
826 pk_control_get_daemon_state_internal (control, g_steal_pointer (&task));
827 } else {
828 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
829 G_DBUS_PROXY_FLAGS_NONE,
830 NULL,
831 PK_DBUS_SERVICE,
832 PK_DBUS_PATH,
833 PK_DBUS_INTERFACE,
834 priv->cancellable,
835 pk_control_get_daemon_state_proxy_cb,
836 g_steal_pointer (&task));
837 }
838 }
839
840 /**
841 * pk_control_get_daemon_state_finish:
842 * @control: a valid #PkControl instance
843 * @res: the #GAsyncResult
844 * @error: A #GError or %NULL
845 *
846 * Gets the result from the asynchronous function.
847 *
848 * Returns: (nullable): the ID, or %NULL if unset, free with g_free()
849 *
850 * Since: 0.5.2
851 **/
852 gchar *
853 pk_control_get_daemon_state_finish (PkControl *control,
854 GAsyncResult *res,
855 GError **error)
856 {
857 g_return_val_if_fail (PK_IS_CONTROL (control), NULL);
858 g_return_val_if_fail (g_task_is_valid (res, control), NULL);
859 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_get_daemon_state_async), NULL);
860 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
861
862 return g_task_propagate_pointer (G_TASK (res), error);
863 }
864
865 /**********************************************************************/
866
867 /*
868 * pk_control_set_proxy_cb:
869 **/
870 static void
871 pk_control_set_proxy_cb (GObject *source_object,
872 GAsyncResult *res,
873 gpointer user_data)
874 {
875 GDBusProxy *proxy = G_DBUS_PROXY (source_object);
876 g_autoptr(GTask) task = G_TASK (user_data);
877 g_autoptr(GError) error = NULL;
878 g_autoptr(GVariant) value = NULL;
879
880 /* get the result */
881 value = g_dbus_proxy_call_finish (proxy, res, &error);
882 if (value == NULL) {
883 g_debug ("Failed to set proxy: %s", error->message);
884 g_task_return_error (task, g_steal_pointer (&error));
885 return;
886 }
887
888 /* we're done */
889 g_task_return_boolean (task, TRUE);
890 }
891
892 /*
893 * pk_control_set_proxy_internal:
894 **/
895 static void
896 pk_control_set_proxy_internal (PkControl *control,
897 GTask *task)
898 {
899 PkControlPrivate *priv = GET_PRIVATE(control);
900 GCancellable *cancellable;
901 GVariant *parameters;
902
903 g_assert (PK_IS_CONTROL (control));
904 g_assert (G_IS_TASK (task));
905
906 cancellable = g_task_get_cancellable (task);
907 parameters = g_task_get_task_data (task);
908 g_dbus_proxy_call (priv->proxy,
909 "SetProxy",
910 parameters,
911 G_DBUS_CALL_FLAGS_NONE,
912 PK_CONTROL_DBUS_METHOD_TIMEOUT,
913 cancellable,
914 pk_control_set_proxy_cb,
915 g_steal_pointer (&task));
916 }
917
918 /*
919 * pk_control_set_proxy_proxy_cb:
920 **/
921 static void
922 pk_control_set_proxy_proxy_cb (GObject *source_object,
923 GAsyncResult *res,
924 gpointer user_data)
925 {
926 g_autoptr(GError) error = NULL;
927 g_autoptr(GTask) task = G_TASK (user_data);
928 g_autoptr(GDBusProxy) proxy = NULL;
929 PkControl *control = g_task_get_source_object (task);
930
931 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
932 if (proxy == NULL) {
933 g_task_return_error (task, g_steal_pointer (&error));
934 return;
935 }
936
937 pk_control_proxy_connect (control, proxy);
938 pk_control_set_proxy_internal (control, g_steal_pointer (&task));
939 }
940
941 /**
942 * pk_control_set_proxy2_async: (finish-func pk_control_set_proxy_finish):
943 * @control: a valid #PkControl instance
944 * @proxy_http: a HTTP proxy string such as "username:[email protected]:8080", or %NULL
945 * @proxy_https: a HTTPS proxy string such as "username:[email protected]:8080", or %NULL
946 * @proxy_ftp: a FTP proxy string such as "server.lan:8080", or %NULL
947 * @proxy_socks: a SOCKS proxy string such as "server.lan:8080", or %NULL
948 * @no_proxy: a list of download IPs that shouldn't go through the proxy, or %NULL
949 * @pac: a PAC string, or %NULL
950 * @cancellable: a #GCancellable or %NULL
951 * @callback: the function to run on completion
952 * @user_data: the data to pass to @callback
953 *
954 * Set a proxy on the PK daemon
955 *
956 * Since: 0.6.13
957 **/
958 void
959 pk_control_set_proxy2_async (PkControl *control,
960 const gchar *proxy_http,
961 const gchar *proxy_https,
962 const gchar *proxy_ftp,
963 const gchar *proxy_socks,
964 const gchar *no_proxy,
965 const gchar *pac,
966 GCancellable *cancellable,
967 GAsyncReadyCallback callback,
968 gpointer user_data)
969 {
970 PkControlPrivate *priv = GET_PRIVATE(control);
971 g_autoptr(GTask) task = NULL;
972 g_autoptr(GError) error = NULL;
973 GVariant *parameters = NULL;
974
975 g_return_if_fail (PK_IS_CONTROL (control));
976 g_return_if_fail (callback != NULL);
977 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
978
979 /* check not already cancelled */
980 if (cancellable != NULL &&
981 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
982 g_task_report_error (control, callback, user_data,
983 pk_control_set_proxy_async,
984 g_steal_pointer (&error));
985 return;
986 }
987
988 /* save state */
989 parameters = g_variant_new ("(ssssss)",
990 proxy_http ? proxy_http : "",
991 proxy_https ? proxy_https : "",
992 proxy_ftp ? proxy_ftp : "",
993 proxy_socks ? proxy_socks : "",
994 no_proxy ? no_proxy : "",
995 pac ? pac : "");
996
997 task = g_task_new (control, cancellable, callback, user_data);
998 g_task_set_source_tag (task, pk_control_set_proxy_async);
999 g_task_set_task_data (task, g_variant_ref_sink (parameters), (GDestroyNotify) g_variant_unref);
1000
1001 /* skip straight to the D-Bus method if already connection */
1002 if (priv->proxy != NULL) {
1003 pk_control_set_proxy_internal (control, g_steal_pointer (&task));
1004 } else {
1005 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
1006 G_DBUS_PROXY_FLAGS_NONE,
1007 NULL,
1008 PK_DBUS_SERVICE,
1009 PK_DBUS_PATH,
1010 PK_DBUS_INTERFACE,
1011 priv->cancellable,
1012 pk_control_set_proxy_proxy_cb,
1013 g_steal_pointer (&task));
1014 }
1015 }
1016
1017 /**
1018 * pk_control_set_proxy_async:
1019 * @control: a valid #PkControl instance
1020 * @proxy_http: a HTTP proxy string such as "username:[email protected]:8080"
1021 * @proxy_ftp: a FTP proxy string such as "server.lan:8080"
1022 * @cancellable: a #GCancellable or %NULL
1023 * @callback: the function to run on completion
1024 * @user_data: the data to pass to @callback
1025 *
1026 * Set a proxy on the PK daemon
1027 *
1028 * NOTE: This is just provided for backwards compatibility.
1029 * Clients should really be using pk_control_set_proxy2_async().
1030 *
1031 * Since: 0.5.2
1032 **/
1033 void
1034 pk_control_set_proxy_async (PkControl *control,
1035 const gchar *proxy_http,
1036 const gchar *proxy_ftp,
1037 GCancellable *cancellable,
1038 GAsyncReadyCallback callback,
1039 gpointer user_data)
1040 {
1041 pk_control_set_proxy2_async (control,
1042 proxy_http,
1043 NULL,
1044 proxy_ftp,
1045 NULL,
1046 NULL,
1047 NULL,
1048 cancellable,
1049 callback,
1050 user_data);
1051 }
1052
1053 /**
1054 * pk_control_set_proxy_finish:
1055 * @control: a valid #PkControl instance
1056 * @res: the #GAsyncResult
1057 * @error: A #GError or %NULL
1058 *
1059 * Gets the result from the asynchronous function.
1060 *
1061 * Returns: %TRUE if we set the proxy successfully
1062 *
1063 * Since: 0.5.2
1064 **/
1065 gboolean
1066 pk_control_set_proxy_finish (PkControl *control,
1067 GAsyncResult *res,
1068 GError **error)
1069 {
1070 g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
1071 g_return_val_if_fail (g_task_is_valid (res, control), FALSE);
1072 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_set_proxy_async), FALSE);
1073 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1074
1075 return g_task_propagate_boolean (G_TASK (res), error);
1076 }
1077
1078 /**********************************************************************/
1079
1080 /*
1081 * pk_control_get_transaction_list_cb:
1082 **/
1083 static void
1084 pk_control_get_transaction_list_cb (GObject *source_object,
1085 GAsyncResult *res,
1086 gpointer user_data)
1087 {
1088 const gchar **tlist_tmp = NULL;
1089 GDBusProxy *proxy = G_DBUS_PROXY (source_object);
1090 g_autoptr(GTask) task = G_TASK (user_data);
1091 g_autoptr(GError) error = NULL;
1092 g_autoptr(GVariant) value = NULL;
1093
1094 /* get the result */
1095 value = g_dbus_proxy_call_finish (proxy, res, &error);
1096 if (value == NULL) {
1097 /* fix up the D-Bus error */
1098 pk_control_fixup_dbus_error (error);
1099 g_task_return_error (task, g_steal_pointer (&error));
1100 return;
1101 }
1102
1103 /* unwrap data */
1104 g_variant_get (value, "(^a&o)", &tlist_tmp);
1105
1106 /* we're done */
1107 if (tlist_tmp == NULL) {
1108 g_task_return_pointer (task, g_new0 (gchar *, 1), g_free);
1109 } else {
1110 g_task_return_pointer (task, g_strdupv ((gchar **)tlist_tmp), (GDestroyNotify) g_strfreev);
1111 }
1112 }
1113
1114 /*
1115 * pk_control_get_transaction_list_internal:
1116 **/
1117 static void
1118 pk_control_get_transaction_list_internal (PkControl *control,
1119 GTask *task)
1120 {
1121 PkControlPrivate *priv = GET_PRIVATE(control);
1122 GCancellable *cancellable;
1123
1124 g_assert (PK_IS_CONTROL (control));
1125 g_assert (G_IS_TASK (task));
1126
1127 cancellable = g_task_get_cancellable (task);
1128 g_dbus_proxy_call (priv->proxy,
1129 "GetTransactionList",
1130 NULL,
1131 G_DBUS_CALL_FLAGS_NONE,
1132 PK_CONTROL_DBUS_METHOD_TIMEOUT,
1133 cancellable,
1134 pk_control_get_transaction_list_cb,
1135 g_steal_pointer (&task));
1136 }
1137
1138 /*
1139 * pk_control_get_transaction_list_proxy_cb:
1140 **/
1141 static void
1142 pk_control_get_transaction_list_proxy_cb (GObject *source_object,
1143 GAsyncResult *res,
1144 gpointer user_data)
1145 {
1146 g_autoptr(GError) error = NULL;
1147 g_autoptr(GTask) task = G_TASK (user_data);
1148 g_autoptr(GDBusProxy) proxy = NULL;
1149 PkControl *control = g_task_get_source_object (task);
1150
1151 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
1152 if (proxy == NULL) {
1153 g_task_return_error (task, g_steal_pointer (&error));
1154 return;
1155 }
1156
1157 pk_control_proxy_connect (control, proxy);
1158 pk_control_get_transaction_list_internal (control, g_steal_pointer (&task));
1159 }
1160
1161 /**
1162 * pk_control_get_transaction_list_async:
1163 * @control: a valid #PkControl instance
1164 * @cancellable: a #GCancellable or %NULL
1165 * @callback: the function to run on completion
1166 * @user_data: the data to pass to @callback
1167 *
1168 * Gets the transactions currently running in the daemon.
1169 *
1170 * Since: 0.5.2
1171 **/
1172 void
1173 pk_control_get_transaction_list_async (PkControl *control,
1174 GCancellable *cancellable,
1175 GAsyncReadyCallback callback,
1176 gpointer user_data)
1177 {
1178 PkControlPrivate *priv = GET_PRIVATE(control);
1179 g_autoptr(GTask) task = NULL;
1180 g_autoptr(GError) error = NULL;
1181
1182 g_return_if_fail (PK_IS_CONTROL (control));
1183 g_return_if_fail (callback != NULL);
1184 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1185
1186 /* check not already cancelled */
1187 if (cancellable != NULL &&
1188 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
1189 g_task_report_error (control, callback, user_data,
1190 pk_control_get_transaction_list_async,
1191 g_steal_pointer (&error));
1192 return;
1193 }
1194
1195 task = g_task_new (control, cancellable, callback, user_data);
1196 g_task_set_source_tag (task, pk_control_get_transaction_list_async);
1197
1198 /* skip straight to the D-Bus method if already connection */
1199 if (priv->proxy != NULL) {
1200 pk_control_get_transaction_list_internal (control, g_steal_pointer (&task));
1201 } else {
1202 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
1203 G_DBUS_PROXY_FLAGS_NONE,
1204 NULL,
1205 PK_DBUS_SERVICE,
1206 PK_DBUS_PATH,
1207 PK_DBUS_INTERFACE,
1208 priv->cancellable,
1209 pk_control_get_transaction_list_proxy_cb,
1210 g_steal_pointer (&task));
1211 }
1212 }
1213
1214 /**
1215 * pk_control_get_transaction_list_finish:
1216 * @control: a valid #PkControl instance
1217 * @res: the #GAsyncResult
1218 * @error: A #GError or %NULL
1219 *
1220 * Gets the result from the asynchronous function.
1221 *
1222 * Returns: (transfer full): A GStrv list of transaction ID's, free with g_strfreev()
1223 *
1224 * Since: 0.5.2
1225 **/
1226 gchar **
1227 pk_control_get_transaction_list_finish (PkControl *control,
1228 GAsyncResult *res,
1229 GError **error)
1230 {
1231 g_return_val_if_fail (PK_IS_CONTROL (control), NULL);
1232 g_return_val_if_fail (g_task_is_valid (res, control), NULL);
1233 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_get_transaction_list_async), NULL);
1234 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1235
1236 return g_task_propagate_pointer (G_TASK (res), error);
1237 }
1238
1239 /**********************************************************************/
1240
1241 /*
1242 * pk_control_get_time_since_action_cb:
1243 **/
1244 static void
1245 pk_control_get_time_since_action_cb (GObject *source_object,
1246 GAsyncResult *res,
1247 gpointer user_data)
1248 {
1249 GDBusProxy *proxy = G_DBUS_PROXY (source_object);
1250 g_autoptr(GTask) task = G_TASK (user_data);
1251 g_autoptr(GVariant) value = NULL;
1252 g_autoptr(GError) error = NULL;
1253 guint time = 0;
1254
1255 /* get the result */
1256 value = g_dbus_proxy_call_finish (proxy, res, &error);
1257 if (value == NULL) {
1258 /* fix up the D-Bus error */
1259 pk_control_fixup_dbus_error (error);
1260 g_task_return_error (task, g_steal_pointer (&error));
1261 return;
1262 }
1263
1264 /* save data */
1265 g_variant_get (value, "(u)", &time);
1266 if (time == 0) {
1267 g_task_return_new_error (task,
1268 PK_CONTROL_ERROR, PK_CONTROL_ERROR_FAILED,
1269 "could not get time");
1270 return;
1271 }
1272
1273 /* we're done */
1274 g_task_return_pointer (task, GUINT_TO_POINTER (time), NULL);
1275 }
1276
1277 /*
1278 * pk_control_get_time_since_action_internal:
1279 **/
1280 static void
1281 pk_control_get_time_since_action_internal (PkControl *control,
1282 GTask *task)
1283 {
1284 PkControlPrivate *priv = GET_PRIVATE(control);
1285 GCancellable *cancellable;
1286 guint role;
1287
1288 g_assert (PK_IS_CONTROL (control));
1289 g_assert (G_IS_TASK (task));
1290
1291 cancellable = g_task_get_cancellable (task);
1292 role = GPOINTER_TO_UINT (g_task_get_task_data (task));
1293 g_dbus_proxy_call (priv->proxy,
1294 "GetTimeSinceAction",
1295 g_variant_new ("(u)", role),
1296 G_DBUS_CALL_FLAGS_NONE,
1297 PK_CONTROL_DBUS_METHOD_TIMEOUT,
1298 cancellable,
1299 pk_control_get_time_since_action_cb,
1300 g_steal_pointer (&task));
1301 }
1302
1303 /*
1304 * pk_control_get_time_since_action_proxy_cb:
1305 **/
1306 static void
1307 pk_control_get_time_since_action_proxy_cb (GObject *source_object,
1308 GAsyncResult *res,
1309 gpointer user_data)
1310 {
1311 g_autoptr(GError) error = NULL;
1312 g_autoptr(GTask) task = G_TASK (user_data);
1313 g_autoptr(GDBusProxy) proxy = NULL;
1314 PkControl *control = g_task_get_source_object (task);
1315
1316 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
1317 if (proxy == NULL) {
1318 g_task_return_error (task, g_steal_pointer (&error));
1319 return;
1320 }
1321
1322 pk_control_proxy_connect (control, proxy);
1323 pk_control_get_time_since_action_internal (control, g_steal_pointer (&task));
1324 }
1325
1326 /**
1327 * pk_control_get_time_since_action_async:
1328 * @control: a valid #PkControl instance
1329 * @role: the role enum, e.g. %PK_ROLE_ENUM_GET_UPDATES
1330 * @cancellable: a #GCancellable or %NULL
1331 * @callback: the function to run on completion
1332 * @user_data: the data to pass to @callback
1333 *
1334 * We may want to know how long it has been since we refreshed the cache or
1335 * retrieved the update list.
1336 *
1337 * Since: 0.5.2
1338 **/
1339 void
1340 pk_control_get_time_since_action_async (PkControl *control,
1341 PkRoleEnum role,
1342 GCancellable *cancellable,
1343 GAsyncReadyCallback callback,
1344 gpointer user_data)
1345 {
1346 PkControlPrivate *priv = GET_PRIVATE(control);
1347 g_autoptr(GTask) task = NULL;
1348 g_autoptr(GError) error = NULL;
1349
1350 g_return_if_fail (PK_IS_CONTROL (control));
1351 g_return_if_fail (callback != NULL);
1352 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1353
1354 /* check not already cancelled */
1355 if (cancellable != NULL &&
1356 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
1357 g_task_report_error (control, callback, user_data,
1358 pk_control_get_time_since_action_async,
1359 g_steal_pointer (&error));
1360 return;
1361 }
1362
1363 task = g_task_new (control, cancellable, callback, user_data);
1364 g_task_set_source_tag (task, pk_control_get_time_since_action_async);
1365 g_task_set_task_data (task, GUINT_TO_POINTER ((guint) role), NULL);
1366
1367 /* skip straight to the D-Bus method if already connection */
1368 if (priv->proxy != NULL) {
1369 pk_control_get_time_since_action_internal (control, g_steal_pointer (&task));
1370 } else {
1371 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
1372 G_DBUS_PROXY_FLAGS_NONE,
1373 NULL,
1374 PK_DBUS_SERVICE,
1375 PK_DBUS_PATH,
1376 PK_DBUS_INTERFACE,
1377 priv->cancellable,
1378 pk_control_get_time_since_action_proxy_cb,
1379 g_steal_pointer (&task));
1380 }
1381 }
1382
1383 /**
1384 * pk_control_get_time_since_action_finish:
1385 * @control: a valid #PkControl instance
1386 * @res: the #GAsyncResult
1387 * @error: A #GError or %NULL
1388 *
1389 * Gets the result from the asynchronous function.
1390 *
1391 * Returns: %TRUE if the daemon serviced the request
1392 *
1393 * Since: 0.5.2
1394 **/
1395 guint
1396 pk_control_get_time_since_action_finish (PkControl *control,
1397 GAsyncResult *res,
1398 GError **error)
1399 {
1400 g_return_val_if_fail (PK_IS_CONTROL (control), 0);
1401 g_return_val_if_fail (g_task_is_valid (res, control), 0);
1402 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_get_time_since_action_async), 0);
1403 g_return_val_if_fail (error == NULL || *error == NULL, 0);
1404
1405 return GPOINTER_TO_UINT(g_task_propagate_pointer (G_TASK (res), error));
1406 }
1407
1408 /**********************************************************************/
1409
1410 /*
1411 * pk_control_can_authorize_cb:
1412 **/
1413 static void
1414 pk_control_can_authorize_cb (GObject *source_object,
1415 GAsyncResult *res,
1416 gpointer user_data)
1417 {
1418 GDBusProxy *proxy = G_DBUS_PROXY (source_object);
1419 g_autoptr(GTask) task = G_TASK (user_data);
1420 g_autoptr(GError) error = NULL;
1421 g_autoptr(GVariant) value = NULL;
1422 guint authorize = PK_AUTHORIZE_ENUM_UNKNOWN;
1423
1424 /* get the result */
1425 value = g_dbus_proxy_call_finish (proxy, res, &error);
1426 if (value == NULL) {
1427 /* fix up the D-Bus error */
1428 pk_control_fixup_dbus_error (error);
1429 g_task_return_error (task, g_steal_pointer (&error));
1430 return;
1431 }
1432
1433 /* save data */
1434 g_variant_get (value, "(u)", &authorize);
1435 if (authorize == PK_AUTHORIZE_ENUM_UNKNOWN) {
1436 g_task_return_new_error (task,
1437 PK_CONTROL_ERROR, PK_CONTROL_ERROR_FAILED,
1438 "could not get state");
1439 return;
1440 }
1441
1442 /* we're done */
1443 g_task_return_pointer (task, GUINT_TO_POINTER (authorize), NULL);
1444 }
1445
1446 /*
1447 * pk_control_can_authorize_internal:
1448 **/
1449 static void
1450 pk_control_can_authorize_internal (PkControl *control,
1451 GTask *task)
1452 {
1453 PkControlPrivate *priv = GET_PRIVATE(control);
1454 GCancellable *cancellable;
1455 const gchar *action_id;
1456
1457 g_assert (PK_IS_CONTROL (control));
1458 g_assert (G_IS_TASK (task));
1459
1460 cancellable = g_task_get_cancellable (task);
1461 action_id = g_task_get_task_data (task);
1462 g_dbus_proxy_call (priv->proxy,
1463 "CanAuthorize",
1464 g_variant_new ("(s)", action_id),
1465 G_DBUS_CALL_FLAGS_NONE,
1466 PK_CONTROL_DBUS_METHOD_TIMEOUT,
1467 cancellable,
1468 pk_control_can_authorize_cb,
1469 g_steal_pointer (&task));
1470 }
1471
1472 /*
1473 * pk_control_can_authorize_proxy_cb:
1474 **/
1475 static void
1476 pk_control_can_authorize_proxy_cb (GObject *source_object,
1477 GAsyncResult *res,
1478 gpointer user_data)
1479 {
1480 g_autoptr(GError) error = NULL;
1481 g_autoptr(GTask) task = G_TASK (user_data);
1482 g_autoptr(GDBusProxy) proxy = NULL;
1483 PkControl *control = g_task_get_source_object (task);
1484
1485 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
1486 if (proxy == NULL) {
1487 g_task_return_error (task, g_steal_pointer (&error));
1488 return;
1489 }
1490
1491 pk_control_proxy_connect (control, proxy);
1492 pk_control_can_authorize_internal (control, g_steal_pointer (&task));
1493 }
1494
1495 /**
1496 * pk_control_can_authorize_async:
1497 * @control: a valid #PkControl instance
1498 * @action_id: The action ID, for instance "org.freedesktop.PackageKit.install-untrusted"
1499 * @cancellable: a #GCancellable or %NULL
1500 * @callback: the function to run on completion
1501 * @user_data: the data to pass to @callback
1502 *
1503 * We may want to know before we run a method if we are going to be denied,
1504 * accepted or challenged for authentication.
1505 *
1506 * Since: 0.5.2
1507 **/
1508 void
1509 pk_control_can_authorize_async (PkControl *control,
1510 const gchar *action_id,
1511 GCancellable *cancellable,
1512 GAsyncReadyCallback callback,
1513 gpointer user_data)
1514 {
1515 PkControlPrivate *priv = GET_PRIVATE(control);
1516 g_autoptr(GTask) task = NULL;
1517 g_autoptr(GError) error = NULL;
1518
1519 g_return_if_fail (PK_IS_CONTROL (control));
1520 g_return_if_fail (callback != NULL);
1521 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1522
1523 /* check not already cancelled */
1524 if (cancellable != NULL &&
1525 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
1526 g_task_report_error (control, callback, user_data,
1527 pk_control_can_authorize_async,
1528 g_steal_pointer (&error));
1529 return;
1530 }
1531
1532 task = g_task_new (control, cancellable, callback, user_data);
1533 g_task_set_source_tag (task, pk_control_can_authorize_async);
1534 g_task_set_task_data (task, g_strdup (action_id), g_free);
1535
1536 /* skip straight to the D-Bus method if already connection */
1537 if (priv->proxy != NULL) {
1538 pk_control_can_authorize_internal (control, g_steal_pointer (&task));
1539 } else {
1540 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
1541 G_DBUS_PROXY_FLAGS_NONE,
1542 NULL,
1543 PK_DBUS_SERVICE,
1544 PK_DBUS_PATH,
1545 PK_DBUS_INTERFACE,
1546 priv->cancellable,
1547 pk_control_can_authorize_proxy_cb,
1548 g_steal_pointer (&task));
1549 }
1550 }
1551
1552 /**
1553 * pk_control_can_authorize_finish:
1554 * @control: a valid #PkControl instance
1555 * @res: the #GAsyncResult
1556 * @error: A #GError or %NULL
1557 *
1558 * Gets the result from the asynchronous function.
1559 *
1560 * Returns: the #PkAuthorizeEnum or %PK_AUTHORIZE_ENUM_UNKNOWN if the method failed
1561 *
1562 * Since: 0.5.2
1563 **/
1564 PkAuthorizeEnum
1565 pk_control_can_authorize_finish (PkControl *control, GAsyncResult *res, GError **error)
1566 {
1567 gpointer result;
1568
1569 g_return_val_if_fail (PK_IS_CONTROL (control), PK_AUTHORIZE_ENUM_UNKNOWN);
1570 g_return_val_if_fail (g_task_is_valid (res, control), PK_AUTHORIZE_ENUM_UNKNOWN);
1571 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_can_authorize_async), PK_AUTHORIZE_ENUM_UNKNOWN);
1572
1573 result = g_task_propagate_pointer (G_TASK (res), error);
1574 if (!result)
1575 return PK_AUTHORIZE_ENUM_UNKNOWN;
1576
1577 return (PkAuthorizeEnum) GPOINTER_TO_UINT(result);
1578 }
1579
1580 /**********************************************************************/
1581
1582 /*
1583 * pk_control_get_properties_cb:
1584 **/
1585 static void
1586 pk_control_get_properties_cb (GObject *source_object,
1587 GAsyncResult *res,
1588 gpointer user_data)
1589 {
1590 g_autoptr(GTask) task = G_TASK (user_data);
1591 g_autoptr(GError) error = NULL;
1592 g_autoptr(GDBusProxy) proxy = NULL;
1593 PkControl *control = g_task_get_source_object (task);
1594
1595 proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
1596 if (proxy == NULL) {
1597 g_task_return_error (task, g_steal_pointer (&error));
1598 return;
1599 }
1600
1601 pk_control_proxy_connect (control, proxy);
1602
1603 /* we're done */
1604 g_task_return_boolean (task, TRUE);
1605 }
1606
1607 /**
1608 * pk_control_get_properties_async:
1609 * @control: a valid #PkControl instance
1610 * @cancellable: a #GCancellable or %NULL
1611 * @callback: the function to run on completion
1612 * @user_data: the data to pass to @callback
1613 *
1614 * Gets global properties from the daemon.
1615 *
1616 * Since: 0.5.2
1617 **/
1618 void
1619 pk_control_get_properties_async (PkControl *control,
1620 GCancellable *cancellable,
1621 GAsyncReadyCallback callback,
1622 gpointer user_data)
1623 {
1624 PkControlPrivate *priv = GET_PRIVATE(control);
1625 g_autoptr(GTask) task = NULL;
1626 g_autoptr(GError) error = NULL;
1627
1628 g_return_if_fail (PK_IS_CONTROL (control));
1629 g_return_if_fail (callback != NULL);
1630 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
1631
1632 /* check not already cancelled */
1633 if (cancellable != NULL &&
1634 g_cancellable_set_error_if_cancelled (cancellable, &error)) {
1635 g_task_report_error (control, callback, user_data,
1636 pk_control_get_properties_async,
1637 g_steal_pointer (&error));
1638 return;
1639 }
1640
1641 task = g_task_new (control, cancellable, callback, user_data);
1642 g_task_set_source_tag (task, pk_control_get_properties_async);
1643
1644 /* already done */
1645 if (priv->proxy != NULL) {
1646 g_task_return_boolean (task, TRUE);
1647 return;
1648 }
1649
1650 /* get a connection to the main interface */
1651 g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
1652 G_DBUS_PROXY_FLAGS_NONE,
1653 NULL,
1654 PK_DBUS_SERVICE,
1655 PK_DBUS_PATH,
1656 PK_DBUS_INTERFACE,
1657 priv->cancellable,
1658 pk_control_get_properties_cb,
1659 g_steal_pointer (&task));
1660 }
1661
1662 /**
1663 * pk_control_get_properties_finish:
1664 * @control: a valid #PkControl instance
1665 * @res: the #GAsyncResult
1666 * @error: A #GError or %NULL
1667 *
1668 * Gets the result from the asynchronous function.
1669 *
1670 * Returns: %TRUE if we set the proxy successfully
1671 *
1672 * Since: 0.5.2
1673 **/
1674 gboolean
1675 pk_control_get_properties_finish (PkControl *control, GAsyncResult *res, GError **error)
1676 {
1677 g_return_val_if_fail (PK_IS_CONTROL (control), FALSE);
1678 g_return_val_if_fail (g_task_is_valid (res, control), FALSE);
1679 g_return_val_if_fail (g_async_result_is_tagged (res, pk_control_get_properties_async), FALSE);
1680 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1681
1682 return g_task_propagate_boolean (G_TASK (res), error);
1683 }
1684
1685 /**********************************************************************/
1686
1687 /*
1688 * pk_control_get_property:
1689 **/
1690 static void
1691 pk_control_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
1692 {
1693 PkControl *control = PK_CONTROL (object);
1694 PkControlPrivate *priv = GET_PRIVATE(control);
1695
1696 switch (prop_id) {
1697 case PROP_VERSION_MAJOR:
1698 g_value_set_uint (value, priv->version_major);
1699 break;
1700 case PROP_VERSION_MINOR:
1701 g_value_set_uint (value, priv->version_minor);
1702 break;
1703 case PROP_VERSION_MICRO:
1704 g_value_set_uint (value, priv->version_micro);
1705 break;
1706 case PROP_BACKEND_NAME:
1707 g_value_set_string (value, priv->backend_name);
1708 break;
1709 case PROP_BACKEND_DESCRIPTION:
1710 g_value_set_string (value, priv->backend_description);
1711 break;
1712 case PROP_BACKEND_AUTHOR:
1713 g_value_set_string (value, priv->backend_author);
1714 break;
1715 case PROP_ROLES:
1716 g_value_set_uint64 (value, priv->roles);
1717 break;
1718 case PROP_GROUPS:
1719 g_value_set_uint64 (value, priv->groups);
1720 break;
1721 case PROP_FILTERS:
1722 g_value_set_uint64 (value, priv->filters);
1723 break;
1724 case PROP_PROVIDES:
1725 g_value_set_uint64 (value, priv->provides);
1726 break;
1727 case PROP_MIME_TYPES:
1728 g_value_set_boxed (value, priv->mime_types);
1729 break;
1730 case PROP_LOCKED:
1731 g_value_set_boolean (value, priv->locked);
1732 break;
1733 case PROP_NETWORK_STATE:
1734 g_value_set_enum (value, priv->network_state);
1735 break;
1736 case PROP_DISTRO_ID:
1737 g_value_set_string (value, priv->distro_id);
1738 break;
1739 case PROP_CONNECTED:
1740 g_value_set_boolean (value, priv->connected);
1741 break;
1742 default:
1743 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1744 break;
1745 }
1746 }
1747
1748 /*
1749 * pk_control_set_property:
1750 **/
1751 static void
1752 pk_control_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
1753 {
1754 switch (prop_id) {
1755 default:
1756 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1757 break;
1758 }
1759 }
1760
1761 /*
1762 * pk_control_class_init:
1763 **/
1764 static void
1765 pk_control_class_init (PkControlClass *klass)
1766 {
1767 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1768 object_class->get_property = pk_control_get_property;
1769 object_class->set_property = pk_control_set_property;
1770 object_class->finalize = pk_control_finalize;
1771
1772 /**
1773 * PkControl:version-major:
1774 *
1775 * Since: 0.5.2
1776 */
1777 obj_properties[PROP_VERSION_MAJOR] =
1778 g_param_spec_uint ("version-major", NULL, NULL,
1779 0, G_MAXUINT, 0,
1780 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1781
1782 /**
1783 * PkControl:version-minor:
1784 *
1785 * Since: 0.5.2
1786 */
1787 obj_properties[PROP_VERSION_MINOR] =
1788 g_param_spec_uint ("version-minor", NULL, NULL,
1789 0, G_MAXUINT, 0,
1790 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1791
1792 /**
1793 * PkControl:version-micro:
1794 *
1795 * Since: 0.5.2
1796 */
1797 obj_properties[PROP_VERSION_MICRO] =
1798 g_param_spec_uint ("version-micro", NULL, NULL,
1799 0, G_MAXUINT, 0,
1800 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1801
1802 /**
1803 * PkControl:backend-name:
1804 *
1805 * Since: 0.5.2
1806 */
1807 obj_properties[PROP_BACKEND_NAME] =
1808 g_param_spec_string ("backend-name", NULL, NULL,
1809 NULL,
1810 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1811
1812 /**
1813 * PkControl:backend-description:
1814 *
1815 * Since: 0.5.2
1816 */
1817 obj_properties[PROP_BACKEND_DESCRIPTION] =
1818 g_param_spec_string ("backend-description", NULL, NULL,
1819 NULL,
1820 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1821
1822 /**
1823 * PkControl:backend-author:
1824 *
1825 * Since: 0.5.2
1826 */
1827 obj_properties[PROP_BACKEND_AUTHOR] =
1828 g_param_spec_string ("backend-author", NULL, NULL,
1829 NULL,
1830 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1831
1832 /**
1833 * PkControl:roles:
1834 *
1835 * Since: 0.5.2
1836 */
1837 obj_properties[PROP_ROLES] =
1838 g_param_spec_uint64 ("roles", NULL, NULL,
1839 0, G_MAXUINT64, 0,
1840 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1841
1842 /**
1843 * PkControl:groups:
1844 *
1845 * Since: 0.5.2
1846 */
1847 obj_properties[PROP_GROUPS] =
1848 g_param_spec_uint64 ("groups", NULL, NULL,
1849 0, G_MAXUINT64, 0,
1850 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1851
1852 /**
1853 * PkControl:filters:
1854 *
1855 * Since: 0.5.2
1856 */
1857 obj_properties[PROP_FILTERS] =
1858 g_param_spec_uint64 ("filters", NULL, NULL,
1859 0, G_MAXUINT64, 0,
1860 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1861
1862 /**
1863 * PkControl:provides:
1864 *
1865 * Since: 0.8.8
1866 */
1867 obj_properties[PROP_PROVIDES] =
1868 g_param_spec_uint64 ("provides", NULL, NULL,
1869 0, G_MAXUINT64, 0,
1870 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1871
1872 /**
1873 * PkControl:mime-types:
1874 *
1875 * Since: 0.8.1
1876 */
1877 obj_properties[PROP_MIME_TYPES] =
1878 g_param_spec_boxed ("mime-types", NULL, NULL,
1879 G_TYPE_STRV,
1880 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1881
1882 /**
1883 * PkControl:locked:
1884 *
1885 * Since: 0.5.3
1886 */
1887 obj_properties[PROP_LOCKED] =
1888 g_param_spec_boolean ("locked", NULL, NULL,
1889 FALSE,
1890 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1891
1892 /**
1893 * PkControl:network-state:
1894 *
1895 * Since: 0.5.3
1896 */
1897 obj_properties[PROP_NETWORK_STATE] =
1898 g_param_spec_enum ("network-state", NULL, NULL,
1899 PK_TYPE_NETWORK_ENUM, PK_NETWORK_ENUM_LAST,
1900 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1901
1902 /**
1903 * PkControl:distro-id:
1904 *
1905 * Since: 0.5.5
1906 */
1907 obj_properties[PROP_DISTRO_ID] =
1908 g_param_spec_string ("distro-id", NULL, NULL,
1909 NULL,
1910 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1911
1912 /**
1913 * PkControl:connected:
1914 *
1915 * Since: 0.5.3
1916 */
1917 obj_properties[PROP_CONNECTED] =
1918 g_param_spec_boolean ("connected", NULL, NULL,
1919 FALSE,
1920 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
1921
1922 g_object_class_install_properties (object_class, PROP_LAST, obj_properties);
1923
1924 /**
1925 * PkControl::installed-changed:
1926 * @control: the #PkControl instance that emitted the signal
1927 *
1928 * The ::installed-changed signal is emitted when the list of installed apps may have
1929 * changed and the control program may have to update some UI.
1930 *
1931 * Since: 1.2.9
1932 **/
1933 signals[SIGNAL_INSTALLED_CHANGED] =
1934 g_signal_new ("installed-changed",
1935 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1936 G_STRUCT_OFFSET (PkControlClass, installed_changed),
1937 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1938 G_TYPE_NONE, 0);
1939 /**
1940 * PkControl::updates-changed:
1941 * @control: the #PkControl instance that emitted the signal
1942 *
1943 * The ::updates-changed signal is emitted when the update list may have
1944 * changed and the control program may have to update some UI.
1945 **/
1946 signals[SIGNAL_UPDATES_CHANGED] =
1947 g_signal_new ("updates-changed",
1948 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1949 G_STRUCT_OFFSET (PkControlClass, updates_changed),
1950 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1951 G_TYPE_NONE, 0);
1952 /**
1953 * PkControl::repo-list-changed:
1954 * @control: the #PkControl instance that emitted the signal
1955 *
1956 * The ::repo-list-changed signal is emitted when the repo list may have
1957 * changed and the control program may have to update some UI.
1958 **/
1959 signals[SIGNAL_REPO_LIST_CHANGED] =
1960 g_signal_new ("repo-list-changed",
1961 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1962 G_STRUCT_OFFSET (PkControlClass, repo_list_changed),
1963 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1964 G_TYPE_NONE, 0);
1965 /**
1966 * PkControl::restart-schedule:
1967 * @control: the #PkControl instance that emitted the signal
1968 *
1969 * The ::restart_schedule signal is emitted when the packagekitd service
1970 * has been restarted because it has been upgraded.
1971 * Client programs should reload themselves when it is convenient to
1972 * do so, as old client tools may not be compatible with the new daemon.
1973 **/
1974 signals[SIGNAL_RESTART_SCHEDULE] =
1975 g_signal_new ("restart-schedule",
1976 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1977 G_STRUCT_OFFSET (PkControlClass, restart_schedule),
1978 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1979 G_TYPE_NONE, 0);
1980 /**
1981 * PkControl::transaction-list-changed:
1982 * @control: the #PkControl instance that emitted the signal
1983 * @transaction_ids: an #GStrv array of transaction ID's
1984 *
1985 * The ::transaction-list-changed signal is emitted when the list
1986 * of transactions handled by the daemon is changed.
1987 **/
1988 signals[SIGNAL_TRANSACTION_LIST_CHANGED] =
1989 g_signal_new ("transaction-list-changed",
1990 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1991 G_STRUCT_OFFSET (PkControlClass, transaction_list_changed),
1992 NULL, NULL, g_cclosure_marshal_VOID__BOXED,
1993 G_TYPE_NONE, 1, G_TYPE_STRV);
1994 }
1995
1996 /*
1997 * pk_control_name_appeared_cb:
1998 **/
1999 static void
2000 pk_control_name_appeared_cb (GDBusConnection *connection,
2001 const gchar *name,
2002 const gchar *name_owner,
2003 gpointer user_data)
2004 {
2005 PkControl *control = PK_CONTROL (user_data);
2006 PkControlPrivate *priv = GET_PRIVATE(control);
2007 priv->connected = TRUE;
2008 g_debug ("notify::connected");
2009 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_CONNECTED]);
2010 }
2011
2012 /*
2013 * pk_control_proxy_destroy:
2014 **/
2015 static void
2016 pk_control_proxy_destroy (PkControl *control)
2017 {
2018 PkControlPrivate *priv = GET_PRIVATE(control);
2019
2020 if (priv->proxy == NULL)
2021 return;
2022
2023 g_signal_handlers_disconnect_by_func (priv->proxy,
2024 G_CALLBACK (pk_control_properties_changed_cb),
2025 control);
2026 g_signal_handlers_disconnect_by_func (priv->proxy,
2027 G_CALLBACK (pk_control_signal_cb),
2028 control);
2029 g_clear_object (&priv->proxy);
2030 }
2031
2032 /*
2033 * pk_control_name_vanished_cb:
2034 **/
2035 static void
2036 pk_control_name_vanished_cb (GDBusConnection *connection,
2037 const gchar *name,
2038 gpointer user_data)
2039 {
2040 PkControl *control = PK_CONTROL (user_data);
2041 PkControlPrivate *priv = GET_PRIVATE(control);
2042
2043 priv->connected = FALSE;
2044 g_debug ("notify::connected");
2045 g_object_notify_by_pspec (G_OBJECT(control), obj_properties[PROP_CONNECTED]);
2046
2047 /* destroy the proxy, as even though it's "well known" we get a
2048 * GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown if we try to
2049 * use this after the server has restarted */
2050 pk_control_proxy_destroy (control);
2051 }
2052
2053 /*
2054 * pk_control_init:
2055 * @control: This class instance
2056 **/
2057 static void
2058 pk_control_init (PkControl *control)
2059 {
2060 PkControlPrivate *priv = GET_PRIVATE(control);
2061 priv->network_state = PK_NETWORK_ENUM_UNKNOWN;
2062 priv->version_major = G_MAXUINT;
2063 priv->version_minor = G_MAXUINT;
2064 priv->version_micro = G_MAXUINT;
2065 priv->cancellable = g_cancellable_new ();
2066 priv->watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
2067 PK_DBUS_SERVICE,
2068 G_BUS_NAME_WATCHER_FLAGS_NONE,
2069 pk_control_name_appeared_cb,
2070 pk_control_name_vanished_cb,
2071 control,
2072 NULL);
2073 control->priv = priv;
2074 }
2075
2076 /*
2077 * pk_control_finalize:
2078 * @object: The object to finalize
2079 **/
2080 static void
2081 pk_control_finalize (GObject *object)
2082 {
2083 PkControl *control = PK_CONTROL (object);
2084 PkControlPrivate *priv = GET_PRIVATE(control);
2085
2086 /* ensure we cancel any in-flight D-Bus calls */
2087 g_cancellable_cancel (priv->cancellable);
2088 g_bus_unwatch_name (priv->watch_id);
2089
2090 /* disconnect proxy and destroy it */
2091 pk_control_proxy_destroy (control);
2092
2093 g_clear_pointer (&priv->backend_name, g_free);
2094 g_clear_pointer (&priv->backend_description, g_free);
2095 g_clear_pointer (&priv->backend_author, g_free);
2096 g_clear_pointer (&priv->mime_types, g_strfreev);
2097 g_clear_pointer (&priv->distro_id, g_free);
2098 g_clear_object (&priv->cancellable);
2099
2100 G_OBJECT_CLASS (pk_control_parent_class)->finalize (object);
2101 }
2102
2103 /**
2104 * pk_control_new:
2105 *
2106 * Returns: a new #PkControl object.
2107 *
2108 * Since: 0.5.2
2109 **/
2110 PkControl *
2111 pk_control_new (void)
2112 {
2113 if (pk_control_object != NULL) {
2114 g_object_ref (pk_control_object);
2115 } else {
2116 pk_control_object = g_object_new (PK_TYPE_CONTROL, NULL);
2117 g_object_add_weak_pointer (pk_control_object, &pk_control_object);
2118 }
2119 return PK_CONTROL (pk_control_object);
2120 }