[gnome-calendar/wip/gbsneto/gcal-event: 2/2] event: add a wrapper around events



commit 672d9b358d56b9b6b31d8a4249f1245a9eb2120e
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sun Feb 7 12:32:03 2016 -0200

    event: add a wrapper around events
    
    Instead of relying on confusing structures, raw data
    and direct access to components, add a wrapper for
    events.

 src/Makefile.am  |    2 +
 src/gcal-event.c |  796 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcal-event.h |   79 ++++++
 src/gcal-utils.c |   24 ++
 src/gcal-utils.h |    3 +
 5 files changed, 904 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 9f1fb5a..d465ece 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -34,6 +34,8 @@ gnome_calendar_SOURCES =                                  \
     gcal-date-selector.h                                  \
     gcal-edit-dialog.c                                    \
     gcal-edit-dialog.h                                    \
+    gcal-event.c                                          \
+    gcal-event.h                                          \
     gcal-event-widget.c                                   \
     gcal-event-widget.h                                   \
     gcal-manager.c                                        \
diff --git a/src/gcal-event.c b/src/gcal-event.c
new file mode 100644
index 0000000..640ee77
--- /dev/null
+++ b/src/gcal-event.c
@@ -0,0 +1,796 @@
+/* gcal-event.c
+ *
+ * Copyright (C) 2016 Georges Basile Stavracas Neto <georges stavracas gmail 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gcal-event.h"
+#include "gcal-utils.h"
+
+struct _GcalEvent
+{
+  GObject             parent;
+
+  /*
+   * The description is cached in the class because it
+   * may come as a GSList of descriptions, in which
+   * case we merge them and cache it here.
+   */
+  gchar              *description;
+
+  GTimeZone          *timezone;
+  GDateTime          *dt_start;
+  GDateTime          *dt_end;
+
+  gboolean            all_day : 1;
+
+  ECalComponent      *component;
+  ESource            *source;
+};
+
+G_DEFINE_TYPE (GcalEvent, gcal_event, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_ALL_DAY,
+  PROP_COMPONENT,
+  PROP_DESCRIPTION,
+  PROP_DATE_END,
+  PROP_DATE_START,
+  PROP_LOCATION,
+  PROP_SOURCE,
+  PROP_SUMMARY,
+  PROP_TIMEZONE,
+  N_PROPS
+};
+
+static void
+gcal_event_set_component_internal (GcalEvent     *self,
+                                   ECalComponent *component)
+{
+  if (g_set_object (&self->component, component))
+    {
+      ECalComponentDateTime start;
+      ECalComponentDateTime end;
+      GDateTime *date_start;
+      GDateTime *date_end;
+      gchar *description;
+
+      /* Setup start date */
+      e_cal_component_get_dtstart (component, &start);
+      date_start = icaltime_to_datetime (start.value, &self->timezone);
+
+      gcal_event_set_date_start (self, date_start);
+
+      /* Setup end date */
+      e_cal_component_get_dtend (component, &end);
+      date_end = icaltime_to_datetime (start.value, NULL);
+
+      gcal_event_set_date_end (self, date_end);
+
+      /* Setup all day */
+      gcal_event_set_all_day (self, start.value->is_date || end.value->is_date);
+
+      /* Setup description */
+      description = get_desc_from_component (component, "\n");
+      gcal_event_set_description (self, description);
+
+      g_object_notify (G_OBJECT (self), "component");
+      g_object_notify (G_OBJECT (self), "location");
+      g_object_notify (G_OBJECT (self), "summary");
+
+      g_clear_pointer (&date_start, g_date_time_unref);
+      g_clear_pointer (&date_end, g_date_time_unref);
+      g_clear_pointer (&description, g_free);
+    }
+}
+
+static void
+gcal_event_set_source_internal (GcalEvent *self,
+                                ESource   *source)
+{
+  if (g_set_object (&self->source, source))
+    g_object_notify (G_OBJECT (self), "source");
+}
+
+static void
+gcal_event_finalize (GObject *object)
+{
+  GcalEvent *self = (GcalEvent *)object;
+
+  g_clear_pointer (&self->dt_start, g_date_time_unref);
+  g_clear_pointer (&self->dt_end, g_date_time_unref);
+  g_clear_pointer (&self->timezone, g_time_zone_unref);
+  g_clear_pointer (&self->description, g_free);
+  g_clear_object (&self->component);
+  g_clear_object (&self->source);
+
+  G_OBJECT_CLASS (gcal_event_parent_class)->finalize (object);
+}
+
+static void
+gcal_event_get_property (GObject    *object,
+                         guint       prop_id,
+                         GValue     *value,
+                         GParamSpec *pspec)
+{
+  GcalEvent *self = GCAL_EVENT (object);
+
+  switch (prop_id)
+    {
+    case PROP_ALL_DAY:
+      g_value_set_boolean (value, self->all_day);
+      break;
+
+    case PROP_COMPONENT:
+      g_value_set_object (value, self->component);
+      break;
+
+    case PROP_DATE_END:
+      g_value_set_boxed (value, self->dt_end);
+      break;
+
+    case PROP_DATE_START:
+      g_value_set_boxed (value, self->dt_start);
+      break;
+
+    case PROP_DESCRIPTION:
+      g_value_set_string (value, gcal_event_get_description (self));
+      break;
+
+    case PROP_LOCATION:
+      g_value_set_string (value, gcal_event_get_location (self));
+      break;
+
+    case PROP_SOURCE:
+      g_value_set_object (value, self->source);
+      break;
+
+    case PROP_SUMMARY:
+      g_value_set_string (value, gcal_event_get_summary (self));
+      break;
+
+    case PROP_TIMEZONE:
+      g_value_set_boxed (value, self->timezone);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gcal_event_set_property (GObject      *object,
+                         guint         prop_id,
+                         const GValue *value,
+                         GParamSpec   *pspec)
+{
+  GcalEvent *self = GCAL_EVENT (object);
+
+  switch (prop_id)
+    {
+    case PROP_ALL_DAY:
+      gcal_event_set_all_day (self, g_value_get_boolean (value));
+      break;
+
+    case PROP_COMPONENT:
+      gcal_event_set_component_internal (self, g_value_get_object (value));
+      break;
+
+    case PROP_DATE_END:
+      gcal_event_set_date_end (self, g_value_get_boxed (value));
+      break;
+
+    case PROP_DATE_START:
+      gcal_event_set_date_start (self, g_value_get_boxed (value));
+      break;
+
+    case PROP_DESCRIPTION:
+      gcal_event_set_description (self, g_value_get_string (value));
+      break;
+
+    case PROP_LOCATION:
+      gcal_event_set_location (self, g_value_get_string (value));
+      break;
+
+    case PROP_SOURCE:
+      gcal_event_set_source_internal (self, g_value_get_object (value));
+      break;
+
+    case PROP_SUMMARY:
+      gcal_event_set_summary (self, g_value_get_string (value));
+      break;
+
+    case PROP_TIMEZONE:
+      gcal_event_set_timezone (self, g_value_get_boxed (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gcal_event_class_init (GcalEventClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->finalize = gcal_event_finalize;
+  object_class->get_property = gcal_event_get_property;
+  object_class->set_property = gcal_event_set_property;
+
+  /**
+   * GcalEvent::all-day:
+   *
+   * Whether the event is all day or not.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_ALL_DAY,
+                                   g_param_spec_boolean ("all-day",
+                                                         "If event is all day",
+                                                         "Whether the event is all day or not",
+                                                         FALSE,
+                                                         G_PARAM_READWRITE));
+
+  /**
+   * GcalEvent::component:
+   *
+   * The #ECalComponent of this event.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_COMPONENT,
+                                   g_param_spec_object ("component",
+                                                        "Component",
+                                                        "The ECalComponent of the event",
+                                                        E_TYPE_CAL_COMPONENT,
+                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
+
+  /**
+   * GcalEvent::date-end:
+   *
+   * The end date of the event.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_DATE_END,
+                                   g_param_spec_boxed ("date-end",
+                                                       "End date of the event",
+                                                       "The end date of the event",
+                                                       G_TYPE_DATE_TIME,
+                                                       G_PARAM_READWRITE));
+
+  /**
+   * GcalEvent::date-start:
+   *
+   * The start date of the event.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_DATE_START,
+                                   g_param_spec_boxed ("date-start",
+                                                       "Start date of the event",
+                                                       "The start date of the event",
+                                                       G_TYPE_DATE_TIME,
+                                                       G_PARAM_READWRITE));
+
+  /**
+   * GcalEvent::description:
+   *
+   * The description of the event.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_DESCRIPTION,
+                                   g_param_spec_string ("description",
+                                                        "Description of the event",
+                                                        "The description of the event",
+                                                        "",
+                                                        G_PARAM_READWRITE));
+
+  /**
+   * GcalEvent::location:
+   *
+   * The location of the event.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_LOCATION,
+                                   g_param_spec_string ("location",
+                                                        "Location of the event",
+                                                        "The location of the event",
+                                                        "",
+                                                        G_PARAM_READWRITE));
+
+  /**
+   * GcalEvent::source:
+   *
+   * The #ESource this event belongs to.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_SOURCE,
+                                   g_param_spec_object ("source",
+                                                        "ESource",
+                                                        "The ESource this event belongs to",
+                                                        E_TYPE_SOURCE,
+                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
+
+  /**
+   * GcalEvent::summary:
+   *
+   * The summary of the event.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_SUMMARY,
+                                   g_param_spec_string ("summary",
+                                                        "Summary of the event",
+                                                        "The summary of the event",
+                                                        "",
+                                                        G_PARAM_READWRITE));
+
+  /**
+   * GcalEvent::timezone:
+   *
+   * The timezone of the event.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_TIMEZONE,
+                                   g_param_spec_boxed ("timezone",
+                                                       "Timezone of the event",
+                                                       "The timezone of the event",
+                                                       G_TYPE_TIME_ZONE,
+                                                       G_PARAM_READWRITE));
+}
+
+static void
+gcal_event_init (GcalEvent *self)
+{
+  ;
+}
+
+/**
+ * gcal_event_new:
+ * @source: (nullable): an #ESource
+ * @component: a #ECalComponent
+ *
+ * Creates a new event which belongs to @source and
+ * is represented by @component. New events will have
+ * a %NULL @source.
+ *
+ * Returns: (transfer full): a #GcalEvent
+ */
+GcalEvent*
+gcal_event_new (ESource       *source,
+                ECalComponent *component)
+{
+  return g_object_new (GCAL_TYPE_EVENT,
+                       "source", source,
+                       "component", component,
+                       NULL);
+}
+
+/**
+ * gcal_event_get_all_day:
+ * @self: a #GcalEvent
+ *
+ * Retrieves whether the event is all day or not.
+ *
+ * Returns: %TRUE if @self is all day, %FALSE otherwise.
+ */
+gboolean
+gcal_event_get_all_day (GcalEvent *self)
+{
+  g_return_val_if_fail (GCAL_IS_EVENT (self), FALSE);
+
+  return self->all_day;
+}
+
+/**
+ * gcal_event_set_all_day:
+ * @self: a #GcalEvent
+ * @all_day: whether the event is all day or not
+ *
+ * Sets the @GcalEvent::all-day property.
+ */
+void
+gcal_event_set_all_day (GcalEvent *self,
+                        gboolean   all_day)
+{
+  g_return_if_fail (GCAL_IS_EVENT (self));
+
+  if (self->all_day != !!all_day)
+    {
+      self->all_day = !!all_day;
+
+      g_object_notify (G_OBJECT (self), "all-day");
+    }
+}
+
+/**
+ * gcal_event_get_date_end:
+ * @self: a #GcalEvent
+ *
+ * Retrieves the end date of @self.
+ *
+ * Returns: (transfer none): a #GDateTime.
+ */
+GDateTime*
+gcal_event_get_date_end (GcalEvent *self)
+{
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  return self->dt_end;
+}
+
+/**
+ * gcal_event_set_date_end:
+ * @self: a #GcalEvent
+ * @dt: a #GDateTime
+ *
+ * Sets the end date as @dt.
+ */
+void
+gcal_event_set_date_end (GcalEvent *self,
+                         GDateTime *dt)
+{
+  g_return_if_fail (GCAL_IS_EVENT (self));
+
+  if (!g_date_time_equal (self->dt_end, dt))
+    {
+      g_clear_pointer (&self->dt_end, g_date_time_unref);
+      self->dt_end = g_date_time_ref (dt);
+
+      g_object_notify (G_OBJECT (self), "date-end");
+    }
+}
+
+/**
+ * gcal_event_get_date_start:
+ * @self: a #GcalEvent
+ *
+ * Retrieves the start date of @self.
+ *
+ * Returns: (transfer none): a #GDateTime.
+ */
+GDateTime*
+gcal_event_get_date_start (GcalEvent *self)
+{
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  return self->dt_start;
+}
+
+/**
+ * gcal_event_set_date_start:
+ * @self: a #GcalEvent
+ * @dt: a #GDateTime
+ *
+ * Sets the start date as @dt.
+ */
+void
+gcal_event_set_date_start (GcalEvent *self,
+                           GDateTime *dt)
+{
+  g_return_if_fail (GCAL_IS_EVENT (self));
+
+  if (!g_date_time_equal (self->dt_start, dt))
+    {
+      g_clear_pointer (&self->dt_start, g_date_time_unref);
+      self->dt_start = g_date_time_ref (dt);
+
+      g_object_notify (G_OBJECT (self), "date-start");
+    }
+}
+
+/**
+ * gcal_event_get_description:
+ * @self a #GcalEvent
+ *
+ * Retrieves the description of @self.
+ *
+ * Returns: (transfer none): the description of the event.
+ */
+const gchar*
+gcal_event_get_description (GcalEvent *self)
+{
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  return self->description ? self->description : "";
+}
+
+/**
+ * gcal_event_set_description:
+ * @self: a #GcalEvent
+ * @description: (nullable): the new description of the event
+ *
+ * Sets the description of the event.
+ */
+void
+gcal_event_set_description (GcalEvent   *self,
+                            const gchar *description)
+{
+  g_return_if_fail (GCAL_IS_EVENT (self));
+
+  if (g_strcmp0 (self->description, description) != 0)
+    {
+      ECalComponentText text_component;
+      GSList list;
+
+      self->description = g_strdup (description);
+
+      text_component.value = description;
+      text_component.altrep = NULL;
+
+      list.data = &text_component;
+      list.next = NULL;
+
+      e_cal_component_set_description_list (self->component, &list);
+      e_cal_component_commit_sequence (self->component);
+
+      g_object_notify (G_OBJECT (self), "description");
+    }
+}
+
+/**
+ * gcal_event_get_location:
+ * @self: a #GcalEvent
+ *
+ * Retrieves the location of the event.
+ *
+ * Returns: (transfer none): the location of the event
+ */
+const gchar*
+gcal_event_get_location (GcalEvent *self)
+{
+  const gchar *location;
+
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  e_cal_component_get_location (self->component, &location);
+
+  return location ? location : "";
+}
+
+/**
+ * gcal_event_set_location:
+ * @self: a #GcalEvent
+ * @location: (nullable): the new location of the event
+ *
+ * Sets the location of the event.
+ */
+void
+gcal_event_set_location (GcalEvent   *self,
+                         const gchar *location)
+{
+  const gchar *current_location;
+
+  g_return_if_fail (GCAL_IS_EVENT (self));
+
+  current_location = gcal_event_get_location (self);
+
+  if (g_strcmp0 (current_location, location) != 0)
+    {
+      e_cal_component_set_location (self->component, location);
+
+      g_object_notify (G_OBJECT (self), "location");
+    }
+}
+
+/**
+ * gcal_event_get_summary:
+ * @self: a #GcalEvent
+ *
+ * Retrieves the summary of this event.
+ *
+ * Returns: (transfer none): the summary of the event.
+ */
+const gchar*
+gcal_event_get_summary (GcalEvent *self)
+{
+  ECalComponentText summary;
+
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  e_cal_component_get_summary (self->component, &summary);
+
+  return summary.value ? summary.value : "";
+}
+
+/**
+ * gcal_event_set_summary:
+ * @event: a #GcalEvent
+ * @summary: (nullable): the new summary of @event.
+ *
+ * Sets the summary of @event.
+ */
+void
+gcal_event_set_summary (GcalEvent   *self,
+                        const gchar *summary)
+{
+  const gchar *current_summary;
+
+  g_return_if_fail (GCAL_IS_EVENT (self));
+
+  current_summary = gcal_event_get_summary (self);
+
+  if (g_strcmp0 (current_summary, summary) != 0)
+    {
+      ECalComponentText text_component;
+
+      text_component.value = summary ? summary : "";
+      text_component.altrep = NULL;
+
+      e_cal_component_set_summary (self->component, &text_component);
+      e_cal_component_commit_sequence (self->component);
+
+      g_object_notify (G_OBJECT (self), "summary");
+    }
+}
+
+/**
+ * gcal_event_get_timezone:
+ * @self: a #GcalEvent
+ *
+ * Retrieves the event's timezone.
+ *
+ * Returns: (transfer none): a #GTimeZone
+ */
+GTimeZone*
+gcal_event_get_timezone (GcalEvent *self)
+{
+  g_return_val_if_fail (GCAL_IS_EVENT (self), NULL);
+
+  return self->timezone;
+}
+
+/**
+ * gcal_event_set_timezone:
+ * @self: a #GcalEvent
+ * @timezone: a #GTimeZone
+ *
+ * Sets the timezone of the event to @timezone.
+ */
+void
+gcal_event_set_timezone (GcalEvent *self,
+                         GTimeZone *timezone)
+{
+  g_return_if_fail (GCAL_IS_EVENT (self));
+
+  if (self->timezone != timezone)
+    {
+      g_clear_pointer (&self->timezone, g_time_zone_unref);
+      self->timezone = g_time_zone_ref (timezone);
+
+      g_object_notify (G_OBJECT (self), "timezone");
+    }
+}
+
+/**
+ * gcal_event_compare:
+ * @event1: a #GcalEvent
+ * @event2: a #GcalEvent
+ *
+ * Compare @event1 and @event2. It compares the start dates of
+ * the events and, when they have the same date (time is ignored),
+ * the @GcalEvent::all-day is the tiebreaker criteria.
+ *
+ * Events with equal start dates are sorted by the length.
+ *
+ * Returns: 1, 0 or -1 if @event1 is, respectively, lower, equal or
+ * greater than @event2.
+ */
+gint
+gcal_event_compare (GcalEvent *event1,
+                    GcalEvent *event2)
+{
+  if (!event1 && !event2)
+    return 0;
+  if (!event1)
+    return 1;
+  else if (!event2)
+    return -1;
+
+  /*
+   * When the start date is the same, the priority is:
+   *  1. All day events (with largest time range)
+   *  2. All day events (with shortest time range)
+   *  3. Timed events (with largest time range)
+   *  4. Timed events (with shortest time range)
+   */
+  if (g_date_time_get_year (event1->dt_start) == g_date_time_get_year (event2->dt_start) &&
+      g_date_time_get_month (event1->dt_start) == g_date_time_get_month (event2->dt_start) &&
+      g_date_time_get_day_of_month (event1->dt_start) == g_date_time_get_day_of_month (event2->dt_start))
+    {
+      GTimeSpan span1, span2;
+
+      span1 = g_date_time_difference (event1->dt_start, event1->dt_end);
+      span2 = g_date_time_difference (event2->dt_start, event2->dt_end);
+
+      if (event1->all_day && event2->all_day)
+        {
+          // Case 1 & 2
+          return span1 - span2;
+        }
+      else if (event1->all_day)
+        {
+          return 1;
+        }
+      else if (event2->all_day)
+        {
+          return -1;
+        }
+      else
+        {
+          // Case 3 & 4
+          gint result;
+
+          result = g_date_time_compare (event1->dt_start, event2->dt_start);
+
+          if (result == 0)
+            return span1 - span2;
+          else
+            return result;
+        }
+    }
+
+  return g_date_time_compare (event1->dt_start, event2->dt_start);
+}
+
+/**
+ * gcal_event_compare_with_current:
+ * @event1: a #GcalEvent
+ * @event2: a #GcalEvent
+ * @current_time: the current time
+ *
+ * Compares @event1 and @event2 related to @current_time; the closest
+ * an event is from @current_time, the more priority it has.
+ *
+ * Returns: -1, 0 and 1 if @event1 is, respectively, farther, equal or
+ * closer from @current_time relative to @event2.
+ */
+gint
+gcal_event_compare_with_current (GcalEvent *event1,
+                                 GcalEvent *event2,
+                                 time_t    *current_time)
+{
+  time_t time1, time2;
+  time_t diff1, diff2;
+
+  if (!event1 && !event2)
+    return 0;
+  if (!event1)
+    return 1;
+  else if (!event2)
+    return -1;
+
+  time1 = g_date_time_to_unix (event1->dt_start);
+  time2 = g_date_time_to_unix (event2->dt_start);
+  diff1 = time1 - *current_time;
+  diff2 = time2 - *current_time;
+
+  if (diff1 != diff2)
+    {
+      if (diff1 == 0)
+        return -1;
+      else if (diff2 == 0)
+        return 1;
+
+      if (diff1 > 0 && diff2 < 0)
+        return -1;
+      else if (diff2 > 0 && diff1 < 0)
+        return 1;
+      else if (diff1 < 0 && diff2 < 0)
+        return ABS (diff1) - ABS (diff2);
+      else if (diff1 > 0 && diff2 > 0)
+        return diff1 - diff2;
+    }
+
+  return 0;
+}
diff --git a/src/gcal-event.h b/src/gcal-event.h
new file mode 100644
index 0000000..df9a2ce
--- /dev/null
+++ b/src/gcal-event.h
@@ -0,0 +1,79 @@
+/* gcal-event.h
+ *
+ * Copyright (C) 2016 Georges Basile Stavracas Neto <georges stavracas gmail 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GCAL_EVENT_H
+#define GCAL_EVENT_H
+
+#include <glib-object.h>
+#include <libecal/libecal.h>
+#include <libedataserver/libedataserver.h>
+
+G_BEGIN_DECLS
+
+#define GCAL_TYPE_EVENT (gcal_event_get_type())
+
+G_DECLARE_FINAL_TYPE (GcalEvent, gcal_event, GCAL, EVENT, GObject)
+
+GcalEvent*           gcal_event_new                              (ESource            *source,
+                                                                  ECalComponent      *component);
+
+gboolean             gcal_event_get_all_day                      (GcalEvent          *self);
+
+void                 gcal_event_set_all_day                      (GcalEvent          *self,
+                                                                  gboolean            all_day);
+
+GDateTime*           gcal_event_get_date_end                     (GcalEvent          *self);
+
+void                 gcal_event_set_date_end                     (GcalEvent          *self,
+                                                                  GDateTime          *dt);
+
+GDateTime*           gcal_event_get_date_start                   (GcalEvent          *self);
+
+void                 gcal_event_set_date_start                   (GcalEvent          *self,
+                                                                  GDateTime          *dt);
+
+const gchar*         gcal_event_get_description                  (GcalEvent          *self);
+
+void                 gcal_event_set_description                  (GcalEvent          *self,
+                                                                  const gchar        *description);
+
+const gchar*         gcal_event_get_location                     (GcalEvent          *self);
+
+void                 gcal_event_set_location                     (GcalEvent          *self,
+                                                                  const gchar        *location);
+
+const gchar*         gcal_event_get_summary                      (GcalEvent          *self);
+
+void                 gcal_event_set_summary                      (GcalEvent          *self,
+                                                                  const gchar        *summary);
+
+GTimeZone*           gcal_event_get_timezone                     (GcalEvent          *self);
+
+void                 gcal_event_set_timezone                     (GcalEvent          *self,
+                                                                  GTimeZone          *timezone);
+
+gint                 gcal_event_compare                          (GcalEvent          *event1,
+                                                                  GcalEvent          *event2);
+
+gint                 gcal_event_compare_with_current             (GcalEvent          *event1,
+                                                                  GcalEvent          *event2,
+                                                                  time_t             *current_time);
+
+G_END_DECLS
+
+#endif /* GCAL_EVENT_H */
diff --git a/src/gcal-utils.c b/src/gcal-utils.c
index 7969f28..b3e1ccf 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -63,6 +63,30 @@ month_item[12] =
 
 G_DEFINE_BOXED_TYPE (icaltimetype, icaltime, gcal_dup_icaltime, g_free)
 
+GDateTime*
+icaltime_to_datetime (const icaltimetype  *date,
+                      GTimeZone          **timezone)
+{
+  GDateTime *dt;
+  GTimeZone *tz;
+
+  tz = date->zone ? g_time_zone_new (icaltime_get_tzid (*date)) : g_time_zone_new_local ();
+  dt = g_date_time_new (tz,
+                        date->year,
+                        date->month,
+                        date->day,
+                        date->hour,
+                        date->minute,
+                        date->second);
+
+  if (timezone)
+    *timezone = tz;
+  else
+    g_clear_pointer (&tz, g_time_zone_unref);
+
+  return dt;
+}
+
 icaltimetype*
 gcal_dup_icaltime (const icaltimetype *date)
 {
diff --git a/src/gcal-utils.h b/src/gcal-utils.h
index cbc8e7d..f856b76 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -52,6 +52,9 @@ const gchar*  (*GcalTranslateFunc)                              (GtkWidget
 
 GType           icaltime_get_type                               (void)            G_GNUC_CONST;
 
+GDateTime*      icaltime_to_datetime                            (const icaltimetype    *date,
+                                                                 GTimeZone            **timezone);
+
 icaltimetype*   gcal_dup_icaltime                               (const icaltimetype    *date);
 
 gchar*          gcal_get_weekday                                (gint                   i);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]