]> saetta.ns0.it Git - zakaudit/gui/commitdiff
Started table management.
authorAndrea Zagli <azagli@libero.it>
Sat, 14 Aug 2010 15:59:11 +0000 (17:59 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 14 Aug 2010 15:59:11 +0000 (17:59 +0200)
data/audit-gui/gui/audit-gui.gui
src/Makefile.am
src/datasources.c
src/table.c [new file with mode: 0644]
src/table.h [new file with mode: 0644]

index da6ed8742e29d7a24c8420c1af92f29b03491d3c..ba45c0e8636e2b1187f47450e61696ac232b9d36 100644 (file)
       </object>
     </child>
   </object>
-  <object class="GtkTreeStore" id="tstore_tables_fields">
-    <columns>
-      <!-- column-name id_table -->
-      <column type="guint"/>
-      <!-- column-name name_table -->
-      <column type="gchararray"/>
-      <!-- column-name id_field -->
-      <column type="guint"/>
-      <!-- column-name name_field -->
-      <column type="gchararray"/>
-      <!-- column-name choosen -->
-      <column type="gboolean"/>
-    </columns>
-  </object>
   <object class="GtkTreeStore" id="tstore_ds_tables_fields">
     <columns>
       <!-- column-name id_ds -->
       <column type="gchararray"/>
       <!-- column-name choosen -->
       <column type="gboolean"/>
-      <!-- column-name choosen_visible -->
+    </columns>
+  </object>
+  <object class="GtkListStore" id="lstore_fields">
+    <columns>
+      <!-- column-name id -->
+      <column type="guint"/>
+      <!-- column-name name -->
+      <column type="gchararray"/>
+      <!-- column-name choosen -->
       <column type="gboolean"/>
     </columns>
   </object>
+  <object class="GtkListStore" id="lstore_tables">
+    <columns>
+      <!-- column-name name -->
+      <column type="gchararray"/>
+    </columns>
+  </object>
   <object class="GtkWindow" id="w_table">
     <property name="title" translatable="yes">Table - Audit GUI</property>
     <property name="modal">True</property>
     <property name="window_position">center-on-parent</property>
+    <property name="default_width">450</property>
+    <property name="default_height">300</property>
     <property name="destroy_with_parent">True</property>
     <child>
       <object class="GtkVBox" id="vbox4">
         <property name="border_width">5</property>
         <property name="orientation">vertical</property>
         <property name="spacing">5</property>
+        <child>
+          <object class="GtkTable" id="table2">
+            <property name="visible">True</property>
+            <property name="n_rows">3</property>
+            <property name="n_columns">2</property>
+            <property name="column_spacing">5</property>
+            <property name="row_spacing">5</property>
+            <child>
+              <object class="GtkLabel" id="label5">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">Table</property>
+              </object>
+              <packing>
+                <property name="top_attach">1</property>
+                <property name="bottom_attach">2</property>
+                <property name="x_options">GTK_FILL</property>
+                <property name="y_options">GTK_FILL</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="label6">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">ID</property>
+              </object>
+              <packing>
+                <property name="x_options">GTK_FILL</property>
+                <property name="y_options">GTK_FILL</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="label7">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">lbl_id</property>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="right_attach">2</property>
+                <property name="y_options">GTK_FILL</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkLabel" id="label8">
+                <property name="label" translatable="yes">lbl_id_datasources</property>
+              </object>
+              <packing>
+                <property name="top_attach">2</property>
+                <property name="bottom_attach">3</property>
+                <property name="x_options">GTK_FILL</property>
+                <property name="y_options">GTK_FILL</property>
+              </packing>
+            </child>
+            <child>
+              <placeholder/>
+            </child>
+            <child>
+              <object class="GtkComboBox" id="combobox1">
+                <property name="visible">True</property>
+                <property name="model">lstore_tables</property>
+                <child>
+                  <object class="GtkCellRendererText" id="cellrenderertext1"/>
+                  <attributes>
+                    <attribute name="text">0</attribute>
+                  </attributes>
+                </child>
+              </object>
+              <packing>
+                <property name="left_attach">1</property>
+                <property name="right_attach">2</property>
+                <property name="top_attach">1</property>
+                <property name="bottom_attach">2</property>
+                <property name="y_options">GTK_FILL</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
         <child>
           <object class="GtkScrolledWindow" id="scrolledwindow1">
             <property name="visible">True</property>
               <object class="GtkTreeView" id="treeview1">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
-                <property name="model">tstore_tables_fields</property>
+                <property name="model">lstore_fields</property>
                 <child>
-                  <object class="GtkTreeViewColumn" id="treeviewcolumn1">
-                    <property name="title">Table</property>
+                  <object class="GtkTreeViewColumn" id="treeviewcolumn2">
+                    <property name="title">Fields</property>
                     <child>
-                      <object class="GtkCellRendererText" id="cellrenderertext1"/>
+                      <object class="GtkCellRendererText" id="cellrenderertext2"/>
                       <attributes>
                         <attribute name="text">1</attribute>
                       </attributes>
                   </object>
                 </child>
                 <child>
-                  <object class="GtkTreeViewColumn" id="treeviewcolumn2">
-                    <property name="title">Field</property>
-                    <child>
-                      <object class="GtkCellRendererText" id="cellrenderertext2"/>
-                      <attributes>
-                        <attribute name="text">3</attribute>
-                      </attributes>
-                    </child>
+                  <object class="GtkTreeViewColumn" id="treeviewcolumn1">
+                    <property name="title">Under audit</property>
                     <child>
                       <object class="GtkCellRendererToggle" id="cellrenderertoggle1"/>
                       <attributes>
-                        <attribute name="activatable">4</attribute>
+                        <attribute name="activatable">2</attribute>
                       </attributes>
                     </child>
                   </object>
             </child>
           </object>
           <packing>
-            <property name="position">0</property>
+            <property name="position">1</property>
           </packing>
         </child>
         <child>
           </object>
           <packing>
             <property name="expand">False</property>
-            <property name="position">1</property>
+            <property name="position">2</property>
           </packing>
         </child>
       </object>
                 <property name="search_column">0</property>
                 <child>
                   <object class="GtkTreeViewColumn" id="treeviewcolumn5">
-                    <property name="title">Datasource</property>
+                    <property name="title">Objects</property>
                     <child>
                       <object class="GtkCellRendererText" id="cellrenderertext5"/>
                       <attributes>
+                        <attribute name="visible">0</attribute>
                         <attribute name="text">1</attribute>
                       </attributes>
                     </child>
                     <child>
                       <object class="GtkCellRendererText" id="cellrenderertext6"/>
                       <attributes>
+                        <attribute name="visible">0</attribute>
                         <attribute name="text">2</attribute>
                       </attributes>
                     </child>
-                  </object>
-                </child>
-                <child>
-                  <object class="GtkTreeViewColumn" id="treeviewcolumn3">
-                    <property name="title">Table</property>
                     <child>
                       <object class="GtkCellRendererText" id="cellrenderertext3"/>
                       <attributes>
+                        <attribute name="visible">3</attribute>
                         <attribute name="text">4</attribute>
                       </attributes>
                     </child>
-                  </object>
-                </child>
-                <child>
-                  <object class="GtkTreeViewColumn" id="treeviewcolumn4">
-                    <property name="title">Field</property>
                     <child>
                       <object class="GtkCellRendererText" id="cellrenderertext4"/>
                       <attributes>
+                        <attribute name="visible">5</attribute>
                         <attribute name="text">6</attribute>
                       </attributes>
                     </child>
-                    <child>
-                      <object class="GtkCellRendererToggle" id="cellrenderertoggle2"/>
-                      <attributes>
-                        <attribute name="visible">8</attribute>
-                        <attribute name="activatable">7</attribute>
-                      </attributes>
-                    </child>
                   </object>
                 </child>
               </object>
             <property name="layout_style">end</property>
             <child>
               <object class="GtkButton" id="button7">
-                <property name="label">gtk-new</property>
+                <property name="label">New _Datasource</property>
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
                 <property name="receives_default">True</property>
-                <property name="use_stock">True</property>
+                <property name="use_underline">True</property>
               </object>
               <packing>
                 <property name="expand">False</property>
                 <property name="position">0</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkButton" id="button10">
+                <property name="label" translatable="yes">New _Table</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_underline">True</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
             <child>
               <object class="GtkButton" id="button9">
                 <property name="label">gtk-edit</property>
               <packing>
                 <property name="expand">False</property>
                 <property name="fill">False</property>
-                <property name="position">1</property>
+                <property name="position">2</property>
               </packing>
             </child>
             <child>
               <packing>
                 <property name="expand">False</property>
                 <property name="fill">False</property>
-                <property name="position">2</property>
+                <property name="position">3</property>
               </packing>
             </child>
           </object>
index eaeb64ec1b73282dcedfa18f702fe637ac3c8722..834d08282ed924df250f120d3ab1ed2e0026fa3b 100644 (file)
@@ -16,4 +16,6 @@ audit_gui_SOURCES = \
                 datasources.h \
                 main.c \
                 openauditds.c \
-                openauditds.h
+                openauditds.h \
+                table.c \
+                table.h
index 9efbf8e25066f5ad36c726d0c7a75106f2a8d73d..e433a8d71d4ac692c5c01be366896ef6c43e5f12 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "datasources.h"
 #include "datasource.h"
+#include "table.h"
 
 static void datasources_class_init (DatasourcesClass *klass);
 static void datasources_init (Datasources *datasources);
@@ -39,7 +40,9 @@ static void datasources_get_property (GObject *object,
                                      GValue *value,
                                      GParamSpec *pspec);
 
-static void datasources_on_btn_new_clicked (GtkButton *button,
+static void datasources_on_btn_new_datasource_clicked (GtkButton *button,
+                      gpointer user_data);
+static void datasources_on_btn_new_table_clicked (GtkButton *button,
                       gpointer user_data);
 static void datasources_on_btn_edit_clicked (GtkButton *button,
                       gpointer user_data);
@@ -126,7 +129,9 @@ Datasources
        priv->tstore_datasources = GTK_TREE_STORE (gtk_builder_get_object (priv->commons->gtkbuilder, "tstore_ds_tables_fields"));
 
        g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button7"),
-                         "clicked", G_CALLBACK (datasources_on_btn_new_clicked), (gpointer)a);
+                         "clicked", G_CALLBACK (datasources_on_btn_new_datasource_clicked), (gpointer)a);
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button10"),
+                         "clicked", G_CALLBACK (datasources_on_btn_new_table_clicked), (gpointer)a);
        g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button9"),
                          "clicked", G_CALLBACK (datasources_on_btn_edit_clicked), (gpointer)a);
        g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button8"),
