summaryrefslogtreecommitdiff
path: root/libupower-glib
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2010-03-26 12:49:01 +0000
committerRichard Hughes <richard@hughsie.com>2010-03-26 12:49:01 +0000
commit6d607a406cef0484cab383e36a5e216a19320ec6 (patch)
tree90288937a72be8114bc6f289aa39b0f2b7f7d972 /libupower-glib
parentdaa2d5f3b745e52e9b2a9117d8020e7d4378369a (diff)
downloadupower-6d607a406cef0484cab383e36a5e216a19320ec6.tar.gz
Convert the daemon to using objects from libupower-glib, not devkit-power-gobject
Diffstat (limited to 'libupower-glib')
-rw-r--r--libupower-glib/up-history-item.c77
-rw-r--r--libupower-glib/up-history-item.h4
-rw-r--r--libupower-glib/up-stats-item.c6
3 files changed, 87 insertions, 0 deletions
diff --git a/libupower-glib/up-history-item.c b/libupower-glib/up-history-item.c
index 4aeefa0..082b408 100644
--- a/libupower-glib/up-history-item.c
+++ b/libupower-glib/up-history-item.c
@@ -32,6 +32,7 @@
#include "config.h"
#include <glib.h>
+#include <stdlib.h>
#include "up-history-item.h"
@@ -108,6 +109,26 @@ up_history_item_set_time (UpHistoryItem *history_item, guint time)
}
/**
+ * up_history_item_set_time_to_present:
+ * @history_item: #UpHistoryItem
+ *
+ * Sets the item time to the present value.
+ *
+ * Since: 0.9.1
+ **/
+void
+up_history_item_set_time_to_present (UpHistoryItem *history_item)
+{
+ GTimeVal timeval;
+
+ g_return_if_fail (UP_IS_HISTORY_ITEM (history_item));
+
+ g_get_current_time (&timeval);
+ history_item->priv->time = timeval.tv_sec;
+ g_object_notify (G_OBJECT(history_item), "time");
+}
+
+/**
* up_history_item_get_time:
* @history_item: #UpHistoryItem
*
@@ -155,6 +176,62 @@ up_history_item_get_state (UpHistoryItem *history_item)
}
/**
+ * up_history_item_to_string:
+ * @history_item: #UpHistoryItem
+ *
+ * Converts the history item to a string representation.
+ *
+ * Since: 0.9.1
+ **/
+gchar *
+up_history_item_to_string (UpHistoryItem *history_item)
+{
+ g_return_val_if_fail (UP_IS_HISTORY_ITEM (history_item), NULL);
+ return g_strdup_printf ("%i\t%.3f\t%s",
+ history_item->priv->time,
+ history_item->priv->value,
+ up_device_state_to_string (history_item->priv->state));
+}
+
+/**
+ * up_history_item_set_from_string:
+ * @history_item: #UpHistoryItem
+ *
+ * Converts the history item to a string representation.
+ *
+ * Since: 0.9.1
+ **/
+gboolean
+up_history_item_set_from_string (UpHistoryItem *history_item, const gchar *text)
+{
+ gchar **parts = NULL;
+ guint length;
+ gboolean ret = FALSE;
+
+ g_return_val_if_fail (UP_IS_HISTORY_ITEM (history_item), FALSE);
+ g_return_val_if_fail (text != NULL, FALSE);
+
+ /* split by tab */
+ parts = g_strsplit (text, "\t", 0);
+ length = g_strv_length (parts);
+ if (length != 3) {
+ g_warning ("invalid string: '%s'", text);
+ goto out;
+ }
+
+ /* parse */
+ up_history_item_set_time (history_item, atoi (parts[0]));
+ up_history_item_set_value (history_item, atof (parts[1]));
+ up_history_item_set_state (history_item, up_device_state_from_string (parts[2]));
+
+ /* success */
+ ret = TRUE;
+out:
+ g_strfreev (parts);
+ return ret;
+}
+
+/**
* up_history_item_set_property:
**/
static void
diff --git a/libupower-glib/up-history-item.h b/libupower-glib/up-history-item.h
index a573590..8378060 100644
--- a/libupower-glib/up-history-item.h
+++ b/libupower-glib/up-history-item.h
@@ -60,9 +60,13 @@ void up_history_item_set_value (UpHistoryItem *history_item,
guint up_history_item_get_time (UpHistoryItem *history_item);
void up_history_item_set_time (UpHistoryItem *history_item,
guint time);
+void up_history_item_set_time_to_present (UpHistoryItem *history_item);
UpDeviceState up_history_item_get_state (UpHistoryItem *history_item);
void up_history_item_set_state (UpHistoryItem *history_item,
UpDeviceState state);
+gchar *up_history_item_to_string (UpHistoryItem *history_item);
+gboolean up_history_item_set_from_string (UpHistoryItem *history_item,
+ const gchar *text);
G_END_DECLS
diff --git a/libupower-glib/up-stats-item.c b/libupower-glib/up-stats-item.c
index 2aac171..89b319c 100644
--- a/libupower-glib/up-stats-item.c
+++ b/libupower-glib/up-stats-item.c
@@ -96,6 +96,12 @@ void
up_stats_item_set_accuracy (UpStatsItem *stats_item, gdouble accuracy)
{
g_return_if_fail (UP_IS_STATS_ITEM (stats_item));
+
+ /* constrain */
+ if (accuracy < 0.0f)
+ accuracy = 0.0f;
+ else if (accuracy > 100.0f)
+ accuracy = 100.0f;
stats_item->priv->accuracy = accuracy;
g_object_notify (G_OBJECT(stats_item), "accuracy");
}