From ceb821417779737ccf0107eb5b1f49bb61811c5c Mon Sep 17 00:00:00 2001 From: Hans Petter Jansson Date: Thu, 16 Jan 2003 21:14:02 +0000 Subject: Implement EAlarmList as CalComponentAlarm list with a GtkTreeModel 2003-01-16 Hans Petter Jansson * gui/e-alarm-list.[ch]: Implement EAlarmList as CalComponentAlarm list with a GtkTreeModel interface. * gui/Makefile.am: Add e-alarm-list.[ch]. * gui/dialogs/alarm-page.[ch]: Use GtkTreeView with the new EAlarmList as model for the alarm list. Update copyright. * gui/dialogs/recurrence-page.[ch]: Update copyright. (free_exception_date_time): Removed. (fill_exception_widgets): Kill a lingering clist operation. svn path=/trunk/; revision=19499 --- calendar/ChangeLog | 14 + calendar/gui/Makefile.am | 2 + calendar/gui/dialogs/alarm-page.c | 130 +++++-- calendar/gui/dialogs/alarm-page.h | 3 +- calendar/gui/dialogs/recurrence-page.c | 21 +- calendar/gui/dialogs/recurrence-page.h | 3 +- calendar/gui/e-alarm-list.c | 688 +++++++++++++++++++++++++++++++++ calendar/gui/e-alarm-list.h | 79 ++++ 8 files changed, 880 insertions(+), 60 deletions(-) create mode 100644 calendar/gui/e-alarm-list.c create mode 100644 calendar/gui/e-alarm-list.h (limited to 'calendar') diff --git a/calendar/ChangeLog b/calendar/ChangeLog index d0cfb00c4c..41a1ec6916 100644 --- a/calendar/ChangeLog +++ b/calendar/ChangeLog @@ -1,3 +1,17 @@ +2003-01-16 Hans Petter Jansson + + * gui/e-alarm-list.[ch]: Implement EAlarmList as CalComponentAlarm + list with a GtkTreeModel interface. + + * gui/Makefile.am: Add e-alarm-list.[ch]. + + * gui/dialogs/alarm-page.[ch]: Use GtkTreeView with the new + EAlarmList as model for the alarm list. Update copyright. + + * gui/dialogs/recurrence-page.[ch]: Update copyright. + (free_exception_date_time): Removed. + (fill_exception_widgets): Kill a lingering clist operation. + 2003-01-15 Hans Petter Jansson * gui/e-meeting-time-sel.c (e_meeting_time_selector_construct): diff --git a/calendar/gui/Makefile.am b/calendar/gui/Makefile.am index 345220f627..5ccdb1fec1 100644 --- a/calendar/gui/Makefile.am +++ b/calendar/gui/Makefile.am @@ -92,6 +92,8 @@ libevolution_calendar_la_SOURCES = \ comp-util.h \ control-factory.c \ control-factory.h \ + e-alarm-list.c \ + e-alarm-list.h \ e-calendar-table.h \ e-calendar-table.c \ e-cell-date-edit-text.h \ diff --git a/calendar/gui/dialogs/alarm-page.c b/calendar/gui/dialogs/alarm-page.c index 1af357e7c3..c8f890cc40 100644 --- a/calendar/gui/dialogs/alarm-page.c +++ b/calendar/gui/dialogs/alarm-page.c @@ -1,11 +1,14 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ + /* Evolution calendar - Alarm page of the calendar component dialogs * - * Copyright (C) 2001 Ximian, Inc. + * Copyright (C) 2001-2003 Ximian, Inc. * * Authors: Federico Mena-Quintero * Miguel de Icaza * Seth Alves * JP Rosevear + * Hans Petter Jansson * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -27,6 +30,7 @@ #include #include +#include #include #include #include @@ -37,6 +41,7 @@ #include "../calendar-config.h" #include "comp-editor-util.h" #include "alarm-options.h" +#include "../e-alarm-list.h" #include "alarm-page.h" @@ -68,6 +73,9 @@ struct _AlarmPagePrivate { /* Alarm options dialog and the alarm we maintain */ CalComponentAlarm *alarm; + /* Alarm store for the GtkTreeView list widget */ + EAlarmList *list_store; + gboolean updating; }; @@ -190,7 +198,7 @@ alarm_page_init (AlarmPage *apage) /* create the default alarm, which will contain the * X-EVOLUTION-NEEDS-DESCRIPTION property, so that we - * set a correct description if none is ser */ + * set a correct description if none is set */ priv->alarm = cal_component_alarm_new (); icalcomp = cal_component_alarm_get_icalcomponent (priv->alarm); @@ -215,7 +223,7 @@ alarm_page_finalize (GObject *object) priv = apage->priv; if (priv->xml) { - g_object_unref((priv->xml)); + g_object_unref (priv->xml); priv->xml = NULL; } @@ -224,6 +232,11 @@ alarm_page_finalize (GObject *object) priv->alarm = NULL; } + if (priv->list_store) { + g_object_unref (priv->list_store); + priv->list_store = NULL; + } + g_free (priv); apage->priv = NULL; @@ -281,7 +294,7 @@ clear_widgets (AlarmPage *apage) e_dialog_option_menu_set (priv->time, CAL_ALARM_TRIGGER_RELATIVE_START, time_map); /* List data */ - gtk_clist_clear (GTK_CLIST (priv->list)); + e_alarm_list_clear (priv->list_store); } /* Builds a string for the duration of the alarm. If the duration is zero, returns NULL. */ @@ -456,21 +469,15 @@ static void append_reminder (AlarmPage *apage, CalComponentAlarm *alarm) { AlarmPagePrivate *priv; - GtkCList *clist; - char *c[1]; + GtkTreeView *view; + GtkTreeIter iter; int i; priv = apage->priv; + view = GTK_TREE_VIEW (priv->list); - clist = GTK_CLIST (priv->list); - - c[0] = get_alarm_string (alarm); - i = gtk_clist_append (clist, c); - - gtk_clist_set_row_data_full (clist, i, alarm, (GtkDestroyNotify) cal_component_alarm_free); - gtk_clist_select_row (clist, i, 0); - g_free (c[0]); - + e_alarm_list_append (priv->list_store, &iter, alarm); + gtk_tree_selection_select_iter (gtk_tree_view_get_selection (view), &iter); gtk_widget_set_sensitive (priv->delete, TRUE); } @@ -482,7 +489,6 @@ alarm_page_fill_widgets (CompEditorPage *page, CalComponent *comp) AlarmPagePrivate *priv; CalComponentText text; GList *alarms, *l; - GtkCList *clist; CompEditorPageDates dates; apage = ALARM_PAGE (page); @@ -509,7 +515,6 @@ alarm_page_fill_widgets (CompEditorPage *page, CalComponent *comp) alarms = cal_component_get_alarm_uids (comp); - clist = GTK_CLIST (priv->list); for (l = alarms; l != NULL; l = l->next) { CalComponentAlarm *ca, *ca_copy; const char *auid; @@ -536,8 +541,11 @@ alarm_page_fill_component (CompEditorPage *page, CalComponent *comp) { AlarmPage *apage; AlarmPagePrivate *priv; + GtkTreeView *view; + GtkTreeModel *model; + GtkTreeIter iter; + gboolean valid_iter; GList *list, *l; - GtkCList *clist; int i; apage = ALARM_PAGE (page); @@ -556,13 +564,16 @@ alarm_page_fill_component (CompEditorPage *page, CalComponent *comp) /* Add the new alarms */ - clist = GTK_CLIST (priv->list); - for (i = 0; i < clist->rows; i++) { + view = GTK_TREE_VIEW (priv->list); + model = GTK_TREE_MODEL (priv->list_store); + + for (valid_iter = gtk_tree_model_get_iter_first (model, &iter); valid_iter; + valid_iter = gtk_tree_model_iter_next (model, &iter)) { CalComponentAlarm *alarm, *alarm_copy; icalcomponent *icalcomp; icalproperty *icalprop; - alarm = gtk_clist_get_row_data (clist, i); + alarm = (CalComponentAlarm *) e_alarm_list_get_alarm (priv->list_store, &iter); g_assert (alarm != NULL); /* We set the description of the alarm if it's got @@ -755,26 +766,39 @@ delete_clicked_cb (GtkButton *button, gpointer data) { AlarmPage *apage; AlarmPagePrivate *priv; - GtkCList *clist; + GtkTreeSelection *selection; + GtkTreeIter iter; + GtkTreePath *path; + gboolean valid_iter; int sel; apage = ALARM_PAGE (data); priv = apage->priv; - clist = GTK_CLIST (priv->list); - if (!clist->selection) + selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->list)); + if (!gtk_tree_selection_get_selected (selection, NULL, &iter)) { + g_warning ("Could not get a selection to delete."); return; + } - sel = GPOINTER_TO_INT (clist->selection->data); + path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->list_store), &iter); + e_alarm_list_remove (priv->list_store, &iter); - gtk_clist_remove (clist, sel); - if (sel >= clist->rows) - sel--; + /* Select closest item after removal */ + valid_iter = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path); + if (!valid_iter) { + gtk_tree_path_prev (path); + valid_iter = gtk_tree_model_get_iter (GTK_TREE_MODEL (priv->list_store), &iter, path); + } - if (clist->rows > 0) - gtk_clist_select_row (clist, sel, 0); + if (valid_iter) { + gtk_tree_selection_select_iter (selection, &iter); + gtk_widget_set_sensitive (priv->delete, TRUE); + } else gtk_widget_set_sensitive (priv->delete, FALSE); + + gtk_tree_path_free (path); } /* Callback used when the alarm options button is clicked */ @@ -799,26 +823,52 @@ static void init_widgets (AlarmPage *apage) { AlarmPagePrivate *priv; + GtkTreeViewColumn *column; + GtkCellRenderer *cell_renderer; priv = apage->priv; /* Reminder buttons */ - g_signal_connect((priv->add), "clicked", - G_CALLBACK (add_clicked_cb), apage); - g_signal_connect((priv->delete), "clicked", - G_CALLBACK (delete_clicked_cb), apage); + g_signal_connect ((priv->add), "clicked", + G_CALLBACK (add_clicked_cb), apage); + g_signal_connect ((priv->delete), "clicked", + G_CALLBACK (delete_clicked_cb), apage); + + gtk_widget_set_sensitive (priv->delete, FALSE); /* Connect the default signal handler to use to make sure we notify * upstream of changes to the widget values. */ - g_signal_connect((priv->add), "clicked", - G_CALLBACK (field_changed_cb), apage); - g_signal_connect((priv->delete), "clicked", - G_CALLBACK (field_changed_cb), apage); + g_signal_connect ((priv->add), "clicked", + G_CALLBACK (field_changed_cb), apage); + g_signal_connect ((priv->delete), "clicked", + G_CALLBACK (field_changed_cb), apage); /* Options button */ - g_signal_connect((priv->button_options), "clicked", - G_CALLBACK (button_options_clicked_cb), apage); + g_signal_connect ((priv->button_options), "clicked", + G_CALLBACK (button_options_clicked_cb), apage); + + /* Alarm list */ + + /* Model */ + priv->list_store = e_alarm_list_new (); + gtk_tree_view_set_model (GTK_TREE_VIEW (priv->list), + GTK_TREE_MODEL (priv->list_store)); + + /* View */ + column = gtk_tree_view_column_new (); + gtk_tree_view_column_set_title (column, "Action/Trigger"); /* Not shown */ + cell_renderer = GTK_CELL_RENDERER (gtk_cell_renderer_text_new ()); + gtk_tree_view_column_pack_start (column, cell_renderer, TRUE); + gtk_tree_view_column_add_attribute (column, cell_renderer, "text", E_ALARM_LIST_COLUMN_DESCRIPTION); + gtk_tree_view_append_column (GTK_TREE_VIEW (priv->list), column); + +#if 0 + /* If we want the alarm setup widgets to reflect the currently selected alarm, we + * need to do something like this */ + g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->list)), "changed", + G_CALLBACK (alarm_selection_changed_cb), apage); +#endif } diff --git a/calendar/gui/dialogs/alarm-page.h b/calendar/gui/dialogs/alarm-page.h index 4209ab013c..d814e850b3 100644 --- a/calendar/gui/dialogs/alarm-page.h +++ b/calendar/gui/dialogs/alarm-page.h @@ -1,11 +1,12 @@ /* Evolution calendar - Alarm page of the calendar component dialogs * - * Copyright (C) 2001 Ximian, Inc. + * Copyright (C) 2001-2003 Ximian, Inc. * * Authors: Federico Mena-Quintero * Miguel de Icaza * Seth Alves * JP Rosevear + * Hans Petter Jansson * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public diff --git a/calendar/gui/dialogs/recurrence-page.c b/calendar/gui/dialogs/recurrence-page.c index e9667e943b..2e3d9f479b 100644 --- a/calendar/gui/dialogs/recurrence-page.c +++ b/calendar/gui/dialogs/recurrence-page.c @@ -2,12 +2,13 @@ /* Evolution calendar - Recurrence page of the calendar component dialogs * - * Copyright (C) 2001 Ximian, Inc. + * Copyright (C) 2001-2003 Ximian, Inc. * * Authors: Federico Mena-Quintero * Miguel de Icaza * Seth Alves * JP Rosevear + * Hans Petter Jansson * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -32,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -321,15 +321,6 @@ recurrence_page_init (RecurrencePage *rpage) priv->comp = NULL; } -/* Frees the CalComponentDateTime stored in the GtkCList */ -static void -free_exception_date_time (CalComponentDateTime *dt) -{ - g_free (dt->value); - g_free ((char*)dt->tzid); - g_free (dt); -} - /* Destroy handler for the recurrence page */ static void recurrence_page_finalize (GObject *object) @@ -496,14 +487,11 @@ fill_exception_widgets (RecurrencePage *rpage, CalComponent *comp) { RecurrencePagePrivate *priv; GSList *list, *l; - gboolean added; + gboolean added = FALSE; priv = rpage->priv; - cal_component_get_exdate_list (comp, &list); - added = FALSE; - for (l = list; l; l = l->next) { CalComponentDateTime *cdt; @@ -514,9 +502,6 @@ fill_exception_widgets (RecurrencePage *rpage, CalComponent *comp) } cal_component_free_exdate_list (list); - - if (added) - gtk_clist_select_row (GTK_CLIST (priv->exception_list), 0, 0); } /* Computes a weekday mask for the start day of a calendar component, diff --git a/calendar/gui/dialogs/recurrence-page.h b/calendar/gui/dialogs/recurrence-page.h index 319d4b1b1a..7a10eff069 100644 --- a/calendar/gui/dialogs/recurrence-page.h +++ b/calendar/gui/dialogs/recurrence-page.h @@ -1,11 +1,12 @@ /* Evolution calendar - Recurrence page of the calendar component dialogs * - * Copyright (C) 2001 Ximian, Inc. + * Copyright (C) 2001-2003 Ximian, Inc. * * Authors: Federico Mena-Quintero * Miguel de Icaza * Seth Alves * JP Rosevear + * Hans Petter Jansson * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public diff --git a/calendar/gui/e-alarm-list.c b/calendar/gui/e-alarm-list.c new file mode 100644 index 0000000000..e81db8c302 --- /dev/null +++ b/calendar/gui/e-alarm-list.c @@ -0,0 +1,688 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ + +/* EAlarmList - list of calendar alarms with GtkTreeModel interface. + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * 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. + * + * Authors: Hans Petter Jansson + */ + +#include +#include +#include +#include +#include +#include +#include +#include "calendar-config.h" +#include "e-alarm-list.h" + +#define G_LIST(x) ((GList *) x) +#define E_ALARM_LIST_IS_SORTED(list) (E_ALARM_LIST (list)->sort_column_id != -2) +#define IS_VALID_ITER(dt_list, iter) (iter!= NULL && iter->user_data != NULL && \ + dt_list->stamp == iter->stamp) + +static GType column_types [E_ALARM_LIST_NUM_COLUMNS]; + +static void e_alarm_list_init (EAlarmList *file_list); +static void e_alarm_list_class_init (EAlarmListClass *class); +static void e_alarm_list_tree_model_init (GtkTreeModelIface *iface); +static void e_alarm_list_finalize (GObject *object); +static guint e_alarm_list_get_flags (GtkTreeModel *tree_model); +static gint e_alarm_list_get_n_columns (GtkTreeModel *tree_model); +static GType e_alarm_list_get_column_type (GtkTreeModel *tree_model, + gint index); +static gboolean e_alarm_list_get_iter (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreePath *path); +static GtkTreePath *e_alarm_list_get_path (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static void e_alarm_list_get_value (GtkTreeModel *tree_model, + GtkTreeIter *iter, + gint column, + GValue *value); +static gboolean e_alarm_list_iter_next (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static gboolean e_alarm_list_iter_children (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent); +static gboolean e_alarm_list_iter_has_child (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static gint e_alarm_list_iter_n_children (GtkTreeModel *tree_model, + GtkTreeIter *iter); +static gboolean e_alarm_list_iter_nth_child (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent, + gint n); +static gboolean e_alarm_list_iter_parent (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *child); + +static GObjectClass *parent_class = NULL; + +GtkType +e_alarm_list_get_type (void) +{ + static GType alarm_list_type = 0; + + if (!alarm_list_type) { + static const GTypeInfo alarm_list_info = + { + sizeof (EAlarmListClass), + NULL, /* base_init */ + NULL, /* base_finalize */ + (GClassInitFunc) e_alarm_list_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (EAlarmList), + 0, + (GInstanceInitFunc) e_alarm_list_init, + }; + + static const GInterfaceInfo tree_model_info = + { + (GInterfaceInitFunc) e_alarm_list_tree_model_init, + NULL, + NULL + }; + + column_types [E_ALARM_LIST_COLUMN_DESCRIPTION] = G_TYPE_STRING; + + alarm_list_type = g_type_register_static (G_TYPE_OBJECT, "EAlarmList", + &alarm_list_info, 0); + g_type_add_interface_static (alarm_list_type, + GTK_TYPE_TREE_MODEL, + &tree_model_info); + } + + return alarm_list_type; +} + +static void +e_alarm_list_class_init (EAlarmListClass *class) +{ + GObjectClass *object_class; + + parent_class = g_type_class_peek_parent (class); + object_class = (GObjectClass *) class; + + object_class->finalize = e_alarm_list_finalize; +} + +static void +e_alarm_list_tree_model_init (GtkTreeModelIface *iface) +{ + iface->get_flags = e_alarm_list_get_flags; + iface->get_n_columns = e_alarm_list_get_n_columns; + iface->get_column_type = e_alarm_list_get_column_type; + iface->get_iter = e_alarm_list_get_iter; + iface->get_path = e_alarm_list_get_path; + iface->get_value = e_alarm_list_get_value; + iface->iter_next = e_alarm_list_iter_next; + iface->iter_children = e_alarm_list_iter_children; + iface->iter_has_child = e_alarm_list_iter_has_child; + iface->iter_n_children = e_alarm_list_iter_n_children; + iface->iter_nth_child = e_alarm_list_iter_nth_child; + iface->iter_parent = e_alarm_list_iter_parent; +} + +static void +e_alarm_list_init (EAlarmList *alarm_list) +{ + alarm_list->stamp = g_random_int (); + alarm_list->columns_dirty = FALSE; + alarm_list->list = NULL; +} + +EAlarmList * +e_alarm_list_new (void) +{ + EAlarmList *alarm_list; + + alarm_list = E_ALARM_LIST (g_object_new (e_alarm_list_get_type (), NULL)); + + return alarm_list; +} + +static void +all_rows_deleted (EAlarmList *alarm_list) +{ + GtkTreePath *path; + gint i; + + if (!alarm_list->list) + return; + + path = gtk_tree_path_new (); + i = g_list_length (alarm_list->list); + gtk_tree_path_append_index (path, i); + + for ( ; i >= 0; i--) { + gtk_tree_model_row_deleted (GTK_TREE_MODEL (alarm_list), path); + gtk_tree_path_prev (path); + } + + gtk_tree_path_free (path); +} + +static void +row_deleted (EAlarmList *alarm_list, gint n) +{ + GtkTreePath *path; + gint i; + + path = gtk_tree_path_new (); + gtk_tree_path_append_index (path, n); + gtk_tree_model_row_deleted (GTK_TREE_MODEL (alarm_list), path); + gtk_tree_path_free (path); +} + +static void +row_added (EAlarmList *alarm_list, gint n) +{ + GtkTreePath *path; + GtkTreeIter iter; + + path = gtk_tree_path_new (); + gtk_tree_path_append_index (path, n); + + if (gtk_tree_model_get_iter (GTK_TREE_MODEL (alarm_list), &iter, path)) + gtk_tree_model_row_inserted (GTK_TREE_MODEL (alarm_list), path, &iter); + + gtk_tree_path_free (path); +} + +static void +row_updated (EAlarmList *alarm_list, gint n) +{ + GtkTreePath *path; + GtkTreeIter iter; + + path = gtk_tree_path_new (); + gtk_tree_path_append_index (path, n); + + if (gtk_tree_model_get_iter (GTK_TREE_MODEL (alarm_list), &iter, path)) + gtk_tree_model_row_changed (GTK_TREE_MODEL (alarm_list), path, &iter); + + gtk_tree_path_free (path); +} + +static void +e_alarm_list_finalize (GObject *object) +{ + EAlarmList *alarm_list = E_ALARM_LIST (object); +} + +/* Fulfill the GtkTreeModel requirements */ +static guint +e_alarm_list_get_flags (GtkTreeModel *tree_model) +{ + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), 0); + + return GTK_TREE_MODEL_LIST_ONLY; +} + +static gint +e_alarm_list_get_n_columns (GtkTreeModel *tree_model) +{ + EAlarmList *alarm_list = (EAlarmList *) tree_model; + + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), 0); + + alarm_list->columns_dirty = TRUE; + return E_ALARM_LIST_NUM_COLUMNS; +} + +static GType +e_alarm_list_get_column_type (GtkTreeModel *tree_model, + gint index) +{ + EAlarmList *alarm_list = (EAlarmList *) tree_model; + + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), G_TYPE_INVALID); + g_return_val_if_fail (index < E_ALARM_LIST_NUM_COLUMNS && + index >= 0, G_TYPE_INVALID); + + alarm_list->columns_dirty = TRUE; + return column_types [index]; +} + +const CalComponentAlarm * +e_alarm_list_get_alarm (EAlarmList *alarm_list, GtkTreeIter *iter) +{ + g_return_val_if_fail (IS_VALID_ITER (alarm_list, iter), NULL); + + return G_LIST (iter->user_data)->data; +} + +static void +free_alarm (CalComponentAlarm *alarm) +{ + cal_component_alarm_free (alarm); +} + +static CalComponentAlarm * +copy_alarm (const CalComponentAlarm *alarm) +{ + return cal_component_alarm_clone ((CalComponentAlarm *) alarm); +} + +void +e_alarm_list_set_alarm (EAlarmList *alarm_list, GtkTreeIter *iter, + const CalComponentAlarm *alarm) +{ + CalComponentAlarm *alarm_old; + + g_return_if_fail (IS_VALID_ITER (alarm_list, iter)); + + alarm_old = G_LIST (iter->user_data)->data; + free_alarm (alarm_old); + G_LIST (iter->user_data)->data = copy_alarm (alarm); + row_updated (alarm_list, g_list_position (alarm_list->list, G_LIST (iter->user_data))); +} + +void +e_alarm_list_append (EAlarmList *alarm_list, GtkTreeIter *iter, + const CalComponentAlarm *alarm) +{ + CalComponentAlarm *alarm_copy; + + g_return_if_fail (alarm != NULL); + + alarm_list->list = g_list_append (alarm_list->list, copy_alarm (alarm)); + row_added (alarm_list, g_list_length (alarm_list->list) - 1); + + if (iter) { + iter->user_data = g_list_last (alarm_list->list); + iter->stamp = alarm_list->stamp; + } +} + +void +e_alarm_list_remove (EAlarmList *alarm_list, GtkTreeIter *iter) +{ + gint n; + + g_return_if_fail (IS_VALID_ITER (alarm_list, iter)); + + n = g_list_position (alarm_list->list, G_LIST (iter->user_data)); + free_alarm ((CalComponentAlarm *) G_LIST (iter->user_data)->data); + alarm_list->list = g_list_delete_link (alarm_list->list, G_LIST (iter->user_data)); + row_deleted (alarm_list, n); +} + +void +e_alarm_list_clear (EAlarmList *alarm_list) +{ + GList *l; + + all_rows_deleted (alarm_list); + + for (l = alarm_list->list; l; l = g_list_next (l)) { + free_alarm ((CalComponentAlarm *) l->data); + } + + g_list_free (alarm_list->list); + alarm_list->list = NULL; +} + +static gboolean +e_alarm_list_get_iter (GtkTreeModel *tree_model, GtkTreeIter *iter, GtkTreePath *path) +{ + EAlarmList *alarm_list = (EAlarmList *) tree_model; + GList *l; + gint i; + + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), FALSE); + g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE); + + if (!alarm_list->list) + return FALSE; + + alarm_list->columns_dirty = TRUE; + + i = gtk_tree_path_get_indices (path)[0]; + l = g_list_nth (alarm_list->list, i); + if (!l) + return FALSE; + + iter->user_data = l; + iter->stamp = alarm_list->stamp; + return TRUE; +} + +static GtkTreePath * +e_alarm_list_get_path (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + EAlarmList *alarm_list = (EAlarmList *) tree_model; + GtkTreePath *retval; + GList *l; + + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), NULL); + g_return_val_if_fail (iter->stamp == E_ALARM_LIST (tree_model)->stamp, NULL); + + l = iter->user_data; + retval = gtk_tree_path_new (); + gtk_tree_path_append_index (retval, g_list_position (alarm_list->list, l)); + return retval; +} + +/* Builds a string for the duration of the alarm. If the duration is zero, returns NULL. */ +static char * +get_alarm_duration_string (struct icaldurationtype *duration) +{ + GString *string = g_string_new (NULL); + char *ret; + gboolean have_something; + + have_something = FALSE; + + if (duration->days > 1) { + g_string_sprintf (string, _("%d days"), duration->days); + have_something = TRUE; + } else if (duration->days == 1) { + g_string_append (string, _("1 day")); + have_something = TRUE; + } + + if (duration->weeks > 1) { + g_string_sprintf (string, _("%d weeks"), duration->weeks); + have_something = TRUE; + } else if (duration->weeks == 1) { + g_string_append (string, _("1 week")); + have_something = TRUE; + } + + if (duration->hours > 1) { + g_string_sprintf (string, _("%d hours"), duration->hours); + have_something = TRUE; + } else if (duration->hours == 1) { + g_string_append (string, _("1 hour")); + have_something = TRUE; + } + + if (duration->minutes > 1) { + g_string_sprintf (string, _("%d minutes"), duration->minutes); + have_something = TRUE; + } else if (duration->minutes == 1) { + g_string_append (string, _("1 minute")); + have_something = TRUE; + } + + if (duration->seconds > 1) { + g_string_sprintf (string, _("%d seconds"), duration->seconds); + have_something = TRUE; + } else if (duration->seconds == 1) { + g_string_append (string, _("1 second")); + have_something = TRUE; + } + + if (have_something) { + ret = string->str; + g_string_free (string, FALSE); + return ret; + } else { + g_string_free (string, TRUE); + return NULL; + } +} + +static char * +get_alarm_string (CalComponentAlarm *alarm) +{ + CalAlarmAction action; + CalAlarmTrigger trigger; + char string[256]; + char *base, *str = NULL, *dur; + + string [0] = '\0'; + + cal_component_alarm_get_action (alarm, &action); + cal_component_alarm_get_trigger (alarm, &trigger); + + switch (action) { + case CAL_ALARM_AUDIO: + base = _("Play a sound"); + break; + + case CAL_ALARM_DISPLAY: + base = _("Display a message"); + break; + + case CAL_ALARM_EMAIL: + base = _("Send an email"); + break; + + case CAL_ALARM_PROCEDURE: + base = _("Run a program"); + break; + + case CAL_ALARM_NONE: + case CAL_ALARM_UNKNOWN: + default: + base = _("Unknown action to be performed"); + break; + } + + /* FIXME: This does not look like it will localize correctly. */ + + switch (trigger.type) { + case CAL_ALARM_TRIGGER_RELATIVE_START: + dur = get_alarm_duration_string (&trigger.u.rel_duration); + + if (dur) { + if (trigger.u.rel_duration.is_neg) + str = g_strdup_printf (_("%s %s before the start of the appointment"), + base, dur); + else + str = g_strdup_printf (_("%s %s after the start of the appointment"), + base, dur); + + g_free (dur); + } else + str = g_strdup_printf (_("%s at the start of the appointment"), base); + + break; + + case CAL_ALARM_TRIGGER_RELATIVE_END: + dur = get_alarm_duration_string (&trigger.u.rel_duration); + + if (dur) { + if (trigger.u.rel_duration.is_neg) + str = g_strdup_printf (_("%s %s before the end of the appointment"), + base, dur); + else + str = g_strdup_printf (_("%s %s after the end of the appointment"), + base, dur); + + g_free (dur); + } else + str = g_strdup_printf (_("%s at the end of the appointment"), base); + + break; + + case CAL_ALARM_TRIGGER_ABSOLUTE: { + struct icaltimetype itt; + icaltimezone *utc_zone, *current_zone; + char *location; + struct tm tm; + char buf[256]; + + /* Absolute triggers come in UTC, so convert them to the local timezone */ + + itt = trigger.u.abs_time; + + utc_zone = icaltimezone_get_utc_timezone (); + location = calendar_config_get_timezone (); + current_zone = icaltimezone_get_builtin_timezone (location); + + tm = icaltimetype_to_tm_with_zone (&itt, utc_zone, current_zone); + + e_time_format_date_and_time (&tm, calendar_config_get_24_hour_format (), + FALSE, FALSE, buf, sizeof (buf)); + + str = g_strdup_printf (_("%s at %s"), base, buf); + + break; } + + case CAL_ALARM_TRIGGER_NONE: + default: + str = g_strdup_printf (_("%s for an unknown trigger type"), base); + break; + } + + return str; +} + +static void +e_alarm_list_get_value (GtkTreeModel *tree_model, + GtkTreeIter *iter, + gint column, + GValue *value) +{ + EAlarmList *alarm_list = E_ALARM_LIST (tree_model); + CalComponentAlarm *alarm; + GList *l; + const gchar *str; + + g_return_if_fail (E_IS_ALARM_LIST (tree_model)); + g_return_if_fail (column < E_ALARM_LIST_NUM_COLUMNS); + g_return_if_fail (E_ALARM_LIST (tree_model)->stamp == iter->stamp); + g_return_if_fail (IS_VALID_ITER (alarm_list, iter)); + + g_value_init (value, column_types [column]); + + if (!alarm_list->list) + return; + + l = iter->user_data; + alarm = l->data; + + if (!alarm) + return; + + switch (column) { + case E_ALARM_LIST_COLUMN_DESCRIPTION: + str = get_alarm_string (alarm); + g_value_set_string (value, str); + break; + } +} + +static gboolean +e_alarm_list_iter_next (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + GList *l; + + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), FALSE); + g_return_val_if_fail (IS_VALID_ITER (E_ALARM_LIST (tree_model), iter), FALSE); + + if (!E_ALARM_LIST (tree_model)->list) + return FALSE; + + l = iter->user_data; + l = g_list_next (l); + if (l) { + iter->user_data = l; + return TRUE; + } + + return FALSE; +} + +static gboolean +e_alarm_list_iter_children (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent) +{ + EAlarmList *alarm_list = E_ALARM_LIST (tree_model); + + /* this is a list, nodes have no children */ + if (parent) + return FALSE; + + /* but if parent == NULL we return the list itself as children of the + * "root" */ + + if (!alarm_list->list) + return FALSE; + + iter->stamp = E_ALARM_LIST (tree_model)->stamp; + iter->user_data = alarm_list->list; + return TRUE; +} + +static gboolean +e_alarm_list_iter_has_child (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + g_return_val_if_fail (IS_VALID_ITER (E_ALARM_LIST (tree_model), iter), FALSE); + return FALSE; +} + +static gint +e_alarm_list_iter_n_children (GtkTreeModel *tree_model, + GtkTreeIter *iter) +{ + EAlarmList *alarm_list = E_ALARM_LIST (tree_model); + + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), -1); + g_return_val_if_fail (IS_VALID_ITER (alarm_list, iter), -1); + + if (iter == NULL) + return g_list_length (alarm_list->list); + + g_return_val_if_fail (E_ALARM_LIST (tree_model)->stamp == iter->stamp, -1); + return 0; +} + +static gboolean +e_alarm_list_iter_nth_child (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *parent, + gint n) +{ + EAlarmList *alarm_list = E_ALARM_LIST (tree_model); + + g_return_val_if_fail (E_IS_ALARM_LIST (tree_model), FALSE); + + if (parent) + return FALSE; + + if (alarm_list->list) { + GList *l; + + l = g_list_nth (alarm_list->list, n); + if (!l) + return FALSE; + + iter->stamp = alarm_list->stamp; + iter->user_data = l; + return TRUE; + } + + return FALSE; +} + +static gboolean +e_alarm_list_iter_parent (GtkTreeModel *tree_model, + GtkTreeIter *iter, + GtkTreeIter *child) +{ + return FALSE; +} diff --git a/calendar/gui/e-alarm-list.h b/calendar/gui/e-alarm-list.h new file mode 100644 index 0000000000..2a03f9099b --- /dev/null +++ b/calendar/gui/e-alarm-list.h @@ -0,0 +1,79 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ + +/* EAlarmList - list of calendar alarms with GtkTreeModel interface. + * + * Copyright (C) 2003 Ximian, Inc. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of version 2 of the GNU General Public + * License as published by the Free Software Foundation. + * + * 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. + * + * Authors: Hans Petter Jansson + */ + +#ifndef E_ALARM_LIST_H +#define E_ALARM_LIST_H + +#include +#include + +G_BEGIN_DECLS + +#define E_TYPE_ALARM_LIST (e_alarm_list_get_type ()) +#define E_ALARM_LIST(obj) (GTK_CHECK_CAST ((obj), E_TYPE_ALARM_LIST, EAlarmList)) +#define E_ALARM_LIST_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), E_TYPE_ALARM_LIST, EAlarmListClass)) +#define E_IS_ALARM_LIST(obj) (GTK_CHECK_TYPE ((obj), E_TYPE_ALARM_LIST)) +#define E_IS_ALARM_LIST_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), E_TYPE_ALARM_LIST)) +#define E_ALARM_LIST_GET_CLASS(obj) (GTK_CHECK_GET_CLASS ((obj), E_TYPE_ALARM_LIST, EAlarmListClass)) + +typedef struct _EAlarmList EAlarmList; +typedef struct _EAlarmListClass EAlarmListClass; + +typedef enum +{ + E_ALARM_LIST_COLUMN_DESCRIPTION, + + E_ALARM_LIST_NUM_COLUMNS +} +EAlarmListColumnType; + +struct _EAlarmList +{ + GObject parent; + + /* Private */ + + gint stamp; + GList *list; + + guint columns_dirty : 1; +}; + +struct _EAlarmListClass +{ + GObjectClass parent_class; +}; + +GtkType e_alarm_list_get_type (void); +EAlarmList *e_alarm_list_new (void); + +const CalComponentAlarm *e_alarm_list_get_alarm (EAlarmList *alarm_list, GtkTreeIter *iter); +void e_alarm_list_set_alarm (EAlarmList *alarm_list, GtkTreeIter *iter, + const CalComponentAlarm *datetime); +void e_alarm_list_append (EAlarmList *alarm_list, GtkTreeIter *iter, + const CalComponentAlarm *datetime); +void e_alarm_list_remove (EAlarmList *alarm_list, GtkTreeIter *iter); +void e_alarm_list_clear (EAlarmList *alarm_list); + +G_END_DECLS + +#endif /* E_ALARM_LIST_H */ -- cgit v1.2.3