From: Andrea Zagli Date: Fri, 20 Dec 2013 12:29:27 +0000 (+0100) Subject: Continued GUI to view changes. X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=c3f1c2560fc5dce0c2f5e9553da5f6e9b1f552a3;p=zakaudit%2Flibzakaudit Continued GUI to view changes. --- diff --git a/data/libzakaudit/gui/libzakaudit.ui b/data/libzakaudit/gui/libzakaudit.ui index b82369b..489a408 100644 --- a/data/libzakaudit/gui/libzakaudit.ui +++ b/data/libzakaudit/gui/libzakaudit.ui @@ -2,22 +2,6 @@ - - - - - - - - - - - - - - - - False True @@ -40,63 +24,7 @@ True True - tstore_changes True - - - Date - - - - 1 - - - - - - - Type - - - - 2 - - - - - - - User - - - - 3 - - - - - - - Field - - - - 4 - - - - - - - Value - - - - 5 - - - - diff --git a/src/audit.c b/src/audit.c index d3cceee..d8bcbe7 100644 --- a/src/audit.c +++ b/src/audit.c @@ -28,6 +28,23 @@ #include "libzakaudit.h" +enum +{ + COL_CHANGES_ID, + COL_CHANGES_DATE, + COL_CHANGES_OPERATION, + COL_CHANGES_USER, + CHANGES_STATIC_COLUMNS +}; + +typedef struct +{ + guint col; + gchar *field_name; + gchar *field_long_name; + gchar *field_value; +} ChangesColumn; + typedef struct { gint id; @@ -847,7 +864,7 @@ zak_audit_action_v (ZakAudit *zak_audit, if (action < 1) { g_warning ("Action %d not valid.", action); - return; + return FALSE; } /* find the new action's id */ @@ -1225,7 +1242,6 @@ zak_audit_action_from_gdastatement (ZakAudit *zak_audit, return ret; } - /** * zak_audit_get_changes_widget: * @zak_audit: an #ZakAudit object. @@ -1255,13 +1271,21 @@ GtkWidget gchar *field_name; gchar *field_value; + guint row; + guint rows; + ChangesColumn *column; + GHashTable *ht_columns; + GType *gtype; + gchar *sql; GString *sql_where; GdaDataModel *dm; - GtkTreeStore *tstore; + GtkTreeView *tview; + GtkCellRenderer *renderer; + GtkTreeViewColumn *vcolumn; + GtkListStore *lstore; GtkTreeIter iter; - GtkTreeIter iter_parent; ZakAuditPrivate *priv = ZAK_AUDIT_GET_PRIVATE (zak_audit); @@ -1277,9 +1301,22 @@ GtkWidget return NULL; } + l = g_strv_length ((gchar **)key_values); + if (l == 0) + { + return NULL; + } + + ht_columns = g_hash_table_new (g_str_hash, g_str_equal); + + error = NULL; + gtk_builder_add_objects_from_file (priv->gtkbuilder, priv->guifile, + g_strsplit ("w_changes", + "|", -1), + &error); + sql_where = g_string_new (""); - l = g_strv_length ((gchar **)key_values); for (strpart = 0; strpart < l; strpart++) { field_name = (gchar *)key_values[strpart]; @@ -1300,42 +1337,97 @@ GtkWidget field_value); } - sql = g_strdup_printf ("(SELECT a.id, a.type, a.username, a.date," - " v.value" - " FROM actions AS a" - " INNER JOIN values AS v ON a.id = v.id_actions" - " INNER JOIN fields AS f ON v.id_fields = f.id" - " WHERE a.type = %d" - " AND a.id_tables = %d" - "%s" - " AND f.is_key = TRUE)" - " UNION" - " (SELECT a.id, a.type, a.username, a.date," - " v.value" - " FROM actions AS a" - " INNER JOIN values AS v ON a.id = v.id_actions" - " INNER JOIN fields AS f ON v.id_fields = f.id" - " WHERE a.type = %d" - " AND a.id_tables = %d" - "%s" - " AND f.is_key = TRUE)" - " UNION", - " (SELECT a.id, a.type, a.username, a.date," + tview = GTK_TREE_VIEW (gtk_builder_get_object (priv->gtkbuilder, "treeview1")); + + sql = g_strdup_printf ("SELECT *" + " FROM fields" + " WHERE id_tables = %d", + table->id); + dm = gdaex_query (priv->gdaex, sql); + if (dm != NULL && gda_data_model_get_n_rows (dm) > 0) + { + rows = gda_data_model_get_n_rows (dm); + + gtype = g_new0 (GType, CHANGES_STATIC_COLUMNS + rows); + + gtype[COL_CHANGES_ID] = G_TYPE_INT; /* id */ + renderer = gtk_cell_renderer_spin_new (); + gtk_cell_renderer_set_alignment (renderer, 1.0, 0.5); + vcolumn = gtk_tree_view_column_new (); + gtk_tree_view_column_pack_start (vcolumn, renderer, TRUE); + gtk_tree_view_column_set_title (vcolumn, "ID"); + gtk_tree_view_column_add_attribute (vcolumn, renderer, "text", COL_CHANGES_ID); + + gtype[COL_CHANGES_DATE] = G_TYPE_STRING; /* date */ + renderer = gtk_cell_renderer_text_new (); + vcolumn = gtk_tree_view_column_new (); + gtk_tree_view_column_pack_start (vcolumn, renderer, TRUE); + gtk_tree_view_column_set_title (vcolumn, "Date"); + gtk_tree_view_column_add_attribute (vcolumn, renderer, "text", COL_CHANGES_DATE); + + gtype[COL_CHANGES_OPERATION] = G_TYPE_STRING; /* type */ + renderer = gtk_cell_renderer_text_new (); + vcolumn = gtk_tree_view_column_new (); + gtk_tree_view_column_pack_start (vcolumn, renderer, TRUE); + gtk_tree_view_column_set_title (vcolumn, "Operation"); + gtk_tree_view_column_add_attribute (vcolumn, renderer, "text", COL_CHANGES_OPERATION); + + gtype[COL_CHANGES_USER] = G_TYPE_STRING; /* username */ + renderer = gtk_cell_renderer_text_new (); + vcolumn = gtk_tree_view_column_new (); + gtk_tree_view_column_pack_start (vcolumn, renderer, TRUE); + gtk_tree_view_column_set_title (vcolumn, "User"); + gtk_tree_view_column_add_attribute (vcolumn, renderer, "text", COL_CHANGES_USER); + + for (row = 0; row < rows; row++) + { + column = g_new0 (ChangesColumn, 1); + column->col = CHANGES_STATIC_COLUMNS + row; + column->field_name = gdaex_data_model_get_field_value_stringify_at (dm, row, "name"); + + /* TODO to ask + * column->field_long_name = + */ + + /* TODO find the last value in database + * column->field_value = + */ + + g_hash_table_insert (ht_columns, (gpointer)column->field_name, (gpointer)column); + + gtype[CHANGES_STATIC_COLUMNS + row] = G_TYPE_STRING; + } + } + else + { + if (dm != NULL) + { + g_object_unref (dm); + } + g_warning ("Fields not found for table <%s>.", table->name); + return NULL; + } + if (dm != NULL) + { + g_object_unref (dm); + } + + lstore = GTK_TREE_STORE (gtk_list_store_newv (4 + rows, gtype)); + gtk_tree_view_set_model (tview, GTK_TREE_MODEL (lstore)); + + /* find the last action + * go backward + */ + sql = g_strdup_printf ("SELECT a.id, a.type, a.username, a.date," " v.value" " FROM actions AS a" " INNER JOIN values AS v ON a.id = v.id_actions" " INNER JOIN fields AS f ON v.id_fields = f.id" - " WHERE a.type = %d" - " AND a.id_tables = %d" + " WHERE a.id_tables = %d" "%s" - " AND f.is_key = TRUE)", - ZAK_AUDIT_ACTION_INSERT, - table->id, - sql_where->str, - ZAK_AUDIT_ACTION_AFTER_UPDATE, - table->id, - sql_where->str, - ZAK_AUDIT_ACTION_DELETE, + " AND f.is_key = TRUE" + " ORDER BY a.id DESC" + " LIMIT 1", table->id, sql_where->str); @@ -1343,18 +1435,73 @@ GtkWidget g_free (sql); if (dm != NULL && gda_data_model_get_n_rows (dm) > 0) { - error = NULL; - gtk_builder_add_objects_from_file (priv->gtkbuilder, priv->guifile, - g_strsplit ("tstore_changes" - "|w_changes", - "|", -1), - &error); - - tstore = (GtkTreeStore *)gtk_builder_get_object (priv->gtkbuilder, "tstore_changes"); + gtk_list_store_append (lstore, &iter); + gtk_list_store_set (lstore, &iter, + COL_CHANGES_ID, gdaex_data_model_get_value_integer_at (dm, 0, "id"), + COL_CHANGES_DATE, g_date_time_format (gdaex_data_model_get_field_value_gdatetime_at (dm, 0, "date"), "%d/%m/%Y %H.%M.%S"), + COL_CHANGES_OPERATION, gdaex_data_model_get_field_value_integer_at (dm, 0, "type") == ZAK_AUDIT_ACTION_DELETE ? "Delete" : + gdaex_data_model_get_field_value_integer_at (dm, 0, "type") == ZAK_AUDIT_ACTION_AFTER_UPDATE ? "Edit" : + gdaex_data_model_get_field_value_integer_at (dm, 0, "type") == ZAK_AUDIT_ACTION_INSERT ? "Insert" : "non valid", + COL_CHANGES_USER, gdaex_data_model_get_field_value_stringify_at (dm, 0, "username"), + -1); /* TODO check if key field value is changed */ + /* TODO i could find field that there's not in columns (the dm passed)*/ + if (gdaex_data_model_get_field_value_integer_at (dm, 0, "type") == ZAK_AUDIT_ACTION_DELETE) + { + /* last values are stored in zakaudit tables */ + sql = g_strdup_printf ("SELECT v.*, f.name" + " FROM values AS v" + " INNSER JOIN fields AS f ON v.id_fields = f.id" + " WHERE v.id_actions = %d", + gdaex_data_model_get_field_value_integer_at (dm, 0, "id")); + dm = gdaex_query (priv->gdaex, sql); + g_free (sql); + if (dm != NULL) + { + rows = gda_data_model_get_n_rows (dm); + for (row = 0; row < rows; row++) + { + column = (ChangesColumn *)g_hash_table_lookup (ht_columns, gdaex_data_model_get_field_value_stringify_at (dm, row, "name")); + if (column != NULL) + { + column->field_value = gdaex_data_model_get_field_value_stringify_at (dm, row, "value"); + gtk_list_store_set (lstore, &iter, + column->col, column->field_value, + -1); + } + } + } + } + else if (gdaex_data_model_get_field_value_integer_at (dm, 0, "type") == ZAK_AUDIT_ACTION_AFTER_UPDATE + || gdaex_data_model_get_field_value_integer_at (dm, 0, "type") == ZAK_AUDIT_ACTION_INSERT) + { + /* last values are stored in datasource audited */ + sql = g_strdup_printf ("SELECT *" + " FROM %s" + " WHERE %s", + table->name, + sql_where->str + 5); + dm = gdaex_query (datasource->gdaex, sql); + g_free (sql); + if (dm != NULL) + { + rows = gda_data_model_get_n_columns (dm); + for (row = 0; row < rows; row++) + { + column = (ChangesColumn *)g_hash_table_lookup (ht_columns, gda_data_model_get_column_name (dm, row)); + if (column != NULL) + { + column->field_value = gdaex_data_model_get_value_stringify_at (dm, 0, row); + gtk_list_store_set (lstore, &iter, + column->col, column->field_value, + -1); + } + } + } + } } if (dm != NULL) {