diff options
Diffstat (limited to 'calendar/cal-util')
-rw-r--r-- | calendar/cal-util/.cvsignore | 7 | ||||
-rw-r--r-- | calendar/cal-util/Makefile.am | 49 | ||||
-rw-r--r-- | calendar/cal-util/cal-component.c | 4094 | ||||
-rw-r--r-- | calendar/cal-util/cal-component.h | 361 | ||||
-rw-r--r-- | calendar/cal-util/cal-recur.c | 3776 | ||||
-rw-r--r-- | calendar/cal-util/cal-recur.h | 57 | ||||
-rw-r--r-- | calendar/cal-util/cal-util.c | 75 | ||||
-rw-r--r-- | calendar/cal-util/cal-util.h | 58 | ||||
-rw-r--r-- | calendar/cal-util/calobj.c | 2008 | ||||
-rw-r--r-- | calendar/cal-util/calobj.h | 312 | ||||
-rw-r--r-- | calendar/cal-util/test-recur.c | 196 | ||||
-rw-r--r-- | calendar/cal-util/timeutil.c | 395 | ||||
-rw-r--r-- | calendar/cal-util/timeutil.h | 65 |
13 files changed, 0 insertions, 11453 deletions
diff --git a/calendar/cal-util/.cvsignore b/calendar/cal-util/.cvsignore deleted file mode 100644 index 9f93120f8a..0000000000 --- a/calendar/cal-util/.cvsignore +++ /dev/null @@ -1,7 +0,0 @@ -.libs -Makefile.in -Makefile -.deps -*.lo -*.la -test-recur diff --git a/calendar/cal-util/Makefile.am b/calendar/cal-util/Makefile.am deleted file mode 100644 index 80c8da2662..0000000000 --- a/calendar/cal-util/Makefile.am +++ /dev/null @@ -1,49 +0,0 @@ -noinst_PROGRAMS = test-recur - -INCLUDES = \ - -DGNOMELOCALEDIR=\""$(localedir)"\" \ - -DG_LOG_DOMAIN=\"cal-util\" \ - -I$(top_srcdir) \ - -I$(top_srcdir)/calendar \ - -I. \ - -I.. \ - -I$(top_builddir) \ - -I$(top_srcdir)/libical/src/libical \ - -I$(top_builddir)/libical/src/libical \ - $(GNOME_INCLUDEDIR) - -# -# cal util library -# - -lib_LTLIBRARIES = libcal-util.la - -libcal_util_la_SOURCES = \ - cal-component.c \ - cal-recur.c \ - cal-util.c \ - timeutil.c - -libcal_utilincludedir = $(includedir)/evolution/cal-util - -libcal_utilinclude_HEADERS = \ - cal-component.h \ - cal-recur.h \ - cal-util.h \ - timeutil.h - -# -# static library for use in conduits' shared libraries -# -noinst_LTLIBRARIES = libcal-util-static.la -libcal_util_static_la_SOURCES = $(libcal_util_la_SOURCES) -libcal_util_static_la_LDFLAGS = --all-static - -test_recur_SOURCES = \ - test-recur.c - -test_recur_LDADD = \ - libcal-util.la \ - $(top_builddir)/libversit/libversit.la \ - $(top_builddir)/libical/src/libical/libical.la \ - $(EXTRA_GNOME_LIBS) diff --git a/calendar/cal-util/cal-component.c b/calendar/cal-util/cal-component.c deleted file mode 100644 index d716c0e581..0000000000 --- a/calendar/cal-util/cal-component.c +++ /dev/null @@ -1,4094 +0,0 @@ -/* Evolution calendar - iCalendar component object - * - * Copyright (C) 2000 Helix Code, Inc. - * Copyright (C) 2000 Ximian, Inc. - * - * Author: Federico Mena-Quintero <federico@ximian.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -#include <config.h> -#include <string.h> -#include <stdlib.h> -#include <unistd.h> -#include "cal-component.h" -#include "timeutil.h" - - - -/* Extension property for alarm components so that we can reference them by UID */ -#define EVOLUTION_ALARM_UID_PROPERTY "X-EVOLUTION-ALARM-UID" - -/* Private part of the CalComponent structure */ -struct _CalComponentPrivate { - /* The icalcomponent we wrap */ - icalcomponent *icalcomp; - - /* Properties */ - - icalproperty *uid; - - icalproperty *status; - - icalproperty *categories; - - icalproperty *classification; - - struct text { - icalproperty *prop; - icalparameter *altrep_param; - }; - - GSList *comment_list; /* list of struct text */ - - icalproperty *completed; - icalproperty *created; - - GSList *description_list; /* list of struct text */ - - struct datetime { - icalproperty *prop; - icalparameter *tzid_param; - }; - - struct datetime dtstart; - struct datetime dtend; - - icalproperty *dtstamp; - - struct datetime due; - - GSList *exdate_list; /* list of struct datetime */ - GSList *exrule_list; /* list of icalproperty objects */ - - icalproperty *geo; - icalproperty *last_modified; - icalproperty *percent; - icalproperty *priority; - - struct period { - icalproperty *prop; - icalparameter *value_param; - }; - - GSList *rdate_list; /* list of struct period */ - - GSList *rrule_list; /* list of icalproperty objects */ - - icalproperty *sequence; - - struct { - icalproperty *prop; - icalparameter *altrep_param; - } summary; - - icalproperty *transparency; - icalproperty *url; - - /* Subcomponents */ - - GHashTable *alarm_uid_hash; - - /* Whether we should increment the sequence number when piping the - * object over the wire. - */ - guint need_sequence_inc : 1; -}; - -/* Private structure for alarms */ -struct _CalComponentAlarm { - /* Alarm icalcomponent we wrap */ - icalcomponent *icalcomp; - - /* Our extension UID property */ - icalproperty *uid; - - /* Properties */ - - icalproperty *action; - icalproperty *trigger; -}; - - - -static void cal_component_class_init (CalComponentClass *class); -static void cal_component_init (CalComponent *comp); -static void cal_component_destroy (GtkObject *object); - -static GtkObjectClass *parent_class; - - - -/** - * cal_component_get_type: - * - * Registers the #CalComponent class if necessary, and returns the type ID - * associated to it. - * - * Return value: The type ID of the #CalComponent class. - **/ -GtkType -cal_component_get_type (void) -{ - static GtkType cal_component_type = 0; - - if (!cal_component_type) { - static const GtkTypeInfo cal_component_info = { - "CalComponent", - sizeof (CalComponent), - sizeof (CalComponentClass), - (GtkClassInitFunc) cal_component_class_init, - (GtkObjectInitFunc) cal_component_init, - NULL, /* reserved_1 */ - NULL, /* reserved_2 */ - (GtkClassInitFunc) NULL - }; - - cal_component_type = gtk_type_unique (GTK_TYPE_OBJECT, &cal_component_info); - } - - return cal_component_type; -} - -/* Class initialization function for the calendar component object */ -static void -cal_component_class_init (CalComponentClass *class) -{ - GtkObjectClass *object_class; - - object_class = (GtkObjectClass *) class; - - parent_class = gtk_type_class (GTK_TYPE_OBJECT); - - object_class->destroy = cal_component_destroy; -} - -/* Object initialization function for the calendar component object */ -static void -cal_component_init (CalComponent *comp) -{ - CalComponentPrivate *priv; - - priv = g_new0 (CalComponentPrivate, 1); - comp->priv = priv; - - priv->alarm_uid_hash = g_hash_table_new (g_str_hash, g_str_equal); -} - -/* Does a simple g_free() of the elements of a GSList and then frees the list - * itself. Returns NULL. - */ -static GSList * -free_slist (GSList *slist) -{ - GSList *l; - - for (l = slist; l; l = l->next) - g_free (l->data); - - g_slist_free (slist); - return NULL; -} - -/* Used from g_hash_table_foreach_remove() to free the alarm UIDs hash table. - * We do not need to do anything to individual elements since we were storing - * the UID pointers inside the icalproperties themselves. - */ -static gboolean -free_alarm_cb (gpointer key, gpointer value, gpointer data) -{ - return TRUE; -} - -/* Frees the internal icalcomponent only if it does not have a parent. If it - * does, it means we don't own it and we shouldn't free it. - */ -static void -free_icalcomponent (CalComponent *comp) -{ - CalComponentPrivate *priv; - - priv = comp->priv; - - if (!priv->icalcomp) - return; - - /* Free the icalcomponent */ - - if (icalcomponent_get_parent (priv->icalcomp) == NULL) - icalcomponent_free (priv->icalcomp); - - priv->icalcomp = NULL; - - /* Free the mappings */ - - priv->uid = NULL; - - priv->status = NULL; - - priv->categories = NULL; - - priv->classification = NULL; - priv->comment_list = NULL; - priv->completed = NULL; - priv->created = NULL; - - priv->description_list = free_slist (priv->description_list); - - priv->dtend.prop = NULL; - priv->dtend.tzid_param = NULL; - - priv->dtstamp = NULL; - - priv->dtstart.prop = NULL; - priv->dtstart.tzid_param = NULL; - - priv->due.prop = NULL; - priv->due.tzid_param = NULL; - - priv->exdate_list = free_slist (priv->exdate_list); - - g_slist_free (priv->exrule_list); - priv->exrule_list = NULL; - - priv->geo = NULL; - priv->last_modified = NULL; - priv->percent = NULL; - priv->priority = NULL; - - priv->rdate_list = free_slist (priv->rdate_list); - - g_slist_free (priv->rrule_list); - priv->rrule_list = NULL; - - priv->sequence = NULL; - - priv->summary.prop = NULL; - priv->summary.altrep_param = NULL; - - priv->transparency = NULL; - priv->url = NULL; - - /* Free the subcomponents */ - - g_hash_table_foreach_remove (priv->alarm_uid_hash, free_alarm_cb, NULL); - - /* Clean up */ - - priv->need_sequence_inc = FALSE; -} - -/* Destroy handler for the calendar component object */ -static void -cal_component_destroy (GtkObject *object) -{ - CalComponent *comp; - CalComponentPrivate *priv; - - g_return_if_fail (object != NULL); - g_return_if_fail (IS_CAL_COMPONENT (object)); - - comp = CAL_COMPONENT (object); - priv = comp->priv; - - free_icalcomponent (comp); - g_hash_table_destroy (priv->alarm_uid_hash); - priv->alarm_uid_hash = NULL; - - g_free (priv); - comp->priv = NULL; - - if (GTK_OBJECT_CLASS (parent_class)->destroy) - (* GTK_OBJECT_CLASS (parent_class)->destroy) (object); -} - - - -/** - * cal_component_gen_uid: - * - * Generates a unique identifier suitable for calendar components. - * - * Return value: A unique identifier string. Every time this function is called - * a different string is returned. - **/ -char * -cal_component_gen_uid (void) -{ - char *iso, *ret; - static char *hostname; - time_t t = time (NULL); - static int serial; - - if (!hostname) { - static char buffer [512]; - - if ((gethostname (buffer, sizeof (buffer) - 1) == 0) && - (buffer [0] != 0)) - hostname = buffer; - else - hostname = "localhost"; - } - - iso = isodate_from_time_t (t); - ret = g_strdup_printf ("%s-%d-%d-%d-%d@%s", - iso, - getpid (), - getgid (), - getppid (), - serial++, - hostname); - g_free (iso); - - return ret; -} - -/** - * cal_component_new: - * - * Creates a new empty calendar component object. You should set it from an - * #icalcomponent structure by using cal_component_set_icalcomponent() or with a - * new empty component type by using cal_component_set_new_vtype(). - * - * Return value: A newly-created calendar component object. - **/ -CalComponent * -cal_component_new (void) -{ - return CAL_COMPONENT (gtk_type_new (CAL_COMPONENT_TYPE)); -} - -/** - * cal_component_clone: - * @comp: A calendar component object. - * - * Creates a new calendar component object by copying the information from - * another one. - * - * Return value: A newly-created calendar component with the same values as the - * original one. - **/ -CalComponent * -cal_component_clone (CalComponent *comp) -{ - CalComponentPrivate *priv; - CalComponent *new_comp; - icalcomponent *new_icalcomp; - - g_return_val_if_fail (comp != NULL, NULL); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), NULL); - - priv = comp->priv; - g_return_val_if_fail (priv->need_sequence_inc == FALSE, NULL); - - new_comp = cal_component_new (); - - if (priv->icalcomp) { - new_icalcomp = icalcomponent_new_clone (priv->icalcomp); - cal_component_set_icalcomponent (new_comp, new_icalcomp); - } - - return new_comp; -} - -/* Scans a date/time and timezone pair property */ -static void -scan_datetime (CalComponent *comp, struct datetime *datetime, icalproperty *prop) -{ - CalComponentPrivate *priv; - - priv = comp->priv; - - datetime->prop = prop; - datetime->tzid_param = icalproperty_get_first_parameter (prop, ICAL_TZID_PARAMETER); -} - -/* Scans an exception date property */ -static void -scan_exdate (CalComponent *comp, icalproperty *prop) -{ - CalComponentPrivate *priv; - struct datetime *dt; - - priv = comp->priv; - - dt = g_new (struct datetime, 1); - dt->prop = prop; - dt->tzid_param = icalproperty_get_first_parameter (prop, ICAL_TZID_PARAMETER); - - priv->exdate_list = g_slist_append (priv->exdate_list, dt); -} - -/* Scans an icalperiodtype property */ -static void -scan_period (CalComponent *comp, GSList **list, icalproperty *prop) -{ - struct period *period; - - period = g_new (struct period, 1); - period->prop = prop; - period->value_param = icalproperty_get_first_parameter (prop, ICAL_VALUE_PARAMETER); - - *list = g_slist_append (*list, period); -} - -/* Scans an icalrecurtype property */ -static void -scan_recur (CalComponent *comp, GSList **list, icalproperty *prop) -{ - *list = g_slist_append (*list, prop); -} - -/* Scans the summary property */ -static void -scan_summary (CalComponent *comp, icalproperty *prop) -{ - CalComponentPrivate *priv; - - priv = comp->priv; - - priv->summary.prop = prop; - priv->summary.altrep_param = icalproperty_get_first_parameter (prop, ICAL_ALTREP_PARAMETER); -} - -/* Scans a text (i.e. text + altrep) property */ -static void -scan_text (CalComponent *comp, GSList **text_list, icalproperty *prop) -{ - struct text *text; - - text = g_new (struct text, 1); - text->prop = prop; - text->altrep_param = icalproperty_get_first_parameter (prop, ICAL_ALTREP_PARAMETER); - - *text_list = g_slist_append (*text_list, text); -} - -/* Scans an icalproperty and adds its mapping to the component */ -static void -scan_property (CalComponent *comp, icalproperty *prop) -{ - CalComponentPrivate *priv; - icalproperty_kind kind; - - priv = comp->priv; - - kind = icalproperty_isa (prop); - - switch (kind) { - case ICAL_STATUS_PROPERTY: - priv->status = prop; - break; - - case ICAL_CATEGORIES_PROPERTY: - priv->categories = prop; - break; - - case ICAL_CLASS_PROPERTY: - priv->classification = prop; - break; - - case ICAL_COMMENT_PROPERTY: - scan_text (comp, &priv->comment_list, prop); - break; - - case ICAL_COMPLETED_PROPERTY: - priv->completed = prop; - break; - - case ICAL_CREATED_PROPERTY: - priv->created = prop; - break; - - case ICAL_DESCRIPTION_PROPERTY: - scan_text (comp, &priv->description_list, prop); - break; - - case ICAL_DTEND_PROPERTY: - scan_datetime (comp, &priv->dtend, prop); - break; - - case ICAL_DTSTAMP_PROPERTY: - priv->dtstamp = prop; - break; - - case ICAL_DTSTART_PROPERTY: - scan_datetime (comp, &priv->dtstart, prop); - break; - - case ICAL_DUE_PROPERTY: - scan_datetime (comp, &priv->due, prop); - break; - - case ICAL_EXDATE_PROPERTY: - scan_exdate (comp, prop); - break; - - case ICAL_EXRULE_PROPERTY: - scan_recur (comp, &priv->exrule_list, prop); - break; - - case ICAL_GEO_PROPERTY: - priv->geo = prop; - break; - - case ICAL_LASTMODIFIED_PROPERTY: - priv->last_modified = prop; - break; - - case ICAL_PERCENTCOMPLETE_PROPERTY: - priv->percent = prop; - break; - - case ICAL_PRIORITY_PROPERTY: - priv->priority = prop; - break; - - case ICAL_RDATE_PROPERTY: - scan_period (comp, &priv->rdate_list, prop); - break; - - case ICAL_RRULE_PROPERTY: - scan_recur (comp, &priv->rrule_list, prop); - break; - - case ICAL_SEQUENCE_PROPERTY: - priv->sequence = prop; - break; - - case ICAL_SUMMARY_PROPERTY: - scan_summary (comp, prop); - break; - - case ICAL_TRANSP_PROPERTY: - priv->transparency = prop; - break; - - case ICAL_UID_PROPERTY: - priv->uid = prop; - break; - - case ICAL_URL_PROPERTY: - priv->url = prop; - break; - - default: - break; - } -} - -/* Gets our alarm UID string from a property that is known to contain it */ -static const char * -alarm_uid_from_prop (icalproperty *prop) -{ - const char *xstr; - - g_assert (icalproperty_isa (prop) == ICAL_X_PROPERTY); - - xstr = icalproperty_get_x (prop); - g_assert (xstr != NULL); - - return xstr; -} - -/* Sets our alarm UID extension property on an alarm component. Returns a - * pointer to the UID string inside the property itself. - */ -static const char * -set_alarm_uid (icalcomponent *alarm, const char *auid) -{ - icalproperty *prop; - const char *inprop_auid; - - /* Create the new property */ - - prop = icalproperty_new_x ((char *) auid); - icalproperty_set_x_name (prop, EVOLUTION_ALARM_UID_PROPERTY); - - icalcomponent_add_property (alarm, prop); - - inprop_auid = alarm_uid_from_prop (prop); - return inprop_auid; -} - -/* Removes any alarm UID extension properties from an alarm subcomponent */ -static void -remove_alarm_uid (icalcomponent *alarm) -{ - icalproperty *prop; - GSList *list, *l; - - list = NULL; - - for (prop = icalcomponent_get_first_property (alarm, ICAL_X_PROPERTY); - prop; - prop = icalcomponent_get_next_property (alarm, ICAL_X_PROPERTY)) { - const char *xname; - - xname = icalproperty_get_x_name (prop); - g_assert (xname != NULL); - - if (strcmp (xname, EVOLUTION_ALARM_UID_PROPERTY) == 0) - list = g_slist_prepend (list, prop); - } - - for (l = list; l; l = l->next) { - prop = l->data; - icalcomponent_remove_property (alarm, prop); - icalproperty_free (prop); - } - - g_slist_free (list); -} - -/* Adds an alarm subcomponent to the calendar component's mapping table. The - * actual UID with which it gets added may not be the same as the specified one; - * this function will change it if the table already had an alarm subcomponent - * with the specified UID. Returns the actual UID used. - */ -static const char * -add_alarm (CalComponent *comp, icalcomponent *alarm, const char *auid) -{ - CalComponentPrivate *priv; - icalcomponent *old_alarm; - - priv = comp->priv; - - /* First we see if we already have an alarm with the requested UID. In - * that case, we need to change the new UID to something else. This - * should never happen, but who knows. - */ - - old_alarm = g_hash_table_lookup (priv->alarm_uid_hash, auid); - if (old_alarm != NULL) { - char *new_auid; - - g_message ("add_alarm(): Got alarm with duplicated UID `%s', changing it...", auid); - - remove_alarm_uid (alarm); - - new_auid = cal_component_gen_uid (); - auid = set_alarm_uid (alarm, new_auid); - g_free (new_auid); - } - - g_hash_table_insert (priv->alarm_uid_hash, (char *) auid, alarm); - return auid; -} - -/* Scans an alarm subcomponent, adds an UID extension property to it (so that we - * can reference alarms by unique IDs), and adds its mapping to the component. */ -static void -scan_alarm (CalComponent *comp, icalcomponent *alarm) -{ - CalComponentPrivate *priv; - icalproperty *prop; - const char *auid; - char *new_auid; - - priv = comp->priv; - - for (prop = icalcomponent_get_first_property (alarm, ICAL_X_PROPERTY); - prop; - prop = icalcomponent_get_next_property (alarm, ICAL_X_PROPERTY)) { - const char *xname; - - xname = icalproperty_get_x_name (prop); - g_assert (xname != NULL); - - if (strcmp (xname, EVOLUTION_ALARM_UID_PROPERTY) == 0) { - auid = alarm_uid_from_prop (prop); - add_alarm (comp, alarm, auid); - return; - } - } - - /* The component has no alarm UID property, so we create one. */ - - new_auid = cal_component_gen_uid (); - auid = set_alarm_uid (alarm, new_auid); - g_free (new_auid); - - add_alarm (comp, alarm, auid); -} - -/* Scans an icalcomponent for its properties so that we can provide - * random-access to them. It also builds a hash table of the component's alarm - * subcomponents. - */ -static void -scan_icalcomponent (CalComponent *comp) -{ - CalComponentPrivate *priv; - icalproperty *prop; - icalcompiter iter; - - priv = comp->priv; - - g_assert (priv->icalcomp != NULL); - - /* Scan properties */ - - for (prop = icalcomponent_get_first_property (priv->icalcomp, ICAL_ANY_PROPERTY); - prop; - prop = icalcomponent_get_next_property (priv->icalcomp, ICAL_ANY_PROPERTY)) - scan_property (comp, prop); - - /* Scan subcomponents */ - - for (iter = icalcomponent_begin_component (priv->icalcomp, ICAL_VALARM_COMPONENT); - icalcompiter_deref (&iter) != NULL; - icalcompiter_next (&iter)) { - icalcomponent *subcomp; - - subcomp = icalcompiter_deref (&iter); - scan_alarm (comp, subcomp); - } -} - -/* Ensures that the mandatory calendar component properties (uid, dtstamp) do - * exist. If they don't exist, it creates them automatically. - */ -static void -ensure_mandatory_properties (CalComponent *comp) -{ - CalComponentPrivate *priv; - - priv = comp->priv; - g_assert (priv->icalcomp != NULL); - - if (!priv->uid) { - char *uid; - - uid = cal_component_gen_uid (); - priv->uid = icalproperty_new_uid (uid); - g_free (uid); - - icalcomponent_add_property (priv->icalcomp, priv->uid); - } - - if (!priv->dtstamp) { - time_t tim; - struct icaltimetype t; - - tim = time (NULL); - t = icaltime_from_timet (tim, FALSE); - - priv->dtstamp = icalproperty_new_dtstamp (t); - icalcomponent_add_property (priv->icalcomp, priv->dtstamp); - } -} - -/** - * cal_component_set_new_vtype: - * @comp: A calendar component object. - * @type: Type of calendar component to create. - * - * Clears any existing component data from a calendar component object and - * creates a new #icalcomponent of the specified type for it. The only property - * that will be set in the new component will be its unique identifier. - **/ -void -cal_component_set_new_vtype (CalComponent *comp, CalComponentVType type) -{ - CalComponentPrivate *priv; - icalcomponent *icalcomp; - icalcomponent_kind kind; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - - free_icalcomponent (comp); - - if (type == CAL_COMPONENT_NO_TYPE) - return; - - /* Figure out the kind and create the icalcomponent */ - - switch (type) { - case CAL_COMPONENT_EVENT: - kind = ICAL_VEVENT_COMPONENT; - break; - - case CAL_COMPONENT_TODO: - kind = ICAL_VTODO_COMPONENT; - break; - - case CAL_COMPONENT_JOURNAL: - kind = ICAL_VJOURNAL_COMPONENT; - break; - - case CAL_COMPONENT_FREEBUSY: - kind = ICAL_VFREEBUSY_COMPONENT; - break; - - case CAL_COMPONENT_TIMEZONE: - kind = ICAL_VTIMEZONE_COMPONENT; - break; - - default: - g_assert_not_reached (); - kind = ICAL_NO_COMPONENT; - } - - icalcomp = icalcomponent_new (kind); - if (!icalcomp) { - g_message ("cal_component_set_new_vtype(): Could not create the icalcomponent!"); - return; - } - - /* Scan the component to build our mapping table */ - - priv->icalcomp = icalcomp; - scan_icalcomponent (comp); - - /* Add missing stuff */ - - ensure_mandatory_properties (comp); -} - -/** - * cal_component_set_icalcomponent: - * @comp: A calendar component object. - * @icalcomp: An #icalcomponent. - * - * Sets the contents of a calendar component object from an #icalcomponent - * structure. If the @comp already had an #icalcomponent set into it, it will - * will be freed automatically if the #icalcomponent does not have a parent - * component itself. - * - * Supported component types are VEVENT, VTODO, VJOURNAL, VFREEBUSY, and VTIMEZONE. - * - * Return value: TRUE on success, FALSE if @icalcomp is an unsupported component - * type. - **/ -gboolean -cal_component_set_icalcomponent (CalComponent *comp, icalcomponent *icalcomp) -{ - CalComponentPrivate *priv; - icalcomponent_kind kind; - - g_return_val_if_fail (comp != NULL, FALSE); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), FALSE); - - priv = comp->priv; - - if (priv->icalcomp == icalcomp) - return TRUE; - - free_icalcomponent (comp); - - if (!icalcomp) { - priv->icalcomp = NULL; - return TRUE; - } - - kind = icalcomponent_isa (icalcomp); - - if (!(kind == ICAL_VEVENT_COMPONENT - || kind == ICAL_VTODO_COMPONENT - || kind == ICAL_VJOURNAL_COMPONENT - || kind == ICAL_VFREEBUSY_COMPONENT - || kind == ICAL_VTIMEZONE_COMPONENT)) - return FALSE; - - priv->icalcomp = icalcomp; - - scan_icalcomponent (comp); - ensure_mandatory_properties (comp); - - return TRUE; -} - -/** - * cal_component_get_icalcomponent: - * @comp: A calendar component object. - * - * Queries the #icalcomponent structure that a calendar component object is - * wrapping. - * - * Return value: An #icalcomponent structure, or NULL if the @comp has no - * #icalcomponent set to it. - **/ -icalcomponent * -cal_component_get_icalcomponent (CalComponent *comp) -{ - CalComponentPrivate *priv; - - g_return_val_if_fail (comp != NULL, NULL); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), NULL); - - priv = comp->priv; - g_return_val_if_fail (priv->need_sequence_inc == FALSE, NULL); - - return priv->icalcomp; -} - -/** - * cal_component_get_vtype: - * @comp: A calendar component object. - * - * Queries the type of a calendar component object. - * - * Return value: The type of the component, as defined by RFC 2445. - **/ -CalComponentVType -cal_component_get_vtype (CalComponent *comp) -{ - CalComponentPrivate *priv; - icalcomponent_kind kind; - - g_return_val_if_fail (comp != NULL, CAL_COMPONENT_NO_TYPE); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), CAL_COMPONENT_NO_TYPE); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, CAL_COMPONENT_NO_TYPE); - - kind = icalcomponent_isa (priv->icalcomp); - switch (kind) { - case ICAL_VEVENT_COMPONENT: - return CAL_COMPONENT_EVENT; - - case ICAL_VTODO_COMPONENT: - return CAL_COMPONENT_TODO; - - case ICAL_VJOURNAL_COMPONENT: - return CAL_COMPONENT_JOURNAL; - - case ICAL_VFREEBUSY_COMPONENT: - return CAL_COMPONENT_FREEBUSY; - - case ICAL_VTIMEZONE_COMPONENT: - return CAL_COMPONENT_TIMEZONE; - - default: - /* We should have been loaded with a supported type! */ - g_assert_not_reached (); - return CAL_COMPONENT_NO_TYPE; - } -} - -/** - * cal_component_get_as_string: - * @comp: A calendar component. - * - * Gets the iCalendar string representation of a calendar component. You should - * call cal_component_commit_sequence() before this function to ensure that the - * component's sequence number is consistent with the state of the object. - * - * Return value: String representation of the calendar component according to - * RFC 2445. - **/ -char * -cal_component_get_as_string (CalComponent *comp) -{ - CalComponentPrivate *priv; - char *str, *buf; - - g_return_val_if_fail (comp != NULL, NULL); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), NULL); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, NULL); - - /* Ensure that the user has committed the new SEQUENCE */ - g_return_val_if_fail (priv->need_sequence_inc == FALSE, NULL); - - /* We dup the string; libical owns that memory */ - - str = icalcomponent_as_ical_string (priv->icalcomp); - - if (str) - buf = g_strdup (str); - else - buf = NULL; - - return buf; -} - -/** - * cal_component_commit_sequence: - * @comp: - * - * Increments the sequence number property in a calendar component object if it - * needs it. This needs to be done when any of a number of properties listed in - * RFC 2445 change values, such as the start and end dates of a component. - * - * This function must be called before calling cal_component_get_as_string() to - * ensure that the component is fully consistent. - **/ -void -cal_component_commit_sequence (CalComponent *comp) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!priv->need_sequence_inc) - return; - - if (priv->sequence) { - int seq; - - seq = icalproperty_get_sequence (priv->sequence); - icalproperty_set_sequence (priv->sequence, seq + 1); - } else { - /* The component had no SEQUENCE property, so assume that the - * default would have been zero. Since it needed incrementing - * anyways, we use a value of 1 here. - */ - priv->sequence = icalproperty_new_sequence (1); - icalcomponent_add_property (priv->icalcomp, priv->sequence); - } - - priv->need_sequence_inc = FALSE; -} - -/** - * cal_component_get_uid: - * @comp: A calendar component object. - * @uid: Return value for the UID string. - * - * Queries the unique identifier of a calendar component object. - **/ -void -cal_component_get_uid (CalComponent *comp, const char **uid) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (uid != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - /* This MUST exist, since we ensured that it did */ - g_assert (priv->uid != NULL); - - *uid = icalproperty_get_uid (priv->uid); -} - -/** - * cal_component_set_uid: - * @comp: A calendar component object. - * @uid: Unique identifier. - * - * Sets the unique identifier string of a calendar component object. - **/ -void -cal_component_set_uid (CalComponent *comp, const char *uid) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (uid != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - /* This MUST exist, since we ensured that it did */ - g_assert (priv->uid != NULL); - - icalproperty_set_uid (priv->uid, (char *) uid); -} - -/** - * cal_component_get_categories: - * @comp: A calendar component object. - * @categories: - * - * - **/ -void -cal_component_get_categories (CalComponent *comp, const char **categories) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (categories != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (priv->categories) - *categories = icalproperty_get_categories (priv->categories); - else - *categories = NULL; -} - -/** - * cal_component_set_categories: - * @comp: A calendar component object. - * @categories: - * - * - **/ -void -cal_component_set_categories (CalComponent *comp, const char *categories) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!categories || !(*categories)) { - if (priv->categories) { - icalcomponent_remove_property (priv->icalcomp, priv->categories); - icalproperty_free (priv->categories); - priv->url = NULL; - } - - return; - } - - if (priv->categories) - icalproperty_set_categories (priv->categories, (char *) categories); - else { - priv->categories = icalproperty_new_categories ((char *) categories); - icalcomponent_add_property (priv->icalcomp, priv->categories); - } -} - - -/** - * cal_component_get_categories_list: - * @comp: A calendar component object. - * @categ_list: Return value for the list of strings, where each string is a - * category. This should be freed using cal_component_free_categories_list(). - * - * Queries the list of categories of a calendar component object. Each element - * in the returned categ_list is a string with the corresponding category. - **/ -void -cal_component_get_categories_list (CalComponent *comp, GSList **categ_list) -{ - CalComponentPrivate *priv; - const char *categories; - const char *p; - const char *cat_start; - char *str; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (categ_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!priv->categories) { - *categ_list = NULL; - return; - } - - categories = icalproperty_get_categories (priv->categories); - g_assert (categories != NULL); - - cat_start = categories; - *categ_list = NULL; - - for (p = categories; *p; p++) - if (*p == ',') { - str = g_strndup (cat_start, p - cat_start); - *categ_list = g_slist_prepend (*categ_list, str); - - cat_start = p + 1; - } - - str = g_strndup (cat_start, p - cat_start); - *categ_list = g_slist_prepend (*categ_list, str); - - *categ_list = g_slist_reverse (*categ_list); -} - -/* Creates a comma-delimited string of categories */ -static char * -stringify_categories (GSList *categ_list) -{ - GString *s; - GSList *l; - char *str; - - s = g_string_new (NULL); - - for (l = categ_list; l; l = l->next) { - g_string_append (s, l->data); - - if (l->next != NULL) - g_string_append (s, ","); - } - - str = s->str; - g_string_free (s, FALSE); - - return str; -} - -/** - * cal_component_set_categories_list: - * @comp: A calendar component object. - * @categ_list: List of strings, one for each category. - * - * Sets the list of categories of a calendar component object. - **/ -void -cal_component_set_categories_list (CalComponent *comp, GSList *categ_list) -{ - CalComponentPrivate *priv; - char *categories_str; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!categ_list) { - if (priv->categories) { - icalcomponent_remove_property (priv->icalcomp, priv->categories); - icalproperty_free (priv->categories); - } - - return; - } - - /* Create a single string of categories */ - categories_str = stringify_categories (categ_list); - - /* Set the categories */ - priv->categories = icalproperty_new_categories (categories_str); - g_free (categories_str); - - icalcomponent_add_property (priv->icalcomp, priv->categories); -} - -/** - * cal_component_get_classification: - * @comp: A calendar component object. - * @classif: Return value for the classification. - * - * Queries the classification of a calendar component object. If the - * classification property is not set on this component, this function returns - * #CAL_COMPONENT_CLASS_NONE. - **/ -void -cal_component_get_classification (CalComponent *comp, CalComponentClassification *classif) -{ - CalComponentPrivate *priv; - const char *class; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (classif != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!priv->classification) { - *classif = CAL_COMPONENT_CLASS_NONE; - return; - } - - class = icalproperty_get_class (priv->classification); - - if (strcasecmp (class, "PUBLIC") == 0) - *classif = CAL_COMPONENT_CLASS_PUBLIC; - else if (strcasecmp (class, "PRIVATE") == 0) - *classif = CAL_COMPONENT_CLASS_PRIVATE; - else if (strcasecmp (class, "CONFIDENTIAL") == 0) - *classif = CAL_COMPONENT_CLASS_CONFIDENTIAL; - else - *classif = CAL_COMPONENT_CLASS_UNKNOWN; -} - -/** - * cal_component_set_classification: - * @comp: A calendar component object. - * @classif: Classification to use. - * - * Sets the classification property of a calendar component object. To unset - * the property, specify CAL_COMPONENT_CLASS_NONE for @classif. - **/ -void -cal_component_set_classification (CalComponent *comp, CalComponentClassification classif) -{ - CalComponentPrivate *priv; - char *str; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (classif != CAL_COMPONENT_CLASS_UNKNOWN); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (classif == CAL_COMPONENT_CLASS_NONE) { - if (priv->classification) { - icalcomponent_remove_property (priv->icalcomp, priv->classification); - icalproperty_free (priv->classification); - priv->classification = NULL; - } - - return; - } - - switch (classif) { - case CAL_COMPONENT_CLASS_PUBLIC: - str = "PUBLIC"; - break; - - case CAL_COMPONENT_CLASS_PRIVATE: - str = "PRIVATE"; - break; - - case CAL_COMPONENT_CLASS_CONFIDENTIAL: - str = "CONFIDENTIAL"; - break; - - default: - g_assert_not_reached (); - str = NULL; - } - - if (priv->classification) - icalproperty_set_class (priv->classification, str); - else { - priv->classification = icalproperty_new_class (str); - icalcomponent_add_property (priv->icalcomp, priv->classification); - } -} - -/* Gets a text list value */ -static void -get_text_list (GSList *text_list, - const char *(* get_prop_func) (icalproperty *prop), - GSList **tl) -{ - GSList *l; - - *tl = NULL; - - if (!text_list) - return; - - for (l = text_list; l; l = l->next) { - struct text *text; - CalComponentText *t; - - text = l->data; - g_assert (text->prop != NULL); - - t = g_new (CalComponentText, 1); - t->value = (* get_prop_func) (text->prop); - - if (text->altrep_param) - t->altrep = icalparameter_get_altrep (text->altrep_param); - else - t->altrep = NULL; - - *tl = g_slist_prepend (*tl, t); - } - - *tl = g_slist_reverse (*tl); -} - -/* Sets a text list value */ -static void -set_text_list (CalComponent *comp, - icalproperty *(* new_prop_func) (const char *value), - GSList **text_list, - GSList *tl) -{ - CalComponentPrivate *priv; - GSList *l; - - priv = comp->priv; - - /* Remove old texts */ - - for (l = *text_list; l; l = l->next) { - struct text *text; - - text = l->data; - g_assert (text->prop != NULL); - - icalcomponent_remove_property (priv->icalcomp, text->prop); - icalproperty_free (text->prop); - g_free (text); - } - - g_slist_free (*text_list); - *text_list = NULL; - - /* Add in new texts */ - - for (l = tl; l; l = l->next) { - CalComponentText *t; - struct text *text; - - t = l->data; - g_return_if_fail (t->value != NULL); - - text = g_new (struct text, 1); - - text->prop = (* new_prop_func) ((char *) t->value); - icalcomponent_add_property (priv->icalcomp, text->prop); - - if (t->altrep) { - text->altrep_param = icalparameter_new_altrep ((char *) t->altrep); - icalproperty_add_parameter (text->prop, text->altrep_param); - } else - text->altrep_param = NULL; - - *text_list = g_slist_prepend (*text_list, text); - } - - *text_list = g_slist_reverse (*text_list); -} - -/** - * cal_component_get_comment_list: - * @comp: A calendar component object. - * @text_list: Return value for the comment properties and their parameters, as - * a list of #CalComponentText structures. This should be freed using the - * cal_component_free_text_list() function. - * - * Queries the comment of a calendar component object. The comment property can - * appear several times inside a calendar component, and so a list of - * #CalComponentText is returned. - **/ -void -cal_component_get_comment_list (CalComponent *comp, GSList **text_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (text_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_text_list (priv->comment_list, icalproperty_get_comment, text_list); -} - -/** - * cal_component_set_comment_list: - * @comp: A calendar component object. - * @text_list: List of #CalComponentText structures. - * - * Sets the comment of a calendar component object. The comment property can - * appear several times inside a calendar component, and so a list of - * #CalComponentText structures is used. - **/ -void -cal_component_set_comment_list (CalComponent *comp, GSList *text_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_text_list (comp, icalproperty_new_comment, &priv->comment_list, text_list); -} - -/* Gets a struct icaltimetype value */ -static void -get_icaltimetype (icalproperty *prop, - struct icaltimetype (* get_prop_func) (icalproperty *prop), - struct icaltimetype **t) -{ - if (!prop) { - *t = NULL; - return; - } - - *t = g_new (struct icaltimetype, 1); - **t = (* get_prop_func) (prop); -} - -/* Sets a struct icaltimetype value */ -static void -set_icaltimetype (CalComponent *comp, icalproperty **prop, - icalproperty *(* prop_new_func) (struct icaltimetype v), - void (* prop_set_func) (icalproperty *prop, struct icaltimetype v), - struct icaltimetype *t) -{ - CalComponentPrivate *priv; - - priv = comp->priv; - - if (!t) { - if (*prop) { - icalcomponent_remove_property (priv->icalcomp, *prop); - icalproperty_free (*prop); - *prop = NULL; - } - - return; - } - - if (*prop) - (* prop_set_func) (*prop, *t); - else { - *prop = (* prop_new_func) (*t); - icalcomponent_add_property (priv->icalcomp, *prop); - } -} - -/** - * cal_component_get_completed: - * @comp: A calendar component object. - * @t: Return value for the completion date. This should be freed using the - * cal_component_free_icaltimetype() function. - * - * Queries the date at which a calendar compoment object was completed. - **/ -void -cal_component_get_completed (CalComponent *comp, struct icaltimetype **t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (t != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_icaltimetype (priv->completed, icalproperty_get_completed, t); -} - -/** - * cal_component_set_completed: - * @comp: A calendar component object. - * @t: Value for the completion date. - * - * Sets the date at which a calendar component object was completed. - **/ -void -cal_component_set_completed (CalComponent *comp, struct icaltimetype *t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_icaltimetype (comp, &priv->completed, - icalproperty_new_completed, - icalproperty_set_completed, - t); -} - - -/** - * cal_component_get_created: - * @comp: A calendar component object. - * @t: Return value for the creation date. This should be freed using the - * cal_component_free_icaltimetype() function. - * - * Queries the date in which a calendar component object was created in the - * calendar store. - **/ -void -cal_component_get_created (CalComponent *comp, struct icaltimetype **t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (t != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_icaltimetype (priv->created, icalproperty_get_created, t); -} - -/** - * cal_component_set_created: - * @comp: A calendar component object. - * @t: Value for the creation date. - * - * Sets the date in which a calendar component object is created in the calendar - * store. This should only be used inside a calendar store application, i.e. - * not by calendar user agents. - **/ -void -cal_component_set_created (CalComponent *comp, struct icaltimetype *t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_icaltimetype (comp, &priv->created, - icalproperty_new_created, - icalproperty_set_created, - t); -} - -/** - * cal_component_get_description_list: - * @comp: A calendar component object. - * @text_list: Return value for the description properties and their parameters, - * as a list of #CalComponentText structures. This should be freed using the - * cal_component_free_text_list() function. - * - * Queries the description of a calendar component object. Journal components - * may have more than one description, and as such this function returns a list - * of #CalComponentText structures. All other types of components can have at - * most one description. - **/ -void -cal_component_get_description_list (CalComponent *comp, GSList **text_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (text_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_text_list (priv->description_list, icalproperty_get_description, text_list); -} - -/** - * cal_component_set_description_list: - * @comp: A calendar component object. - * @text_list: List of #CalComponentSummary structures. - * - * Sets the description of a calendar component object. Journal components may - * have more than one description, and as such this function takes in a list of - * #CalComponentDescription structures. All other types of components can have - * at most one description. - **/ -void -cal_component_set_description_list (CalComponent *comp, GSList *text_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_text_list (comp, icalproperty_new_description, &priv->description_list, text_list); -} - -/* Gets a date/time and timezone pair */ -static void -get_datetime (struct datetime *datetime, - struct icaltimetype (* get_prop_func) (icalproperty *prop), - CalComponentDateTime *dt) -{ - if (datetime->prop) { - dt->value = g_new (struct icaltimetype, 1); - *dt->value = (* get_prop_func) (datetime->prop); - } else - dt->value = NULL; - - if (datetime->tzid_param) - dt->tzid = icalparameter_get_tzid (datetime->tzid_param); - else - dt->tzid = NULL; -} - -/* Sets a date/time and timezone pair */ -static void -set_datetime (CalComponent *comp, struct datetime *datetime, - icalproperty *(* prop_new_func) (struct icaltimetype v), - void (* prop_set_func) (icalproperty * prop, struct icaltimetype v), - CalComponentDateTime *dt) -{ - CalComponentPrivate *priv; - - priv = comp->priv; - - if (!dt) { - if (datetime->prop) { - icalcomponent_remove_property (priv->icalcomp, datetime->prop); - icalproperty_free (datetime->prop); - - datetime->prop = NULL; - datetime->tzid_param = NULL; - } - - return; - } - - g_return_if_fail (dt->value != NULL); - - if (datetime->prop) - (* prop_set_func) (datetime->prop, *dt->value); - else { - datetime->prop = (* prop_new_func) (*dt->value); - icalcomponent_add_property (priv->icalcomp, datetime->prop); - } - - if (dt->tzid) { - g_assert (datetime->prop != NULL); - - if (datetime->tzid_param) - icalparameter_set_tzid (datetime->tzid_param, (char *) dt->tzid); - else { - datetime->tzid_param = icalparameter_new_tzid ((char *) dt->tzid); - icalproperty_add_parameter (datetime->prop, datetime->tzid_param); - } - } else if (datetime->tzid_param) { - icalproperty_remove_parameter (datetime->prop, ICAL_TZID_PARAMETER); - icalparameter_free (datetime->tzid_param); - datetime->tzid_param = NULL; - } -} - -/** - * cal_component_get_dtend: - * @comp: A calendar component object. - * @dt: Return value for the date/time end. This should be freed with the - * cal_component_free_datetime() function. - * - * Queries the date/time end of a calendar component object. - **/ -void -cal_component_get_dtend (CalComponent *comp, CalComponentDateTime *dt) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (dt != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_datetime (&priv->dtend, icalproperty_get_dtend, dt); -} - -/** - * cal_component_set_dtend: - * @comp: A calendar component object. - * @dt: End date/time. - * - * Sets the date/time end property of a calendar component object. - **/ -void -cal_component_set_dtend (CalComponent *comp, CalComponentDateTime *dt) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_datetime (comp, &priv->dtend, - icalproperty_new_dtend, - icalproperty_set_dtend, - dt); - - priv->need_sequence_inc = TRUE; -} - -/** - * cal_component_get_dtstamp: - * @comp: A calendar component object. - * @t: Return value for the date/timestamp. - * - * Queries the date/timestamp property of a calendar component object, which is - * the last time at which the object was modified by a calendar user agent. - **/ -void -cal_component_get_dtstamp (CalComponent *comp, struct icaltimetype *t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (t != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - /* This MUST exist, since we ensured that it did */ - g_assert (priv->dtstamp != NULL); - - *t = icalproperty_get_dtstamp (priv->dtstamp); -} - -/** - * cal_component_set_dtstamp: - * @comp: A calendar component object. - * @t: Date/timestamp value. - * - * Sets the date/timestamp of a calendar component object. This should be - * called whenever a calendar user agent makes a change to a component's - * properties. - **/ -void -cal_component_set_dtstamp (CalComponent *comp, struct icaltimetype *t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (t != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - /* This MUST exist, since we ensured that it did */ - g_assert (priv->dtstamp != NULL); - - icalproperty_set_dtstamp (priv->dtstamp, *t); -} - -/** - * cal_component_get_dtstart: - * @comp: A calendar component object. - * @dt: Return value for the date/time start. This should be freed with the - * cal_component_free_datetime() function. - * - * Queries the date/time start of a calendar component object. - **/ -void -cal_component_get_dtstart (CalComponent *comp, CalComponentDateTime *dt) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (dt != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_datetime (&priv->dtstart, icalproperty_get_dtstart, dt); -} - -/** - * cal_component_set_dtstart: - * @comp: A calendar component object. - * @dt: Start date/time. - * - * Sets the date/time start property of a calendar component object. - **/ -void -cal_component_set_dtstart (CalComponent *comp, CalComponentDateTime *dt) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_datetime (comp, &priv->dtstart, - icalproperty_new_dtstart, - icalproperty_set_dtstart, - dt); - - priv->need_sequence_inc = TRUE; -} - -/** - * cal_component_get_due: - * @comp: A calendar component object. - * @dt: Return value for the due date/time. This should be freed with the - * cal_component_free_datetime() function. - * - * Queries the due date/time of a calendar component object. - **/ -void -cal_component_get_due (CalComponent *comp, CalComponentDateTime *dt) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (dt != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_datetime (&priv->due, icalproperty_get_due, dt); -} - -/** - * cal_component_set_due: - * @comp: A calendar component object. - * @dt: End date/time. - * - * Sets the due date/time property of a calendar component object. - **/ -void -cal_component_set_due (CalComponent *comp, CalComponentDateTime *dt) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_datetime (comp, &priv->due, - icalproperty_new_due, - icalproperty_set_due, - dt); - - priv->need_sequence_inc = TRUE; -} - -/* Builds a list of CalComponentPeriod structures based on a list of icalproperties */ -static void -get_period_list (GSList *period_list, - struct icaldatetimeperiodtype (* get_prop_func) (icalproperty *prop), - GSList **list) -{ - GSList *l; - - *list = NULL; - - if (!period_list) - return; - - for (l = period_list; l; l = l->next) { - struct period *period; - CalComponentPeriod *p; - struct icaldatetimeperiodtype ip; - - period = l->data; - g_assert (period->prop != NULL); - - p = g_new (CalComponentPeriod, 1); - - /* Get value parameter */ - - if (period->value_param) { - icalparameter_value value_type; - - value_type = icalparameter_get_value (period->value_param); - - if (value_type == ICAL_VALUE_DATE || value_type == ICAL_VALUE_DATETIME) - p->type = CAL_COMPONENT_PERIOD_DATETIME; - else if (value_type == ICAL_VALUE_DURATION) - p->type = CAL_COMPONENT_PERIOD_DURATION; - else { - g_message ("get_period_list(): Unknown value for period %d; " - "using DATETIME", value_type); - p->type = CAL_COMPONENT_PERIOD_DATETIME; - } - } else - p->type = CAL_COMPONENT_PERIOD_DATETIME; - - /* Get start and end/duration */ - - ip = (* get_prop_func) (period->prop); - - p->start = ip.period.start; - - if (p->type == CAL_COMPONENT_PERIOD_DATETIME) - p->u.end = ip.period.end; - else if (p->type == CAL_COMPONENT_PERIOD_DURATION) - p->u.duration = ip.period.duration; - else - g_assert_not_reached (); - - /* Put in list */ - - *list = g_slist_prepend (*list, p); - } - - *list = g_slist_reverse (*list); -} - -/* Sets a period list value */ -static void -set_period_list (CalComponent *comp, - icalproperty *(* new_prop_func) (struct icaldatetimeperiodtype period), - GSList **period_list, - GSList *pl) -{ - CalComponentPrivate *priv; - GSList *l; - - priv = comp->priv; - - /* Remove old periods */ - - for (l = *period_list; l; l = l->next) { - struct period *period; - - period = l->data; - g_assert (period->prop != NULL); - - icalcomponent_remove_property (priv->icalcomp, period->prop); - icalproperty_free (period->prop); - g_free (period); - } - - g_slist_free (*period_list); - *period_list = NULL; - - /* Add in new periods */ - - for (l = pl; l; l = l->next) { - CalComponentPeriod *p; - struct period *period; - struct icaldatetimeperiodtype ip; - icalparameter_value value_type; - - g_assert (l->data != NULL); - p = l->data; - - /* Create libical value */ - - ip.period.start = p->start; - - if (p->type == CAL_COMPONENT_PERIOD_DATETIME) { - value_type = ICAL_VALUE_DATETIME; - ip.period.end = p->u.end; - } else if (p->type == CAL_COMPONENT_PERIOD_DURATION) { - value_type = ICAL_VALUE_DURATION; - ip.period.duration = p->u.duration; - } else { - g_assert_not_reached (); - return; - } - - /* Create property */ - - period = g_new (struct period, 1); - - period->prop = (* new_prop_func) (ip); - period->value_param = icalparameter_new_value (value_type); - icalproperty_add_parameter (period->prop, period->value_param); - - /* Add to list */ - - *period_list = g_slist_prepend (*period_list, period); - } - - *period_list = g_slist_reverse (*period_list); -} - -/** - * cal_component_get_exdate_list: - * @comp: A calendar component object. - * @exdate_list: Return value for the list of exception dates, as a list of - * #CalComponentDateTime structures. This should be freed using the - * cal_component_free_exdate_list() function. - * - * Queries the list of exception date properties in a calendar component object. - **/ -void -cal_component_get_exdate_list (CalComponent *comp, GSList **exdate_list) -{ - CalComponentPrivate *priv; - GSList *l; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (exdate_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - *exdate_list = NULL; - - for (l = priv->exdate_list; l; l = l->next) { - struct datetime *dt; - CalComponentDateTime *cdt; - - dt = l->data; - - cdt = g_new (CalComponentDateTime, 1); - cdt->value = g_new (struct icaltimetype, 1); - - *cdt->value = icalproperty_get_exdate (dt->prop); - - if (dt->tzid_param) - cdt->tzid = icalparameter_get_tzid (dt->tzid_param); - else - cdt->tzid = NULL; - - *exdate_list = g_slist_prepend (*exdate_list, cdt); - } - - *exdate_list = g_slist_reverse (*exdate_list); -} - -/** - * cal_component_set_exdate_list: - * @comp: A calendar component object. - * @exdate_list: List of #CalComponentDateTime structures. - * - * Sets the list of exception dates in a calendar component object. - **/ -void -cal_component_set_exdate_list (CalComponent *comp, GSList *exdate_list) -{ - CalComponentPrivate *priv; - GSList *l; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - /* Remove old exception dates */ - - for (l = priv->exdate_list; l; l = l->next) { - struct datetime *dt; - - dt = l->data; - - icalcomponent_remove_property (priv->icalcomp, dt->prop); - icalproperty_free (dt->prop); - g_free (dt); - } - - g_slist_free (priv->exdate_list); - priv->exdate_list = NULL; - - /* Add in new exception dates */ - - for (l = exdate_list; l; l = l->next) { - CalComponentDateTime *cdt; - struct datetime *dt; - - g_assert (l->data != NULL); - cdt = l->data; - - g_assert (cdt->value != NULL); - - dt = g_new (struct datetime, 1); - dt->prop = icalproperty_new_exdate (*cdt->value); - - if (cdt->tzid) { - dt->tzid_param = icalparameter_new_tzid ((char *) cdt->tzid); - icalproperty_add_parameter (dt->prop, dt->tzid_param); - } else - dt->tzid_param = NULL; - - icalcomponent_add_property (priv->icalcomp, dt->prop); - priv->exdate_list = g_slist_prepend (priv->exdate_list, dt); - } - - priv->exdate_list = g_slist_reverse (priv->exdate_list); - - priv->need_sequence_inc = TRUE; -} - -/** - * cal_component_has_exdates: - * @comp: A calendar component object. - * - * Queries whether a calendar component object has any exception dates defined - * for it. - * - * Return value: TRUE if the component has exception dates, FALSE otherwise. - **/ -gboolean -cal_component_has_exdates (CalComponent *comp) -{ - CalComponentPrivate *priv; - - g_return_val_if_fail (comp != NULL, FALSE); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), FALSE); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, FALSE); - - return (priv->exdate_list != NULL); -} - -/* Gets a list of recurrence rules */ -static void -get_recur_list (GSList *recur_list, - struct icalrecurrencetype (* get_prop_func) (icalproperty *prop), - GSList **list) -{ - GSList *l; - - *list = NULL; - - for (l = recur_list; l; l = l->next) { - icalproperty *prop; - struct icalrecurrencetype *r; - - prop = l->data; - - r = g_new (struct icalrecurrencetype, 1); - *r = (* get_prop_func) (prop); - - *list = g_slist_prepend (*list, r); - } - - *list = g_slist_reverse (*list); -} - -/* Sets a list of recurrence rules */ -static void -set_recur_list (CalComponent *comp, - icalproperty *(* new_prop_func) (struct icalrecurrencetype recur), - GSList **recur_list, - GSList *rl) -{ - CalComponentPrivate *priv; - GSList *l; - - priv = comp->priv; - - /* Remove old recurrences */ - - for (l = *recur_list; l; l = l->next) { - icalproperty *prop; - - prop = l->data; - icalcomponent_remove_property (priv->icalcomp, prop); - icalproperty_free (prop); - } - - g_slist_free (*recur_list); - *recur_list = NULL; - - /* Add in new recurrences */ - - for (l = rl; l; l = l->next) { - icalproperty *prop; - struct icalrecurrencetype *recur; - - g_assert (l->data != NULL); - recur = l->data; - - prop = (* new_prop_func) (*recur); - icalcomponent_add_property (priv->icalcomp, prop); - - *recur_list = g_slist_prepend (*recur_list, prop); - } - - *recur_list = g_slist_reverse (*recur_list); -} - -/** - * cal_component_get_exrule_list: - * @comp: A calendar component object. - * @recur_list: List of exception rules as struct #icalrecurrencetype - * structures. This should be freed using the cal_component_free_recur_list() - * function. - * - * Queries the list of exception rule properties of a calendar component - * object. - **/ -void -cal_component_get_exrule_list (CalComponent *comp, GSList **recur_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (recur_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_recur_list (priv->exrule_list, icalproperty_get_exrule, recur_list); -} - -/** - * cal_component_get_exrule_property_list: - * @comp: A calendar component object. - * @recur_list: Returns a list of exception rule properties. - * - * Queries the list of exception rule properties of a calendar component object. - **/ -void -cal_component_get_exrule_property_list (CalComponent *comp, GSList **recur_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (recur_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - *recur_list = priv->exrule_list; -} - -/** - * cal_component_set_exrule_list: - * @comp: A calendar component object. - * @recur_list: List of struct #icalrecurrencetype structures. - * - * Sets the list of exception rules in a calendar component object. - **/ -void -cal_component_set_exrule_list (CalComponent *comp, GSList *recur_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_recur_list (comp, icalproperty_new_exrule, &priv->exrule_list, recur_list); - - priv->need_sequence_inc = TRUE; -} - -/** - * cal_component_has_exrules: - * @comp: A calendar component object. - * - * Queries whether a calendar component object has any exception rules defined - * for it. - * - * Return value: TRUE if the component has exception rules, FALSE otherwise. - **/ -gboolean -cal_component_has_exrules (CalComponent *comp) -{ - CalComponentPrivate *priv; - - g_return_val_if_fail (comp != NULL, FALSE); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), FALSE); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, FALSE); - - return (priv->exrule_list != NULL); -} - -/** - * cal_component_has_exceptions: - * @comp: A calendar component object - * - * Queries whether a calendar component object has any exception dates - * or exception rules. - * - * Return value: TRUE if the component has exceptions, FALSE otherwise. - **/ -gboolean -cal_component_has_exceptions (CalComponent *comp) -{ - return cal_component_has_exdates (comp) || cal_component_has_exrules (comp); -} - -/** - * cal_component_get_geo: - * @comp: A calendar component object. - * @geo: Return value for the geographic position property. This should be - * freed using the cal_component_free_geo() function. - * - * Sets the geographic position property of a calendar component object. - **/ -void -cal_component_get_geo (CalComponent *comp, struct icalgeotype **geo) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (geo != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (priv->geo) { - *geo = g_new (struct icalgeotype, 1); - **geo = icalproperty_get_geo (priv->geo); - } else - *geo = NULL; -} - -/** - * cal_component_set_geo: - * @comp: A calendar component object. - * @geo: Value for the geographic position property. - * - * Sets the geographic position property on a calendar component object. - **/ -void -cal_component_set_geo (CalComponent *comp, struct icalgeotype *geo) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!geo) { - if (priv->geo) { - icalcomponent_remove_property (priv->icalcomp, priv->geo); - icalproperty_free (priv->geo); - priv->geo = NULL; - } - - return; - } - - if (priv->geo) - icalproperty_set_geo (priv->geo, *geo); - else { - priv->geo = icalproperty_new_geo (*geo); - icalcomponent_add_property (priv->icalcomp, priv->geo); - } -} - -/** - * cal_component_get_last_modified: - * @comp: A calendar component object. - * @t: Return value for the last modified time value. - * - * Queries the time at which a calendar component object was last modified in - * the calendar store. - **/ -void -cal_component_get_last_modified (CalComponent *comp, struct icaltimetype **t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (t != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_icaltimetype (priv->last_modified, icalproperty_get_lastmodified, t); -} - -/** - * cal_component_set_last_modified: - * @comp: A calendar component object. - * @t: Value for the last time modified. - * - * Sets the time at which a calendar component object was last stored in the - * calendar store. This should not be called by plain calendar user agents. - **/ -void -cal_component_set_last_modified (CalComponent *comp, struct icaltimetype *t) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_icaltimetype (comp, &priv->last_modified, - icalproperty_new_lastmodified, - icalproperty_set_lastmodified, - t); -} - -/** - * cal_component_get_percent: - * @comp: A calendar component object. - * @percent: Return value for the percent-complete property. This should be - * freed using the cal_component_free_percent() function. - * - * Queries the percent-complete property of a calendar component object. - **/ -void -cal_component_get_percent (CalComponent *comp, int **percent) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (percent != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (priv->percent) { - *percent = g_new (int, 1); - **percent = icalproperty_get_percentcomplete (priv->percent); - } else - *percent = NULL; -} - -/** - * cal_component_set_percent: - * @comp: A calendar component object. - * @percent: Value for the percent-complete property. - * - * Sets the percent-complete property of a calendar component object. - **/ -void -cal_component_set_percent (CalComponent *comp, int *percent) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!percent) { - if (priv->percent) { - icalcomponent_remove_property (priv->icalcomp, priv->percent); - icalproperty_free (priv->percent); - priv->percent = NULL; - } - - return; - } - - g_return_if_fail (*percent >= 0 && *percent <= 100); - - if (priv->percent) - icalproperty_set_percentcomplete (priv->percent, *percent); - else { - priv->percent = icalproperty_new_percentcomplete (*percent); - icalcomponent_add_property (priv->icalcomp, priv->percent); - } -} - -/** - * cal_component_get_priority: - * @comp: A calendar component object. - * @priority: Return value for the priority property. This should be freed using - * the cal_component_free_priority() function. - * - * Queries the priority property of a calendar component object. - **/ -void -cal_component_get_priority (CalComponent *comp, int **priority) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (priority != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (priv->priority) { - *priority = g_new (int, 1); - **priority = icalproperty_get_priority (priv->priority); - } else - *priority = NULL; -} - -/** - * cal_component_set_priority: - * @comp: A calendar component object. - * @priority: Value for the priority property. - * - * Sets the priority property of a calendar component object. - **/ -void -cal_component_set_priority (CalComponent *comp, int *priority) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!priority) { - if (priv->priority) { - icalcomponent_remove_property (priv->icalcomp, priv->priority); - icalproperty_free (priv->priority); - priv->priority = NULL; - } - - return; - } - - g_return_if_fail (*priority >= 0 && *priority <= 9); - - if (priv->priority) - icalproperty_set_priority (priv->priority, *priority); - else { - priv->priority = icalproperty_new_priority (*priority); - icalcomponent_add_property (priv->icalcomp, priv->priority); - } -} - -/** - * cal_component_get_rdate_list: - * @comp: A calendar component object. - * @period_list: Return value for the list of recurrence dates, as a list of - * #CalComponentPeriod structures. This should be freed using the - * cal_component_free_period_list() function. - * - * Queries the list of recurrence date properties in a calendar component - * object. - **/ -void -cal_component_get_rdate_list (CalComponent *comp, GSList **period_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (period_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_period_list (priv->rdate_list, icalproperty_get_rdate, period_list); -} - -/** - * cal_component_set_rdate_list: - * @comp: A calendar component object. - * @period_list: List of #CalComponentPeriod structures. - * - * Sets the list of recurrence dates in a calendar component object. - **/ -void -cal_component_set_rdate_list (CalComponent *comp, GSList *period_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_period_list (comp, icalproperty_new_rdate, &priv->rdate_list, period_list); - - priv->need_sequence_inc = TRUE; -} - -/** - * cal_component_has_rdates: - * @comp: A calendar component object. - * - * Queries whether a calendar component object has any recurrence dates defined - * for it. - * - * Return value: TRUE if the component has recurrence dates, FALSE otherwise. - **/ -gboolean -cal_component_has_rdates (CalComponent *comp) -{ - CalComponentPrivate *priv; - - g_return_val_if_fail (comp != NULL, FALSE); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), FALSE); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, FALSE); - - return (priv->rdate_list != NULL); -} - -/** - * cal_component_get_rrule_list: - * @comp: A calendar component object. - * @recur_list: List of recurrence rules as struct #icalrecurrencetype - * structures. This should be freed using the cal_component_free_recur_list() - * function. - * - * Queries the list of recurrence rule properties of a calendar component - * object. - **/ -void -cal_component_get_rrule_list (CalComponent *comp, GSList **recur_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (recur_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - get_recur_list (priv->rrule_list, icalproperty_get_rrule, recur_list); -} - -/** - * cal_component_get_rrule_property_list: - * @comp: A calendar component object. - * @recur_list: Returns a list of recurrence rule properties. - * - * Queries a list of recurrence rule properties of a calendar component object. - **/ -void -cal_component_get_rrule_property_list (CalComponent *comp, GSList **recur_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (recur_list != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - *recur_list = priv->rrule_list; -} - -/** - * cal_component_set_rrule_list: - * @comp: A calendar component object. - * @recur_list: List of struct #icalrecurrencetype structures. - * - * Sets the list of recurrence rules in a calendar component object. - **/ -void -cal_component_set_rrule_list (CalComponent *comp, GSList *recur_list) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - set_recur_list (comp, icalproperty_new_rrule, &priv->rrule_list, recur_list); - - priv->need_sequence_inc = TRUE; -} - -/** - * cal_component_has_rrules: - * @comp: A calendar component object. - * - * Queries whether a calendar component object has any recurrence rules defined - * for it. - * - * Return value: TRUE if the component has recurrence rules, FALSE otherwise. - **/ -gboolean -cal_component_has_rrules (CalComponent *comp) -{ - CalComponentPrivate *priv; - - g_return_val_if_fail (comp != NULL, FALSE); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), FALSE); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, FALSE); - - return (priv->rrule_list != NULL); -} - -/** - * cal_component_has_recurrences: - * @comp: A calendar component object - * - * Queries whether a calendar component object has any recurrence dates or - * recurrence rules. - * - * Return value: TRUE if the component has recurrences, FALSE otherwise. - **/ -gboolean -cal_component_has_recurrences (CalComponent *comp) -{ - return cal_component_has_rdates (comp) || cal_component_has_rrules (comp); -} - -/** - * cal_component_get_sequence: - * @comp: A calendar component object. - * @sequence: Return value for the sequence number. This should be freed using - * cal_component_free_sequence(). - * - * Queries the sequence number of a calendar component object. - **/ -void -cal_component_get_sequence (CalComponent *comp, int **sequence) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (sequence != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!priv->sequence) { - *sequence = NULL; - return; - } - - *sequence = g_new (int, 1); - **sequence = icalproperty_get_sequence (priv->sequence); -} - -/** - * cal_component_set_sequence: - * @comp: A calendar component object. - * @sequence: Sequence number value. - * - * Sets the sequence number of a calendar component object. Normally this - * function should not be called, since the sequence number is incremented - * automatically at the proper times. - **/ -void -cal_component_set_sequence (CalComponent *comp, int *sequence) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - priv->need_sequence_inc = FALSE; - - if (!sequence) { - if (priv->sequence) { - icalcomponent_remove_property (priv->icalcomp, priv->sequence); - icalproperty_free (priv->sequence); - priv->sequence = NULL; - } - - return; - } - - if (priv->sequence) - icalproperty_set_sequence (priv->sequence, *sequence); - else { - priv->sequence = icalproperty_new_sequence (*sequence); - icalcomponent_add_property (priv->icalcomp, priv->sequence); - } -} - -/** - * cal_component_get_status: - * @comp: A calendar component object. - * @status: Return value for the status value. It is set to #ICAL_STATUS_NONE - * if the component has no status property. - * - * Queries the status property of a calendar component object. - **/ -void -cal_component_get_status (CalComponent *comp, icalproperty_status *status) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (status != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!priv->status) { - *status = ICAL_STATUS_NONE; - return; - } - - *status = icalproperty_get_status (priv->status); -} - -/** - * cal_component_set_status: - * @comp: A calendar component object. - * @status: Status value. You should use #ICAL_STATUS_NONE if you want to unset - * this property. - * - * Sets the status property of a calendar component object. - **/ -void -cal_component_set_status (CalComponent *comp, icalproperty_status status) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - priv->need_sequence_inc = TRUE; - - if (status == ICAL_STATUS_NONE) { - if (priv->status) { - icalcomponent_remove_property (priv->icalcomp, priv->status); - icalproperty_free (priv->status); - priv->status = NULL; - } - - return; - } - - if (priv->status) { - icalproperty_set_status (priv->status, status); - } else { - priv->status = icalproperty_new_status (status); - icalcomponent_add_property (priv->icalcomp, priv->status); - } -} - -/** - * cal_component_get_summary: - * @comp: A calendar component object. - * @summary: Return value for the summary property and its parameters. - * - * Queries the summary of a calendar component object. - **/ -void -cal_component_get_summary (CalComponent *comp, CalComponentText *summary) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (summary != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (priv->summary.prop) - summary->value = icalproperty_get_summary (priv->summary.prop); - else - summary->value = NULL; - - if (priv->summary.altrep_param) - summary->altrep = icalparameter_get_altrep (priv->summary.altrep_param); - else - summary->altrep = NULL; -} - -/** - * cal_component_set_summary: - * @comp: A calendar component object. - * @summary: Summary property and its parameters. - * - * Sets the summary of a calendar component object. - **/ -void -cal_component_set_summary (CalComponent *comp, CalComponentText *summary) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!summary) { - if (priv->summary.prop) { - icalcomponent_remove_property (priv->icalcomp, priv->summary.prop); - icalproperty_free (priv->summary.prop); - - priv->summary.prop = NULL; - priv->summary.altrep_param = NULL; - } - - return; - } - - g_return_if_fail (summary->value != NULL); - - if (priv->summary.prop) - icalproperty_set_summary (priv->summary.prop, (char *) summary->value); - else { - priv->summary.prop = icalproperty_new_summary ((char *) summary->value); - icalcomponent_add_property (priv->icalcomp, priv->summary.prop); - } - - if (summary->altrep) { - g_assert (priv->summary.prop != NULL); - - if (priv->summary.altrep_param) - icalparameter_set_altrep (priv->summary.altrep_param, - (char *) summary->altrep); - else { - priv->summary.altrep_param = icalparameter_new_altrep ( - (char *) summary->altrep); - icalproperty_add_parameter (priv->summary.prop, - priv->summary.altrep_param); - } - } else if (priv->summary.altrep_param) { - icalproperty_remove_parameter (priv->summary.prop, ICAL_ALTREP_PARAMETER); - icalparameter_free (priv->summary.altrep_param); - priv->summary.altrep_param = NULL; - } -} - -/** - * cal_component_get_transparency: - * @comp: A calendar component object. - * @transp: Return value for the time transparency. - * - * Queries the time transparency of a calendar component object. - **/ -void -cal_component_get_transparency (CalComponent *comp, CalComponentTransparency *transp) -{ - CalComponentPrivate *priv; - const char *val; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (transp != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!priv->transparency) { - *transp = CAL_COMPONENT_TRANSP_NONE; - return; - } - - val = icalproperty_get_transp (priv->transparency); - - if (strcasecmp (val, "TRANSPARENT") == 0) - *transp = CAL_COMPONENT_TRANSP_TRANSPARENT; - else if (strcasecmp (val, "OPAQUE") == 0) - *transp = CAL_COMPONENT_TRANSP_OPAQUE; - else - *transp = CAL_COMPONENT_TRANSP_UNKNOWN; -} - -/** - * cal_component_set_transparency: - * @comp: A calendar component object. - * @transp: Time transparency value. - * - * Sets the time transparency of a calendar component object. - **/ -void -cal_component_set_transparency (CalComponent *comp, CalComponentTransparency transp) -{ - CalComponentPrivate *priv; - char *str; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (transp != CAL_COMPONENT_TRANSP_UNKNOWN); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - - if (transp == CAL_COMPONENT_TRANSP_NONE) { - if (priv->transparency) { - icalcomponent_remove_property (priv->icalcomp, priv->transparency); - icalproperty_free (priv->transparency); - priv->transparency = NULL; - } - - return; - } - - switch (transp) { - case CAL_COMPONENT_TRANSP_TRANSPARENT: - str = "TRANSPARENT"; - break; - - case CAL_COMPONENT_TRANSP_OPAQUE: - str = "OPAQUE"; - break; - - default: - g_assert_not_reached (); - str = NULL; - } - - if (priv->transparency) - icalproperty_set_transp (priv->transparency, str); - else { - priv->transparency = icalproperty_new_transp (str); - icalcomponent_add_property (priv->icalcomp, priv->transparency); - } -} - -/** - * cal_component_get_url: - * @comp: A calendar component object. - * @url: Return value for the URL. - * - * Queries the uniform resource locator property of a calendar component object. - **/ -void -cal_component_get_url (CalComponent *comp, const char **url) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (url != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (priv->url) - *url = icalproperty_get_url (priv->url); - else - *url = NULL; -} - -/** - * cal_component_set_url: - * @comp: A calendar component object. - * @url: URL value. - * - * Sets the uniform resource locator property of a calendar component object. - **/ -void -cal_component_set_url (CalComponent *comp, const char *url) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - if (!url || !(*url)) { - if (priv->url) { - icalcomponent_remove_property (priv->icalcomp, priv->url); - icalproperty_free (priv->url); - priv->url = NULL; - } - - return; - } - - if (priv->url) - icalproperty_set_url (priv->url, (char *) url); - else { - priv->url = icalproperty_new_url ((char *) url); - icalcomponent_add_property (priv->icalcomp, priv->url); - } -} - - - -/** - * cal_component_free_categories_list: - * @categ_list: List of category strings. - * - * Frees a list of category strings. - **/ -void -cal_component_free_categories_list (GSList *categ_list) -{ - GSList *l; - - for (l = categ_list; l; l = l->next) - g_free (l->data); - - g_slist_free (categ_list); -} - -/** - * cal_component_free_datetime: - * @dt: A date/time structure. - * - * Frees a date/time structure. - **/ -void -cal_component_free_datetime (CalComponentDateTime *dt) -{ - g_return_if_fail (dt != NULL); - - if (dt->value) - g_free (dt->value); -} - -/** - * cal_component_free_exdate_list: - * @exdate_list: List of #CalComponentDateTime structures. - * - * Frees a list of #CalComponentDateTime structures as returned by the - * cal_component_get_exdate_list() function. - **/ -void -cal_component_free_exdate_list (GSList *exdate_list) -{ - GSList *l; - - for (l = exdate_list; l; l = l->next) { - CalComponentDateTime *cdt; - - g_assert (l->data != NULL); - cdt = l->data; - - g_assert (cdt->value != NULL); - g_free (cdt->value); - - g_free (cdt); - } - - g_slist_free (exdate_list); -} - -/** - * cal_component_free_geo: - * @geo: An #icalgeotype structure. - * - * Frees a struct #icalgeotype structure as returned by the calendar component - * functions. - **/ -void -cal_component_free_geo (struct icalgeotype *geo) -{ - g_return_if_fail (geo != NULL); - - g_free (geo); -} - -/** - * cal_component_free_icaltimetype: - * @t: An #icaltimetype structure. - * - * Frees a struct #icaltimetype value as returned by the calendar component - * functions. - **/ -void -cal_component_free_icaltimetype (struct icaltimetype *t) -{ - g_return_if_fail (t != NULL); - - g_free (t); -} - -/** - * cal_component_free_percent: - * @percent: Percent value. - * - * Frees a percent value as returned by the cal_component_get_percent() - * function. - **/ -void -cal_component_free_percent (int *percent) -{ - g_return_if_fail (percent != NULL); - - g_free (percent); -} - -/** - * cal_component_free_priority: - * @priority: Priority value. - * - * Frees a priority value as returned by the cal_component_get_priority() - * function. - **/ -void -cal_component_free_priority (int *priority) -{ - g_return_if_fail (priority != NULL); - - g_free (priority); -} - -/** - * cal_component_free_period_list: - * @period_list: List of #CalComponentPeriod structures. - * - * Frees a list of #CalComponentPeriod structures. - **/ -void -cal_component_free_period_list (GSList *period_list) -{ - GSList *l; - - for (l = period_list; l; l = l->next) { - CalComponentPeriod *period; - - g_assert (l->data != NULL); - - period = l->data; - g_free (period); - } - - g_slist_free (period_list); -} - -/** - * cal_component_free_recur_list: - * @recur_list: List of struct #icalrecurrencetype structures. - * - * Frees a list of struct #icalrecurrencetype structures. - **/ -void -cal_component_free_recur_list (GSList *recur_list) -{ - GSList *l; - - for (l = recur_list; l; l = l->next) { - struct icalrecurrencetype *r; - - g_assert (l->data != NULL); - r = l->data; - - g_free (r); - } - - g_slist_free (recur_list); -} - -/** - * cal_component_free_sequence: - * @sequence: Sequence number value. - * - * Frees a sequence number value. - **/ -void -cal_component_free_sequence (int *sequence) -{ - g_return_if_fail (sequence != NULL); - - g_free (sequence); -} - -/** - * cal_component_free_text_list: - * @text_list: List of #CalComponentText structures. - * - * Frees a list of #CalComponentText structures. This function should only be - * used to free lists of text values as returned by the other getter functions - * of #CalComponent. - **/ -void -cal_component_free_text_list (GSList *text_list) -{ - GSList *l; - - for (l = text_list; l; l = l->next) { - CalComponentText *text; - - g_assert (l->data != NULL); - - text = l->data; - g_return_if_fail (text != NULL); - g_free (text); - } - - g_slist_free (text_list); -} - - - -/** - * cal_component_has_alarms: - * @comp: A calendar component object. - * - * Checks whether the component has any alarms. - * - * Return value: TRUE if the component has any alarms. - **/ -gboolean -cal_component_has_alarms (CalComponent *comp) -{ - CalComponentPrivate *priv; - - g_return_val_if_fail (comp != NULL, FALSE); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), FALSE); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, FALSE); - - return g_hash_table_size (priv->alarm_uid_hash) != 0; -} - -/** - * cal_component_add_alarm: - * @comp: A calendar component. - * @alarm: An alarm. - * - * Adds an alarm subcomponent to a calendar component. You should have created - * the @alarm by using cal_component_alarm_new(); it is invalid to use a - * #CalComponentAlarm structure that came from cal_component_get_alarm(). After - * adding the alarm, the @alarm structure is no longer valid because the - * internal structures may change and you should get rid of it by using - * cal_component_alarm_free(). - **/ -void -cal_component_add_alarm (CalComponent *comp, CalComponentAlarm *alarm) -{ - CalComponentPrivate *priv; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (alarm != NULL); - - priv = comp->priv; - - add_alarm (comp, alarm->icalcomp, icalproperty_get_x (alarm->uid)); - icalcomponent_add_component (priv->icalcomp, alarm->icalcomp); -} - -/** - * cal_component_remove_alarm: - * @comp: A calendar component. - * @auid: UID of the alarm to remove. - * - * Removes an alarm subcomponent from a calendar component. If the alarm that - * corresponds to the specified @auid had been fetched with - * cal_component_get_alarm(), then those alarm structures will be invalid; you - * should get rid of them with cal_component_alarm_free() before using this - * function. - **/ -void -cal_component_remove_alarm (CalComponent *comp, const char *auid) -{ - CalComponentPrivate *priv; - icalcomponent *alarm; - - g_return_if_fail (comp != NULL); - g_return_if_fail (IS_CAL_COMPONENT (comp)); - g_return_if_fail (auid != NULL); - - priv = comp->priv; - g_return_if_fail (priv->icalcomp != NULL); - - alarm = g_hash_table_lookup (priv->alarm_uid_hash, auid); - if (!alarm) - return; - - g_hash_table_remove (priv->alarm_uid_hash, auid); - icalcomponent_remove_component (priv->icalcomp, alarm); - icalcomponent_free (alarm); -} - - -/* Scans an icalproperty from a calendar component and adds its mapping to our - * own alarm structure. - */ -static void -scan_alarm_property (CalComponentAlarm *alarm, icalproperty *prop) -{ - icalproperty_kind kind; - const char *xname; - - kind = icalproperty_isa (prop); - - switch (kind) { - case ICAL_ACTION_PROPERTY: - alarm->action = prop; - break; - - case ICAL_TRIGGER_PROPERTY: - alarm->trigger = prop; - break; - - case ICAL_X_PROPERTY: - xname = icalproperty_get_x_name (prop); - g_assert (xname != NULL); - - if (strcmp (xname, EVOLUTION_ALARM_UID_PROPERTY) == 0) - alarm->uid = prop; - - break; - - default: - break; - } -} - -/* Creates a CalComponentAlarm from a libical alarm subcomponent */ -static CalComponentAlarm * -make_alarm (icalcomponent *subcomp) -{ - CalComponentAlarm *alarm; - icalproperty *prop; - - alarm = g_new (CalComponentAlarm, 1); - - alarm->icalcomp = subcomp; - alarm->uid = NULL; - - for (prop = icalcomponent_get_first_property (subcomp, ICAL_ANY_PROPERTY); - prop; - prop = icalcomponent_get_next_property (subcomp, ICAL_ANY_PROPERTY)) - scan_alarm_property (alarm, prop); - - g_assert (alarm->uid != NULL); - - return alarm; -} - -/* Used from g_hash_table_foreach(); adds an alarm UID to a list */ -static void -add_alarm_uid (gpointer key, gpointer value, gpointer data) -{ - const char *auid; - GList **l; - - auid = key; - l = data; - - *l = g_list_prepend (*l, g_strdup (auid)); -} - -/** - * cal_component_get_alarm_uids: - * @comp: A calendar component. - * - * Builds a list of the unique identifiers of the alarm subcomponents inside a - * calendar component. - * - * Return value: List of unique identifiers for alarms. This should be freed - * using cal_obj_uid_list_free(). - **/ -GList * -cal_component_get_alarm_uids (CalComponent *comp) -{ - CalComponentPrivate *priv; - GList *l; - - g_return_val_if_fail (comp != NULL, NULL); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), NULL); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, NULL); - - l = NULL; - g_hash_table_foreach (priv->alarm_uid_hash, add_alarm_uid, &l); - - return l; -} - -/** - * cal_component_get_alarm: - * @comp: A calendar component. - * @auid: Unique identifier for the sought alarm subcomponent. - * - * Queries a particular alarm subcomponent of a calendar component. - * - * Return value: The alarm subcomponent that corresponds to the specified @auid, - * or #NULL if no alarm exists with that UID. This should be freed using - * cal_component_alarm_free(). - **/ -CalComponentAlarm * -cal_component_get_alarm (CalComponent *comp, const char *auid) -{ - CalComponentPrivate *priv; - icalcomponent *alarm; - - g_return_val_if_fail (comp != NULL, NULL); - g_return_val_if_fail (IS_CAL_COMPONENT (comp), NULL); - - priv = comp->priv; - g_return_val_if_fail (priv->icalcomp != NULL, NULL); - - g_return_val_if_fail (auid != NULL, NULL); - - alarm = g_hash_table_lookup (priv->alarm_uid_hash, auid); - - if (alarm) - return make_alarm (alarm); - else - return NULL; -} - -/** - * cal_component_alarms_free: - * @alarms: Component alarms structure. - * - * Frees a #CalComponentAlarms structure. - **/ -void -cal_component_alarms_free (CalComponentAlarms *alarms) -{ - GSList *l; - - g_return_if_fail (alarms != NULL); - - g_assert (alarms->comp != NULL); - gtk_object_unref (GTK_OBJECT (alarms->comp)); - - for (l = alarms->alarms; l; l = l->next) { - CalAlarmInstance *instance; - - instance = l->data; - g_assert (instance != NULL); - g_free (instance); - } - - g_slist_free (alarms->alarms); - g_free (alarms); -} - -/** - * cal_component_alarm_new: - * - * - * - * Return value: a new alarm component - **/ -CalComponentAlarm * -cal_component_alarm_new (void) -{ - CalComponentAlarm *alarm; - char *new_auid ; - - alarm = g_new (CalComponentAlarm, 1); - - alarm->icalcomp = icalcomponent_new (ICAL_VALARM_COMPONENT); - - new_auid = cal_component_gen_uid (); - alarm->uid = icalproperty_new_x (new_auid); - icalproperty_set_x_name (alarm->uid, EVOLUTION_ALARM_UID_PROPERTY); - icalcomponent_add_property (alarm->icalcomp, alarm->uid); - g_free (new_auid); - - alarm->action = NULL; - alarm->trigger = NULL; - - return alarm; -} - -/** - * cal_component_alarm_clone: - * @alarm: An alarm subcomponent. - * - * Creates a new alarm subcomponent by copying the information from another one. - * - * Return value: A newly-created alarm subcomponent with the same values as the - * original one. Should be freed with cal_component_alarm_free(). - **/ -CalComponentAlarm * -cal_component_alarm_clone (CalComponentAlarm *alarm) -{ - icalcomponent *icalcomp; - - g_return_val_if_fail (alarm != NULL, NULL); - - icalcomp = icalcomponent_new_clone (alarm->icalcomp); - return make_alarm (icalcomp); -} - -/** - * cal_component_alarm_get_uid: - * @alarm: An alarm subcomponent. - * - * Queries the unique identifier of an alarm subcomponent. - * - * Return value: UID of the alarm. - **/ -const char * -cal_component_alarm_get_uid (CalComponentAlarm *alarm) -{ - g_return_val_if_fail (alarm != NULL, NULL); - - return alarm_uid_from_prop (alarm->uid); -} - -/** - * cal_component_alarm_get_action: - * @alarm: An alarm. - * @action: Return value for the alarm's action type. - * - * Queries the action type of an alarm. - **/ -void -cal_component_alarm_get_action (CalComponentAlarm *alarm, CalAlarmAction *action) -{ - enum icalproperty_action ipa; - - g_return_if_fail (alarm != NULL); - g_return_if_fail (action != NULL); - - g_assert (alarm->icalcomp != NULL); - - if (!alarm->action) { - *action = CAL_ALARM_NONE; - return; - } - - ipa = icalproperty_get_action (alarm->action); - - switch (ipa) { - case ICAL_ACTION_AUDIO: - *action = CAL_ALARM_AUDIO; - break; - - case ICAL_ACTION_DISPLAY: - *action = CAL_ALARM_DISPLAY; - break; - - case ICAL_ACTION_EMAIL: - *action = CAL_ALARM_EMAIL; - break; - - case ICAL_ACTION_PROCEDURE: - *action = CAL_ALARM_PROCEDURE; - break; - - case ICAL_ACTION_NONE: - *action = CAL_ALARM_NONE; - break; - - default: - *action = CAL_ALARM_UNKNOWN; - } -} - -/** - * cal_component_alarm_set_action: - * @alarm: An alarm. - * @action: Action type. - * - * Sets the action type for an alarm. - **/ -void -cal_component_alarm_set_action (CalComponentAlarm *alarm, CalAlarmAction action) -{ - enum icalproperty_action ipa; - - g_return_if_fail (alarm != NULL); - g_return_if_fail (action != CAL_ALARM_NONE); - g_return_if_fail (action != CAL_ALARM_UNKNOWN); - - g_assert (alarm->icalcomp != NULL); - - switch (action) { - case CAL_ALARM_AUDIO: - ipa = ICAL_ACTION_AUDIO; - break; - - case CAL_ALARM_DISPLAY: - ipa = ICAL_ACTION_DISPLAY; - break; - - case CAL_ALARM_EMAIL: - ipa = ICAL_ACTION_EMAIL; - break; - - case CAL_ALARM_PROCEDURE: - ipa = ICAL_ACTION_PROCEDURE; - break; - - default: - g_assert_not_reached (); - ipa = ICAL_ACTION_NONE; - } - - if (alarm->action) - icalproperty_set_action (alarm->action, ipa); - else { - alarm->action = icalproperty_new_action (ipa); - icalcomponent_add_property (alarm->icalcomp, alarm->action); - } -} - -/** - * cal_component_alarm_get_trigger: - * @alarm: An alarm. - * @trigger: Return value for the trigger time. - * - * Queries the trigger time for an alarm. - **/ -void -cal_component_alarm_get_trigger (CalComponentAlarm *alarm, CalAlarmTrigger *trigger) -{ - icalparameter *param; - struct icaltriggertype t; - gboolean relative; - - g_return_if_fail (alarm != NULL); - g_return_if_fail (trigger != NULL); - - g_assert (alarm->icalcomp != NULL); - - if (!alarm->trigger) { - trigger->type = CAL_ALARM_TRIGGER_NONE; - return; - } - - /* Get trigger type */ - - param = icalproperty_get_first_parameter (alarm->trigger, ICAL_VALUE_PARAMETER); - if (param) { - icalparameter_value value; - - value = icalparameter_get_value (param); - - switch (value) { - case ICAL_VALUE_DURATION: - relative = TRUE; - break; - - case ICAL_VALUE_DATETIME: - relative = FALSE; - break; - - default: - g_message ("cal_component_alarm_get_trigger(): Unknown value for trigger " - "value %d; using RELATIVE", value); - - relative = TRUE; - break; - } - } else - relative = TRUE; - - /* Get trigger value and the RELATED parameter */ - - t = icalproperty_get_trigger (alarm->trigger); - - if (relative) { - trigger->u.rel_duration = t.duration; - - param = icalproperty_get_first_parameter (alarm->trigger, ICAL_RELATED_PARAMETER); - if (param) { - icalparameter_related rel; - - rel = icalparameter_get_related (param); - - switch (rel) { - case ICAL_RELATED_START: - trigger->type = CAL_ALARM_TRIGGER_RELATIVE_START; - break; - - case ICAL_RELATED_END: - trigger->type = CAL_ALARM_TRIGGER_RELATIVE_END; - break; - - default: - g_assert_not_reached (); - } - } else - trigger->type = CAL_ALARM_TRIGGER_RELATIVE_START; - } else { - trigger->u.abs_time = t.time; - trigger->type = CAL_ALARM_TRIGGER_ABSOLUTE; - } -} - -/** - * cal_component_alarm_set_trigger: - * @alarm: An alarm. - * @trigger: Trigger time structure. - * - * Sets the trigger time of an alarm. - **/ -void -cal_component_alarm_set_trigger (CalComponentAlarm *alarm, CalAlarmTrigger trigger) -{ - struct icaltriggertype t; - icalparameter *param; - icalparameter_value value_type; - icalparameter_related related; - - g_return_if_fail (alarm != NULL); - g_return_if_fail (trigger.type != CAL_ALARM_TRIGGER_NONE); - - g_assert (alarm->icalcomp != NULL); - - /* Delete old trigger */ - - if (alarm->trigger) { - icalcomponent_remove_property (alarm->icalcomp, alarm->trigger); - icalproperty_free (alarm->trigger); - alarm->trigger = NULL; - } - - /* Set the value */ - - related = ICAL_RELATED_START; /* Keep GCC happy */ - - t.time = icaltime_null_time (); - t.duration = icaldurationtype_null_duration (); - switch (trigger.type) { - case CAL_ALARM_TRIGGER_RELATIVE_START: - t.duration = trigger.u.rel_duration; - t.time.is_date = -1; - value_type = ICAL_VALUE_DURATION; - related = ICAL_RELATED_START; - break; - - case CAL_ALARM_TRIGGER_RELATIVE_END: - t.duration = trigger.u.rel_duration; - t.time.is_date = -1; - value_type = ICAL_VALUE_DURATION; - related = ICAL_RELATED_END; - break; - - case CAL_ALARM_TRIGGER_ABSOLUTE: - t.time = trigger.u.abs_time; - value_type = ICAL_VALUE_DATETIME; - break; - - default: - g_assert_not_reached (); - return; - } - - alarm->trigger = icalproperty_new_trigger (t); - icalcomponent_add_property (alarm->icalcomp, alarm->trigger); - - /* Value parameters */ - - param = icalproperty_get_first_parameter (alarm->trigger, ICAL_VALUE_PARAMETER); - if (param) - icalparameter_set_value (param, value_type); - else { - param = icalparameter_new_value (value_type); - icalproperty_add_parameter (alarm->trigger, param); - } - - /* Related parameter */ - - if (trigger.type != CAL_ALARM_TRIGGER_ABSOLUTE) { - param = icalproperty_get_first_parameter (alarm->trigger, ICAL_RELATED_PARAMETER); - - if (param) - icalparameter_set_related (param, related); - else { - param = icalparameter_new_related (related); - icalproperty_add_parameter (alarm->trigger, param); - } - } -} - -/** - * cal_component_alarm_free: - * @alarm: A calendar alarm. - * - * Frees an alarm structure. - **/ -void -cal_component_alarm_free (CalComponentAlarm *alarm) -{ - g_return_if_fail (alarm != NULL); - - g_assert (alarm->icalcomp != NULL); - - if (icalcomponent_get_parent (alarm->icalcomp) == NULL) - icalcomponent_free (alarm->icalcomp); - - alarm->icalcomp = NULL; - alarm->uid = NULL; - alarm->action = NULL; - alarm->trigger = NULL; - - g_free (alarm); -} - - -/* Returns TRUE if both strings match, i.e. they are both NULL or the - strings are equal. */ -static gboolean -cal_component_strings_match (const gchar *string1, - const gchar *string2) -{ - if (string1 == NULL || string2 == NULL) - return (string1 == string2) ? TRUE : FALSE; - - if (!strcmp (string1, string2)) - return TRUE; - - return FALSE; -} - - -/** - * cal_component_event_dates_match: - * @comp1: A calendar component object. - * @comp2: A calendar component object. - * - * Checks if the DTSTART and DTEND properties of the 2 components match. - * Note that the events may have different recurrence properties which are not - * taken into account here. - * - * Returns: TRUE if the DTSTART and DTEND properties of the 2 components match. - **/ -gboolean -cal_component_event_dates_match (CalComponent *comp1, - CalComponent *comp2) -{ - CalComponentDateTime comp1_dtstart, comp1_dtend; - CalComponentDateTime comp2_dtstart, comp2_dtend; - - cal_component_get_dtstart (comp1, &comp1_dtstart); - cal_component_get_dtend (comp1, &comp1_dtend); - cal_component_get_dtstart (comp2, &comp2_dtstart); - cal_component_get_dtend (comp2, &comp2_dtend); - - /* If either value is NULL they must both be NULL to match. */ - if (comp1_dtstart.value == NULL || comp2_dtstart.value == NULL) { - if (comp1_dtstart.value != comp2_dtstart.value) - return FALSE; - } else { - if (icaltime_compare (*comp1_dtstart.value, - *comp2_dtstart.value)) - return FALSE; - } - - if (comp1_dtend.value == NULL || comp2_dtend.value == NULL) { - if (comp1_dtend.value != comp2_dtend.value) - return FALSE; - } else { - if (icaltime_compare (*comp1_dtend.value, - *comp2_dtend.value)) - return FALSE; - } - - /* Now check the timezones. */ - if (!cal_component_strings_match (comp1_dtstart.tzid, - comp2_dtstart.tzid)) - return FALSE; - - if (!cal_component_strings_match (comp1_dtend.tzid, - comp2_dtend.tzid)) - return FALSE; - - return TRUE; -} - - - diff --git a/calendar/cal-util/cal-component.h b/calendar/cal-util/cal-component.h deleted file mode 100644 index e4a2e2421e..0000000000 --- a/calendar/cal-util/cal-component.h +++ /dev/null @@ -1,361 +0,0 @@ -/* Evolution calendar - iCalendar component object - * - * Copyright (C) 2000 Helix Code, Inc. - * Copyright (C) 2000 Ximian, Inc. - * - * Author: Federico Mena-Quintero <federico@ximian.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -#ifndef CAL_COMPONENT_H -#define CAL_COMPONENT_H - -#include <libgnome/gnome-defs.h> -#include <time.h> -#include <gtk/gtkobject.h> -#include <ical.h> - -BEGIN_GNOME_DECLS - - - -#define CAL_COMPONENT_TYPE (cal_component_get_type ()) -#define CAL_COMPONENT(obj) (GTK_CHECK_CAST ((obj), CAL_COMPONENT_TYPE, CalComponent)) -#define CAL_COMPONENT_CLASS(klass) (GTK_CHECK_CLASS_CAST ((klass), CAL_COMPONENT_TYPE, \ - CalComponentClass)) -#define IS_CAL_COMPONENT(obj) (GTK_CHECK_TYPE ((obj), CAL_COMPONENT_TYPE)) -#define IS_CAL_COMPONENT_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), CAL_COMPONENT_TYPE)) - -/* Types of calendar components to be stored by a CalComponent, as per RFC 2445. - * We don't put the alarm component type here since we store alarms as separate - * structures inside the other "real" components. - */ -typedef enum { - CAL_COMPONENT_NO_TYPE, - CAL_COMPONENT_EVENT, - CAL_COMPONENT_TODO, - CAL_COMPONENT_JOURNAL, - CAL_COMPONENT_FREEBUSY, - CAL_COMPONENT_TIMEZONE -} CalComponentVType; - -/* Field identifiers for a calendar component; these are used by the data model - * for ETable. - * - * NOTE: These are also used in the ETable specification, and the column - * numbers are saved in the user settings file. So don't reorder them! - */ -typedef enum { - CAL_COMPONENT_FIELD_CATEGORIES, /* concatenation of the categories list */ - CAL_COMPONENT_FIELD_CLASSIFICATION, - CAL_COMPONENT_FIELD_COMPLETED, - CAL_COMPONENT_FIELD_DTEND, - CAL_COMPONENT_FIELD_DTSTART, - CAL_COMPONENT_FIELD_DUE, - CAL_COMPONENT_FIELD_GEO, - CAL_COMPONENT_FIELD_PERCENT, - CAL_COMPONENT_FIELD_PRIORITY, - CAL_COMPONENT_FIELD_SUMMARY, - CAL_COMPONENT_FIELD_TRANSPARENCY, - CAL_COMPONENT_FIELD_URL, - CAL_COMPONENT_FIELD_HAS_ALARMS, /* not a real field */ - CAL_COMPONENT_FIELD_ICON, /* not a real field */ - CAL_COMPONENT_FIELD_COMPLETE, /* not a real field */ - CAL_COMPONENT_FIELD_RECURRING, /* not a real field */ - CAL_COMPONENT_FIELD_OVERDUE, /* not a real field */ - CAL_COMPONENT_FIELD_COLOR, /* not a real field */ - CAL_COMPONENT_FIELD_STATUS, - CAL_COMPONENT_FIELD_NUM_FIELDS -} CalComponentField; - -/* Structures and enumerations to return properties and their parameters */ - -/* CLASSIFICATION property */ -typedef enum { - CAL_COMPONENT_CLASS_NONE, - CAL_COMPONENT_CLASS_PUBLIC, - CAL_COMPONENT_CLASS_PRIVATE, - CAL_COMPONENT_CLASS_CONFIDENTIAL, - CAL_COMPONENT_CLASS_UNKNOWN -} CalComponentClassification; - -/* Properties that have time and timezone information */ -typedef struct { - /* Actual date/time value */ - struct icaltimetype *value; - - /* Timezone ID */ - const char *tzid; -} CalComponentDateTime; - -/* Way in which a period of time is specified */ -typedef enum { - CAL_COMPONENT_PERIOD_DATETIME, - CAL_COMPONENT_PERIOD_DURATION -} CalComponentPeriodType; - -/* Period of time, can have explicit start/end times or start/duration instead */ -typedef struct { - CalComponentPeriodType type; - - struct icaltimetype start; - - union { - struct icaltimetype end; - struct icaldurationtype duration; - } u; -} CalComponentPeriod; - -/* Text properties */ -typedef struct { - /* Description string */ - const char *value; - - /* Alternate representation URI */ - const char *altrep; -} CalComponentText; - -/* Time transparency */ -typedef enum { - CAL_COMPONENT_TRANSP_NONE, - CAL_COMPONENT_TRANSP_TRANSPARENT, - CAL_COMPONENT_TRANSP_OPAQUE, - CAL_COMPONENT_TRANSP_UNKNOWN -} CalComponentTransparency; - -/* Main calendar component object */ - -typedef struct _CalComponent CalComponent; -typedef struct _CalComponentClass CalComponentClass; - -typedef struct _CalComponentPrivate CalComponentPrivate; - -struct _CalComponent { - GtkObject object; - - /* Private data */ - CalComponentPrivate *priv; -}; - -struct _CalComponentClass { - GtkObjectClass parent_class; -}; - -/* Calendar component */ - -GtkType cal_component_get_type (void); - -char *cal_component_gen_uid (void); - -CalComponent *cal_component_new (void); - -CalComponent *cal_component_clone (CalComponent *comp); - -void cal_component_set_new_vtype (CalComponent *comp, CalComponentVType type); - -gboolean cal_component_set_icalcomponent (CalComponent *comp, icalcomponent *icalcomp); -icalcomponent *cal_component_get_icalcomponent (CalComponent *comp); - -CalComponentVType cal_component_get_vtype (CalComponent *comp); - -char *cal_component_get_as_string (CalComponent *comp); - -void cal_component_commit_sequence (CalComponent *comp); - -void cal_component_get_uid (CalComponent *comp, const char **uid); -void cal_component_set_uid (CalComponent *comp, const char *uid); - -void cal_component_get_categories (CalComponent *comp, const char **categories); -void cal_component_set_categories (CalComponent *comp, const char *categories); -void cal_component_get_categories_list (CalComponent *comp, GSList **categ_list); -void cal_component_set_categories_list (CalComponent *comp, GSList *categ_list); - -void cal_component_get_classification (CalComponent *comp, CalComponentClassification *classif); -void cal_component_set_classification (CalComponent *comp, CalComponentClassification classif); - -void cal_component_get_comment_list (CalComponent *comp, GSList **text_list); -void cal_component_set_comment_list (CalComponent *comp, GSList *text_list); - -void cal_component_get_completed (CalComponent *comp, struct icaltimetype **t); -void cal_component_set_completed (CalComponent *comp, struct icaltimetype *t); - -void cal_component_get_created (CalComponent *comp, struct icaltimetype **t); -void cal_component_set_created (CalComponent *comp, struct icaltimetype *t); - -void cal_component_get_description_list (CalComponent *comp, GSList **text_list); -void cal_component_set_description_list (CalComponent *comp, GSList *text_list); - -void cal_component_get_dtend (CalComponent *comp, CalComponentDateTime *dt); -void cal_component_set_dtend (CalComponent *comp, CalComponentDateTime *dt); - -void cal_component_get_dtstamp (CalComponent *comp, struct icaltimetype *t); -void cal_component_set_dtstamp (CalComponent *comp, struct icaltimetype *t); - -void cal_component_get_dtstart (CalComponent *comp, CalComponentDateTime *dt); -void cal_component_set_dtstart (CalComponent *comp, CalComponentDateTime *dt); - -void cal_component_get_due (CalComponent *comp, CalComponentDateTime *dt); -void cal_component_set_due (CalComponent *comp, CalComponentDateTime *dt); - -void cal_component_get_exdate_list (CalComponent *comp, GSList **exdate_list); -void cal_component_set_exdate_list (CalComponent *comp, GSList *exdate_list); -gboolean cal_component_has_exdates (CalComponent *comp); - -void cal_component_get_exrule_list (CalComponent *comp, GSList **recur_list); -void cal_component_get_exrule_property_list (CalComponent *comp, GSList **recur_list); -void cal_component_set_exrule_list (CalComponent *comp, GSList *recur_list); -gboolean cal_component_has_exrules (CalComponent *comp); - -gboolean cal_component_has_exceptions (CalComponent *comp); - -void cal_component_get_geo (CalComponent *comp, struct icalgeotype **geo); -void cal_component_set_geo (CalComponent *comp, struct icalgeotype *geo); - -void cal_component_get_last_modified (CalComponent *comp, struct icaltimetype **t); -void cal_component_set_last_modified (CalComponent *comp, struct icaltimetype *t); - -void cal_component_get_percent (CalComponent *comp, int **percent); -void cal_component_set_percent (CalComponent *comp, int *percent); - -void cal_component_get_priority (CalComponent *comp, int **priority); -void cal_component_set_priority (CalComponent *comp, int *priority); - -void cal_component_get_rdate_list (CalComponent *comp, GSList **period_list); -void cal_component_set_rdate_list (CalComponent *comp, GSList *period_list); -gboolean cal_component_has_rdates (CalComponent *comp); - -void cal_component_get_rrule_list (CalComponent *comp, GSList **recur_list); -void cal_component_get_rrule_property_list (CalComponent *comp, GSList **recur_list); -void cal_component_set_rrule_list (CalComponent *comp, GSList *recur_list); -gboolean cal_component_has_rrules (CalComponent *comp); - -gboolean cal_component_has_recurrences (CalComponent *comp); - -void cal_component_get_sequence (CalComponent *comp, int **sequence); -void cal_component_set_sequence (CalComponent *comp, int *sequence); - -void cal_component_get_status (CalComponent *comp, icalproperty_status *status); -void cal_component_set_status (CalComponent *comp, icalproperty_status status); - -void cal_component_get_summary (CalComponent *comp, CalComponentText *summary); -void cal_component_set_summary (CalComponent *comp, CalComponentText *summary); - -void cal_component_get_transparency (CalComponent *comp, CalComponentTransparency *transp); -void cal_component_set_transparency (CalComponent *comp, CalComponentTransparency transp); - -void cal_component_get_url (CalComponent *comp, const char **url); -void cal_component_set_url (CalComponent *comp, const char *url); - -gboolean cal_component_event_dates_match (CalComponent *comp1, CalComponent *comp2); - -/* Functions to free returned values */ - -void cal_component_free_categories_list (GSList *categ_list); -void cal_component_free_datetime (CalComponentDateTime *dt); -void cal_component_free_exdate_list (GSList *exdate_list); -void cal_component_free_geo (struct icalgeotype *geo); -void cal_component_free_icaltimetype (struct icaltimetype *t); -void cal_component_free_percent (int *percent); -void cal_component_free_priority (int *priority); -void cal_component_free_period_list (GSList *period_list); -void cal_component_free_recur_list (GSList *recur_list); -void cal_component_free_sequence (int *sequence); -void cal_component_free_text_list (GSList *text_list); - -/* Alarms */ - -/* Opaque structure used to represent alarm subcomponents */ -typedef struct _CalComponentAlarm CalComponentAlarm; - -/* An alarm occurrence, i.e. a trigger instance */ -typedef struct { - /* UID of the alarm that triggered */ - const char *auid; - - /* Trigger time, i.e. "5 minutes before the appointment" */ - time_t trigger; - - /* Actual event occurrence to which this trigger corresponds */ - time_t occur; -} CalAlarmInstance; - -/* Alarm trigger instances for a particular component */ -typedef struct { - /* The actual component */ - CalComponent *comp; - - /* List of CalAlarmInstance structures */ - GSList *alarms; -} CalComponentAlarms; - -/* Alarm types */ -typedef enum { - CAL_ALARM_NONE, - CAL_ALARM_AUDIO, - CAL_ALARM_DISPLAY, - CAL_ALARM_EMAIL, - CAL_ALARM_PROCEDURE, - CAL_ALARM_UNKNOWN -} CalAlarmAction; - -/* Whether a trigger is relative to the start or end of an event occurrence, or - * whether it is specified to occur at an absolute time. - */ -typedef enum { - CAL_ALARM_TRIGGER_NONE, - CAL_ALARM_TRIGGER_RELATIVE_START, - CAL_ALARM_TRIGGER_RELATIVE_END, - CAL_ALARM_TRIGGER_ABSOLUTE -} CalAlarmTriggerType; - -typedef struct { - CalAlarmTriggerType type; - - union { - struct icaldurationtype rel_duration; - struct icaltimetype abs_time; - } u; -} CalAlarmTrigger; - -gboolean cal_component_has_alarms (CalComponent *comp); -void cal_component_add_alarm (CalComponent *comp, CalComponentAlarm *alarm); -void cal_component_remove_alarm (CalComponent *comp, const char *auid); - -GList *cal_component_get_alarm_uids (CalComponent *comp); -CalComponentAlarm *cal_component_get_alarm (CalComponent *comp, const char *auid); - -void cal_component_alarms_free (CalComponentAlarms *alarms); - -/* CalComponentAlarms */ -CalComponentAlarm *cal_component_alarm_new (void); -CalComponentAlarm *cal_component_alarm_clone (CalComponentAlarm *alarm); - -const char *cal_component_alarm_get_uid (CalComponentAlarm *alarm); - -void cal_component_alarm_get_action (CalComponentAlarm *alarm, CalAlarmAction *action); -void cal_component_alarm_set_action (CalComponentAlarm *alarm, CalAlarmAction action); - -void cal_component_alarm_get_trigger (CalComponentAlarm *alarm, CalAlarmTrigger *trigger); -void cal_component_alarm_set_trigger (CalComponentAlarm *alarm, CalAlarmTrigger trigger); - -void cal_component_alarm_free (CalComponentAlarm *alarm); - - - - -END_GNOME_DECLS - -#endif diff --git a/calendar/cal-util/cal-recur.c b/calendar/cal-util/cal-recur.c deleted file mode 100644 index 8ac6f771d9..0000000000 --- a/calendar/cal-util/cal-recur.c +++ /dev/null @@ -1,3776 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Evolution calendar recurrence rule functions - * - * Copyright (C) 2000 Helix Code, Inc. - * - * Author: Damon Chaplin <damon@helixcode.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -#include <config.h> -#include <stdlib.h> -#include <string.h> -#include <cal-util/cal-recur.h> -#include <cal-util/timeutil.h> - - -/* - * Introduction to The Recurrence Generation Functions: - * - * Note: This is pretty complicated. See the iCalendar spec (RFC 2445) for - * the specification of the recurrence rules and lots of examples - * (sections 4.3.10 & 4.8.5). We also want to support the older - * vCalendar spec, though this should be easy since it is basically a - * subset of iCalendar. - * - * o An iCalendar event can have any number of recurrence rules specifying - * occurrences of the event, as well as dates & times of specific - * occurrences. It can also have any number of recurrence rules and - * specific dates & times specifying exceptions to the occurrences. - * So we first merge all the occurrences generated, eliminating any - * duplicates, then we generate all the exceptions and remove these to - * form the final set of occurrences. - * - * o There are 7 frequencies of occurrences: YEARLY, MONTHLY, WEEKLY, DAILY, - * HOURLY, MINUTELY & SECONDLY. The 'interval' property specifies the - * multiples of the frequency which we step by. We generate a 'set' of - * occurrences for each period defined by the frequency & interval. - * So for a YEARLY frequency with an interval of 3, we generate a set of - * occurrences for every 3rd year. We use complete years here - any - * generated occurrences that occur before the event's start (or after its - * end) are just discarded. - * - * o There are 8 frequency modifiers: BYMONTH, BYWEEKNO, BYYEARDAY, BYMONTHDAY, - * BYDAY, BYHOUR, BYMINUTE & BYSECOND. These can either add extra occurrences - * or filter out occurrences. For example 'FREQ=YEARLY;BYMONTH=1,2' produces - * 2 occurrences for each year rather than the default 1. And - * 'FREQ=DAILY;BYMONTH=1' filters out all occurrences except those in Jan. - * If the modifier works on periods which are less than the recurrence - * frequency, then extra occurrences are added, otherwise occurrences are - * filtered. So we have 2 functions for each modifier - one to expand events - * and the other to filter. We use a table of functions for each frequency - * which points to the appropriate function to use for each modifier. - * - * o Any number of frequency modifiers can be used in a recurrence rule. - * (Though the iCalendar spec says that BYWEEKNO can only be used in a YEARLY - * rule, and some modifiers aren't appropriate for some frequencies - e.g. - * BYMONTHDAY is not really useful in a WEEKLY frequency, and BYYEARDAY is - * not useful in a MONTHLY or WEEKLY frequency). - * The frequency modifiers are applied in the order given above. The first 5 - * modifier rules (BYMONTH, BYWEEKNO, BYYEARDAY, BYMONTHDAY & BYDAY) all - * produce the days on which the occurrences take place, and so we have to - * compute some of these in parallel rather than sequentially, or we may end - * up with too many days. - * - * o Note that some expansion functions may produce days which are invalid, - * e.g. 31st September, 30th Feb. These invalid days are removed before the - * BYHOUR, BYMINUTE & BYSECOND modifier functions are applied. - * - * o After the set of occurrences for the frequency interval are generated, - * the BYSETPOS property is used to select which of the occurrences are - * finally output. If BYSETPOS is not specified then all the occurrences are - * output. - * - * - * FIXME: I think there are a few errors in this code: - * - * 1) I'm not sure it should be generating events in parallel like it says - * above. That needs to be checked. - * - * 2) I didn't think about timezone changes when implementing this. I just - * assumed all the occurrences of the event would be in local time. - * But when clocks go back or forwards due to daylight-saving time, some - * special handling may be needed, especially for the shorter frequencies. - * e.g. for a MINUTELY frequency it should probably iterate over all the - * minutes before and after clocks go back (i.e. some may be the same local - * time but have different UTC offsets). For longer frequencies, if an - * occurrence lands on the overlapping or non-existant time when clocks - * go back/forward, then it may need to choose which of the times to use - * or move the time forward or something. I'm not sure this is clear in the - * spec. - */ - -/* Define this for some debugging output. */ -#if 0 -#define CAL_OBJ_DEBUG 1 -#endif - -/* We will use icalrecurrencetype instead of this eventually. */ -typedef struct { - icalrecurrencetype_frequency freq; - - int interval; - - /* Specifies the end of the recurrence. No occurrences are generated - after this date. If it is 0, the event recurs forever. */ - time_t enddate; - - /* WKST property - the week start day: 0 = Monday to 6 = Sunday. */ - gint week_start_day; - - - /* NOTE: I've used GList's here, but it doesn't matter if we use - other data structures like arrays. The code should be easy to - change. So long as it is easy to see if the modifier is set. */ - - /* For BYMONTH modifier. A list of GINT_TO_POINTERs, 0-11. */ - GList *bymonth; - - /* For BYWEEKNO modifier. A list of GINT_TO_POINTERs, [+-]1-53. */ - GList *byweekno; - - /* For BYYEARDAY modifier. A list of GINT_TO_POINTERs, [+-]1-366. */ - GList *byyearday; - - /* For BYMONTHDAY modifier. A list of GINT_TO_POINTERs, [+-]1-31. */ - GList *bymonthday; - - /* For BYDAY modifier. A list of GINT_TO_POINTERs, in pairs. - The first of each pair is the weekday, 0 = Monday to 6 = Sunday. - The second of each pair is the week number [+-]0-53. */ - GList *byday; - - /* For BYHOUR modifier. A list of GINT_TO_POINTERs, 0-23. */ - GList *byhour; - - /* For BYMINUTE modifier. A list of GINT_TO_POINTERs, 0-59. */ - GList *byminute; - - /* For BYSECOND modifier. A list of GINT_TO_POINTERs, 0-60. */ - GList *bysecond; - - /* For BYSETPOS modifier. A list of GINT_TO_POINTERs, +ve or -ve. */ - GList *bysetpos; -} CalRecurrence; - -/* This is what we use to pass to all the filter functions. */ -typedef struct _RecurData RecurData; -struct _RecurData { - CalRecurrence *recur; - - /* This is used for the WEEKLY frequency. It is the offset from the - week_start_day. */ - gint weekday_offset; - - /* This is used for fast lookup in BYMONTH filtering. */ - guint8 months[12]; - - /* This is used for fast lookup in BYYEARDAY filtering. */ - guint8 yeardays[367], neg_yeardays[367]; /* Days are 1 - 366. */ - - /* This is used for fast lookup in BYMONTHDAY filtering. */ - guint8 monthdays[32], neg_monthdays[32]; /* Days are 1 to 31. */ - - /* This is used for fast lookup in BYDAY filtering. */ - guint8 weekdays[7]; - - /* This is used for fast lookup in BYHOUR filtering. */ - guint8 hours[24]; - - /* This is used for fast lookup in BYMINUTE filtering. */ - guint8 minutes[60]; - - /* This is used for fast lookup in BYSECOND filtering. */ - guint8 seconds[62]; -}; - -/* This is what we use to represent a date & time. */ -typedef struct _CalObjTime CalObjTime; -struct _CalObjTime { - guint16 year; - guint8 month; /* 0 - 11 */ - guint8 day; /* 1 - 31 */ - guint8 hour; /* 0 - 23 */ - guint8 minute; /* 0 - 59 */ - guint8 second; /* 0 - 59 (maybe up to 61 for leap seconds) */ - guint8 is_rdate; /* TRUE if this is an RDATE, which may have an - end or duration set. */ -}; - -/* This is what we use to represent specific recurrence dates. - Note that we assume it starts with a CalObjTime when sorting. */ -typedef struct _CalObjRecurrenceDate CalObjRecurrenceDate; -struct _CalObjRecurrenceDate { - CalObjTime start; - CalComponentPeriod *period; -}; - -/* The paramter we use to store the enddate in RRULE and EXRULE properties. */ -#define EVOLUTION_END_DATE_PARAMETER "X-EVOLUTION-ENDDATE" - -typedef gboolean (*CalObjFindStartFn) (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -typedef gboolean (*CalObjFindNextFn) (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); -typedef GArray* (*CalObjFilterFn) (RecurData *recur_data, - GArray *occs); - -typedef struct _CalRecurVTable CalRecurVTable; -struct _CalRecurVTable { - CalObjFindStartFn find_start_position; - CalObjFindNextFn find_next_position; - - CalObjFilterFn bymonth_filter; - CalObjFilterFn byweekno_filter; - CalObjFilterFn byyearday_filter; - CalObjFilterFn bymonthday_filter; - CalObjFilterFn byday_filter; - CalObjFilterFn byhour_filter; - CalObjFilterFn byminute_filter; - CalObjFilterFn bysecond_filter; -}; - - -/* This is used to specify which parts of the CalObjTime to compare in - cal_obj_time_compare(). */ -typedef enum { - CALOBJ_YEAR, - CALOBJ_MONTH, - CALOBJ_DAY, - CALOBJ_HOUR, - CALOBJ_MINUTE, - CALOBJ_SECOND -} CalObjTimeComparison; - -static void cal_recur_generate_instances_of_rule (CalComponent *comp, - icalproperty *prop, - time_t start, - time_t end, - CalRecurInstanceFn cb, - gpointer cb_data); - -static CalRecurrence * cal_recur_from_icalproperty (icalproperty *prop, - gboolean exception); -static gint cal_recur_ical_weekday_to_weekday (enum icalrecurrencetype_weekday day); -static void cal_recur_free (CalRecurrence *r); - - -static gboolean cal_object_get_rdate_end (CalObjTime *occ, - GArray *rdate_periods); -static void cal_object_compute_duration (CalObjTime *start, - CalObjTime *end, - gint *days, - gint *seconds); - -static gboolean generate_instances_for_chunk (CalComponent *comp, - time_t comp_dtstart, - GSList *rrules, - GSList *rdates, - GSList *exrules, - GSList *exdates, - gboolean single_rule, - CalObjTime *event_start, - time_t interval_start, - CalObjTime *chunk_start, - CalObjTime *chunk_end, - gint duration_days, - gint duration_seconds, - CalRecurInstanceFn cb, - gpointer cb_data); - -static GArray* cal_obj_expand_recurrence (CalObjTime *event_start, - CalRecurrence *recur, - CalObjTime *interval_start, - CalObjTime *interval_end, - gboolean *finished); - -static GArray* cal_obj_generate_set_yearly (RecurData *recur_data, - CalRecurVTable *vtable, - CalObjTime *occ); -static GArray* cal_obj_generate_set_monthly (RecurData *recur_data, - CalRecurVTable *vtable, - CalObjTime *occ); -static GArray* cal_obj_generate_set_default (RecurData *recur_data, - CalRecurVTable *vtable, - CalObjTime *occ); - - -static CalRecurVTable* cal_obj_get_vtable (icalrecurrencetype_frequency recur_type); -static void cal_obj_initialize_recur_data (RecurData *recur_data, - CalRecurrence *recur, - CalObjTime *event_start); -static void cal_obj_sort_occurrences (GArray *occs); -static gint cal_obj_time_compare_func (const void *arg1, - const void *arg2); -static void cal_obj_remove_duplicates_and_invalid_dates (GArray *occs); -static void cal_obj_remove_exceptions (GArray *occs, - GArray *ex_occs); -static GArray* cal_obj_bysetpos_filter (CalRecurrence *recur, - GArray *occs); - - -static gboolean cal_obj_yearly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -static gboolean cal_obj_yearly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); - -static gboolean cal_obj_monthly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -static gboolean cal_obj_monthly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); - -static gboolean cal_obj_weekly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -static gboolean cal_obj_weekly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); - -static gboolean cal_obj_daily_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -static gboolean cal_obj_daily_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); - -static gboolean cal_obj_hourly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -static gboolean cal_obj_hourly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); - -static gboolean cal_obj_minutely_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -static gboolean cal_obj_minutely_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); - -static gboolean cal_obj_secondly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime); -static gboolean cal_obj_secondly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end); - -static GArray* cal_obj_bymonth_expand (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_bymonth_filter (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byweekno_expand (RecurData *recur_data, - GArray *occs); -#if 0 -/* This isn't used at present. */ -static GArray* cal_obj_byweekno_filter (RecurData *recur_data, - GArray *occs); -#endif -static GArray* cal_obj_byyearday_expand (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byyearday_filter (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_bymonthday_expand (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_bymonthday_filter (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byday_expand_yearly (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byday_expand_monthly (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byday_expand_weekly (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byday_filter (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byhour_expand (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byhour_filter (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byminute_expand (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_byminute_filter (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_bysecond_expand (RecurData *recur_data, - GArray *occs); -static GArray* cal_obj_bysecond_filter (RecurData *recur_data, - GArray *occs); - -static void cal_obj_time_add_months (CalObjTime *cotime, - gint months); -static void cal_obj_time_add_days (CalObjTime *cotime, - gint days); -static void cal_obj_time_add_hours (CalObjTime *cotime, - gint hours); -static void cal_obj_time_add_minutes (CalObjTime *cotime, - gint minutes); -static void cal_obj_time_add_seconds (CalObjTime *cotime, - gint seconds); -static gint cal_obj_time_compare (CalObjTime *cotime1, - CalObjTime *cotime2, - CalObjTimeComparison type); -static gint cal_obj_time_weekday (CalObjTime *cotime); -static gint cal_obj_time_weekday_offset (CalObjTime *cotime, - CalRecurrence *recur); -static gint cal_obj_time_day_of_year (CalObjTime *cotime); -static void cal_obj_time_find_first_week (CalObjTime *cotime, - RecurData *recur_data); -static void cal_object_time_from_time (CalObjTime *cotime, - time_t t); -#if 0 -static gint cal_obj_date_only_compare_func (const void *arg1, - const void *arg2); -#endif - - - -static gboolean cal_recur_ensure_end_dates (CalComponent *comp, - gboolean refresh); -static gboolean cal_recur_ensure_rule_end_date (CalComponent *comp, - icalproperty *prop, - gboolean exception, - gboolean refresh); -static gboolean cal_recur_ensure_rule_end_date_cb (CalComponent *comp, - time_t instance_start, - time_t instance_end, - gpointer data); -static time_t cal_recur_get_rule_end_date (icalproperty *prop); -static void cal_recur_set_rule_end_date (icalproperty *prop, - time_t end_date); - - -#ifdef CAL_OBJ_DEBUG -static char* cal_obj_time_to_string (CalObjTime *cotime); -#endif - - -CalRecurVTable cal_obj_yearly_vtable = { - cal_obj_yearly_find_start_position, - cal_obj_yearly_find_next_position, - - cal_obj_bymonth_expand, - cal_obj_byweekno_expand, - cal_obj_byyearday_expand, - cal_obj_bymonthday_expand, - cal_obj_byday_expand_yearly, - cal_obj_byhour_expand, - cal_obj_byminute_expand, - cal_obj_bysecond_expand -}; - -CalRecurVTable cal_obj_monthly_vtable = { - cal_obj_monthly_find_start_position, - cal_obj_monthly_find_next_position, - - cal_obj_bymonth_filter, - NULL, /* BYWEEKNO is only applicable to YEARLY frequency. */ - NULL, /* BYYEARDAY is not useful in a MONTHLY frequency. */ - cal_obj_bymonthday_expand, - cal_obj_byday_expand_monthly, - cal_obj_byhour_expand, - cal_obj_byminute_expand, - cal_obj_bysecond_expand -}; - -CalRecurVTable cal_obj_weekly_vtable = { - cal_obj_weekly_find_start_position, - cal_obj_weekly_find_next_position, - - cal_obj_bymonth_filter, - NULL, /* BYWEEKNO is only applicable to YEARLY frequency. */ - NULL, /* BYYEARDAY is not useful in a WEEKLY frequency. */ - NULL, /* BYMONTHDAY is not useful in a WEEKLY frequency. */ - cal_obj_byday_expand_weekly, - cal_obj_byhour_expand, - cal_obj_byminute_expand, - cal_obj_bysecond_expand -}; - -CalRecurVTable cal_obj_daily_vtable = { - cal_obj_daily_find_start_position, - cal_obj_daily_find_next_position, - - cal_obj_bymonth_filter, - NULL, /* BYWEEKNO is only applicable to YEARLY frequency. */ - cal_obj_byyearday_filter, - cal_obj_bymonthday_filter, - cal_obj_byday_filter, - cal_obj_byhour_expand, - cal_obj_byminute_expand, - cal_obj_bysecond_expand -}; - -CalRecurVTable cal_obj_hourly_vtable = { - cal_obj_hourly_find_start_position, - cal_obj_hourly_find_next_position, - - cal_obj_bymonth_filter, - NULL, /* BYWEEKNO is only applicable to YEARLY frequency. */ - cal_obj_byyearday_filter, - cal_obj_bymonthday_filter, - cal_obj_byday_filter, - cal_obj_byhour_filter, - cal_obj_byminute_expand, - cal_obj_bysecond_expand -}; - -CalRecurVTable cal_obj_minutely_vtable = { - cal_obj_minutely_find_start_position, - cal_obj_minutely_find_next_position, - - cal_obj_bymonth_filter, - NULL, /* BYWEEKNO is only applicable to YEARLY frequency. */ - cal_obj_byyearday_filter, - cal_obj_bymonthday_filter, - cal_obj_byday_filter, - cal_obj_byhour_filter, - cal_obj_byminute_filter, - cal_obj_bysecond_expand -}; - -CalRecurVTable cal_obj_secondly_vtable = { - cal_obj_secondly_find_start_position, - cal_obj_secondly_find_next_position, - - cal_obj_bymonth_filter, - NULL, /* BYWEEKNO is only applicable to YEARLY frequency. */ - cal_obj_byyearday_filter, - cal_obj_bymonthday_filter, - cal_obj_byday_filter, - cal_obj_byhour_filter, - cal_obj_byminute_filter, - cal_obj_bysecond_filter -}; - -/* - * Calls the given callback function for each occurrence of the event that - * intersects the range between the given start and end times (the end time is - * not included). Note that the occurrences may start before the given start - * time. - * - * If the callback routine returns FALSE the occurrence generation stops. - * - * Both start and end can be -1, in which case we start at the events first - * instance and continue until it ends, or forever if it has no enddate. - */ -void -cal_recur_generate_instances (CalComponent *comp, - time_t start, - time_t end, - CalRecurInstanceFn cb, - gpointer cb_data) -{ -#if 0 - g_print ("In cal_recur_generate_instances comp: %p\n", comp); - g_print (" start: %li - %s", start, ctime (&start)); - g_print (" end : %li - %s", end, ctime (&end)); -#endif - cal_recur_generate_instances_of_rule (comp, NULL, start, end, - cb, cb_data); -} - - -/* - * Calls the given callback function for each occurrence of the given - * recurrence rule between the given start and end times. If the rule is NULL - * it uses all the rules from the component. - * - * If the callback routine returns FALSE the occurrence generation stops. - * - * The use of the specific rule is for determining the end of a rule when - * COUNT is set. The callback will count instances and store the enddate - * when COUNT is reached. - * - * Both start and end can be -1, in which case we start at the events first - * instance and continue until it ends, or forever if it has no enddate. - */ -static void -cal_recur_generate_instances_of_rule (CalComponent *comp, - icalproperty *prop, - time_t start, - time_t end, - CalRecurInstanceFn cb, - gpointer cb_data) -{ - CalComponentDateTime dtstart, dtend; - time_t dtstart_time, dtend_time; - GSList *rrules = NULL, *rdates = NULL, elem; - GSList *exrules = NULL, *exdates = NULL; - CalObjTime interval_start, interval_end, event_start, event_end; - CalObjTime chunk_start, chunk_end; - gint days, seconds, year; - gboolean single_rule; - - g_return_if_fail (comp != NULL); - g_return_if_fail (cb != NULL); - g_return_if_fail (start >= -1); - g_return_if_fail (end >= -1); - - /* Get dtstart, dtend, recurrences, and exceptions */ - - cal_component_get_dtstart (comp, &dtstart); - cal_component_get_dtend (comp, &dtend); - - if (!dtstart.value) { - g_message ("cal_recur_generate_instances_of_rule(): bogus " - "component, does not have DTSTART. Skipping..."); - goto out; - } - - dtstart_time = icaltime_as_timet (*dtstart.value); - if (start == -1) - start = dtstart_time; - - /* FIXME: DURATION could be used instead, couldn't it? - Damon */ - if (dtend.value) - dtend_time = icaltime_as_timet (*dtend.value); - else - dtend_time = time_day_end (dtstart_time); - - /* If there is no recurrence, just call the callback if the event - intersects the given interval. */ - if (!(cal_component_has_recurrences (comp) - || cal_component_has_exceptions (comp))) { - if ((end == -1 || dtstart_time < end) && dtend_time > start) { - (* cb) (comp, dtstart_time, dtend_time, cb_data); - } - - goto out; - } - - /* If a specific recurrence rule is being used, set up a simple list, - else get the recurrence rules from the component. */ - if (prop) { - single_rule = TRUE; - - elem.data = prop; - elem.next = NULL; - rrules = &elem; - } else { - single_rule = FALSE; - - /* Make sure all the enddates for the rules are set. */ - cal_recur_ensure_end_dates (comp, FALSE); - - cal_component_get_rrule_property_list (comp, &rrules); - cal_component_get_rdate_list (comp, &rdates); - cal_component_get_exrule_property_list (comp, &exrules); - cal_component_get_exdate_list (comp, &exdates); - } - - /* Convert the interval start & end to CalObjTime. Note that if end - is -1 interval_end won't be set, so don't use it! - Also note that we use end - 1 since we want the interval to be - inclusive as it makes the code simpler. */ - cal_object_time_from_time (&interval_start, start); - if (end != -1) - cal_object_time_from_time (&interval_end, end - 1); - - cal_object_time_from_time (&event_start, dtstart_time); - cal_object_time_from_time (&event_end, dtend_time); - - /* Calculate the duration of the event, which we use for all - occurrences. We can't just subtract start from end since that may - be affected by daylight-saving time. So we want a value of days - + seconds. */ - cal_object_compute_duration (&event_start, &event_end, - &days, &seconds); - - /* Take off the duration from interval_start, so we get occurrences - that start just before the start time but overlap it. But only do - that if the interval is after the event's start time. */ - if (start > dtstart_time) { - cal_obj_time_add_days (&interval_start, -days); - cal_obj_time_add_seconds (&interval_start, -seconds); - } - - /* Expand the recurrence for each year between start & end, or until - the callback returns 0 if end is 0. We do a year at a time to - give the callback function a chance to break out of the loop, and - so we don't get into problems with infinite recurrences. Since we - have to work on complete sets of occurrences, if there is a yearly - frequency it wouldn't make sense to break it into smaller chunks, - since we would then be calculating the same sets several times. - Though this does mean that we sometimes do a lot more work than - is necessary, e.g. if COUNT is set to something quite low. */ - for (year = interval_start.year; - end == -1 || year <= interval_end.year; - year++) { - chunk_start = interval_start; - chunk_start.year = year; - if (end != -1) - chunk_end = interval_end; - chunk_end.year = year; - - if (year != interval_start.year) { - chunk_start.month = 0; - chunk_start.day = 1; - chunk_start.hour = 0; - chunk_start.minute = 0; - chunk_start.second = 0; - } - if (end == -1 || year != interval_end.year) { - chunk_end.month = 11; - chunk_end.day = 31; - chunk_end.hour = 23; - chunk_end.minute = 59; - chunk_end.second = 61; - chunk_end.is_rdate = FALSE; - } - - if (!generate_instances_for_chunk (comp, dtstart_time, - rrules, rdates, - exrules, exdates, - single_rule, - &event_start, - start, - &chunk_start, &chunk_end, - days, seconds, - cb, cb_data)) - break; - } - - if (!prop) { - cal_component_free_period_list (rdates); - cal_component_free_exdate_list (exdates); - } - - out: - cal_component_free_datetime (&dtstart); - cal_component_free_datetime (&dtend); -} - -/* Builds a list of GINT_TO_POINTER() elements out of a short array from a - * struct icalrecurrencetype. - */ -static GList * -array_to_list (short *array, int max_elements) -{ - GList *l; - int i; - - l = NULL; - - for (i = 0; i < max_elements && array[i] != ICAL_RECURRENCE_ARRAY_MAX; i++) - l = g_list_prepend (l, GINT_TO_POINTER ((int) (array[i]))); - return g_list_reverse (l); -} - -/** - * cal_recur_from_icalproperty: - * @ir: An RRULE or EXRULE #icalproperty. - * - * Converts an #icalproperty to a #CalRecurrence. This should be - * freed using the cal_recur_free() function. - * - * Return value: #CalRecurrence structure. - **/ -static CalRecurrence * -cal_recur_from_icalproperty (icalproperty *prop, gboolean exception) -{ - struct icalrecurrencetype ir; - CalRecurrence *r; - gint max_elements, i; - - g_return_val_if_fail (prop != NULL, NULL); - - r = g_new (CalRecurrence, 1); - - if (exception) - ir = icalproperty_get_exrule (prop); - else - ir = icalproperty_get_rrule (prop); - - r->freq = ir.freq; - r->interval = ir.interval; - - if (ir.count != 0) { - r->enddate = cal_recur_get_rule_end_date (prop); - } else { - /* FIXME: icaltime_as_timet() seems to return -1 if UNTIL isn't - set, but a simpler test would be better. */ - r->enddate = icaltime_as_timet (ir.until); - if (r->enddate == -1) - r->enddate = 0; - else if (ir.until.is_date) - /* FIXME: Decide what to do here. */ - r->enddate = time_add_day (r->enddate, 1) - 1; - } - - r->week_start_day = cal_recur_ical_weekday_to_weekday (ir.week_start); - - r->bymonth = array_to_list (ir.by_month, - sizeof (ir.by_month) / sizeof (ir.by_month[0])); - - r->byweekno = array_to_list (ir.by_week_no, - sizeof (ir.by_week_no) / sizeof (ir.by_week_no[0])); - - r->byyearday = array_to_list (ir.by_year_day, - sizeof (ir.by_year_day) / sizeof (ir.by_year_day[0])); - - r->bymonthday = array_to_list (ir.by_month_day, - sizeof (ir.by_month_day) / sizeof (ir.by_month_day[0])); - - /* FIXME: libical only supports 8 values, out of possible 107 * 7. */ - r->byday = NULL; - max_elements = sizeof (ir.by_day) / sizeof (ir.by_day[0]); - for (i = 0; i < max_elements && ir.by_day[i] != ICAL_RECURRENCE_ARRAY_MAX; i++) { - enum icalrecurrencetype_weekday day; - gint weeknum, weekday; - - day = icalrecurrencetype_day_day_of_week (ir.by_day[i]); - weeknum = icalrecurrencetype_day_position (ir.by_day[i]); - - weekday = cal_recur_ical_weekday_to_weekday (day); - - r->byday = g_list_prepend (r->byday, - GINT_TO_POINTER (weeknum)); - r->byday = g_list_prepend (r->byday, - GINT_TO_POINTER (weekday)); - } - - r->byhour = array_to_list (ir.by_hour, - sizeof (ir.by_hour) / sizeof (ir.by_hour[0])); - - r->byminute = array_to_list (ir.by_minute, - sizeof (ir.by_minute) / sizeof (ir.by_minute[0])); - - r->bysecond = array_to_list (ir.by_second, - sizeof (ir.by_second) / sizeof (ir.by_second[0])); - - r->bysetpos = array_to_list (ir.by_set_pos, - sizeof (ir.by_set_pos) / sizeof (ir.by_set_pos[0])); - - return r; -} - - -static gint -cal_recur_ical_weekday_to_weekday (enum icalrecurrencetype_weekday day) -{ - gint weekday; - - switch (day) { - case ICAL_NO_WEEKDAY: /* Monday is the default in RFC2445. */ - case ICAL_MONDAY_WEEKDAY: - weekday = 0; - break; - case ICAL_TUESDAY_WEEKDAY: - weekday = 1; - break; - case ICAL_WEDNESDAY_WEEKDAY: - weekday = 2; - break; - case ICAL_THURSDAY_WEEKDAY: - weekday = 3; - break; - case ICAL_FRIDAY_WEEKDAY: - weekday = 4; - break; - case ICAL_SATURDAY_WEEKDAY: - weekday = 5; - break; - case ICAL_SUNDAY_WEEKDAY: - weekday = 6; - break; - default: - g_warning ("cal_recur_ical_weekday_to_weekday(): Unknown week day %d", - day); - weekday = 0; - } - - return weekday; -} - - -/** - * cal_recur_free: - * @r: A #CalRecurrence structure. - * - * Frees a #CalRecurrence structure. - **/ -static void -cal_recur_free (CalRecurrence *r) -{ - g_return_if_fail (r != NULL); - - g_list_free (r->bymonth); - g_list_free (r->byweekno); - g_list_free (r->byyearday); - g_list_free (r->bymonthday); - g_list_free (r->byday); - g_list_free (r->byhour); - g_list_free (r->byminute); - g_list_free (r->bysecond); - g_list_free (r->bysetpos); - - g_free (r); -} - -/* Generates one year's worth of recurrence instances. Returns TRUE if all the - * callback invocations returned TRUE, or FALSE when any one of them returns - * FALSE, i.e. meaning that the instance generation should be stopped. - * - * This should only output instances whose start time is between chunk_start - * and chunk_end (inclusive), or we may generate duplicates when we do the next - * chunk. (This applies mainly to weekly recurrences, since weeks can span 2 - * years.) - * - * It should also only output instances that are on or after the event's - * DTSTART property and that intersect the required interval, between - * interval_start and interval_end. - */ -static gboolean -generate_instances_for_chunk (CalComponent *comp, - time_t comp_dtstart, - GSList *rrules, - GSList *rdates, - GSList *exrules, - GSList *exdates, - gboolean single_rule, - CalObjTime *event_start, - time_t interval_start, - CalObjTime *chunk_start, - CalObjTime *chunk_end, - gint duration_days, - gint duration_seconds, - CalRecurInstanceFn cb, - gpointer cb_data) -{ - GArray *occs, *ex_occs, *tmp_occs, *rdate_periods; - CalObjTime cotime, *occ; - GSList *elem; - gint i; - time_t start_time, end_time; - struct tm start_tm, end_tm; - gboolean cb_status = TRUE, rule_finished, finished = TRUE; - -#if 0 - g_print ("In generate_instances_for_chunk rrules: %p\n" - " %i/%i/%i %02i:%02i:%02i - %i/%i/%i %02i:%02i:%02i\n", - rrules, - chunk_start->day, chunk_start->month + 1, - chunk_start->year, chunk_start->hour, - chunk_start->minute, chunk_start->second, - chunk_end->day, chunk_end->month + 1, - chunk_end->year, chunk_end->hour, - chunk_end->minute, chunk_end->second); -#endif - - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - ex_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - rdate_periods = g_array_new (FALSE, FALSE, - sizeof (CalObjRecurrenceDate)); - - /* The original DTSTART property is included in the occurrence set, - but not if we are just generating occurrences for a single rule. */ - if (!single_rule) { - /* We add it if it is in this chunk. If it is after this chunk - we set finished to FALSE, since we know we aren't finished - yet. */ - if (cal_obj_time_compare_func (event_start, chunk_end) >= 0) - finished = FALSE; - else if (cal_obj_time_compare_func (event_start, chunk_start) >= 0) - g_array_append_vals (occs, event_start, 1); - } - - /* Expand each of the recurrence rules. */ - for (elem = rrules; elem; elem = elem->next) { - icalproperty *prop; - CalRecurrence *r; - - prop = elem->data; - r = cal_recur_from_icalproperty (prop, FALSE); - - tmp_occs = cal_obj_expand_recurrence (event_start, r, - chunk_start, - chunk_end, - &rule_finished); - cal_recur_free (r); - - /* If any of the rules return FALSE for finished, we know we - have to carry on so we set finished to FALSE. */ - if (!rule_finished) - finished = FALSE; - - g_array_append_vals (occs, tmp_occs->data, tmp_occs->len); - g_array_free (tmp_occs, TRUE); - } - - /* Add on specific occurrence dates, flag them as RDATEs, and store - a pointer to the period in the rdate_periods array. */ - for (elem = rdates; elem; elem = elem->next) { - CalComponentPeriod *p; - CalObjRecurrenceDate rdate; - time_t t; - - p = elem->data; - t = icaltime_as_timet (p->start); - cal_object_time_from_time (&cotime, t); - - /* If the rdate is after the current chunk we set finished - to FALSE, and we skip it. */ - if (cal_obj_time_compare_func (&cotime, chunk_end) >= 0) { - finished = FALSE; - continue; - } - - /* Check if the end date or duration is set. If it is we need - to store it so we can get it later. (libical seems to set - second to -1 to denote an unset time. See icalvalue.c) */ - if (p->type != CAL_COMPONENT_PERIOD_DATETIME - || p->u.end.second != -1) { - cotime.is_rdate = TRUE; - - rdate.start = cotime; - rdate.period = p; - g_array_append_val (rdate_periods, rdate); - } - - g_array_append_val (occs, cotime); - } - - /* Expand each of the exception rules. */ - for (elem = exrules; elem; elem = elem->next) { - icalproperty *prop; - CalRecurrence *r; - - prop = elem->data; - r = cal_recur_from_icalproperty (prop, FALSE); - - tmp_occs = cal_obj_expand_recurrence (event_start, r, - chunk_start, - chunk_end, - &rule_finished); - cal_recur_free (r); - - g_array_append_vals (ex_occs, tmp_occs->data, tmp_occs->len); - g_array_free (tmp_occs, TRUE); - } - - /* Add on specific exception dates. */ - for (elem = exdates; elem; elem = elem->next) { - CalComponentDateTime *cdt; - time_t t; - - /* FIXME we should only be dealing with dates, not times too. - - No, I think it is supposed to be dates & times - Damon. - I'm not sure what the semantics of just a date would be, - since the event could recur several times each day. */ - cdt = elem->data; - t = icaltime_as_timet (*cdt->value); - cal_object_time_from_time (&cotime, t); - - g_array_append_val (ex_occs, cotime); - } - - - /* Sort all the arrays. */ - cal_obj_sort_occurrences (occs); - cal_obj_sort_occurrences (ex_occs); - - qsort (rdate_periods->data, rdate_periods->len, - sizeof (CalObjRecurrenceDate), cal_obj_time_compare_func); - - /* Create the final array, by removing the exceptions from the - occurrences, and removing any duplicates. */ - cal_obj_remove_exceptions (occs, ex_occs); - - /* Call the callback for each occurrence. If it returns 0 we break - out of the loop. */ - for (i = 0; i < occs->len; i++) { - /* Convert each CalObjTime into a start & end time_t, and - check it is within the bounds of the event & interval. */ - occ = &g_array_index (occs, CalObjTime, i); -#if 0 - g_print ("Checking occurrence: %s\n", - cal_obj_time_to_string (occ)); -#endif - start_tm.tm_year = occ->year - 1900; - start_tm.tm_mon = occ->month; - start_tm.tm_mday = occ->day; - start_tm.tm_hour = occ->hour; - start_tm.tm_min = occ->minute; - start_tm.tm_sec = occ->second; - start_tm.tm_isdst = -1; - start_time = mktime (&start_tm); - - if (start_time == -1) { - g_warning ("mktime failed - time_t out of range?"); - finished = TRUE; - break; - } - - /* Check to ensure that the start time is at or after the - event's DTSTART time, and that it is inside the chunk that - we are currently working on. (Note that the chunk_end time - is never after the interval end time, so this also tests - that we don't go past the end of the required interval). */ - if (start_time < comp_dtstart - || cal_obj_time_compare_func (occ, chunk_start) < 0 - || cal_obj_time_compare_func (occ, chunk_end) > 0) { -#if 0 - g_print (" start time invalid\n"); -#endif - continue; - } - - if (occ->is_rdate) { - if (!cal_object_get_rdate_end (occ, rdate_periods)) { - cal_obj_time_add_days (occ, duration_days); - cal_obj_time_add_seconds (occ, - duration_seconds); - } - } else { - cal_obj_time_add_days (occ, duration_days); - cal_obj_time_add_seconds (occ, duration_seconds); - } - - end_tm.tm_year = occ->year - 1900; - end_tm.tm_mon = occ->month; - end_tm.tm_mday = occ->day; - end_tm.tm_hour = occ->hour; - end_tm.tm_min = occ->minute; - end_tm.tm_sec = occ->second; - end_tm.tm_isdst = -1; - end_time = mktime (&end_tm); - - if (end_time == -1) { - g_warning ("mktime failed - time_t out of range?"); - finished = TRUE; - break; - } - - /* Check that the end time is after the interval start, so we - know that it intersects the required interval. */ - if (end_time <= interval_start) { -#if 0 - g_print (" end time invalid\n"); -#endif - continue; - } - - cb_status = (*cb) (comp, start_time, end_time, cb_data); - if (!cb_status) - break; - } - - g_array_free (occs, TRUE); - g_array_free (ex_occs, TRUE); - g_array_free (rdate_periods, TRUE); - - /* We return TRUE (i.e. carry on) only if the callback has always - returned TRUE and we know that we have more occurrences to generate - (i.e. finished is FALSE). */ - return cb_status && !finished; -} - - -/* This looks up the occurrence time in the sorted rdate_periods array, and - tries to compute the end time of the occurrence. If no end time or duration - is set it returns FALSE and the default duration will be used. */ -static gboolean -cal_object_get_rdate_end (CalObjTime *occ, - GArray *rdate_periods) -{ - CalObjRecurrenceDate *rdate = NULL; - CalComponentPeriod *p; - gint lower, upper, middle, cmp = 0; - time_t t; - - lower = 0; - upper = rdate_periods->len; - - while (lower < upper) { - middle = (lower + upper) >> 1; - - rdate = &g_array_index (rdate_periods, CalObjRecurrenceDate, - middle); - - cmp = cal_obj_time_compare_func (occ, &rdate->start); - - if (cmp == 0) - break; - else if (cmp < 0) - upper = middle; - else - lower = middle + 1; - } - - /* This should never happen. */ - if (cmp == 0) { - g_warning ("Recurrence date not found"); - return FALSE; - } - - p = rdate->period; - if (p->type == CAL_COMPONENT_PERIOD_DATETIME) { - t = icaltime_as_timet (p->u.end); - cal_object_time_from_time (occ, t); - } else { - cal_obj_time_add_days (occ, p->u.duration.weeks * 7 - + p->u.duration.days); - cal_obj_time_add_hours (occ, p->u.duration.hours); - cal_obj_time_add_minutes (occ, p->u.duration.minutes); - cal_obj_time_add_seconds (occ, p->u.duration.seconds); - } - - return TRUE; -} - - -static void -cal_object_compute_duration (CalObjTime *start, - CalObjTime *end, - gint *days, - gint *seconds) -{ - GDate start_date, end_date; - gint start_seconds, end_seconds; - - g_date_clear (&start_date, 1); - g_date_clear (&end_date, 1); - g_date_set_dmy (&start_date, start->day, start->month + 1, - start->year); - g_date_set_dmy (&end_date, end->day, end->month + 1, - end->year); - - *days = g_date_julian (&end_date) - g_date_julian (&start_date); - start_seconds = start->hour * 3600 + start->minute * 60 - + start->second; - end_seconds = end->hour * 3600 + end->minute * 60 + end->second; - - *seconds = end_seconds - start_seconds; - if (*seconds < 0) { - *days = *days - 1; - *seconds += 24 * 60 * 60; - } -} - - -/* Returns an unsorted GArray of CalObjTime's resulting from expanding the - given recurrence rule within the given interval. Note that it doesn't - clip the generated occurrences to the interval, i.e. if the interval - starts part way through the year this function still returns all the - occurrences for the year. Clipping is done later. - The finished flag is set to FALSE if there are more occurrences to generate - after the given interval.*/ -static GArray* -cal_obj_expand_recurrence (CalObjTime *event_start, - CalRecurrence *recur, - CalObjTime *interval_start, - CalObjTime *interval_end, - gboolean *finished) -{ - CalRecurVTable *vtable; - CalObjTime *event_end = NULL, event_end_cotime; - RecurData recur_data; - CalObjTime occ, *cotime; - GArray *all_occs, *occs; - gint len; - - /* This is the resulting array of CalObjTime elements. */ - all_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - *finished = TRUE; - - vtable = cal_obj_get_vtable (recur->freq); - if (!vtable) - return all_occs; - - /* Calculate some useful data such as some fast lookup tables. */ - cal_obj_initialize_recur_data (&recur_data, recur, event_start); - - /* Compute the event_end, if the recur's enddate is set. */ - if (recur->enddate > 0) { - cal_object_time_from_time (&event_end_cotime, - recur->enddate); - event_end = &event_end_cotime; - - /* If the enddate is before the requested interval return. */ - if (cal_obj_time_compare_func (event_end, interval_start) < 0) - return all_occs; - } - - /* Set finished to FALSE if we know there will be more occurrences to - do after this interval. */ - if (!interval_end || !event_end - || cal_obj_time_compare_func (event_end, interval_end) > 0) - *finished = FALSE; - - /* Get the first period based on the frequency and the interval that - intersects the interval between start and end. */ - if ((*vtable->find_start_position) (event_start, event_end, - &recur_data, - interval_start, interval_end, - &occ)) - return all_occs; - - /* Loop until the event ends or we go past the end of the required - interval. */ - for (;;) { - /* Generate the set of occurrences for this period. */ - switch (recur->freq) { - case ICAL_YEARLY_RECURRENCE: - occs = cal_obj_generate_set_yearly (&recur_data, - vtable, &occ); - break; - case ICAL_MONTHLY_RECURRENCE: - occs = cal_obj_generate_set_monthly (&recur_data, - vtable, &occ); - break; - default: - occs = cal_obj_generate_set_default (&recur_data, - vtable, &occ); - break; - } - - /* Sort the occurrences and remove duplicates. */ - cal_obj_sort_occurrences (occs); - cal_obj_remove_duplicates_and_invalid_dates (occs); - - /* Apply the BYSETPOS property. */ - occs = cal_obj_bysetpos_filter (recur, occs); - - /* Remove any occs after event_end. */ - len = occs->len - 1; - if (event_end) { - while (len >= 0) { - cotime = &g_array_index (occs, CalObjTime, - len); - if (cal_obj_time_compare_func (cotime, - event_end) <= 0) - break; - len--; - } - } - - /* Add the occurrences onto the main array. */ - if (len >= 0) - g_array_append_vals (all_occs, occs->data, len + 1); - - g_array_free (occs, TRUE); - - /* Skip to the next period, or exit the loop if finished. */ - if ((*vtable->find_next_position) (&occ, event_end, - &recur_data, interval_end)) - break; - } - - return all_occs; -} - - -static GArray* -cal_obj_generate_set_yearly (RecurData *recur_data, - CalRecurVTable *vtable, - CalObjTime *occ) -{ - CalRecurrence *recur = recur_data->recur; - GArray *occs_arrays[4], *occs, *occs2; - gint num_occs_arrays = 0, i; - - /* This is a bit complicated, since the iCalendar spec says that - several BYxxx modifiers can be used simultaneously. So we have to - be quite careful when determining the days of the occurrences. - The BYHOUR, BYMINUTE & BYSECOND modifiers are no problem at all. - - The modifiers we have to worry about are: BYMONTH, BYWEEKNO, - BYYEARDAY, BYMONTHDAY & BYDAY. We can't do these sequentially - since each filter will mess up the results of the previous one. - But they aren't all completely independant, e.g. BYMONTHDAY and - BYDAY are related to BYMONTH, and BYDAY is related to BYWEEKNO. - - BYDAY & BYMONTHDAY can also be applied independently, which makes - it worse. So we assume that if BYMONTH or BYWEEKNO is used, then - the BYDAY modifier applies to those, else it is applied - independantly. - - We expand the occurrences in parallel into the occs_arrays[] array, - and then merge them all into one GArray before expanding BYHOUR, - BYMINUTE & BYSECOND. */ - - if (recur->bymonth) { - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - - occs = (*vtable->bymonth_filter) (recur_data, occs); - - /* If BYMONTHDAY & BYDAY are both set we need to expand them - in parallel and add the results. */ - if (recur->bymonthday && recur->byday) { - /* Copy the occs array. */ - occs2 = g_array_new (FALSE, FALSE, - sizeof (CalObjTime)); - g_array_append_vals (occs2, occs->data, occs->len); - - occs = (*vtable->bymonthday_filter) (recur_data, occs); - /* Note that we explicitly call the monthly version - of the BYDAY expansion filter. */ - occs2 = cal_obj_byday_expand_monthly (recur_data, - occs2); - - /* Add the 2 resulting arrays together. */ - g_array_append_vals (occs, occs2->data, occs2->len); - g_array_free (occs2, TRUE); - } else { - occs = (*vtable->bymonthday_filter) (recur_data, occs); - /* Note that we explicitly call the monthly version - of the BYDAY expansion filter. */ - occs = cal_obj_byday_expand_monthly (recur_data, occs); - } - - occs_arrays[num_occs_arrays++] = occs; - } - - if (recur->byweekno) { - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - - occs = (*vtable->byweekno_filter) (recur_data, occs); - /* Note that we explicitly call the weekly version of the - BYDAY expansion filter. */ - occs = cal_obj_byday_expand_weekly (recur_data, occs); - - occs_arrays[num_occs_arrays++] = occs; - } - - if (recur->byyearday) { - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - - occs = (*vtable->byyearday_filter) (recur_data, occs); - - occs_arrays[num_occs_arrays++] = occs; - } - - /* If BYMONTHDAY is set, and BYMONTH is not set, we need to - expand BYMONTHDAY independantly. */ - if (recur->bymonthday && !recur->bymonth) { - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - - occs = (*vtable->bymonthday_filter) (recur_data, occs); - - occs_arrays[num_occs_arrays++] = occs; - } - - /* If BYDAY is set, and BYMONTH and BYWEEKNO are not set, we need to - expand BYDAY independantly. */ - if (recur->byday && !recur->bymonth && !recur->byweekno) { - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - - occs = (*vtable->byday_filter) (recur_data, occs); - - occs_arrays[num_occs_arrays++] = occs; - } - - /* Add all the arrays together. If no filters were used we just - create an array with one element. */ - if (num_occs_arrays > 0) { - occs = occs_arrays[0]; - for (i = 1; i < num_occs_arrays; i++) { - occs2 = occs_arrays[i]; - g_array_append_vals (occs, occs2->data, occs2->len); - g_array_free (occs2, TRUE); - } - } else { - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - } - - /* Now expand BYHOUR, BYMINUTE & BYSECOND. */ - occs = (*vtable->byhour_filter) (recur_data, occs); - occs = (*vtable->byminute_filter) (recur_data, occs); - occs = (*vtable->bysecond_filter) (recur_data, occs); - - return occs; -} - - -static GArray* -cal_obj_generate_set_monthly (RecurData *recur_data, - CalRecurVTable *vtable, - CalObjTime *occ) -{ - GArray *occs, *occs2; - - /* We start with just the one time in each set. */ - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - - occs = (*vtable->bymonth_filter) (recur_data, occs); - - /* We need to combine the output of BYMONTHDAY & BYDAY, by doing them - in parallel rather than sequentially. If we did them sequentially - then we would lose the occurrences generated by BYMONTHDAY, and - instead have repetitions of the occurrences from BYDAY. */ - if (recur_data->recur->bymonthday && recur_data->recur->byday) { - occs2 = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs2, occs->data, occs->len); - - occs = (*vtable->bymonthday_filter) (recur_data, occs); - occs2 = (*vtable->byday_filter) (recur_data, occs2); - - g_array_append_vals (occs, occs2->data, occs2->len); - g_array_free (occs2, TRUE); - } else { - occs = (*vtable->bymonthday_filter) (recur_data, occs); - occs = (*vtable->byday_filter) (recur_data, occs); - } - - occs = (*vtable->byhour_filter) (recur_data, occs); - occs = (*vtable->byminute_filter) (recur_data, occs); - occs = (*vtable->bysecond_filter) (recur_data, occs); - - return occs; -} - - -static GArray* -cal_obj_generate_set_default (RecurData *recur_data, - CalRecurVTable *vtable, - CalObjTime *occ) -{ - GArray *occs; - -#if 0 - g_print ("Generating set for %i/%i/%i %02i:%02i:%02i\n", - occ->day, occ->month + 1, occ->year, occ->hour, occ->minute, - occ->second); -#endif - - /* We start with just the one time in the set. */ - occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - g_array_append_vals (occs, occ, 1); - - occs = (*vtable->bymonth_filter) (recur_data, occs); - if (vtable->byweekno_filter) - occs = (*vtable->byweekno_filter) (recur_data, occs); - if (vtable->byyearday_filter) - occs = (*vtable->byyearday_filter) (recur_data, occs); - if (vtable->bymonthday_filter) - occs = (*vtable->bymonthday_filter) (recur_data, occs); - occs = (*vtable->byday_filter) (recur_data, occs); - - occs = (*vtable->byhour_filter) (recur_data, occs); - occs = (*vtable->byminute_filter) (recur_data, occs); - occs = (*vtable->bysecond_filter) (recur_data, occs); - - return occs; -} - - - -/* Returns the function table corresponding to the recurrence frequency. */ -static CalRecurVTable* cal_obj_get_vtable (icalrecurrencetype_frequency recur_type) -{ - CalRecurVTable* vtable; - - switch (recur_type) { - case ICAL_YEARLY_RECURRENCE: - vtable = &cal_obj_yearly_vtable; - break; - case ICAL_MONTHLY_RECURRENCE: - vtable = &cal_obj_monthly_vtable; - break; - case ICAL_WEEKLY_RECURRENCE: - vtable = &cal_obj_weekly_vtable; - break; - case ICAL_DAILY_RECURRENCE: - vtable = &cal_obj_daily_vtable; - break; - case ICAL_HOURLY_RECURRENCE: - vtable = &cal_obj_hourly_vtable; - break; - case ICAL_MINUTELY_RECURRENCE: - vtable = &cal_obj_minutely_vtable; - break; - case ICAL_SECONDLY_RECURRENCE: - vtable = &cal_obj_secondly_vtable; - break; - default: - g_warning ("Unknown recurrence frequenct"); - vtable = NULL; - } - - return vtable; -} - - -/* This creates a number of fast lookup tables used when filtering with the - modifier properties BYMONTH, BYYEARDAY etc. */ -static void -cal_obj_initialize_recur_data (RecurData *recur_data, - CalRecurrence *recur, - CalObjTime *event_start) -{ - GList *elem; - gint month, yearday, monthday, weekday, week_num, hour, minute, second; - - /* Clear the entire RecurData. */ - memset (recur_data, 0, sizeof (RecurData)); - - recur_data->recur = recur; - - /* Set the weekday, used for the WEEKLY frequency and the BYWEEKNO - modifier. */ - recur_data->weekday_offset = cal_obj_time_weekday_offset (event_start, - recur); - - /* Create an array of months from bymonths for fast lookup. */ - elem = recur->bymonth; - while (elem) { - month = GPOINTER_TO_INT (elem->data); - recur_data->months[month] = 1; - elem = elem->next; - } - - /* Create an array of yeardays from byyearday for fast lookup. - We create a second array to handle the negative values. The first - element there corresponds to the last day of the year. */ - elem = recur->byyearday; - while (elem) { - yearday = GPOINTER_TO_INT (elem->data); - if (yearday >= 0) - recur_data->yeardays[yearday] = 1; - else - recur_data->neg_yeardays[-yearday] = 1; - elem = elem->next; - } - - /* Create an array of monthdays from bymonthday for fast lookup. - We create a second array to handle the negative values. The first - element there corresponds to the last day of the month. */ - elem = recur->bymonthday; - while (elem) { - monthday = GPOINTER_TO_INT (elem->data); - if (monthday >= 0) - recur_data->monthdays[monthday] = 1; - else - recur_data->neg_monthdays[-monthday] = 1; - elem = elem->next; - } - - /* Create an array of weekdays from byday for fast lookup. */ - elem = recur->byday; - while (elem) { - weekday = GPOINTER_TO_INT (elem->data); - elem = elem->next; - /* The week number is not used when filtering. */ - week_num = GPOINTER_TO_INT (elem->data); - elem = elem->next; - - recur_data->weekdays[weekday] = 1; - } - - /* Create an array of hours from byhour for fast lookup. */ - elem = recur->byhour; - while (elem) { - hour = GPOINTER_TO_INT (elem->data); - recur_data->hours[hour] = 1; - elem = elem->next; - } - - /* Create an array of minutes from byminutes for fast lookup. */ - elem = recur->byminute; - while (elem) { - minute = GPOINTER_TO_INT (elem->data); - recur_data->minutes[minute] = 1; - elem = elem->next; - } - - /* Create an array of seconds from byseconds for fast lookup. */ - elem = recur->bysecond; - while (elem) { - second = GPOINTER_TO_INT (elem->data); - recur_data->seconds[second] = 1; - elem = elem->next; - } -} - - -static void -cal_obj_sort_occurrences (GArray *occs) -{ - qsort (occs->data, occs->len, sizeof (CalObjTime), - cal_obj_time_compare_func); -} - - -static void -cal_obj_remove_duplicates_and_invalid_dates (GArray *occs) -{ - static const int days_in_month[12] = { - 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 - }; - - CalObjTime *occ, *prev_occ = NULL; - gint len, i, j = 0, year, month, days; - gboolean keep_occ; - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - keep_occ = TRUE; - - if (prev_occ && cal_obj_time_compare_func (occ, - prev_occ) == 0) - keep_occ = FALSE; - - year = occ->year; - month = occ->month; - days = days_in_month[occ->month]; - /* If it is february and a leap year, add a day. */ - if (month == 1 && (year % 4 == 0 - && (year % 100 != 0 - || year % 400 == 0))) - days++; - if (occ->day > days) - keep_occ = FALSE; - - if (keep_occ) { - if (i != j) - g_array_index (occs, CalObjTime, j) - = g_array_index (occs, CalObjTime, i); - j++; - } - - prev_occ = occ; - } - - g_array_set_size (occs, j); -} - - -/* Removes the exceptions from the ex_occs array from the occurrences in the - occs array, and removes any duplicates. Both arrays are sorted. */ -static void -cal_obj_remove_exceptions (GArray *occs, - GArray *ex_occs) -{ - CalObjTime *occ, *prev_occ = NULL, *ex_occ = NULL, *last_occ_kept; - gint i, j = 0, cmp, ex_index, occs_len, ex_occs_len; - gboolean keep_occ, current_time_is_exception = FALSE; - - if (occs->len == 0) - return; - - ex_index = 0; - occs_len = occs->len; - ex_occs_len = ex_occs->len; - - if (ex_occs_len > 0) - ex_occ = &g_array_index (ex_occs, CalObjTime, ex_index); - - for (i = 0; i < occs_len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - keep_occ = TRUE; - - /* If the occurrence is a duplicate of the previous one, skip - it. */ - if (prev_occ - && cal_obj_time_compare_func (occ, prev_occ) == 0) { - keep_occ = FALSE; - - /* If this occurrence is an RDATE, and the previous - occurrence in the array was kept, set the RDATE flag - of the last one, so we still use the end date - or duration. */ - if (occ->is_rdate && !current_time_is_exception) { - last_occ_kept = &g_array_index (occs, - CalObjTime, - j - 1); - last_occ_kept->is_rdate = TRUE; - } - } else { - /* We've found a new occurrence time. Reset the flag - to indicate that it hasn't been found in the - exceptions array (yet). */ - current_time_is_exception = FALSE; - - if (ex_occ) { - /* Step through the exceptions until we come - to one that matches or follows this - occurrence. */ - while (ex_occ) { - cmp = cal_obj_time_compare_func (ex_occ, occ); - /* I'm pretty sure this is wrong. */ - /*cmp = cal_obj_date_only_compare_func (ex_occ, occ);*/ - if (cmp > 0) - break; - - /* Move to the next exception, or set - ex_occ to NULL when we reach the - end of array. */ - ex_index++; - if (ex_index < ex_occs_len) - ex_occ = &g_array_index (ex_occs, CalObjTime, ex_index); - else - ex_occ = NULL; - - /* If the exception did match this - occurrence we remove it, and set the - flag to indicate that the current - time is an exception. */ - if (cmp == 0) { - current_time_is_exception = TRUE; - keep_occ = FALSE; - break; - } - } - } - } - - if (keep_occ) { - if (i != j) - g_array_index (occs, CalObjTime, j) - = g_array_index (occs, CalObjTime, i); - j++; - } - - prev_occ = occ; - } - - g_array_set_size (occs, j); -} - - - -static GArray* -cal_obj_bysetpos_filter (CalRecurrence *recur, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, pos; - - /* If BYSETPOS has not been specified, or the array is empty, just - return the array. */ - elem = recur->bysetpos; - if (!elem || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - /* Iterate over the indices given in bysetpos, adding the corresponding - element from occs to new_occs. */ - len = occs->len; - while (elem) { - pos = GPOINTER_TO_INT (elem->data); - - /* Negative values count back from the end of the array. */ - if (pos < 0) - pos += len; - - if (pos >= 0 && pos < len) { - occ = &g_array_index (occs, CalObjTime, pos); - g_array_append_vals (new_occs, occ, 1); - } - elem = elem->next; - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - - -/* Finds the first year from the event_start, counting in multiples of the - recurrence interval, that intersects the given interval. It returns TRUE - if there is no intersection. */ -static gboolean -cal_obj_yearly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime) -{ - *cotime = *event_start; - - /* Move on to the next interval, if the event starts before the - given interval. */ - if (cotime->year < interval_start->year) { - gint years = interval_start->year - cotime->year - + recur_data->recur->interval - 1; - years -= years % recur_data->recur->interval; - /* NOTE: The day may now be invalid, e.g. 29th Feb. */ - cotime->year += years; - } - - if ((event_end && cotime->year > event_end->year) - || (interval_end && cotime->year > interval_end->year)) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_yearly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end) -{ - /* NOTE: The day may now be invalid, e.g. 29th Feb. */ - cotime->year += recur_data->recur->interval; - - if ((event_end && cotime->year > event_end->year) - || (interval_end && cotime->year > interval_end->year)) - return TRUE; - - return FALSE; -} - - - -static gboolean -cal_obj_monthly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime) -{ - *cotime = *event_start; - - /* Move on to the next interval, if the event starts before the - given interval. */ - if (cal_obj_time_compare (cotime, interval_start, CALOBJ_MONTH) < 0) { - gint months = (interval_start->year - cotime->year) * 12 - + interval_start->month - cotime->month - + recur_data->recur->interval - 1; - months -= months % recur_data->recur->interval; - /* NOTE: The day may now be invalid, e.g. 31st Sep. */ - cal_obj_time_add_months (cotime, months); - } - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_MONTH) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_MONTH) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_monthly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end) -{ - /* NOTE: The day may now be invalid, e.g. 31st Sep. */ - cal_obj_time_add_months (cotime, recur_data->recur->interval); - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_MONTH) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_MONTH) > 0) - return TRUE; - - return FALSE; -} - - - -static gboolean -cal_obj_weekly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime) -{ - GDate event_start_date, interval_start_date; - guint32 event_start_julian, interval_start_julian; - gint interval_start_weekday_offset; - CalObjTime week_start; - - if (event_end && cal_obj_time_compare (event_end, interval_start, - CALOBJ_DAY) < 0) - return TRUE; - if (interval_end && cal_obj_time_compare (event_start, interval_end, - CALOBJ_DAY) > 0) - return TRUE; - - *cotime = *event_start; - - /* Convert the event start and interval start to GDates, so we can - easily find the number of days between them. */ - g_date_clear (&event_start_date, 1); - g_date_set_dmy (&event_start_date, event_start->day, - event_start->month + 1, event_start->year); - g_date_clear (&interval_start_date, 1); - g_date_set_dmy (&interval_start_date, interval_start->day, - interval_start->month + 1, interval_start->year); - - /* Calculate the start of the weeks corresponding to the event start - and interval start. */ - event_start_julian = g_date_julian (&event_start_date); - event_start_julian -= recur_data->weekday_offset; - - interval_start_julian = g_date_julian (&interval_start_date); - interval_start_weekday_offset = cal_obj_time_weekday_offset (interval_start, recur_data->recur); - interval_start_julian -= interval_start_weekday_offset; - - /* We want to find the first full week using the recurrence interval - that intersects the given interval dates. */ - if (event_start_julian < interval_start_julian) { - gint weeks = (interval_start_julian - event_start_julian) / 7; - weeks += recur_data->recur->interval - 1; - weeks -= weeks % recur_data->recur->interval; - cal_obj_time_add_days (cotime, weeks * 7); - } - - week_start = *cotime; - cal_obj_time_add_days (&week_start, -recur_data->weekday_offset); - - if (event_end && cal_obj_time_compare (&week_start, event_end, - CALOBJ_DAY) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (&week_start, interval_end, - CALOBJ_DAY) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_weekly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end) -{ - CalObjTime week_start; - - cal_obj_time_add_days (cotime, recur_data->recur->interval * 7); - - /* Return TRUE if the start of this week is after the event finishes - or is after the end of the required interval. */ - week_start = *cotime; - cal_obj_time_add_days (&week_start, -recur_data->weekday_offset); - -#ifdef CAL_OBJ_DEBUG - g_print ("Next day: %s\n", cal_obj_time_to_string (cotime)); - g_print ("Week Start: %s\n", cal_obj_time_to_string (&week_start)); -#endif - - if (event_end && cal_obj_time_compare (&week_start, event_end, - CALOBJ_DAY) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (&week_start, interval_end, - CALOBJ_DAY) > 0) { -#ifdef CAL_OBJ_DEBUG - g_print ("Interval end reached: %s\n", - cal_obj_time_to_string (interval_end)); -#endif - return TRUE; - } - - return FALSE; -} - - -static gboolean -cal_obj_daily_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime) -{ - GDate event_start_date, interval_start_date; - guint32 event_start_julian, interval_start_julian, days; - - if (interval_end && cal_obj_time_compare (event_start, interval_end, - CALOBJ_DAY) > 0) - return TRUE; - if (event_end && cal_obj_time_compare (event_end, interval_start, - CALOBJ_DAY) < 0) - return TRUE; - - *cotime = *event_start; - - /* Convert the event start and interval start to GDates, so we can - easily find the number of days between them. */ - g_date_clear (&event_start_date, 1); - g_date_set_dmy (&event_start_date, event_start->day, - event_start->month + 1, event_start->year); - g_date_clear (&interval_start_date, 1); - g_date_set_dmy (&interval_start_date, interval_start->day, - interval_start->month + 1, interval_start->year); - - event_start_julian = g_date_julian (&event_start_date); - interval_start_julian = g_date_julian (&interval_start_date); - - if (event_start_julian < interval_start_julian) { - days = interval_start_julian - event_start_julian - + recur_data->recur->interval - 1; - days -= days % recur_data->recur->interval; - cal_obj_time_add_days (cotime, days); - } - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_DAY) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_DAY) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_daily_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end) -{ - cal_obj_time_add_days (cotime, recur_data->recur->interval); - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_DAY) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_DAY) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_hourly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime) -{ - GDate event_start_date, interval_start_date; - guint32 event_start_julian, interval_start_julian, hours; - - if (interval_end && cal_obj_time_compare (event_start, interval_end, - CALOBJ_HOUR) > 0) - return TRUE; - if (event_end && cal_obj_time_compare (event_end, interval_start, - CALOBJ_HOUR) < 0) - return TRUE; - - *cotime = *event_start; - - if (cal_obj_time_compare (event_start, interval_start, - CALOBJ_HOUR) < 0) { - /* Convert the event start and interval start to GDates, so we - can easily find the number of days between them. */ - g_date_clear (&event_start_date, 1); - g_date_set_dmy (&event_start_date, event_start->day, - event_start->month + 1, event_start->year); - g_date_clear (&interval_start_date, 1); - g_date_set_dmy (&interval_start_date, interval_start->day, - interval_start->month + 1, - interval_start->year); - - event_start_julian = g_date_julian (&event_start_date); - interval_start_julian = g_date_julian (&interval_start_date); - - hours = (interval_start_julian - event_start_julian) * 24; - hours += interval_start->hour - event_start->hour; - hours += recur_data->recur->interval - 1; - hours -= hours % recur_data->recur->interval; - cal_obj_time_add_hours (cotime, hours); - } - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_HOUR) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_HOUR) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_hourly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end) -{ - cal_obj_time_add_hours (cotime, recur_data->recur->interval); - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_HOUR) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_HOUR) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_minutely_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime) -{ - GDate event_start_date, interval_start_date; - guint32 event_start_julian, interval_start_julian, minutes; - - if (interval_end && cal_obj_time_compare (event_start, interval_end, - CALOBJ_MINUTE) > 0) - return TRUE; - if (event_end && cal_obj_time_compare (event_end, interval_start, - CALOBJ_MINUTE) < 0) - return TRUE; - - *cotime = *event_start; - - if (cal_obj_time_compare (event_start, interval_start, - CALOBJ_MINUTE) < 0) { - /* Convert the event start and interval start to GDates, so we - can easily find the number of days between them. */ - g_date_clear (&event_start_date, 1); - g_date_set_dmy (&event_start_date, event_start->day, - event_start->month + 1, event_start->year); - g_date_clear (&interval_start_date, 1); - g_date_set_dmy (&interval_start_date, interval_start->day, - interval_start->month + 1, - interval_start->year); - - event_start_julian = g_date_julian (&event_start_date); - interval_start_julian = g_date_julian (&interval_start_date); - - minutes = (interval_start_julian - event_start_julian) - * 24 * 60; - minutes += (interval_start->hour - event_start->hour) * 24; - minutes += interval_start->minute - event_start->minute; - minutes += recur_data->recur->interval - 1; - minutes -= minutes % recur_data->recur->interval; - cal_obj_time_add_minutes (cotime, minutes); - } - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_MINUTE) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_MINUTE) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_minutely_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end) -{ - cal_obj_time_add_minutes (cotime, recur_data->recur->interval); - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_MINUTE) > 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_MINUTE) > 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_secondly_find_start_position (CalObjTime *event_start, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_start, - CalObjTime *interval_end, - CalObjTime *cotime) -{ - GDate event_start_date, interval_start_date; - guint32 event_start_julian, interval_start_julian, seconds; - - if (interval_end && cal_obj_time_compare (event_start, interval_end, - CALOBJ_SECOND) > 0) - return TRUE; - if (event_end && cal_obj_time_compare (event_end, interval_start, - CALOBJ_SECOND) < 0) - return TRUE; - - *cotime = *event_start; - - if (cal_obj_time_compare (event_start, interval_start, - CALOBJ_SECOND) < 0) { - /* Convert the event start and interval start to GDates, so we - can easily find the number of days between them. */ - g_date_clear (&event_start_date, 1); - g_date_set_dmy (&event_start_date, event_start->day, - event_start->month + 1, event_start->year); - g_date_clear (&interval_start_date, 1); - g_date_set_dmy (&interval_start_date, interval_start->day, - interval_start->month + 1, - interval_start->year); - - event_start_julian = g_date_julian (&event_start_date); - interval_start_julian = g_date_julian (&interval_start_date); - - seconds = (interval_start_julian - event_start_julian) - * 24 * 60 * 60; - seconds += (interval_start->hour - event_start->hour) - * 24 * 60; - seconds += (interval_start->minute - event_start->minute) * 60; - seconds += interval_start->second - event_start->second; - seconds += recur_data->recur->interval - 1; - seconds -= seconds % recur_data->recur->interval; - cal_obj_time_add_seconds (cotime, seconds); - } - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_SECOND) >= 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_SECOND) >= 0) - return TRUE; - - return FALSE; -} - - -static gboolean -cal_obj_secondly_find_next_position (CalObjTime *cotime, - CalObjTime *event_end, - RecurData *recur_data, - CalObjTime *interval_end) -{ - cal_obj_time_add_seconds (cotime, recur_data->recur->interval); - - if (event_end && cal_obj_time_compare (cotime, event_end, - CALOBJ_SECOND) >= 0) - return TRUE; - if (interval_end && cal_obj_time_compare (cotime, interval_end, - CALOBJ_SECOND) >= 0) - return TRUE; - - return FALSE; -} - - - - - -/* If the BYMONTH rule is specified it expands each occurrence in occs, by - using each of the months in the bymonth list. */ -static GArray* -cal_obj_bymonth_expand (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, i; - - /* If BYMONTH has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->bymonth || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - elem = recur_data->recur->bymonth; - while (elem) { - /* NOTE: The day may now be invalid, e.g. 31st Feb. */ - occ->month = GPOINTER_TO_INT (elem->data); - g_array_append_vals (new_occs, occ, 1); - elem = elem->next; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -/* If the BYMONTH rule is specified it filters out all occurrences in occs - which do not match one of the months in the bymonth list. */ -static GArray* -cal_obj_bymonth_filter (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - gint len, i; - - /* If BYMONTH has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->bymonth || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - if (recur_data->months[occ->month]) - g_array_append_vals (new_occs, occ, 1); - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - -static GArray* -cal_obj_byweekno_expand (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ, year_start_cotime, year_end_cotime, cotime; - GList *elem; - gint len, i, weekno; - - /* If BYWEEKNO has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byweekno || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - /* Find the day that would correspond to week 1 (note that - week 1 is the first week starting from the specified week - start day that has 4 days in the new year). */ - year_start_cotime = *occ; - cal_obj_time_find_first_week (&year_start_cotime, - recur_data); - - /* Find the day that would correspond to week 1 of the next - year, which we use for -ve week numbers. */ - year_end_cotime = *occ; - year_end_cotime.year++; - cal_obj_time_find_first_week (&year_end_cotime, - recur_data); - - /* Now iterate over the week numbers in byweekno, generating a - new occurrence for each one. */ - elem = recur_data->recur->byweekno; - while (elem) { - weekno = GPOINTER_TO_INT (elem->data); - if (weekno > 0) { - cotime = year_start_cotime; - cal_obj_time_add_days (&cotime, - (weekno - 1) * 7); - } else { - cotime = year_end_cotime; - cal_obj_time_add_days (&cotime, weekno * 7); - } - - /* Skip occurrences if they fall outside the year. */ - if (cotime.year == occ->year) - g_array_append_val (new_occs, cotime); - elem = elem->next; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -#if 0 -/* This isn't used at present. */ -static GArray* -cal_obj_byweekno_filter (RecurData *recur_data, - GArray *occs) -{ - - return occs; -} -#endif - - -static GArray* -cal_obj_byyearday_expand (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ, year_start_cotime, year_end_cotime, cotime; - GList *elem; - gint len, i, dayno; - - /* If BYYEARDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byyearday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - /* Find the day that would correspond to day 1. */ - year_start_cotime = *occ; - year_start_cotime.month = 0; - year_start_cotime.day = 1; - - /* Find the day that would correspond to day 1 of the next - year, which we use for -ve day numbers. */ - year_end_cotime = *occ; - year_end_cotime.year++; - year_end_cotime.month = 0; - year_end_cotime.day = 1; - - /* Now iterate over the day numbers in byyearday, generating a - new occurrence for each one. */ - elem = recur_data->recur->byyearday; - while (elem) { - dayno = GPOINTER_TO_INT (elem->data); - if (dayno > 0) { - cotime = year_start_cotime; - cal_obj_time_add_days (&cotime, dayno - 1); - } else { - cotime = year_end_cotime; - cal_obj_time_add_days (&cotime, dayno); - } - - /* Skip occurrences if they fall outside the year. */ - if (cotime.year == occ->year) - g_array_append_val (new_occs, cotime); - elem = elem->next; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -/* Note: occs must not contain invalid dates, e.g. 31st September. */ -static GArray* -cal_obj_byyearday_filter (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - gint yearday, len, i, days_in_year; - - /* If BYYEARDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byyearday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - yearday = cal_obj_time_day_of_year (occ); - if (recur_data->yeardays[yearday]) { - g_array_append_vals (new_occs, occ, 1); - } else { - days_in_year = g_date_is_leap_year (occ->year) - ? 366 : 365; - if (recur_data->neg_yeardays[days_in_year + 1 - - yearday]) - g_array_append_vals (new_occs, occ, 1); - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - -static GArray* -cal_obj_bymonthday_expand (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ, month_start_cotime, month_end_cotime, cotime; - GList *elem; - gint len, i, dayno; - - /* If BYMONTHDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->bymonthday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - /* Find the day that would correspond to day 1. */ - month_start_cotime = *occ; - month_start_cotime.day = 1; - - /* Find the day that would correspond to day 1 of the next - month, which we use for -ve day numbers. */ - month_end_cotime = *occ; - month_end_cotime.month++; - month_end_cotime.day = 1; - - /* Now iterate over the day numbers in bymonthday, generating a - new occurrence for each one. */ - elem = recur_data->recur->bymonthday; - while (elem) { - dayno = GPOINTER_TO_INT (elem->data); - if (dayno > 0) { - cotime = month_start_cotime; - cal_obj_time_add_days (&cotime, dayno - 1); - } else { - cotime = month_end_cotime; - cal_obj_time_add_days (&cotime, dayno); - } - - /* Skip occurrences if they fall outside the month. */ - if (cotime.month == occ->month) - g_array_append_val (new_occs, cotime); - elem = elem->next; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -static GArray* -cal_obj_bymonthday_filter (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - gint len, i, days_in_month; - - /* If BYMONTHDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->bymonthday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - if (recur_data->monthdays[occ->day]) { - g_array_append_vals (new_occs, occ, 1); - } else { - days_in_month = time_days_in_month (occ->year, - occ->month); - if (recur_data->neg_monthdays[days_in_month + 1 - - occ->day]) - g_array_append_vals (new_occs, occ, 1); - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - -static GArray* -cal_obj_byday_expand_yearly (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, i, weekday, week_num; - gint first_weekday, last_weekday, offset; - guint16 year; - - /* If BYDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - elem = recur_data->recur->byday; - while (elem) { - weekday = GPOINTER_TO_INT (elem->data); - elem = elem->next; - week_num = GPOINTER_TO_INT (elem->data); - elem = elem->next; - - year = occ->year; - if (week_num == 0) { - /* Expand to every Mon/Tue/etc. in the year. */ - occ->month = 0; - occ->day = 1; - first_weekday = cal_obj_time_weekday (occ); - offset = (weekday + 7 - first_weekday) % 7; - cal_obj_time_add_days (occ, offset); - - while (occ->year == year) { - g_array_append_vals (new_occs, occ, 1); - cal_obj_time_add_days (occ, 7); - } - - } else if (week_num > 0) { - /* Add the nth Mon/Tue/etc. in the year. */ - occ->month = 0; - occ->day = 1; - first_weekday = cal_obj_time_weekday (occ); - offset = (weekday + 7 - first_weekday) % 7; - offset += (week_num - 1) * 7; - cal_obj_time_add_days (occ, offset); - if (occ->year == year) - g_array_append_vals (new_occs, occ, 1); - - } else { - /* Add the -nth Mon/Tue/etc. in the year. */ - occ->month = 11; - occ->day = 31; - last_weekday = cal_obj_time_weekday (occ); - offset = (last_weekday + 7 - weekday) % 7; - offset += (week_num - 1) * 7; - cal_obj_time_add_days (occ, -offset); - if (occ->year == year) - g_array_append_vals (new_occs, occ, 1); - } - - /* Reset the year, as we may have gone past the end. */ - occ->year = year; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -static GArray* -cal_obj_byday_expand_monthly (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, i, weekday, week_num; - gint first_weekday, last_weekday, offset; - guint16 year; - guint8 month; - - /* If BYDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - elem = recur_data->recur->byday; - while (elem) { - weekday = GPOINTER_TO_INT (elem->data); - elem = elem->next; - week_num = GPOINTER_TO_INT (elem->data); - elem = elem->next; - - year = occ->year; - month = occ->month; - if (week_num == 0) { - /* Expand to every Mon/Tue/etc. in the month.*/ - occ->day = 1; - first_weekday = cal_obj_time_weekday (occ); - offset = (weekday + 7 - first_weekday) % 7; - cal_obj_time_add_days (occ, offset); - - while (occ->year == year - && occ->month == month) { - g_array_append_vals (new_occs, occ, 1); - cal_obj_time_add_days (occ, 7); - } - - } else if (week_num > 0) { - /* Add the nth Mon/Tue/etc. in the month. */ - occ->day = 1; - first_weekday = cal_obj_time_weekday (occ); - offset = (weekday + 7 - first_weekday) % 7; - offset += (week_num - 1) * 7; - cal_obj_time_add_days (occ, offset); - if (occ->year == year && occ->month == month) - g_array_append_vals (new_occs, occ, 1); - - } else { - /* Add the -nth Mon/Tue/etc. in the month. */ - occ->day = time_days_in_month (occ->year, - occ->month); - last_weekday = cal_obj_time_weekday (occ); - offset = (last_weekday + 7 - weekday) % 7; - offset += (week_num - 1) * 7; - cal_obj_time_add_days (occ, -offset); - if (occ->year == year && occ->month == month) - g_array_append_vals (new_occs, occ, 1); - } - - /* Reset the year & month, as we may have gone past - the end. */ - occ->year = year; - occ->month = month; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -/* Note: occs must not contain invalid dates, e.g. 31st September. */ -static GArray* -cal_obj_byday_expand_weekly (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, i, weekday, week_num; - gint weekday_offset, new_weekday_offset; - - /* If BYDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - elem = recur_data->recur->byday; - while (elem) { - weekday = GPOINTER_TO_INT (elem->data); - elem = elem->next; - - /* FIXME: Currently we just ignore this, but maybe we - should skip all elements where week_num != 0. - The spec isn't clear about this. */ - week_num = GPOINTER_TO_INT (elem->data); - elem = elem->next; - - weekday_offset = cal_obj_time_weekday_offset (occ, recur_data->recur); - new_weekday_offset = (weekday + 7 - recur_data->recur->week_start_day) % 7; - cal_obj_time_add_days (occ, new_weekday_offset - weekday_offset); - g_array_append_vals (new_occs, occ, 1); - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -/* Note: occs must not contain invalid dates, e.g. 31st September. */ -static GArray* -cal_obj_byday_filter (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - gint len, i, weekday; - - /* If BYDAY has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byday || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - weekday = cal_obj_time_weekday (occ); - - /* See if the weekday on its own is set. */ - if (recur_data->weekdays[weekday]) - g_array_append_vals (new_occs, occ, 1); - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - -/* If the BYHOUR rule is specified it expands each occurrence in occs, by - using each of the hours in the byhour list. */ -static GArray* -cal_obj_byhour_expand (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, i; - - /* If BYHOUR has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byhour || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - elem = recur_data->recur->byhour; - while (elem) { - occ->hour = GPOINTER_TO_INT (elem->data); - g_array_append_vals (new_occs, occ, 1); - elem = elem->next; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -/* If the BYHOUR rule is specified it filters out all occurrences in occs - which do not match one of the hours in the byhour list. */ -static GArray* -cal_obj_byhour_filter (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - gint len, i; - - /* If BYHOUR has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byhour || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - if (recur_data->hours[occ->hour]) - g_array_append_vals (new_occs, occ, 1); - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - -/* If the BYMINUTE rule is specified it expands each occurrence in occs, by - using each of the minutes in the byminute list. */ -static GArray* -cal_obj_byminute_expand (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, i; - - /* If BYMINUTE has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byminute || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - elem = recur_data->recur->byminute; - while (elem) { - occ->minute = GPOINTER_TO_INT (elem->data); - g_array_append_vals (new_occs, occ, 1); - elem = elem->next; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -/* If the BYMINUTE rule is specified it filters out all occurrences in occs - which do not match one of the minutes in the byminute list. */ -static GArray* -cal_obj_byminute_filter (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - gint len, i; - - /* If BYMINUTE has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->byminute || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - if (recur_data->minutes[occ->minute]) - g_array_append_vals (new_occs, occ, 1); - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - -/* If the BYSECOND rule is specified it expands each occurrence in occs, by - using each of the seconds in the bysecond list. */ -static GArray* -cal_obj_bysecond_expand (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - GList *elem; - gint len, i; - - /* If BYSECOND has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->bysecond || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - - elem = recur_data->recur->bysecond; - while (elem) { - occ->second = GPOINTER_TO_INT (elem->data); - g_array_append_vals (new_occs, occ, 1); - elem = elem->next; - } - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - -/* If the BYSECOND rule is specified it filters out all occurrences in occs - which do not match one of the seconds in the bysecond list. */ -static GArray* -cal_obj_bysecond_filter (RecurData *recur_data, - GArray *occs) -{ - GArray *new_occs; - CalObjTime *occ; - gint len, i; - - /* If BYSECOND has not been specified, or the array is empty, just - return the array. */ - if (!recur_data->recur->bysecond || occs->len == 0) - return occs; - - new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime)); - - len = occs->len; - for (i = 0; i < len; i++) { - occ = &g_array_index (occs, CalObjTime, i); - if (recur_data->seconds[occ->second]) - g_array_append_vals (new_occs, occ, 1); - } - - g_array_free (occs, TRUE); - - return new_occs; -} - - - - - -/* Adds a positive or negative number of months to the given CalObjTime, - updating the year appropriately so we end up with a valid month. - Note that the day may be invalid, e.g. 30th Feb. */ -static void -cal_obj_time_add_months (CalObjTime *cotime, - gint months) -{ - guint month, years; - - /* We use a guint to avoid overflow on the guint8. */ - month = cotime->month + months; - cotime->month = month % 12; - if (month > 0) { - cotime->year += month / 12; - } else { - years = month / 12; - if (cotime->month != 0) { - cotime->month += 12; - years -= 1; - } - cotime->year += years; - } -} - - -/* Adds a positive or negative number of days to the given CalObjTime, - updating the month and year appropriately so we end up with a valid day. */ -static void -cal_obj_time_add_days (CalObjTime *cotime, - gint days) -{ - gint day, days_in_month; - - /* We use a guint to avoid overflow on the guint8. */ - day = cotime->day; - day += days; - - if (days >= 0) { - for (;;) { - days_in_month = time_days_in_month (cotime->year, - cotime->month); - if (day <= days_in_month) - break; - - cotime->month++; - if (cotime->month >= 12) { - cotime->year++; - cotime->month = 0; - } - - day -= days_in_month; - } - - cotime->day = (guint8) day; - } else { - while (day <= 0) { - if (cotime->month == 0) { - cotime->year--; - cotime->month = 11; - } else { - cotime->month--; - } - - days_in_month = time_days_in_month (cotime->year, - cotime->month); - day += days_in_month; - } - - cotime->day = (guint8) day; - } -} - - -/* Adds a positive or negative number of hours to the given CalObjTime, - updating the day, month & year appropriately so we end up with a valid - time. */ -static void -cal_obj_time_add_hours (CalObjTime *cotime, - gint hours) -{ - gint hour, days; - - /* We use a gint to avoid overflow on the guint8. */ - hour = cotime->hour + hours; - cotime->hour = hour % 24; - if (hour >= 0) { - if (hour >= 24) - cal_obj_time_add_days (cotime, hour / 24); - } else { - days = hour / 24; - if (cotime->hour != 0) { - cotime->hour += 24; - days -= 1; - } - cal_obj_time_add_days (cotime, days); - } -} - - -/* Adds a positive or negative number of minutes to the given CalObjTime, - updating the rest of the CalObjTime appropriately. */ -static void -cal_obj_time_add_minutes (CalObjTime *cotime, - gint minutes) -{ - gint minute, hours; - - /* We use a gint to avoid overflow on the guint8. */ - minute = cotime->minute + minutes; - cotime->minute = minute % 60; - if (minute >= 0) { - if (minute >= 60) - cal_obj_time_add_hours (cotime, minute / 60); - } else { - hours = minute / 60; - if (cotime->minute != 0) { - cotime->minute += 60; - hours -= 1; - } - cal_obj_time_add_hours (cotime, hours); - } -} - - -/* Adds a positive or negative number of seconds to the given CalObjTime, - updating the rest of the CalObjTime appropriately. */ -static void -cal_obj_time_add_seconds (CalObjTime *cotime, - gint seconds) -{ - gint second, minutes; - - /* We use a gint to avoid overflow on the guint8. */ - second = cotime->second + seconds; - cotime->second = second % 60; - if (second >= 0) { - if (second >= 60) - cal_obj_time_add_minutes (cotime, second / 60); - } else { - minutes = second / 60; - if (cotime->second != 0) { - cotime->second += 60; - minutes -= 1; - } - cal_obj_time_add_minutes (cotime, minutes); - } -} - - -/* Compares 2 CalObjTimes. Returns -1 if the cotime1 is before cotime2, 0 if - they are the same, or 1 if cotime1 is after cotime2. The comparison type - specifies which parts of the times we are interested in, e.g. if CALOBJ_DAY - is used we only want to know if the days are different. */ -static gint -cal_obj_time_compare (CalObjTime *cotime1, - CalObjTime *cotime2, - CalObjTimeComparison type) -{ - if (cotime1->year < cotime2->year) - return -1; - if (cotime1->year > cotime2->year) - return 1; - - if (type == CALOBJ_YEAR) - return 0; - - if (cotime1->month < cotime2->month) - return -1; - if (cotime1->month > cotime2->month) - return 1; - - if (type == CALOBJ_MONTH) - return 0; - - if (cotime1->day < cotime2->day) - return -1; - if (cotime1->day > cotime2->day) - return 1; - - if (type == CALOBJ_DAY) - return 0; - - if (cotime1->hour < cotime2->hour) - return -1; - if (cotime1->hour > cotime2->hour) - return 1; - - if (type == CALOBJ_HOUR) - return 0; - - if (cotime1->minute < cotime2->minute) - return -1; - if (cotime1->minute > cotime2->minute) - return 1; - - if (type == CALOBJ_MINUTE) - return 0; - - if (cotime1->second < cotime2->second) - return -1; - if (cotime1->second > cotime2->second) - return 1; - - return 0; -} - - -/* This is the same as the above function, but without the comparison type. - It is used for qsort(). */ -static gint -cal_obj_time_compare_func (const void *arg1, - const void *arg2) -{ - CalObjTime *cotime1, *cotime2; - gint retval; - - cotime1 = (CalObjTime*) arg1; - cotime2 = (CalObjTime*) arg2; - - if (cotime1->year < cotime2->year) - retval = -1; - else if (cotime1->year > cotime2->year) - retval = 1; - - else if (cotime1->month < cotime2->month) - retval = -1; - else if (cotime1->month > cotime2->month) - retval = 1; - - else if (cotime1->day < cotime2->day) - retval = -1; - else if (cotime1->day > cotime2->day) - retval = 1; - - else if (cotime1->hour < cotime2->hour) - retval = -1; - else if (cotime1->hour > cotime2->hour) - retval = 1; - - else if (cotime1->minute < cotime2->minute) - retval = -1; - else if (cotime1->minute > cotime2->minute) - retval = 1; - - else if (cotime1->second < cotime2->second) - retval = -1; - else if (cotime1->second > cotime2->second) - retval = 1; - - else - retval = 0; - -#if 0 - g_print ("%s - ", cal_obj_time_to_string (cotime1)); - g_print ("%s : %i\n", cal_obj_time_to_string (cotime2), retval); -#endif - - return retval; -} - -#if 0 -static gint -cal_obj_date_only_compare_func (const void *arg1, - const void *arg2) -{ - CalObjTime *cotime1, *cotime2; - - cotime1 = (CalObjTime*) arg1; - cotime2 = (CalObjTime*) arg2; - - if (cotime1->year < cotime2->year) - return -1; - if (cotime1->year > cotime2->year) - return 1; - - if (cotime1->month < cotime2->month) - return -1; - if (cotime1->month > cotime2->month) - return 1; - - if (cotime1->day < cotime2->day) - return -1; - if (cotime1->day > cotime2->day) - return 1; - - return 0; -} -#endif - -/* Returns the weekday of the given CalObjTime, from 0 (Mon) - 6 (Sun). */ -static gint -cal_obj_time_weekday (CalObjTime *cotime) -{ - GDate date; - gint weekday; - - g_date_clear (&date, 1); - g_date_set_dmy (&date, cotime->day, cotime->month + 1, cotime->year); - - /* This results in a value of 0 (Monday) - 6 (Sunday). */ - weekday = g_date_weekday (&date) - 1; - - return weekday; -} - - -/* Returns the weekday of the given CalObjTime, from 0 - 6. The week start - day is Monday by default, but can be set in the recurrence rule. */ -static gint -cal_obj_time_weekday_offset (CalObjTime *cotime, - CalRecurrence *recur) -{ - GDate date; - gint weekday, offset; - - g_date_clear (&date, 1); - g_date_set_dmy (&date, cotime->day, cotime->month + 1, cotime->year); - - /* This results in a value of 0 (Monday) - 6 (Sunday). */ - weekday = g_date_weekday (&date) - 1; - - /* This calculates the offset of our day from the start of the week. - We just add on a week (to avoid any possible negative values) and - then subtract the specified week start day, then convert it into a - value from 0-6. */ - offset = (weekday + 7 - recur->week_start_day) % 7; - - return offset; -} - - -/* Returns the day of the year of the given CalObjTime, from 1 - 366. */ -static gint -cal_obj_time_day_of_year (CalObjTime *cotime) -{ - GDate date; - - g_date_clear (&date, 1); - g_date_set_dmy (&date, cotime->day, cotime->month + 1, cotime->year); - - return g_date_day_of_year (&date); -} - - -/* Finds the first week in the given CalObjTime's year, using the same weekday - as the event start day (i.e. from the RecurData). - The first week of the year is the first week starting from the specified - week start day that has 4 days in the new year. It may be in the previous - year. */ -static void -cal_obj_time_find_first_week (CalObjTime *cotime, - RecurData *recur_data) -{ - GDate date; - gint weekday, week_start_day, first_full_week_start_offset, offset; - - /* Find out the weekday of the 1st of the year, 0 (Mon) - 6 (Sun). */ - g_date_clear (&date, 1); - g_date_set_dmy (&date, 1, 1, cotime->year); - weekday = g_date_weekday (&date) - 1; - - /* Calculate the first day of the year that starts a new week, i.e. the - first week_start_day after weekday, using 0 = 1st Jan. - e.g. if the 1st Jan is a Tuesday (1) and week_start_day is a - Monday (0), the result will be (0 + 7 - 1) % 7 = 6 (7th Jan). */ - week_start_day = recur_data->recur->week_start_day; - first_full_week_start_offset = (week_start_day + 7 - weekday) % 7; - - /* Now see if we have to move backwards 1 week, i.e. if the week - starts on or after Jan 5th (since the previous week has 4 days in - this year and so will be the first week of the year). */ - if (first_full_week_start_offset >= 4) - first_full_week_start_offset -= 7; - - /* Now add the days to get to the event's weekday. */ - offset = first_full_week_start_offset + recur_data->weekday_offset; - - /* Now move the cotime to the appropriate day. */ - cotime->month = 0; - cotime->day = 1; - cal_obj_time_add_days (cotime, offset); -} - - -static void -cal_object_time_from_time (CalObjTime *cotime, - time_t t) -{ - struct tm *tmp_tm; - time_t tmp_time_t; - - tmp_time_t = t; - tmp_tm = localtime (&tmp_time_t); - - cotime->year = tmp_tm->tm_year + 1900; - cotime->month = tmp_tm->tm_mon; - cotime->day = tmp_tm->tm_mday; - cotime->hour = tmp_tm->tm_hour; - cotime->minute = tmp_tm->tm_min; - cotime->second = tmp_tm->tm_sec; - cotime->is_rdate = FALSE; -} - - -/* Debugging function to convert a CalObjTime to a string. It uses a static - buffer so beware. */ -#ifdef CAL_OBJ_DEBUG -static char* -cal_obj_time_to_string (CalObjTime *cotime) -{ - static char buffer[20]; - char *weekdays[] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", - " " }; - gint weekday; - - weekday = cal_obj_time_weekday (cotime); - - sprintf (buffer, "%s %02i/%02i/%04i %02i:%02i:%02i", - weekdays[weekday], - cotime->day, cotime->month + 1, cotime->year, - cotime->hour, cotime->minute, cotime->second); - return buffer; -} -#endif - - -/* This recalculates the end dates for recurrence & exception rules which use - the COUNT property. If refresh is TRUE it will recalculate all enddates - for rules which use COUNT. If refresh is FALSE, it will only calculate - the enddate if it hasn't already been set. It returns TRUE if the component - was changed, i.e. if the component should be saved at some point. - We store the enddate in the "X-EVOLUTION-ENDDATE" parameter of the RRULE - or EXRULE. */ -static gboolean -cal_recur_ensure_end_dates (CalComponent *comp, - gboolean refresh) -{ - GSList *rrules, *exrules, *elem; - gboolean changed = FALSE; - - /* Do the RRULEs. */ - cal_component_get_rrule_property_list (comp, &rrules); - for (elem = rrules; elem; elem = elem->next) { - changed |= cal_recur_ensure_rule_end_date (comp, elem->data, - FALSE, refresh); - } - - /* Do the EXRULEs. */ - cal_component_get_exrule_property_list (comp, &exrules); - for (elem = exrules; elem; elem = elem->next) { - changed |= cal_recur_ensure_rule_end_date (comp, elem->data, - TRUE, refresh); - } - - return changed; -} - - -typedef struct _CalRecurEnsureEndDateData CalRecurEnsureEndDateData; -struct _CalRecurEnsureEndDateData { - gint count; - gint instances; - time_t end_date; -}; - - -static gboolean -cal_recur_ensure_rule_end_date (CalComponent *comp, - icalproperty *prop, - gboolean exception, - gboolean refresh) -{ - struct icalrecurrencetype rule; - CalRecurEnsureEndDateData cb_data; - - if (exception) - rule = icalproperty_get_exrule (prop); - else - rule = icalproperty_get_rrule (prop); - - /* If the rule doesn't use COUNT just return. */ - if (rule.count == 0) - return FALSE; - - /* If refresh is FALSE, we check if the enddate is already set, and - if it is we just return. */ - if (!refresh) { - if (cal_recur_get_rule_end_date (prop) != -1) - return FALSE; - } - - /* Calculate the end date. */ - cb_data.count = rule.count; - cb_data.instances = 0; - cal_recur_generate_instances_of_rule (comp, prop, -1, -1, - cal_recur_ensure_rule_end_date_cb, - &cb_data); - - /* Store the end date in the "X-EVOLUTION-ENDDATE" parameter of the - rule. */ - cal_recur_set_rule_end_date (prop, cb_data.end_date); - - return TRUE; -} - - -static gboolean -cal_recur_ensure_rule_end_date_cb (CalComponent *comp, - time_t instance_start, - time_t instance_end, - gpointer data) -{ - CalRecurEnsureEndDateData *cb_data; - - cb_data = (CalRecurEnsureEndDateData*) data; - - cb_data->instances++; - - if (cb_data->instances == cb_data->count) { - cb_data->end_date = instance_start; - return FALSE; - } - - return TRUE; -} - - -static time_t -cal_recur_get_rule_end_date (icalproperty *prop) -{ - icalparameter *param; - const char *xname, *xvalue; - icalvalue *value; - struct icaltimetype icaltime; - - param = icalproperty_get_first_parameter (prop, ICAL_X_PARAMETER); - while (param) { - xname = icalparameter_get_xname (param); - if (xname && !strcmp (xname, EVOLUTION_END_DATE_PARAMETER)) { - xvalue = icalparameter_get_x (param); - value = icalvalue_new_from_string (ICAL_DATETIME_VALUE, - xvalue); - if (value) { - icaltime = icalvalue_get_datetime (value); - icalvalue_free (value); - - return icaltime_as_timet (icaltime); - } - } - - param = icalproperty_get_next_parameter (prop, - ICAL_X_PARAMETER); - } - - return -1; -} - - -static void -cal_recur_set_rule_end_date (icalproperty *prop, - time_t end_date) -{ - icalparameter *param; - icalvalue *value; - struct icaltimetype icaltime; - const char *end_date_string, *xname; - - icaltime = icaltime_from_timet (end_date, FALSE); - value = icalvalue_new_datetime (icaltime); - end_date_string = icalvalue_as_ical_string (value); - icalvalue_free (value); - - /* If we already have an X-EVOLUTION-ENDDATE parameter, set the value - to the new date-time. */ - param = icalproperty_get_first_parameter (prop, ICAL_X_PARAMETER); - while (param) { - xname = icalparameter_get_xname (param); - if (xname && !strcmp (xname, EVOLUTION_END_DATE_PARAMETER)) { - icalparameter_set_x (param, end_date_string); - return; - } - param = icalproperty_get_next_parameter (prop, ICAL_X_PARAMETER); - } - - /* Create a new X-EVOLUTION-ENDDATE and add it to the property. */ - param = icalparameter_new_x (end_date_string); - icalparameter_set_xname (param, EVOLUTION_END_DATE_PARAMETER); - icalproperty_add_parameter (prop, param); -} - diff --git a/calendar/cal-util/cal-recur.h b/calendar/cal-util/cal-recur.h deleted file mode 100644 index de5a139f78..0000000000 --- a/calendar/cal-util/cal-recur.h +++ /dev/null @@ -1,57 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Evolution calendar recurrence rule functions - * - * Copyright (C) 2000 Helix Code, Inc. - * - * Author: Damon Chaplin <damon@helixcode.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -#ifndef CAL_RECUR_H -#define CAL_RECUR_H - -#include <libgnome/gnome-defs.h> -#include <glib.h> -#include <cal-util/cal-component.h> - -BEGIN_GNOME_DECLS - -typedef gboolean (* CalRecurInstanceFn) (CalComponent *comp, - time_t instance_start, - time_t instance_end, - gpointer data); - -/* - * Calls the given callback function for each occurrence of the event that - * intersects the range between the given start and end times (the end time is - * not included). Note that the occurrences may start before the given start - * time. - * - * If the callback routine returns FALSE the occurrence generation stops. - * - * Both start and end can be -1, in which case we start at the events first - * instance and continue until it ends, or forever if it has no enddate. - */ -void cal_recur_generate_instances (CalComponent *comp, - time_t start, - time_t end, - CalRecurInstanceFn cb, - gpointer cb_data); - -END_GNOME_DECLS - -#endif diff --git a/calendar/cal-util/cal-util.c b/calendar/cal-util/cal-util.c deleted file mode 100644 index 67da788b0f..0000000000 --- a/calendar/cal-util/cal-util.c +++ /dev/null @@ -1,75 +0,0 @@ -/* Evolution calendar utilities and types - * - * Copyright (C) 2000 Helix Code, Inc. - * Copyright (C) 2000 Ximian, Inc. - * - * Author: Federico Mena-Quintero <federico@ximian.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -#include <config.h> -#include <stdlib.h> -#include "cal-util.h" - - - -/** - * cal_obj_instance_list_free: - * @list: List of #CalObjInstance structures. - * - * Frees a list of #CalObjInstance structures. - **/ -void -cal_obj_instance_list_free (GList *list) -{ - CalObjInstance *i; - GList *l; - - for (l = list; l; l = l->next) { - i = l->data; - - g_assert (i != NULL); - g_assert (i->uid != NULL); - - g_free (i->uid); - g_free (i); - } - - g_list_free (list); -} - -/** - * cal_obj_uid_list_free: - * @list: List of strings with unique identifiers. - * - * Frees a list of unique identifiers for calendar objects. - **/ -void -cal_obj_uid_list_free (GList *list) -{ - GList *l; - - for (l = list; l; l = l->next) { - char *uid; - - uid = l->data; - - g_assert (uid != NULL); - g_free (uid); - } - - g_list_free (list); -} diff --git a/calendar/cal-util/cal-util.h b/calendar/cal-util/cal-util.h deleted file mode 100644 index a0a111258d..0000000000 --- a/calendar/cal-util/cal-util.h +++ /dev/null @@ -1,58 +0,0 @@ -/* Evolution calendar utilities and types - * - * Copyright (C) 2000 Helix Code, Inc. - * Copyright (C) 2000 Ximian, Inc. - * - * Author: Federico Mena-Quintero <federico@ximian.com> - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * 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. - */ - -#ifndef CAL_UTIL_H -#define CAL_UTIL_H - -#include <libgnome/gnome-defs.h> -#include <time.h> -#include <glib.h> - -BEGIN_GNOME_DECLS - - - -/* Instance of a calendar object. This can be an actual occurrence, a - * recurrence, or an alarm trigger of a `real' calendar object. - */ -typedef struct { - char *uid; /* UID of the object */ - time_t start; /* Start time of instance */ - time_t end; /* End time of instance */ -} CalObjInstance; - -void cal_obj_instance_list_free (GList *list); - -/* Used for multiple UID queries */ -typedef enum { - CALOBJ_TYPE_EVENT = 1 << 0, - CALOBJ_TYPE_TODO = 1 << 1, - CALOBJ_TYPE_JOURNAL = 1 << 2, - CALOBJ_TYPE_ANY = 0x07 -} CalObjType; - -void cal_obj_uid_list_free (GList *list); - -END_GNOME_DECLS - -#endif - diff --git a/calendar/cal-util/calobj.c b/calendar/cal-util/calobj.c deleted file mode 100644 index bcb1b9bb88..0000000000 --- a/calendar/cal-util/calobj.c +++ /dev/null @@ -1,2008 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ -/* - * Calendar objects implementations. - * Copyright (C) 1998 the Free Software Foundation - * - * Authors: - * Miguel de Icaza (miguel@gnu.org) - * Federico Mena (quartic@gimp.org) - */ -#include <config.h> -#include <string.h> -#include <glib.h> -#include <ctype.h> -#include <unistd.h> -#include <time.h> -#include "calobj.h" -#include "timeutil.h" -#include "libversit/vcc.h" -#include "icalendar-save.h" -#include "icalendar.h" - - - -/* VCalendar product ID */ -#define PRODID "-//Helix Code//NONSGML Evolution Calendar//EN" - -static gint compare_exdates (gconstpointer a, gconstpointer b); -static void ical_object_normalize_summary (iCalObject *ico); -static void list_free (GList *list); - - - -char * -ical_gen_uid (void) -{ - static char *hostname; - time_t t = time (NULL); - static int serial; - - if (!hostname){ - char buffer [128]; - - if ((gethostname (buffer, sizeof (buffer)-1) == 0) && - (buffer [0] != 0)) - hostname = g_strdup (buffer); - else - hostname = g_strdup ("localhost"); - } - - return g_strdup_printf ( - "%s-%d-%d-%d-%d@%s", - isodate_from_time_t (t), - getpid (), - getgid (), - getppid (), - serial++, - hostname); -} - -iCalObject * -ical_object_new (void) -{ - iCalObject *ico; - - ico = g_new0 (iCalObject, 1); - - ico->seq = -1; - ico->dtstamp = time (NULL); - ico->uid = ical_gen_uid (); - - ico->pilot_id = 0; - ico->pilot_status = ICAL_PILOT_SYNC_MOD; - - ico->ref_count = 1; - - return ico; -} - -iCalObject * -ical_new (char *comment, char *organizer, char *summary) -{ - iCalObject *ico; - - ico = ical_object_new (); - - ico->comment = g_strdup (comment); - ico->organizer = g_new0 (iCalPerson, 1); - ico->organizer->addr = g_strdup (organizer); - ico->summary = g_strdup (summary); - ico->class = g_strdup ("PUBLIC"); - ico->status = g_strdup ("NEEDS ACTION"); - - ico->dalarm.type = ALARM_DISPLAY; - ico->palarm.type = ALARM_PROGRAM; - ico->malarm.type = ALARM_MAIL; - ico->aalarm.type = ALARM_AUDIO; - - ical_object_normalize_summary (ico); - - return ico; -} - - -void -ical_object_ref (iCalObject *ico) -{ - ico->ref_count++; -} - - -#define free_if_defined(x) if (x){ g_free (x); x = 0; } -#define lfree_if_defined(x) if (x){ list_free (x); x = 0; } -static void -ical_object_destroy (iCalObject *ico) -{ - /* Regular strings */ - free_if_defined (ico->comment); - free_if_defined (ico->organizer); - free_if_defined (ico->summary); - free_if_defined (ico->uid); - free_if_defined (ico->status); - free_if_defined (ico->class); - free_if_defined (ico->url); - free_if_defined (ico->recur); - - /* Lists */ - lfree_if_defined (ico->exdate); - lfree_if_defined (ico->categories); - lfree_if_defined (ico->resources); - lfree_if_defined (ico->related); - lfree_if_defined (ico->attach); - - /* Alarms */ - g_free (ico->dalarm.data); - g_free (ico->palarm.data); - g_free (ico->malarm.data); - g_free (ico->aalarm.data); - - g_free (ico); -} - -void -ical_object_unref (iCalObject *ico) -{ - ico->ref_count--; - if (ico->ref_count == 0) - ical_object_destroy (ico); -} - - -static void -my_free (gpointer data, gpointer user_dat_ignored) -{ - g_free (data); -} - -static void -list_free (GList *list) -{ - g_list_foreach (list, my_free, 0); - g_list_free (list); -} - -/* This resets any recurrence rules of the iCalObject. */ -void -ical_object_reset_recurrence (iCalObject *ico) -{ - free_if_defined (ico->recur); - lfree_if_defined (ico->exdate); -} - -static GList * -set_list (char *str) -{ - GList *list = 0; - char *s; - - for (s = strtok (str, ";"); s; s = strtok (NULL, ";")) - list = g_list_prepend (list, g_strdup (s)); - - return list; -} - -static GList * -set_date_list (char *str) -{ - GList *list = 0; - char *s; - - for (s = strtok (str, ";,"); s; s = strtok (NULL, ";,")){ - time_t *t = g_new (time_t, 1); - - while (*s && isspace (*s)) - s++; - *t = time_from_isodate (s); - list = g_list_prepend (list, t); - } - return list; -} - -void -ical_object_add_exdate (iCalObject *o, time_t t) -{ - time_t *pt = g_new (time_t, 1); - - *pt = t; - o->exdate = g_list_prepend (o->exdate, pt); -} - -static void -ignore_space(char **str) -{ - while (**str && isspace (**str)) - (*str)++; -} - -static void -skip_numbers (char **str) -{ - while (**str){ - ignore_space (str); - if (!isdigit (**str)) - return; - while (**str && isdigit (**str)) - (*str)++; - } -} - -static void -weekdaylist (iCalObject *o, char **str) -{ - int i; - struct { - char first_letter, second_letter; - int index; - } days [] = { - { 'S', 'U', 0 }, - { 'M', 'O', 1 }, - { 'T', 'U', 2 }, - { 'W', 'E', 3 }, - { 'T', 'H', 4 }, - { 'F', 'R', 5 }, - { 'S', 'A', 6 } - }; - - ignore_space (str); - do { - for (i = 0; i < 7; i++){ - if (**str == days [i].first_letter && *(*str+1) == days [i].second_letter){ - o->recur->weekday |= 1 << i; - *str += 2; - if (**str == ' ') - (*str)++; - } - } - } while (isalpha ((unsigned char) **str)); - - if (o->recur->weekday == 0){ - struct tm tm = *localtime (&o->dtstart); - - o->recur->weekday = 1 << tm.tm_wday; - } -} - -static void -weekdaynum (iCalObject *o, char **str) -{ - int i; - struct { - char first_letter, second_letter; - int index; - } days [] = { - { 'S', 'U', 0 }, - { 'M', 'O', 1 }, - { 'T', 'U', 2 }, - { 'W', 'E', 3 }, - { 'T', 'H', 4 }, - { 'F', 'R', 5 }, - { 'S', 'A', 6 } - }; - - ignore_space (str); - do { - for (i = 0; i < 7; i++){ - if (**str == days [i].first_letter && *(*str+1) == days [i].second_letter){ - o->recur->weekday = i; - *str += 2; - if (**str == ' ') - (*str)++; - } - } - } while (isalpha ((unsigned char) **str)); -} - -static void -ocurrencelist (iCalObject *o, char **str) -{ - char *p; - - ignore_space (str); - p = *str; - if (!isdigit (*p)) - return; - - if (!(*p >= '1' && *p <= '5')) - return; - - if (!(*(p+1) == '+' || *(p+1) == '-')) - return; - - o->recur->u.month_pos = (*p-'0') * (*(p+1) == '+' ? 1 : -1); - *str += 2; -} - -#if 0 - -static void -daynumber (iCalObject *o, char **str) -{ - int val = 0; - char *p = *str; - - ignore_space (str); - if (strcmp (p, "LD")){ - o->recur->u.month_day = DAY_LASTDAY; - *str += 2; - return; - } - - if (!(isdigit (*p))) - return; - - while (**str && isdigit (**str)){ - val = val * 10 + (**str - '0'); - (*str)++; - } - - if (**str == '+') - (*str)++; - - if (**str == '-') - val *= -1; - o->recur->u.month_day = val; -} - -#endif - -static void -daynumberlist (iCalObject *o, char **str) -{ - int first = 0; - int val = 0; - - ignore_space (str); - - while (**str){ - if (!isdigit (**str)) - return; - while (**str && isdigit (**str)){ - val = 10 * val + (**str - '0'); - (*str)++; - } - if (!first){ - /* - * Some broken applications set this to zero - */ - if (val == 0){ - struct tm day = *localtime (&o->dtstart); - - val = day.tm_mday; - } - o->recur->u.month_day = val; - first = 1; - val = 0; - } - } -} - -static void -load_recur_weekly (iCalObject *o, char **str) -{ - weekdaylist (o, str); -} - -static void -load_recur_monthly_pos (iCalObject *o, char **str) -{ - ocurrencelist (o, str); - weekdaynum (o, str); -} - -static void -load_recur_monthly_day (iCalObject *o, char **str) -{ - daynumberlist (o, str); -} - -static void -load_recur_yearly_month (iCalObject *o, char **str) -{ - /* Skip as we do not support multiple months and we do expect - * the dtstart to agree with the value on this field - */ - skip_numbers (str); -} - -static void -load_recur_yearly_day (iCalObject *o, char **str) -{ - /* Skip as we do not support multiple days and we do expect - * the dtstart to agree with the value on this field - * - * FIXME: we should support every-n-years - */ - skip_numbers (str); -} - -static void -duration (iCalObject *o, char **str) -{ - unsigned int duration = 0; - - ignore_space (str); - if (**str != '#') - return; - (*str)++; - while (**str && isdigit (**str)){ - duration = duration * 10 + (**str - '0'); - (*str)++; - } - o->recur->duration = duration; -} - -static void -enddate (iCalObject *o, char **str) -{ - ignore_space (str); - if (isdigit (**str)){ - o->recur->_enddate = time_from_isodate (*str); - *str += 16; - } -} - -static int -load_recurrence (iCalObject *o, char *str) -{ - enum RecurType type; - int interval = 0; - - type = -1; - switch (*str++){ - case 'D': - type = RECUR_DAILY; - break; - - case 'W': - type = RECUR_WEEKLY; - break; - - case 'M': - if (*str == 'P') - type = RECUR_MONTHLY_BY_POS; - else if (*str == 'D') - type = RECUR_MONTHLY_BY_DAY; - str++; - break; - - case 'Y': - if (*str == 'M') - type = RECUR_YEARLY_BY_MONTH; - else if (*str == 'D') - type = RECUR_YEARLY_BY_DAY; - str++; - break; - } - if (type == -1) - return 0; - - o->recur = g_new0 (Recurrence, 1); - o->recur->type = type; - ignore_space (&str); - - /* Get the interval */ - for (;*str && isdigit (*str);str++) - interval = interval * 10 + (*str-'0'); - - if (interval == 0) - interval = 1; - - o->recur->interval = interval; - - /* this is the default per the spec */ - o->recur->duration = 2; - - ignore_space (&str); - - switch (type){ - case RECUR_DAILY: - break; - case RECUR_WEEKLY: - load_recur_weekly (o, &str); - break; - case RECUR_MONTHLY_BY_POS: - load_recur_monthly_pos (o, &str); - break; - case RECUR_MONTHLY_BY_DAY: - load_recur_monthly_day (o, &str); - break; - case RECUR_YEARLY_BY_MONTH: - load_recur_yearly_month (o, &str); - break; - case RECUR_YEARLY_BY_DAY: - load_recur_yearly_day (o, &str); - break; - default: - g_warning ("Unimplemented recurrence type %d", (int) type); - break; - } - duration (o, &str); - enddate (o, &str); - - /* Compute the enddate */ - if (o->recur->_enddate == 0){ - if (o->recur->duration != 0){ - ical_object_compute_end (o); - } else - o->recur->enddate = 0; - } else { - o->recur->enddate = o->recur->_enddate; - } - return 1; -} - -#define is_a_prop_of(obj,prop) isAPropertyOf (obj,prop) -#define str_val(obj) the_str = fakeCString (vObjectUStringZValue (obj)) -#define has(obj,prop) (vo = isAPropertyOf (obj, prop)) - -/* - * FIXME: This is loosing precission. Enhanec the thresholds - */ -#define HOURS(n) (n*(60*60)) - -static void -setup_alarm_at (iCalObject *ico, CalendarAlarm *alarm, char *iso_time, VObject *vo) -{ - time_t alarm_time = time_from_isodate (iso_time); - time_t base = ico->dtstart; - int d = difftime (base, alarm_time); - VObject *a; - char *the_str; - - alarm->enabled = 1; - if (d > HOURS (2)){ - if (d > HOURS (48)){ - alarm->count = d / HOURS (24); - alarm->units = ALARM_DAYS; - } else { - alarm->count = d / (60*60); - alarm->units = ALARM_HOURS; - } - } else { - alarm->count = d / 60; - alarm->units = ALARM_MINUTES; - } - - if ((a = is_a_prop_of (vo, VCSnoozeTimeProp))){ - alarm->snooze_secs = isodiff_to_secs (str_val (a)); - free (the_str); - } - - if ((a = is_a_prop_of (vo, VCRepeatCountProp))){ - alarm->snooze_repeat = atoi (str_val (a)); - free (the_str); - } -} - -/* - * Duplicates an iCalObject. Implementation is a grand hack. - * If you need the new ICalObject to have a new uid, free the current one, - * and call ical_gen_uid() to generate a new one. - */ -iCalObject * -ical_object_duplicate (iCalObject *o) -{ - VObject *vo; - iCalObject *new; - - vo = ical_object_to_vobject (o); - switch (o->type){ - case ICAL_EVENT: - new = ical_object_create_from_vobject (vo, VCEventProp); - break; - case ICAL_TODO: - new = ical_object_create_from_vobject (vo, VCTodoProp); - break; - default: - new = NULL; - } - - cleanVObject (vo); - return new; -} - -/* FIXME: we need to load the recurrence properties */ -iCalObject * -ical_object_create_from_vobject (VObject *o, const char *object_name) -{ - time_t now = time (NULL); - iCalObject *ical; - VObject *vo, *a; - VObjectIterator i; - char *the_str; - - ical = g_new0 (iCalObject, 1); - - if (strcmp (object_name, VCEventProp) == 0) - ical->type = ICAL_EVENT; - else if (strcmp (object_name, VCTodoProp) == 0) - ical->type = ICAL_TODO; - else { - g_free (ical); - return 0; - } - - ical->ref_count = 1; - - /* uid */ - if (has (o, VCUniqueStringProp)){ - ical->uid = g_strdup (str_val (vo)); - free (the_str); - } else { - ical->uid = ical_gen_uid (); - } - - /* seq */ - if (has (o, VCSequenceProp)){ - ical->seq = atoi (str_val (vo)); - free (the_str); - } else - ical->seq = 0; - - /* dtstart */ - if (has (o, VCDTstartProp)){ - ical->dtstart = time_from_isodate (str_val (vo)); - free (the_str); - } else - ical->dtstart = 0; - - /* dtend */ - ical->dtend = 0; /* default value */ - if (ical->type == ICAL_EVENT){ - if (has (o, VCDTendProp)){ - ical->dtend = time_from_isodate (str_val (vo)); - free (the_str); - } - } else if (ical->type == ICAL_TODO){ - if (has (o, VCDueProp)){ - ical->dtend = time_from_isodate (str_val (vo)); - free (the_str); - } - } - - /* dcreated */ - if (has (o, VCDCreatedProp)){ - ical->created = time_from_isodate (str_val (vo)); - free (the_str); - } - - /* completed */ - if (has (o, VCCompletedProp)){ - ical->completed = time_from_isodate (str_val (vo)); - free (the_str); - } - - /* last_mod */ - if (has (o, VCLastModifiedProp)){ - ical->last_mod = time_from_isodate (str_val (vo)); - free (the_str); - } else - ical->last_mod = now; - - /* exdate */ - if (has (o, VCExpDateProp)){ - ical->exdate = set_date_list (str_val (vo)); - free (the_str); - } - - /* description/comment */ - if (has (o, VCDescriptionProp)){ - ical->comment = g_strdup (str_val (vo)); - free (the_str); - } - - /* summary */ - if (has (o, VCSummaryProp)){ - ical->summary = g_strdup (str_val (vo)); - free (the_str); - - /* Convert any CR/LF/CRLF sequences in the summary field to - spaces so we just have a one-line field. */ - ical_object_normalize_summary (ical); - } else - ical->summary = g_strdup (""); - - /* status */ - if (has (o, VCStatusProp)){ - ical->status = g_strdup (str_val (vo)); - free (the_str); - } else - ical->status = g_strdup ("NEEDS ACTION"); - - if (has (o, VCClassProp)){ - ical->class = g_strdup (str_val (vo)); - free (the_str); - } else - ical->class = g_strdup ("PUBLIC"); - - /* categories */ - if (has (o, VCCategoriesProp)){ - ical->categories = set_list (str_val (vo)); - free (the_str); - } - - /* resources */ - if (has (o, VCResourcesProp)){ - ical->resources = set_list (str_val (vo)); - free (the_str); - } - - /* priority */ - if (has (o, VCPriorityProp)){ - ical->priority = atoi (str_val (vo)); - free (the_str); - } - - /* tranparency */ - if (has (o, VCTranspProp)){ - ical->transp = atoi (str_val (vo)) ? ICAL_TRANSPARENT : ICAL_OPAQUE; - free (the_str); - } - - /* Organizer */ - if (has (o, VCOrgNameProp)){ - ical->organizer = g_new0 (iCalPerson, 1); - ical->organizer->addr = g_strdup (str_val (vo)); - free (the_str); - } - - /* related */ - if (has (o, VCRelatedToProp)){ - char *str; - char *s; - iCalRelation *rel; - str = str_val (vo); - for (s = strtok (str, ";"); s; s = strtok (NULL, ";")) { - rel = g_new0 (iCalRelation, 1); - rel->uid = g_strdup (s); - rel->reltype = g_strdup ("PARENT"); - ical->related = g_list_prepend (ical->related, rel); - } - free (the_str); - } - - /* attach */ - initPropIterator (&i, o); - while (moreIteration (&i)){ - vo = nextVObject (&i); - if (strcmp (vObjectName (vo), VCAttachProp) == 0){ - ical->attach = g_list_prepend (ical->attach, g_strdup (str_val (vo))); - free (the_str); - } - } - - /* url */ - if (has (o, VCURLProp)){ - /* There seems to be a problem with the URL property. For some - reason an empty property gets saved, vObjectUStringZValue - returns NULL and fakeCString crashes. So we check for NULL. - */ - const wchar_t *zval; - - zval = vObjectUStringZValue (o); - if (zval) { - the_str = fakeCString (zval); - ical->url = g_strdup (the_str); - free (the_str); - } - } - - /* dalarm */ - ical->dalarm.type = ALARM_DISPLAY; - ical->dalarm.enabled = 0; - if (has (o, VCDAlarmProp)){ - if ((a = is_a_prop_of (vo, VCRunTimeProp))){ - setup_alarm_at (ical, &ical->dalarm, str_val (a), vo); - free (the_str); - } - } - - /* aalarm */ - ical->aalarm.type = ALARM_AUDIO; - ical->aalarm.enabled = 0; - if (has (o, VCAAlarmProp)){ - if ((a = is_a_prop_of (vo, VCRunTimeProp))){ - setup_alarm_at (ical, &ical->aalarm, str_val (a), vo); - free (the_str); - } - } - - /* palarm */ - ical->palarm.type = ALARM_PROGRAM; - ical->palarm.enabled = 0; - if (has (o, VCPAlarmProp)){ - ical->palarm.type = ALARM_PROGRAM; - if ((a = is_a_prop_of (vo, VCRunTimeProp))){ - setup_alarm_at (ical, &ical->palarm, str_val (a), vo); - free (the_str); - - if ((a = is_a_prop_of (vo, VCProcedureNameProp))){ - ical->palarm.data = g_strdup (str_val (a)); - free (the_str); - } else - ical->palarm.data = g_strdup (""); - } - } - - /* malarm */ - ical->malarm.type = ALARM_MAIL; - ical->malarm.enabled = 0; - if (has (o, VCMAlarmProp)){ - ical->malarm.type = ALARM_MAIL; - if ((a = is_a_prop_of (vo, VCRunTimeProp))){ - setup_alarm_at (ical, &ical->malarm, str_val (a), vo); - free (the_str); - - if ((a = is_a_prop_of (vo, VCEmailAddressProp))){ - ical->malarm.data = g_strdup (str_val (a)); - free (the_str); - } else - ical->malarm.data = g_strdup (""); - } - } - - /* rrule */ - if (has (o, VCRRuleProp)){ - if (!load_recurrence (ical, str_val (vo))) { - ical_object_unref (ical); - return NULL; - } - free (the_str); - } - - /* - * Pilot - */ - if (has (o, XPilotIdProp)){ - ical->pilot_id = atoi (str_val (vo)); - free (the_str); - } else - ical->pilot_id = 0; - - if (has (o, XPilotStatusProp)){ - ical->pilot_status = atoi (str_val (vo)); - free (the_str); - } else - ical->pilot_status = ICAL_PILOT_SYNC_MOD; - - return ical; -} - -static char * -to_str (int num) -{ - static char buf [40]; - - sprintf (buf, "%d", num); - return buf; -} - -/* - * stores a GList in the property. - */ -static void -store_list (VObject *o, char *prop, GList *values) -{ - GList *l; - int len; - char *result, *p; - - for (len = 0, l = values; l; l = l->next) - len += strlen (l->data) + 1; - - result = g_malloc (len); - - for (p = result, l = values; l; l = l->next) { - int len = strlen (l->data); - - strcpy (p, l->data); - - if (l->next) { - p [len] = ';'; - p += len+1; - } else - p += len; - } - - *p = 0; - - addPropValue (o, prop, result); - g_free (result); -} - -static void -store_rel_list (VObject *o, char *prop, GList *values) -{ - GList *l; - int len; - char *result, *p; - - for (len = 0, l = values; l; l = l->next) - len += strlen (((iCalRelation*)(l->data))->uid) + 1; - - result = g_malloc (len); - - for (p = result, l = values; l; l = l->next) { - int len = strlen (((iCalRelation*)(l->data))->uid); - - strcpy (p, ((iCalRelation*)(l->data))->uid); - - if (l->next) { - p [len] = ';'; - p += len+1; - } else - p += len; - } - - *p = 0; - - addPropValue (o, prop, result); - g_free (result); -} - -static void -store_date_list (VObject *o, char *prop, GList *values) -{ - GList *l; - int size, len; - char *s, *p; - - size = g_list_length (values); - s = p = g_malloc ((size * 17 + 1) * sizeof (char)); - - for (l = values; l; l = l->next){ - strcpy (s, isodate_from_time_t (*(time_t *)l->data)); - len = strlen (s); - s [len] = ','; - s += len + 1; - } - s--; - *s = 0; - addPropValue (o, prop, p); - g_free (p); -} - -static char *recur_type_name [] = { "D", "W", "MP", "MD", "YM", "YD" }; -static char *recur_day_list [] = { "SU", "MO", "TU","WE", "TH", "FR", "SA" }; -static char *alarm_names [] = { VCMAlarmProp, VCPAlarmProp, VCDAlarmProp, VCAAlarmProp }; - -static VObject * -save_alarm (VObject *o, CalendarAlarm *alarm, iCalObject *ical) -{ - VObject *alarm_object; - struct tm tm; - time_t alarm_time; - - if (!alarm->enabled) - return NULL; - tm = *localtime (&ical->dtstart); - switch (alarm->units){ - case ALARM_MINUTES: - tm.tm_min -= alarm->count; - break; - - case ALARM_HOURS: - tm.tm_hour -= alarm->count; - break; - - case ALARM_DAYS: - tm.tm_mday -= alarm->count; - break; - } - - alarm_time = mktime (&tm); - alarm_object = addProp (o, alarm_names [alarm->type]); - addPropValue (alarm_object, VCRunTimeProp, isodate_from_time_t (alarm_time)); - - if (alarm->snooze_secs) - addPropValue (alarm_object, VCSnoozeTimeProp, isodiff_from_secs (alarm->snooze_secs)); - else - addPropValue (alarm_object, VCSnoozeTimeProp, ""); - - if (alarm->snooze_repeat){ - char buf [20]; - - sprintf (buf, "%d", alarm->snooze_repeat); - addPropValue (alarm_object, VCRepeatCountProp, buf); - } else - addPropValue (alarm_object, VCRepeatCountProp, ""); - return alarm_object; -} - -VObject * -ical_object_to_vobject (iCalObject *ical) -{ - VObject *o, *alarm, *s; - GList *l; - - if (ical->type == ICAL_EVENT) - o = newVObject (VCEventProp); - else - o = newVObject (VCTodoProp); - - /* uid */ - if (ical->uid) - addPropValue (o, VCUniqueStringProp, ical->uid); - - /* seq */ - addPropValue (o, VCSequenceProp, to_str (ical->seq)); - - /* dtstart */ - addPropValue (o, VCDTstartProp, isodate_from_time_t (ical->dtstart)); - - /* dtend */ - if (ical->type == ICAL_EVENT){ - addPropValue (o, VCDTendProp, isodate_from_time_t (ical->dtend)); - } else if (ical->type == ICAL_TODO){ - addPropValue (o, VCDueProp, isodate_from_time_t (ical->dtend)); - } - - /* dcreated */ - addPropValue (o, VCDCreatedProp, isodate_from_time_t (ical->created)); - - /* completed */ - if (ical->completed) - addPropValue (o, VCDTendProp, isodate_from_time_t (ical->completed)); - - /* last_mod */ - addPropValue (o, VCLastModifiedProp, isodate_from_time_t (ical->last_mod)); - - /* exdate */ - if (ical->exdate) - store_date_list (o, VCExpDateProp, ical->exdate); - - /* description/comment */ - if (ical->comment && strlen (ical->comment)){ - s = addPropValue (o, VCDescriptionProp, ical->comment); - if (strchr (ical->comment, '\n')) - addProp (s, VCQuotedPrintableProp); - } - - /* summary */ - if (ical->summary && strlen (ical->summary)) { - s = addPropValue (o, VCSummaryProp, ical->summary); - if (strchr (ical->summary, '\n')) - addProp (s, VCQuotedPrintableProp); - } - - /* status */ - addPropValue (o, VCStatusProp, ical->status); - - /* class */ - addPropValue (o, VCClassProp, ical->class); - - /* categories */ - if (ical->categories) - store_list (o, VCCategoriesProp, ical->categories); - - /* resources */ - if (ical->resources) - store_list (o, VCCategoriesProp, ical->resources); - - /* priority */ - addPropValue (o, VCPriorityProp, to_str (ical->priority)); - - /* transparency */ - addPropValue (o, VCTranspProp, to_str (ical->transp)); - - /* Owner/organizer */ - if (ical->organizer && ical->organizer->addr) - addPropValue (o, VCOrgNameProp, ical->organizer->addr); - - /* related */ - if (ical->related) - store_rel_list (o, VCRelatedToProp, ical->related); - - /* attach */ - for (l = ical->attach; l; l = l->next) - addPropValue (o, VCAttachProp, l->data); - - /* url */ - if (ical->url) - addPropValue (o, VCURLProp, ical->url); - - if (ical->recur){ - char result [256]; - char buffer [80]; - int i; - - sprintf (result, "%s%d ", recur_type_name [ical->recur->type], ical->recur->interval); - switch (ical->recur->type){ - case RECUR_DAILY: - break; - - case RECUR_WEEKLY: - for (i = 0; i < 7; i++){ - if (ical->recur->weekday & (1 << i)){ - sprintf (buffer, "%s ", recur_day_list [i]); - strcat (result, buffer); - } - } - break; - - case RECUR_MONTHLY_BY_POS: { - int nega = ical->recur->u.month_pos < 0; - - sprintf (buffer, "%d%s ", nega ? -ical->recur->u.month_pos : ical->recur->u.month_pos, - nega ? "-" : "+"); - strcat (result, buffer); - /* the gui is set up for a single day, not a set here in this case */ - sprintf (buffer, "%s ", recur_day_list [ical->recur->weekday]); - strcat (result, buffer); - } - break; - - case RECUR_MONTHLY_BY_DAY: - sprintf (buffer, "%d ", ical->recur->u.month_pos); - strcat (result, buffer); - break; - - case RECUR_YEARLY_BY_MONTH: - break; - - case RECUR_YEARLY_BY_DAY: - break; - } - if (ical->recur->_enddate == 0) - sprintf (buffer, "#%d ",ical->recur->duration); - else - sprintf (buffer, "%s ", isodate_from_time_t (ical->recur->_enddate)); - strcat (result, buffer); - addPropValue (o, VCRRuleProp, result); - } - - save_alarm (o, &ical->aalarm, ical); - save_alarm (o, &ical->dalarm, ical); - - if ((alarm = save_alarm (o, &ical->palarm, ical))) - addPropValue (alarm, VCProcedureNameProp, ical->palarm.data); - if ((alarm = save_alarm (o, &ical->malarm, ical))) - addPropValue (alarm, VCEmailAddressProp, ical->malarm.data); - - /* Pilot */ - { - char buffer [20]; - - sprintf (buffer, "%d", ical->pilot_id); - addPropValue (o, XPilotIdProp, buffer); - sprintf (buffer, "%d", ical->pilot_status); - addPropValue (o, XPilotStatusProp, buffer); - } - - return o; -} - -void -ical_foreach (GList *events, calendarfn fn, void *closure) -{ - for (; events; events = events->next){ - iCalObject *ical = events->data; - - (*fn) (ical, ical->dtstart, ical->dtend, closure); - } -} - -static int -is_date_in_list (GList *list, struct tm *date) -{ - struct tm tm; - - for (; list; list = list->next){ - time_t *timep = list->data; - - tm = *localtime (timep); - if (date->tm_mday == tm.tm_mday && - date->tm_mon == tm.tm_mon && - date->tm_year == tm.tm_year){ - return 1; - } - } - return 0; -} - -/* Generates an event instance based on the reference time */ -static gboolean -generate (iCalObject *ico, time_t reference, calendarfn cb, void *closure) -{ - time_t offset; - struct tm tm_start, ref; - time_t start, end; - - offset = ico->dtend - ico->dtstart; - - tm_start = *localtime (&ico->dtstart); - ref = *localtime (&reference); - - tm_start.tm_mday = ref.tm_mday; - tm_start.tm_mon = ref.tm_mon; - tm_start.tm_year = ref.tm_year; - - start = mktime (&tm_start); - if (start == -1) { - g_message ("generate(): Produced invalid start date!"); - return FALSE; - } - - end = start + offset; - -#if 0 - /* FIXME: I think this is not needed, since we are offsetting by full day values, - * and the times should remain the same --- if you have a daily appointment - * at 18:00, it is always at 18:00 even during daylight savings. - * - * However, what should happen on the exact change-of-savings day with - * appointments in the early morning hours? - */ - - if (ref.tm_isdst > tm_start.tm_isdst) { - tm_start.tm_hour--; - tm_end.tm_hour--; - } else if (ref.tm_isdst < tm_start.tm_isdst) { - tm_start.tm_hour++; - tm_end.tm_hour++; - } -#endif - - if (ico->exdate && is_date_in_list (ico->exdate, &tm_start)) - return TRUE; - - return (*cb) (ico, start, end, closure); -} - -int -ical_object_get_first_weekday (int weekday_mask) -{ - int i; - - for (i = 0; i < 7; i++) - if (weekday_mask & (1 << i)) - return i; - - return -1; -} - -#define time_in_range(t, a, b) ((t >= a) && (b ? (t < b) : 1)) -#define recur_in_range(t, r) (r->enddate ? (t < r->enddate) : 1) - -/* - * Generate every possible event. Invokes the callback routine for - * every occurrence of the event in the [START, END] time interval. - * - * If END is zero, the event is generated forever. - * The callback routine is expected to return 0 when no further event - * generation is requested. - */ -void -ical_object_generate_events (iCalObject *ico, time_t start, time_t end, calendarfn cb, void *closure) -{ - time_t current; - int first_week_day; - - /* If there is no recurrence, just check ranges */ - - if (!ico->recur) { - if ((end && (ico->dtstart < end) && (ico->dtend > start)) - || ((end == 0) && (ico->dtend > start))) { - /* The new calendar views expect the times to not be - clipped, so they can show that it continues past - the end of the viewable area. */ -#if 0 - time_t ev_s, ev_e; - - /* Clip range */ - - ev_s = MAX (ico->dtstart, start); - ev_e = MIN (ico->dtend, end); - - (* cb) (ico, ev_s, ev_e, closure); -#else - (* cb) (ico, ico->dtstart, ico->dtend, closure); -#endif - } - return; - } - - /* The event has a recurrence rule -- check that we will generate at least one instance */ - - if (end != 0) { - if (ico->dtstart > end) - return; - - if (!IS_INFINITE (ico->recur) && (ico->recur->enddate < start)) - return; - } - - /* Generate the instances */ - - current = ico->dtstart; - - switch (ico->recur->type) { - case RECUR_DAILY: - do { - if (time_in_range (current, start, end) && recur_in_range (current, ico->recur)) - if (!generate (ico, current, cb, closure)) - return; - - /* Advance */ - - current = time_add_day (current, ico->recur->interval); - - if (current == -1) { - g_warning ("RECUR_DAILY: time_add_day() returned invalid time"); - return; - } - } while ((current < end) || (end == 0)); - - break; - - case RECUR_WEEKLY: - do { - struct tm tm; - - tm = *localtime (¤t); - - if (time_in_range (current, start, end) && recur_in_range (current, ico->recur)) { - /* Weekdays to recur on are specified as a bitmask */ - if (ico->recur->weekday & (1 << tm.tm_wday)) { - if (!generate (ico, current, cb, closure)) - return; - } - } - - /* Advance by day for scanning the week or by interval at week end */ - - if (tm.tm_wday == 6) - current = time_add_day (current, (ico->recur->interval - 1) * 7 + 1); - else - current = time_add_day (current, 1); - - if (current == -1) { - g_warning ("RECUR_WEEKLY: time_add_day() returned invalid time\n"); - return; - } - } while (current < end || (end == 0)); - - break; - - case RECUR_MONTHLY_BY_POS: - /* FIXME: We only deal with positives now */ - if (ico->recur->u.month_pos < 0) { - g_warning ("RECUR_MONTHLY_BY_POS does not support negative positions yet"); - return; - } - - if (ico->recur->u.month_pos == 0) - return; - - first_week_day = /* ical_object_get_first_weekday (ico->recur->weekday); */ - ico->recur->weekday; /* the i/f only lets you choose a single day of the week! */ - - /* This should not happen, but take it into account */ - if (first_week_day == -1) { - g_warning ("ical_object_get_first_weekday() returned -1"); - return; - } - - do { - struct tm tm; - time_t t; - int week_day_start; - - tm = *localtime (¤t); - tm.tm_mday = 1; - t = mktime (&tm); - tm = *localtime (&t); - week_day_start = tm.tm_wday; - - tm.tm_mday = (7 * (ico->recur->u.month_pos - ((week_day_start <= first_week_day ) ? 1 : 0)) - - (week_day_start - first_week_day) + 1); - if( tm.tm_mday > 31 ) - { - tm.tm_mday = 1; - tm.tm_mon += ico->recur->interval; - current = mktime (&tm); - continue; - } - - switch( tm.tm_mon ) - { - case 3: - case 5: - case 8: - case 10: - if( tm.tm_mday > 30 ) - { - tm.tm_mday = 1; - tm.tm_mon += ico->recur->interval; - current = mktime (&tm); - continue; - } - break; - case 1: - if( ((tm.tm_year+1900)%4) == 0 - && ((tm.tm_year+1900)%400) != 100 - && ((tm.tm_year+1900)%400) != 200 - && ((tm.tm_year+1900)%400) != 300 ) - { - - if( tm.tm_mday > 29 ) - { - tm.tm_mday = 1; - tm.tm_mon += ico->recur->interval; - current = mktime (&tm); - continue; - } - } - else - { - if( tm.tm_mday > 28 ) - { - tm.tm_mday = 1; - tm.tm_mon += ico->recur->interval; - current = mktime (&tm); - continue; - } - } - break; - } - - t = mktime (&tm); - - if (time_in_range (t, start, end) && recur_in_range (current, ico->recur)) - if (!generate (ico, t, cb, closure)) - return; - - /* Advance by the appropriate number of months */ - - current = mktime (&tm); - - tm.tm_mday = 1; - tm.tm_mon += ico->recur->interval; - current = mktime (&tm); - - if (current == -1) { - g_warning ("RECUR_MONTHLY_BY_DAY: mktime error\n"); - return; - } - } while ((current < end) || (end == 0)); - - break; - - case RECUR_MONTHLY_BY_DAY: - do { - struct tm tm; - time_t t; - int p; - - tm = *localtime (¤t); - - p = tm.tm_mday; - tm.tm_mday = ico->recur->u.month_day; - t = mktime (&tm); - if (time_in_range (t, start, end) && recur_in_range (current, ico->recur)) - if (!generate (ico, t, cb, closure)) - return; - - /* Advance by the appropriate number of months */ - - tm.tm_mday = p; - tm.tm_mon += ico->recur->interval; - current = mktime (&tm); - - if (current == -1) { - g_warning ("RECUR_MONTHLY_BY_DAY: mktime error\n"); - return; - } - } while (current < end || (end == 0)); - - break; - - case RECUR_YEARLY_BY_MONTH: - case RECUR_YEARLY_BY_DAY: - do { - if (time_in_range (current, start, end) && recur_in_range (current, ico->recur)) - if (!generate (ico, current, cb, closure)) - return; - - /* Advance */ - - current = time_add_year (current, ico->recur->interval); - } while (current < end || (end == 0)); - - break; - - default: - g_assert_not_reached (); - } -} - -static int -duration_callback (iCalObject *ico, time_t start, time_t end, void *closure) -{ - int *count = closure; - struct tm tm; - - tm = *localtime (&start); - - (*count)++; - if (ico->recur->duration == *count) { - ico->recur->enddate = time_day_end (end); - return 0; - } - return 1; -} - -/* Computes ico->recur->enddate from ico->recur->duration */ -void -ical_object_compute_end (iCalObject *ico) -{ - int count = 0; - - g_return_if_fail (ico->recur != NULL); - - ico->recur->_enddate = 0; - ico->recur->enddate = 0; - ical_object_generate_events (ico, ico->dtstart, 0, duration_callback, &count); -} - -int -alarm_compute_offset (CalendarAlarm *a) -{ - if (!a->enabled) - return -1; - switch (a->units){ - case ALARM_MINUTES: - a->offset = a->count * 60; - break; - case ALARM_HOURS: - a->offset = a->count * 3600; - break; - case ALARM_DAYS: - a->offset = a->count * 24 * 3600; - } - return a->offset; -} - - -/** - * ical_object_find_in_string: - * @uid: Unique identifier of the sought object. - * @vcalobj: String representation of a complete calendar object. - * @ico: The resulting #iCalObject is stored here. - * - * Parses a complete vCalendar object string and tries to find the calendar - * object that matches the specified @uid. If found, it stores the resulting - * #iCalObject in the @ico parameter. - * - * Return value: A result code depending on whether the parse and search were - * successful. - **/ -CalObjFindStatus -ical_object_find_in_string (const char *uid, const char *vcalobj, iCalObject **ico) -{ -#if 0 - icalcomponent* comp = NULL; - icalcomponent *subcomp; - iCalObject *ical; - - g_return_val_if_fail (vcalobj != NULL, CAL_OBJ_FIND_NOT_FOUND); - - comp = icalparser_parse_string (vcalobj); - - if (!comp) { - printf ("CAL_OBJ_FIND_SYNTAX_ERROR #1\n"); - return CAL_OBJ_FIND_SYNTAX_ERROR; - } - - subcomp = icalcomponent_get_first_component (comp, - ICAL_ANY_COMPONENT); - if (!subcomp) { - printf ("CAL_OBJ_FIND_SYNTAX_ERROR #2\n"); - return CAL_OBJ_FIND_SYNTAX_ERROR; - } - - while (subcomp) { - ical = ical_object_create_from_icalcomponent (subcomp); - if (ical->type != ICAL_EVENT && - ical->type != ICAL_TODO && - ical->type != ICAL_JOURNAL) { - g_warning ("Skipping unsupported iCalendar component"); - } else { - if (strcasecmp (ical->uid, uid) == 0) { - (*ico) = ical; - (*ico)->ref_count = 1; - printf ("CAL_OBJ_FIND_SUCCESS\n"); - - printf ("ical_object_find_in_string:\n"); - printf ("-----------------------------------------------------\n"); - dump_icalobject (*ico); - printf ("-----------------------------------------------------\n"); - - - return CAL_OBJ_FIND_SUCCESS; - } - } - subcomp = icalcomponent_get_next_component (comp, - ICAL_ANY_COMPONENT); - } - - printf ("CAL_OBJ_FIND_NOT_FOUND\n"); - return CAL_OBJ_FIND_NOT_FOUND; - -#else /* 1 */ - VObject *vcal; - VObjectIterator i; - CalObjFindStatus status; - - g_return_val_if_fail (uid != NULL, CAL_OBJ_FIND_SYNTAX_ERROR); - g_return_val_if_fail (vcalobj != NULL, CAL_OBJ_FIND_SYNTAX_ERROR); - g_return_val_if_fail (ico != NULL, CAL_OBJ_FIND_SYNTAX_ERROR); - - *ico = NULL; - status = CAL_OBJ_FIND_NOT_FOUND; - - vcal = Parse_MIME (vcalobj, strlen (vcalobj)); - - if (!vcal) - return CAL_OBJ_FIND_SYNTAX_ERROR; - - initPropIterator (&i, vcal); - - while (moreIteration (&i)) { - VObject *vobj; - VObject *uid_prop; - char *the_str; - - vobj = nextVObject (&i); - - uid_prop = isAPropertyOf (vobj, VCUniqueStringProp); - if (!uid_prop) - continue; - - /* str_val() sets the_str to the string representation of the - * property. - */ - str_val (uid_prop); - - if (strcmp (the_str, uid) == 0) { - const char *object_name; - - object_name = vObjectName (vobj); - *ico = ical_object_create_from_vobject (vobj, object_name); - - if (*ico) - status = CAL_OBJ_FIND_SUCCESS; - } - - free (the_str); - - if (status == CAL_OBJ_FIND_SUCCESS) - break; - } - - cleanVObject (vcal); - cleanStrTbl (); - - return status; -#endif /* 1 */ -} - - -#if 1 -/* Creates a VObject with the base information of a calendar */ -static VObject * -get_calendar_base_vobject (void) -{ - VObject *vobj; - time_t now; - struct tm tm; - - /* We call localtime for the side effect of setting tzname */ - - now = time (NULL); - tm = *localtime (&now); - - vobj = newVObject (VCCalProp); - - addPropValue (vobj, VCProdIdProp, PRODID); - -#if defined (HAVE_TM_ZONE) - addPropValue (vobj, VCTimeZoneProp, tm.tm_zone); -#elif defined (HAVE_TZNAME) - addPropValue (vobj, VCTimeZoneProp, tzname[0]); -#endif - - /* Per the vCalendar spec, this must be "1.0" */ - addPropValue (vobj, VCVersionProp, "1.0"); - - return vobj; -} -#endif /* 0 */ - -/** - * ical_object_to_string: - * @ico: A calendar object. - * - * Converts a vCalendar object to its string representation. It is wrapped - * inside a complete VCALENDAR object because other auxiliary information such - * as timezones may appear there. - * - * Return value: String representation of the object. - **/ -char * -ical_object_to_string (iCalObject *ico) -{ -#if 0 - icalcomponent *top = icalcomponent_new (ICAL_VCALENDAR_COMPONENT); - char *out_cal_string; - icalcomponent *comp; - - printf ("ical_object_to_string:\n"); - printf ("-----------------------------------------------------\n"); - dump_icalobject (ico); - printf ("-----------------------------------------------------\n"); - - comp = icalcomponent_create_from_ical_object (ico); - icalcomponent_add_component (top, comp); - out_cal_string = icalcomponent_as_ical_string (top); - return g_strdup (out_cal_string); - -#else /* 1 */ - VObject *vcalobj, *vobj; - char *buf, *gbuf; - - vcalobj = get_calendar_base_vobject (); - vobj = ical_object_to_vobject (ico); - addVObjectProp (vcalobj, vobj); - - buf = writeMemVObject (NULL, NULL, vcalobj); - - cleanVObject (vcalobj); - cleanStrTbl (); - - /* We have to g_strdup() it because libversit uses malloc()/realloc(), - * and we want clients to be able to use g_free(). Sigh. - */ - gbuf = g_strdup (buf); - free (buf); - - return gbuf; -#endif /* 1 */ -} - - -/** - * ical_object_compare_dates: - * @ico1: A calendar event. - * @ico2: A calendar event to compare with @ico1. - * - * Returns TRUE if the dates of both objects match, including any recurrence - * rules. Both calendar objects must have a type of ICAL_EVENT. - * - * Return value: TRUE if both calendar objects have the same dates. - **/ -gboolean -ical_object_compare_dates (iCalObject *ico1, - iCalObject *ico2) -{ - Recurrence *recur1, *recur2; - gint num_exdates; - GList *elem1, *elem2; - time_t *time1, *time2; - - g_return_val_if_fail (ico1 != NULL, FALSE); - g_return_val_if_fail (ico2 != NULL, FALSE); - g_return_val_if_fail (ico1->type == ICAL_EVENT, FALSE); - g_return_val_if_fail (ico2->type == ICAL_EVENT, FALSE); - - /* First check the base dates. */ - if (ico1->dtstart != ico2->dtstart - || ico1->dtend != ico2->dtend) - return FALSE; - - recur1 = ico1->recur; - recur2 = ico2->recur; - - /* If the event doesn't recur, we already know it matches. */ - if (!recur1 && !recur2) - return TRUE; - - /* Check that both recur. */ - if (!(recur1 && recur2)) - return FALSE; - - /* Now we need to see if the recurrence rules are the same. */ - if (recur1->type != recur2->type - || recur1->interval != recur2->interval - || recur1->enddate != recur2->enddate - || recur1->weekday != recur2->weekday - || recur1->duration != recur2->duration - || recur1->_enddate != recur2->_enddate - || recur1->__count != recur2->__count) - return FALSE; - - switch (recur1->type) { - case RECUR_MONTHLY_BY_POS: - if (recur1->u.month_pos != recur2->u.month_pos) - return FALSE; - break; - case RECUR_MONTHLY_BY_DAY: - if (recur1->u.month_day != recur2->u.month_day) - return FALSE; - break; - default: - break; - } - - /* Now check if the excluded dates match. */ - num_exdates = g_list_length (ico1->exdate); - if (g_list_length (ico2->exdate) != num_exdates) - return FALSE; - if (num_exdates == 0) - return TRUE; - - ico1->exdate = g_list_sort (ico1->exdate, compare_exdates); - ico2->exdate = g_list_sort (ico2->exdate, compare_exdates); - - elem1 = ico1->exdate; - elem2 = ico2->exdate; - while (elem1) { - time1 = (time_t*) elem1->data; - time2 = (time_t*) elem2->data; - - if (*time1 != *time2) - return FALSE; - - elem1 = elem1->next; - elem2 = elem2->next; - } - - return TRUE; -} - - -static gint -compare_exdates (gconstpointer a, gconstpointer b) -{ - const time_t *ca = a, *cb = b; - time_t diff = *ca - *cb; - return (diff < 0) ? -1 : (diff > 0) ? 1 : 0; -} - - -/* Converts any CR/LF sequences in the summary field to spaces so we just - have a one-line field. The iCalObjects summary field is changed. */ -static void -ical_object_normalize_summary (iCalObject *ico) -{ - gchar *src, *dest, ch; - gboolean just_output_space = FALSE; - - src = dest = ico->summary; - while ((ch = *src++)) { - if (ch == '\n' || ch == '\r') { - /* We only output 1 space for each sequence of CR & LF - characters. */ - if (!just_output_space) { - *dest++ = ' '; - just_output_space = TRUE; - } - } else { - *dest++ = ch; - just_output_space = FALSE; - } - } - *dest = '\0'; -} - - -void dump_icalobject (iCalObject *ico) -{ - if (!ico) { - printf ("<<NULL>>\n"); - return; - } - - printf ("type "); - switch (ico->type) { - case ICAL_EVENT: printf ("event"); break; - case ICAL_TODO: printf ("todo"); break; - case ICAL_JOURNAL: printf ("journal"); break; - case ICAL_FBREQUEST: printf ("fbrequest"); break; - case ICAL_FBREPLY: printf ("fbreply"); break; - case ICAL_BUSYTIME: printf ("busytime"); break; - case ICAL_TIMEZONE: printf ("timezone"); break; - } - printf ("\n"); - - printf ("attach-length %d\n", g_list_length (ico->attach)); - - printf ("attendee-length %d\n", g_list_length (ico->attendee)); - - printf ("catagories-length %d\n", g_list_length (ico->categories)); - - printf ("class '%s'\n", ico->class ? ico->class : "NULL"); - - printf ("comment '%s'\n", ico->comment ? ico->comment : "NULL"); - - printf ("completed %ld=%s", - ico->completed, ctime (&ico->completed)); - - printf ("created %ld=%s", ico->created, ctime (&ico->created)); - - printf ("contact-length %d\n", g_list_length (ico->contact)); - - printf ("desc '%s'\n", ico->desc ? ico->desc : "NULL"); - - printf ("dtstamp %ld=%s", ico->dtstamp, ctime (&ico->dtstamp)); - - printf ("dtstart %ld=%s", ico->dtstart, ctime (&ico->dtstart)); - - printf ("dtend %ld=%s", ico->dtend, ctime (&ico->dtend)); - - printf ("date_only %d\n", ico->date_only); - - printf ("exdate-length %d\n", g_list_length (ico->exdate)); - - printf ("exrule-length %d\n", g_list_length (ico->exrule)); - - printf ("iCalGeo %d %f %f\n", - ico->geo.valid, ico->geo.latitude, ico->geo.longitude); - - printf ("last_mod %ld=%s", ico->last_mod, ctime (&ico->last_mod)); - - printf ("location '%s'\n", ico->location ? ico->location : "NULL"); - - printf ("organizer %p\n", ico->organizer); - - printf ("percent %d\n", ico->percent); - - printf ("priority %d\n", ico->priority); - - printf ("rstatus '%s'\n", ico->rstatus ? ico->rstatus : "NULL"); - - printf ("related-length %d\n", g_list_length (ico->related)); - - printf ("resources-length %d\n", g_list_length (ico->resources)); - - printf ("rdate-length %d\n", g_list_length (ico->rdate)); - - printf ("rrule-length %d\n", g_list_length (ico->rrule)); - - printf ("seq %d\n", ico->seq); - - printf ("status '%s'\n", ico->status ? ico->status : "NULL"); - - printf ("summary '%s'\n", ico->summary ? ico->summary : "NULL"); - - printf ("transp "); - switch (ico->transp) { - case ICAL_OPAQUE: printf ("opaque"); break; - case ICAL_TRANSPARENT: printf ("transparent"); break; - } - printf ("\n"); - - printf ("uid '%s'\n", ico->uid ? ico->uid : "NULL"); - - printf ("url '%s'\n", ico->url ? ico->url : "NULL"); - - printf ("recurid %ld=%s", ico->recurid, ctime (&ico->recurid)); - - printf ("dalarm %d\n", ico->dalarm.enabled); - - printf ("aalarm %d\n", ico->aalarm.enabled); - - printf ("palarm %d\n", ico->palarm.enabled); - - printf ("malarm %d\n", ico->malarm.enabled); - - printf ("alarms-length %d\n", g_list_length (ico->alarms)); - - printf ("recur %p\n", ico->recur); - - printf ("new %d\n", ico->new); - - printf ("user_data %p\n", ico->user_data); - - printf ("ref_count %d\n", ico->ref_count); -} diff --git a/calendar/cal-util/calobj.h b/calendar/cal-util/calobj.h deleted file mode 100644 index 3caef945e2..0000000000 --- a/calendar/cal-util/calobj.h +++ /dev/null @@ -1,312 +0,0 @@ -/* - * Internal representation of a Calendar object. This is modeled after the - * iCalendar/vCalendar specificiation - * - * Authors: Miguel de Icaza (miguel@gnu.org) - * Federico Mena (quartic@gimp.org). - */ -#ifndef CALOBJ_H -#define CALOBJ_H - -#include <libgnome/libgnome.h> -#include "libversit/vcc.h" - -BEGIN_GNOME_DECLS - -/* Alarm types */ -enum AlarmType { - ALARM_MAIL, - ALARM_PROGRAM, - ALARM_DISPLAY, - ALARM_AUDIO -}; - -/* Whether the alarm should trigger N mins/hours/days before its due time */ -enum AlarmUnit { - ALARM_MINUTES, - ALARM_HOURS, - ALARM_DAYS -}; - -/* Field identifiers for the iCalObject structure. These are also used to - identify columns in ECalendarTable, so be careful when reordering them. */ -typedef enum { - ICAL_OBJECT_FIELD_COMMENT, - ICAL_OBJECT_FIELD_COMPLETED, - ICAL_OBJECT_FIELD_CREATED, - ICAL_OBJECT_FIELD_DESCRIPTION, - ICAL_OBJECT_FIELD_DTSTAMP, - ICAL_OBJECT_FIELD_DTSTART, - ICAL_OBJECT_FIELD_DTEND, - ICAL_OBJECT_FIELD_GEO, - ICAL_OBJECT_FIELD_LAST_MOD, - ICAL_OBJECT_FIELD_LOCATION, - ICAL_OBJECT_FIELD_ORGANIZER, - ICAL_OBJECT_FIELD_PERCENT, - ICAL_OBJECT_FIELD_PRIORITY, - ICAL_OBJECT_FIELD_SUMMARY, - ICAL_OBJECT_FIELD_URL, - ICAL_OBJECT_FIELD_HAS_ALARMS, /* not a real field */ - ICAL_OBJECT_FIELD_ICON, /* not a real field */ - ICAL_OBJECT_FIELD_COMPLETE, /* not a real field */ - ICAL_OBJECT_FIELD_RECURRING, /* not a real field */ - ICAL_OBJECT_FIELD_OVERDUE, /* not a real field */ - ICAL_OBJECT_FIELD_COLOR, /* not a real field */ - ICAL_OBJECT_FIELD_NUM_FIELDS -} iCalObjectField; - -typedef struct { - enum AlarmType type; - int enabled; - int count; - enum AlarmUnit units; - char *data; /* not used for iCalendar alarms */ - - /* the following pointers are used for iCalendar alarms */ - - char *attach; /* AUDIO, EMAIL, PROC */ - char *desc; /* DISPLAY, EMAIL, PROC */ - char *summary; /* EMAIL */ - char *attendee; /* EMAIL */ - - /* Does not get saved, internally used */ - time_t offset; - time_t trigger; - - int snooze_secs; - int snooze_repeat; - - /* Widgets */ - void *w_count; /* A GtkEntry */ - void *w_enabled; /* A GtkChecButton */ - void *w_timesel; /* A GtkMenu */ - void *w_entry; /* A GnomeEntryFile/GtkEntry for PROGRAM/MAIL */ - void *w_label; -} CalendarAlarm; - -/* Calendar object type */ -typedef enum { - ICAL_EVENT, - ICAL_TODO, - ICAL_JOURNAL, - ICAL_FBREQUEST, - ICAL_FBREPLY, - ICAL_BUSYTIME, - ICAL_TIMEZONE -} iCalType; - -/* For keys that might contain binary or text/binary */ -typedef struct { - char *data; - int len; -} iCalValue; - -typedef enum { - ICAL_PILOT_SYNC_NONE = 0, - ICAL_PILOT_SYNC_MOD = 1, - ICAL_PILOT_SYNC_DEL = 3 -} iCalPilotState; - -typedef struct { - int valid; /* true if the Geography was specified */ - double latitude; - double longitude; -} iCalGeo; - -typedef enum { - ICAL_OPAQUE, - ICAL_TRANSPARENT -} iCalTransp; - -typedef struct { - char *uid; - char *reltype; -} iCalRelation; - -typedef char NotYet; - -enum RecurType { - RECUR_DAILY, - RECUR_WEEKLY, - RECUR_MONTHLY_BY_POS, - RECUR_MONTHLY_BY_DAY, - RECUR_YEARLY_BY_MONTH, - RECUR_YEARLY_BY_DAY, -}; - -#define DAY_LASTDAY 10000 - -typedef struct { - enum RecurType type; - - int interval; - - /* Used for recur computation */ - time_t enddate; /* If the value is zero, it is an infinite event - * otherwise, it is either the _enddate value (if - * this is what got specified) or it is our computed - * ending date (computed from the duration item). - */ - - int weekday; - - union { - int month_pos; - int month_day; - } u; - - int duration; - time_t _enddate; /* As found on the vCalendar file */ - int __count; -} Recurrence; - -/* - NOTE: iCalPerson is used for various property values which specify - people (e.g. ATTENDEE, ORGANIZER, etc. Not all fields are valid - under RFC 2445 for all property values, but iCalPerson can store - them anyway. Enforcing the RFC is a job for the parser. -*/ - -typedef struct { - char *addr; - char *name; - char *role; - char *partstat; - gboolean rsvp; - char *cutype; /* calendar user type */ - GList *member; /* group memberships */ - GList *deleg_to; - GList *deleg_from; - char *sent_by; - char *directory; - GList *altrep; /* list of char* URI's */ -} iCalPerson; - -#define IS_INFINITE(r) (r->duration == 0) - -/* Flags to indicate what has changed in an object */ -typedef enum { - CHANGE_NEW = 1 << 0, /* new object */ - CHANGE_SUMMARY = 1 << 1, /* summary */ - CHANGE_DATES = 1 << 2, /* dtstart / dtend */ - CHANGE_ALL = CHANGE_SUMMARY | CHANGE_DATES -} CalObjectChange; - -/* - * This describes an iCalendar object, note that we never store durations, instead we - * always compute the end time computed from the start + duration. - */ -typedef struct { - iCalType type; - - GList *attach; /* type: one or more URIs or binary data */ - GList *attendee; /* type: CAL-ADDRESS (list of iCalPerson) */ - GList *categories; /* type: one or more TEXT */ - char *class; - - char *comment; /* we collapse one or more TEXTs into one */ - time_t completed; - time_t created; - GList *contact; /* type: one or more TEXT */ - char *desc; - time_t dtstamp; - time_t dtstart; - time_t dtend; /* also duedate for todo's */ - gboolean date_only; /* set if the start/end times were - specified using dates, not times (internal use, not stored to disk) */ - GList *exdate; /* type: one or more time_t's */ - GList *exrule; /* type: one or more RECUR */ - iCalGeo geo; - time_t last_mod; - char *location; - iCalPerson *organizer; - int percent; - int priority; - char *rstatus; /* request status for freebusy */ - GList *related; /* type: one or more TEXT */ - GList *resources; /* type: one or more TEXT */ - GList *rdate; /* type: one or more recurrence date */ - GList *rrule; /* type: one or more recurrence rules */ - int seq; - char *status; - char *summary; - iCalTransp transp; - char *uid; - char *url; - time_t recurid; - - CalendarAlarm dalarm; - CalendarAlarm aalarm; - CalendarAlarm palarm; - CalendarAlarm malarm; - - GList *alarms; - - Recurrence *recur; - - int new; - void *user_data; /* Generic data pointer */ - - /* Pilot */ - iCalPilotState pilot_status; /* Status information */ - guint32 pilot_id; /* Pilot ID */ - - guint ref_count; -} iCalObject; - -/* The callback for the recurrence generator */ -typedef int (*calendarfn) (iCalObject *, time_t, time_t, void *); - -iCalObject *ical_new (char *comment, char *organizer, char *summary); -iCalObject *ical_object_new (void); - -void ical_object_ref (iCalObject *ico); -void ical_object_unref (iCalObject *ico); - -iCalObject *ical_object_create_from_vobject (VObject *obj, const char *object_name); -VObject *ical_object_to_vobject (iCalObject *ical); -iCalObject *ical_object_duplicate (iCalObject *o); -void ical_foreach (GList *events, calendarfn fn, void *closure); -void ical_object_generate_events (iCalObject *ico, time_t start, time_t end, calendarfn cb, void *closure); -void ical_object_add_exdate (iCalObject *o, time_t t); - -/* Computes the enddate field of the recurrence based on the duration */ -void ical_object_compute_end (iCalObject *ico); - -typedef enum { - CAL_OBJ_FIND_SUCCESS, - CAL_OBJ_FIND_SYNTAX_ERROR, - CAL_OBJ_FIND_NOT_FOUND -} CalObjFindStatus; - -CalObjFindStatus ical_object_find_in_string (const char *uid, const char *vcalobj, iCalObject **ico); - -char *ical_object_to_string (iCalObject *ico); - - -/* Returns the first toggled day in a weekday mask -- we do this because we do not support multiple - * days on a monthly-by-pos recurrence. If no days are toggled, it returns -1. - */ -int ical_object_get_first_weekday (int weekday_mask); - -/* Returns the number of seconds configured to trigger the alarm in advance to an event */ -int alarm_compute_offset (CalendarAlarm *a); - - -/* Returns TRUE if the dates of both objects match, including any recurrence - rules. */ -gboolean ical_object_compare_dates (iCalObject *ico1, iCalObject *ico2); - -/* Generates a new uid for a calendar object. Should be g_free'd eventually. */ -char *ical_gen_uid (void); - -/* This resets any recurrence rules of the iCalObject. */ -void ical_object_reset_recurrence (iCalObject *ico); - - -void dump_icalobject (iCalObject *ico); - -END_GNOME_DECLS - -#endif - diff --git a/calendar/cal-util/test-recur.c b/calendar/cal-util/test-recur.c deleted file mode 100644 index e84b0f9201..0000000000 --- a/calendar/cal-util/test-recur.c +++ /dev/null @@ -1,196 +0,0 @@ -/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ - -/* - * Author : - * Damon Chaplin <damon@helixcode.com> - * - * Copyright 2000, Helix Code, Inc. - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation; either version 2 of the - * License, or (at your option) any later version. - * - * 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 - */ - -/* - * This tests the recurrence rule expansion functions. - * - * NOTE: currently it starts from the event start date and continues - * until all recurrence rules/dates end or we reach MAX_OCCURRENCES - * occurrences. So it does not test generating occurrences for a specific - * interval. A nice addition might be to do this automatically and compare - * the results from the complete set to ensure they match. - */ - -#include <config.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <gtk/gtkmain.h> -#include <cal-util/cal-recur.h> - - -/* Since events can recur infinitely, we set a limit to the number of - occurrences we output. */ -#define MAX_OCCURRENCES 1000 - -static void usage (void); -static icalcomponent* scan_ics_file (char *filename); -static char* get_line (char *s, - size_t size, - void *data); -static void generate_occurrences (icalcomponent *comp); -static gboolean occurrence_cb (CalComponent *comp, - time_t instance_start, - time_t instance_end, - gpointer data); - - -int -main (int argc, - char *argv[]) -{ - gchar *filename; - icalcomponent *icalcomp; - - gtk_init (&argc, &argv); - - if (argc != 2) - usage (); - - filename = argv[1]; - - icalcomp = scan_ics_file (filename); - if (icalcomp) - generate_occurrences (icalcomp); - - return 0; -} - - -static void -usage (void) -{ - g_print ("Usage: test-recur <filename>\n"); - exit (1); -} - - -static icalcomponent* -scan_ics_file (char *filename) -{ - FILE *fp; - icalcomponent *icalcomp; - icalparser *parser; - - g_print ("Opening file: %s\n", filename); - fp = fopen (filename, "r"); - - if (!fp) { - g_print ("Can't open file: %s\n", filename); - return NULL; - } - - parser = icalparser_new (); - icalparser_set_gen_data (parser, fp); - - icalcomp = icalparser_parse (parser, get_line); - icalparser_free (parser); - - return icalcomp; -} - - -/* Callback used from icalparser_parse() */ -static char * -get_line (char *s, - size_t size, - void *data) -{ - return fgets (s, size, (FILE*) data); -} - - -static void -generate_occurrences (icalcomponent *icalcomp) -{ - icalcompiter iter; - - for (iter = icalcomponent_begin_component (icalcomp, ICAL_ANY_COMPONENT); - icalcompiter_deref (&iter) != NULL; - icalcompiter_next (&iter)) { - icalcomponent *tmp_icalcomp; - CalComponent *comp; - icalcomponent_kind kind; - gint occurrences; - - tmp_icalcomp = icalcompiter_deref (&iter); - kind = icalcomponent_isa (tmp_icalcomp); - - if (!(kind == ICAL_VEVENT_COMPONENT - || kind == ICAL_VTODO_COMPONENT - || kind == ICAL_VJOURNAL_COMPONENT)) - continue; - - comp = cal_component_new (); - - if (!cal_component_set_icalcomponent (comp, tmp_icalcomp)) - continue; - - g_print ("#############################################################################\n"); - g_print ("%s\n\n", icalcomponent_as_ical_string (tmp_icalcomp)); - g_print ("Instances:\n"); - - occurrences = 0; - /* I use specific times when I am trying to pin down a bug seen - in one of the calendar views. */ -#if 0 - cal_recur_generate_instances (comp, 982022400, 982108800, - occurrence_cb, &occurrences); -#else - cal_recur_generate_instances (comp, -1, -1, - occurrence_cb, &occurrences); -#endif - - /* Print the component again so we can see the - X-EVOLUTION-ENDDATE parameter (only set if COUNT is used). - */ - g_print ("#############################################################################\n"); -#if 0 - g_print ("%s\n\n", icalcomponent_as_ical_string (tmp_icalcomp)); -#endif - } -} - - -static gboolean -occurrence_cb (CalComponent *comp, - time_t instance_start, - time_t instance_end, - gpointer data) -{ - char start[32], finish[32]; - gint *occurrences; - - occurrences = (gint*) data; - - strcpy (start, ctime (&instance_start)); - start[24] = '\0'; - strcpy (finish, ctime (&instance_end)); - finish[24] = '\0'; - - g_print ("%s - %s\n", start, finish); - - (*occurrences)++; - return (*occurrences == MAX_OCCURRENCES) ? FALSE : TRUE; -} diff --git a/calendar/cal-util/timeutil.c b/calendar/cal-util/timeutil.c deleted file mode 100644 index 7bb4c904ec..0000000000 --- a/calendar/cal-util/timeutil.c +++ /dev/null @@ -1,395 +0,0 @@ -/* Miscellaneous time-related utilities - * - * Copyright (C) 1998 The Free Software Foundation - * Copyright (C) 2000 Helix Code, Inc. - * Copyright (C) 2000 Ximian, Inc. - * - * Authors: Federico Mena <federico@ximian.com> - * Miguel de Icaza <miguel@ximian.com> - */ - -#include <string.h> -#include <ctype.h> -#include <glib.h> -#include "timeutil.h" - - - -void -print_time_t (time_t t) -{ - struct tm *tm = localtime (&t); - - printf ("%d/%02d/%02d %02d:%02d:%02d", - 1900 + tm->tm_year, tm->tm_mon+1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec); -} - -/** - * isodate_from_time_t: - * @t: A time value. - * - * Creates an ISO 8601 local time representation from a time value. - * - * Return value: String with the ISO 8601 representation of the local time. - **/ -char * -isodate_from_time_t (time_t t) -{ - struct tm *tm; - char isotime[40]; - - tm = localtime (&t); - strftime (isotime, sizeof (isotime)-1, "%Y%m%dT%H%M%S", tm); - return g_strdup (isotime); -} - -/** - * time_from_isodate: - * @str: Date/time value in ISO 8601 format. - * - * Converts an ISO 8601 time string into a time_t value. - * - * Return value: Time_t corresponding to the specified ISO string. - **/ -time_t -time_from_isodate (const char *str) -{ - int len; - struct tm my_tm; - time_t t; - int i; - - g_return_val_if_fail (str != NULL, -1); - - /* yyyymmdd[Thhmmss[Z]] */ - - len = strlen (str); - - if (!(len == 8 || len == 15 || len == 16)) - return -1; - - for (i = 0; i < len; i++) - if (!((i != 8 && i != 15 && isdigit (str[i])) - || (i == 8 && str[i] == 'T') - || (i == 15 && str[i] == 'Z'))) - return -1; - - memset (&my_tm, 0, sizeof (my_tm)); - -#define digit_at(x,y) (x[y] - '0') - - my_tm.tm_year = (digit_at (str, 0) * 1000 + digit_at (str, 1) * 100 + - digit_at (str, 2) * 10 + digit_at (str, 3)) - 1900; - - my_tm.tm_mon = digit_at (str, 4) * 10 + digit_at (str, 5) - 1; - my_tm.tm_mday = digit_at (str, 6) * 10 + digit_at (str, 7); - - if (len > 8) { - my_tm.tm_hour = digit_at (str, 9) * 10 + digit_at (str, 10); - my_tm.tm_min = digit_at (str, 11) * 10 + digit_at (str, 12); - my_tm.tm_sec = digit_at (str, 13) * 10 + digit_at (str, 14); - } - - my_tm.tm_isdst = -1; - - t = mktime (&my_tm); - - if (len == 16) { -#if defined(HAVE_TM_GMTOFF) - t += my_tm.tm_gmtoff; -#elif defined(HAVE_TIMEZONE) - t -= timezone; -#endif - } - - return t; -} - -time_t -time_add_minutes (time_t time, int minutes) -{ - struct tm *tm = localtime (&time); - time_t new_time; - - tm->tm_min += minutes; - if ((new_time = mktime (tm)) == -1) { - g_message ("time_add_minutes(): mktime() could not handle " - "adding %d minutes with\n", minutes); - print_time_t (time); - printf ("\n"); - return time; - } - return new_time; -} - -/* Adds a day onto the time, using local time. - Note that if clocks go forward due to daylight savings time, there are - some non-existent local times, so the hour may be changed to make it a - valid time. This also means that it may not be wise to keep calling - time_add_day() to step through a certain period - if the hour gets changed - to make it valid time, any further calls to time_add_day() will also return - this hour, which may not be what you want. */ -time_t -time_add_day (time_t time, int days) -{ - struct tm *tm = localtime (&time); - time_t new_time; -#if 0 - int dst_flag = tm->tm_isdst; -#endif - - tm->tm_mday += days; - tm->tm_isdst = -1; - - if ((new_time = mktime (tm)) == -1) { - g_message ("time_add_day(): mktime() could not handling adding %d days with\n", - days); - print_time_t (time); - printf ("\n"); - return time; - } - -#if 0 - /* I don't know what this is for. See also time_day_begin() and - time_day_end(). - Damon. */ - if (dst_flag > tm->tm_isdst) { - tm->tm_hour++; - new_time += 3600; - } else if (dst_flag < tm->tm_isdst) { - tm->tm_hour--; - new_time -= 3600; - } -#endif - - return new_time; -} - -time_t -time_add_week (time_t time, int weeks) -{ - return time_add_day (time, weeks * 7); -} - -time_t -time_add_month (time_t time, int months) -{ - struct tm *tm = localtime (&time); - time_t new_time; - int mday; - - mday = tm->tm_mday; - - tm->tm_mon += months; - tm->tm_isdst = -1; - if ((new_time = mktime (tm)) == -1) { - g_message ("time_add_month(): mktime() could not handling adding %d months with\n", - months); - print_time_t (time); - printf ("\n"); - return time; - } - tm = localtime (&new_time); - if (tm->tm_mday < mday) { - tm->tm_mon--; - tm->tm_mday = time_days_in_month (tm->tm_year+1900, tm->tm_mon); - return new_time = mktime (tm); - } - else - return new_time; -} - -time_t -time_add_year (time_t time, int years) -{ - struct tm *tm = localtime (&time); - time_t new_time; - - tm->tm_year += years; - if ((new_time = mktime (tm)) == -1) { - g_message ("time_add_year(): mktime() could not handling adding %d years with\n", - years); - print_time_t (time); - printf ("\n"); - return time; - } - return new_time; -} - -/* Number of days in a month, for normal and leap years */ -static const int days_in_month[2][12] = { - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, - { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } -}; - -/* Returns whether the specified year is a leap year */ -static int -is_leap_year (int year) -{ - if (year <= 1752) - return !(year % 4); - else - return (!(year % 4) && (year % 100)) || !(year % 400); -} - -int -time_days_in_month (int year, int month) -{ - g_return_val_if_fail (year >= 1900, 0); - g_return_val_if_fail ((month >= 0) && (month < 12), 0); - - return days_in_month [is_leap_year (year)][month]; -} - -time_t -time_from_day (int year, int month, int day) -{ - struct tm tm; - - memset (&tm, 0, sizeof (tm)); - tm.tm_year = year - 1900; - tm.tm_mon = month; - tm.tm_mday = day; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -time_t -time_year_begin (time_t t) -{ - struct tm tm; - - tm = *localtime (&t); - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_mon = 0; - tm.tm_mday = 1; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -time_t -time_year_end (time_t t) -{ - struct tm tm; - - tm = *localtime (&t); - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_mon = 0; - tm.tm_mday = 1; - tm.tm_year++; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -time_t -time_month_begin (time_t t) -{ - struct tm tm; - - tm = *localtime (&t); - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_mday = 1; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -time_t -time_month_end (time_t t) -{ - struct tm tm; - - tm = *localtime (&t); - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_mday = 1; - tm.tm_mon++; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -/* Returns the start of the week. week_start_day should use the same values - as mktime(), i.e. 0 (Sun) to 6 (Sat). */ -time_t -time_week_begin (time_t t, int week_start_day) -{ - struct tm tm; - int offset; - - tm = *localtime (&t); - - /* Calculate the current offset from the week start day. */ - offset = (tm.tm_wday + 7 - week_start_day) % 7; - - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_mday -= offset; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -/* Returns the end of the week. week_start_day should use the same values - as mktime(), i.e. 0 (Sun) to 6 (Sat). */ -time_t -time_week_end (time_t t, int week_start_day) -{ - struct tm tm; - int offset; - - tm = *localtime (&t); - - /* Calculate the current offset from the week start day. */ - offset = (tm.tm_wday + 7 - week_start_day) % 7; - - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_mday += 7 - offset; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -/* Returns the start of the day, according to the local time. */ -time_t -time_day_begin (time_t t) -{ - struct tm tm; - - tm = *localtime (&t); - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_isdst = -1; - - return mktime (&tm); -} - -/* Returns the end of the day, according to the local time. */ -time_t -time_day_end (time_t t) -{ - struct tm tm; - - tm = *localtime (&t); - tm.tm_mday++; - tm.tm_hour = 0; - tm.tm_min = 0; - tm.tm_sec = 0; - tm.tm_isdst = -1; - - return mktime (&tm); -} diff --git a/calendar/cal-util/timeutil.h b/calendar/cal-util/timeutil.h deleted file mode 100644 index 3907a11b8c..0000000000 --- a/calendar/cal-util/timeutil.h +++ /dev/null @@ -1,65 +0,0 @@ -/* Miscellaneous time-related utilities - * - * Copyright (C) 1998 The Free Software Foundation - * Copyright (C) 2000 Helix Code, Inc. - * Copyright (C) 2000 Ximian, Inc. - * - * Authors: Federico Mena <federico@ximian.com> - * Miguel de Icaza <miguel@ximian.com> - */ - -#ifndef TIMEUTIL_H -#define TIMEUTIL_H - - -#include <time.h> -#include <ical.h> - - -char *isodate_from_time_t (time_t t); -time_t time_from_isodate (const char *str); - -time_t time_add_minutes (time_t time, int minutes); -time_t time_add_day (time_t time, int days); -time_t time_add_week (time_t time, int weeks); -time_t time_add_month (time_t time, int months); -time_t time_add_year (time_t time, int years); - - -/* Returns the number of days in the specified month. Years are full years - (starting from year 1). Months are in [0, 11]. */ -int time_days_in_month (int year, int month); - -/* Converts the specified date to a time_t at the start of the specified day. - Years are full years (starting from year 1). Months are in [0, 11]. - Days are 1-based. */ -time_t time_from_day (int year, int month, int day); - -/* For the functions below, time ranges are considered to contain the start - time, but not the end time. */ - -/* These two functions take a time value and return the beginning or end of - the corresponding year, respectively. */ -time_t time_year_begin (time_t t); -time_t time_year_end (time_t t); - -/* These two functions take a time value and return the beginning or end of - the corresponding month, respectively. */ -time_t time_month_begin (time_t t); -time_t time_month_end (time_t t); - -/* These functions take a time value and return the beginning or end of the - corresponding week, respectively. week_start_day should use the same values - as mktime(), i.e. 0 (Sun) to 6 (Sat). */ -time_t time_week_begin (time_t t, int week_start_day); -time_t time_week_end (time_t t, int week_start_day); - -/* These two functions take a time value and return the beginning or end of - the corresponding day, respectively. */ -time_t time_day_begin (time_t t); -time_t time_day_end (time_t t); - -void print_time_t (time_t t); - - -#endif |