Mercurial > projects > package-update-indicator
view pui-backend.c @ 0:6884bb8130ca
Initial revision
author | Guido Berhoerster <guido+pui@berhoerster.name> |
---|---|
date | Sun, 20 May 2018 11:32:57 +0200 |
parents | |
children | 3d72ca76538d |
line wrap: on
line source
/* * Copyright (C) 2018 Guido Berhoerster <guido+pui@berhoerster.name> * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include <errno.h> #include <fcntl.h> #include <packagekit-glib2/packagekit.h> #include <string.h> #include <sys/stat.h> #include <sys/types.h> #include <utime.h> #include "pui-common.h" #include "pui-backend.h" #include "pui-get-updates.h" struct _PuiBackend { GObject parent_instance; PkControl *pk_control; GCancellable *cancellable; gint64 last_check; PkNetworkEnum network_state; guint periodic_check_id; guint refresh_interval; guint important_updates; guint normal_updates; }; static void pui_backend_async_initable_iface_init(gpointer, gpointer); G_DEFINE_TYPE_WITH_CODE(PuiBackend, pui_backend, G_TYPE_OBJECT, G_IMPLEMENT_INTERFACE(G_TYPE_ASYNC_INITABLE, pui_backend_async_initable_iface_init)) enum { STATE_CHANGED, RESTART_REQUIRED, SIGNAL_LAST }; enum { PROP_0, PROP_IMPORTANT_UPDATES, PROP_NORMAL_UPDATES, PROP_REFRESH_INTERVAL, PROP_LAST }; static guint signals[SIGNAL_LAST] = { 0 }; static GParamSpec *properties[PROP_LAST] = { NULL }; static gboolean periodic_check(gpointer); GQuark pui_backend_error_quark(void) { return (g_quark_from_static_string("pui-backend-error-quark")); } static void process_pk_package(gpointer data, gpointer user_data) { PkPackage *package = data; PuiBackend *self = user_data; PkInfoEnum type_info = pk_package_get_info(package); switch (type_info) { case PK_INFO_ENUM_LOW: /* FALLTHROUGH */ case PK_INFO_ENUM_ENHANCEMENT: /* FALLTHROUGH */ case PK_INFO_ENUM_NORMAL: self->normal_updates++; break; case PK_INFO_ENUM_BUGFIX: /* FALLTHROUGH */ case PK_INFO_ENUM_IMPORTANT: /* FALLTHROUGH */ case PK_INFO_ENUM_SECURITY: self->important_updates++; break; default: break; } } static void on_get_updates_finished(GObject *source_object, GAsyncResult *async_result, gpointer user_data) { PuiBackend *self = user_data; GPtrArray *package_list = NULL; GError *error = NULL; guint prev_normal_updates = self->normal_updates; guint prev_important_updates = self->important_updates; package_list = pui_get_updates_finish(async_result, &error); if (package_list == NULL) { if (g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED) || g_error_matches(error, PUI_GET_UPDATES_ERROR, PUI_GET_UPDATES_ERROR_CANCELLED)) { /* cancelled */ g_debug("cancelled checking for updates"); } else { g_warning("failed to check for updates: %s", error->message); } g_error_free(error); goto out; } self->normal_updates = 0; self->important_updates = 0; g_ptr_array_foreach(package_list, process_pk_package, self); g_debug("normal updates: %u, important updates: %u", self->normal_updates, self->important_updates); if (self->normal_updates != prev_normal_updates) { g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_NORMAL_UPDATES]); } if (self->important_updates != prev_important_updates) { g_object_notify_by_pspec(G_OBJECT(self), properties[PROP_IMPORTANT_UPDATES]); } if ((self->normal_updates != prev_normal_updates) || (self->important_updates != prev_important_updates)) { g_debug("emitting signal state-changed"); g_signal_emit(self, signals[STATE_CHANGED], 0); } /* last successful check */ self->last_check = g_get_monotonic_time(); out: /* reschedule periodic check */ if (self->network_state != PK_NETWORK_ENUM_OFFLINE) { self->periodic_check_id = g_timeout_add_seconds(PUI_CHECK_UPDATES_INTERVAL, periodic_check, self); } if (package_list != NULL) { g_ptr_array_unref(package_list); } } static gboolean periodic_check(gpointer user_data) { PuiBackend *self = user_data; g_debug("running periodic check"); pui_get_updates_async(self->pk_control, self->refresh_interval, self->cancellable, on_get_updates_finished, self); /* next periodic check will be scheduled after completion */ self->periodic_check_id = 0; return (G_SOURCE_REMOVE); } static void pui_backend_set_property(GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { PuiBackend *self = PUI_BACKEND(object); switch (property_id) { case PROP_REFRESH_INTERVAL: self->refresh_interval = g_value_get_uint(value); g_debug("property \"refresh-interval\" set to %u", self->refresh_interval); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); break; } } static void pui_backend_get_property(GObject *object, guint property_id, GValue *value, GParamSpec *pspec) { PuiBackend *self = PUI_BACKEND(object); switch (property_id) { case PROP_IMPORTANT_UPDATES: g_value_set_uint(value, self->important_updates); break; case PROP_NORMAL_UPDATES: g_value_set_uint(value, self->normal_updates); break; case PROP_REFRESH_INTERVAL: g_value_set_uint(value, self->refresh_interval); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id, pspec); break; } } static void pui_backend_dispose(GObject *object) { PuiBackend *self = PUI_BACKEND(object); if (self->periodic_check_id != 0) { g_source_remove(self->periodic_check_id); self->periodic_check_id = 0; } if (self->cancellable != NULL) { g_cancellable_cancel(self->cancellable); g_clear_object(&self->cancellable); } if (self->pk_control != NULL) { g_clear_object(&self->pk_control); } G_OBJECT_CLASS(pui_backend_parent_class)->dispose(object); } static void pui_backend_class_init(PuiBackendClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); object_class->set_property = pui_backend_set_property; object_class->get_property = pui_backend_get_property; object_class->dispose = pui_backend_dispose; properties[PROP_IMPORTANT_UPDATES] = g_param_spec_uint("important-updates", "Important updates", "Number of available important updates", 0, G_MAXUINT, 0, G_PARAM_READABLE); properties[PROP_NORMAL_UPDATES] = g_param_spec_uint("normal-updates", "Normal updates", "Number of available normal updates", 0, G_MAXUINT, 0, G_PARAM_READABLE); properties[PROP_REFRESH_INTERVAL] = g_param_spec_uint("refresh-interval", "Refresh interval", "Interval in seconds for refreshing the package cache", 0, G_MAXUINT, PUI_DEFAULT_REFRESH_INTERVAL, G_PARAM_READWRITE); g_object_class_install_properties(object_class, PROP_LAST, properties); signals[STATE_CHANGED] = g_signal_new("state-changed", G_TYPE_FROM_CLASS(object_class), G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL, NULL, NULL, G_TYPE_NONE, 0); signals[RESTART_REQUIRED] = g_signal_new("restart-required", G_TYPE_FROM_CLASS(object_class), G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE | G_SIGNAL_NO_HOOKS, 0, NULL, NULL, NULL, G_TYPE_NONE, 0); } static void pui_backend_init(PuiBackend *self) { self->cancellable = g_cancellable_new(); self->pk_control = pk_control_new(); } static void on_get_properties_finished(GObject *object, GAsyncResult *result, gpointer user_data) { PkControl *control = PK_CONTROL(object); PuiBackend *self; GTask *task = user_data; GError *error = NULL; gchar *backend_name = NULL; PkBitfield roles = 0; gchar *roles_str = NULL; self = g_task_get_task_data(task); if (!pk_control_get_properties_finish(control, result, &error)) { g_task_return_error(task, error); goto out; } /* check whether the backend supports GetUpdates */ g_object_get(control, "backend-name", &backend_name, "roles", &roles, "network-state", &self->network_state, NULL); g_debug("backend: %s", backend_name); roles_str = pk_role_bitfield_to_string(roles); g_debug("roles: %s", roles_str); g_debug("network-state: %s", pk_network_enum_to_string(self->network_state)); if (!pk_bitfield_contain(roles, PK_ROLE_ENUM_GET_UPDATES)) { error = g_error_new(PUI_BACKEND_ERROR, PUI_BACKEND_ERROR_GET_UPDATES_NOT_IMPLEMENTED, "Getting updates is not implemented in the %s PackageKit " "backend", backend_name); g_task_return_error(task, error); goto out; } g_task_return_boolean(task, TRUE); out: g_free(roles_str); g_free(backend_name); g_object_unref(task); } static void on_notify_network_state(PkControl *pk_control, GParamSpec *pspec, gpointer user_data) { PuiBackend *self = user_data; PkNetworkEnum network_state; guint elapsed_time; guint remaining_time; g_object_get(pk_control, "network-state", &network_state, NULL); g_debug("network state changed: %s", pk_network_enum_to_string(network_state)); if ((self->network_state == PK_NETWORK_ENUM_OFFLINE) && (network_state != PK_NETWORK_ENUM_OFFLINE)) { /* schedule periodic checks when coming back online */ elapsed_time = (g_get_monotonic_time() - self->last_check) / G_USEC_PER_SEC; /* * if more time that the check interval has passed since the * last check, schedule a check after a short delay, otherwise * wait until the interval has passed */ remaining_time = (elapsed_time < PUI_CHECK_UPDATES_INTERVAL) ? PUI_CHECK_UPDATES_INTERVAL - elapsed_time : PUI_STARTUP_DELAY; self->periodic_check_id = g_timeout_add_seconds(remaining_time, periodic_check, self); } else if ((self->network_state != PK_NETWORK_ENUM_OFFLINE) && (network_state == PK_NETWORK_ENUM_OFFLINE)) { /* cancel periodic checks while offline */ if (self->periodic_check_id != 0) { g_source_remove(self->periodic_check_id); } } self->network_state = network_state; } static void on_updates_changed(PkControl *control, gpointer user_data) { PuiBackend *self = user_data; /* * schedule a check after a short delay so that a rapid succession of * signals is coalesced */ if (self->network_state != PK_NETWORK_ENUM_OFFLINE) { if (self->periodic_check_id != 0) { g_source_remove(self->periodic_check_id); } self->periodic_check_id = g_timeout_add_seconds(PUI_STARTUP_DELAY, periodic_check, self); } } static void on_restart_schedule(PkControl *control, gpointer user_data) { PuiBackend *self = user_data; g_debug("emitting signal restart-required"); g_signal_emit(self, signals[RESTART_REQUIRED], 0); } static void pui_backend_init_async(GAsyncInitable *initable, int io_priority, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { PuiBackend *self = PUI_BACKEND(initable); GTask *task; task = g_task_new(G_OBJECT(initable), cancellable, callback, user_data); g_task_set_priority(task, io_priority); g_task_set_task_data(task, g_object_ref(self), (GDestroyNotify)g_object_unref); pk_control_get_properties_async(self->pk_control, cancellable, on_get_properties_finished, task); } static gboolean pui_backend_init_finish(GAsyncInitable *initable, GAsyncResult *result, GError **errorp) { PuiBackend *self = PUI_BACKEND(initable); GTask *task = G_TASK(result); if (!g_task_propagate_boolean(task, errorp)) { return (FALSE); } /* get notification when the network state changes */ g_signal_connect(self->pk_control, "notify::network-state", G_CALLBACK(on_notify_network_state), self); /* get notifications when the available package updates change */ g_signal_connect(self->pk_control, "updates-changed", G_CALLBACK(on_updates_changed), self); /* get notifications when an application restart is required */ g_signal_connect(self->pk_control, "restart-schedule", G_CALLBACK(on_restart_schedule), self); /* schedule first check after a small delay */ self->periodic_check_id = g_timeout_add_seconds(PUI_STARTUP_DELAY, periodic_check, self); return (TRUE); } static void pui_backend_async_initable_iface_init(gpointer g_iface, gpointer iface_data) { GAsyncInitableIface *iface = g_iface; iface->init_async = pui_backend_init_async; iface->init_finish = pui_backend_init_finish; } void pui_backend_new_async(GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) { g_async_initable_new_async(PUI_TYPE_BACKEND, G_PRIORITY_DEFAULT, cancellable, callback, user_data, NULL); } PuiBackend * pui_backend_new_finish(GAsyncResult *result, GError **errorp) { GObject *object; GObject *source_object; source_object = g_async_result_get_source_object(result); object = g_async_initable_new_finish(G_ASYNC_INITABLE(source_object), result, errorp); g_object_unref(source_object); return ((object != NULL) ? PUI_BACKEND(object) : NULL); }