From: Andrea Zagli Date: Sat, 27 Aug 2011 16:08:50 +0000 (+0200) Subject: Started migration to GDateTime in GtkFormFieldDateTime. X-Git-Tag: 0.4.0~10 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=1e72e492856c08012e0e1e225ad5bf16b9f161ff;p=libgtkform Started migration to GDateTime in GtkFormFieldDateTime. refs #9 --- diff --git a/configure.ac b/configure.ac index 1d3ca47..610b9de 100644 --- a/configure.ac +++ b/configure.ac @@ -46,7 +46,8 @@ PKG_CHECK_EXISTS(gladeui-1.0 >= 3.2.0, [GLADEUI_FOUND=yes], [GLADEUI_FOUND=no]) AM_CONDITIONAL(GLADEUI, test $GLADEUI_FOUND = yes) -PKG_CHECK_MODULES(GTKFORM, [gtk+-2.0 >= 2.12.0 +PKG_CHECK_MODULES(GTKFORM, [glib-2.0 >= 2.26 + gtk+-2.0 >= 2.12.0 libxml-2.0 >= 2.0.0 libgdaex >= 0.3.0 libgda-4.0 >= 4.2.3]) diff --git a/libgtkform/fielddatetime.c b/libgtkform/fielddatetime.c index 9c75802..6cb3157 100644 --- a/libgtkform/fielddatetime.c +++ b/libgtkform/fielddatetime.c @@ -64,8 +64,8 @@ static gboolean gtk_form_field_datetime_is_valid_datetime (GtkFormFieldDateTime typedef struct _GtkFormFieldDateTimePrivate GtkFormFieldDateTimePrivate; struct _GtkFormFieldDateTimePrivate { - struct tm *default_value; - struct tm *original_value; + GDateTime *default_value; + GDateTime *original_value; gint type; @@ -173,12 +173,12 @@ gchar * gtk_form_field_datetime_get_value: * @field: a #GtkFormField object. * - * Returns: a #GValue of type G_TYPE_POINTER that point to a struct tm. + * Returns: a #GValue of type G_TYPE_POINTER that point to a #GDateTime. */ GValue *gtk_form_field_datetime_get_value (GtkFormField *field) { - struct tm *datetime; + GDateTime *gdatetime; GValue *ret; gchar *value; GtkFormFieldDateTimePrivate *priv; @@ -191,12 +191,12 @@ GValue value = gtk_form_field_datetime_get_value_stringify (field); - datetime = gtk_form_field_datetime_get_tm_from_str (value, priv->display_format); - if (value != NULL && datetime != NULL) + gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (value, priv->display_format); + if (value != NULL && gdatetime != NULL) { ret = g_malloc0 (sizeof (GValue)); g_value_init (ret, G_TYPE_POINTER); - g_value_set_pointer (ret, datetime); + g_value_set_pointer (ret, gdatetime); g_free (value); } @@ -228,14 +228,14 @@ gchar if (value != NULL && format != NULL) { - struct tm *datetime; + GDateTime *gdatetime; - datetime = gtk_form_field_datetime_get_tm_from_str (value, priv->display_format); - if (datetime != NULL) + gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (value, priv->display_format); + if (gdatetime != NULL) { gchar *dvalue; - dvalue = gtk_form_field_datetime_get_str_from_tm (datetime, format); + dvalue = g_date_time_format (gdatetime, format); if (dvalue != NULL) { ret = g_strconcat ("'", dvalue, "'", NULL); @@ -254,7 +254,7 @@ gchar * gtk_form_field_datetime_get_original_value: * @field: a #GtkFormField object. * - * Returns: a #GValue of type G_TYPE_POINTER that point to a struct tm. + * Returns: a #GValue of type G_TYPE_POINTER that point to a #GDateTime. */ GValue *gtk_form_field_datetime_get_original_value (GtkFormField *field) @@ -270,7 +270,7 @@ GValue ret = g_malloc0 (sizeof (GValue)); g_value_init (ret, G_TYPE_POINTER); - g_value_set_pointer (ret, g_memdup (priv->original_value, sizeof (struct tm))); + g_value_set_pointer (ret, g_date_time_ref (priv->original_value)); return ret; } @@ -295,20 +295,24 @@ gtk_form_field_datetime_clear (GtkFormField *field) { gchar *value; - value = gtk_form_field_datetime_get_str_from_tm (priv->default_value, priv->display_format); + value = g_date_time_format (priv->default_value, priv->display_format); if (value != NULL) { ret = gtk_form_field_datetime_set_value_stringify (field, value); g_free (value); } + else + { + ret = gtk_form_field_datetime_set_value_stringify (field, ""); + } } else { ret = gtk_form_field_datetime_set_value_stringify (field, ""); } - priv->original_value = g_memdup (priv->default_value, sizeof (struct tm)); + priv->original_value = g_date_time_ref (priv->default_value); return ret; } @@ -338,25 +342,25 @@ gtk_form_field_datetime_is_changed (GtkFormField *field) { gboolean ret = FALSE; GtkFormFieldDateTimePrivate *priv; - struct tm *value; + GDateTime *value; g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE); priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); - value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field), + value = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format); if (value != NULL && priv->original_value != NULL) { - ret = (value->tm_year != priv->original_value->tm_year - || value->tm_mon != priv->original_value->tm_mon - || value->tm_mday != priv->original_value->tm_mday); + ret = (g_date_time_get_year (value) != g_date_time_get_year (priv->original_value) + || g_date_time_get_month (value) != g_date_time_get_month (priv->original_value) + || g_date_time_get_day_of_month (value) != g_date_time_get_day_of_month (priv->original_value)); if (!ret && priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATETIME) { - ret = (value->tm_hour != priv->original_value->tm_hour - || value->tm_min != priv->original_value->tm_min - || value->tm_sec != priv->original_value->tm_sec); + ret = (g_date_time_get_hour (value) != g_date_time_get_hour (priv->original_value) + || g_date_time_get_minute (value) != g_date_time_get_minute (priv->original_value) + || g_date_time_get_second (value) != g_date_time_get_second (priv->original_value)); } } else if ((value == NULL || priv->original_value == NULL) @@ -365,7 +369,7 @@ gtk_form_field_datetime_is_changed (GtkFormField *field) ret = TRUE; } - g_free (value); + g_date_time_unref (value); return ret; } @@ -382,6 +386,8 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) GtkFormFieldDateTimePrivate *priv; + GDateTime *gdatetime; + struct tm *tm_value; gchar *min_value; @@ -391,10 +397,16 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE); - ret = TRUE; - priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); + gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format); + if (gdatetime == NULL) + { + return FALSE; + } + + ret = TRUE; + tm_value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format); if (tm_value != NULL) @@ -508,27 +520,24 @@ gtk_form_field_datetime_is_valid (GtkFormField *field) void gtk_form_field_datetime_set_as_origin (GtkFormField *field) { - gboolean ret = FALSE; GtkFormFieldDateTimePrivate *priv; - struct tm *value; + GDateTime *value; g_return_if_fail (IS_GTK_FORM_FIELD_DATETIME (field)); priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); - value = gtk_form_field_datetime_get_tm_from_str (gtk_form_field_datetime_get_value_stringify (field), + value = gtk_form_field_datetime_get_gdatetime_from_str (gtk_form_field_datetime_get_value_stringify (field), priv->display_format); - priv->original_value = g_memdup (value, sizeof (struct tm)); - - if (priv->original_value != NULL && priv->type == GTK_FORM_FIELD_DATETIME_TYPE_DATE) - { - priv->original_value->tm_hour = 0; - priv->original_value->tm_min = 0; - priv->original_value->tm_sec = 0; - } + priv->original_value = g_date_time_new_local (priv->type != GTK_FORM_FIELD_DATETIME_TYPE_TIME ? g_date_time_get_year (value) : 1, + priv->type != GTK_FORM_FIELD_DATETIME_TYPE_TIME ?g_date_time_get_month (value) : 1, + priv->type != GTK_FORM_FIELD_DATETIME_TYPE_TIME ?g_date_time_get_day_of_month (value) : 1, + priv->type != GTK_FORM_FIELD_DATETIME_TYPE_DATE ? g_date_time_get_hour (value): 0, + priv->type != GTK_FORM_FIELD_DATETIME_TYPE_DATE ? g_date_time_get_minute (value): 0, + priv->type != GTK_FORM_FIELD_DATETIME_TYPE_DATE ? g_date_time_get_seconds (value): 0.0); - g_free (value); + g_date_time_unref (value); } /** @@ -544,7 +553,7 @@ gtk_form_field_datetime_set_from_datamodel (GtkFormField *field, GdaDataModel *d gboolean ret; GtkFormFieldDateTimePrivate *priv; gchar *field_name; - struct tm *datetime; + GDateTime *datetime; gchar *value; g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE); @@ -558,15 +567,15 @@ gtk_form_field_datetime_set_from_datamodel (GtkFormField *field, GdaDataModel *d if (field_name != NULL && g_strcmp0 (g_strstrip (field_name), "") != 0) { - datetime = gdaex_data_model_get_field_value_tm_at (dm, row, field_name); + datetime = gdaex_data_model_get_field_value_gdatetime_at (dm, row, field_name); - value = gtk_form_field_datetime_get_str_from_tm (datetime, priv->display_format); + value = g_date_time_format (datetime, priv->display_format); - ret = gtk_form_field_datetime_set_value_stringify (field, value); + ret = gtk_form_field_datetime_set_value_stringify (field, value != NULL ? value : ""); - priv->original_value = g_memdup (datetime, sizeof (struct tm)); + priv->original_value = g_date_time_ref (datetime); - g_free (datetime); + g_date_time_unref (datetime); g_free (value); } else @@ -646,7 +655,7 @@ gtk_form_field_datetime_set_value (GtkFormField *field, GValue *gvalue) { gboolean ret; GtkFormFieldDateTimePrivate *priv; - struct tm *datetime; + GDateTime *datetime; gchar *value; g_return_val_if_fail (IS_GTK_FORM_FIELD_DATETIME (field), FALSE); @@ -656,12 +665,12 @@ gtk_form_field_datetime_set_value (GtkFormField *field, GValue *gvalue) priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (GTK_FORM_FIELD_DATETIME (field)); - datetime = (struct tm *)g_value_get_pointer (gvalue); - value = gtk_form_field_datetime_get_str_from_tm (datetime, priv->display_format); - ret = gtk_form_field_datetime_set_value_stringify (field, value); - priv->original_value = g_memdup (datetime, sizeof (struct tm)); + datetime = (GDateTime *)g_value_get_pointer (gvalue); + value = g_date_time_format (datetime, priv->display_format); + ret = gtk_form_field_datetime_set_value_stringify (field, value != NULL ? value : ""); + priv->original_value = g_date_time_ref (datetime); - g_free (datetime); + g_date_time_unref (datetime); g_free (value); return ret; @@ -677,6 +686,38 @@ struct tm *gtk_form_field_datetime_get_tm_from_str (const gchar *str, const gchar *format) { struct tm *ret; + + GDateTime *gdatetime; + + ret = NULL; + + gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (str, format); + + if (gdatetime != NULL) + { + ret = g_malloc0 (sizeof (struct tm)); + ret->tm_year = g_date_time_get_year (gdatetime) - 1900; + ret->tm_mon = g_date_time_get_month (gdatetime) - 1; + ret->tm_mday = g_date_time_get_day_of_month (gdatetime); + ret->tm_hour = g_date_time_get_hour (gdatetime); + ret->tm_min = g_date_time_get_minute (gdatetime); + ret->tm_sec = g_date_time_get_second (gdatetime); + } + + return ret; +} + +/** + * gtk_form_field_datetime_get_gdatetime_from_str: + * @str: the #gchar to be parsed. + * @format: + * + */ +GDateTime +*gtk_form_field_datetime_get_gdatetime_from_str (const gchar *str, const gchar *format) +{ + GDateTime *ret; + gchar *new_str; gchar *new_format; gchar **str_tokens; @@ -686,7 +727,15 @@ struct tm guint len_formatv; guint i; guint i_to; - GDate *date; + + gint year; + gint month; + gint day; + gint hour; + gint minute; + gdouble seconds; + + gboolean error; g_return_val_if_fail (str != NULL, NULL); @@ -697,11 +746,18 @@ struct tm return NULL; } - new_format = g_strstrip (g_strdup (format)); - if (g_strcmp0 (new_format, "") == 0) + if (format == NULL) { new_format = g_strdup ("%Y-%m-%d %H:%M:%S"); } + else + { + new_format = g_strstrip (g_strdup (format)); + if (g_strcmp0 (new_format, "") == 0) + { + new_format = g_strdup ("%Y-%m-%d %H:%M:%S"); + } + } /* removes format identifiers to find delimiters */ delimiters = gtk_form_field_datetime_str_replace (new_format, "%Y", ""); @@ -734,61 +790,96 @@ struct tm g_free (delimiters); g_free (new_format); g_free (new_str); + g_strfreev (str_tokens); return NULL; } + year = 1; + month = 1; + day = 1; + hour = 0; + minute = 0; + seconds = 0.0; + + error = FALSE; + ret = NULL; + len_strv = g_strv_length (str_tokens); len_formatv = g_strv_length (format_tokens); - ret = g_malloc0 (sizeof (struct tm)); - ret->tm_mday = 1; - ret->tm_isdst = -1; - i_to = MIN (len_strv, len_formatv); for (i = 0; i < i_to; i++) { if (g_strcmp0 (format_tokens[i], "%Y") == 0) { - ret->tm_year = strtol (str_tokens[i], NULL, 10) - 1900; + year = strtol (str_tokens[i], NULL, 10); + if (year < 1 || year > 9999) + { + error = TRUE; + break; + } } else if (g_strcmp0 (format_tokens[i], "%m") == 0) { - ret->tm_mon = strtol (str_tokens[i], NULL, 10) - 1; + month = strtol (str_tokens[i], NULL, 10); + if (month < 1 || month > 12) + { + error = TRUE; + break; + } } else if (g_strcmp0 (format_tokens[i], "%d") == 0) { - ret->tm_mday = strtol (str_tokens[i], NULL, 10); + day = strtol (str_tokens[i], NULL, 10); + if (day < 1 || day > 31) + { + error = TRUE; + break; + } } else if (g_strcmp0 (format_tokens[i], "%H") == 0) { - ret->tm_hour = strtol (str_tokens[i], NULL, 10); + hour = strtol (str_tokens[i], NULL, 10); + if (hour > 23) + { + error = TRUE; + break; + } } else if (g_strcmp0 (format_tokens[i], "%M") == 0) { - ret->tm_min = strtol (str_tokens[i], NULL, 10); + minute = strtol (str_tokens[i], NULL, 10); + if (minute > 59) + { + error = TRUE; + break; + } } else if (g_strcmp0 (format_tokens[i], "%S") == 0) { - ret->tm_sec = strtol (str_tokens[i], NULL, 10); + seconds = g_strtod (str_tokens[i], NULL); + if (seconds > 59.0) + { + error = TRUE; + break; + } } } g_free (delimiters); g_free (new_format); g_free (new_str); + g_strfreev (str_tokens); + g_strfreev (format_tokens); - /* check if it is valid */ - date = g_date_new_dmy (ret->tm_mday, - ret->tm_mon + 1, - ret->tm_year + 1900); - if (date == NULL || !g_date_valid (date)) - { - return NULL; - } - - if (ret != NULL) + if (!error) { - mktime (ret); + ret = g_date_time_new_local (year, + month, + day, + hour, + minute, + seconds); } return ret; @@ -862,6 +953,32 @@ GDate return ret; } +/** + * gtk_form_field_datetime_get_gdatetime_from_tm: + * @datetime: a struct tm. + * + * Returns: a #GDateTime that represent the struct tm with time information. + */ +GDateTime +*gtk_form_field_datetime_get_gdatetime_from_tm (struct tm *datetime) +{ + GDateTime *ret; + + ret = NULL; + + if (datetime != NULL) + { + ret = g_date_time_new_local (datetime->tm_mday, + datetime->tm_mon + 1, + datetime->tm_year + 1900, + datetime->tm_hour, + datetime->tm_min, + (gdouble)datetime->tm_sec); + } + + return ret; +} + /** * gtk_form_field_datetime_get_now: * @@ -887,7 +1004,6 @@ gtk_form_field_datetime_set_property (GObject *object, GParamSpec *pspec) { GtkFormFieldDateTime *field = (GtkFormFieldDateTime *)object; - GtkFormFieldDateTimePrivate *priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (field); switch (property_id) @@ -917,7 +1033,6 @@ gtk_form_field_datetime_get_property (GObject *object, GParamSpec *pspec) { GtkFormFieldDateTime *field = (GtkFormFieldDateTime *)object; - GtkFormFieldDateTimePrivate *priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (field); switch (property_id) @@ -1012,6 +1127,9 @@ static gchar gchar *ret; gchar *p; + if (string == NULL) return NULL; + if (origin == NULL || replace == NULL) return g_strdup (string); + p = g_strstr_len (string, -1, origin); if (p == NULL) @@ -1034,17 +1152,17 @@ gtk_form_field_datetime_is_valid_datetime (GtkFormFieldDateTime *field) GtkFormFieldDateTimePrivate *priv; gchar *str_date; - struct tm *tm_date; + GDateTime *gdatetime; priv = GTK_FORM_FIELD_DATETIME_GET_PRIVATE (field); str_date = gtk_form_field_datetime_get_value_stringify (GTK_FORM_FIELD (field)); - tm_date = gtk_form_field_datetime_get_tm_from_str (str_date, priv->display_format); + gdatetime = gtk_form_field_datetime_get_gdatetime_from_str (str_date, priv->display_format); - ret = (tm_date != NULL); + ret = (gdatetime != NULL); g_free (str_date); - g_free (tm_date); + g_date_time_unref (gdatetime); return ret; } diff --git a/libgtkform/fielddatetime.h b/libgtkform/fielddatetime.h index d645ea5..bf6ea46 100644 --- a/libgtkform/fielddatetime.h +++ b/libgtkform/fielddatetime.h @@ -79,6 +79,9 @@ struct tm *gtk_form_field_datetime_get_tm_from_str (const gchar *str, const gcha gchar *gtk_form_field_datetime_get_str_from_tm (struct tm *datetime, const gchar *format); GDate *gtk_form_field_datetime_get_gdate_from_tm (struct tm *datetime); +GDateTime *gtk_form_field_datetime_get_gdatetime_from_tm (struct tm *datetime); + +GDateTime *gtk_form_field_datetime_get_gdatetime_from_str (const gchar *str, const gchar *format); struct tm *gtk_form_field_datetime_get_now (void); diff --git a/libgtkform/form.c b/libgtkform/form.c index a98a105..5026aca 100644 --- a/libgtkform/form.c +++ b/libgtkform/form.c @@ -812,23 +812,20 @@ gtk_form_load_from_xml (GtkForm *form, xmlDoc *xmldoc, GtkBuilder *gtkbuilder) } else if (g_strcmp0 (type, "datetime") == 0) { - struct tm *datetime; + GDateTime *datetime; + datetime = NULL; if (g_strcmp0 (prop, "now") == 0) { - time_t time_now; - - time (&time_now); - datetime = localtime (&time_now); - g_object_set (G_OBJECT (field), "default", datetime, NULL); + datetime = g_date_time_new_now_local (); } else { - datetime = gtk_form_field_datetime_get_tm_from_str (prop, "%Y-%m-%d %H:%M:%S"); - if (datetime != NULL) - { - g_object_set (G_OBJECT (field), "default", datetime, NULL); - } + datetime = gtk_form_field_datetime_get_gdatetime_from_str (prop, "%Y-%m-%d %H:%M:%S"); + } + if (datetime != NULL) + { + g_object_set (G_OBJECT (field), "default", datetime, NULL); } } else if (g_strcmp0 (type, "float") == 0)