projects/package-update-indicator

view pui-backend.c @ 10:adba37525ee5

Notify about required session or system restarts

Follow all PackageKit transactions and indicate when a session or system
restart is required.
Generate enum types with glib-mkenums.
author Guido Berhoerster <guido+pui@berhoerster.name>
date Fri Jul 06 14:12:46 2018 +0200 (2018-07-06)
parents 2477a6151087
children 9905d4ae351c
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
41 struct _PuiBackend {
42 GObject parent_instance;
43 PkControl *pk_control;
44 GCancellable *cancellable;
45 PkClient *pk_client;
46 PkTransactionList *transaction_list;
47 UpClient *up_client;
48 UpDevice *up_device;
49 gchar *proxy_http;
50 gchar *proxy_https;
51 gchar *proxy_ftp;
52 gchar *proxy_socks;
53 gchar *no_proxy;
54 gchar *pac;
55 gint64 last_check;
56 PkNetworkEnum network_state;
57 gboolean inhibited;
58 gboolean is_battery_low;
59 guint periodic_check_id;
60 guint refresh_interval;
61 gboolean use_mobile_connection;
62 guint important_updates;
63 guint normal_updates;
64 PuiRestart restart_type;
65 };
67 static void pui_backend_async_initable_iface_init(gpointer, gpointer);
69 G_DEFINE_TYPE_WITH_CODE(PuiBackend, pui_backend, G_TYPE_OBJECT,
70 G_IMPLEMENT_INTERFACE(G_TYPE_ASYNC_INITABLE,
71 pui_backend_async_initable_iface_init))
73 enum {
74 STATE_CHANGED,
75 RESTART_REQUIRED,
76 SIGNAL_LAST
77 };
79 enum {
80 PROP_0,
81 PROP_IMPORTANT_UPDATES,
82 PROP_NORMAL_UPDATES,
83 PROP_RESTART_TYPE,
84 PROP_REFRESH_INTERVAL,
85 PROP_USE_MOBILE_CONNECTION,
86 PROP_LAST
87 };
89 static guint signals[SIGNAL_LAST] = { 0 };
90 static GParamSpec *properties[PROP_LAST] = { NULL };
92 static gboolean periodic_check(gpointer);
94 GQuark
95 pui_backend_error_quark(void)
96 {
97 return (g_quark_from_static_string("pui-backend-error-quark"));
98 }
100 static void
101 process_pk_package(gpointer data, gpointer user_data)
102 {
103 PkPackage *package = data;
104 PuiBackend *self = user_data;
105 PkInfoEnum type_info = pk_package_get_info(package);
107 switch (type_info) {
108 case PK_INFO_ENUM_LOW: /* FALLTHROUGH */
109 case PK_INFO_ENUM_ENHANCEMENT: /* FALLTHROUGH */
110 case PK_INFO_ENUM_NORMAL:
111 self->normal_updates++;
112 break;
113 case PK_INFO_ENUM_BUGFIX: /* FALLTHROUGH */
114 case PK_INFO_ENUM_IMPORTANT: /* FALLTHROUGH */
115 case PK_INFO_ENUM_SECURITY:
116 self->important_updates++;
117 break;
118 default:
119 break;
120 }
121 }
123 static void
124 on_get_updates_finished(GObject *source_object, GAsyncResult *async_result,
125 gpointer user_data)
126 {
127 PuiBackend *self = user_data;
128 GPtrArray *package_list = NULL;
129 GError *error = NULL;
130 guint prev_normal_updates = self->normal_updates;
131 guint prev_important_updates = self->important_updates;
133 package_list = pui_get_updates_finish(async_result, &error);
134 if (package_list == NULL) {
135 if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) ||
136 g_error_matches(error, PUI_GET_UPDATES_ERROR,
137 PUI_GET_UPDATES_ERROR_CANCELLED)) {
138 /* cancelled */
139 g_debug("cancelled checking for updates");
140 } else {
141 g_warning("failed to check for updates: %s",
142 error->message);
143 }
144 g_error_free(error);
145 goto out;
146 }
148 self->normal_updates = 0;
149 self->important_updates = 0;
150 g_ptr_array_foreach(package_list, process_pk_package, self);
151 g_debug("normal updates: %u, important updates: %u",
152 self->normal_updates, self->important_updates);
153 if (self->normal_updates != prev_normal_updates) {
154 g_object_notify_by_pspec(G_OBJECT(self),
155 properties[PROP_NORMAL_UPDATES]);
156 }
157 if (self->important_updates != prev_important_updates) {
158 g_object_notify_by_pspec(G_OBJECT(self),
159 properties[PROP_IMPORTANT_UPDATES]);
160 }
161 if ((self->normal_updates != prev_normal_updates) ||
162 (self->important_updates != prev_important_updates)) {
163 g_debug("emitting signal state-changed");
164 g_signal_emit(self, signals[STATE_CHANGED], 0);
165 }
167 /* last successful check */
168 self->last_check = g_get_monotonic_time();
170 out:
171 g_clear_object(&self->cancellable);
173 /* reschedule periodic check */
174 if (!self->inhibited) {
175 self->periodic_check_id =
176 g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
177 periodic_check, self);
178 }
180 if (package_list != NULL) {
181 g_ptr_array_unref(package_list);
182 }
183 }
185 static gboolean
186 periodic_check(gpointer user_data)
187 {
188 PuiBackend *self = user_data;
190 g_debug("running periodic check");
192 self->cancellable = g_cancellable_new();
193 pui_get_updates_async(self->pk_control, self->refresh_interval,
194 self->cancellable, on_get_updates_finished, self);
196 /* next periodic check will be scheduled after completion */
197 self->periodic_check_id = 0;
199 return (G_SOURCE_REMOVE);
200 }
202 static void
203 check_inhibit(PuiBackend *self)
204 {
205 gboolean inhibited;
206 guint elapsed_time;
207 guint remaining_time;
209 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
210 (!self->use_mobile_connection &&
211 (self->network_state == PK_NETWORK_ENUM_MOBILE)) ||
212 self->is_battery_low);
213 if (self->inhibited == inhibited) {
214 return;
215 }
217 self->inhibited = inhibited;
218 if (inhibited) {
219 /* cancel periodic checks */
220 if (self->periodic_check_id != 0) {
221 g_source_remove(self->periodic_check_id);
222 }
224 /* cancel running operation */
225 if ((self->cancellable != NULL) &&
226 !g_cancellable_is_cancelled(self->cancellable)) {
227 g_cancellable_cancel(self->cancellable);
228 g_clear_object(&self->cancellable);
229 }
230 } else {
231 /* schedule periodic checks when no longer inhibited */
232 elapsed_time = (g_get_monotonic_time() - self->last_check) /
233 G_USEC_PER_SEC;
234 /*
235 * if more time that the check interval has passed since the
236 * last check, schedule a check after a short delay, otherwise
237 * wait until the interval has passed
238 */
239 remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
240 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
241 PUI_STARTUP_DELAY;
242 self->periodic_check_id = g_timeout_add_seconds(remaining_time,
243 periodic_check, self);
244 }
245 }
247 static void
248 pui_backend_set_property(GObject *object, guint property_id,
249 const GValue *value, GParamSpec *pspec)
250 {
251 PuiBackend *self = PUI_BACKEND(object);
253 switch (property_id) {
254 case PROP_REFRESH_INTERVAL:
255 self->refresh_interval = g_value_get_uint(value);
256 g_debug("property \"refresh-interval\" set to %u",
257 self->refresh_interval);
258 break;
259 case PROP_USE_MOBILE_CONNECTION:
260 self->use_mobile_connection = g_value_get_boolean(value);
261 g_debug("property \"use-mobile-connection\" set to %s",
262 self->use_mobile_connection ? "true" : "false");
263 check_inhibit(self);
264 break;
265 default:
266 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
267 break;
268 }
269 }
271 static void
272 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
273 GParamSpec *pspec)
274 {
275 PuiBackend *self = PUI_BACKEND(object);
277 switch (property_id) {
278 case PROP_IMPORTANT_UPDATES:
279 g_value_set_uint(value, self->important_updates);
280 break;
281 case PROP_NORMAL_UPDATES:
282 g_value_set_uint(value, self->normal_updates);
283 break;
284 case PROP_RESTART_TYPE:
285 g_value_set_enum(value, self->restart_type);
286 break;
287 case PROP_REFRESH_INTERVAL:
288 g_value_set_uint(value, self->refresh_interval);
289 break;
290 case PROP_USE_MOBILE_CONNECTION:
291 g_value_set_boolean(value, self->use_mobile_connection);
292 break;
293 default:
294 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
295 break;
296 }
297 }
299 static void
300 pui_backend_dispose(GObject *object)
301 {
302 PuiBackend *self = PUI_BACKEND(object);
304 if (self->periodic_check_id != 0) {
305 g_source_remove(self->periodic_check_id);
306 self->periodic_check_id = 0;
307 }
309 if (self->transaction_list != NULL) {
310 g_clear_object(&self->transaction_list);
311 }
313 if (self->pk_client != NULL) {
314 g_clear_object(&self->pk_client);
315 }
317 if (self->cancellable != NULL) {
318 g_cancellable_cancel(self->cancellable);
319 g_clear_object(&self->cancellable);
320 }
322 if (self->pk_control != NULL) {
323 g_clear_object(&self->pk_control);
324 }
326 if (self->up_device != NULL) {
327 g_clear_object(&self->up_device);
328 }
330 if (self->up_client != NULL) {
331 g_clear_object(&self->up_client);
332 }
334 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
335 }
337 static void
338 pui_backend_finalize(GObject *object)
339 {
340 PuiBackend *self = PUI_BACKEND(object);
342 g_free(self->proxy_http);
343 g_free(self->proxy_https);
344 g_free(self->proxy_ftp);
345 g_free(self->proxy_socks);
346 g_free(self->no_proxy);
347 g_free(self->pac);
349 G_OBJECT_CLASS(pui_backend_parent_class)->finalize(object);
350 }
352 static void
353 pui_backend_class_init(PuiBackendClass *klass)
354 {
355 GObjectClass *object_class = G_OBJECT_CLASS(klass);
357 object_class->set_property = pui_backend_set_property;
358 object_class->get_property = pui_backend_get_property;
359 object_class->dispose = pui_backend_dispose;
360 object_class->finalize = pui_backend_finalize;
362 properties[PROP_IMPORTANT_UPDATES] =
363 g_param_spec_uint("important-updates", "Important updates",
364 "Number of available important updates", 0, G_MAXUINT, 0,
365 G_PARAM_READABLE);
367 properties[PROP_NORMAL_UPDATES] =
368 g_param_spec_uint("normal-updates", "Normal updates",
369 "Number of available normal updates", 0, G_MAXUINT, 0,
370 G_PARAM_READABLE);
372 properties[PROP_RESTART_TYPE] =
373 g_param_spec_enum("restart-type", "Type of restart required",
374 "The Type of restart required", PUI_TYPE_RESTART, PUI_RESTART_NONE,
375 G_PARAM_READABLE);
377 properties[PROP_REFRESH_INTERVAL] =
378 g_param_spec_uint("refresh-interval", "Refresh interval",
379 "Interval in seconds for refreshing the package cache", 0,
380 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
382 properties[PROP_USE_MOBILE_CONNECTION] =
383 g_param_spec_boolean("use-mobile-connection",
384 "Whether to use a mobile connection", "Whether to use a mobile "
385 "connection for refreshing the package cache", FALSE,
386 G_PARAM_READWRITE);
388 g_object_class_install_properties(object_class, PROP_LAST, properties);
390 signals[STATE_CHANGED] = g_signal_new("state-changed",
391 G_TYPE_FROM_CLASS(object_class),
392 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
393 NULL, NULL, NULL, G_TYPE_NONE, 0);
395 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
396 G_TYPE_FROM_CLASS(object_class),
397 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
398 NULL, NULL, NULL, G_TYPE_NONE, 0);
399 }
401 static void
402 pui_backend_init(PuiBackend *self)
403 {
404 self->pk_control = pk_control_new();
406 self->pk_client = pk_client_new();
408 self->inhibited = TRUE;
410 self->up_client = up_client_new();
411 if (self->up_client) {
412 self->up_device = up_client_get_display_device(self->up_client);
413 }
414 }
416 static void
417 on_get_properties_finished(GObject *object, GAsyncResult *result,
418 gpointer user_data)
419 {
420 PkControl *control = PK_CONTROL(object);
421 PuiBackend *self;
422 GTask *task = user_data;
423 GError *error = NULL;
424 gchar *backend_name = NULL;
425 PkBitfield roles = 0;
426 gchar *roles_str = NULL;
428 self = g_task_get_task_data(task);
430 if (!pk_control_get_properties_finish(control, result, &error)) {
431 g_task_return_error(task, error);
432 goto out;
433 }
435 /* check whether the backend supports GetUpdates */
436 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
437 "network-state", &self->network_state, NULL);
438 g_debug("backend: %s", backend_name);
439 roles_str = pk_role_bitfield_to_string(roles);
440 g_debug("roles: %s", roles_str);
441 g_debug("network-state: %s",
442 pk_network_enum_to_string(self->network_state));
443 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
444 error = g_error_new(PUI_BACKEND_ERROR,
445 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
446 "Getting updates is not implemented in the %s PackageKit "
447 "backend", backend_name);
448 g_task_return_error(task, error);
449 goto out;
450 }
452 g_task_return_boolean(task, TRUE);
453 out:
454 g_free(roles_str);
455 g_free(backend_name);
456 g_object_unref(task);
457 }
459 static void
460 on_notify_device_charge_percentage(UpDevice *device, GParamSpec *pspec,
461 gpointer user_data)
462 {
463 PuiBackend *self = user_data;
464 UpDeviceKind kind;
465 gdouble percentage;
467 g_object_get(device, "kind", &kind, "percentage", &percentage, NULL);
468 if ((kind != UP_DEVICE_KIND_BATTERY) && (kind != UP_DEVICE_KIND_UPS)) {
469 return;
470 }
471 g_debug("charge percentage changed: %.0f%%\n", percentage);
472 if ((self->is_battery_low && (percentage > LOW_BATTERY_THRESHOLD)) ||
473 (!self->is_battery_low && (percentage < LOW_BATTERY_THRESHOLD))) {
474 self->is_battery_low = !self->is_battery_low;
475 check_inhibit(self);
476 }
477 }
479 static void
480 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
481 gpointer user_data)
482 {
483 PuiBackend *self = user_data;
485 g_object_get(pk_control, "network-state", &self->network_state, NULL);
486 g_debug("network state changed: %s",
487 pk_network_enum_to_string(self->network_state));
488 check_inhibit(self);
489 }
491 static void
492 on_updates_changed(PkControl *control, gpointer user_data)
493 {
494 PuiBackend *self = user_data;
496 g_debug("number of updates changed");
498 /*
499 * schedule a check after a short delay so that a rapid succession of
500 * signals is coalesced
501 */
502 if (!self->inhibited) {
503 if (self->periodic_check_id != 0) {
504 g_source_remove(self->periodic_check_id);
505 }
506 self->periodic_check_id =
507 g_timeout_add_seconds(PUI_STARTUP_DELAY, periodic_check,
508 self);
509 }
510 }
512 static void
513 on_restart_schedule(PkControl *control, gpointer user_data)
514 {
515 PuiBackend *self = user_data;
517 /*
518 * do not restart package-update-indicator if a session or system
519 * restart is required since that iformation would be lost across the
520 * restart, rather keep running and risk errors when interacting with
521 * a newer version of the PackageKit daemon
522 */
523 if (self->restart_type > PUI_RESTART_NONE) {
524 return;
525 }
527 g_debug("emitting signal restart-required");
528 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
529 }
531 static void
532 on_transaction_adopt_finish(GObject *source_object, GAsyncResult *result,
533 gpointer user_data)
534 {
535 PuiBackend *self = user_data;
536 PkClient *pk_client = PK_CLIENT(source_object);
537 PkResults *results;
538 GError *error = NULL;
539 PkRestartEnum restart;
541 results = pk_client_generic_finish(pk_client, result, &error);
542 if (results == NULL) {
543 g_warning("failed to get transaction results: %s",
544 error->message);
545 g_error_free(error);
546 goto out;
547 }
549 /* check if transaction requires a restart */
550 restart = pk_results_get_require_restart_worst(results);
551 switch (restart) {
552 case PK_RESTART_ENUM_SESSION: /* FALLTHROUGH */
553 case PK_RESTART_ENUM_SECURITY_SESSION:
554 if (self->restart_type < PUI_RESTART_SESSION) {
555 self->restart_type = PUI_RESTART_SESSION;
556 g_object_notify_by_pspec(G_OBJECT(self),
557 properties[PROP_RESTART_TYPE]);
558 g_signal_emit(self, signals[STATE_CHANGED], 0);
559 }
560 break;
561 case PK_RESTART_ENUM_SYSTEM: /* FALLTHROUGH */
562 case PK_RESTART_ENUM_SECURITY_SYSTEM:
563 if (self->restart_type < PUI_RESTART_SYSTEM) {
564 self->restart_type = PUI_RESTART_SYSTEM;
565 g_object_notify_by_pspec(G_OBJECT(self),
566 properties[PROP_RESTART_TYPE]);
567 g_signal_emit(self, signals[STATE_CHANGED], 0);
568 }
569 break;
570 default:
571 /* do nothing */
572 break;
573 }
575 g_debug("transaction finished, required restart: %s",
576 pk_restart_enum_to_string(restart));
578 out:
579 if (results != NULL) {
580 g_object_unref(results);
581 }
582 }
584 static void
585 on_transaction_list_added(PkTransactionList *transaction_list,
586 const gchar *transaction_id, gpointer user_data)
587 {
588 PuiBackend *self = user_data;
590 /* adopt transaction in order to monitor it for restart requirements */
591 pk_client_adopt_async(self->pk_client, transaction_id, NULL, NULL,
592 NULL, on_transaction_adopt_finish, user_data);
593 }
595 static void
596 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
597 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
598 {
599 PuiBackend *self = PUI_BACKEND(initable);
600 GTask *task;
602 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
603 g_task_set_priority(task, io_priority);
604 g_task_set_task_data(task, g_object_ref(self),
605 (GDestroyNotify)g_object_unref);
607 pk_control_get_properties_async(self->pk_control, cancellable,
608 on_get_properties_finished, task);
609 }
611 static gboolean
612 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
613 GError **errorp)
614 {
615 PuiBackend *self = PUI_BACKEND(initable);
616 GTask *task = G_TASK(result);
617 UpDeviceKind kind;
618 gdouble percentage;
620 if (!g_task_propagate_boolean(task, errorp)) {
621 return (FALSE);
622 }
624 if (self->up_device != NULL) {
625 /* get the kind of device and charge percentage */
626 g_object_get(self->up_device, "kind", &kind, "percentage",
627 &percentage, NULL);
628 if ((kind == UP_DEVICE_KIND_BATTERY) ||
629 (kind == UP_DEVICE_KIND_UPS)) {
630 self->is_battery_low =
631 (percentage < LOW_BATTERY_THRESHOLD);
632 }
634 /* get notification if the charge percentage changes */
635 g_signal_connect(self->up_device, "notify::percentage",
636 G_CALLBACK(on_notify_device_charge_percentage), self);
637 }
639 /* get notification when the network state changes */
640 g_signal_connect(self->pk_control, "notify::network-state",
641 G_CALLBACK(on_notify_network_state), self);
642 /* get notifications when the available package updates change */
643 g_signal_connect(self->pk_control, "updates-changed",
644 G_CALLBACK(on_updates_changed), self);
645 /* get notifications when an application restart is required */
646 g_signal_connect(self->pk_control, "restart-schedule",
647 G_CALLBACK(on_restart_schedule), self);
648 /* get notifications when a transactions are added */
649 self->transaction_list = pk_transaction_list_new();
650 g_signal_connect(self->transaction_list, "added",
651 G_CALLBACK(on_transaction_list_added), self);
653 check_inhibit(self);
655 return (TRUE);
656 }
658 static void
659 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
660 {
661 GAsyncInitableIface *iface = g_iface;
663 iface->init_async = pui_backend_init_async;
664 iface->init_finish = pui_backend_init_finish;
665 }
667 void
668 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
669 gpointer user_data)
670 {
671 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
672 cancellable, callback, user_data, NULL);
673 }
675 PuiBackend *
676 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
677 {
678 GObject *object;
679 GObject *source_object;
681 source_object = g_async_result_get_source_object(result);
682 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
683 result, errorp);
684 g_object_unref(source_object);
686 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
687 }
689 static void
690 on_set_proxy_finished(GObject *source_object, GAsyncResult *result,
691 gpointer user_data)
692 {
693 PuiBackend *self = user_data;
694 GError *error = NULL;
696 if (!pk_control_set_proxy_finish(self->pk_control, result, &error)) {
697 g_warning("failed to set proxies: %s", error->message);
698 g_error_free(error);
699 }
700 }
702 static void
703 on_polkit_permission_finished(GObject *source_object, GAsyncResult *result,
704 gpointer user_data)
705 {
706 PuiBackend *self = user_data;
707 GPermission *permission;
708 GError *error = NULL;
710 permission = polkit_permission_new_finish(result, &error);
711 if (permission == NULL) {
712 g_warning("failed to create PolKit permission for setting the "
713 "network proxies: %s", error->message);
714 g_error_free(error);
715 return;
716 }
718 if (!g_permission_get_allowed(permission)) {
719 /* setting the proxy requires authentication or is disallowed */
720 g_debug("setting the network proxy is not allowed");
721 return;
722 }
724 g_debug("setting HTTP proxy to \"%s\"", (self->proxy_http != NULL) ?
725 self->proxy_http : "(null)");
726 g_debug("setting HTTPS proxy to \"%s\"", (self->proxy_https != NULL) ?
727 self->proxy_https : "(null)");
728 g_debug("setting FTP proxy to \"%s\"", (self->proxy_ftp != NULL) ?
729 self->proxy_ftp : "(null)");
730 g_debug("setting SOCKS proxy to \"%s\"", (self->proxy_socks != NULL) ?
731 self->proxy_socks : "(null)");
732 g_debug("setting the list of download IPs which should not go through "
733 "a proxy to \"%s\"", (self->no_proxy != NULL) ? self->no_proxy :
734 "(null)");
735 g_debug("setting the PAC string to \"%s\"", (self->pac != NULL) ?
736 self->pac : "(null)");
737 pk_control_set_proxy2_async(self->pk_control, self->proxy_http,
738 self->proxy_https, self->proxy_ftp, self->proxy_socks,
739 self->no_proxy, self->pac, NULL, on_set_proxy_finished, self);
740 }
742 void
743 pui_backend_set_proxy(PuiBackend *self, const gchar *proxy_http,
744 const gchar *proxy_https, const gchar *proxy_ftp, const gchar *proxy_socks,
745 const gchar *no_proxy, const gchar *pac)
746 {
747 g_free(self->proxy_http);
748 self->proxy_http = (proxy_http != NULL) ? g_strdup(proxy_http) : NULL;
749 g_free(self->proxy_https);
750 self->proxy_https = (proxy_https != NULL) ? g_strdup(proxy_https) :
751 NULL;
752 g_free(self->proxy_ftp);
753 self->proxy_ftp = (proxy_ftp != NULL) ? g_strdup(proxy_ftp) : NULL;
754 g_free(self->proxy_socks);
755 self->proxy_socks = (proxy_socks != NULL) ? g_strdup(proxy_socks) :
756 NULL;
757 g_free(self->no_proxy);
758 self->no_proxy = (no_proxy != NULL) ? g_strdup(no_proxy) : NULL;
759 g_free(self->pac);
760 self->pac = (pac != NULL) ? g_strdup(pac) : NULL;
762 polkit_permission_new("org.freedesktop.packagekit."
763 "system-network-proxy-configure", NULL, NULL,
764 on_polkit_permission_finished, self);
765 }