Updated Traditional Chinese translation(Hong Kong and Taiwan)
[evolution.git] / calendar / gui / e-alarm-list.c
blobc7f7ff9691c2b18c458152fd6c7602d89864283f
1 /*
2 * EAlarmList - list of calendar alarms with GtkTreeModel interface.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
18 * Authors:
19 * Hans Petter Jansson <hpj@ximian.com>
21 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
25 #include <config.h>
26 #include <string.h>
27 #include <glib/gi18n.h>
28 #include <libecal/e-cal-time-util.h>
29 #include <libedataserver/e-time-utils.h>
30 #include "calendar-config.h"
31 #include "e-alarm-list.h"
33 #define G_LIST(x) ((GList *) x)
34 #define E_ALARM_LIST_IS_SORTED(list) (E_ALARM_LIST (list)->sort_column_id != -2)
35 #define IS_VALID_ITER(dt_list, iter) (iter!= NULL && iter->user_data != NULL && \
36 dt_list->stamp == iter->stamp)
38 static GType column_types[E_ALARM_LIST_NUM_COLUMNS];
40 static void e_alarm_list_init (EAlarmList *file_list);
41 static void e_alarm_list_class_init (EAlarmListClass *class);
42 static void e_alarm_list_tree_model_init (GtkTreeModelIface *iface);
43 static void e_alarm_list_finalize (GObject *object);
44 static GtkTreeModelFlags e_alarm_list_get_flags (GtkTreeModel *tree_model);
45 static gint e_alarm_list_get_n_columns (GtkTreeModel *tree_model);
46 static GType e_alarm_list_get_column_type (GtkTreeModel *tree_model,
47 gint index);
48 static gboolean e_alarm_list_get_iter (GtkTreeModel *tree_model,
49 GtkTreeIter *iter,
50 GtkTreePath *path);
51 static GtkTreePath *e_alarm_list_get_path (GtkTreeModel *tree_model,
52 GtkTreeIter *iter);
53 static void e_alarm_list_get_value (GtkTreeModel *tree_model,
54 GtkTreeIter *iter,
55 gint column,
56 GValue *value);
57 static gboolean e_alarm_list_iter_next (GtkTreeModel *tree_model,
58 GtkTreeIter *iter);
59 static gboolean e_alarm_list_iter_children (GtkTreeModel *tree_model,
60 GtkTreeIter *iter,
61 GtkTreeIter *parent);
62 static gboolean e_alarm_list_iter_has_child (GtkTreeModel *tree_model,
63 GtkTreeIter *iter);
64 static gint e_alarm_list_iter_n_children (GtkTreeModel *tree_model,
65 GtkTreeIter *iter);
66 static gboolean e_alarm_list_iter_nth_child (GtkTreeModel *tree_model,
67 GtkTreeIter *iter,
68 GtkTreeIter *parent,
69 gint n);
70 static gboolean e_alarm_list_iter_parent (GtkTreeModel *tree_model,
71 GtkTreeIter *iter,
72 GtkTreeIter *child);
74 static GObjectClass *parent_class = NULL;
76 GType
77 e_alarm_list_get_type (void)
79 static GType alarm_list_type = 0;
81 if (!alarm_list_type) {
82 static const GTypeInfo alarm_list_info =
84 sizeof (EAlarmListClass),
85 NULL, /* base_init */
86 NULL, /* base_finalize */
87 (GClassInitFunc) e_alarm_list_class_init,
88 NULL, /* class_finalize */
89 NULL, /* class_data */
90 sizeof (EAlarmList),
92 (GInstanceInitFunc) e_alarm_list_init,
95 static const GInterfaceInfo tree_model_info =
97 (GInterfaceInitFunc) e_alarm_list_tree_model_init,
98 NULL,
99 NULL
102 column_types[E_ALARM_LIST_COLUMN_DESCRIPTION] = G_TYPE_STRING;
104 alarm_list_type = g_type_register_static (G_TYPE_OBJECT, "EAlarmList",
105 &alarm_list_info, 0);
106 g_type_add_interface_static (alarm_list_type,
107 GTK_TYPE_TREE_MODEL,
108 &tree_model_info);
111 return alarm_list_type;
114 static void
115 e_alarm_list_class_init (EAlarmListClass *class)
117 GObjectClass *object_class;
119 parent_class = g_type_class_peek_parent (class);
120 object_class = (GObjectClass *) class;
122 object_class->finalize = e_alarm_list_finalize;
125 static void
126 e_alarm_list_tree_model_init (GtkTreeModelIface *iface)
128 iface->get_flags = e_alarm_list_get_flags;
129 iface->get_n_columns = e_alarm_list_get_n_columns;
130 iface->get_column_type = e_alarm_list_get_column_type;
131 iface->get_iter = e_alarm_list_get_iter;
132 iface->get_path = e_alarm_list_get_path;
133 iface->get_value = e_alarm_list_get_value;
134 iface->iter_next = e_alarm_list_iter_next;
135 iface->iter_children = e_alarm_list_iter_children;
136 iface->iter_has_child = e_alarm_list_iter_has_child;
137 iface->iter_n_children = e_alarm_list_iter_n_children;
138 iface->iter_nth_child = e_alarm_list_iter_nth_child;
139 iface->iter_parent = e_alarm_list_iter_parent;
142 static void
143 e_alarm_list_init (EAlarmList *alarm_list)
145 alarm_list->stamp = g_random_int ();
146 alarm_list->columns_dirty = FALSE;
147 alarm_list->list = NULL;
150 EAlarmList *
151 e_alarm_list_new (void)
153 EAlarmList *alarm_list;
155 alarm_list = E_ALARM_LIST (g_object_new (e_alarm_list_get_type (), NULL));
157 return alarm_list;
160 static void
161 all_rows_deleted (EAlarmList *alarm_list)
163 GtkTreePath *path;
164 gint i;
166 if (!alarm_list->list)
167 return;
169 path = gtk_tree_path_new ();
170 i = g_list_length (alarm_list->list);
171 gtk_tree_path_append_index (path, i);
173 for (; i >= 0; i--) {
174 gtk_tree_model_row_deleted (GTK_TREE_MODEL (alarm_list), path);
175 gtk_tree_path_prev (path);
178 gtk_tree_path_free (path);
181 static void
182 row_deleted (EAlarmList *alarm_list, gint n)
184 GtkTreePath *path;
186 path = gtk_tree_path_new ();
187 gtk_tree_path_append_index (path, n);
188 gtk_tree_model_row_deleted (GTK_TREE_MODEL (alarm_list), path);
189 gtk_tree_path_free (path);
192 static void
193 row_added (EAlarmList *alarm_list, gint n)
195 GtkTreePath *path;
196 GtkTreeIter iter;
198 path = gtk_tree_path_new ();
199 gtk_tree_path_append_index (path, n);
201 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (alarm_list), &iter, path))
202 gtk_tree_model_row_inserted (GTK_TREE_MODEL (alarm_list), path, &iter);
204 gtk_tree_path_free (path);
207 static void
208 row_updated (EAlarmList *alarm_list, gint n)
210 GtkTreePath *path;
211 GtkTreeIter iter;
213 path = gtk_tree_path_new ();
214 gtk_tree_path_append_index (path, n);
216 if (gtk_tree_model_get_iter (GTK_TREE_MODEL (alarm_list), &iter, path))
217 gtk_tree_model_row_changed (GTK_TREE_MODEL (alarm_list), path, &iter);
219 gtk_tree_path_free (path);
222 static void
223 e_alarm_list_finalize (GObject *object)
225 if (G_OBJECT_CLASS (parent_class)->finalize)
226 (* G_OBJECT_CLASS (parent_class)->finalize) (object);
229 /* Fulfill the GtkTreeModel requirements */
230 static GtkTreeModelFlags
231 e_alarm_list_get_flags (GtkTreeModel *tree_model)
233 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), 0);
235 return GTK_TREE_MODEL_LIST_ONLY;
238 static gint
239 e_alarm_list_get_n_columns (GtkTreeModel *tree_model)
241 EAlarmList *alarm_list = (EAlarmList *) tree_model;
243 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), 0);
245 alarm_list->columns_dirty = TRUE;
246 return E_ALARM_LIST_NUM_COLUMNS;
249 static GType
250 e_alarm_list_get_column_type (GtkTreeModel *tree_model,
251 gint index)
253 EAlarmList *alarm_list = (EAlarmList *) tree_model;
255 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), G_TYPE_INVALID);
256 g_return_val_if_fail (index < E_ALARM_LIST_NUM_COLUMNS &&
257 index >= 0, G_TYPE_INVALID);
259 alarm_list->columns_dirty = TRUE;
260 return column_types[index];
263 const ECalComponentAlarm *
264 e_alarm_list_get_alarm (EAlarmList *alarm_list, GtkTreeIter *iter)
266 g_return_val_if_fail (IS_VALID_ITER (alarm_list, iter), NULL);
268 return G_LIST (iter->user_data)->data;
271 static void
272 free_alarm (ECalComponentAlarm *alarm)
274 e_cal_component_alarm_free (alarm);
277 static ECalComponentAlarm *
278 copy_alarm (const ECalComponentAlarm *alarm)
280 return e_cal_component_alarm_clone ((ECalComponentAlarm *) alarm);
283 void
284 e_alarm_list_set_alarm (EAlarmList *alarm_list, GtkTreeIter *iter,
285 const ECalComponentAlarm *alarm)
287 ECalComponentAlarm *alarm_old;
289 g_return_if_fail (IS_VALID_ITER (alarm_list, iter));
291 alarm_old = G_LIST (iter->user_data)->data;
292 free_alarm (alarm_old);
293 G_LIST (iter->user_data)->data = copy_alarm (alarm);
294 row_updated (alarm_list, g_list_position (alarm_list->list, G_LIST (iter->user_data)));
297 void
298 e_alarm_list_append (EAlarmList *alarm_list, GtkTreeIter *iter,
299 const ECalComponentAlarm *alarm)
301 g_return_if_fail (alarm != NULL);
303 alarm_list->list = g_list_append (alarm_list->list, copy_alarm (alarm));
304 row_added (alarm_list, g_list_length (alarm_list->list) - 1);
306 if (iter) {
307 iter->user_data = g_list_last (alarm_list->list);
308 iter->stamp = alarm_list->stamp;
312 void
313 e_alarm_list_remove (EAlarmList *alarm_list, GtkTreeIter *iter)
315 gint n;
317 g_return_if_fail (IS_VALID_ITER (alarm_list, iter));
319 n = g_list_position (alarm_list->list, G_LIST (iter->user_data));
320 free_alarm ((ECalComponentAlarm *) G_LIST (iter->user_data)->data);
321 alarm_list->list = g_list_delete_link (alarm_list->list, G_LIST (iter->user_data));
322 row_deleted (alarm_list, n);
325 void
326 e_alarm_list_clear (EAlarmList *alarm_list)
328 GList *l;
330 all_rows_deleted (alarm_list);
332 for (l = alarm_list->list; l; l = g_list_next (l)) {
333 free_alarm ((ECalComponentAlarm *) l->data);
336 g_list_free (alarm_list->list);
337 alarm_list->list = NULL;
340 static gboolean
341 e_alarm_list_get_iter (GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreePath *path)
343 EAlarmList *alarm_list = (EAlarmList *) tree_model;
344 GList *l;
345 gint i;
347 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), FALSE);
348 g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
350 if (!alarm_list->list)
351 return FALSE;
353 alarm_list->columns_dirty = TRUE;
355 i = gtk_tree_path_get_indices (path)[0];
356 l = g_list_nth (alarm_list->list, i);
357 if (!l)
358 return FALSE;
360 iter->user_data = l;
361 iter->stamp = alarm_list->stamp;
362 return TRUE;
365 static GtkTreePath *
366 e_alarm_list_get_path (GtkTreeModel *tree_model,
367 GtkTreeIter *iter)
369 EAlarmList *alarm_list = (EAlarmList *) tree_model;
370 GtkTreePath *retval;
371 GList *l;
373 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), NULL);
374 g_return_val_if_fail (iter->stamp == E_ALARM_LIST (tree_model)->stamp, NULL);
376 l = iter->user_data;
377 retval = gtk_tree_path_new ();
378 gtk_tree_path_append_index (retval, g_list_position (alarm_list->list, l));
379 return retval;
382 /* Builds a string for the duration of the alarm. If the duration is zero, returns NULL. */
383 static gchar *
384 get_alarm_duration_string (struct icaldurationtype *duration)
386 GString *string = g_string_new (NULL);
387 gchar *ret;
388 gboolean have_something;
390 have_something = FALSE;
392 if (duration->days >= 1) {
393 /* Translator: Entire string is like "Pop up an alert %d days before start of appointment" */
394 g_string_printf (string, ngettext("%d day", "%d days", duration->days), duration->days);
395 have_something = TRUE;
398 if (duration->weeks >= 1) {
399 /* Translator: Entire string is like "Pop up an alert %d weeks before start of appointment" */
400 g_string_printf (string, ngettext("%d week","%d weeks", duration->weeks), duration->weeks);
401 have_something = TRUE;
404 if (duration->hours >= 1) {
405 /* Translator: Entire string is like "Pop up an alert %d hours before start of appointment" */
406 g_string_printf (string, ngettext("%d hour", "%d hours", duration->hours), duration->hours);
407 have_something = TRUE;
410 if (duration->minutes >= 1) {
411 /* Translator: Entire string is like "Pop up an alert %d minutes before start of appointment" */
412 g_string_printf (string, ngettext("%d minute", "%d minutes", duration->minutes), duration->minutes);
413 have_something = TRUE;
416 if (duration->seconds >= 1) {
417 /* Translator: Entire string is like "Pop up an alert %d seconds before start of appointment" */
418 g_string_printf (string, ngettext("%d second", "%d seconds", duration->seconds), duration->seconds);
419 have_something = TRUE;
422 if (have_something) {
423 ret = string->str;
424 g_string_free (string, FALSE);
425 return ret;
426 } else {
427 g_string_free (string, TRUE);
428 return NULL;
432 static gchar *
433 get_alarm_string (ECalComponentAlarm *alarm)
435 ECalComponentAlarmAction action;
436 ECalComponentAlarmTrigger trigger;
437 gchar *base, *str = NULL, *dur;
439 e_cal_component_alarm_get_action (alarm, &action);
440 e_cal_component_alarm_get_trigger (alarm, &trigger);
442 switch (action) {
443 case E_CAL_COMPONENT_ALARM_AUDIO:
444 base = _("Play a sound");
445 break;
447 case E_CAL_COMPONENT_ALARM_DISPLAY:
448 base = _("Pop up an alert");
449 break;
451 case E_CAL_COMPONENT_ALARM_EMAIL:
452 base = _("Send an email");
453 break;
455 case E_CAL_COMPONENT_ALARM_PROCEDURE:
456 base = _("Run a program");
457 break;
459 case E_CAL_COMPONENT_ALARM_NONE:
460 case E_CAL_COMPONENT_ALARM_UNKNOWN:
461 default:
462 base = _("Unknown action to be performed");
463 break;
466 /* FIXME: This does not look like it will localize correctly. */
468 switch (trigger.type) {
469 case E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START:
470 dur = get_alarm_duration_string (&trigger.u.rel_duration);
472 if (dur) {
473 if (trigger.u.rel_duration.is_neg)
474 /*Translator: The first %s refers to the base, which would be actions like
475 * "Play a Sound". Second %s refers to the duration string e.g:"15 minutes"*/
476 str = g_strdup_printf (_("%s %s before the start of the appointment"),
477 base, dur);
478 else
479 /*Translator: The first %s refers to the base, which would be actions like
480 * "Play a Sound". Second %s refers to the duration string e.g:"15 minutes"*/
481 str = g_strdup_printf (_("%s %s after the start of the appointment"),
482 base, dur);
484 g_free (dur);
485 } else
486 /*Translator: The %s refers to the base, which would be actions like
487 * "Play a sound" */
488 str = g_strdup_printf (_("%s at the start of the appointment"), base);
490 break;
492 case E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_END:
493 dur = get_alarm_duration_string (&trigger.u.rel_duration);
495 if (dur) {
496 if (trigger.u.rel_duration.is_neg)
497 /* Translator: The first %s refers to the base, which would be actions like
498 * "Play a Sound". Second %s refers to the duration string e.g:"15 minutes" */
499 str = g_strdup_printf (_("%s %s before the end of the appointment"),
500 base, dur);
501 else
502 /* Translator: The first %s refers to the base, which would be actions like
503 * "Play a Sound". Second %s refers to the duration string e.g:"15 minutes" */
504 str = g_strdup_printf (_("%s %s after the end of the appointment"),
505 base, dur);
507 g_free (dur);
508 } else
509 /* Translator: The %s refers to the base, which would be actions like
510 * "Play a sound" */
511 str = g_strdup_printf (_("%s at the end of the appointment"), base);
513 break;
515 case E_CAL_COMPONENT_ALARM_TRIGGER_ABSOLUTE: {
516 struct icaltimetype itt;
517 icaltimezone *utc_zone, *current_zone;
518 struct tm tm;
519 gchar buf[256];
521 /* Absolute triggers come in UTC, so convert them to the local timezone */
523 itt = trigger.u.abs_time;
525 utc_zone = icaltimezone_get_utc_timezone ();
526 current_zone = calendar_config_get_icaltimezone ();
528 tm = icaltimetype_to_tm_with_zone (&itt, utc_zone, current_zone);
530 e_time_format_date_and_time (&tm, calendar_config_get_24_hour_format (),
531 FALSE, FALSE, buf, sizeof (buf));
533 /* Translator: The first %s refers to the base, which would be actions like
534 * "Play a Sound". Second %s is an absolute time, e.g. "10:00AM" */
535 str = g_strdup_printf (_("%s at %s"), base, buf);
537 break; }
539 case E_CAL_COMPONENT_ALARM_TRIGGER_NONE:
540 default:
541 /* Translator: The %s refers to the base, which would be actions like
542 * "Play a sound". "Trigger types" are absolute or relative dates */
543 str = g_strdup_printf (_("%s for an unknown trigger type"), base);
544 break;
547 return str;
550 static void
551 e_alarm_list_get_value (GtkTreeModel *tree_model,
552 GtkTreeIter *iter,
553 gint column,
554 GValue *value)
556 EAlarmList *alarm_list = E_ALARM_LIST (tree_model);
557 ECalComponentAlarm *alarm;
558 GList *l;
559 gchar *str;
561 g_return_if_fail (E_IS_ALARM_LIST (tree_model));
562 g_return_if_fail (column < E_ALARM_LIST_NUM_COLUMNS);
563 g_return_if_fail (E_ALARM_LIST (tree_model)->stamp == iter->stamp);
564 g_return_if_fail (IS_VALID_ITER (alarm_list, iter));
566 g_value_init (value, column_types[column]);
568 if (!alarm_list->list)
569 return;
571 l = iter->user_data;
572 alarm = l->data;
574 if (!alarm)
575 return;
577 switch (column) {
578 case E_ALARM_LIST_COLUMN_DESCRIPTION:
579 str = get_alarm_string (alarm);
580 g_value_set_string (value, str);
581 g_free (str);
582 break;
586 static gboolean
587 e_alarm_list_iter_next (GtkTreeModel *tree_model,
588 GtkTreeIter *iter)
590 GList *l;
592 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), FALSE);
593 g_return_val_if_fail (IS_VALID_ITER (E_ALARM_LIST (tree_model), iter), FALSE);
595 if (!E_ALARM_LIST (tree_model)->list)
596 return FALSE;
598 l = iter->user_data;
599 l = g_list_next (l);
600 if (l) {
601 iter->user_data = l;
602 return TRUE;
605 return FALSE;
608 static gboolean
609 e_alarm_list_iter_children (GtkTreeModel *tree_model,
610 GtkTreeIter *iter,
611 GtkTreeIter *parent)
613 EAlarmList *alarm_list = E_ALARM_LIST (tree_model);
615 /* this is a list, nodes have no children */
616 if (parent)
617 return FALSE;
619 /* but if parent == NULL we return the list itself as children of the
620 * "root" */
622 if (!alarm_list->list)
623 return FALSE;
625 iter->stamp = E_ALARM_LIST (tree_model)->stamp;
626 iter->user_data = alarm_list->list;
627 return TRUE;
630 static gboolean
631 e_alarm_list_iter_has_child (GtkTreeModel *tree_model,
632 GtkTreeIter *iter)
634 g_return_val_if_fail (IS_VALID_ITER (E_ALARM_LIST (tree_model), iter), FALSE);
635 return FALSE;
638 static gint
639 e_alarm_list_iter_n_children (GtkTreeModel *tree_model,
640 GtkTreeIter *iter)
642 EAlarmList *alarm_list = E_ALARM_LIST (tree_model);
644 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), -1);
646 if (iter == NULL)
647 return g_list_length (alarm_list->list);
649 g_return_val_if_fail (E_ALARM_LIST (tree_model)->stamp == iter->stamp, -1);
650 return 0;
653 static gboolean
654 e_alarm_list_iter_nth_child (GtkTreeModel *tree_model,
655 GtkTreeIter *iter,
656 GtkTreeIter *parent,
657 gint n)
659 EAlarmList *alarm_list = E_ALARM_LIST (tree_model);
661 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), FALSE);
663 if (parent)
664 return FALSE;
666 if (alarm_list->list) {
667 GList *l;
669 l = g_list_nth (alarm_list->list, n);
670 if (!l)
671 return FALSE;
673 iter->stamp = alarm_list->stamp;
674 iter->user_data = l;
675 return TRUE;
678 return FALSE;
681 static gboolean
682 e_alarm_list_iter_parent (GtkTreeModel *tree_model,
683 GtkTreeIter *iter,
684 GtkTreeIter *child)
686 return FALSE;