Ended user management. 0.1.0
authorAndrea Zagli <azagli@libero.it>
Sun, 8 Aug 2010 09:49:01 +0000 (11:49 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 8 Aug 2010 09:49:01 +0000 (11:49 +0200)
Still lacks users search.

configure.ac
data/libaute-db/form/user.form
src/Makefile.am
src/aute_db.c
src/aute_db.h [new file with mode: 0644]
src/user.c

index bed2e656f2e5a6abca14207a5df1af96560dd39c..004c3ad41b891dc1853dc08f6d28cdae5688c215 100644 (file)
@@ -19,7 +19,7 @@ AC_PROG_LIBTOOL
 
 # Checks for libraries.
 PKG_CHECK_MODULES(LIBAUTEDB, [gtk+-2.0 >= 2.16.0
-                              libaute >= 0.0.2
+                              libaute >= 0.1.0
                               libgtkform >= 0.1.4])
 
 AC_SUBST(LIBAUTEDB_CFLAGS)
index b591d4bb2099f814293b8da859cb6df50b3fc2a6..3c8a12276e7a27a301200deb193c271eb19219b1 100644 (file)
@@ -10,7 +10,7 @@
                </field>
        </widget>
 
-       <widget type="label" name="label114">
+       <widget type="label" name="label14">
                <field type="text" name="password">
                </field>
        </widget>
index 9234dbd326bf0b02c94df8a5052d813f3f4bd278..3a07774c4ff5e72486361a163540d727a7660f68 100644 (file)
@@ -15,6 +15,7 @@ libaute_pluginsdir = $(libdir)/libaute/plugins
 libaute_plugins_LTLIBRARIES = libaute-db.la
 
 libaute_db_la_SOURCES = aute_db.c \
+                        aute_db.h \
                         user.c \
                         user.h
 
index ebff62e1c338d3bbf22ea761222a12456478b535..51b627ad6fc0c2109becbfa206c5f09a22a4d944 100644 (file)
@@ -43,6 +43,8 @@ static GtkWidget *exp_cambio;
 static GtkWidget *txt_password_nuova;
 static GtkWidget *txt_password_conferma;
 
+static GtkWidget *w_users;
+
 static GtkTreeView *trv_users;
 static GtkListStore *lstore_users;
 static GtkTreeSelection *sel_users;
@@ -122,14 +124,36 @@ get_gdaex (GSList *parameters)
                }
 }
 
+static GtkWindow
+*autedb_get_gtkwidget_parent_gtkwindow (GtkWidget *widget)
+{
+       GtkWindow *w;
+       GtkWidget *w_parent;
+
+       w = NULL;
+
+       w_parent = gtk_widget_get_parent (widget);
+       while (w_parent != NULL && !GTK_IS_WINDOW (w_parent))
+               {
+                       w_parent = gtk_widget_get_parent (w_parent);
+               }
+
+       if (GTK_IS_WINDOW (w_parent))
+               {
+                       w = GTK_WINDOW (w_parent);
+               }
+
+       return w;
+}
+
 /**
- * cifra_password:
+ * autedb_cifra_password:
  * @password: una stringa da cifrare.
  *
  * Return: la @password cifrata.
  */
