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/>
19 * Hans Petter Jansson <hpj@ximian.com>
21 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
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
,
48 static gboolean
e_alarm_list_get_iter (GtkTreeModel
*tree_model
,
51 static GtkTreePath
*e_alarm_list_get_path (GtkTreeModel
*tree_model
,
53 static void e_alarm_list_get_value (GtkTreeModel
*tree_model
,
57 static gboolean
e_alarm_list_iter_next (GtkTreeModel
*tree_model
,
59 static gboolean
e_alarm_list_iter_children (GtkTreeModel
*tree_model
,
62 static gboolean
e_alarm_list_iter_has_child (GtkTreeModel
*tree_model
,
64 static gint
e_alarm_list_iter_n_children (GtkTreeModel
*tree_model
,
66 static gboolean
e_alarm_list_iter_nth_child (GtkTreeModel
*tree_model
,
70 static gboolean
e_alarm_list_iter_parent (GtkTreeModel
*tree_model
,
74 static GObjectClass
*parent_class
= NULL
;
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
),
86 NULL
, /* base_finalize */
87 (GClassInitFunc
) e_alarm_list_class_init
,
88 NULL
, /* class_finalize */
89 NULL
, /* class_data */
92 (GInstanceInitFunc
) e_alarm_list_init
,
95 static const GInterfaceInfo tree_model_info
=
97 (GInterfaceInitFunc
) e_alarm_list_tree_model_init
,
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
,
111 return alarm_list_type
;
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
;
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
;
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
;
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
));
161 all_rows_deleted (EAlarmList
*alarm_list
)
166 if (!alarm_list
->list
)
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
);
182 row_deleted (EAlarmList
*alarm_list
, gint n
)
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
);
193 row_added (EAlarmList
*alarm_list
, gint n
)
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
);
208 row_updated (EAlarmList
*alarm_list
, gint n
)
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
);
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
;
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
;
250 e_alarm_list_get_column_type (GtkTreeModel
*tree_model
,
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
;
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
);
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
)));
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);
307 iter
->user_data
= g_list_last (alarm_list
->list
);
308 iter
->stamp
= alarm_list
->stamp
;
313 e_alarm_list_remove (EAlarmList
*alarm_list
, GtkTreeIter
*iter
)
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
);
326 e_alarm_list_clear (EAlarmList
*alarm_list
)
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
;
341 e_alarm_list_get_iter (GtkTreeModel
*tree_model
, GtkTreeIter
*iter
, GtkTreePath
*path
)
343 EAlarmList
*alarm_list
= (EAlarmList
*) tree_model
;
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
)
353 alarm_list
->columns_dirty
= TRUE
;
355 i
= gtk_tree_path_get_indices (path
)[0];
356 l
= g_list_nth (alarm_list
->list
, i
);
361 iter
->stamp
= alarm_list
->stamp
;
366 e_alarm_list_get_path (GtkTreeModel
*tree_model
,
369 EAlarmList
*alarm_list
= (EAlarmList
*) tree_model
;
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
);
377 retval
= gtk_tree_path_new ();
378 gtk_tree_path_append_index (retval
, g_list_position (alarm_list
->list
, l
));
382 /* Builds a string for the duration of the alarm. If the duration is zero, returns NULL. */
384 get_alarm_duration_string (struct icaldurationtype
*duration
)
386 GString
*string
= g_string_new (NULL
);
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
) {
424 g_string_free (string
, FALSE
);
427 g_string_free (string
, TRUE
);
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
);
443 case E_CAL_COMPONENT_ALARM_AUDIO
:
444 base
= _("Play a sound");
447 case E_CAL_COMPONENT_ALARM_DISPLAY
:
448 base
= _("Pop up an alert");
451 case E_CAL_COMPONENT_ALARM_EMAIL
:
452 base
= _("Send an email");
455 case E_CAL_COMPONENT_ALARM_PROCEDURE
:
456 base
= _("Run a program");
459 case E_CAL_COMPONENT_ALARM_NONE
:
460 case E_CAL_COMPONENT_ALARM_UNKNOWN
:
462 base
= _("Unknown action to be performed");
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
);
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"),
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"),
486 /*Translator: The %s refers to the base, which would be actions like
488 str
= g_strdup_printf (_("%s at the start of the appointment"), base
);
492 case E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_END
:
493 dur
= get_alarm_duration_string (&trigger
.u
.rel_duration
);
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"),
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"),
509 /* Translator: The %s refers to the base, which would be actions like
511 str
= g_strdup_printf (_("%s at the end of the appointment"), base
);
515 case E_CAL_COMPONENT_ALARM_TRIGGER_ABSOLUTE
: {
516 struct icaltimetype itt
;
517 icaltimezone
*utc_zone
, *current_zone
;
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
);
539 case E_CAL_COMPONENT_ALARM_TRIGGER_NONE
:
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
);
551 e_alarm_list_get_value (GtkTreeModel
*tree_model
,
556 EAlarmList
*alarm_list
= E_ALARM_LIST (tree_model
);
557 ECalComponentAlarm
*alarm
;
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
)
578 case E_ALARM_LIST_COLUMN_DESCRIPTION
:
579 str
= get_alarm_string (alarm
);
580 g_value_set_string (value
, str
);
587 e_alarm_list_iter_next (GtkTreeModel
*tree_model
,
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
)
609 e_alarm_list_iter_children (GtkTreeModel
*tree_model
,
613 EAlarmList
*alarm_list
= E_ALARM_LIST (tree_model
);
615 /* this is a list, nodes have no children */
619 /* but if parent == NULL we return the list itself as children of the
622 if (!alarm_list
->list
)
625 iter
->stamp
= E_ALARM_LIST (tree_model
)->stamp
;
626 iter
->user_data
= alarm_list
->list
;
631 e_alarm_list_iter_has_child (GtkTreeModel
*tree_model
,
634 g_return_val_if_fail (IS_VALID_ITER (E_ALARM_LIST (tree_model
), iter
), FALSE
);
639 e_alarm_list_iter_n_children (GtkTreeModel
*tree_model
,
642 EAlarmList
*alarm_list
= E_ALARM_LIST (tree_model
);
644 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model
), -1);
647 return g_list_length (alarm_list
->list
);
649 g_return_val_if_fail (E_ALARM_LIST (tree_model
)->stamp
== iter
->stamp
, -1);
654 e_alarm_list_iter_nth_child (GtkTreeModel
*tree_model
,
659 EAlarmList
*alarm_list
= E_ALARM_LIST (tree_model
);
661 g_return_val_if_fail (E_IS_ALARM_LIST (tree_model
), FALSE
);
666 if (alarm_list
->list
) {
669 l
= g_list_nth (alarm_list
->list
, n
);
673 iter
->stamp
= alarm_list
->stamp
;
682 e_alarm_list_iter_parent (GtkTreeModel
*tree_model
,