From: Andrea Zagli Date: Wed, 28 Sep 2016 16:20:23 +0000 (+0200) Subject: Added function ZakConfi::set_config. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=ef9df329ec0cab1715f58b7fdf4f2d83b9408c85;p=zakconfi%2Flibzakconfi Added function ZakConfi::set_config. --- diff --git a/.gitignore b/.gitignore index 81eacfa..99325c2 100644 --- a/.gitignore +++ b/.gitignore @@ -42,7 +42,6 @@ docs/reference/html/ docs/reference/xml/ libtool stamp-h1 -tests/test POTFILES mkinstalldirs stamp-it @@ -52,6 +51,7 @@ Rules-quot tests/test tests/test_get_configs_list tests/test_add_config +tests/test_set_config *.csv gtk-doc.m4 *.gir diff --git a/plugins/db/plgdb.c b/plugins/db/plgdb.c index a7d72a6..706c239 100644 --- a/plugins/db/plgdb.c +++ b/plugins/db/plgdb.c @@ -96,25 +96,9 @@ zak_confi_db_plugin_set_property (GObject *object, break; case PROP_NAME: - priv->name = g_strdup (g_value_get_string (value)); - sql = g_strdup_printf ("UPDATE configs " - "SET name = '%s' " - "WHERE id = %d", - gdaex_strescape (priv->name, NULL), - priv->id_config); - gdaex_execute (priv->gdaex, sql); - g_free (sql); break; case PROP_DESCRIPTION: - priv->description = g_strdup (g_value_get_string (value)); - sql = g_strdup_printf ("UPDATE configs " - "SET description = '%s' " - "WHERE id = %d", - gdaex_strescape (priv->description, NULL), - priv->id_config); - gdaex_execute (priv->gdaex, sql); - g_free (sql); break; case PROP_ROOT: @@ -550,6 +534,50 @@ static ZakConfiConfi return cc; } +static gboolean +zak_confi_db_plugin_set_config (ZakConfiPluggable *pluggable, + const gchar *name, + const gchar *description) +{ + gboolean ret; + gchar *sql; + + ZakConfiDBPluginPrivate *priv = ZAK_CONFI_DB_PLUGIN_GET_PRIVATE (pluggable); + + g_return_val_if_fail (name != NULL, FALSE); + + ret = TRUE; + + sql = g_strdup_printf ("UPDATE configs" + " SET name = '%s'" + " WHERE id = %d", + gdaex_strescape (name, NULL), + priv->id_config); + if (gdaex_execute (priv->gdaex, sql) < 1) + { + ret = FALSE; + } + g_free (sql); + priv->name = g_strdup (name); + + if (description != NULL) + { + sql = g_strdup_printf ("UPDATE configs" + " SET description = '%s'" + " WHERE id = %d", + gdaex_strescape (description, NULL), + priv->id_config); + if (gdaex_execute (priv->gdaex, sql) < 1) + { + ret = FALSE; + } + g_free (sql); + priv->description = g_strdup (description); + } + + return ret; +} + static ZakConfiKey *zak_confi_db_plugin_add_key (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, const gchar *value) { @@ -931,6 +959,7 @@ zak_confi_pluggable_iface_init (ZakConfiPluggableInterface *iface) iface->path_set_value = zak_confi_db_plugin_path_set_value; iface->get_tree = zak_confi_db_plugin_get_tree; iface->add_config = zak_confi_db_plugin_add_config; + iface->set_config = zak_confi_db_plugin_set_config; iface->add_key = zak_confi_db_plugin_add_key; iface->key_set_key = zak_confi_db_plugin_key_set_key; iface->path_get_confi_key = zak_confi_db_plugin_path_get_confi_key; diff --git a/plugins/file/plgfile.c b/plugins/file/plgfile.c index 79c8510..4634fb9 100644 --- a/plugins/file/plgfile.c +++ b/plugins/file/plgfile.c @@ -91,13 +91,9 @@ zak_confi_file_plugin_set_property (GObject *object, break; case PROP_NAME: - priv->name = g_strdup (g_value_get_string (value)); - zak_confi_file_plugin_path_set_value ((ZakConfiPluggable *)plugin, "/CONFI/name", priv->name); break; case PROP_DESCRIPTION: - priv->description = g_strdup (g_value_get_string (value)); - zak_confi_file_plugin_path_set_value ((ZakConfiPluggable *)plugin, "/CONFI/description", priv->description); break; case PROP_ROOT: @@ -425,6 +421,21 @@ static ZakConfiConfi return NULL; } +static gboolean +zak_confi_file_plugin_set_config (ZakConfiPluggable *pluggable, const gchar *name, const gchar *description) +{ + ZakConfiFilePluginPrivate *priv = ZAK_CONFI_FILE_PLUGIN_GET_PRIVATE (pluggable); + + priv->name = g_strdup (name); + zak_confi_file_plugin_path_set_value (pluggable, "/CONFI/name", priv->name); + + if (description != NULL) + { + priv->description = g_strdup (description); + zak_confi_file_plugin_path_set_value (pluggable, "/CONFI/description", priv->description); + } +} + static ZakConfiKey *zak_confi_file_plugin_add_key (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, const gchar *value) { @@ -595,6 +606,7 @@ zak_confi_pluggable_iface_init (ZakConfiPluggableInterface *iface) iface->path_set_value = zak_confi_file_plugin_path_set_value; iface->get_tree = zak_confi_file_plugin_get_tree; iface->add_config = zak_confi_file_plugin_add_config; + iface->set_config = zak_confi_file_plugin_set_config; iface->add_key = zak_confi_file_plugin_add_key; iface->key_set_key = zak_confi_file_plugin_key_set_key; iface->path_get_confi_key = zak_confi_file_plugin_path_get_confi_key; diff --git a/src/confi.c b/src/confi.c index 7907253..07213c0 100644 --- a/src/confi.c +++ b/src/confi.c @@ -397,6 +397,34 @@ zak_confi_set_root (ZakConfi *confi, const gchar *root) return ret; } +/** + * zak_confi_set_config: + * @confi: a #ZakConfi object. + * @name: + * @description: + * + * Returns: + */ +gboolean +zak_confi_set_config (ZakConfi *confi, + const gchar *name, + const gchar *description) +{ + ZakConfiPrivate *priv = ZAK_CONFI_GET_PRIVATE (confi); + + if (priv->pluggable == NULL) + { + g_warning ("Not initialized."); + return FALSE; + } + else + { + return zak_confi_pluggable_set_config (priv->pluggable, + name, + description); + } +} + /** * zak_confi_add_key: * @confi: a #ZakConfi object. diff --git a/src/confipluggable.c b/src/confipluggable.c index bb6b644..d979258 100644 --- a/src/confipluggable.c +++ b/src/confipluggable.c @@ -61,7 +61,7 @@ zak_confi_pluggable_default_init (ZakConfiPluggableInterface *iface) "Configuraton Name", "The configuration name", "", - G_PARAM_READWRITE)); + G_PARAM_READABLE)); /** * ZakConfiPluggable:description: @@ -72,7 +72,7 @@ zak_confi_pluggable_default_init (ZakConfiPluggableInterface *iface) "Configuraton Description", "The configuration description", "", - G_PARAM_READWRITE)); + G_PARAM_READABLE)); /** * ZakConfiPluggable:root: @@ -192,7 +192,7 @@ GNode } /** - * zak_confi_pluggable_add_confi: + * zak_confi_pluggable_add_config: * @pluggable: a #ZakConfiPluggable object. * @name: the name of the config.. * @description: the config's description. @@ -212,6 +212,27 @@ ZakConfiConfi return iface->add_config (pluggable, name, description); } +/** + * zak_confi_pluggable_set_config: + * @pluggable: a #ZakConfiPluggable object. + * @name: the name of the config.. + * @description: the config's description. + * + * Returns: + */ +gboolean +zak_confi_pluggable_set_config (ZakConfiPluggable *pluggable, const gchar *name, const gchar *description) +{ + ZakConfiPluggableInterface *iface; + + g_return_val_if_fail (ZAK_CONFI_IS_PLUGGABLE (pluggable), FALSE); + + iface = ZAK_CONFI_PLUGGABLE_GET_IFACE (pluggable); + g_return_val_if_fail (iface->set_config != NULL, FALSE); + + return iface->set_config (pluggable, name, description); +} + /** * zak_confi_pluggable_add_key: * @pluggable: a #ZakConfiPluggable object. diff --git a/src/confipluggable.h b/src/confipluggable.h index 5b13341..2ccfa4f 100644 --- a/src/confipluggable.h +++ b/src/confipluggable.h @@ -67,6 +67,9 @@ struct _ZakConfiPluggableInterface { ZakConfiConfi *(*add_config) (ZakConfiPluggable *pluggable, const gchar *name, const gchar *description); + gboolean (*set_config) (ZakConfiPluggable *pluggable, + const gchar *name, + const gchar *description); ZakConfiKey *(*add_key) (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, @@ -98,6 +101,9 @@ GNode *zak_confi_pluggable_get_tree (ZakConfiPluggable *pluggable); ZakConfiConfi *zak_confi_pluggable_add_config (ZakConfiPluggable *pluggable, const gchar *name, const gchar *description); +gboolean zak_confi_pluggable_set_config (ZakConfiPluggable *pluggable, + const gchar *name, + const gchar *description); ZakConfiKey *zak_confi_pluggable_add_key (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, diff --git a/src/libzakconfi.h b/src/libzakconfi.h index 0e7e97c..96ec605 100644 --- a/src/libzakconfi.h +++ b/src/libzakconfi.h @@ -69,6 +69,10 @@ GNode *zak_confi_get_tree (ZakConfi *confi); gchar *zak_confi_normalize_root (const gchar *root); gboolean zak_confi_set_root (ZakConfi *confi, const gchar *root); +gboolean zak_confi_set_config (ZakConfi *confi, + const gchar *name, + const gchar *description); + ZakConfiKey *zak_confi_add_key (ZakConfi *confi, const gchar *parent, const gchar *key, diff --git a/tests/Makefile.am b/tests/Makefile.am index 69ca77d..d07a501 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -11,6 +11,7 @@ LDADD = $(top_builddir)/src/libzakconfi.la noinst_PROGRAMS = test \ test_add_config \ - test_get_configs_list + test_get_configs_list \ + test_set_config EXTRA_DIST = gir.py diff --git a/tests/test_set_config.c b/tests/test_set_config.c new file mode 100644 index 0000000..95b81ff --- /dev/null +++ b/tests/test_set_config.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2005-2016 Andrea Zagli + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include + +#include "libzakconfi.h" + +int +main (int argc, char **argv) +{ + PeasEngine *engine; + PeasPluginInfo *ppinfo; + + ZakConfi *confi; + + if (argc < 4) + { + g_error ("Usage: test_set_config "); + return 0; + } + + engine = peas_engine_get_default (); + peas_engine_add_search_path (engine, "./plugins", NULL); + + confi = zak_confi_new (argv[1]); + + if (confi == NULL + || !zak_confi_set_config (confi, argv[2], argv[3])) + { + g_warning ("Config %s not setted.", argv[1]); + } + + return 0; +}