@@ -301,15 +306,34 @@ datasources_edit (Datasources *datasources)
                        gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_datasources), &iter,
                                            COL_ID_DS, &id,
                                            -1);
+                       if (id != 0)
+                               {
+                                       Datasource *c = datasource_new (priv->commons, id);
 
-                       Datasource *c = datasource_new (priv->commons, id);
+                                       g_signal_connect (G_OBJECT (c), "updated",
+                                                                 G_CALLBACK (datasources_on_datasource_updated), (gpointer)datasources);
+
+                                       w = datasource_get_widget (c);
+                                       gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")));
+                                       gtk_widget_show_all (w);
+                               }
+                       else
+                               {
+                                       gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_datasources), &iter,
+                                                                   COL_ID_TABLE, &id,
+                                                                   -1);
+                                       if (id != 0)
+                                               {
+                                                       Table *c = table_new (priv->commons, id, 0);
 
-                       g_signal_connect (G_OBJECT (c), "updated",
-                                         G_CALLBACK (datasources_on_datasource_updated), (gpointer)datasources);
+                                                       g_signal_connect (G_OBJECT (c), "updated",
+                                                                                         G_CALLBACK (datasources_on_datasource_updated), (gpointer)datasources);
 
-                       w = datasource_get_widget (c);
-                       gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")));
-                       gtk_widget_show_all (w);
+                                                       w = table_get_widget (c);
+                                                       gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")));
+                                                       gtk_widget_show (w);
+                                               }
+                               }
                }
        else
                {
@@ -361,7 +385,7 @@ datasources_get_property (GObject *object, guint property_id, GValue *value, GPa
 
 /* CALLBACK */
 static void
-datasources_on_btn_new_clicked (GtkButton *button,
+datasources_on_btn_new_datasource_clicked (GtkButton *button,
                       gpointer user_data)
 {
        GtkWidget *w;
@@ -379,6 +403,83 @@ datasources_on_btn_new_clicked (GtkButton *button,
        gtk_widget_show_all (w);
 }
 
+static void
+datasources_on_btn_new_table_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       GtkWidget *w;
+
+       GtkWidget *dialog;
+       GtkTreeIter iter;
+
+       guint id_ds;
+
+       Datasources *datasources = (Datasources *)user_data;
+       DatasourcesPrivate *priv = DATASOURCES_GET_PRIVATE (datasources);
+
+       if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter))
+               {
+                       gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_datasources), &iter,
+                                           COL_ID_DS, &id_ds,
+                                           -1);
+
+                       if (id_ds == 0)
+                               {
+                                       GtkTreePath *path;
+
+                                       path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->tstore_datasources), &iter);
+                                       if (path != NULL)
+                                               {
+                                                       guint depth;
+                                                       
+                                                       depth = gtk_tree_path_get_depth (path);
+
+                                                       if (depth >= 2) gtk_tree_path_up (path);
+                                                       if (depth >= 3) gtk_tree_path_up (path);
+
+                                                       if (gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->tstore_datasources), &iter, path))
+                                                               {
+                                                                       gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_datasources), &iter,
+                                                                                                               COL_ID_DS, &id_ds,
+                                                                                                               -1);                                                                    
+                                                               }
+                                               }
+                               }
+
+                       if (id_ds != 0)
+                               {
+                                       Table *c = table_new (priv->commons, 0, id_ds);
+
+                                       g_signal_connect (G_OBJECT (c), "updated",
+                                                                         G_CALLBACK (datasources_on_datasource_updated), (gpointer)datasources);
+
+                                       w = table_get_widget (c);
+                                       gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")));
+                                       gtk_widget_show (w);
+                               }
+                       else
+                               {
+                                       dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+                                                                                GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                GTK_MESSAGE_WARNING,
+                                                                                GTK_BUTTONS_OK,
+                                                                                "Select a datasource or a table.");
+                                       gtk_dialog_run (GTK_DIALOG (dialog));
+                                       gtk_widget_destroy (dialog);
+                               }
+               }
+       else
+               {
+                       dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                        GTK_MESSAGE_WARNING,
+                                                        GTK_BUTTONS_OK,
+                                                        "Select a datasource or a table.");
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+               }
+}
+
 static void
 datasources_on_btn_edit_clicked (GtkButton *button,
                       gpointer user_data)