-static gchar
-*cifra_password (const gchar *password)
+gchar
+*autedb_cifra_password (const gchar *password)
 {
        gchar digest[17] = "";
        gchar pwd_gcrypt[33] = "";
@@ -166,7 +190,7 @@ static gchar
                          " AND password = '%s'"
                          " AND status <> 'E'",
                          gdaex_strescape (utente, NULL),
-                         gdaex_strescape (cifra_password (password), NULL));
+                         gdaex_strescape (autedb_cifra_password (password), NULL));
        dm = gdaex_query (gdaex, sql);
        if (dm == NULL || gda_data_model_get_n_rows (dm) <= 0)
                {
@@ -196,7 +220,7 @@ static gchar
                                        sql = g_strdup_printf ("UPDATE users"
                                                         " SET password = '%s'"
                                                         " WHERE code = '%s'",
-                                                        gdaex_strescape (cifra_password (password_nuova), NULL),
+                                                        gdaex_strescape (autedb_cifra_password (password_nuova), NULL),
                                                         gdaex_strescape (utente, NULL));
                                        if (gdaex_execute (gdaex, sql) == -1)
                                                {
@@ -222,7 +246,7 @@ autedb_load_users_list ()
 
        gtk_list_store_clear (lstore_users);
 
-       sql = g_strdup_printf ("SELECT * FROM users WHERE status <> 'E'");
+       sql = g_strdup_printf ("SELECT * FROM users WHERE status <> 'E' ORDER BY surname, name, code");
        dm = gdaex_query (gdaex, sql);
        if (dm != NULL)
                {
@@ -231,7 +255,7 @@ autedb_load_users_list ()
                                {
                                        gtk_list_store_append (lstore_users, &iter);
                                        gtk_list_store_set (lstore_users, &iter,
-                                                           COL_STATUS, gdaex_data_model_get_field_value_stringify_at (dm, row, "status"),
+                                                           COL_STATUS, gdaex_data_model_get_field_value_boolean_at (dm, row, "enabled") ? "" : "Disabled",
                                                            COL_CODE, gdaex_data_model_get_field_value_stringify_at (dm, row, "code"),
                                                            COL_NAME, g_strdup_printf ("%s %s",
                                                                      gdaex_data_model_get_field_value_stringify_at (dm, row, "surname"),
@@ -248,6 +272,41 @@ autedb_on_user_aggiornato (gpointer instance, gpointer user_data)
        autedb_load_users_list ();
 }
 
+static void
+autedb_edit_user ()
+{
+       GtkTreeIter iter;
+       gchar *code;
+
+       if (gtk_tree_selection_get_selected (sel_users, NULL, &iter))
+               {
+                       GtkWidget *w;
+
+                       gtk_tree_model_get (GTK_TREE_MODEL (lstore_users), &iter,
+                                           COL_CODE, &code,
+                                                               -1);
+
+                       User *u = user_new (gtkbuilder, gdaex, guifile, formdir, code);
+
+                       g_signal_connect (G_OBJECT (u), "aggiornato",
+                                         G_CALLBACK (autedb_on_user_aggiornato), NULL);
+
+                       w = user_get_widget (u);
+                       gtk_window_set_transient_for (GTK_WINDOW (w), autedb_get_gtkwidget_parent_gtkwindow (w_users));
+                       gtk_widget_show (w);
+               }
+       else
+               {
+                       GtkWidget *dialog = gtk_message_dialog_new (autedb_get_gtkwidget_parent_gtkwindow (w_users),
+                                                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                                        GTK_MESSAGE_WARNING,
+                                                                                        GTK_BUTTONS_OK,
+                                                                                        "Occorre prima selezionare un modello");
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+               }
+}
+
 static void
 autedb_on_btn_new_clicked (GtkButton *button,
                            gpointer user_data)
@@ -260,14 +319,7 @@ autedb_on_btn_new_clicked (GtkButton *button,
                          G_CALLBACK (autedb_on_user_aggiornato), NULL);
 
        w = user_get_widget (u);
-       /*if (priv->selezione)
-               {
-                       gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (priv->widget));
-               }
-       else
-               {
-                       gtk_window_set_transient_for (GTK_WINDOW (w), GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")));
-               }*/
+       gtk_window_set_transient_for (GTK_WINDOW (w), autedb_get_gtkwidget_parent_gtkwindow (w_users));
        gtk_widget_show (w);
 }
   
@@ -275,6 +327,7 @@ static void
 autedb_on_btn_edit_clicked (GtkButton *button,
                            gpointer user_data)
 {
+       autedb_edit_user ();
 }
   
 static void
@@ -289,12 +342,12 @@ autedb_on_btn_delete_clicked (GtkButton *button,
 
        if (gtk_tree_selection_get_selected (sel_users, NULL, &iter))
                {
-                       /*dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+                       dialog = gtk_message_dialog_new (autedb_get_gtkwidget_parent_gtkwindow (w_users),
                                                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                                                                         GTK_MESSAGE_QUESTION,
                                                                                         GTK_BUTTONS_YES_NO,
-                                                                                        "Sicuro di voler eliminare il modello selezionato?");
-                       risp = gtk_dialog_run (GTK_DIALOG (dialog));*/
+                                                                                        "Sicuro di voler eliminare l'utente selezionato?");
+                       risp = gtk_dialog_run (GTK_DIALOG (dialog));
                        if (risp == GTK_RESPONSE_YES)
                                {
                                        gtk_tree_model_get (GTK_TREE_MODEL (lstore_users), &iter,
@@ -306,20 +359,29 @@ autedb_on_btn_delete_clicked (GtkButton *button,
 
                                        autedb_load_users_list ();
                                }
-                       /*gtk_widget_destroy (dialog);*/
+                       gtk_widget_destroy (dialog);
                }
        else
                {
-                       /*dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+                       dialog = gtk_message_dialog_new (autedb_get_gtkwidget_parent_gtkwindow (w_users),
                                                                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                                                                         GTK_MESSAGE_WARNING,
                                                                                         GTK_BUTTONS_OK,
                                                                                         "Occorre prima selezionare un modello");
                        gtk_dialog_run (GTK_DIALOG (dialog));
-                       gtk_widget_destroy (dialog);*/
+                       gtk_widget_destroy (dialog);
                }
 }
-  
+
+static void
+autedb_on_trv_users_row_activated (GtkTreeView *tree_view,
+                                             GtkTreePath *tree_path,
+                                                                                        GtkTreeViewColumn *column,
+                                                                                        gpointer user_data)
+{
+       autedb_edit_user ();
+}
+
 static void
 autedb_on_btn_find_clicked (GtkButton *button,
                            gpointer user_data)
@@ -407,15 +469,13 @@ gchar
 }
 
 /**
- * get_user_gui_manager:
+ * get_management_gui:
  * @parameters:
  *
  */
 GtkWidget
-*get_user_gui_manager (GSList *parameters)
+*get_management_gui (GSList *parameters)
 {
-       GtkWidget *w;
-
        GError *error;
 
        error = NULL;
@@ -438,8 +498,8 @@ GtkWidget
                        return NULL;
                }
 
-       w = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "vbx_users_list"));
-       if (w == NULL)
+       w_users = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, "vbx_users_list"));
+       if (w_users == NULL)
                {
                        g_warning ("Unable to find the widget vbx_users_list.");
                        return NULL;
@@ -449,6 +509,9 @@ GtkWidget
        lstore_users = GTK_LIST_STORE (gtk_builder_get_object (gtkbuilder, "lstore_users"));
        sel_users = gtk_tree_view_get_selection (trv_users);
 
+       g_signal_connect (gtk_builder_get_object (gtkbuilder, "treeview1"),
+                         "row-activated", G_CALLBACK (autedb_on_trv_users_row_activated), NULL);
+
        g_signal_connect (G_OBJECT (gtk_builder_get_object (gtkbuilder, "button1")), "clicked",
                          G_CALLBACK (autedb_on_btn_new_clicked), NULL);
        g_signal_connect (G_OBJECT (gtk_builder_get_object (gtkbuilder, "button2")), "clicked",
@@ -460,258 +523,5 @@ GtkWidget
 
        autedb_load_users_list ();
 
-       return w;
-}
-
-/**
- * crea_utente:
- * @parameters:
- * @codice:
- * @password:
- */
-gboolean
-crea_utente (GSList *parameters, const gchar *codice, const gchar *password)
-{
-       gchar *codice_;
-       gchar *password_;
-       gchar *cnc_string;
-       gchar *sql;
-       GdaEx *gdaex;
-       GdaDataModel *dm;
-
-       if (codice == FALSE || password == NULL)
-               {
-                       g_warning ("codice o password nulli.");
-                       return FALSE;
-               }
-
-       codice_ = g_strstrip (g_strdup (codice));
-       password_ = g_strstrip (g_strdup (password));
-
-       if (strcmp (codice_, "") == 0 || strcmp (password_, "") == 0)
-               {
-                       g_warning ("codice o password vuoti.");
-                       return FALSE;
-               }
-
-       cnc_string = NULL;
-
-#ifdef HAVE_LIBCONFI
-       /* the first and only parameters must be a Confi object */
-       /* leggo i parametri di connessione dalla configurazione */
-       if (IS_CONFI (parameters->data))
-               {
-                       if (!get_connection_parameters_from_confi (CONFI (parameters->data), &cnc_string))
-                               {
-                                       cnc_string = NULL;
-                               }
-               }
-#endif
-
-       if (cnc_string == NULL)
-               {
-                       GSList *param;
-
-                       param = g_slist_next (parameters);
-                       if (param != NULL && param->data != NULL)
-                               {
-                                       cnc_string = g_strdup ((gchar *)param->data);
-                                       cnc_string = g_strstrip (cnc_string);
-                                       if (g_strcmp0 (cnc_string, "") == 0)
-                                               {
-                                                       cnc_string = NULL;
-                                               }
-                               }
-               }
-
-       if (cnc_string == NULL)
-               {
-                       return FALSE;
-               }
-
-       /* creo un oggetto GdaO */
-       gdaex = gdaex_new_from_string (cnc_string);
-       if (gdaex == NULL) return FALSE;
-
-       /* controllo se esiste gia' */
-       sql = g_strdup_printf ("SELECT code FROM users WHERE code = '%s'",
-                         gdaex_strescape (codice_, NULL));
-       dm = gdaex_query (gdaex, sql);
-       if (dm != NULL && gda_data_model_get_n_rows (dm) > 0)
-               {
-                       /* aggiorno l'utente */
-                       sql = g_strdup_printf ("UPDATE users SET password = '%s' WHERE code = '%s'",
-                                              gdaex_strescape (cifra_password (password_), NULL),
-                                              gdaex_strescape (codice_, NULL));
-               }
-       else
-               {
-                       /* creo l'utente */
-                       sql = g_strdup_printf ("INSERT INTO users VALUES ('%s', '%s', '')",
-                                              gdaex_strescape (codice_, NULL),
-                                              gdaex_strescape (cifra_password (password_), NULL));
-               }
-
-       return (gdaex_execute (gdaex, sql) >= 0);
-}
-
-/**
- * modifice_utente:
- * @parameters:
- * @codice:
- * @password:
- */
-gboolean
-modifica_utente (GSList *parameters, const gchar *codice, const gchar *password)
-{
-       gchar *codice_;
-       gchar *password_;
-       gchar *cnc_string;
-       gchar *sql;
-       GdaEx *gdaex;
-       GdaDataModel *dm;
-
-       if (codice == FALSE || password == NULL)
-               {
-                       g_warning ("codice o password nulli.");
-                       return FALSE;
-               }
-
-       codice_ = g_strstrip (g_strdup (codice));
-       password_ = g_strstrip (g_strdup (password));
-
-       if (strcmp (codice_, "") == 0 || strcmp (password_, "") == 0)
-               {
-                       g_warning ("codice o password vuoti.");
-                       return FALSE;
-               }
-
-#ifdef HAVE_LIBCONFI
-       /* the first and only parameters must be a Confi object */
-       /* leggo i parametri di connessione dalla configurazione */
-       if (IS_CONFI (parameters->data))
-               {
-                       if (!get_connection_parameters_from_confi (CONFI (parameters->data), &cnc_string))
-                               {
-                                       cnc_string = NULL;
-                               }
-               }
-#endif
-
-       if (cnc_string == NULL)
-               {
-                       GSList *param;
-
-                       param = g_slist_next (parameters);
-                       if (param != NULL && param->data != NULL)
-                               {
-                                       cnc_string = g_strdup ((gchar *)param->data);
-                                       cnc_string = g_strstrip (cnc_string);
-                                       if (g_strcmp0 (cnc_string, "") == 0)
-                                               {
-                                                       cnc_string = NULL;
-                                               }
-                               }
-               }
-
-       if (cnc_string == NULL)
-               {
-                       return FALSE;
-               }
-
-       /* creo un oggetto GdaEx */
-       gdaex = gdaex_new_from_string (cnc_string);
-       if (gdaex == NULL) return FALSE;
-
-       /* controllo se non esiste */
-       sql = g_strdup_printf ("SELECT code FROM users WHERE code = '%s'",
-                         gdaex_strescape (codice_, NULL));
-       dm = gdaex_query (gdaex, sql);
-       if (dm == NULL || gda_data_model_get_n_rows (dm) <= 0)
-               {
-                       /* creo l'utente */
-                       sql = g_strdup_printf ("INSERT INTO users VALUES ('%s', '%s', '')",
-                                              gdaex_strescape (codice_, NULL),
-                                              gdaex_strescape (cifra_password (password_), NULL));
-               }
-       else
-               {
-                       /* aggiorno l'utente */
-                       sql = g_strdup_printf ("UPDATE users SET password = '%s' WHERE code = '%s'",
-                                              gdaex_strescape (cifra_password (password_), NULL),
-                                              gdaex_strescape (codice_, NULL));
-               }
-
-       return (gdaex_execute (gdaex, sql) >= 0);
-}
-
-/**
- * elimina_utente:
- * @parameters:
- * @codice:
- */
-gboolean
-elimina_utente (GSList *parameters, const gchar *codice)
-{
-       gchar *codice_;
-       gchar *cnc_string;
-       gchar *sql;
-       GdaEx *gdaex;
-
-       if (codice == FALSE)
-               {
-                       g_warning ("codice nullo.");
-                       return FALSE;
-               }
-
-       codice_ = g_strstrip (g_strdup (codice));
-
-       if (strcmp (codice_, "") == 0)
-               {
-                       g_warning ("codice vuoto.");
-                       return FALSE;
-               }
-
-#ifdef HAVE_LIBCONFI
-       /* the first and only parameters must be a Confi object */
-       /* leggo i parametri di connessione dalla configurazione */
-       if (IS_CONFI (parameters->data))
-               {
-                       if (!get_connection_parameters_from_confi (CONFI (parameters->data), &cnc_string))
-                               {
-                                       cnc_string = NULL;
-                               }
-               }
-#endif
-
-       if (cnc_string == NULL)
-               {
-                       GSList *param;
-
-                       param = g_slist_next (parameters);
-                       if (param != NULL && param->data != NULL)
-                               {
-                                       cnc_string = g_strdup ((gchar *)param->data);
-                                       cnc_string = g_strstrip (cnc_string);
-                                       if (g_strcmp0 (cnc_string, "") == 0)
-                                               {
-                                                       cnc_string = NULL;
-                                               }
-                               }
-               }
-
-       if (cnc_string == NULL)
-               {
-                       return FALSE;
-               }
-
-       /* creo un oggetto GdaEx */
-       gdaex = gdaex_new_from_string (cnc_string);
-       if (gdaex == NULL) return FALSE;
-
-       /* elimino _logicamente_ l'utente */
-       sql = g_strdup_printf ("UPDATE users SET status = 'E' WHERE code = '%s'",
-                           gdaex_strescape (codice_, NULL));
-
-       return (gdaex_execute (gdaex, sql) >= 0);
+       return w_users;
 }
diff --git a/src/aute_db.h b/src/aute_db.h
new file mode 100644 (file)
index 0000000..6678484
--- /dev/null
@@ -0,0 +1,31 @@
+/* 
+ * 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 __AUTEDB_H__
+#define __AUTEDB_H__
+
+G_BEGIN_DECLS
+
+
+gchar *autedb_cifra_password (const gchar *password);
+
+
+G_END_DECLS
+
+#endif /* __AUTEDB_H__ */
index 581ae3b6654d552f10d4ea3d1846ce579a7da65c..251c9d3014942362e0eef508241fdc9429ba0645 100644 (file)
@@ -20,6 +20,7 @@
 #include <libgtkform/form.h>
 
 #include "user.h"
+#include "aute_db.h"
 
 static void user_class_init (UserClass *klass);
 static void user_init (User *user);
@@ -51,7 +52,8 @@ static void user_on_btn_salva_clicked (GtkButton *button,
 
 enum
 {
-       TXT_CODE
+       TXT_CODE,
+       LBL_PASSWORD
 };
 
 typedef struct _UserPrivate UserPrivate;
@@ -152,6 +154,7 @@ User
 
        priv->objects = gtk_form_get_objects_by_name (priv->form,
                                                      "entry1",
+                                                     "label14",
                                                      NULL);
 
        g_signal_connect (priv->w,
@@ -226,6 +229,8 @@ user_salva (User *user)
 
        if (priv->code == NULL || g_strcmp0 (priv->code, "") == 0)
                {
+                       gtk_label_set_text (GTK_LABEL (priv->objects[LBL_PASSWORD]),
+                                           autedb_cifra_password (gtk_entry_get_text (GTK_ENTRY (priv->objects[TXT_CODE]))));
                        sql = gtk_form_get_sql (priv->form, GTK_FORM_SQL_INSERT);
                }
        else