From d6f8c542f9c7802e1d2b99f1b000d83963b1bfe7 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 28 Sep 2016 17:54:48 +0200 Subject: [PATCH] Added function ZakConfi::add_config. --- .gitignore | 1 + plugins/db/plgdb.c | 32 +++++++++++++++++++++++++++++ plugins/file/plgfile.c | 7 +++++++ src/confi.c | 42 ++++++++++++++++++++++++++++++++++---- src/confipluggable.c | 23 ++++++++++++++++++++- src/confipluggable.h | 6 ++++++ src/libzakconfi.h | 6 +++++- tests/Makefile.am | 1 + tests/test_add_config.c | 45 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 tests/test_add_config.c diff --git a/.gitignore b/.gitignore index bd44a40..81eacfa 100644 --- a/.gitignore +++ b/.gitignore @@ -51,6 +51,7 @@ Rules-quot *.exe tests/test tests/test_get_configs_list +tests/test_add_config *.csv gtk-doc.m4 *.gir diff --git a/plugins/db/plgdb.c b/plugins/db/plgdb.c index 9d01540..a7d72a6 100644 --- a/plugins/db/plgdb.c +++ b/plugins/db/plgdb.c @@ -519,6 +519,37 @@ GNode return node; } +static ZakConfiConfi +*zak_confi_db_plugin_add_config (ZakConfiPluggable *pluggable, const gchar *name, const gchar *description) +{ + ZakConfiConfi *cc; + + gchar *sql; + gint id; + + ZakConfiDBPluginPrivate *priv = ZAK_CONFI_DB_PLUGIN_GET_PRIVATE (pluggable); + + cc = NULL; + + id = gdaex_get_new_id (priv->gdaex, "configs", "id", NULL); + + sql = g_strdup_printf ("INSERT INTO configs" + " VALUES (%d, '%s', '%s')", + id, + gdaex_strescape (name, NULL), + gdaex_strescape (description, NULL)); + if (gdaex_execute (priv->gdaex, sql) > 0) + { + cc = g_new0 (ZakConfiConfi, 1); + cc->name = g_strdup (name); + cc->description = g_strdup (description); + } + + g_free (sql); + + return cc; +} + static ZakConfiKey *zak_confi_db_plugin_add_key (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, const gchar *value) { @@ -899,6 +930,7 @@ zak_confi_pluggable_iface_init (ZakConfiPluggableInterface *iface) iface->path_get_value = zak_confi_db_plugin_path_get_value; 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->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 7382d80..79c8510 100644 --- a/plugins/file/plgfile.c +++ b/plugins/file/plgfile.c @@ -419,6 +419,12 @@ GNode return node; } +static ZakConfiConfi +*zak_confi_file_plugin_add_config (ZakConfiPluggable *pluggable, const gchar *name, const gchar *description) +{ + return NULL; +} + static ZakConfiKey *zak_confi_file_plugin_add_key (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, const gchar *value) { @@ -588,6 +594,7 @@ zak_confi_pluggable_iface_init (ZakConfiPluggableInterface *iface) iface->path_get_value = zak_confi_file_plugin_path_get_value; 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->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 2d5ae68..7907253 100644 --- a/src/confi.c +++ b/src/confi.c @@ -254,16 +254,19 @@ PeasPluginInfo */ GList *zak_confi_get_configs_list (const gchar *cnc_string, - const gchar *filter) + const gchar *filter) { ZakConfiPluggable *pluggable; GList *lst; - lst = NULL; - pluggable = zak_confi_get_confi_pluggable_from_cnc_string (cnc_string); - if (pluggable != NULL) + if (pluggable == NULL) + { + g_warning ("Not initialized."); + lst = NULL; + } + else { lst = zak_confi_pluggable_get_configs_list (pluggable, filter); } @@ -271,6 +274,37 @@ GList return lst; } +/** + * zak_confi_add_config: + * @cnc_string: + * @name: + * @description: + * + * Returns: a #ZakConfiConfi struct. + */ +ZakConfiConfi +*zak_confi_add_config (const gchar *cnc_string, + const gchar *name, + const gchar *description) +{ + ZakConfiPluggable *pluggable; + ZakConfiConfi *cc; + + pluggable = zak_confi_get_confi_pluggable_from_cnc_string (cnc_string); + + if (pluggable == NULL) + { + g_warning ("Not initialized."); + cc = NULL; + } + else + { + cc = zak_confi_pluggable_add_config (pluggable, name, description); + } + + return cc; +} + /** * zak_confi_get_tree: * @confi: a #ZakConfi object. diff --git a/src/confipluggable.c b/src/confipluggable.c index beae829..bb6b644 100644 --- a/src/confipluggable.c +++ b/src/confipluggable.c @@ -191,6 +191,27 @@ GNode return iface->get_tree (pluggable); } +/** + * zak_confi_pluggable_add_confi: + * @pluggable: a #ZakConfiPluggable object. + * @name: the name of the config.. + * @description: the config's description. + * + * Returns: a #ZakConfiConfi struct filled with data from the key just added. + */ +ZakConfiConfi +*zak_confi_pluggable_add_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->add_config != NULL, FALSE); + + return iface->add_config (pluggable, name, description); +} + /** * zak_confi_pluggable_add_key: * @pluggable: a #ZakConfiPluggable object. @@ -198,7 +219,7 @@ GNode * @key: the key's name. * @value: the key's value. * - * Returns: a #ZakConfigKey struct filled with data from the key just added. + * Returns: a #ZakConfiKey struct filled with data from the key just added. */ ZakConfiKey *zak_confi_pluggable_add_key (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, const gchar *value) diff --git a/src/confipluggable.h b/src/confipluggable.h index 1b8fd1c..5b13341 100644 --- a/src/confipluggable.h +++ b/src/confipluggable.h @@ -64,6 +64,9 @@ struct _ZakConfiPluggableInterface { const gchar *path, const gchar *value); GNode *(*get_tree) (ZakConfiPluggable *pluggable); + ZakConfiConfi *(*add_config) (ZakConfiPluggable *pluggable, + const gchar *name, + const gchar *description); ZakConfiKey *(*add_key) (ZakConfiPluggable *pluggable, const gchar *parent, const gchar *key, @@ -92,6 +95,9 @@ gboolean zak_confi_pluggable_path_set_value (ZakConfiPluggable *pluggable, const gchar *path, const gchar *value); GNode *zak_confi_pluggable_get_tree (ZakConfiPluggable *pluggable); +ZakConfiConfi *zak_confi_pluggable_add_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 fb167e9..0e7e97c 100644 --- a/src/libzakconfi.h +++ b/src/libzakconfi.h @@ -58,7 +58,11 @@ ZakConfi *zak_confi_new (const gchar *cnc_string); PeasPluginInfo *zak_confi_get_plugin_info (ZakConfi *confi); GList *zak_confi_get_configs_list (const gchar *cnc_string, - const gchar *filter); + const gchar *filter); + +ZakConfiConfi *zak_confi_add_config (const gchar *cnc_string, + const gchar *name, + const gchar *description); GNode *zak_confi_get_tree (ZakConfi *confi); diff --git a/tests/Makefile.am b/tests/Makefile.am index 0f2cbdc..69ca77d 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -10,6 +10,7 @@ LIBS = $(LIBCONFI_LIBS) \ LDADD = $(top_builddir)/src/libzakconfi.la noinst_PROGRAMS = test \ + test_add_config \ test_get_configs_list EXTRA_DIST = gir.py diff --git a/tests/test_add_config.c b/tests/test_add_config.c new file mode 100644 index 0000000..1778064 --- /dev/null +++ b/tests/test_add_config.c @@ -0,0 +1,45 @@ +/* + * 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; + + if (argc < 4) + { + g_error ("Usage: test_add_config "); + return 0; + } + + engine = peas_engine_get_default (); + peas_engine_add_search_path (engine, "./plugins", NULL); + + if (zak_confi_add_config (argv[1], argv[2], argv[3]) == NULL) + { + g_warning ("Config %s not created.", argv[1]); + } + + return 0; +} -- 2.49.0