@@ -417,9 +518,36 @@ datasources_on_btn_delete_clicked (GtkButton *button,
                                                            -1);
 
                                        error = NULL;
-                                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser,
+                                       if (id != 0)
+                                               {
+                                                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser,
                                                                            g_strdup_printf ("UPDATE datasources SET status = 'D' WHERE id = %d", id),
                                                                            NULL, NULL);
+                                               }
+                                       else
+                                               {
+                                                       gtk_tree_model_get (GTK_TREE_MODEL (priv->tstore_datasources), &iter,
+                                                           COL_ID_TABLE, &id,
+                                                           -1);
+                                                       if (id != 0)
+                                                               {
+                                                                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser,
+                                                                           g_strdup_printf ("UPDATE tables SET status = 'D' WHERE id = %d", id),
+                                                                           NULL, NULL);
+                                                               
+                                                               }
+                                               }
+
+                                       if (stmt != NULL)
+                                               {
+                                                       dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                        GTK_MESSAGE_WARNING,
+                                                        GTK_BUTTONS_OK,
+                                                        "You must select a datasource or a table.");
+                                                       return;
+                                               }
+
                                        if (gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error) <= 0)
                                                {
                                                        dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
@@ -440,7 +568,7 @@ datasources_on_btn_delete_clicked (GtkButton *button,
                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                                         GTK_MESSAGE_WARNING,
                                                         GTK_BUTTONS_OK,
-                                                        "Select a datasource.");
+                                                        "Select a datasource or a table.");
                        gtk_dialog_run (GTK_DIALOG (dialog));
                        gtk_widget_destroy (dialog);
                }
