]> saetta.ns0.it Git - zakautho/gui/commitdiff
Roles management.
authorAndrea Zagli <azagli@libero.it>
Wed, 8 Jun 2011 15:23:54 +0000 (17:23 +0200)
committerAndrea Zagli <azagli@libero.it>
Wed, 8 Jun 2011 15:23:54 +0000 (17:23 +0200)
data/autoz-gui/gui/Makefile.am
data/autoz-gui/gui/autoz-gui.ui
src/Makefile.am
src/commons.h
src/main.c
src/openauditds.c
src/role.c [new file with mode: 0644]
src/role.h [new file with mode: 0644]
src/roles.c [new file with mode: 0644]
src/roles.h [new file with mode: 0644]

index a73fe1d5f45ff1ef842947b87e511fd86c331888..32f001560777e214fcf3beedc333405da94319b7 100644 (file)
@@ -1,5 +1,5 @@
 guidir = $(datadir)/$(PACKAGE)/gui
 
-gui_DATA = autoz-gui.gui
+gui_DATA = autoz-gui.ui
 
 EXTRA_DIST = $(gui_DATA)
index 5d8a3d2e1e2ee40c5d092b60152bcad63678203b..087108ce4f0c1305bab173a7577dfcc978dd43f6 100644 (file)
@@ -72,6 +72,7 @@
                         <property name="visible">True</property>
                         <property name="label" translatable="yes">_Roles</property>
                         <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_mnu_view_roles_activate"/>
                       </object>
                     </child>
                     <child>
         <property name="orientation">vertical</property>
         <property name="spacing">5</property>
         <child>
-          <placeholder/>
+          <object class="GtkHBox" id="hbox1">
+            <property name="visible">True</property>
+            <property name="spacing">5</property>
+            <child>
+              <object class="GtkLabel" id="label9">
+                <property name="visible">True</property>
+                <property name="xalign">0</property>
+                <property name="label" translatable="yes">Table name prefix</property>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkEntry" id="entry3">
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="invisible_char">&#x2022;</property>
+              </object>
+              <packing>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkFrame" id="frame1">
+            <property name="visible">True</property>
+            <property name="label_xalign">0</property>
+            <child>
+              <placeholder/>
+            </child>
+            <child type="label">
+              <object class="GtkLabel" id="label7">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">&lt;b&gt;Connection parameters&lt;/b&gt;</property>
+                <property name="use_markup">True</property>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
         </child>
         <child>
           <object class="GtkHButtonBox" id="hbuttonbox1">
           </object>
           <packing>
             <property name="expand">False</property>
-            <property name="position">1</property>
+            <property name="position">2</property>
           </packing>
         </child>
       </object>
index 5c053d62ad43083605fa672ef840b28552fe2b39..9d9ea209c231a0ce89e1d7afd9a8377daa6dc28f 100644 (file)
@@ -1,9 +1,9 @@
 guidir = $(datadir)/$(PACKAGE)/gui
 
-AM_CPPFLAGS = $(AUDITGUI_CFLAGS) \
+AM_CPPFLAGS = $(AUTOZGUI_CFLAGS) \
               -DGUIDIR=\""$(guidir)"\"
 
-LIBS = $(AUDITGUI_LIBS) \
+LIBS = $(AUTOZGUI_LIBS) \
        -export-dynamic
 
 bin_PROGRAMS = autoz-gui
@@ -12,4 +12,8 @@ autoz_gui_SOURCES = \
                 commons.h \
                 main.c \
                 openauditds.c \
-                openauditds.h
+                openauditds.h \
+                role.c \
+                role.h \
+                roles.c \
+                roles.h
index 0fc225b2406542552556c96a130c1b4548247800..60b1b718215ae3978658322c0837f9a49d765665 100644 (file)
@@ -36,6 +36,8 @@ typedef struct
 
                GdaConnection *gdacon;
                GdaSqlParser *gdaparser;
+
+               gchar *prefix;
        } Commons;
 
 #endif /* __COMMONS_H__ */
