]> saetta.ns0.it Git - zakform/json/commitdiff
First saving to file.
authorAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2020 08:46:34 +0000 (10:46 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 9 Aug 2020 08:46:34 +0000 (10:46 +0200)
libzakformjson.pc.in
src/form.c

index 93bdcc416e69b7d7ae11e5145119fc582182a30b..b491991a9af446ae755acaa28dd3c21eb77b646b 100644 (file)
@@ -5,7 +5,7 @@ includedir=@includedir@
 modulesdir=@libdir@/@PACKAGE@/modules
 
 Name: @PACKAGE_NAME@
-Description: Class for ZakFormIProvider based on json file.
+Description: Class for ZakFormIProvider based on json format.
 Version: @PACKAGE_VERSION@
 Requires: libzakform libzakutils json-glib-1.0
 Libs: -L${libdir} -lzakformjson
index 5662d1e8dc5208fe2eaf31c2fce6fd92383ec3fb..a26fa0bde4b355e1ec0722b0ea7ba94fa7a4d31b 100644 (file)
@@ -44,13 +44,15 @@ static void zak_form_json_provider_get_property (GObject *object,
 static void zak_form_json_provider_dispose (GObject *gobject);
 static void zak_form_json_provider_finalize (GObject *gobject);
 
-static gchar *zak_form_json_provider_get_group (GPtrArray *elements);
+static JsonObject *zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements);
 
 static gboolean zak_form_json_provider_load (ZakFormIProvider *provider, GPtrArray *elements);
 static gboolean zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements);
 static gboolean zak_form_json_provider_update (ZakFormIProvider *provider, GPtrArray *elements);
 static gboolean zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements);
 
+static void zak_form_json_provider_write_to_file (ZakFormJsonProvider *provider);
+
 typedef struct
        {
                gchar *filename;
@@ -123,11 +125,18 @@ ZakFormJsonProvider
                        error = NULL;
                        gf = g_file_new_for_path (filename);
                        ostr = g_file_replace (gf, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, &error);
-                       if (ostr != NULL)
+                       if (ostr != NULL
+                           && error == NULL)
                                {
                                        g_output_stream_close (G_OUTPUT_STREAM (ostr), NULL, NULL);
                                        g_object_unref (ostr);
                                }
+                       else
+                               {
+                                       g_warning ("Unable to create file «%s»: %s",
+                                                  filename,
+                                                  error != NULL && error->message != NULL ? error->message : "no details.");
+                               }
                        g_object_unref (gf);
                }
 
@@ -136,17 +145,30 @@ ZakFormJsonProvider
        error = NULL;
        if (!json_parser_load_from_file (parser, filename, &error))
                {
-                       g_warning ("Unable to open json file: «%s»\n. %s",
+                       g_warning ("Unable to open json file: «%s»: %s",
                                   filename,
-                                  error != NULL && error->message != NULL ? error->message : "No details.");
-                       return NULL;
+                                  error != NULL && error->message != NULL ? error->message : "no details.");
                }
 
        zak_form_json_provider = ZAK_FORM_JSON_PROVIDER (g_object_new (zak_form_json_provider_get_type (), NULL));
        priv = zak_form_json_provider_get_instance_private (zak_form_json_provider);
 
+       priv->filename = g_strdup (filename);
        priv->root = json_parser_get_root (parser);
 
+       if (priv->root == NULL)
+               {
+                       JsonArray *jar;
+
+                       g_warning ("Unable to get json root element.");
+
+                       jar = json_array_new ();
+                       priv->root = json_node_new (JSON_NODE_ARRAY);
+                       json_node_init_array (priv->root, jar);
+
+                       zak_form_json_provider_write_to_file (zak_form_json_provider);
+               }
+
        return zak_form_json_provider;
 }
 
@@ -184,6 +206,17 @@ ZakFormJsonProvider
 
        priv->root = json_parser_get_root (parser);
 
+       if (priv->root == NULL)
+               {
+                       JsonArray *jar;
+
+                       g_warning ("Unable to get json root element.");
+
+                       jar = json_array_new ();
+                       priv->root = json_node_new (JSON_NODE_ARRAY);
+                       json_node_init_array (priv->root, jar);
+               }
+
        return zak_form_json_provider;
 }
 
@@ -291,7 +324,7 @@ static JsonObject
 
        node = json_path_match (path, priv->root);
 
-       ar = json_node_get_array (node);;
+       ar = json_node_get_array (node);
        obj = json_array_get_object_element (ar, 0);
 
        return obj;
