From: Andrea Zagli Date: Sun, 16 Aug 2020 14:01:52 +0000 (+0200) Subject: Implemented ::delete and fixed ::get_object. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=cc40abeec55d19fb16bee0bcbfcbda761d76b129;p=zakform%2Fjson Implemented ::delete and fixed ::get_object. --- diff --git a/src/form.c b/src/form.c index 1862883..278611c 100644 --- a/src/form.c +++ b/src/form.c @@ -45,7 +45,7 @@ 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 JsonObject *zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements); +static JsonObject *zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements, guint *idx); static gboolean zak_form_json_provider_load (ZakFormIProvider *provider, GPtrArray *elements); static gboolean zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements); @@ -333,7 +333,7 @@ zak_form_json_provider_finalize (GObject *gobject) } static JsonObject -*zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements) +*zak_form_json_provider_get_object (ZakFormIProvider *provider, GPtrArray *elements, guint *idx) { gchar **fields; gchar **values; @@ -348,7 +348,7 @@ static JsonObject gboolean ok; - JsonNode *node; + JsonNode *jnode; JsonArray *ar; JsonObject *obj; @@ -380,11 +380,7 @@ static JsonObject } } - if (k > 0) - { - k--; - } - else + if (k < 1) { /* no key fields */ return NULL; @@ -396,15 +392,13 @@ static JsonObject l = json_array_get_length (ar); for (i = 0; i < l; i++) { - JsonNode *jnode; - jnode = json_array_get_element (ar, i); obj = json_node_get_object (jnode); ok = TRUE; for (j = 0; j < k; j++) { - if (g_strcmp0 (json_object_get_string_member (obj, fields[k]), values[k]) != 0) + if (g_strcmp0 (json_object_get_string_member (obj, fields[j]), values[j]) != 0) { ok = FALSE; break; @@ -413,6 +407,10 @@ static JsonObject if (ok) { + if (idx != NULL) + { + *idx = i; + } break; } } @@ -436,7 +434,7 @@ zak_form_json_provider_load (ZakFormIProvider *provider, GPtrArray *elements) ret = TRUE; - obj = zak_form_json_provider_get_object (provider, elements); + obj = zak_form_json_provider_get_object (provider, elements, NULL); for (i = 0; i < elements->len; i++) { @@ -465,7 +463,7 @@ zak_form_json_provider_insert (ZakFormIProvider *provider, GPtrArray *elements) ret = TRUE; - obj = zak_form_json_provider_get_object (provider, elements); + obj = zak_form_json_provider_get_object (provider, elements, NULL); if (obj == NULL) { @@ -549,36 +547,21 @@ zak_form_json_provider_delete (ZakFormIProvider *provider, GPtrArray *elements) JsonObject *obj; - guint i; - - GError *error; + guint idx; 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, &idx); - for (i = 0; i < elements->len; i++) - { - ZakFormElement *element = (ZakFormElement *)g_ptr_array_index (elements, i); - if (zak_form_element_get_to_save (element)) - { - error = NULL; - /* g_key_file_remove_key (priv->kfile, group, */ - /* zak_form_element_get_name (element), */ - /* &error); */ - - - 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"); */ - /* } */ + if (obj != NULL) + {g_warning ("IDX %d", idx); + json_array_remove_element (json_node_get_array (priv->root), idx); + if (priv->filename != NULL) + { + zak_form_json_provider_write_to_file (ZAK_FORM_JSON_PROVIDER (provider)); } }