index 0e8b23dd420c34627cbf706d45c0f1a9fb45e709..fd655b5999cd07506f76ebcde4bd282c5d2d3816 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "commons.h"
 #include "openauditds.h"
+#include "roles.h"
 
 G_MODULE_EXPORT void on_mnu_file_close_activate (GtkMenuItem *menuitem,
                             gpointer user_data);
@@ -72,6 +73,17 @@ main_on_ds_opened (gpointer instance, const gchar *arg1, gpointer user_data)
                        return;
                }
 
+       pos = g_strrstr (cnc, "{prefix}");
+       if (pos != NULL)
+               {
+                       cnc = g_strndup (cnc, pos - cnc);
+                       commons->prefix = g_strdup (pos + 8);
+               }
+       else
+               {
+                       commons->prefix = g_strdup ("");
+               }
+
        error = NULL;
        commons->gdacon = gda_connection_open_from_string (NULL, cnc, NULL,
                                                           GDA_CONNECTION_OPTIONS_NONE,
@@ -99,6 +111,7 @@ main_on_ds_opened (gpointer instance, const gchar *arg1, gpointer user_data)
                }
 
        gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (commons->gtkbuilder, "menuitem2")), TRUE);
+       gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (commons->gtkbuilder, "menuitem3")), TRUE);
 
        g_free (cnc);
 }
@@ -133,6 +146,20 @@ on_mnu_file_close_activate (GtkMenuItem *menuitem,
        vbx_body_child = NULL;
 
        gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (commons->gtkbuilder, "menuitem2")), FALSE);
+       gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (commons->gtkbuilder, "menuitem3")), FALSE);
+}
+
+G_MODULE_EXPORT void
+on_mnu_view_roles_activate (GtkMenuItem *menuitem,
+                            gpointer user_data)
+{
+       GtkWidget *vbx;
+
+       Roles *m = roles_new (commons, FALSE);
+
+       vbx = roles_get_widget (m);
+
+       main_set_vbx_body_child (vbx);
 }
 
 G_MODULE_EXPORT void
@@ -148,7 +175,7 @@ on_mnu_help_about_activate (GtkMenuItem *menuitem,
                                           &error);
        if (error != NULL)
                {
-                       g_error ("Errore: %s.", error->message);
+                       g_error ("Error: %s.", error->message);
                }
 
        diag = GTK_WIDGET (gtk_builder_get_object (commons->gtkbuilder, "dlg_about"));
@@ -203,7 +230,7 @@ main (int argc, char *argv[])
                                           &error);
        if (error != NULL)
                {
-                       g_error ("Errore: %s", error->message);
+                       g_error ("Error: %s", error->message);
                }
 
        gtk_builder_connect_signals (commons->gtkbuilder, NULL);
@@ -217,6 +244,7 @@ main (int argc, char *argv[])
        gtk_widget_show (w);
 
        gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (commons->gtkbuilder, "menuitem2")), FALSE);
+       gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (commons->gtkbuilder, "menuitem3")), FALSE);
 
        if (argc > 0)
                {
index e7aa48f941e69c929ac13894b15e08be97a6eb80..c9f33b38fc3dce450108718f931826bd488afb14 100644 (file)
@@ -109,15 +109,15 @@ OpenAuditDS
 
        error = NULL;
        gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile,
-                                          g_strsplit ("w_open_audit_datasource", "|", -1),
+                                          g_strsplit ("w_open_autoz_datasource", "|", -1),
                                           &error);
        if (error != NULL)
                {
-                       g_warning ("Errore: %s.", error->message);
+                       g_warning ("Error: %s.", error->message);
                        return NULL;
                }
 
-       priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_open_audit_datasource"));
+       priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_open_autoz_datasource"));
 
        g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button1"),
                          "clicked", G_CALLBACK (open_audit_ds_on_btn_cancel_clicked), (gpointer *)a);
@@ -134,10 +134,8 @@ OpenAuditDS
        g_signal_connect (G_OBJECT (priv->wlogin), "changed",
                          G_CALLBACK (open_audit_ds_on_wlogin_changed), (gpointer *)a);
 
