projects/package-update-indicator

view pui-backend.c @ 52:707e9e3deeac

Do not hardcode pkg-config

Using a macro allows passing a different executable in case of
cross-compilation.
author Helmut Grohne <helmut@subdivi.de>
date Sat Dec 12 01:08:21 2020 -0500 (11 months ago)
parents 4a859595eabd
children
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 if (self->last_check == 0) {
294 /* first check after startup */
295 remaining_time = PUI_STARTUP_DELAY;
297 g_debug("scheduled first check in: %ds",
298 remaining_time);
299 } else {
300 /* schedule periodic checks when no longer inhibited */
301 elapsed_time =
302 (g_get_monotonic_time() - self->last_check) /
303 G_USEC_PER_SEC;
304 /*
305 * if more time than the check interval has passed
306 * since the last check, schedule a check after a short
307 * delay, otherwise wait until the interval has passed
308 */
309 remaining_time =
310 (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
311 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
312 PUI_STARTUP_DELAY;
314 g_debug("perioidic checks no longer inhibited, "
315 "time since last check: %ds, next check in: %ds",
316 elapsed_time, remaining_time);
317 }
318 self->check_id = g_timeout_add_seconds(remaining_time,
319 periodic_check, self);
320 }
321 }
323 static void
324 pui_backend_set_property(GObject *object, guint property_id,
325 const GValue *value, GParamSpec *pspec)
326 {
327 PuiBackend *self = PUI_BACKEND(object);
329 switch (property_id) {
330 case PROP_REFRESH_INTERVAL:
331 self->refresh_interval = g_value_get_uint(value);
332 g_debug("property \"refresh-interval\" set to %u",
333 self->refresh_interval);
334 break;
335 case PROP_USE_MOBILE_CONNECTION:
336 self->use_mobile_connection = g_value_get_boolean(value);
337 g_debug("property \"use-mobile-connection\" set to %s",
338 self->use_mobile_connection ? "true" : "false");
339 check_inhibit(self);
340 break;
341 default:
342 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
343 break;
344 }
345 }
347 static void
348 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
349 GParamSpec *pspec)
350 {
351 PuiBackend *self = PUI_BACKEND(object);
353 switch (property_id) {
354 case PROP_IMPORTANT_UPDATES:
355 g_value_set_uint(value, self->important_updates);
356 break;
357 case PROP_NORMAL_UPDATES:
358 g_value_set_uint(value, self->normal_updates);
359 break;
360 case PROP_RESTART_TYPE:
361 g_value_set_enum(value, self->restart_type);
362 break;
363 case PROP_REFRESH_INTERVAL:
364 g_value_set_uint(value, self->refresh_interval);
365 break;
366 case PROP_USE_MOBILE_CONNECTION:
367 g_value_set_boolean(value, self->use_mobile_connection);
368 break;
369 default:
370 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
371 break;
372 }
373 }
375 static void
376 pui_backend_dispose(GObject *object)
377 {
378 PuiBackend *self = PUI_BACKEND(object);
380 if (self->check_id != 0) {
381 g_source_remove(self->check_id);
382 self->check_id = 0;
383 }
385 if (self->unblock_updates_changed_id != 0) {
386 g_source_remove(self->unblock_updates_changed_id);
387 self->unblock_updates_changed_id = 0;
388 }
390 if (self->transaction_list != NULL) {
391 g_clear_object(&self->transaction_list);
392 }
394 if (self->pk_client != NULL) {
395 g_clear_object(&self->pk_client);
396 }
398 if (self->cancellable != NULL) {
399 g_cancellable_cancel(self->cancellable);
400 g_clear_object(&self->cancellable);
401 }
403 if (self->pk_control != NULL) {
404 g_clear_object(&self->pk_control);
405 }
407 if (self->up_device != NULL) {
408 g_clear_object(&self->up_device);
409 }
411 if (self->up_client != NULL) {
412 g_clear_object(&self->up_client);
413 }
415 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
416 }
418 static void
419 pui_backend_finalize(GObject *object)
420 {
421 PuiBackend *self = PUI_BACKEND(object);
423 g_free(self->proxy_http);
424 g_free(self->proxy_https);
425 g_free(self->proxy_ftp);
426 g_free(self->proxy_socks);
427 g_free(self->no_proxy);
428 g_free(self->pac);
430 G_OBJECT_CLASS(pui_backend_parent_class)->finalize(object);
431 }
433 static void
434 pui_backend_class_init(PuiBackendClass *klass)
435 {
436 GObjectClass *object_class = G_OBJECT_CLASS(klass);
438 object_class->set_property = pui_backend_set_property;
439 object_class->get_property = pui_backend_get_property;
440 object_class->dispose = pui_backend_dispose;
441 object_class->finalize = pui_backend_finalize;
443 properties[PROP_IMPORTANT_UPDATES] =
444 g_param_spec_uint("important-updates", "Important updates",
445 "Number of available important updates", 0, G_MAXUINT, 0,
446 G_PARAM_READABLE);
448 properties[PROP_NORMAL_UPDATES] =
449 g_param_spec_uint("normal-updates", "Normal updates",
450 "Number of available normal updates", 0, G_MAXUINT, 0,
451 G_PARAM_READABLE);
453 properties[PROP_RESTART_TYPE] =
454 g_param_spec_enum("restart-type", "Type of restart required",
455 "The Type of restart required", PUI_TYPE_RESTART, PUI_RESTART_NONE,
456 G_PARAM_READABLE);
458 properties[PROP_REFRESH_INTERVAL] =
459 g_param_spec_uint("refresh-interval", "Refresh interval",
460 "Interval in seconds for refreshing the package cache", 0,
461 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
463 properties[PROP_USE_MOBILE_CONNECTION] =
464 g_param_spec_boolean("use-mobile-connection",
465 "Whether to use a mobile connection", "Whether to use a mobile "
466 "connection for refreshing the package cache", FALSE,
467 G_PARAM_READWRITE);
469 g_object_class_install_properties(object_class, PROP_LAST, properties);
471 signals[STATE_CHANGED] = g_signal_new("state-changed",
472 G_TYPE_FROM_CLASS(object_class),
473 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
474 NULL, NULL, NULL, G_TYPE_NONE, 0);
476 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
477 G_TYPE_FROM_CLASS(object_class),
478 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
479 NULL, NULL, NULL, G_TYPE_NONE, 0);
480 }
482 static void
483 pui_backend_init(PuiBackend *self)
484 {
485 self->pk_control = pk_control_new();
487 self->pk_client = pk_client_new();
489 self->inhibited = TRUE;
491 self->up_client = up_client_new();
492 if (self->up_client) {
493 self->up_device = up_client_get_display_device(self->up_client);
494 }
495 }
497 static void
498 on_get_properties_finished(GObject *object, GAsyncResult *result,
499 gpointer user_data)
500 {
501 PkControl *control = PK_CONTROL(object);
502 PuiBackend *self;
503 GTask *task = user_data;
504 GError *error = NULL;
505 gchar *backend_name = NULL;
506 PkBitfield roles = 0;
507 gchar *roles_str = NULL;
509 self = g_task_get_task_data(task);
511 if (!pk_control_get_properties_finish(control, result, &error)) {
512 g_task_return_error(task, error);
513 goto out;
514 }
516 /* check whether the backend supports GetUpdates */
517 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
518 "network-state", &self->network_state, NULL);
519 g_debug("backend: %s", backend_name);
520 roles_str = pk_role_bitfield_to_string(roles);
521 g_debug("roles: %s", roles_str);
522 g_debug("network-state: %s",
523 pk_network_enum_to_string(self->network_state));
524 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
525 error = g_error_new(PUI_BACKEND_ERROR,
526 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
527 "Getting updates is not implemented in the %s PackageKit "
528 "backend", backend_name);
529 g_task_return_error(task, error);
530 goto out;
531 }
533 g_task_return_boolean(task, TRUE);
534 out:
535 g_free(roles_str);
536 g_free(backend_name);
537 g_object_unref(task);
538 }
540 static void
541 on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
542 gpointer user_data)
543 {
544 PuiBackend *self = user_data;
545 UpDeviceKind kind;
546 gdouble percentage;
548 g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
549 if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
550 return;
551 }
552 g_debug("charge percentage changed: %.0f%%\n", percentage);
553 if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
554 (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
555 self->is_battery_low = !self->is_battery_low;
556 check_inhibit(self);
557 }
558 }
560 static void
561 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
562 gpointer user_data)
563 {
564 PuiBackend *self = user_data;
566 g_object_get(pk_control, "network-state", &self->network_state, NULL);
567 g_debug("network state changed: %s",
568 pk_network_enum_to_string(self->network_state));
569 check_inhibit(self);
570 }
572 static void
573 on_updates_changed(PkControl *control, gpointer user_data)
574 {
575 PuiBackend *self = user_data;
577 g_debug("package metatdata cache invalidated");
579 /*
580 * schedule a check after a short delay so that a rapid succession of
581 * signals is coalesced
582 */
583 if (!self->inhibited) {
584 if (self->check_id != 0) {
585 g_source_remove(self->check_id);
586 }
587 self->check_id =
588 g_timeout_add_seconds(PUI_UPDATES_CHANGED_DELAY,
589 irregular_check, self);
590 }
591 }
593 static void
594 on_restart_schedule(PkControl *control, gpointer user_data)
595 {
596 PuiBackend *self = user_data;
598 /*
599 * do not restart package-update-indicator if a session or system
600 * restart is required since that iformation would be lost across the
601 * restart, rather keep running and risk errors when interacting with
602 * a newer version of the PackageKit daemon
603 */
604 if (self->restart_type > PUI_RESTART_NONE) {
605 return;
606 }
608 g_debug("emitting signal restart-required");
609 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
610 }
612 static void
613 on_transaction_adopt_finish(GObject *source_object, GAsyncResult *result,
614 gpointer user_data)
615 {
616 PuiBackend *self = user_data;
617 PkClient *pk_client = PK_CLIENT(source_object);
618 PkResults *results;
619 GError *error = NULL;
620 PkRestartEnum restart;
622 results = pk_client_generic_finish(pk_client, result, &error);
623 if (results == NULL) {
624 g_warning("failed to get transaction results: %s",
625 error->message);
626 g_error_free(error);
627 goto out;
628 }
630 /* check if transaction requires a restart */
631 restart = pk_results_get_require_restart_worst(results);
632 switch (restart) {
633 case PK_RESTART_ENUM_SESSION: /* FALLTHROUGH */
634 case PK_RESTART_ENUM_SECURITY_SESSION:
635 if (self->restart_type < PUI_RESTART_SESSION) {
636 self->restart_type = PUI_RESTART_SESSION;
637 g_object_notify_by_pspec(G_OBJECT(self),
638 properties[PROP_RESTART_TYPE]);
639 g_signal_emit(self, signals[STATE_CHANGED], 0);
640 }
641 break;
642 case PK_RESTART_ENUM_SYSTEM: /* FALLTHROUGH */
643 case PK_RESTART_ENUM_SECURITY_SYSTEM:
644 if (self->restart_type < PUI_RESTART_SYSTEM) {
645 self->restart_type = PUI_RESTART_SYSTEM;
646 g_object_notify_by_pspec(G_OBJECT(self),
647 properties[PROP_RESTART_TYPE]);
648 g_signal_emit(self, signals[STATE_CHANGED], 0);
649 }
650 break;
651 default:
652 /* do nothing */
653 break;
654 }
656 g_debug("transaction finished, required restart: %s",
657 pk_restart_enum_to_string(restart));
659 out:
660 if (results != NULL) {
661 g_object_unref(results);
662 }
663 }
665 static void
666 on_transaction_list_added(PkTransactionList *transaction_list,
667 const gchar *transaction_id, gpointer user_data)
668 {
669 PuiBackend *self = user_data;
671 /* adopt transaction in order to monitor it for restart requirements */
672 pk_client_adopt_async(self->pk_client, transaction_id, NULL, NULL,
673 NULL, on_transaction_adopt_finish, user_data);
674 }
676 static void
677 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
678 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
679 {
680 PuiBackend *self = PUI_BACKEND(initable);
681 GTask *task;
683 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
684 g_task_set_priority(task, io_priority);
685 g_task_set_task_data(task, g_object_ref(self),
686 (GDestroyNotify)g_object_unref);
688 pk_control_get_properties_async(self->pk_control, cancellable,
689 on_get_properties_finished, task);
690 }
692 static gboolean
693 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
694 GError **errorp)
695 {
696 PuiBackend *self = PUI_BACKEND(initable);
697 GTask *task = G_TASK(result);
698 UpDeviceKind kind;
699 gdouble percentage;
701 if (!g_task_propagate_boolean(task, errorp)) {
702 return (FALSE);
703 }
705 if (self->up_device != NULL) {
706 /* get the kind of device and charge percentage */
707 g_object_get(self->up_device, "kind", &kind, "percentage",
708 &percentage, NULL);
709 if ((kind == UP_DEVICE_KIND_BATTERY) ||
710 (kind == UP_DEVICE_KIND_UPS)) {
711 self->is_battery_low =
712 (percentage < LOW_BATTERY_THRESHOLD);
713 }
715 /* get notification if the charge percentage changes */
716 g_signal_connect(self->up_device, "notify::percentage",
717 G_CALLBACK(on_notify_device_charge_percentage), self);
718 }
720 /* get notification when the network state changes */
721 g_signal_connect(self->pk_control, "notify::network-state",
722 G_CALLBACK(on_notify_network_state), self);
723 /* get notifications when the package metatdata cache is invalidated */
724 g_signal_connect(self->pk_control, "updates-changed",
725 G_CALLBACK(on_updates_changed), self);
726 /* get notifications when an application restart is required */
727 g_signal_connect(self->pk_control, "restart-schedule",
728 G_CALLBACK(on_restart_schedule), self);
729 /* get notifications when a transactions are added */
730 self->transaction_list = pk_transaction_list_new();
731 g_signal_connect(self->transaction_list, "added",
732 G_CALLBACK(on_transaction_list_added), self);
734 check_inhibit(self);
736 return (TRUE);
737 }
739 static void
740 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
741 {
742 GAsyncInitableIface *iface = g_iface;
744 iface->init_async = pui_backend_init_async;
745 iface->init_finish = pui_backend_init_finish;
746 }
748 void
749 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
750 gpointer user_data)
751 {
752 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
753 cancellable, callback, user_data, NULL);
754 }
756 PuiBackend *
757 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
758 {
759 GObject *object;
760 GObject *source_object;
762 source_object = g_async_result_get_source_object(result);
763 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
764 result, errorp);
765 g_object_unref(source_object);
767 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
768 }
770 static void
771 on_set_proxy_finished(GObject *source_object, GAsyncResult *result,
772 gpointer user_data)
773 {
774 PuiBackend *self = user_data;
775 GError *error = NULL;
777 if (!pk_control_set_proxy_finish(self->pk_control, result, &error)) {
778 g_warning("failed to set proxies: %s", error->message);
779 g_error_free(error);
780 }
781 }
783 static void
784 on_polkit_permission_finished(GObject *source_object, GAsyncResult *result,
785 gpointer user_data)
786 {
787 PuiBackend *self = user_data;
788 GPermission *permission;
789 GError *error = NULL;
791 permission = polkit_permission_new_finish(result, &error);
792 if (permission == NULL) {
793 g_warning("failed to create PolKit permission for setting the "
794 "network proxies: %s", error->message);
795 g_error_free(error);
796 return;
797 }
799 if (!g_permission_get_allowed(permission)) {
800 /* setting the proxy requires authentication or is disallowed */
801 g_debug("setting the network proxy is not allowed");
802 return;
803 }
805 g_debug("setting HTTP proxy to \"%s\"", (self->proxy_http != NULL) ?
806 self->proxy_http : "(null)");
807 g_debug("setting HTTPS proxy to \"%s\"", (self->proxy_https != NULL) ?
808 self->proxy_https : "(null)");
809 g_debug("setting FTP proxy to \"%s\"", (self->proxy_ftp != NULL) ?
810 self->proxy_ftp : "(null)");
811 g_debug("setting SOCKS proxy to \"%s\"", (self->proxy_socks != NULL) ?
812 self->proxy_socks : "(null)");
813 g_debug("setting the list of download IPs which should not go through "
814 "a proxy to \"%s\"", (self->no_proxy != NULL) ? self->no_proxy :
815 "(null)");
816 g_debug("setting the PAC string to \"%s\"", (self->pac != NULL) ?
817 self->pac : "(null)");
818 pk_control_set_proxy2_async(self->pk_control, self->proxy_http,
819 self->proxy_https, self->proxy_ftp, self->proxy_socks,
820 self->no_proxy, self->pac, NULL, on_set_proxy_finished, self);
821 }
823 void
824 pui_backend_set_proxy(PuiBackend *self, const gchar *proxy_http,
825 const gchar *proxy_https, const gchar *proxy_ftp, const gchar *proxy_socks,
826 const gchar *no_proxy, const gchar *pac)
827 {
828 g_free(self->proxy_http);
829 self->proxy_http = (proxy_http != NULL) ? g_strdup(proxy_http) : NULL;
830 g_free(self->proxy_https);
831 self->proxy_https = (proxy_https != NULL) ? g_strdup(proxy_https) :
832 NULL;
833 g_free(self->proxy_ftp);
834 self->proxy_ftp = (proxy_ftp != NULL) ? g_strdup(proxy_ftp) : NULL;
835 g_free(self->proxy_socks);
836 self->proxy_socks = (proxy_socks != NULL) ? g_strdup(proxy_socks) :
837 NULL;
838 g_free(self->no_proxy);
839 self->no_proxy = (no_proxy != NULL) ? g_strdup(no_proxy) : NULL;
840 g_free(self->pac);
841 self->pac = (pac != NULL) ? g_strdup(pac) : NULL;
843 polkit_permission_new("org.freedesktop.packagekit."
844 "system-network-proxy-configure", NULL, NULL,
845 on_polkit_permission_finished, self);
846 }