2 * ECellDateEditText - a subclass of ECellText used to show and edit the text
3 * representation of the date, from a ECalComponentDateTime* model value.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Damon Chaplin <damon@ximian.com>
20 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
23 #include "evolution-config.h"
29 #include <glib/gi18n.h>
30 #include <libecal/libecal.h>
32 #include "e-cell-date-edit-text.h"
34 #define E_CELL_DATE_EDIT_TEXT_GET_PRIVATE(obj) \
35 (G_TYPE_INSTANCE_GET_PRIVATE \
36 ((obj), E_TYPE_CELL_DATE_EDIT_TEXT, ECellDateEditTextPrivate))
38 struct _ECellDateEditTextPrivate
{
40 /* The timezone to display the date in. */
41 icaltimezone
*timezone
;
43 /* Whether to display in 24-hour format. */
44 gboolean use_24_hour_format
;
50 PROP_USE_24_HOUR_FORMAT
55 e_cell_date_edit_text
,
59 cell_date_edit_text_set_property (GObject
*object
,
64 switch (property_id
) {
66 e_cell_date_edit_text_set_timezone (
67 E_CELL_DATE_EDIT_TEXT (object
),
68 g_value_get_pointer (value
));
71 case PROP_USE_24_HOUR_FORMAT
:
72 e_cell_date_edit_text_set_use_24_hour_format (
73 E_CELL_DATE_EDIT_TEXT (object
),
74 g_value_get_boolean (value
));
78 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
82 cell_date_edit_text_get_property (GObject
*object
,
87 switch (property_id
) {
91 e_cell_date_edit_text_get_timezone (
92 E_CELL_DATE_EDIT_TEXT (object
)));
95 case PROP_USE_24_HOUR_FORMAT
:
98 e_cell_date_edit_text_get_use_24_hour_format (
99 E_CELL_DATE_EDIT_TEXT (object
)));
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
107 cell_date_edit_text_get_text (ECellText
*cell
,
112 ECellDateEditText
*ecd
= E_CELL_DATE_EDIT_TEXT (cell
);
113 ECellDateEditValue
*dv
= e_table_model_value_at (model
, col
, row
);
114 icaltimezone
*timezone
;
119 return g_strdup ("");
121 timezone
= e_cell_date_edit_text_get_timezone (ecd
);
123 /* Note that although the property may be in a different
124 * timezone, we convert it to the current timezone to display
125 * it in the table. If the user actually edits the value,
126 * it will be set to the current timezone. See set_value (). */
127 tmp_tm
= icaltimetype_to_tm_with_zone (&dv
->tt
, dv
->zone
, timezone
);
129 res
= e_datetime_format_format_tm (
130 "calendar", "table", dv
->tt
.is_date
?
131 DTFormatKindDate
: DTFormatKindDateTime
, &tmp_tm
);
133 e_table_model_free_value (model
, col
, dv
);
139 cell_date_edit_text_free_text (ECellText
*cell
,
147 /* FIXME: We need to set the "transient_for" property for the dialog. */
149 show_date_warning (ECellDateEditText
*ecd
)
152 gchar buffer
[64], *format
;
157 /* We are only using this as an example, so the timezone doesn't
159 tmp_tm
= localtime (&t
);
161 if (e_cell_date_edit_text_get_use_24_hour_format (ecd
))
162 /* strftime format of a weekday, a date and a time, 24-hour. */
163 format
= _("%a %m/%d/%Y %H:%M:%S");
165 /* strftime format of a weekday, a date and a time, 12-hour. */
166 format
= _("%a %m/%d/%Y %I:%M:%S %p");
168 e_utf8_strftime (buffer
, sizeof (buffer
), format
, tmp_tm
);
170 dialog
= gtk_message_dialog_new (
174 _("The date must be entered in the format: \n%s"),
176 gtk_dialog_run (GTK_DIALOG (dialog
));
177 gtk_widget_destroy (dialog
);
181 cell_date_edit_text_set_value (ECellText
*cell
,
187 ECellDateEditText
*ecd
= E_CELL_DATE_EDIT_TEXT (cell
);
188 ETimeParseStatus status
;
190 ECellDateEditValue dv
;
191 ECellDateEditValue
*value
;
192 gboolean is_date
= TRUE
;
194 /* Try to parse just a date first. If the value is only a date, we
195 * use a DATE value. */
196 status
= e_time_parse_date (text
, &tmp_tm
);
197 if (status
== E_TIME_PARSE_INVALID
) {
199 status
= e_time_parse_date_and_time (text
, &tmp_tm
);
201 if (status
== E_TIME_PARSE_INVALID
) {
202 show_date_warning (ecd
);
207 if (status
== E_TIME_PARSE_NONE
) {
210 dv
.tt
= icaltime_null_time ();
212 dv
.tt
.year
= tmp_tm
.tm_year
+ 1900;
213 dv
.tt
.month
= tmp_tm
.tm_mon
+ 1;
214 dv
.tt
.day
= tmp_tm
.tm_mday
;
215 dv
.tt
.hour
= tmp_tm
.tm_hour
;
216 dv
.tt
.minute
= tmp_tm
.tm_min
;
217 dv
.tt
.second
= tmp_tm
.tm_sec
;
218 dv
.tt
.is_date
= is_date
;
220 /* FIXME: We assume it is being set to the current timezone.
225 dv
.zone
= e_cell_date_edit_text_get_timezone (ecd
);
231 e_table_model_set_value_at (model
, col
, row
, value
);
235 e_cell_date_edit_text_class_init (ECellDateEditTextClass
*class)
237 GObjectClass
*object_class
;
238 ECellTextClass
*cell_text_class
;
240 g_type_class_add_private (class, sizeof (ECellDateEditTextPrivate
));
242 object_class
= G_OBJECT_CLASS (class);
243 object_class
->set_property
= cell_date_edit_text_set_property
;
244 object_class
->get_property
= cell_date_edit_text_get_property
;
246 cell_text_class
= E_CELL_TEXT_CLASS (class);
247 cell_text_class
->get_text
= cell_date_edit_text_get_text
;
248 cell_text_class
->free_text
= cell_date_edit_text_free_text
;
249 cell_text_class
->set_value
= cell_date_edit_text_set_value
;
251 g_object_class_install_property (
254 g_param_spec_pointer (
260 g_object_class_install_property (
262 PROP_USE_24_HOUR_FORMAT
,
263 g_param_spec_boolean (
264 "use-24-hour-format",
265 "Use 24-Hour Format",
272 e_cell_date_edit_text_init (ECellDateEditText
*ecd
)
274 ecd
->priv
= E_CELL_DATE_EDIT_TEXT_GET_PRIVATE (ecd
);
276 ecd
->priv
->timezone
= icaltimezone_get_utc_timezone ();
277 ecd
->priv
->use_24_hour_format
= TRUE
;
281 * e_cell_date_edit_text_new:
283 * Creates a new ECell renderer that can be used to render and edit dates that
284 * that come from the model. The value returned from the model is
285 * interpreted as being a ECalComponentDateTime*.
287 * Returns: an ECell object that can be used to render dates.
290 e_cell_date_edit_text_new (const gchar
*fontname
,
291 GtkJustification justify
)
295 cell
= g_object_new (E_TYPE_CELL_DATE_EDIT_TEXT
, NULL
);
296 e_cell_text_construct (E_CELL_TEXT (cell
), fontname
, justify
);
302 e_cell_date_edit_text_get_timezone (ECellDateEditText
*ecd
)
304 g_return_val_if_fail (E_IS_CELL_DATE_EDIT_TEXT (ecd
), NULL
);
306 return ecd
->priv
->timezone
;
310 e_cell_date_edit_text_set_timezone (ECellDateEditText
*ecd
,
311 icaltimezone
*timezone
)
313 g_return_if_fail (E_IS_CELL_DATE_EDIT_TEXT (ecd
));
315 if (ecd
->priv
->timezone
== timezone
)
318 ecd
->priv
->timezone
= timezone
;
320 g_object_notify (G_OBJECT (ecd
), "timezone");
324 e_cell_date_edit_text_get_use_24_hour_format (ECellDateEditText
*ecd
)
326 g_return_val_if_fail (E_IS_CELL_DATE_EDIT_TEXT (ecd
), FALSE
);
328 return ecd
->priv
->use_24_hour_format
;
332 e_cell_date_edit_text_set_use_24_hour_format (ECellDateEditText
*ecd
,
333 gboolean use_24_hour
)
335 g_return_if_fail (E_IS_CELL_DATE_EDIT_TEXT (ecd
));
337 if (ecd
->priv
->use_24_hour_format
== use_24_hour
)
340 ecd
->priv
->use_24_hour_format
= use_24_hour
;
342 g_object_notify (G_OBJECT (ecd
), "use-24-hour-format");
346 e_cell_date_edit_compare_cb (gconstpointer a
,
350 ECellDateEditValue
*dv1
= (ECellDateEditValue
*) a
;
351 ECellDateEditValue
*dv2
= (ECellDateEditValue
*) b
;
352 struct icaltimetype tt
;
354 /* First check if either is NULL. NULL dates sort last. */
364 /* Copy the 2nd value and convert it to the same timezone as the first. */
367 icaltimezone_convert_time (&tt
, dv2
->zone
, dv1
->zone
);
369 /* Now we can compare them. */
370 return icaltime_compare (dv1
->tt
, tt
);