From aa60a8261a21826d0079a95598470c4d6b35f2da Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sun, 27 Dec 2015 12:23:13 +0100 Subject: [PATCH] Added GdaExQueryEditor::dispose and ::finalize. Some adjustments. --- src/queryeditor.c | 64 ++++++-- tests/query_editor.c | 356 +++++++++++++++++++++++-------------------- 2 files changed, 244 insertions(+), 176 deletions(-) diff --git a/src/queryeditor.c b/src/queryeditor.c index ca824c6..09aebb7 100644 --- a/src/queryeditor.c +++ b/src/queryeditor.c @@ -69,6 +69,9 @@ enum static void gdaex_query_editor_class_init (GdaExQueryEditorClass *class); static void gdaex_query_editor_init (GdaExQueryEditor *gdaex_query_editor); +static void gdaex_query_editor_dispose (GObject *object); +static void gdaex_query_editor_finalize (GObject *object); + static void gdaex_query_editor_set_property (GObject *object, guint property_id, const GValue *value, @@ -293,6 +296,9 @@ gdaex_query_editor_class_init (GdaExQueryEditorClass *class) object_class->set_property = gdaex_query_editor_set_property; object_class->get_property = gdaex_query_editor_get_property; + object_class->dispose = gdaex_query_editor_dispose; + object_class->finalize = gdaex_query_editor_finalize; + g_type_class_add_private (object_class, sizeof (GdaExQueryEditorPrivate)); } @@ -301,14 +307,6 @@ gdaex_query_editor_init (GdaExQueryEditor *gdaex_query_editor) { GdaExQueryEditorPrivate *priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); - gdaex_query_editor_clean (gdaex_query_editor); - - priv->lstore_link_type = gtk_list_store_new (2, - G_TYPE_UINT, - G_TYPE_STRING); - priv->lstore_where_type = gtk_list_store_new (2, - G_TYPE_UINT, - G_TYPE_STRING); } /** @@ -331,6 +329,15 @@ GdaExQueryEditor priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); + gdaex_query_editor_clean (gdaex_query_editor); + + priv->lstore_link_type = gtk_list_store_new (2, + G_TYPE_UINT, + G_TYPE_STRING); + priv->lstore_where_type = gtk_list_store_new (2, + G_TYPE_UINT, + G_TYPE_STRING); + priv->gdaex = gdaex; /* test if we can use GtkDateEntry */ @@ -2723,21 +2730,52 @@ gdaex_query_editor_get_property (GObject *object, } static void -gdaex_query_editor_clean (GdaExQueryEditor *gdaex_query_editor) +gdaex_query_editor_dispose (GObject *object) { + GdaExQueryEditor *gdaex_query_editor = (GdaExQueryEditor *)object; GdaExQueryEditorPrivate *priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); + gtk_tree_store_clear (priv->tstore_fields); + gtk_list_store_clear (priv->lstore_show); + gtk_tree_store_clear (priv->tstore_where); + gtk_list_store_clear (priv->lstore_order); + if (priv->tables != NULL) { g_hash_table_destroy (priv->tables); + priv->tables = NULL; } - priv->tables = g_hash_table_new (g_str_hash, g_str_equal); - if (GTK_IS_PANED (priv->hpaned_main)) + if (priv->relations != NULL) { - gtk_widget_destroy (priv->hpaned_main); + g_slist_free (priv->relations); + priv->relations = NULL; } - priv->hpaned_main = NULL; + + G_OBJECT_CLASS (gdaex_query_editor_parent_class)->dispose (object); +} + +static void +gdaex_query_editor_finalize (GObject *object) +{ + GdaExQueryEditor *gdaex_query_editor = (GdaExQueryEditor *)object; + GdaExQueryEditorPrivate *priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); + + gtk_widget_destroy (priv->hpaned_main); + + G_OBJECT_CLASS (gdaex_query_editor_parent_class)->finalize (object); +} + +static void +gdaex_query_editor_clean (GdaExQueryEditor *gdaex_query_editor) +{ + GdaExQueryEditorPrivate *priv = GDAEX_QUERY_EDITOR_GET_PRIVATE (gdaex_query_editor); + + if (priv->tables != NULL) + { + g_hash_table_destroy (priv->tables); + } + priv->tables = g_hash_table_new (g_str_hash, g_str_equal); if (priv->relations != NULL) { diff --git a/tests/query_editor.c b/tests/query_editor.c index 22a471b..69ab18a 100644 --- a/tests/query_editor.c +++ b/tests/query_editor.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Andrea Zagli + * Copyright (C) 2011-2015 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -19,8 +19,189 @@ #include #include +GdaExQueryEditor *qe; + +gchar *xmlfile; + +GdaEx *gdaex; + GtkWidget *w; +GtkWidget *widget_qe; +GtkWidget *btn_destroy; +GtkWidget *btn_clean; +GtkWidget *btn_get_sql; +GtkWidget *btn_save_xml; +GtkWidget *btn_load_xml; +GtkWidget *btn_ok; + +GtkWidget *vbox; + +static void +do_query_editor () +{ + GdaExQueryEditorField *field; + + qe = gdaex_query_editor_new (gdaex); + + if (xmlfile != NULL) + { + gdaex_query_editor_load_tables_from_file (qe, xmlfile, TRUE); + } + else + { + gdaex_query_editor_add_table (qe, "clients", "Clients"); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("id"); + field->name_visible = g_strdup ("ID"); + field->type = GDAEX_QE_FIELD_TYPE_INTEGER; + field->for_show = TRUE; + field->always_showed = TRUE; + field->for_where = TRUE; + field->available_where_type = GDAEX_QE_WHERE_TYPE_EQUAL; + gdaex_query_editor_table_add_field (qe, "clients", *field); + g_free (field); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("name"); + field->name_visible = g_strdup ("Name"); + field->description = g_strdup ("The client's name"); + field->type = GDAEX_QE_FIELD_TYPE_TEXT; + field->for_show = TRUE; + field->for_where = TRUE; + field->for_order = TRUE; + field->available_where_type = GDAEX_QE_WHERE_TYPE_STARTS + | GDAEX_QE_WHERE_TYPE_CONTAINS + | GDAEX_QE_WHERE_TYPE_ENDS + | GDAEX_QE_WHERE_TYPE_ISTARTS + | GDAEX_QE_WHERE_TYPE_ICONTAINS + | GDAEX_QE_WHERE_TYPE_IENDS; + gdaex_query_editor_table_add_field (qe, "clients", *field); + g_free (field); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("surname"); + field->name_visible = g_strdup ("Surname"); + field->description = g_strdup ("The client's surname"); + field->type = GDAEX_QE_FIELD_TYPE_TEXT; + field->for_show = TRUE; + field->for_where = TRUE; + field->for_order = TRUE; + field->available_where_type = GDAEX_QE_WHERE_TYPE_STRING + | GDAEX_QE_WHERE_TYPE_IS_NULL; + gdaex_query_editor_table_add_field (qe, "clients", *field); + g_free (field); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("brithday"); + field->name_visible = g_strdup ("Birthday"); + field->description = g_strdup ("The client's birthday"); + field->type = GDAEX_QE_FIELD_TYPE_DATE; + field->for_show = TRUE; + field->for_where = TRUE; + field->for_order = TRUE; + field->available_where_type = GDAEX_QE_WHERE_TYPE_DATETIME + | GDAEX_QE_WHERE_TYPE_IS_NULL; + gdaex_query_editor_table_add_field (qe, "clients", *field); + g_free (field); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("age"); + field->name_visible = g_strdup ("Age"); + field->description = g_strdup ("The client's age"); + field->alias = g_strdup ("client_age"); + field->type = GDAEX_QE_FIELD_TYPE_INTEGER; + field->for_show = TRUE; + field->for_where = TRUE; + field->for_order = TRUE; + field->available_where_type = GDAEX_QE_WHERE_TYPE_NUMBER; + gdaex_query_editor_table_add_field (qe, "clients", *field); + g_free (field); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("datetime"); + field->name_visible = g_strdup ("DateTime"); + field->description = g_strdup ("???"); + field->type = GDAEX_QE_FIELD_TYPE_DATETIME; + field->for_show = TRUE; + field->for_where = TRUE; + field->for_order = TRUE; + field->available_where_type = GDAEX_QE_WHERE_TYPE_DATETIME + | GDAEX_QE_WHERE_TYPE_IS_NULL; + gdaex_query_editor_table_add_field (qe, "clients", *field); + g_free (field); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("id_cities"); + field->name_visible = g_strdup ("City"); + field->description = g_strdup ("The client's city"); + field->type = GDAEX_QE_FIELD_TYPE_INTEGER; + field->for_show = TRUE; + field->for_where = TRUE; + field->for_order = TRUE; + field->decode_table2 = g_strdup ("cities"); + field->decode_join_type = GDAEX_QE_JOIN_TYPE_LEFT; + /*field->decode_fields1 = g_slist_append (field->decode_fields1, "id_cities"); + field->decode_fields2 = g_slist_append (field->decode_fields2, "id");*/ + field->decode_field2 = g_strdup ("id"); + field->decode_field_to_show = g_strdup ("name"); + field->decode_field_alias = g_strdup ("city_name"); + field->available_where_type = GDAEX_QE_WHERE_TYPE_EQUAL; + gdaex_query_editor_table_add_field (qe, "clients", *field); + g_free (field); + + gdaex_query_editor_add_table (qe, "orders", "Orders"); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("id"); + field->type = GDAEX_QE_FIELD_TYPE_INTEGER; + gdaex_query_editor_table_add_field (qe, "orders", *field); + g_free (field); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("id_clients"); + field->type = GDAEX_QE_FIELD_TYPE_INTEGER; + gdaex_query_editor_table_add_field (qe, "orders", *field); + g_free (field); + + gdaex_query_editor_add_relation (qe, + "clients", "orders", + GDAEX_QE_JOIN_TYPE_LEFT, + "id", "id_clients", + NULL); + + field = g_new0 (GdaExQueryEditorField, 1); + field->name = g_strdup ("amount"); + field->name_visible = g_strdup ("Amount"); + field->type = GDAEX_QE_FIELD_TYPE_DOUBLE; + field->for_show = TRUE; + field->for_where = TRUE; + field->for_order = TRUE; + field->available_where_type = GDAEX_QE_WHERE_TYPE_NUMBER; + gdaex_query_editor_table_add_field (qe, "orders", *field); + g_free (field); + } + + widget_qe = gdaex_query_editor_get_widget (qe); + gtk_box_pack_start (GTK_BOX (vbox), widget_qe, TRUE, TRUE, 5); +} + +static void +on_btn_destroy_clicked (GtkButton *button, + gpointer user_data) +{ + if (widget_qe != NULL) + { + g_object_unref (G_OBJECT (qe)); + widget_qe = NULL; + } + else + { + do_query_editor (); + } +} + static void on_btn_clean_clicked (GtkButton *button, gpointer user_data) @@ -188,21 +369,7 @@ main (int argc, char *argv[]) GOptionContext *context; - GdaEx *gdaex; - GdaExQueryEditor *qe; - - GdaExQueryEditorField *field; - - GtkWidget *vbox; - GtkWidget *widget_qe; GtkWidget *hbtnbox; - GtkWidget *btn_clean; - GtkWidget *btn_get_sql; - GtkWidget *btn_save_xml; - GtkWidget *btn_load_xml; - GtkWidget *btn_ok; - - gchar *xmlfile; xmlfile = NULL; @@ -231,147 +398,6 @@ main (int argc, char *argv[]) g_warning ("Error on command line parsing: %s", error->message); } - qe = gdaex_query_editor_new (gdaex); - - if (xmlfile != NULL) - { - gdaex_query_editor_load_tables_from_file (qe, xmlfile, TRUE); - } - else - { - gdaex_query_editor_add_table (qe, "clients", "Clients"); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("id"); - field->name_visible = g_strdup ("ID"); - field->type = GDAEX_QE_FIELD_TYPE_INTEGER; - field->for_show = TRUE; - field->always_showed = TRUE; - field->for_where = TRUE; - field->available_where_type = GDAEX_QE_WHERE_TYPE_EQUAL; - gdaex_query_editor_table_add_field (qe, "clients", *field); - g_free (field); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("name"); - field->name_visible = g_strdup ("Name"); - field->description = g_strdup ("The client's name"); - field->type = GDAEX_QE_FIELD_TYPE_TEXT; - field->for_show = TRUE; - field->for_where = TRUE; - field->for_order = TRUE; - field->available_where_type = GDAEX_QE_WHERE_TYPE_STARTS - | GDAEX_QE_WHERE_TYPE_CONTAINS - | GDAEX_QE_WHERE_TYPE_ENDS - | GDAEX_QE_WHERE_TYPE_ISTARTS - | GDAEX_QE_WHERE_TYPE_ICONTAINS - | GDAEX_QE_WHERE_TYPE_IENDS; - gdaex_query_editor_table_add_field (qe, "clients", *field); - g_free (field); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("surname"); - field->name_visible = g_strdup ("Surname"); - field->description = g_strdup ("The client's surname"); - field->type = GDAEX_QE_FIELD_TYPE_TEXT; - field->for_show = TRUE; - field->for_where = TRUE; - field->for_order = TRUE; - field->available_where_type = GDAEX_QE_WHERE_TYPE_STRING - | GDAEX_QE_WHERE_TYPE_IS_NULL; - gdaex_query_editor_table_add_field (qe, "clients", *field); - g_free (field); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("brithday"); - field->name_visible = g_strdup ("Birthday"); - field->description = g_strdup ("The client's birthday"); - field->type = GDAEX_QE_FIELD_TYPE_DATE; - field->for_show = TRUE; - field->for_where = TRUE; - field->for_order = TRUE; - field->available_where_type = GDAEX_QE_WHERE_TYPE_DATETIME - | GDAEX_QE_WHERE_TYPE_IS_NULL; - gdaex_query_editor_table_add_field (qe, "clients", *field); - g_free (field); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("age"); - field->name_visible = g_strdup ("Age"); - field->description = g_strdup ("The client's age"); - field->alias = g_strdup ("client_age"); - field->type = GDAEX_QE_FIELD_TYPE_INTEGER; - field->for_show = TRUE; - field->for_where = TRUE; - field->for_order = TRUE; - field->available_where_type = GDAEX_QE_WHERE_TYPE_NUMBER; - gdaex_query_editor_table_add_field (qe, "clients", *field); - g_free (field); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("datetime"); - field->name_visible = g_strdup ("DateTime"); - field->description = g_strdup ("???"); - field->type = GDAEX_QE_FIELD_TYPE_DATETIME; - field->for_show = TRUE; - field->for_where = TRUE; - field->for_order = TRUE; - field->available_where_type = GDAEX_QE_WHERE_TYPE_DATETIME - | GDAEX_QE_WHERE_TYPE_IS_NULL; - gdaex_query_editor_table_add_field (qe, "clients", *field); - g_free (field); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("id_cities"); - field->name_visible = g_strdup ("City"); - field->description = g_strdup ("The client's city"); - field->type = GDAEX_QE_FIELD_TYPE_INTEGER; - field->for_show = TRUE; - field->for_where = TRUE; - field->for_order = TRUE; - field->decode_table2 = g_strdup ("cities"); - field->decode_join_type = GDAEX_QE_JOIN_TYPE_LEFT; - /*field->decode_fields1 = g_slist_append (field->decode_fields1, "id_cities"); - field->decode_fields2 = g_slist_append (field->decode_fields2, "id");*/ - field->decode_field2 = g_strdup ("id"); - field->decode_field_to_show = g_strdup ("name"); - field->decode_field_alias = g_strdup ("city_name"); - field->available_where_type = GDAEX_QE_WHERE_TYPE_EQUAL; - gdaex_query_editor_table_add_field (qe, "clients", *field); - g_free (field); - - gdaex_query_editor_add_table (qe, "orders", "Orders"); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("id"); - field->type = GDAEX_QE_FIELD_TYPE_INTEGER; - gdaex_query_editor_table_add_field (qe, "orders", *field); - g_free (field); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("id_clients"); - field->type = GDAEX_QE_FIELD_TYPE_INTEGER; - gdaex_query_editor_table_add_field (qe, "orders", *field); - g_free (field); - - gdaex_query_editor_add_relation (qe, - "clients", "orders", - GDAEX_QE_JOIN_TYPE_LEFT, - "id", "id_clients", - NULL); - - field = g_new0 (GdaExQueryEditorField, 1); - field->name = g_strdup ("amount"); - field->name_visible = g_strdup ("Amount"); - field->type = GDAEX_QE_FIELD_TYPE_DOUBLE; - field->for_show = TRUE; - field->for_where = TRUE; - field->for_order = TRUE; - field->available_where_type = GDAEX_QE_WHERE_TYPE_NUMBER; - gdaex_query_editor_table_add_field (qe, "orders", *field); - g_free (field); - } - w = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_default_size (GTK_WINDOW (w), 610, 400); gtk_window_set_modal (GTK_WINDOW (w), TRUE); @@ -382,39 +408,43 @@ main (int argc, char *argv[]) vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); gtk_container_add (GTK_CONTAINER (w), vbox); - widget_qe = gdaex_query_editor_get_widget (qe); - gtk_box_pack_start (GTK_BOX (vbox), widget_qe, TRUE, TRUE, 5); - hbtnbox = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL); gtk_button_box_set_layout (GTK_BUTTON_BOX (hbtnbox), GTK_BUTTONBOX_END); gtk_box_set_spacing (GTK_BOX (hbtnbox), 5); gtk_box_pack_start (GTK_BOX (vbox), hbtnbox, FALSE, FALSE, 5); + btn_destroy = gtk_button_new_with_mnemonic ("_Destroy"); + gtk_box_pack_start (GTK_BOX (hbtnbox), btn_destroy, TRUE, TRUE, 5); + g_signal_connect (G_OBJECT (btn_destroy), "clicked", + G_CALLBACK (on_btn_destroy_clicked), NULL); + btn_clean = gtk_button_new_from_stock ("gtk-clear"); gtk_box_pack_start (GTK_BOX (hbtnbox), btn_clean, TRUE, TRUE, 5); g_signal_connect (G_OBJECT (btn_clean), "clicked", - G_CALLBACK (on_btn_clean_clicked), qe); + G_CALLBACK (on_btn_clean_clicked), NULL); btn_get_sql = gtk_button_new_with_mnemonic ("Get _Sql"); gtk_box_pack_start (GTK_BOX (hbtnbox), btn_get_sql, TRUE, TRUE, 5); g_signal_connect (G_OBJECT (btn_get_sql), "clicked", - G_CALLBACK (on_btn_get_sql_clicked), qe); + G_CALLBACK (on_btn_get_sql_clicked), NULL); btn_save_xml = gtk_button_new_from_stock ("gtk-save"); gtk_box_pack_start (GTK_BOX (hbtnbox), btn_save_xml, TRUE, TRUE, 5); g_signal_connect (G_OBJECT (btn_save_xml), "clicked", - G_CALLBACK (on_btn_save_xml_clicked), qe); + G_CALLBACK (on_btn_save_xml_clicked), NULL); btn_load_xml = gtk_button_new_from_stock ("gtk-open"); gtk_box_pack_start (GTK_BOX (hbtnbox), btn_load_xml, TRUE, TRUE, 5); g_signal_connect (G_OBJECT (btn_load_xml), "clicked", - G_CALLBACK (on_btn_load_xml_clicked), qe); + G_CALLBACK (on_btn_load_xml_clicked), NULL); btn_ok = gtk_button_new_from_stock ("gtk-close"); gtk_box_pack_start (GTK_BOX (hbtnbox), btn_ok, TRUE, TRUE, 5); g_signal_connect (G_OBJECT (btn_ok), "clicked", gtk_main_quit, NULL); + do_query_editor (); + gtk_widget_show_all (w); gtk_main (); -- 2.49.0