diff options
author | Benjamin Berg <bberg@redhat.com> | 2022-06-21 18:11:23 +0200 |
---|---|---|
committer | Benjamin Berg <bberg@redhat.com> | 2022-06-22 18:37:46 +0200 |
commit | 46b5eb5fe7e8e904f3c1d3ee762105a8f123925e (patch) | |
tree | db941796a504eac2227bc7647f6304140d5a8ddb | |
parent | 8817dec73f77686a514529b69b761b90590108c5 (diff) | |
download | upower-46b5eb5fe7e8e904f3c1d3ee762105a8f123925e.tar.gz |
device: Reload history when the ID changes
When a battery is swapped the old history needs to be saved and the
other history should be loaded.
Change the code to load the history lazily when needed. Then, to reload,
we purely need to clear the history object and it'll be loaded again
when required.
-rw-r--r-- | src/up-device-battery.c | 3 | ||||
-rw-r--r-- | src/up-device.c | 45 |
2 files changed, 31 insertions, 17 deletions
diff --git a/src/up-device-battery.c b/src/up-device-battery.c index c3dc667..6df5f7f 100644 --- a/src/up-device-battery.c +++ b/src/up-device-battery.c @@ -419,9 +419,6 @@ up_device_battery_update_info (UpDeviceBattery *self, UpBatteryInfo *info) "has-statistics", TRUE, NULL); - /* FIXME: The history needs to be re-loaded at this - * point as the ID may have changed! - */ priv->present = TRUE; priv->units = info->units; } diff --git a/src/up-device.c b/src/up-device.c index 9ba1317..c974529 100644 --- a/src/up-device.c +++ b/src/up-device.c @@ -67,6 +67,8 @@ static GParamSpec *properties[N_PROPS]; #define UP_DEVICES_DBUS_PATH "/org/freedesktop/UPower/devices" +static gchar * up_device_get_id (UpDevice *device); + /* This needs to be called when one of those properties changes: * state * power_supply @@ -161,11 +163,28 @@ update_icon_name (UpDevice *device) } static void +ensure_history (UpDevice *device) +{ + UpDevicePrivate *priv = up_device_get_instance_private (device); + g_autofree char *id = NULL; + + if (priv->history) + return; + + priv->history = up_history_new (); + id = up_device_get_id (device); + if (id) + up_history_set_id (priv->history, id); +} + +static void update_history (UpDevice *device) { UpDevicePrivate *priv = up_device_get_instance_private (device); UpExportedDevice *skeleton = UP_EXPORTED_DEVICE (device); + ensure_history (device); + /* save new history */ up_history_set_state (priv->history, up_exported_device_get_state (skeleton)); up_history_set_charge_data (priv->history, up_exported_device_get_percentage (skeleton)); @@ -189,6 +208,12 @@ up_device_notify (GObject *object, GParamSpec *pspec) if (g_strcmp0 (pspec->name, "type") == 0 || g_strcmp0 (pspec->name, "is-present") == 0) { update_icon_name (device); + /* Clearing the history object will force lazily loading. */ + g_clear_object (&priv->history); + } else if (g_strcmp0 (pspec->name, "vendor") == 0 || + g_strcmp0 (pspec->name, "model") == 0 || + g_strcmp0 (pspec->name, "serial") == 0) { + g_clear_object (&priv->history); } else if (g_strcmp0 (pspec->name, "power-supply") == 0 || g_strcmp0 (pspec->name, "time-to-empty") == 0) { update_warning_level (device); @@ -431,7 +456,6 @@ up_device_initable_init (GInitable *initable, UpDevicePrivate *priv = up_device_get_instance_private (device); const gchar *native_path = "DisplayDevice"; UpDeviceClass *klass = UP_DEVICE_GET_CLASS (device); - gchar *id = NULL; int ret; g_return_val_if_fail (UP_IS_DEVICE (device), FALSE); @@ -466,13 +490,6 @@ up_device_initable_init (GInitable *initable, goto register_device; } - /* get the id so we can load the old history */ - id = up_device_get_id (device); - if (id != NULL) { - up_history_set_id (priv->history, id); - g_free (id); - } - register_device: /* put on the bus */ up_device_register_device (device); @@ -505,6 +522,8 @@ up_device_get_statistics (UpExportedDevice *skeleton, goto out; } + ensure_history (device); + /* get the correct data */ if (g_strcmp0 (type, "charging") == 0) array = up_history_get_profile_data (priv->history, TRUE); @@ -578,8 +597,10 @@ up_device_get_history (UpExportedDevice *skeleton, type = UP_HISTORY_TYPE_TIME_EMPTY; /* something recognised */ - if (type != UP_HISTORY_TYPE_UNKNOWN) + if (type != UP_HISTORY_TYPE_UNKNOWN) { + ensure_history (device); array = up_history_get_data (priv->history, type, timespan, resolution); + } /* maybe the device doesn't have any history */ if (array == NULL) { @@ -669,12 +690,8 @@ up_device_get_native (UpDevice *device) static void up_device_init (UpDevice *device) { - UpDevicePrivate *priv = up_device_get_instance_private (device); UpExportedDevice *skeleton; - priv = up_device_get_instance_private (device); - priv->history = up_history_new (); - skeleton = UP_EXPORTED_DEVICE (device); up_exported_device_set_battery_level (skeleton, UP_DEVICE_LEVEL_NONE); @@ -691,7 +708,7 @@ up_device_finalize (GObject *object) g_clear_object (&priv->native); g_clear_object (&priv->daemon); - g_object_unref (priv->history); + g_clear_object (&priv->history); G_OBJECT_CLASS (up_device_parent_class)->finalize (object); } |