projects/package-update-indicator

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