-       gtk_box_pack_start (GTK_BOX (gtk_builder_get_object (priv->commons->gtkbuilder, "vbox2")),
-                           priv->wlogin, TRUE, TRUE, 0);
-       gtk_box_reorder_child (GTK_BOX (gtk_builder_get_object (priv->commons->gtkbuilder, "vbox2")),
-                              priv->wlogin, 0);
+       gtk_container_add (GTK_CONTAINER (gtk_builder_get_object (priv->commons->gtkbuilder, "frame1")),
+                           priv->wlogin);
 
        gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button2")), FALSE);
 
@@ -162,7 +160,6 @@ static void
 open_audit_ds_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
 {
        OpenAuditDS *open_audit_ds = OPEN_AUDIT_DS (object);
-
        OpenAuditDSPrivate *priv = OPEN_AUDIT_DS_GET_PRIVATE (open_audit_ds);
 
        switch (property_id)
@@ -177,7 +174,6 @@ static void
 open_audit_ds_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
 {
        OpenAuditDS *open_audit_ds = OPEN_AUDIT_DS (object);
-
        OpenAuditDSPrivate *priv = OPEN_AUDIT_DS_GET_PRIVATE (open_audit_ds);
 
        switch (property_id)
@@ -210,7 +206,6 @@ open_audit_ds_on_btn_cancel_clicked (GtkButton *button,
                                      gpointer user_data)
 {
        OpenAuditDS *open_audit_ds = (OpenAuditDS *)user_data;
-
        OpenAuditDSPrivate *priv = OPEN_AUDIT_DS_GET_PRIVATE (open_audit_ds);
 
        gtk_widget_destroy (priv->w);
diff --git a/src/role.c b/src/role.c
new file mode 100644 (file)
index 0000000..77c962a
--- /dev/null
@@ -0,0 +1,323 @@
+/* 
+ * Copyright (C) 2011 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 <libgda-ui/libgda-ui.h>
+#include <sql-parser/gda-sql-parser.h>
+
+#include "role.h"
+
+static void role_class_init (RoleClass *klass);
+static void role_init (Role *role);
+
+static void role_load (Role *role);
+static void role_save (Role *role);
+
+static void role_set_property (GObject *object,
+                                     guint property_id,
+                                     const GValue *value,
+                                     GParamSpec *pspec);
+static void role_get_property (GObject *object,
+                                     guint property_id,
+                                     GValue *value,
+                                     GParamSpec *pspec);
+
+static void role_on_btn_cancel_clicked (GtkButton *button,
+                                    gpointer user_data);
+static void role_on_btn_save_clicked (GtkButton *button,
+                                  gpointer user_data);
+
+#define ROLE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_ROLE, RolePrivate))
+
+typedef struct _RolePrivate RolePrivate;
+struct _RolePrivate
+       {
+               Commons *commons;
+
+               GtkWidget *w;
+
+               gint id;
+       };
+
+G_DEFINE_TYPE (Role, role, G_TYPE_OBJECT)
+
+static void
+role_class_init (RoleClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       g_type_class_add_private (object_class, sizeof (RolePrivate));
+
+       object_class->set_property = role_set_property;
+       object_class->get_property = role_get_property;
+
+       /**
+        * Role::updated:
+        * @role:
+        *
+        */
+       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
+role_init (Role *role)
+{
+       RolePrivate *priv = ROLE_GET_PRIVATE (role);
+}
+
+/**
+ * role_new:
+ * @commons:
+ * @id:
+ *
+ * Returns: the newly created #Role object.
+ */
+Role
+*role_new (Commons *commons, gint id)
+{
+       GError *error;
+
+       Role *a = ROLE (g_object_new (role_get_type (), NULL));
+
+       RolePrivate *priv = ROLE_GET_PRIVATE (a);
+
+       priv->commons = commons;
+
+       error = NULL;
+       gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile,
+                                          g_strsplit ("w_role", "|", -1),
+                                          &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error: %s.", error->message);
+                       return NULL;
+               }
+
+       priv->w = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "w_role"));
+
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button3"),
+                         "clicked", G_CALLBACK (role_on_btn_cancel_clicked), (gpointer *)a);
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button4"),
+                         "clicked", G_CALLBACK (role_on_btn_save_clicked), (gpointer *)a);
+
+       priv->id = id;
+       if (priv->id == 0)
+               {
+                       gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label2")), "");
+               }
+       else
+               {
+                       gtk_label_set_text (GTK_LABEL (gtk_builder_get_object (priv->commons->gtkbuilder, "label2")), g_strdup_printf ("%d", priv->id));
+                       role_load (a);
+               }
+
+       return a;
+}
+
+/**
+ * role_get_widget:
+ * @role:
+ *
+ */
+GtkWidget
+*role_get_widget (Role *role)
+{
+       RolePrivate *priv = ROLE_GET_PRIVATE (role);
+
+       return priv->w;
+}
+
+/* PRIVATE */
+static void
+role_load (Role *role)
+{
+       RolePrivate *priv = ROLE_GET_PRIVATE (role);
+
+       GError *error;
+       gchar *sql;
+       GdaStatement *stmt;
+       GdaDataModel *dm;
+
+       sql = g_strdup_printf ("SELECT role_id FROM %sroles WHERE id = %d",
+                              priv->commons->prefix,
+                              priv->id);
+       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+       g_free (sql);
+       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)
+               {
+                       gtk_entry_set_text (GTK_ENTRY (gtk_builder_get_object (priv->commons->gtkbuilder, "entry1")),
+                                           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
+role_save (Role *role)
+{
+       const GdaDsnInfo *info;
+       GError *error;
+       gchar *sql;
+       GdaStatement *stmt;
+       GdaDataModel *dm;
+       GtkWidget *dialog;
+
+       RoleClass *klass = ROLE_GET_CLASS (role);
+
+       RolePrivate *priv = ROLE_GET_PRIVATE (role);
+
+       if (priv->id == 0)
+               {
+                       /* find the new id */
+                       guint new_id;
+
+                       new_id = 0;
+                       sql = g_strdup_printf ("SELECT COALESCE (MAX (id), 0) FROM %sroles", priv->commons->prefix);
+                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+                       g_free (sql);
+                       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 %sroles (id, role_id)"
+                                              " VALUES (%d, '%s',)",
+                                              priv->commons->prefix,
+                                              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 %sroles"
+                                              " SET"
+                                              " role_id = '%s'"
+                                              " WHERE id = %d",
+                                              priv->commons->prefix,
+                                              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);
+               }
+       g_free (sql);
+
+       error = NULL;
+       if (gda_connection_statement_execute_non_select (priv->commons->gdacon, stmt, NULL, NULL, &error) == 1)
+               {
+                       g_object_unref (stmt);
+
+                       g_signal_emit (role, 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, "label2"))), 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, "label2")), "");
+                               }
+
+                       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
+role_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
+{
+       Role *role = ROLE (object);
+       RolePrivate *priv = ROLE_GET_PRIVATE (role);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+role_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
+{
+       Role *role = ROLE (object);
+       RolePrivate *priv = ROLE_GET_PRIVATE (role);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+/* CALLBACK */
+static void
+role_on_btn_cancel_clicked (GtkButton *button,
+                        gpointer user_data)
+{
+       Role *role = (Role *)user_data;
+
+       RolePrivate *priv = ROLE_GET_PRIVATE (role);
+
+       gtk_widget_destroy (priv->w);
+}
+
+static void
+role_on_btn_save_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       role_save ((Role *)user_data);
+}
diff --git a/src/role.h b/src/role.h
new file mode 100644 (file)
index 0000000..43888ed
--- /dev/null
@@ -0,0 +1,65 @@
+/* 
+ * Copyright (C) 2011 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 __ROLE_H__
+#define __ROLE_H__
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <gtk/gtk.h>
+
+#include "commons.h"
+
+G_BEGIN_DECLS
+
+
+#define TYPE_ROLE                 (role_get_type ())
+#define ROLE(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_ROLE, Role))
+#define ROLE_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_ROLE, RoleClass))
+#define IS_ROLE(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_ROLE))
+#define IS_ROLE_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_ROLE))
+#define ROLE_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_ROLE, RoleClass))
+
+
+typedef struct _Role Role;
+typedef struct _RoleClass RoleClass;
+
+struct _Role
+       {
+               GObject parent;
+       };
+
+struct _RoleClass
+       {
+               GObjectClass parent_class;
+
+               guint updated_signal_id;
+       };
+
+GType role_get_type (void) G_GNUC_CONST;
+
+Role *role_new (Commons *commons, gint id);
+
+GtkWidget *role_get_widget (Role *role);
+
+
+G_END_DECLS
+
+#endif /* __ROLE_H__ */
diff --git a/src/roles.c b/src/roles.c
new file mode 100644 (file)
index 0000000..c8ac30b
--- /dev/null
@@ -0,0 +1,390 @@
+/* 
+ * Copyright (C) 2011 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 <string.h>
+
+#include <sql-parser/gda-sql-parser.h>
+
+#include "roles.h"
+#include "role.h"
+
+static void roles_class_init (RolesClass *klass);
+static void roles_init (Roles *roles);
+
+static void roles_load (Roles *roles);
+static void roles_edit (Roles *roles);
+
+static void roles_on_role_updated (gpointer instance, gpointer user_data);
+
+static void roles_set_property (GObject *object,
+                                     guint property_id,
+                                     const GValue *value,
+                                     GParamSpec *pspec);
+static void roles_get_property (GObject *object,
+                                     guint property_id,
+                                     GValue *value,
+                                     GParamSpec *pspec);
+
+static void roles_on_btn_new_clicked (GtkButton *button,
+                      gpointer user_data);
+static void roles_on_btn_edit_clicked (GtkButton *button,
+                      gpointer user_data);
+static void roles_on_btn_delete_clicked (GtkButton *button,
+                        gpointer user_data);
+static void roles_on_trv_roles_row_activated (GtkTreeView *tree_view,
+                                             GtkTreePath *tree_path,
+                                             GtkTreeViewColumn *column,
+                                             gpointer user_data);
+
+#define ROLES_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_ROLES, RolesPrivate))
+
+enum
+{
+       COL_ID,
+       COL_NAME
+};
+
+typedef struct _RolesPrivate RolesPrivate;
+struct _RolesPrivate
+       {
+               Commons *commons;
+
+               GtkWidget *widget;
+
+               GtkTreeSelection *selection;
+               GtkListStore *lstore_roles;
+       };
+
+G_DEFINE_TYPE (Roles, roles, G_TYPE_OBJECT)
+
+static void
+roles_class_init (RolesClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+       g_type_class_add_private (object_class, sizeof (RolesPrivate));
+
+       object_class->set_property = roles_set_property;
+       object_class->get_property = roles_get_property;
+}
+
+static void
+roles_init (Roles *roles)
+{
+       RolesPrivate *priv = ROLES_GET_PRIVATE (roles);
+}
+
+/**
+ * roles_new:
+ * @commons:
+ * @selection:
+ *
+ * Returns: the newly created #Roles object.
+ */
+Roles
+*roles_new (Commons *commons, gboolean selection)
+{
+       GError *error;
+
+       Roles *a = ROLES (g_object_new (roles_get_type (), NULL));
+
+       RolesPrivate *priv = ROLES_GET_PRIVATE (a);
+
+       priv->commons = commons;
+
+       error = NULL;
+       gtk_builder_add_objects_from_file (priv->commons->gtkbuilder, priv->commons->guifile,
+                                          g_strsplit (g_strconcat ("lstore_roles",
+                                                                   (selection ? "|w_roles" : "|vbox5"),
+                                                                   NULL),
+                                                      "|", -1),
+                                          &error);
+       if (error != NULL)
+               {
+                       g_warning ("Error: %s.", error->message);
+                       return NULL;
+               }
+
+       priv->widget = GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, (selection ? "w_roles" : "vbox5")));
+       priv->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview2")));
+       priv->lstore_roles = GTK_LIST_STORE (gtk_builder_get_object (priv->commons->gtkbuilder, "lstore_roles"));
+
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button7"),
+                         "clicked", G_CALLBACK (roles_on_btn_new_clicked), (gpointer)a);
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button9"),
+                         "clicked", G_CALLBACK (roles_on_btn_edit_clicked), (gpointer)a);
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "button8"),
+                         "clicked", G_CALLBACK (roles_on_btn_delete_clicked), (gpointer)a);
+       g_signal_connect (gtk_builder_get_object (priv->commons->gtkbuilder, "treeview2"),
+                         "row-activated", G_CALLBACK (roles_on_trv_roles_row_activated), (gpointer)a);
+
+       if (!selection)
+               {
+                       gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button10")));
+                       gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (priv->commons->gtkbuilder, "button13")));
+               }
+
+       roles_load (a);
+
+       return a;
+}
+
+/**
+ * roles_get_widget:
+ * @roles:
+ *
+ */
+GtkWidget
+*roles_get_widget (Roles *roles)
+{
+       RolesPrivate *priv;
+
+       g_return_val_if_fail (IS_ROLES (roles), NULL);
+
+       priv = ROLES_GET_PRIVATE (roles);
+
+       return priv->widget;
+}
+
+/* PRIVATE */
+static void
+roles_load (Roles *roles)
+{
+       GtkTreeIter iter;
+
+       gchar *sql;
+
+       GdaStatement *stmt;
+       GError *error;
+       GdaDataModel *dm;
+
+       gint rows;
+       gint row;
+
+       RolesPrivate *priv = ROLES_GET_PRIVATE (roles);
+
+       gtk_list_store_clear (priv->lstore_roles);
+
+       sql = g_strdup_printf ("SELECT id, role_id"
+                              " FROM %sroles"
+                              " ORDER BY role_id",
+                              priv->commons->prefix);
+       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser, sql, NULL, NULL);
+       g_free (sql);
+       dm = gda_connection_statement_execute_select (priv->commons->gdacon, stmt, NULL, &error);
+       g_object_unref (stmt);
+       if (dm != NULL)
+               {
+                       rows = gda_data_model_get_n_rows (dm);
+                       for (row = 0; row < rows; row++)
+                               {
+                                       gtk_list_store_append (priv->lstore_roles, &iter);
+                                       gtk_list_store_set (priv->lstore_roles, &iter,
+                                                           COL_ID, g_value_get_int (gda_data_model_get_value_at (dm, 0, row, NULL)),
+                                                           COL_NAME, gda_value_stringify (gda_data_model_get_value_at (dm, 1, row, NULL)),
+                                                           -1);
+                               }
+
+                       g_object_unref (dm);
+               }
+       else
+               {
+                       /* TODO */
+               }
+}
+
+static void
+roles_edit (Roles *roles)
+{
+       GtkTreeIter iter;
+       guint id;
+
+       RolesPrivate *priv = ROLES_GET_PRIVATE (roles);
+
+       if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter))
+               {
+                       GtkWidget *w;
+
+                       gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_roles), &iter,
+                                           COL_ID, &id,
+                                           -1);
+
+                       Role *c = role_new (priv->commons, id);
+
+                       g_signal_connect (G_OBJECT (c), "updated",
+                                         G_CALLBACK (roles_on_role_updated), (gpointer)roles);
+
+                       w = role_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
+               {
+                       GtkWidget *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 role.");
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+               }
+}
+
+static void
+roles_on_role_updated (gpointer instance, gpointer user_data)
+{
+       roles_load ((Roles *)user_data);
+}
+
+static void
+roles_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
+{
+       Roles *roles = ROLES (object);
+       RolesPrivate *priv = ROLES_GET_PRIVATE (roles);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+static void
+roles_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
+{
+       Roles *roles = ROLES (object);
+       RolesPrivate *priv = ROLES_GET_PRIVATE (roles);
+
+       switch (property_id)
+               {
+                       default:
+                               G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                               break;
+               }
+}
+
+/* CALLBACK */
+static void
+roles_on_btn_new_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       GtkWidget *w;
+
+       Roles *roles = (Roles *)user_data;
+       RolesPrivate *priv = ROLES_GET_PRIVATE (roles);
+
+       Role *c = role_new (priv->commons, 0);
+
+       g_signal_connect (G_OBJECT (c), "updated",
+                         G_CALLBACK (roles_on_role_updated), (gpointer)roles);
+
+       w = role_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);
+}
+
+static void
+roles_on_btn_edit_clicked (GtkButton *button,
+                      gpointer user_data)
+{
+       roles_edit ((Roles *)user_data);
+}
+
+static void
+roles_on_btn_delete_clicked (GtkButton *button,
+                        gpointer user_data)
+{
+       GtkWidget *dialog;
+       gboolean risp;
+
+       GtkTreeIter iter;
+       guint id;
+
+       Roles *roles = (Roles *)user_data;
+       RolesPrivate *priv = ROLES_GET_PRIVATE (roles);
+
+       if (gtk_tree_selection_get_selected (priv->selection, NULL, &iter))
+               {
+                       dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_builder_get_object (priv->commons->gtkbuilder, "w_main")),
+                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                        GTK_MESSAGE_QUESTION,
+                                                        GTK_BUTTONS_YES_NO,
+                                                        "Are you sure to want to delete the selected role?");
+                       risp = gtk_dialog_run (GTK_DIALOG (dialog));
+                       if (risp == GTK_RESPONSE_YES)
+                               {
+                                       GError *error;
+                                       GdaStatement *stmt;
+
+                                       gtk_tree_model_get (GTK_TREE_MODEL (priv->lstore_roles), &iter,
+                                                           COL_ID, &id,
+                                                           -1);
+
+                                       error = NULL;
+                                       stmt = gda_sql_parser_parse_string (priv->commons->gdaparser,
+                                                                           g_strdup_printf ("DELETE %sroles WHERE id = %d", priv->commons->prefix, 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 role.");
+                                                       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")),
+                                                                                        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."));
+                                               }
+
+                                       roles_load (roles);
+                               }
+                       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 role.");
+                       gtk_dialog_run (GTK_DIALOG (dialog));
+                       gtk_widget_destroy (dialog);
+               }
+}
+
+static void
+roles_on_trv_roles_row_activated (GtkTreeView *tree_view,
+                                             GtkTreePath *tree_path,
+                                             GtkTreeViewColumn *column,
+                                             gpointer user_data)
+{
+       RolesPrivate *priv = ROLES_GET_PRIVATE ((Roles *)user_data);
+
+       roles_edit ((Roles *)user_data);
+}
diff --git a/src/roles.h b/src/roles.h
new file mode 100644 (file)
index 0000000..8db3586
--- /dev/null
@@ -0,0 +1,63 @@
+/* 
+ * Copyright (C) 2011 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 __ROLES_H__
+#define __ROLES_H__
+
+#include <glib.h>
+#include <glib-object.h>
+
+#include <gtk/gtk.h>
+
+#include "commons.h"
+
+G_BEGIN_DECLS
+
+
+#define TYPE_ROLES                 (roles_get_type ())
+#define ROLES(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_ROLES, Roles))
+#define ROLES_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_ROLES, RolesClass))
+#define IS_ROLES(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_ROLES))
+#define IS_ROLES_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_ROLES))
+#define ROLES_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_ROLES, RolesClass))
+
+
+typedef struct _Roles Roles;
+typedef struct _RolesClass RolesClass;
+
+struct _Roles
+       {
+               GObject parent;
+       };
+
+struct _RolesClass
+       {
+               GObjectClass parent_class;
+       };
+
+GType roles_get_type (void) G_GNUC_CONST;
+
+Roles *roles_new (Commons *commons, gboolean selection);
+
+GtkWidget *roles_get_widget (Roles *roles);
+
+
+G_END_DECLS
+
+#endif /* __ROLES_H__ */