projects/package-update-indicator

view pui-backend.c @ 4:3d72ca76538d

Add setting to control whether to use a mobile connection
author Guido Berhoerster <guido+pui@berhoerster.name>
date Sun Jun 17 11:05:28 2018 +0200 (2018-06-17)
parents 6884bb8130ca
children a4020e99e550
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 <string.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <utime.h>
32 #include "pui-common.h"
33 #include "pui-backend.h"
34 #include "pui-get-updates.h"
36 struct _PuiBackend {
37 GObject parent_instance;
38 PkControl *pk_control;
39 GCancellable *cancellable;
40 gint64 last_check;
41 PkNetworkEnum network_state;
42 gboolean inhibited;
43 guint periodic_check_id;
44 guint refresh_interval;
45 gboolean use_mobile_connection;
46 guint important_updates;
47 guint normal_updates;
48 };
50 static void pui_backend_async_initable_iface_init(gpointer, gpointer);
52 G_DEFINE_TYPE_WITH_CODE(PuiBackend, pui_backend, G_TYPE_OBJECT,
53 G_IMPLEMENT_INTERFACE(G_TYPE_ASYNC_INITABLE,
54 pui_backend_async_initable_iface_init))
56 enum {
57 STATE_CHANGED,
58 RESTART_REQUIRED,
59 SIGNAL_LAST
60 };
62 enum {
63 PROP_0,
64 PROP_IMPORTANT_UPDATES,
65 PROP_NORMAL_UPDATES,
66 PROP_REFRESH_INTERVAL,
67 PROP_USE_MOBILE_CONNECTION,
68 PROP_LAST
69 };
71 static guint signals[SIGNAL_LAST] = { 0 };
72 static GParamSpec *properties[PROP_LAST] = { NULL };
74 static gboolean periodic_check(gpointer);
76 GQuark
77 pui_backend_error_quark(void)
78 {
79 return (g_quark_from_static_string("pui-backend-error-quark"));
80 }
82 static void
83 process_pk_package(gpointer data, gpointer user_data)
84 {
85 PkPackage *package = data;
86 PuiBackend *self = user_data;
87 PkInfoEnum type_info = pk_package_get_info(package);
89 switch (type_info) {
90 case PK_INFO_ENUM_LOW: /* FALLTHROUGH */
91 case PK_INFO_ENUM_ENHANCEMENT: /* FALLTHROUGH */
92 case PK_INFO_ENUM_NORMAL:
93 self->normal_updates++;
94 break;
95 case PK_INFO_ENUM_BUGFIX: /* FALLTHROUGH */
96 case PK_INFO_ENUM_IMPORTANT: /* FALLTHROUGH */
97 case PK_INFO_ENUM_SECURITY:
98 self->important_updates++;
99 break;
100 default:
101 break;
102 }
103 }
105 static void
106 on_get_updates_finished(GObject *source_object, GAsyncResult *async_result,
107 gpointer user_data)
108 {
109 PuiBackend *self = user_data;
110 GPtrArray *package_list = NULL;
111 GError *error = NULL;
112 guint prev_normal_updates = self->normal_updates;
113 guint prev_important_updates = self->important_updates;
115 package_list = pui_get_updates_finish(async_result, &error);
116 if (package_list == NULL) {
117 if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) ||
118 g_error_matches(error, PUI_GET_UPDATES_ERROR,
119 PUI_GET_UPDATES_ERROR_CANCELLED)) {
120 /* cancelled */
121 g_debug("cancelled checking for updates");
122 } else {
123 g_warning("failed to check for updates: %s",
124 error->message);
125 }
126 g_error_free(error);
127 goto out;
128 }
130 self->normal_updates = 0;
131 self->important_updates = 0;
132 g_ptr_array_foreach(package_list, process_pk_package, self);
133 g_debug("normal updates: %u, important updates: %u",
134 self->normal_updates, self->important_updates);
135 if (self->normal_updates != prev_normal_updates) {
136 g_object_notify_by_pspec(G_OBJECT(self),
137 properties[PROP_NORMAL_UPDATES]);
138 }
139 if (self->important_updates != prev_important_updates) {
140 g_object_notify_by_pspec(G_OBJECT(self),
141 properties[PROP_IMPORTANT_UPDATES]);
142 }
143 if ((self->normal_updates != prev_normal_updates) ||
144 (self->important_updates != prev_important_updates)) {
145 g_debug("emitting signal state-changed");
146 g_signal_emit(self, signals[STATE_CHANGED], 0);
147 }
149 /* last successful check */
150 self->last_check = g_get_monotonic_time();
152 out:
153 g_clear_object(&self->cancellable);
155 /* reschedule periodic check */
156 if (!self->inhibited) {
157 self->periodic_check_id =
158 g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL,
159 periodic_check, self);
160 }
162 if (package_list != NULL) {
163 g_ptr_array_unref(package_list);
164 }
165 }
167 static gboolean
168 periodic_check(gpointer user_data)
169 {
170 PuiBackend *self = user_data;
172 g_debug("running periodic check");
174 self->cancellable = g_cancellable_new();
175 pui_get_updates_async(self->pk_control, self->refresh_interval,
176 self->cancellable, on_get_updates_finished, self);
178 /* next periodic check will be scheduled after completion */
179 self->periodic_check_id = 0;
181 return (G_SOURCE_REMOVE);
182 }
184 static void
185 check_inhibit(PuiBackend *self)
186 {
187 gboolean inhibited;
188 guint elapsed_time;
189 guint remaining_time;
191 inhibited = ((self->network_state == PK_NETWORK_ENUM_OFFLINE) ||
192 (!self->use_mobile_connection &&
193 (self->network_state == PK_NETWORK_ENUM_MOBILE)));
194 if (self->inhibited == inhibited) {
195 return;
196 }
198 self->inhibited = inhibited;
199 if (inhibited) {
200 /* cancel periodic checks */
201 if (self->periodic_check_id != 0) {
202 g_source_remove(self->periodic_check_id);
203 }
205 /* cancel running operation */
206 if ((self->cancellable != NULL) &&
207 !g_cancellable_is_cancelled(self->cancellable)) {
208 g_cancellable_cancel(self->cancellable);
209 g_clear_object(&self->cancellable);
210 }
211 } else {
212 /* schedule periodic checks when no longer inhibited */
213 elapsed_time = (g_get_monotonic_time() - self->last_check) /
214 G_USEC_PER_SEC;
215 /*
216 * if more time that the check interval has passed since the
217 * last check, schedule a check after a short delay, otherwise
218 * wait until the interval has passed
219 */
220 remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ?
221 PUI_CHECK_UPDATES_INTERVAL - elapsed_time :
222 PUI_STARTUP_DELAY;
223 self->periodic_check_id = g_timeout_add_seconds(remaining_time,
224 periodic_check, self);
225 }
226 }
228 static void
229 pui_backend_set_property(GObject *object, guint property_id,
230 const GValue *value, GParamSpec *pspec)
231 {
232 PuiBackend *self = PUI_BACKEND(object);
234 switch (property_id) {
235 case PROP_REFRESH_INTERVAL:
236 self->refresh_interval = g_value_get_uint(value);
237 g_debug("property \"refresh-interval\" set to %u",
238 self->refresh_interval);
239 break;
240 case PROP_USE_MOBILE_CONNECTION:
241 self->use_mobile_connection = g_value_get_boolean(value);
242 g_debug("property \"use-mobile-connection\" set to %s",
243 self->use_mobile_connection ? "true" : "false");
244 check_inhibit(self);
245 break;
246 default:
247 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
248 break;
249 }
250 }
252 static void
253 pui_backend_get_property(GObject *object, guint property_id, GValue *value,
254 GParamSpec *pspec)
255 {
256 PuiBackend *self = PUI_BACKEND(object);
258 switch (property_id) {
259 case PROP_IMPORTANT_UPDATES:
260 g_value_set_uint(value, self->important_updates);
261 break;
262 case PROP_NORMAL_UPDATES:
263 g_value_set_uint(value, self->normal_updates);
264 break;
265 case PROP_REFRESH_INTERVAL:
266 g_value_set_uint(value, self->refresh_interval);
267 break;
268 case PROP_USE_MOBILE_CONNECTION:
269 g_value_set_boolean(value, self->use_mobile_connection);
270 break;
271 default:
272 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec);
273 break;
274 }
275 }
277 static void
278 pui_backend_dispose(GObject *object)
279 {
280 PuiBackend *self = PUI_BACKEND(object);
282 if (self->periodic_check_id != 0) {
283 g_source_remove(self->periodic_check_id);
284 self->periodic_check_id = 0;
285 }
287 if (self->cancellable != NULL) {
288 g_cancellable_cancel(self->cancellable);
289 g_clear_object(&self->cancellable);
290 }
292 if (self->pk_control != NULL) {
293 g_clear_object(&self->pk_control);
294 }
296 G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object);
297 }
299 static void
300 pui_backend_class_init(PuiBackendClass *klass)
301 {
302 GObjectClass *object_class = G_OBJECT_CLASS(klass);
304 object_class->set_property = pui_backend_set_property;
305 object_class->get_property = pui_backend_get_property;
306 object_class->dispose = pui_backend_dispose;
308 properties[PROP_IMPORTANT_UPDATES] =
309 g_param_spec_uint("important-updates", "Important updates",
310 "Number of available important updates", 0, G_MAXUINT, 0,
311 G_PARAM_READABLE);
313 properties[PROP_NORMAL_UPDATES] =
314 g_param_spec_uint("normal-updates", "Normal updates",
315 "Number of available normal updates", 0, G_MAXUINT, 0,
316 G_PARAM_READABLE);
318 properties[PROP_REFRESH_INTERVAL] =
319 g_param_spec_uint("refresh-interval", "Refresh interval",
320 "Interval in seconds for refreshing the package cache", 0,
321 G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE);
323 properties[PROP_USE_MOBILE_CONNECTION] =
324 g_param_spec_boolean("use-mobile-connection",
325 "Whether to use a mobile connection", "Whether to use a mobile "
326 "connection for refreshing the package cache", FALSE,
327 G_PARAM_READWRITE);
329 g_object_class_install_properties(object_class, PROP_LAST, properties);
331 signals[STATE_CHANGED] = g_signal_new("state-changed",
332 G_TYPE_FROM_CLASS(object_class),
333 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
334 NULL, NULL, NULL, G_TYPE_NONE, 0);
336 signals[RESTART_REQUIRED] = g_signal_new("restart-required",
337 G_TYPE_FROM_CLASS(object_class),
338 G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0,
339 NULL, NULL, NULL, G_TYPE_NONE, 0);
340 }
342 static void
343 pui_backend_init(PuiBackend *self)
344 {
345 self->pk_control = pk_control_new();
346 self->inhibited = TRUE;
347 }
349 static void
350 on_get_properties_finished(GObject *object, GAsyncResult *result,
351 gpointer user_data)
352 {
353 PkControl *control = PK_CONTROL(object);
354 PuiBackend *self;
355 GTask *task = user_data;
356 GError *error = NULL;
357 gchar *backend_name = NULL;
358 PkBitfield roles = 0;
359 gchar *roles_str = NULL;
361 self = g_task_get_task_data(task);
363 if (!pk_control_get_properties_finish(control, result, &error)) {
364 g_task_return_error(task, error);
365 goto out;
366 }
368 /* check whether the backend supports GetUpdates */
369 g_object_get(control, "backend-name", &backend_name, "roles", &roles,
370 "network-state", &self->network_state, NULL);
371 g_debug("backend: %s", backend_name);
372 roles_str = pk_role_bitfield_to_string(roles);
373 g_debug("roles: %s", roles_str);
374 g_debug("network-state: %s",
375 pk_network_enum_to_string(self->network_state));
376 if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) {
377 error = g_error_new(PUI_BACKEND_ERROR,
378 PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED,
379 "Getting updates is not implemented in the %s PackageKit "
380 "backend", backend_name);
381 g_task_return_error(task, error);
382 goto out;
383 }
385 g_task_return_boolean(task, TRUE);
386 out:
387 g_free(roles_str);
388 g_free(backend_name);
389 g_object_unref(task);
390 }
392 static void
393 on_notify_network_state(PkControl *pk_control, GParamSpec *pspec,
394 gpointer user_data)
395 {
396 PuiBackend *self = user_data;
398 g_object_get(pk_control, "network-state", &self->network_state, NULL);
399 g_debug("network state changed: %s",
400 pk_network_enum_to_string(self->network_state));
401 check_inhibit(self);
402 }
404 static void
405 on_updates_changed(PkControl *control, gpointer user_data)
406 {
407 PuiBackend *self = user_data;
409 /*
410 * schedule a check after a short delay so that a rapid succession of
411 * signals is coalesced
412 */
413 if (!self->inhibited) {
414 if (self->periodic_check_id != 0) {
415 g_source_remove(self->periodic_check_id);
416 }
417 self->periodic_check_id =
418 g_timeout_add_seconds(PUI_STARTUP_DELAY, periodic_check,
419 self);
420 }
421 }
423 static void
424 on_restart_schedule(PkControl *control, gpointer user_data)
425 {
426 PuiBackend *self = user_data;
428 g_debug("emitting signal restart-required");
429 g_signal_emit(self, signals[RESTART_REQUIRED], 0);
430 }
432 static void
433 pui_backend_init_async(GAsyncInitable *initable, int io_priority,
434 GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data)
435 {
436 PuiBackend *self = PUI_BACKEND(initable);
437 GTask *task;
439 task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data);
440 g_task_set_priority(task, io_priority);
441 g_task_set_task_data(task, g_object_ref(self),
442 (GDestroyNotify)g_object_unref);
444 pk_control_get_properties_async(self->pk_control, cancellable,
445 on_get_properties_finished, task);
446 }
448 static gboolean
449 pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result,
450 GError **errorp)
451 {
452 PuiBackend *self = PUI_BACKEND(initable);
453 GTask *task = G_TASK(result);
455 if (!g_task_propagate_boolean(task, errorp)) {
456 return (FALSE);
457 }
459 /* get notification when the network state changes */
460 g_signal_connect(self->pk_control, "notify::network-state",
461 G_CALLBACK(on_notify_network_state), self);
462 /* get notifications when the available package updates change */
463 g_signal_connect(self->pk_control, "updates-changed",
464 G_CALLBACK(on_updates_changed), self);
465 /* get notifications when an application restart is required */
466 g_signal_connect(self->pk_control, "restart-schedule",
467 G_CALLBACK(on_restart_schedule), self);
469 check_inhibit(self);
471 return (TRUE);
472 }
474 static void
475 pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data)
476 {
477 GAsyncInitableIface *iface = g_iface;
479 iface->init_async = pui_backend_init_async;
480 iface->init_finish = pui_backend_init_finish;
481 }
483 void
484 pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback,
485 gpointer user_data)
486 {
487 g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT,
488 cancellable, callback, user_data, NULL);
489 }
491 PuiBackend *
492 pui_backend_new_finish(GAsyncResult *result, GError **errorp)
493 {
494 GObject *object;
495 GObject *source_object;
497 source_object = g_async_result_get_source_object(result);
498 object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object),
499 result, errorp);
500 g_object_unref(source_object);
502 return ((object != NULL) ? PUI_BACKEND(object) : NULL);
503 }