projects/package-update-indicator

view pui-backend.c @ 45:4a859595eabd

Add debug logging when periodic checks are inhibited
author Guido Berhoerster <guido+pui@berhoerster.name>
date Thu Nov 05 11:18:16 2020 +0100 (14 months ago)
parents b9c65915cc54
children 8ed91c5e0116
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 is_offline;
255 gboolean is_disallowed_mobile;
256 gboolean inhibited;
257 guint elapsed_time;
258 guint remaining_time;
260 is_offline = self->network_state == PK_NETWORK_ENUM_OFFLINE;
261 is_disallowed_mobile = !self->use_mobile_connection &&
262 (self->network_state == PK_NETWORK_ENUM_MOBILE);
263 inhibited = is_offline || is_disallowed_mobile || self->is_battery_low;
264 if (self->inhibited == inhibited) {
265 return;
266 }
268 self->inhibited = inhibited;
269 if (inhibited) {
270 /* cancel periodic checks */
271 if (self->check_id != 0) {
272 g_source_remove(self->check_id);
273 }
275 /* cancel running operation */
276 if ((self->cancellable != NULL) &&
277 !g_cancellable_is_cancelled(self->cancellable)) {
278 g_cancellable_cancel(self->cancellable);
279 g_clear_object(&self->cancellable);
280 }
282 if (is_offline) {
283 g_debug("perioidic checks inhibited: network offline");
284 }
285 if (is_disallowed_mobile) {
286 g_debug("perioidic checks inhibited: use of mobile "
287 "connection not allowed");
288 }
289 if (self->is_battery_low) {
290 g_debug("perioidic checks inhibited: low battery");
291 }
292 } else {
293 /* schedule periodic checks when no longer inhibited */
294 elapsed_time = (g_get_monotonic_time() - self->last_check) /
295 G_USEC_PER_SEC;
296 /*
297 * if more time that the check interval has passed since the
298 * last check, schedule a check after a short delay, otherwise
299 * wait until the interval has passed
300 */
301 remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
302 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
303 PUI_STARTUP_DELAY;
304 self->check_id = g_timeout_add_seconds(remaining_time,
305 periodic_check, self);
307 g_debug("perioidic checks no longer inhibited, time since "
308 "last check: %ds, next check in: %ds", elapsed_time,
309 remaining_time);
310 }
311 }
313 static void
314 pui_backend_set_property(GObject *object, guint property_id,
315 const GValue *value, GParamSpec *pspec)
316 {
317 PuiBackend *self = PUI_BACKEND(object);
319 switch (property_id) {
320 case PROP_REFRESH_INTERVAL:
321 self->refresh_interval = g_value_get_uint(value);
322 g_debug("property \"refresh-interval\" set to %u",
323 self->refresh_interval);
324 break;
325 case PROP_USE_MOBILE_CONNECTION:
326 self->use_mobile_connection = g_value_get_boolean(value);
327 g_debug("property \"use-mobile-connection\" set to %s",
328 self->use_mobile_connection ? "true" : "false");
329 check_inhibit(self);
330 break;
331 default:
332 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
333 break;
334 }
335 }
337 static void
338 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
339 GParamSpec *pspec)
340 {
341 PuiBackend *self = PUI_BACKEND(object);
343 switch (property_id) {
344 case PROP_IMPORTANT_UPDATES:
345 g_value_set_uint(value, self->important_updates);
346 break;
347 case PROP_NORMAL_UPDATES:
348 g_value_set_uint(value, self->normal_updates);
349 break;
350 case PROP_RESTART_TYPE:
351 g_value_set_enum(value, self->restart_type);
352 break;
353 case PROP_REFRESH_INTERVAL:
354 g_value_set_uint(value, self->refresh_interval);
355 break;
356 case PROP_USE_MOBILE_CONNECTION:
357 g_value_set_boolean(value, self->use_mobile_connection);
358 break;
359 default:
360 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
361 break;
362 }
363 }
365 static void
366 pui_backend_dispose(GObject *object)
367 {
368 PuiBackend *self = PUI_BACKEND(object);
370 if (self->check_id != 0) {
371 g_source_remove(self->check_id);
372 self->check_id = 0;
373 }
375 if (self->unblock_updates_changed_id != 0) {
376 g_source_remove(self->unblock_updates_changed_id);
377 self->unblock_updates_changed_id = 0;
378 }
380 if (self->transaction_list != NULL) {
381 g_clear_object(&self->transaction_list);
382 }
384 if (self->pk_client != NULL) {
385 g_clear_object(&self->pk_client);
386 }
388 if (self->cancellable != NULL) {
389 g_cancellable_cancel(self->cancellable);
390 g_clear_object(&self->cancellable);
391 }
393 if (self->pk_control != NULL) {
394 g_clear_object(&self->pk_control);
395 }
397 if (self->up_device != NULL) {
398 g_clear_object(&self->up_device);
399 }
401 if (self->up_client != NULL) {
402 g_clear_object(&self->up_client);
403 }
405 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
406 }
408 static void
409 pui_backend_finalize(GObject *object)
410 {
411 PuiBackend *self = PUI_BACKEND(object);
413 g_free(self->proxy_http);
414 g_free(self->proxy_https);
415 g_free(self->proxy_ftp);
416 g_free(self->proxy_socks);
417 g_free(self->no_proxy);
418 g_free(self->pac);
420 G_OBJECT_CLASS(pui_backend_parent_class)->finalize(object);
421 }
423 static void
424 pui_backend_class_init(PuiBackendClass *klass)
425 {
426 GObjectClass *object_class = G_OBJECT_CLASS(klass);
428 object_class->set_property = pui_backend_set_property;
429 object_class->get_property = pui_backend_get_property;
430 object_class->dispose = pui_backend_dispose;
431 object_class->finalize = pui_backend_finalize;
433 properties[PROP_IMPORTANT_UPDATES] =
434 g_param_spec_uint("important-updates", "Important updates",
435 "Number of available important updates", 0, G_MAXUINT, 0,
436 G_PARAM_READABLE);
438 properties[PROP_NORMAL_UPDATES] =
439 g_param_spec_uint("normal-updates", "Normal updates",
440 "Number of available normal updates", 0, G_MAXUINT, 0,
441 G_PARAM_READABLE);
443 properties[PROP_RESTART_TYPE] =
444 g_param_spec_enum("restart-type", "Type of restart required",
445 "The Type of restart required", PUI_TYPE_RESTART, PUI_RESTART_NONE,
446 G_PARAM_READABLE);
448 properties[PROP_REFRESH_INTERVAL] =
449 g_param_spec_uint("refresh-interval", "Refresh interval",
450 "Interval in seconds for refreshing the package cache", 0,
451 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
453 properties[PROP_USE_MOBILE_CONNECTION] =
454 g_param_spec_boolean("use-mobile-connection",
455 "Whether to use a mobile connection", "Whether to use a mobile "
456 "connection for refreshing the package cache", FALSE,
457 G_PARAM_READWRITE);
459 g_object_class_install_properties(object_class, PROP_LAST, properties);
461 signals[STATE_CHANGED] = g_signal_new("state-changed",
462 G_TYPE_FROM_CLASS(object_class),
463 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
464 NULL, NULL, NULL, G_TYPE_NONE, 0);
466 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
467 G_TYPE_FROM_CLASS(object_class),
468 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
469 NULL, NULL, NULL, G_TYPE_NONE, 0);
470 }
472 static void
473 pui_backend_init(PuiBackend *self)
474 {
475 self->pk_control = pk_control_new();
477 self->pk_client = pk_client_new();
479 self->inhibited = TRUE;
481 self->up_client = up_client_new();
482 if (self->up_client) {
483 self->up_device = up_client_get_display_device(self->up_client);
484 }
485 }
487 static void
488 on_get_properties_finished(GObject *object, GAsyncResult *result,
489 gpointer user_data)
490 {
491 PkControl *control = PK_CONTROL(object);
492 PuiBackend *self;
493 GTask *task = user_data;
494 GError *error = NULL;
495 gchar *backend_name = NULL;
496 PkBitfield roles = 0;
497 gchar *roles_str = NULL;
499 self = g_task_get_task_data(task);
501 if (!pk_control_get_properties_finish(control, result, &error)) {
502 g_task_return_error(task, error);
503 goto out;
504 }
506 /* check whether the backend supports GetUpdates */
507 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
508 "network-state", &self->network_state, NULL);
509 g_debug("backend: %s", backend_name);
510 roles_str = pk_role_bitfield_to_string(roles);
511 g_debug("roles: %s", roles_str);
512 g_debug("network-state: %s",
513 pk_network_enum_to_string(self->network_state));
514 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
515 error = g_error_new(PUI_BACKEND_ERROR,
516 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
517 "Getting updates is not implemented in the %s PackageKit "
518 "backend", backend_name);
519 g_task_return_error(task, error);
520 goto out;
521 }
523 g_task_return_boolean(task, TRUE);
524 out:
525 g_free(roles_str);
526 g_free(backend_name);
527 g_object_unref(task);
528 }
530 static void
531 on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
532 gpointer user_data)
533 {
534 PuiBackend *self = user_data;
535 UpDeviceKind kind;
536 gdouble percentage;
538 g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
539 if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
540 return;
541 }
542 g_debug("charge percentage changed: %.0f%%\n", percentage);
543 if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
544 (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
545 self->is_battery_low = !self->is_battery_low;
546 check_inhibit(self);
547 }
548 }
550 static void
551 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
552 gpointer user_data)
553 {
554 PuiBackend *self = user_data;
556 g_object_get(pk_control, "network-state", &self->network_state, NULL);
557 g_debug("network state changed: %s",
558 pk_network_enum_to_string(self->network_state));
559 check_inhibit(self);
560 }
562 static void
563 on_updates_changed(PkControl *control, gpointer user_data)
564 {
565 PuiBackend *self = user_data;
567 g_debug("package metatdata cache invalidated");
569 /*
570 * schedule a check after a short delay so that a rapid succession of
571 * signals is coalesced
572 */
573 if (!self->inhibited) {
574 if (self->check_id != 0) {
575 g_source_remove(self->check_id);
576 }
577 self->check_id =
578 g_timeout_add_seconds(PUI_UPDATES_CHANGED_DELAY,
579 irregular_check, self);
580 }
581 }
583 static void
584 on_restart_schedule(PkControl *control, gpointer user_data)
585 {
586 PuiBackend *self = user_data;
588 /*
589 * do not restart package-update-indicator if a session or system
590 * restart is required since that iformation would be lost across the
591 * restart, rather keep running and risk errors when interacting with
592 * a newer version of the PackageKit daemon
593 */
594 if (self->restart_type > PUI_RESTART_NONE) {
595 return;
596 }
598 g_debug("emitting signal restart-required");
599 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
600 }
602 static void
603 on_transaction_adopt_finish(GObject *source_object, GAsyncResult *result,
604 gpointer user_data)
605 {
606 PuiBackend *self = user_data;
607 PkClient *pk_client = PK_CLIENT(source_object);
608 PkResults *results;
609 GError *error = NULL;
610 PkRestartEnum restart;
612 results = pk_client_generic_finish(pk_client, result, &error);
613 if (results == NULL) {
614 g_warning("failed to get transaction results: %s",
615 error->message);
616 g_error_free(error);
617 goto out;
618 }
620 /* check if transaction requires a restart */
621 restart = pk_results_get_require_restart_worst(results);
622 switch (restart) {
623 case PK_RESTART_ENUM_SESSION: /* FALLTHROUGH */
624 case PK_RESTART_ENUM_SECURITY_SESSION:
625 if (self->restart_type < PUI_RESTART_SESSION) {
626 self->restart_type = PUI_RESTART_SESSION;
627 g_object_notify_by_pspec(G_OBJECT(self),
628 properties[PROP_RESTART_TYPE]);
629 g_signal_emit(self, signals[STATE_CHANGED], 0);
630 }
631 break;
632 case PK_RESTART_ENUM_SYSTEM: /* FALLTHROUGH */
633 case PK_RESTART_ENUM_SECURITY_SYSTEM:
634 if (self->restart_type < PUI_RESTART_SYSTEM) {
635 self->restart_type = PUI_RESTART_SYSTEM;
636 g_object_notify_by_pspec(G_OBJECT(self),
637 properties[PROP_RESTART_TYPE]);
638 g_signal_emit(self, signals[STATE_CHANGED], 0);
639 }
640 break;
641 default:
642 /* do nothing */
643 break;
644 }
646 g_debug("transaction finished, required restart: %s",
647 pk_restart_enum_to_string(restart));
649 out:
650 if (results != NULL) {
651 g_object_unref(results);
652 }
653 }
655 static void
656 on_transaction_list_added(PkTransactionList *transaction_list,
657 const gchar *transaction_id, gpointer user_data)
658 {
659 PuiBackend *self = user_data;
661 /* adopt transaction in order to monitor it for restart requirements */
662 pk_client_adopt_async(self->pk_client, transaction_id, NULL, NULL,
663 NULL, on_transaction_adopt_finish, user_data);
664 }
666 static void
667 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
668 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
669 {
670 PuiBackend *self = PUI_BACKEND(initable);
671 GTask *task;
673 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
674 g_task_set_priority(task, io_priority);
675 g_task_set_task_data(task, g_object_ref(self),
676 (GDestroyNotify)g_object_unref);
678 pk_control_get_properties_async(self->pk_control, cancellable,
679 on_get_properties_finished, task);
680 }
682 static gboolean
683 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
684 GError **errorp)
685 {
686 PuiBackend *self = PUI_BACKEND(initable);
687 GTask *task = G_TASK(result);
688 UpDeviceKind kind;
689 gdouble percentage;
691 if (!g_task_propagate_boolean(task, errorp)) {
692 return (FALSE);
693 }
695 if (self->up_device != NULL) {
696 /* get the kind of device and charge percentage */
697 g_object_get(self->up_device, "kind", &kind, "percentage",
698 &percentage, NULL);
699 if ((kind == UP_DEVICE_KIND_BATTERY) ||
700 (kind == UP_DEVICE_KIND_UPS)) {
701 self->is_battery_low =
702 (percentage < LOW_BATTERY_THRESHOLD);
703 }
705 /* get notification if the charge percentage changes */
706 g_signal_connect(self->up_device, "notify::percentage",
707 G_CALLBACK(on_notify_device_charge_percentage), self);
708 }
710 /* get notification when the network state changes */
711 g_signal_connect(self->pk_control, "notify::network-state",
712 G_CALLBACK(on_notify_network_state), self);
713 /* get notifications when the package metatdata cache is invalidated */
714 g_signal_connect(self->pk_control, "updates-changed",
715 G_CALLBACK(on_updates_changed), self);
716 /* get notifications when an application restart is required */
717 g_signal_connect(self->pk_control, "restart-schedule",
718 G_CALLBACK(on_restart_schedule), self);
719 /* get notifications when a transactions are added */
720 self->transaction_list = pk_transaction_list_new();
721 g_signal_connect(self->transaction_list, "added",
722 G_CALLBACK(on_transaction_list_added), self);
724 check_inhibit(self);
726 return (TRUE);
727 }
729 static void
730 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
731 {
732 GAsyncInitableIface *iface = g_iface;
734 iface->init_async = pui_backend_init_async;
735 iface->init_finish = pui_backend_init_finish;
736 }
738 void
739 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
740 gpointer user_data)
741 {
742 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
743 cancellable, callback, user_data, NULL);
744 }
746 PuiBackend *
747 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
748 {
749 GObject *object;
750 GObject *source_object;
752 source_object = g_async_result_get_source_object(result);
753 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
754 result, errorp);
755 g_object_unref(source_object);
757 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
758 }
760 static void
761 on_set_proxy_finished(GObject *source_object, GAsyncResult *result,
762 gpointer user_data)
763 {
764 PuiBackend *self = user_data;
765 GError *error = NULL;
767 if (!pk_control_set_proxy_finish(self->pk_control, result, &error)) {
768 g_warning("failed to set proxies: %s", error->message);
769 g_error_free(error);
770 }
771 }
773 static void
774 on_polkit_permission_finished(GObject *source_object, GAsyncResult *result,
775 gpointer user_data)
776 {
777 PuiBackend *self = user_data;
778 GPermission *permission;
779 GError *error = NULL;
781 permission = polkit_permission_new_finish(result, &error);
782 if (permission == NULL) {
783 g_warning("failed to create PolKit permission for setting the "
784 "network proxies: %s", error->message);
785 g_error_free(error);
786 return;
787 }
789 if (!g_permission_get_allowed(permission)) {
790 /* setting the proxy requires authentication or is disallowed */
791 g_debug("setting the network proxy is not allowed");
792 return;
793 }
795 g_debug("setting HTTP proxy to \"%s\"", (self->proxy_http != NULL) ?
796 self->proxy_http : "(null)");
797 g_debug("setting HTTPS proxy to \"%s\"", (self->proxy_https != NULL) ?
798 self->proxy_https : "(null)");
799 g_debug("setting FTP proxy to \"%s\"", (self->proxy_ftp != NULL) ?
800 self->proxy_ftp : "(null)");
801 g_debug("setting SOCKS proxy to \"%s\"", (self->proxy_socks != NULL) ?
802 self->proxy_socks : "(null)");
803 g_debug("setting the list of download IPs which should not go through "
804 "a proxy to \"%s\"", (self->no_proxy != NULL) ? self->no_proxy :
805 "(null)");
806 g_debug("setting the PAC string to \"%s\"", (self->pac != NULL) ?
807 self->pac : "(null)");
808 pk_control_set_proxy2_async(self->pk_control, self->proxy_http,
809 self->proxy_https, self->proxy_ftp, self->proxy_socks,
810 self->no_proxy, self->pac, NULL, on_set_proxy_finished, self);
811 }
813 void
814 pui_backend_set_proxy(PuiBackend *self, const gchar *proxy_http,
815 const gchar *proxy_https, const gchar *proxy_ftp, const gchar *proxy_socks,
816 const gchar *no_proxy, const gchar *pac)
817 {
818 g_free(self->proxy_http);
819 self->proxy_http = (proxy_http != NULL) ? g_strdup(proxy_http) : NULL;
820 g_free(self->proxy_https);
821 self->proxy_https = (proxy_https != NULL) ? g_strdup(proxy_https) :
822 NULL;
823 g_free(self->proxy_ftp);
824 self->proxy_ftp = (proxy_ftp != NULL) ? g_strdup(proxy_ftp) : NULL;
825 g_free(self->proxy_socks);
826 self->proxy_socks = (proxy_socks != NULL) ? g_strdup(proxy_socks) :
827 NULL;
828 g_free(self->no_proxy);
829 self->no_proxy = (no_proxy != NULL) ? g_strdup(no_proxy) : NULL;
830 g_free(self->pac);
831 self->pac = (pac != NULL) ? g_strdup(pac) : NULL;
833 polkit_permission_new("org.freedesktop.packagekit."
834 "system-network-proxy-configure", NULL, NULL,
835 on_polkit_permission_finished, self);
836 }