From f9dbe12971960e35a0e5c6b701c6d83d4e199db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sat, 11 Dec 2010 15:46:30 +0100 Subject: [PATCH] Stubplement LdDocument file operations. Prototypes for these functions were wrong; fixed. --- src/ld-document.c | 55 ++++++++++++++++++++++++++++++++++++++++------- src/ld-document.h | 4 ++-- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/ld-document.c b/src/ld-document.c index 54c0ff9..d067d3b 100644 --- a/src/ld-document.c +++ b/src/ld-document.c @@ -229,12 +229,29 @@ ld_document_clear_internal (LdDocument *self) */ gboolean ld_document_load_from_file (LdDocument *self, - const gchar *filename, GError *error) + const gchar *filename, GError **error) { - g_return_val_if_fail (LD_IS_DOCUMENT (self), FALSE); + JsonParser *parser; + GError *json_error; - /* TODO */ - return FALSE; + g_return_val_if_fail (LD_IS_DOCUMENT (self), FALSE); + g_return_val_if_fail (filename != NULL, FALSE); + + /* TODO: Implement loading for real. This is just a stub. */ + parser = json_parser_new (); + + json_error = NULL; + json_parser_load_from_file (parser, filename, &json_error); + if (json_error) + { + g_propagate_error (error, json_error); + g_object_unref (parser); + return FALSE; + } + + ld_document_clear (self); + g_object_unref (parser); + return TRUE; } /** @@ -249,12 +266,34 @@ ld_document_load_from_file (LdDocument *self, */ gboolean ld_document_save_to_file (LdDocument *self, - const gchar *filename, GError *error) + const gchar *filename, GError **error) { - g_return_val_if_fail (LD_IS_DOCUMENT (self), FALSE); + JsonGenerator *generator; + JsonNode *root; + GError *json_error; - /* TODO */ - return FALSE; + g_return_val_if_fail (LD_IS_DOCUMENT (self), FALSE); + g_return_val_if_fail (filename != NULL, FALSE); + + /* TODO: Implement saving for real. This is just a stub. */ + generator = json_generator_new (); + g_object_set (generator, "pretty", TRUE, NULL); + + /* XXX: json-glib dislikes empty objects. */ + root = json_node_new (JSON_NODE_OBJECT); + json_generator_set_root (generator, root); + json_node_free (root); + + json_error = NULL; + json_generator_to_file (generator, filename, &json_error); + if (json_error) + { + g_propagate_error (error, json_error); + g_object_unref (generator); + return FALSE; + } + g_object_unref (generator); + return TRUE; } /** diff --git a/src/ld-document.h b/src/ld-document.h index a27fe23..13602aa 100644 --- a/src/ld-document.h +++ b/src/ld-document.h @@ -60,9 +60,9 @@ GType ld_document_get_type (void) G_GNUC_CONST; LdDocument *ld_document_new (void); void ld_document_clear (LdDocument *self); gboolean ld_document_load_from_file (LdDocument *self, - const gchar *filename, GError *error); + const gchar *filename, GError **error); gboolean ld_document_save_to_file (LdDocument *self, - const gchar *filename, GError *error); + const gchar *filename, GError **error); gboolean ld_document_get_modified (LdDocument *self); void ld_document_set_modified (LdDocument *self, gboolean value);