+++ /dev/null
-/*
- * Copyright (C) 2005-2006 Andrea Zagli <azagli@inwind.it>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-#include <string.h>
-#include <locale.h>
-
-#include <libgdaobj.h>
-
-#include "libform.h"
-
-static void form_class_init (FormClass *class);
-static void form_init (Form *form);
-
-static Field *form_get_field (Form *form, const gchar *name);
-static gchar *form_field_get_value_sql (Field *f);
-static gboolean form_field_check_struct (Field *f);
-
-
-#define FORM_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TYPE_FORM, FormPrivate))
-
-typedef struct _FormPrivate FormPrivate;
-struct _FormPrivate
- {
- GList fields;
- GladeXML *glade;
-
- struct lconv *loc;
- };
-
-
-GType
-form_get_type (void)
-{
- static GType form_type = 0;
-
- if (!form_type)
- {
- static const GTypeInfo form_info =
- {
- sizeof (FormClass),
- NULL, /* base_init */
- NULL, /* base_finalize */
- (GClassInitFunc) form_class_init,
- NULL, /* class_finalize */
- NULL, /* class_data */
- sizeof (Form),
- 0, /* n_preallocs */
- (GInstanceInitFunc) form_init,
- NULL
- };
-
- form_type = g_type_register_static (G_TYPE_OBJECT, "Form",
- &form_info, 0);
- }
-
- return form_type;
-}
-
-static void
-form_class_init (FormClass *class)
-{
- GObjectClass *object_class = G_OBJECT_CLASS (class);
-
- g_type_class_add_private (object_class, sizeof (FormPrivate));
-}
-
-static void
-form_init (Form *form)
-{
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- priv->fields = NULL;
- priv->glade = NULL;
- priv->loc = localeconv ();
-}
-
-/**
- * form_new:
- *
- * Returns: the newly created #Form.
- */
-Form
-*form_new ()
-{
- return FORM (g_object_new (form_get_type (), NULL));
-}
-
-/**
- * form_set_glade:
- * @glade:
- *
- * Returns: the newly created #Form.
- */
-gboolean
-form_set_glade (Form *form, GladeXML *glade)
-{
- gboolean ret = FALSE;
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- priv->glade = glade;
-
- /* TO DO */
- /* setting fields with glade attributes */
-
- ret = TRUE;
-
- return ret;
-}
-
-gboolean
-form_add_field (Form *form, FormField *field)
-{
- gboolean ret = FALSE;
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- priv->field = g_list_append (priv->fields, field);
-
- if (priv->glade != NULL)
- {
- /* TO DO */
- /* setting fields with glade attributes */
- }
-
- ret = TRUE;
-
- return ret;
-}
-
-gboolean
-form_remove_field (Form *form, FormField *field)
-{
- gboolean ret = FALSE;
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- priv->field = g_list_remove (priv->fields, field);
-
- ret = TRUE;
-
- return ret;
-}
-
-/**
- * form_sql_get:
- * @form: a #Form.
- * @type:
- * @table:
- *
- */
-gchar
-*form_get_sql (Form *form, FrmSqlType type, const gchar *table, ...)
-{
- gchar *sql, *fields = "", *keys, *pkeys = NULL, *pname;
- gint i;
- Field *f;
- gboolean with_key;
- va_list ap;
-
- if (table == NULL)
- {
- return NULL;
- }
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- with_key = (type == FRM_SQL_TYPE_UPDATE ||
- type == FRM_SQL_TYPE_DELETE);
-
- va_start (ap, table);
-
- while (TRUE)
- {
- pname = va_arg (ap, gchar *);
- if (pname == NULL)
- {
- break;
- }
-
- if (strcmp (pname, "with-key") == 0)
- {
- with_key = va_arg (ap, gboolean);
- }
- else
- {
- pkeys = g_strjoin (NULL, " AND ", pname, " = ", NULL);
- f = form_get_field (form, pname);
- switch (f->field_type)
- {
- case FRM_FIELD_TYPE_TEXT:
- pkeys = g_strjoin (NULL, pkeys, "'",
- s_strescape (va_arg (ap, gchar *), NULL),
- "'", NULL);
- break;
-
- case FRM_FIELD_TYPE_INTEGER:
- pkeys = g_strjoin (NULL, pkeys,
- g_strdup_printf ("%d", va_arg (ap, gint)),
- NULL);
- break;
-
- case FRM_FIELD_TYPE_FLOAT:
- pkeys = g_strjoin (NULL, pkeys,
- g_strdup_printf ("%f", va_arg (ap, double)),
- NULL);
- break;
-
- case FRM_FIELD_TYPE_BOOLEAN:
- pkeys = g_strjoin (NULL, pkeys, "'",
- (va_arg (ap, gboolean) == TRUE ? "-1" : "0"),
- "'", NULL);
- break;
-
- case FRM_FIELD_TYPE_DATE:
- {
- GDate *date = va_arg (ap, GDate *);
- pkeys = g_strjoin (NULL, pkeys, "'",
- g_strdup_printf ("%04d%02d%02d",
- g_date_get_year (date),
- g_date_get_month (date),
- g_date_get_day (date)),
- "'", NULL);
- }
- break;
- }
- }
- }
-
- va_end (ap);
-
- switch (type)
- {
- case FRM_SQL_TYPE_SELECT:
- {
- sql = g_strdup ("SELECT ");
-
- for (i = 0; i < priv->nfields; i++)
- {
- f = priv->fields[i];
-
- if (strcmp (f->table_name, table) == 0)
- {
- if (with_key && pkeys == NULL && f->key)
- {
- keys = g_strjoin (NULL, keys, " AND ", f->field_name, " = ",
- form_field_get_value_sql (f), NULL);
- }
- fields = g_strjoin (NULL, fields, f->field_name, ", ", NULL);
- }
- }
-
- fields[strlen (fields) - 2] = ' ';
-
- sql = g_strjoin (NULL, sql, fields, "FROM ", g_strdup_printf ("%s", table), NULL);
- if (with_key)
- {
- sql = g_strjoin (NULL, sql, " WHERE TRUE", (pkeys != NULL ? pkeys : keys), NULL);
- }
- break;
- }
-
- case FRM_SQL_TYPE_INSERT:
- {
- gchar *values = "(";
-
- sql = g_strdup_printf ("INSERT INTO %s ", table);
-
- for (i = 0; i < priv->nfields; i++)
- {
- f = priv->fields[i];
-
- if (strcmp (f->table_name, table) == 0)
- {
- fields = g_strjoin (NULL, fields, f->field_name, ", ", NULL);
- values = g_strjoin (NULL, values, form_field_get_value_sql (f), ", ", NULL);
- }
- }
-
- fields[strlen (fields) - 2] = ')';
- values[strlen (values) - 2] = ')';
-
- sql = g_strjoin (NULL, sql, "(", fields, " VALUES ", values, NULL);
- break;
- }
-
- case FRM_SQL_TYPE_UPDATE:
- {
- sql = g_strdup_printf ("UPDATE %s SET ", table);
-
- for (i = 0; i < priv->nfields; i++)
- {
- f = priv->fields[i];
-
- if (strcmp (f->table_name, table) == 0)
- {
- if (with_key && f->key)
- {
- keys = g_strjoin (NULL, keys, " AND ", f->field_name, " = ",
- form_field_get_value_sql (f), NULL);
- }
- else
- {
- fields = g_strjoin (NULL, fields, f->field_name, " = ", form_field_get_value_sql (f), ", ", NULL);
- }
- }
- }
-
- fields[strlen (fields) - 2] = ' ';
-
- sql = g_strjoin (NULL, sql, fields, keys, NULL);
- break;
- }
-
- case FRM_SQL_TYPE_DELETE:
- {
- sql = g_strdup_printf ("DELETE FROM %s%s", table, (with_key ? " WHERE TRUE" : ""));
-
- if (with_key)
- {
- if (pkeys != NULL)
- {
- sql = g_strjoin (NULL, sql, pkeys, NULL);
- }
- else
- {
- for (i = 0; i < priv->nfields; i++)
- {
- f = priv->fields[i];
-
- if (strcmp (f->table_name, table) == 0 && f->key)
- {
- sql = g_strjoin (NULL, sql, " AND ", f->field_name, "=",
- form_field_get_value_sql (f), NULL);
- }
- }
- }
- }
- break;
- }
- }
-
- return sql;
-}
-
-/**
- * form_clear:
- * @form: a #Form.
- *
- * Clears the form.
- */
-gboolean
-form_clear (Form *form)
-{
- gint i;
- Field *f;
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- for (i = 0; i < priv->nfields; i++)
- {
- f = priv->fields[i];
-
- if (f->widget_type == GTK_TYPE_LABEL)
- {
- gtk_label_set_text (GTK_LABEL (f->widget), f->default_value);
- }
- else if (f->widget_type == GTK_TYPE_ENTRY)
- {
- gtk_entry_set_text (GTK_ENTRY (f->widget), f->default_value);
- }
- else if (f->widget_type == GTK_TYPE_SPIN_BUTTON)
- {
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (f->widget), atof (f->default_value));
- }
- else if (f->widget_type == GTK_TYPE_COMBO_BOX)
- {
- gtk_combo_box_set_active (GTK_COMBO_BOX (f->widget), atoi (f->default_value));
- }
- }
-}
-
-/**
- * form_fill_from_datamodel:
- * @form: a #Form.
- * @dm:
- *
- */
-gboolean
-form_fill_from_datamodel (Form *form, GdaDataModel *dm, gint row)
-{
- gint nfield;
- Field *f;
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- for (nfield = 0; nfield < priv->nfields; nfield++)
- {
- f = priv->fields[nfield];
-
- if (f->widget_type == GTK_TYPE_LABEL)
- {
- gtk_label_set_text (GTK_LABEL (f->widget),
- gdao_data_model_get_field_value_stringify_at (dm, row, f->field_name));
- }
- else if (f->widget_type == GTK_TYPE_ENTRY)
- {
- gtk_entry_set_text (GTK_ENTRY (f->widget),
- gdao_data_model_get_field_value_stringify_at (dm, row, f->field_name));
- }
- else if (f->widget_type == GTK_TYPE_SPIN_BUTTON)
- {
- gtk_spin_button_set_value (GTK_SPIN_BUTTON (f->widget),
- gdao_data_model_get_field_value_double_at (dm, row, f->field_name));
- }
- else if (f->widget_type == GTK_TYPE_COMBO_BOX)
- {
- }
- }
-
- return TRUE;
-}
-
-/**
- * form_check:
- * @form: a #Form.
- *
- */
-gboolean
-form_check (Form *form)
-{
- gboolean ret = FALSE;
- gint i;
- Field *f;
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- for (i = 0; i < priv->nfields; i++)
- {
- f = priv->fields[i];
- switch (f->field_type)
- {
- case FRM_FIELD_TYPE_TEXT:
- {
- if (f->obligatory && strcmp (gtk_entry_get_text (GTK_ENTRY (f->widget)), "") == 0)
- {
- return FALSE;
- }
- break;
- }
-
- case FRM_FIELD_TYPE_INTEGER:
- case FRM_FIELD_TYPE_FLOAT:
- {
- break;
- }
- }
- }
-
- return ret;
-}
-
-/**
- * form_field_get:
- * @form:
- * @field:
- * @value:
- *
- */
-gboolean
-form_field_get (Form *form, gint field, GValue *value)
-{
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- if (field > priv->nfields)
- {
- return FALSE;
- }
-
- Field *f = priv->fields[field];
-
- if (f->widget_type == GTK_TYPE_LABEL)
- {
- g_value_init (value, G_TYPE_STRING);
- g_value_set_string (value, gtk_label_get_text (GTK_LABEL (f->widget)));
- }
- else if (f->widget_type == GTK_TYPE_ENTRY)
- {
- g_value_init (value, G_TYPE_STRING);
- g_value_set_string (value, gtk_entry_get_text (GTK_ENTRY (f->widget)));
- }
- else if (f->widget_type == GTK_TYPE_SPIN_BUTTON)
- {
- if (f->field_type == FRM_FIELD_TYPE_INTEGER)
- {
- g_value_init (value, G_TYPE_INT);
- g_value_set_int (value, gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (f->widget)));
- }
- else if (f->field_type == FRM_FIELD_TYPE_FLOAT)
- {
- g_value_init (value, G_TYPE_FLOAT);
- g_value_set_float (value, gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (f->widget)));
- }
- }
- else if (f->widget_type == GTK_TYPE_COMBO_BOX)
- {
- }
-
- return TRUE;
-}
-
-/* PRIVATE */
-static Field
-*form_get_field (Form *form, const gchar *name)
-{
- gint i;
- Field *f = NULL;
-
- FormPrivate *priv = FORM_GET_PRIVATE (form);
-
- for (i = 0; i < priv->nfields; i++)
- {
- f = priv->fields[i];
- if (strcmp (f->field_name, name) == 0)
- {
- break;
- }
- }
-
- return f;
-}
-
-static gchar
-*form_field_get_value_sql (Field *f)
-{
- gchar *ret = "";
-
- FormPrivate *priv = FORM_GET_PRIVATE (f->form);
-
- if (f->widget_type == GTK_TYPE_LABEL)
- {
- ret = (gchar *)gtk_label_get_text (GTK_LABEL (f->widget));
- }
- else if (f->widget_type == GTK_TYPE_ENTRY)
- {
- ret = (gchar *)gtk_entry_get_text (GTK_ENTRY (f->widget));
- }
- else if (f->widget_type == GTK_TYPE_SPIN_BUTTON)
- {
- if (f->field_type == FRM_FIELD_TYPE_INTEGER)
- {
- ret = g_strdup_printf ("%d", gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (f->widget)));
- }
- else if (f->field_type == FRM_FIELD_TYPE_FLOAT)
- {
- ret = g_strdup_printf ("%f", gtk_spin_button_get_value_as_float (GTK_SPIN_BUTTON (f->widget)));
- ret = g_strjoinv (".", g_strsplit (ret, priv->loc->decimal_point, -1));
- }
- }
-
- switch (f->field_type)
- {
- case FRM_FIELD_TYPE_TEXT:
- ret = g_strjoin (NULL, "'", s_strescape(ret, NULL), "'", NULL);
- break;
- }
-
- return ret;
-}
-
-static gboolean
-form_field_check_struct (Field *f)
-{
- if (f->widget != NULL)
- {
- if (f->widget_type == 0)
- {
- f->widget_type = GTK_WIDGET_TYPE (f->widget);
- }
- }
- else
- {
- return FALSE;
- }
-
- if (f->field_type == 0)
- {
- if (f->widget_type == GTK_TYPE_LABEL || f->widget_type == GTK_TYPE_ENTRY)
- {
- f->field_type = FRM_FIELD_TYPE_TEXT;
- }
- else if (f->widget_type == GTK_TYPE_SPIN_BUTTON)
- {
- if (gtk_spin_button_get_digits (GTK_SPIN_BUTTON (f->widget)) > 0)
- {
- f->field_type = FRM_FIELD_TYPE_FLOAT;
- }
- else
- {
- f->field_type = FRM_FIELD_TYPE_INTEGER;
- }
- }
- else if (f->widget_type == GTK_TYPE_COMBO_BOX)
- {
- f->field_type = FRM_FIELD_TYPE_INTEGER;
- }
- }
-
- switch (f->field_type)
- {
- case FRM_FIELD_TYPE_TEXT:
- {
- if (f->default_value == NULL)
- {
- if (f->widget_type == GTK_TYPE_COMBO_BOX)
- {
- f->default_value = g_strdup ("0");
- }
- else
- {
- f->default_value = g_strdup ("");
- }
- }
- break;
- }
-
- case FRM_FIELD_TYPE_INTEGER:
- {
- if (f->default_value == NULL)
- {
- f->default_value = g_strdup ("0");
- }
- else
- {
- f->default_value = g_strdup_printf ("%d", atoi (f->default_value));
- }
- break;
- }
-
- case FRM_FIELD_TYPE_FLOAT:
- {
- if (f->default_value == NULL)
- {
- f->default_value = g_strdup ("0.0");
- }
- else
- {
- f->default_value = g_strdup_printf ("%f", atof (f->default_value));
- }
- break;
- }
-
- case FRM_FIELD_TYPE_BOOLEAN:
- {
- if (f->default_value == NULL)
- {
- f->default_value = g_strdup ("0");
- }
- else
- {
- f->default_value = g_strdup_printf ("%d",
- atoi (f->default_value) == 0 ||
- atoi (f->default_value) == 1 ?
- atoi (f->default_value) : 0);
- }
- break;
- }
- }
-
- if (f->key)
- {
- /* a key is always obligatory */
- f->obligatory = TRUE;
- }
-
- return TRUE;
-}