@@ -333,39 +366,46 @@ zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements)
        GValue *value;
        JsonObject *obj;
 
-       GError *error;
-
        ZakFormJsonProviderPrivate *priv = zak_form_json_provider_get_instance_private (ZAK_FORM_JSON_PROVIDER (provider));
 
        ret = TRUE;
 
-       obj = zak_form_json_provider_get_object (provider, elements);
+       //obj = zak_form_json_provider_get_object (provider, elements);
+       obj = NULL;
+
+       if (obj == NULL)
+               {
+                       JsonNode *jnode;
+
+                       obj = json_object_new ();
+
+                       jnode = json_node_new (JSON_NODE_OBJECT);
+                       json_node_init_object (jnode, obj);
+                       json_array_add_element (json_node_get_array (priv->root), jnode);
+               }
 
        for (i = 0; i < elements->len; i++)
                {
                        ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i);
+
                        if (zak_form_element_get_to_save (element))
                                {
-                                       value = zak_form_element_unformat_gvalue (element, zak_form_element_get_value_gvalue (element));
+                                       JsonNode *jnode;
 
-                                       /* g_key_file_set_string (priv->kfile, group, */
-                                       /*                        zak_form_element_get_name (element), */
-                                       /*                        g_value_get_string (value)); */
-
-                                       error = NULL;
-                                       /* if (!g_key_file_save_to_file (priv->kfile, priv->filename, &error) */
-                                       /*      || error != NULL) */
-                                       /*      { */
-                                       /*              g_warning ("Unable to write to file «%s»: %s.", */
-                                       /*                         priv->filename, */
-                                       /*                         error != NULL && error->message != NULL ? error->message : "no details"); */
-                                       /*      } */
+                                       value = zak_form_element_unformat_gvalue (element, zak_form_element_get_value_gvalue (element));
 
+                                       jnode = json_node_new (JSON_NODE_VALUE);
+                                       json_object_set_member (obj, zak_form_element_get_name (element), json_node_init_string (jnode, g_value_get_string (value)));
 
                                        g_value_unset (value);
                                }
                }
 
+       if (priv->filename != NULL)
+               {
+                       zak_form_json_provider_write_to_file (ZAK_FORM_JSON_PROVIDER  (provider));
+               }
+
        return ret;
 }
 
@@ -417,3 +457,63 @@ zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements)
 
        return ret;
 }
+
+static void
+zak_form_json_provider_write_to_file (ZakFormJsonProvider *provider)
+{
+       GFile *gf;
+       GFileOutputStream *ostr;
+
+       GError *error;
+
+       JsonGenerator *generator;
+
+       ZakFormJsonProviderPrivate *priv = zak_form_json_provider_get_instance_private (ZAK_FORM_JSON_PROVIDER (provider));
+
+       if (priv->filename != NULL)
+               {
+                       /* jsonglib gives error if json is an empty array */
+                       if (json_array_get_length (json_node_get_array (priv->root)) == 0)
+                               {
+                                       /* create the empty file */
+                                       error = NULL;
+                                       gf = g_file_new_for_path (priv->filename);
+                                       ostr = g_file_replace (gf, NULL, FALSE, G_FILE_CREATE_PRIVATE, NULL, &error);
+                                       if (ostr != NULL
+                                           && error == NULL)
+                                               {
+                                                       error = NULL;
+                                                       g_output_stream_write_all (G_OUTPUT_STREAM (ostr),
+                                                                                  "[]",
+                                                                                  3,
+                                                                                  NULL, NULL,
+                                                                                  &error);
+
+                                                       g_output_stream_close (G_OUTPUT_STREAM (ostr), NULL, NULL);
+                                                       g_object_unref (ostr);
+                                               }
+                                       else
+                                               {
+                                                       g_warning ("Unable to create file «%s»: %s",
+                                                                  priv->filename,
+                                                                  error != NULL && error->message != NULL ? error->message : "no details.");
+                                               }
+                               }
+                       else
+                               {
+                                       error = NULL;
+                                       generator = json_generator_new ();
+                                       json_generator_set_root (generator, priv->root);
+
+                                       if (!json_generator_to_file (generator, priv->filename, &error)
+                                           || error != NULL)
+                                               {
+                                                       g_warning ("Unable to write to file «%s»: %s",
+                                                                  priv->filename,
+                                                                  error != NULL && error->message != NULL ? error->message : "no details.");
+                                               }
+
+                                       g_object_unref (generator);
+                               }
+               }
+}