diff --git a/src/table.c b/src/table.c
new file mode 100644 (file)
index 0000000..470cabf
--- /dev/null
@@ -0,0 +1,418 @@
+/* 
+ * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *
+ * 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 <sql-parser/gda-sql-parser.h>
+
+#include "table.h"
+
+static void table_class_init (TableClass *klass);
+static void table_init (Table *table);
+
+static void table_load_db_tables (Table *table);
+static void table_load (Table *table);
+static void table_save (Table *table);
+
+static void table_set_property (GObject *object,
+                                     guint property_id,
+                                     const GValue *value,
+                                     GParamSpec *pspec);
+static void table_get_property (GObject *object,
+                                     guint property_id,
+                                     GValue *value,
+                                     GParamSpec *pspec);
+
+static void table_on_cb_tables_changed (GtkComboBox *widget,
+                                        gpointer user_data);
+
+static void table_on_btn_cancel_clicked (GtkButton *button,
+                                    gpointer user_data);
+static void table_on_btn_save_clicked (GtkButton *button,
+                                  gpointer user_data);
+
+#define TABLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_TABLE, TablePrivate))
+
+typedef struct _TablePrivate TablePrivate;
+struct _TablePrivate
+       {
+               Commons *commons;
+
+               GtkWidget *w;
+
+               gint id;
+       };
+
+G_DEFINE_TYPE (Table, table, G_TYPE_OBJECT)
+
+static void
+table_class_init (TableClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       g_type_class_add_private (object_class, sizeof (TablePrivate));
+
+       object_class->set_property = table_set_property;
+       object_class->get_property = table_get_property;
+
+       /**
+        * Table::updated:
+        * @table:
+        *
+        */
+       klass->updated_signal_id = g_signal_new ("updated",
+                                                      G_TYPE_FROM_CLASS (object_class),
+                                                      G_SIGNAL_RUN_LAST,
+                                                      0,
+                                                      NULL,
+                                                      NULL,
+                                                      g_cclosure_marshal_VOID__VOID,
+                                                      G_TYPE_NONE,
+                                                      0);
+}
+
+static void
+table_init (Table *table)
+{
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+}
+
+/**
+ * table_new:
+ * @commons:
+ * @id:
+ * @id_ds:
+ *
+ * Returns: the newly created #Table object.
+ */
+Table
+*table_new (Commons *commons, guint id, guint id_datasources)
+{
+       GError *error;
+
+       Table *a = TABLE (g_object_new (table_get_type (), NULL));
+
+       TablePrivate *priv = TABLE_GET_PRIVATE (a);
+
+       priv->commons = commons;
+
+       error = NULL;
+       gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile,
+                                          g_strsplit ("lstore_tables|lstore_fields|w_table", "|", -1),
+                                          &error);
+       if (error != NULL)
+               {
+                       g_warning ("Errore: %s.", error->message);
+                       return NULL;
+               }
+
+       priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_table"));
+
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "combobox1"),
+                         "changed", G_CALLBACK (table_on_cb_tables_changed), (gpointer *)a);
+
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button5"),
+                         "clicked", G_CALLBACK (table_on_btn_cancel_clicked), (gpointer *)a);
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button6"),
+                         "clicked", G_CALLBACK (table_on_btn_save_clicked), (gpointer *)a);
+
+       table_load_db_tables (a);
+
+       priv->id = id;
+       if (priv->id == 0)
+               {
+                       gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label7")), "");
+                       gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label8")), g_strdup_printf ("%d", id_datasources));
+               }
+       else
+               {
+                       gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label7")), g_strdup_printf ("%d", priv->id));
+
+                       table_load (a);
+               }
+
+       return a;
+}
+
+/**
+ * table_get_widget:
+ * @table:
+ *
+ */
+GtkWidget
+*table_get_widget (Table *table)
+{
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       return priv->w;
+}
+
+/* PRIVATE */
+static void
+table_load_db_tables (Table *table)
+{
+       GError *error;
+
+       GdaMetaContext mcontext = {"_tables", 0, NULL, NULL};
+
+       GdaDataModel *dm;
+
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+
+       guint rows;
+       guint row;
+
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       error = NULL;
+       if (!gda_connection_update_meta_store (priv->commons->gdacon, &mcontext, &error))
+               {
+                       GtkWidget *dialog;
+                       dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w),
+                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                        GTK_MESSAGE_WARNING,
+                                                                                        GTK_BUTTONS_OK,
+                                                                                        "Unable to get tables list.\n\n",
+                                                                                        (error != NULL && error->message != NULL ? error->message : "No details."));
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+                       return;
+               }
+
+       model = gtk_combo_box_get_model (GTK_COMBO_BOX (gtk_builder_get_object (priv->commons->gtkbuilder, "combobox1")));
+
+       gtk_list_store_clear (GTK_LIST_STORE (model));
+
+       error = NULL;
+       dm = gda_connection_get_meta_store_data (priv->commons->gdacon,
+                                           GDA_CONNECTION_META_TABLES,
+                                           &error,
+                                           0);
+       if (dm != NULL)
+               {
+                       rows = gda_data_model_get_n_rows (dm);
+                       for (row = 0; row < rows; row++)
+                               {
+                                       gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+                                       gtk_list_store_set (GTK_LIST_STORE (model), &iter,
+                                                           0, gda_value_stringify (gda_data_model_get_value_at (dm, 0, row, NULL)),
+                                                           -1);
+                               }
+               }
+}
+
+static void
+table_set_table (Table *table, const gchar *table_name)
+{
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+
+       gchar *name;
+
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       model = gtk_combo_box_get_model (GTK_COMBO_BOX (gtk_builder_get_object (priv->commons->gtkbuilder, "combobox1")));
+
+       if (gtk_tree_model_get_iter_first (model, &iter))
+               {
+                       do
+                               {
+                                       gtk_tree_model_get (model, &iter, 0, &name, -1);
+                                       if (name != NULL && g_strcmp0 (name, table_name) == 0)
+                                               {
+                                                       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (gtk_builder_get_object (priv->commons->gtkbuilder, "combobox1")),
+                                                                                      &iter);
+                                                       break;
+                                               }
+                               } while (gtk_tree_model_iter_next (model, &iter));
+               }
+}
+
+static void
+table_load (Table *table)
+{
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       GError *error;
+       gchar *sql;
+       GdaStatement *stmt;
+       GdaDataModel *dm;
+
+       sql = g_strdup_printf ("SELECT name FROM tables WHERE id = %d",
+                              priv->id);
+       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+       dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error);
+       g_object_unref (stmt);
+       if (dm != NULL && gda_data_model_get_n_rows (dm) == 1)
+               {
+                       table_set_table (table, gda_value_stringify (gda_data_model_get_value_at (dm, 0, 0, NULL)));
+               }
+       else
+               {
+                       GtkWidget *dialog;
+                       dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w),
+                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                        GTK_MESSAGE_WARNING,
+                                                                                        GTK_BUTTONS_OK,
+                                                                                        "Record %d doesn't exist.", priv->id);
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+               }
+}
+
+static void
+table_save (Table *table)
+{
+       GError *error;
+       gchar *sql;
+       GdaStatement *stmt;
+       GdaDataModel *dm;
+       GtkWidget *dialog;
+
+       gchar *cncstring;
+
+       TableClass *klass = TABLE_GET_CLASS (table);
+
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       if (priv->id == 0)
+               {
+                       /* find the new id */
+                       guint new_id;
+
+                       new_id = 0;
+                       sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM tables");
+                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+                       dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error);
+                       g_object_unref (stmt);
+                       if (dm != NULL && gda_data_model_get_n_rows (dm) == 1)
+                               {
+                                       new_id = g_value_get_int (gda_data_model_get_value_at (dm, 0, 0, NULL));
+                               }
+                       new_id++;
+
+                       sql = g_strdup_printf ("INSERT INTO tables (id, name)"
+                                              " VALUES (%d, '%s')",
+                                              new_id,
+                                              gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1"))));
+                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+               }
+       else
+               {
+                       sql = g_strdup_printf ("UPDATE tables"
+                                              " SET"
+                                              " name = '%s'"
+                                              " WHERE id = %d",
+                                              gtk_entry_get_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1"))),
+                                              priv->id);
+                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+               }
+
+       error = NULL;
+       if (gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error) == 1)
+               {
+                       g_object_unref (stmt);
+
+                       g_signal_emit (table, klass->updated_signal_id, 0);
+
+                       if (priv->id == 0)
+                               {
+                                       priv->id = strtol (gtk_label_get_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label7"))), NULL, 10);
+                               }
+
+                       dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w),
+                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                        GTK_MESSAGE_INFO,
+                                                                                        GTK_BUTTONS_OK,
+                                                                                        "Saved with success.");
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+               }
+       else
+               {
+                       if (priv->id == 0)
+                               {
+                                       gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label7")), "");
+                               }
+
+                       dialog = gtk_message_dialog_new (GTK_WINDOW (priv->w),
+                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                        GTK_MESSAGE_WARNING,
+                                                                                        GTK_BUTTONS_OK,
+                                                                                        "Error on saving.\n\n%s",
+                                                                                        (error != NULL && error->message != NULL ? error->message : "No details."));
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+               }
+}
+
+static void
+table_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
+{
+       Table *table = TABLE (object);
+
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+table_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
+{
+       Table *table = TABLE (object);
+
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+/* CALLBACK */
+static void
+table_on_cb_tables_changed (GtkComboBox *widget,
+                            gpointer user_data)
+{
+
+}
+
+static void
+table_on_btn_cancel_clicked (GtkButton *button,
+                             gpointer user_data)
+{
+       Table *table = (Table *)user_data;
+
+       TablePrivate *priv = TABLE_GET_PRIVATE (table);
+
+       gtk_widget_destroy (priv->w);
+}
+
+static void
+table_on_btn_save_clicked (GtkButton *button,
+                           gpointer user_data)
+{
+       table_save ((Table *)user_data);
+}
diff --git a/src/table.h b/src/table.h
new file mode 100644 (file)
index 0000000..95130ff
--- /dev/null
@@ -0,0 +1,66 @@
+/* 
+ * Copyright (C) 2010 Andrea Zagli <azagli@libero.it>
+ *
+ * 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
+ *
+ */
+
+#ifndef __TABLE_H__
+#define __TABLE_H__
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <gtk/gtk.h>
+#include <libgdaex.h>
+
+#include "commons.h"
+
+G_BEGIN_DECLS
+
+
+#define TYPE_TABLE                 (table_get_type ())
+#define TABLE(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TABLE, Table))
+#define TABLE_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TABLE, TableClass))
+#define IS_TABLE(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TABLE))
+#define IS_TABLE_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TABLE))
+#define TABLE_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TABLE, TableClass))
+
+
+typedef struct _Table Table;
+typedef struct _TableClass TableClass;
+
+struct _Table
+       {
+               GObject parent;
+       };
+
+struct _TableClass
+       {
+               GObjectClass parent_class;
+
+               guint updated_signal_id;
+       };
+
+GType table_get_type (void) G_GNUC_CONST;
+
+Table *table_new (Commons *commons, guint id, guint id_datasources);
+
+GtkWidget *table_get_widget (Table *table);
+
+
+G_END_DECLS
+
+#endif /* __TABLE_H__ */