From c7ae441b7e9f2d4c102c2c39cc249721ac71f9ad Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 27 Feb 2011 10:15:36 +0100 Subject: [PATCH] Added test for query editor. Added gui loading. Implemented functions GdaExQueryEditor::add_table and GdaExQueryEditor::table_add_field. --- .gitignore | 4 +- data/libgdaex/gui/libgdaex.ui | 11 +++- src/Makefile.am | 3 + src/gdaex.c | 58 ++++++++++++++++++ src/gdaex.h | 5 ++ src/queryeditor.c | 109 +++++++++++++++++++++++++++++++++- src/queryeditor.h | 9 ++- tests/Makefile.am | 3 +- tests/query_editor.c | 57 ++++++++++++++++++ 9 files changed, 251 insertions(+), 8 deletions(-) create mode 100644 tests/query_editor.c diff --git a/.gitignore b/.gitignore index 2b69f32..6c6b345 100644 --- a/.gitignore +++ b/.gitignore @@ -32,4 +32,6 @@ tests/.deps/ *.la *.bak libgdaex*tar* -tests/test_prefix* +tests/test_prefix +tests/query_editor +tests/*.exe diff --git a/data/libgdaex/gui/libgdaex.ui b/data/libgdaex/gui/libgdaex.ui index 2843836..43599b9 100644 --- a/data/libgdaex/gui/libgdaex.ui +++ b/data/libgdaex/gui/libgdaex.ui @@ -46,18 +46,25 @@ - + 5 + Query Editor + True + center-on-parent + 600 + True normal True vertical - 2 + 5 True True + 200 + True True diff --git a/src/Makefile.am b/src/Makefile.am index d35f53d..fd7f6c4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,8 @@ +guidir = $(datadir)/$(PACKAGE)/gui + AM_CPPFLAGS = -I$(top_srcdir) \ $(GDAEX_CFLAGS) \ + -DGUIDIR=\""$(guidir)"\" \ -DG_LOG_DOMAIN=\"GdaEx\" LIBS = $(GDAEX_LIBS) diff --git a/src/gdaex.c b/src/gdaex.c index 692a52f..26b9842 100644 --- a/src/gdaex.c +++ b/src/gdaex.c @@ -66,6 +66,10 @@ struct _GdaExPrivate gchar *tables_name_prefix; + const gchar *guidir; + const gchar *guifile; + GtkBuilder *gtkbuilder; + guint debug; GFileOutputStream *log_file; }; @@ -132,6 +136,36 @@ static GdaEx GdaExPrivate *priv = GDAEX_GET_PRIVATE (gdaex); + /* gui */ +#ifdef G_OS_WIN32 + + gchar *moddir; + gchar *p; + + moddir = g_win32_get_package_installation_directory_of_module (NULL); + + p = g_strrstr (moddir, g_strdup_printf ("%c", G_DIR_SEPARATOR)); + if (p != NULL + && (g_ascii_strcasecmp (p + 1, "src") == 0 + || g_ascii_strcasecmp (p + 1, ".libs") == 0)) + { + priv->guidir = g_strdup (GUIDIR); + } + else + { + priv->guidir = g_build_filename (moddir, "share", PACKAGE, "gui", NULL); + } + +#else + + priv->guidir = g_strdup (GUIDIR); + +#endif + + priv->guifile = g_build_filename (priv->guidir, "libgdaex.ui", NULL); + + priv->gtkbuilder = gtk_builder_new (); + return gdaex; } @@ -1568,6 +1602,30 @@ gdaex_get_chr_quoting (GdaEx *gdaex) return chr; } +const gchar +*gdaex_get_guifile (GdaEx *gdaex) +{ + GdaExPrivate *priv; + + g_return_val_if_fail (IS_GDAEX (gdaex), NULL); + + priv = GDAEX_GET_PRIVATE (gdaex); + + return (const gchar *)g_strdup (priv->guifile); +} + +GtkBuilder +*gdaex_get_gtkbuilder (GdaEx *gdaex) +{ + GdaExPrivate *priv; + + g_return_val_if_fail (IS_GDAEX (gdaex), NULL); + + priv = GDAEX_GET_PRIVATE (gdaex); + + return priv->gtkbuilder; +} + /* PRIVATE */ static void gdaex_create_connection_parser (GdaEx *gdaex) diff --git a/src/gdaex.h b/src/gdaex.h index 078a69e..ad3f443 100644 --- a/src/gdaex.h +++ b/src/gdaex.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #ifndef __GDAEX_H__ @@ -138,6 +139,10 @@ gchar *gdaex_strescape (const gchar *source, const gchar *exceptions); gchar gdaex_get_chr_quoting (GdaEx *gdaex); +const gchar *gdaex_get_guifile (GdaEx *gdaex); + +GtkBuilder *gdaex_get_gtkbuilder (GdaEx *gdaex); + G_END_DECLS diff --git a/src/queryeditor.c b/src/queryeditor.c index 84d858b..d37e65b 100644 --- a/src/queryeditor.c +++ b/src/queryeditor.c @@ -50,7 +50,10 @@ struct _GdaExQueryEditorPrivate { GdaEx *gdaex; - GtkWidget *hbx_main; + GtkBuilder *gtkbuilder; + + GtkWidget *dialog; + GtkWidget *hpaned_main; GHashTable *tables; /* GdaExQueryEditorTable */ }; @@ -113,6 +116,11 @@ static void gdaex_query_editor_init (GdaExQueryEditor *gdaex_query_editor) { GdaExQueryEditorPrivate *priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); + + priv->tables = g_hash_table_new (g_str_hash, g_str_equal); + + priv->dialog = NULL; + priv->hpaned_main = NULL; } /** @@ -137,17 +145,114 @@ GdaExQueryEditor priv->gdaex = gdaex; + priv->gtkbuilder = gdaex_get_gtkbuilder (priv->gdaex); + + error = NULL; + gtk_builder_add_objects_from_file (priv->gtkbuilder, + gdaex_get_guifile (priv->gdaex), + g_strsplit ("tstore_fields" + "|lstore_show" + "|tstore_where" + "|lstore_order" + "|diag_query_editor", + "|", -1), + &error); + if (error != NULL) + { + g_warning ("Error on gui initialization: %s.", + error->message != NULL ? error->message : "no details"); + return NULL; + } + return gdaex_query_editor; } +GtkWidget +*gdaex_query_editor_get_dialog (GdaExQueryEditor *gdaex_query_editor) +{ + GdaExQueryEditorPrivate *priv; + + g_return_if_fail (GDAEX_IS_QUERY_EDITOR (gdaex_query_editor)); + + priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); + + if (priv->dialog == NULL) + { + priv->dialog = GTK_WIDGET (gtk_builder_get_object (priv->gtkbuilder, "diag_query_editor")); + } + + return priv->dialog; +} + GtkWidget *gdaex_query_editor_get_widget (GdaExQueryEditor *gdaex_query_editor) { GdaExQueryEditorPrivate *priv; + g_return_if_fail (GDAEX_IS_QUERY_EDITOR (gdaex_query_editor)); + priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); - return priv->hbx_main; + if (priv->hpaned_main == NULL) + { + priv->hpaned_main = GTK_WIDGET (gtk_builder_get_object (priv->gtkbuilder, "hpaned1")); + } + + return priv->hpaned_main; +} + +gboolean +gdaex_query_editor_add_table (GdaExQueryEditor *qe, + const gchar *table_name, + const gchar *table_name_visibile) +{ + gboolean ret; + + GdaExQueryEditorPrivate *priv; + GdaExQueryEditorTable *table; + + g_return_val_if_fail (GDAEX_IS_QUERY_EDITOR (qe), FALSE); + + priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe); + + table = g_new0 (GdaExQueryEditorTable, 1); + table->name = g_strstrip (g_strdup (table_name)); + table->name_visible = g_strstrip (g_strdup (table_name_visibile)); + table->fields = g_hash_table_new (g_str_hash, g_str_equal); + + g_hash_table_insert (priv->tables, table->name, table); + + ret = TRUE; + + return ret; +} + +gboolean +gdaex_query_editor_table_add_field (GdaExQueryEditor *qe, + const gchar *table_name, + GdaExQueryEditorField field) +{ + gboolean ret; + + GdaExQueryEditorPrivate *priv; + GdaExQueryEditorTable *table; + + g_return_val_if_fail (GDAEX_IS_QUERY_EDITOR (qe), FALSE); + + priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (qe); + + table = g_hash_table_lookup (priv->tables, table_name); + if (table == NULL) + { + g_warning ("Table «%s» doesn't exists.", table_name); + return FALSE; + } + + g_hash_table_insert (table->fields, field.name, g_memdup (&field, sizeof (GdaExQueryEditorField))); + + ret = TRUE; + + return ret; } /* PRIVATE */ diff --git a/src/queryeditor.h b/src/queryeditor.h index 9b83334..1bdb554 100644 --- a/src/queryeditor.h +++ b/src/queryeditor.h @@ -55,6 +55,7 @@ GType gdaex_query_editor_get_type (void) G_GNUC_CONST; GdaExQueryEditor *gdaex_query_editor_new (GdaEx *gdaex); +GtkWidget *gdaex_query_editor_get_dialog (GdaExQueryEditor *gdaex_query_editor); GtkWidget *gdaex_query_editor_get_widget (GdaExQueryEditor *gdaex_query_editor); typedef struct @@ -68,8 +69,12 @@ typedef struct /* ma ci deve essere la possibilità di fare ricerche anche sulle decodifiche) */ } GdaExQueryEditorField; -gboolean gdaex_query_editor_add_table (GdaExQueryEditor *qe, const gchar *table_name, const gchar *table_name_visibile); -gboolean gdaex_query_editor_table_add_field (GdaExQueryEditor *qe, const gchar *table_name, GdaExQueryEditorField field); +gboolean gdaex_query_editor_add_table (GdaExQueryEditor *qe, + const gchar *table_name, + const gchar *table_name_visibile); +gboolean gdaex_query_editor_table_add_field (GdaExQueryEditor *qe, + const gchar *table_name, + GdaExQueryEditorField field); gboolean gdaex_query_editor_add_relation (GdaExQueryEditor *qe, const gchar *table_name1, GdaExQueryEditorField field1, const gchar *table_name2, GdaExQueryEditorField field2); diff --git a/tests/Makefile.am b/tests/Makefile.am index 403f842..1b7cac7 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -5,7 +5,8 @@ AM_CPPFLAGS = $(GDAEX_CFLAGS) \ -I$(top_srcdir)/src \ -DTESTSDIR="\"@abs_builddir@\"" -noinst_PROGRAMS = test_prefix +noinst_PROGRAMS = test_prefix \ + query_editor LDADD = $(top_builddir)/src/libgdaex.la diff --git a/tests/query_editor.c b/tests/query_editor.c new file mode 100644 index 0000000..c4cc731 --- /dev/null +++ b/tests/query_editor.c @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2011 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include + +int +main (int argc, char *argv[]) +{ + GError *error; + + GOptionContext *context; + + GdaEx *gdaex; + GdaExQueryEditor *qe; + + gtk_init (&argc, &argv); + + gdaex = gdaex = gdaex_new_from_string (g_strdup_printf ("SQLite://DB_DIR=%s;DB_NAME=test_prefix.db", TESTSDIR)); + if (gdaex == NULL) + { + g_error ("Error on GdaEx initialization."); + return 0; + } + + error = NULL; + context = g_option_context_new ("tests"); + g_option_context_add_group (context, gdaex_get_option_group (gdaex)); + g_option_context_parse (context, &argc, &argv, &error); + if (error != NULL) + { + g_warning ("Error on command line parsing: %s", error->message); + } + + qe = gdaex_query_editor_new (gdaex); + + gtk_dialog_run (GTK_DIALOG (gdaex_query_editor_get_dialog (qe))); + + gtk_main (); + + return 0; +} -- 2.49.0