projects/package-update-indicator

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