summaryrefslogtreecommitdiff
path: root/libdevkit-power
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2008-08-29 13:20:52 +0100
committerRichard Hughes <richard@hughsie.com>2008-08-29 13:20:52 +0100
commit5e97daacc0e261f8e47f18f99b7a3df7d838efba (patch)
tree6a2ed4bafb6819bc3e6c37f6209bc656b90c78ed /libdevkit-power
parent0b38c52b4f10d5f04c8e1893b334a118ab32d2f2 (diff)
move the savelist code form DkpHistory to EggObjList as it's a common action
Diffstat (limited to 'libdevkit-power')
-rw-r--r--libdevkit-power/egg-obj-list.c127
-rw-r--r--libdevkit-power/egg-obj-list.h4
2 files changed, 131 insertions, 0 deletions
diff --git a/libdevkit-power/egg-obj-list.c b/libdevkit-power/egg-obj-list.c
index 31cd552..56c87ce 100644
--- a/libdevkit-power/egg-obj-list.c
+++ b/libdevkit-power/egg-obj-list.c
@@ -26,6 +26,7 @@
#include <glib/gi18n.h>
#include <glib.h>
+#include "egg-debug.h"
#include "egg-obj-list.h"
#define EGG_OBJ_LIST_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), EGG_TYPE_OBJ_LIST, EggObjListPrivate))
@@ -191,6 +192,132 @@ egg_obj_list_add (EggObjList *list, const gpointer obj)
}
/**
+ * egg_obj_list_to_file:
+ * @list: a valid #EggObjList instance
+ * @filename: a filename
+ *
+ * Saves a copy of the list to a file
+ **/
+gboolean
+egg_obj_list_to_file (EggObjList *list, const gchar *filename)
+{
+ guint i;
+ gpointer obj;
+ gchar *part;
+ GString *string;
+ gboolean ret = TRUE;
+ GError *error = NULL;
+ EggObjListFreeFunc func_free;
+ EggObjListToStringFunc func_to_string;
+
+ g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
+ g_return_val_if_fail (list->priv->func_to_string != NULL, FALSE);
+ g_return_val_if_fail (list->priv->func_free != NULL, FALSE);
+
+ func_free = list->priv->func_free;
+ func_to_string = list->priv->func_to_string;
+
+ /* generate data */
+ string = g_string_new ("");
+ for (i=0; i<list->len; i++) {
+ obj = egg_obj_list_index (list, i);
+ part = func_to_string (obj);
+ if (part == NULL) {
+ ret = FALSE;
+ break;
+ }
+ g_string_append_printf (string, "%s\n", part);
+ g_free (part);
+ }
+ part = g_string_free (string, FALSE);
+
+ /* we failed to convert to string */
+ if (!ret) {
+ egg_warning ("failed to convert");
+ goto out;
+ }
+
+ /* save to disk */
+ ret = g_file_set_contents (filename, part, -1, &error);
+ if (!ret) {
+ egg_warning ("failed to set data: %s", error->message);
+ g_error_free (error);
+ goto out;
+ }
+ egg_debug ("saved %s", filename);
+
+out:
+ g_free (part);
+ return ret;
+}
+
+/**
+ * egg_obj_list_from_file:
+ * @list: a valid #EggObjList instance
+ * @filename: a filename
+ *
+ * Appends the list from a file
+ **/
+gboolean
+egg_obj_list_from_file (EggObjList *list, const gchar *filename)
+{
+ gboolean ret;
+ GError *error = NULL;
+ gchar *data = NULL;
+ gchar **parts = NULL;
+ guint i;
+ guint length;
+ gpointer obj;
+ EggObjListFreeFunc func_free;
+ EggObjListFromStringFunc func_from_string;
+
+ g_return_val_if_fail (EGG_IS_OBJ_LIST (list), FALSE);
+ g_return_val_if_fail (list->priv->func_from_string != NULL, FALSE);
+ g_return_val_if_fail (list->priv->func_free != NULL, FALSE);
+
+ func_free = list->priv->func_free;
+ func_from_string = list->priv->func_from_string;
+
+ /* do we exist */
+ ret = g_file_test (filename, G_FILE_TEST_EXISTS);
+ if (!ret) {
+ egg_debug ("failed to get data from %s as file does not exist", filename);
+ goto out;
+ }
+
+ /* get contents */
+ ret = g_file_get_contents (filename, &data, NULL, &error);
+ if (!ret) {
+ egg_warning ("failed to get data: %s", error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ /* split by line ending */
+ parts = g_strsplit (data, "\n", 0);
+ length = g_strv_length (parts);
+ if (length == 0) {
+ egg_debug ("no data in %s", filename);
+ goto out;
+ }
+
+ /* add valid entries */
+ egg_debug ("loading %i items of data from %s", length, filename);
+ for (i=0; i<length-1; i++) {
+ obj = func_from_string (parts[i]);
+ if (obj != NULL)
+ egg_obj_list_add (list, obj);
+ func_free (obj);
+ }
+
+out:
+ g_strfreev (parts);
+ g_free (data);
+
+ return ret;
+}
+
+/**
* egg_obj_list_index:
* @list: a valid #EggObjList instance
* @id: A #EggPackageId of the item to match
diff --git a/libdevkit-power/egg-obj-list.h b/libdevkit-power/egg-obj-list.h
index b712e55..e2e7071 100644
--- a/libdevkit-power/egg-obj-list.h
+++ b/libdevkit-power/egg-obj-list.h
@@ -68,6 +68,10 @@ void egg_obj_list_set_from_string (EggObjList *list,
EggObjListFromStringFunc func);
void egg_obj_list_clear (EggObjList *list);
void egg_obj_list_print (EggObjList *list);
+gboolean egg_obj_list_to_file (EggObjList *list,
+ const gchar *filename);
+gboolean egg_obj_list_from_file (EggObjList *list,
+ const gchar *filename);
void egg_obj_list_add (EggObjList *list,
const gpointer data);
const gpointer egg_obj_list_index (EggObjList *list,