projects/package-update-indicator

view pui-backend.c @ 32:b9c65915cc54

Reduce delay before checking for updates after an "updates-changed" signal
author Guido Berhoerster <guido+pui@berhoerster.name>
date Thu Aug 29 17:41:06 2019 +0200 (2019-08-29)
parents 9905d4ae351c
children 4a859595eabd
line source
1 /*
2 * Copyright (C) 2018 Guido Berhoerster <guido+pui@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <packagekit-glib2/packagekit.h>
27 #include <polkit/polkit.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <upower.h>
32 #include <utime.h>
34 #include "pui-common.h"
35 #include "pui-backend.h"
36 #include "pui-get-updates.h"
37 #include "pui-types.h"
39 #define LOW_BATTERY_THRESHOLD 10.0 /* % */
40 #define UPDATES_CHANGED_UNBLOCK_DELAY 4 /* s */
42 struct _PuiBackend {
43 GObject parent_instance;
44 PkControl *pk_control;
45 GCancellable *cancellable;
46 PkClient *pk_client;
47 PkTransactionList *transaction_list;
48 UpClient *up_client;
49 UpDevice *up_device;
50 gchar *proxy_http;
51 gchar *proxy_https;
52 gchar *proxy_ftp;
53 gchar *proxy_socks;
54 gchar *no_proxy;
55 gchar *pac;
56 gint64 last_check;
57 PkNetworkEnum network_state;
58 gboolean inhibited;
59 gboolean is_battery_low;
60 guint check_id;
61 guint unblock_updates_changed_id;
62 guint refresh_interval;
63 gboolean use_mobile_connection;
64 guint important_updates;
65 guint normal_updates;
66 PuiRestart restart_type;
67 };
69 static void pui_backend_async_initable_iface_init(gpointer, gpointer);
71 G_DEFINE_TYPE_WITH_CODE(PuiBackend, pui_backend, G_TYPE_OBJECT,
72 G_IMPLEMENT_INTERFACE(G_TYPE_ASYNC_INITABLE,
73 pui_backend_async_initable_iface_init))
75 enum {
76 STATE_CHANGED,
77 RESTART_REQUIRED,
78 SIGNAL_LAST
79 };
81 enum {
82 PROP_0,
83 PROP_IMPORTANT_UPDATES,
84 PROP_NORMAL_UPDATES,
85 PROP_RESTART_TYPE,
86 PROP_REFRESH_INTERVAL,
87 PROP_USE_MOBILE_CONNECTION,
88 PROP_LAST
89 };
91 static guint signals[SIGNAL_LAST] = { 0 };
92 static GParamSpec *properties[PROP_LAST] = { NULL };
94 static gboolean periodic_check(gpointer);
95 static void on_updates_changed(PkControl *, gpointer);
97 GQuark
98 pui_backend_error_quark(void)
99 {
100 return (g_quark_from_static_string("pui-backend-error-quark"));
101 }
103 static void
104 process_pk_package(gpointer data, gpointer user_data)
105 {
106 PkPackage *package = data;
107 PuiBackend *self = user_data;
108 PkInfoEnum type_info = pk_package_get_info(package);
110 switch (type_info) {
111 case PK_INFO_ENUM_LOW: /* FALLTHROUGH */
112 case PK_INFO_ENUM_ENHANCEMENT: /* FALLTHROUGH */
113 case PK_INFO_ENUM_NORMAL:
114 self->normal_updates++;
115 break;
116 case PK_INFO_ENUM_BUGFIX: /* FALLTHROUGH */
117 case PK_INFO_ENUM_IMPORTANT: /* FALLTHROUGH */
118 case PK_INFO_ENUM_SECURITY:
119 self->important_updates++;
120 break;
121 default:
122 break;
123 }
124 }
126 static gboolean
127 unblock_updates_changed(gpointer user_data)
128 {
129 PuiBackend *self = user_data;
131 g_signal_handlers_unblock_by_func(self->pk_control, on_updates_changed,
132 self);
133 self->unblock_updates_changed_id = 0;
135 return (G_SOURCE_REMOVE);
136 }
138 static void
139 on_get_updates_finished(GObject *source_object, GAsyncResult *async_result,
140 gpointer user_data)
141 {
142 PuiBackend *self = user_data;
143 GPtrArray *package_list = NULL;
144 GError *error = NULL;
145 guint prev_normal_updates = self->normal_updates;
146 guint prev_important_updates = self->important_updates;
148 package_list = pui_get_updates_finish(async_result, &error);
149 if (package_list == NULL) {
150 if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) ||
151 g_error_matches(error, PUI_GET_UPDATES_ERROR,
152 PUI_GET_UPDATES_ERROR_CANCELLED)) {
153 /* cancelled */
154 g_debug("cancelled checking for updates");
155 } else {
156 g_warning("failed to check for updates: %s",
157 error->message);
158 }
159 g_error_free(error);
160 goto out;
161 }
163 self->normal_updates = 0;
164 self->important_updates = 0;
165 g_ptr_array_foreach(package_list, process_pk_package, self);
166 g_debug("normal updates: %u, important updates: %u",
167 self->normal_updates, self->important_updates);
168 if (self->normal_updates != prev_normal_updates) {
169 g_object_notify_by_pspec(G_OBJECT(self),
170 properties[PROP_NORMAL_UPDATES]);
171 }
172 if (self->important_updates != prev_important_updates) {
173 g_object_notify_by_pspec(G_OBJECT(self),
174 properties[PROP_IMPORTANT_UPDATES]);
175 }
176 if ((self->normal_updates != prev_normal_updates) ||
177 (self->important_updates != prev_important_updates)) {
178 g_debug("emitting signal state-changed");
179 g_signal_emit(self, signals[STATE_CHANGED], 0);
180 }
182 /* last successful check */
183 self->last_check = g_get_monotonic_time();
185 out:
186 g_clear_object(&self->cancellable);
188 /* reschedule periodic check */
189 if (!self->inhibited) {
190 self->check_id =
191 g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
192 periodic_check, self);
193 }
195 /* handle get-updates signals again after a short delay */
196 self->unblock_updates_changed_id =
197 g_timeout_add_seconds(UPDATES_CHANGED_UNBLOCK_DELAY,
198 unblock_updates_changed, self);
200 if (package_list != NULL) {
201 g_ptr_array_unref(package_list);
202 }
203 }
205 static void
206 run_check(PuiBackend *self, gboolean refresh_cache)
207 {
208 /* block any get-updates signals emitted when refreshing the cache */
209 if (self->unblock_updates_changed_id != 0) {
210 /* still blocked */
211 g_source_remove(self->unblock_updates_changed_id);
212 self->unblock_updates_changed_id = 0;
213 } else {
214 g_signal_handlers_block_by_func(self->pk_control,
215 G_CALLBACK(on_updates_changed), self);
216 }
218 self->cancellable = g_cancellable_new();
219 pui_get_updates_async(self->pk_control,
220 refresh_cache ? self->refresh_interval : G_MAXUINT,
221 self->cancellable, on_get_updates_finished, self);
223 /* next periodic check will be scheduled after completion */
224 self->check_id = 0;
225 }
227 static gboolean
228 irregular_check(gpointer user_data)
229 {
230 PuiBackend *self = user_data;
232 g_debug("running check");
234 run_check(self, FALSE);
236 return (G_SOURCE_REMOVE);
237 }
239 static gboolean
240 periodic_check(gpointer user_data)
241 {
242 PuiBackend *self = user_data;
244 g_debug("running periodic check");
246 run_check(self, TRUE);
248 return (G_SOURCE_REMOVE);
249 }
251 static void
252 check_inhibit(PuiBackend *self)
253 {
254 gboolean inhibited;
255 guint elapsed_time;
256 guint remaining_time;
258 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
259 (!self->use_mobile_connection &&
260 (self->network_state == PK_NETWORK_ENUM_MOBILE)) ||
261 self->is_battery_low);
262 if (self->inhibited == inhibited) {
263 return;
264 }
266 self->inhibited = inhibited;
267 if (inhibited) {
268 /* cancel periodic checks */
269 if (self->check_id != 0) {
270 g_source_remove(self->check_id);
271 }
273 /* cancel running operation */
274 if ((self->cancellable != NULL) &&
275 !g_cancellable_is_cancelled(self->cancellable)) {
276 g_cancellable_cancel(self->cancellable);
277 g_clear_object(&self->cancellable);
278 }
279 } else {
280 /* schedule periodic checks when no longer inhibited */
281 elapsed_time = (g_get_monotonic_time() - self->last_check) /
282 G_USEC_PER_SEC;
283 /*
284 * if more time that the check interval has passed since the
285 * last check, schedule a check after a short delay, otherwise
286 * wait until the interval has passed
287 */
288 remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
289 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
290 PUI_STARTUP_DELAY;
291 self->check_id = g_timeout_add_seconds(remaining_time,
292 periodic_check, self);
293 }
294 }
296 static void
297 pui_backend_set_property(GObject *object, guint property_id,
298 const GValue *value, GParamSpec *pspec)
299 {
300 PuiBackend *self = PUI_BACKEND(object);
302 switch (property_id) {
303 case PROP_REFRESH_INTERVAL:
304 self->refresh_interval = g_value_get_uint(value);
305 g_debug("property \"refresh-interval\" set to %u",
306 self->refresh_interval);
307 break;
308 case PROP_USE_MOBILE_CONNECTION:
309 self->use_mobile_connection = g_value_get_boolean(value);
310 g_debug("property \"use-mobile-connection\" set to %s",
311 self->use_mobile_connection ? "true" : "false");
312 check_inhibit(self);
313 break;
314 default:
315 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
316 break;
317 }
318 }
320 static void
321 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
322 GParamSpec *pspec)
323 {
324 PuiBackend *self = PUI_BACKEND(object);
326 switch (property_id) {
327 case PROP_IMPORTANT_UPDATES:
328 g_value_set_uint(value, self->important_updates);
329 break;
330 case PROP_NORMAL_UPDATES:
331 g_value_set_uint(value, self->normal_updates);
332 break;
333 case PROP_RESTART_TYPE:
334 g_value_set_enum(value, self->restart_type);
335 break;
336 case PROP_REFRESH_INTERVAL:
337 g_value_set_uint(value, self->refresh_interval);
338 break;
339 case PROP_USE_MOBILE_CONNECTION:
340 g_value_set_boolean(value, self->use_mobile_connection);
341 break;
342 default:
343 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
344 break;
345 }
346 }
348 static void
349 pui_backend_dispose(GObject *object)
350 {
351 PuiBackend *self = PUI_BACKEND(object);
353 if (self->check_id != 0) {
354 g_source_remove(self->check_id);
355 self->check_id = 0;
356 }
358 if (self->unblock_updates_changed_id != 0) {
359 g_source_remove(self->unblock_updates_changed_id);
360 self->unblock_updates_changed_id = 0;
361 }
363 if (self->transaction_list != NULL) {
364 g_clear_object(&self->transaction_list);
365 }
367 if (self->pk_client != NULL) {
368 g_clear_object(&self->pk_client);
369 }
371 if (self->cancellable != NULL) {
372 g_cancellable_cancel(self->cancellable);
373 g_clear_object(&self->cancellable);
374 }
376 if (self->pk_control != NULL) {
377 g_clear_object(&self->pk_control);
378 }
380 if (self->up_device != NULL) {
381 g_clear_object(&self->up_device);
382 }
384 if (self->up_client != NULL) {
385 g_clear_object(&self->up_client);
386 }
388 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
389 }
391 static void
392 pui_backend_finalize(GObject *object)
393 {
394 PuiBackend *self = PUI_BACKEND(object);
396 g_free(self->proxy_http);
397 g_free(self->proxy_https);
398 g_free(self->proxy_ftp);
399 g_free(self->proxy_socks);
400 g_free(self->no_proxy);
401 g_free(self->pac);
403 G_OBJECT_CLASS(pui_backend_parent_class)->finalize(object);
404 }
406 static void
407 pui_backend_class_init(PuiBackendClass *klass)
408 {
409 GObjectClass *object_class = G_OBJECT_CLASS(klass);
411 object_class->set_property = pui_backend_set_property;
412 object_class->get_property = pui_backend_get_property;
413 object_class->dispose = pui_backend_dispose;
414 object_class->finalize = pui_backend_finalize;
416 properties[PROP_IMPORTANT_UPDATES] =
417 g_param_spec_uint("important-updates", "Important updates",
418 "Number of available important updates", 0, G_MAXUINT, 0,
419 G_PARAM_READABLE);
421 properties[PROP_NORMAL_UPDATES] =
422 g_param_spec_uint("normal-updates", "Normal updates",
423 "Number of available normal updates", 0, G_MAXUINT, 0,
424 G_PARAM_READABLE);
426 properties[PROP_RESTART_TYPE] =
427 g_param_spec_enum("restart-type", "Type of restart required",
428 "The Type of restart required", PUI_TYPE_RESTART, PUI_RESTART_NONE,
429 G_PARAM_READABLE);
431 properties[PROP_REFRESH_INTERVAL] =
432 g_param_spec_uint("refresh-interval", "Refresh interval",
433 "Interval in seconds for refreshing the package cache", 0,
434 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
436 properties[PROP_USE_MOBILE_CONNECTION] =
437 g_param_spec_boolean("use-mobile-connection",
438 "Whether to use a mobile connection", "Whether to use a mobile "
439 "connection for refreshing the package cache", FALSE,
440 G_PARAM_READWRITE);
442 g_object_class_install_properties(object_class, PROP_LAST, properties);
444 signals[STATE_CHANGED] = g_signal_new("state-changed",
445 G_TYPE_FROM_CLASS(object_class),
446 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
447 NULL, NULL, NULL, G_TYPE_NONE, 0);
449 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
450 G_TYPE_FROM_CLASS(object_class),
451 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
452 NULL, NULL, NULL, G_TYPE_NONE, 0);
453 }
455 static void
456 pui_backend_init(PuiBackend *self)
457 {
458 self->pk_control = pk_control_new();
460 self->pk_client = pk_client_new();
462 self->inhibited = TRUE;
464 self->up_client = up_client_new();
465 if (self->up_client) {
466 self->up_device = up_client_get_display_device(self->up_client);
467 }
468 }
470 static void
471 on_get_properties_finished(GObject *object, GAsyncResult *result,
472 gpointer user_data)
473 {
474 PkControl *control = PK_CONTROL(object);
475 PuiBackend *self;
476 GTask *task = user_data;
477 GError *error = NULL;
478 gchar *backend_name = NULL;
479 PkBitfield roles = 0;
480 gchar *roles_str = NULL;
482 self = g_task_get_task_data(task);
484 if (!pk_control_get_properties_finish(control, result, &error)) {
485 g_task_return_error(task, error);
486 goto out;
487 }
489 /* check whether the backend supports GetUpdates */
490 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
491 "network-state", &self->network_state, NULL);
492 g_debug("backend: %s", backend_name);
493 roles_str = pk_role_bitfield_to_string(roles);
494 g_debug("roles: %s", roles_str);
495 g_debug("network-state: %s",
496 pk_network_enum_to_string(self->network_state));
497 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
498 error = g_error_new(PUI_BACKEND_ERROR,
499 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
500 "Getting updates is not implemented in the %s PackageKit "
501 "backend", backend_name);
502 g_task_return_error(task, error);
503 goto out;
504 }
506 g_task_return_boolean(task, TRUE);
507 out:
508 g_free(roles_str);
509 g_free(backend_name);
510 g_object_unref(task);
511 }
513 static void
514 on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
515 gpointer user_data)
516 {
517 PuiBackend *self = user_data;
518 UpDeviceKind kind;
519 gdouble percentage;
521 g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
522 if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
523 return;
524 }
525 g_debug("charge percentage changed: %.0f%%\n", percentage);
526 if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
527 (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
528 self->is_battery_low = !self->is_battery_low;
529 check_inhibit(self);
530 }
531 }
533 static void
534 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
535 gpointer user_data)
536 {
537 PuiBackend *self = user_data;
539 g_object_get(pk_control, "network-state", &self->network_state, NULL);
540 g_debug("network state changed: %s",
541 pk_network_enum_to_string(self->network_state));
542 check_inhibit(self);
543 }
545 static void
546 on_updates_changed(PkControl *control, gpointer user_data)
547 {
548 PuiBackend *self = user_data;
550 g_debug("package metatdata cache invalidated");
552 /*
553 * schedule a check after a short delay so that a rapid succession of
554 * signals is coalesced
555 */
556 if (!self->inhibited) {
557 if (self->check_id != 0) {
558 g_source_remove(self->check_id);
559 }
560 self->check_id =
561 g_timeout_add_seconds(PUI_UPDATES_CHANGED_DELAY,
562 irregular_check, self);
563 }
564 }
566 static void
567 on_restart_schedule(PkControl *control, gpointer user_data)
568 {
569 PuiBackend *self = user_data;
571 /*
572 * do not restart package-update-indicator if a session or system
573 * restart is required since that iformation would be lost across the
574 * restart, rather keep running and risk errors when interacting with
575 * a newer version of the PackageKit daemon
576 */
577 if (self->restart_type > PUI_RESTART_NONE) {
578 return;
579 }
581 g_debug("emitting signal restart-required");
582 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
583 }
585 static void
586 on_transaction_adopt_finish(GObject *source_object, GAsyncResult *result,
587 gpointer user_data)
588 {
589 PuiBackend *self = user_data;
590 PkClient *pk_client = PK_CLIENT(source_object);
591 PkResults *results;
592 GError *error = NULL;
593 PkRestartEnum restart;
595 results = pk_client_generic_finish(pk_client, result, &error);
596 if (results == NULL) {
597 g_warning("failed to get transaction results: %s",
598 error->message);
599 g_error_free(error);
600 goto out;
601 }
603 /* check if transaction requires a restart */
604 restart = pk_results_get_require_restart_worst(results);
605 switch (restart) {
606 case PK_RESTART_ENUM_SESSION: /* FALLTHROUGH */
607 case PK_RESTART_ENUM_SECURITY_SESSION:
608 if (self->restart_type < PUI_RESTART_SESSION) {
609 self->restart_type = PUI_RESTART_SESSION;
610 g_object_notify_by_pspec(G_OBJECT(self),
611 properties[PROP_RESTART_TYPE]);
612 g_signal_emit(self, signals[STATE_CHANGED], 0);
613 }
614 break;
615 case PK_RESTART_ENUM_SYSTEM: /* FALLTHROUGH */
616 case PK_RESTART_ENUM_SECURITY_SYSTEM:
617 if (self->restart_type < PUI_RESTART_SYSTEM) {
618 self->restart_type = PUI_RESTART_SYSTEM;
619 g_object_notify_by_pspec(G_OBJECT(self),
620 properties[PROP_RESTART_TYPE]);
621 g_signal_emit(self, signals[STATE_CHANGED], 0);
622 }
623 break;
624 default:
625 /* do nothing */
626 break;
627 }
629 g_debug("transaction finished, required restart: %s",
630 pk_restart_enum_to_string(restart));
632 out:
633 if (results != NULL) {
634 g_object_unref(results);
635 }
636 }
638 static void
639 on_transaction_list_added(PkTransactionList *transaction_list,
640 const gchar *transaction_id, gpointer user_data)
641 {
642 PuiBackend *self = user_data;
644 /* adopt transaction in order to monitor it for restart requirements */
645 pk_client_adopt_async(self->pk_client, transaction_id, NULL, NULL,
646 NULL, on_transaction_adopt_finish, user_data);
647 }
649 static void
650 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
651 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
652 {
653 PuiBackend *self = PUI_BACKEND(initable);
654 GTask *task;
656 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
657 g_task_set_priority(task, io_priority);
658 g_task_set_task_data(task, g_object_ref(self),
659 (GDestroyNotify)g_object_unref);
661 pk_control_get_properties_async(self->pk_control, cancellable,
662 on_get_properties_finished, task);
663 }
665 static gboolean
666 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
667 GError **errorp)
668 {
669 PuiBackend *self = PUI_BACKEND(initable);
670 GTask *task = G_TASK(result);
671 UpDeviceKind kind;
672 gdouble percentage;
674 if (!g_task_propagate_boolean(task, errorp)) {
675 return (FALSE);
676 }
678 if (self->up_device != NULL) {
679 /* get the kind of device and charge percentage */
680 g_object_get(self->up_device, "kind", &kind, "percentage",
681 &percentage, NULL);
682 if ((kind == UP_DEVICE_KIND_BATTERY) ||
683 (kind == UP_DEVICE_KIND_UPS)) {
684 self->is_battery_low =
685 (percentage < LOW_BATTERY_THRESHOLD);
686 }
688 /* get notification if the charge percentage changes */
689 g_signal_connect(self->up_device, "notify::percentage",
690 G_CALLBACK(on_notify_device_charge_percentage), self);
691 }
693 /* get notification when the network state changes */
694 g_signal_connect(self->pk_control, "notify::network-state",
695 G_CALLBACK(on_notify_network_state), self);
696 /* get notifications when the package metatdata cache is invalidated */
697 g_signal_connect(self->pk_control, "updates-changed",
698 G_CALLBACK(on_updates_changed), self);
699 /* get notifications when an application restart is required */
700 g_signal_connect(self->pk_control, "restart-schedule",
701 G_CALLBACK(on_restart_schedule), self);
702 /* get notifications when a transactions are added */
703 self->transaction_list = pk_transaction_list_new();
704 g_signal_connect(self->transaction_list, "added",
705 G_CALLBACK(on_transaction_list_added), self);
707 check_inhibit(self);
709 return (TRUE);
710 }
712 static void
713 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
714 {
715 GAsyncInitableIface *iface = g_iface;
717 iface->init_async = pui_backend_init_async;
718 iface->init_finish = pui_backend_init_finish;
719 }
721 void
722 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
723 gpointer user_data)
724 {
725 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
726 cancellable, callback, user_data, NULL);
727 }
729 PuiBackend *
730 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
731 {
732 GObject *object;
733 GObject *source_object;
735 source_object = g_async_result_get_source_object(result);
736 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
737 result, errorp);
738 g_object_unref(source_object);
740 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
741 }
743 static void
744 on_set_proxy_finished(GObject *source_object, GAsyncResult *result,
745 gpointer user_data)
746 {
747 PuiBackend *self = user_data;
748 GError *error = NULL;
750 if (!pk_control_set_proxy_finish(self->pk_control, result, &error)) {
751 g_warning("failed to set proxies: %s", error->message);
752 g_error_free(error);
753 }
754 }
756 static void
757 on_polkit_permission_finished(GObject *source_object, GAsyncResult *result,
758 gpointer user_data)
759 {
760 PuiBackend *self = user_data;
761 GPermission *permission;
762 GError *error = NULL;
764 permission = polkit_permission_new_finish(result, &error);
765 if (permission == NULL) {
766 g_warning("failed to create PolKit permission for setting the "
767 "network proxies: %s", error->message);
768 g_error_free(error);
769 return;
770 }
772 if (!g_permission_get_allowed(permission)) {
773 /* setting the proxy requires authentication or is disallowed */
774 g_debug("setting the network proxy is not allowed");
775 return;
776 }
778 g_debug("setting HTTP proxy to \"%s\"", (self->proxy_http != NULL) ?
779 self->proxy_http : "(null)");
780 g_debug("setting HTTPS proxy to \"%s\"", (self->proxy_https != NULL) ?
781 self->proxy_https : "(null)");
782 g_debug("setting FTP proxy to \"%s\"", (self->proxy_ftp != NULL) ?
783 self->proxy_ftp : "(null)");
784 g_debug("setting SOCKS proxy to \"%s\"", (self->proxy_socks != NULL) ?
785 self->proxy_socks : "(null)");
786 g_debug("setting the list of download IPs which should not go through "
787 "a proxy to \"%s\"", (self->no_proxy != NULL) ? self->no_proxy :
788 "(null)");
789 g_debug("setting the PAC string to \"%s\"", (self->pac != NULL) ?
790 self->pac : "(null)");
791 pk_control_set_proxy2_async(self->pk_control, self->proxy_http,
792 self->proxy_https, self->proxy_ftp, self->proxy_socks,
793 self->no_proxy, self->pac, NULL, on_set_proxy_finished, self);
794 }
796 void
797 pui_backend_set_proxy(PuiBackend *self, const gchar *proxy_http,
798 const gchar *proxy_https, const gchar *proxy_ftp, const gchar *proxy_socks,
799 const gchar *no_proxy, const gchar *pac)
800 {
801 g_free(self->proxy_http);
802 self->proxy_http = (proxy_http != NULL) ? g_strdup(proxy_http) : NULL;
803 g_free(self->proxy_https);
804 self->proxy_https = (proxy_https != NULL) ? g_strdup(proxy_https) :
805 NULL;
806 g_free(self->proxy_ftp);
807 self->proxy_ftp = (proxy_ftp != NULL) ? g_strdup(proxy_ftp) : NULL;
808 g_free(self->proxy_socks);
809 self->proxy_socks = (proxy_socks != NULL) ? g_strdup(proxy_socks) :
810 NULL;
811 g_free(self->no_proxy);
812 self->no_proxy = (no_proxy != NULL) ? g_strdup(no_proxy) : NULL;
813 g_free(self->pac);
814 self->pac = (pac != NULL) ? g_strdup(pac) : NULL;
816 polkit_permission_new("org.freedesktop.packagekit."
817 "system-network-proxy-configure", NULL, NULL,
818 on_polkit_permission_finished, self);
819 }