[gnome-calendar/wip/gbsneto/quick-add-popover: 1/3] utils: use GDateTime to build component



commit 3c0c3646f4932f0dfe5b47adf3627366d3570250
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Feb 16 11:54:35 2016 -0200

    utils: use GDateTime to build component
    
    As part of our epic journey to GDateTime, and to
    prepare ground for the future work, use GDateTime
    as arguments for building the ECalComponent.

 src/gcal-month-view.c |   41 ++++++++++++++++-----------------------
 src/gcal-utils.c      |   50 ++++++++++++++++++++++++++++++++----------------
 src/gcal-utils.h      |    4 +-
 src/gcal-window.c     |   14 ++++++------
 src/gcal-year-view.c  |   26 ++++++++++++++----------
 5 files changed, 74 insertions(+), 61 deletions(-)
---
diff --git a/src/gcal-month-view.c b/src/gcal-month-view.c
index 33667f0..f13b2e3 100644
--- a/src/gcal-month-view.c
+++ b/src/gcal-month-view.c
@@ -239,9 +239,6 @@ show_popover_for_position (GcalMonthView *view,
 
   gint start_day, end_day;
 
-  icaltimetype *start_date;
-  icaltimetype *end_date = NULL;
-
   widget = GTK_WIDGET (view);
   priv = gcal_month_view_get_instance_private (GCAL_MONTH_VIEW (widget));
   ppriv = GCAL_SUBSCRIBER_VIEW (widget)->priv;
@@ -268,26 +265,23 @@ show_popover_for_position (GcalMonthView *view,
     }
   else
     {
-      start_date = gcal_dup_icaltime (priv->date);
-      start_date->day = start_day;
-      start_date->is_date = 1;
-
-      end_date = gcal_dup_icaltime (priv->date);
-      end_date->day = end_day;
-      end_date->is_date = 1;
-      if (start_date->day > end_date->day)
+      GDateTime *start_dt, *end_dt;
+
+      start_dt = g_date_time_new_local (priv->date->year, priv->date->month, start_day, 0, 0, 0);
+      end_dt = g_date_time_new_local (priv->date->year, priv->date->month, end_day + 1, 0, 0, 0);
+
+      /* Swap dates if start > end */
+      if (start_day > end_day)
         {
-          gint day = start_date->day;
-          start_date->day = end_date->day;
-          end_date->day = day;
+          GDateTime *aux = start_dt;
+          start_dt = end_dt;
+          end_dt = aux;
         }
 
-      end_date->day += 1;
+      g_signal_emit_by_name (GCAL_VIEW (widget), "create-event", start_dt, end_dt, x, y);
 
-      g_signal_emit_by_name (GCAL_VIEW (widget), "create-event", start_date, end_date, x, y);
-
-      g_free (start_date);
-      g_free (end_date);
+      g_date_time_unref (start_dt);
+      g_date_time_unref (end_dt);
     }
 
   gtk_widget_queue_draw (widget);
@@ -747,19 +741,18 @@ add_new_event_button_cb (GtkWidget *button,
 {
   GcalMonthViewPrivate *priv;
   gint day;
-  icaltimetype *start_date;
+  GDateTime *start_date;
 
   priv = gcal_month_view_get_instance_private (GCAL_MONTH_VIEW (user_data));
 
   gtk_widget_hide (priv->overflow_popover);
 
   day = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (priv->overflow_popover), "selected-day"));
-  start_date = gcal_dup_icaltime (priv->date);
-  start_date->day = day;
-  start_date->is_date = 1;
+  start_date = g_date_time_new_local (priv->date->year, priv->date->month, day, 0, 0, 0);
 
   g_signal_emit_by_name (GCAL_VIEW (user_data), "create-event-detailed", start_date, NULL);
-  g_free (start_date);
+
+  g_date_time_unref (start_date);
 }
 
 static void
diff --git a/src/gcal-utils.c b/src/gcal-utils.c
index 9ac6a28..43e63ad 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -416,33 +416,49 @@ get_first_weekday (void)
  * Returns: (Transfer full): an { link ECalComponent} object
  **/
 ECalComponent*
-build_component_from_details (const gchar        *summary,
-                              const icaltimetype *initial_date,
-                              const icaltimetype *final_date)
+build_component_from_details (const gchar *summary,
+                              GDateTime   *initial_date,
+                              GDateTime   *final_date)
 {
   ECalComponent *event;
   ECalComponentDateTime dt;
   ECalComponentText summ;
+  GDateTime *utc_start, *utc_end;
+  gboolean all_day;
 
   event = e_cal_component_new ();
   e_cal_component_set_new_vtype (event, E_CAL_COMPONENT_EVENT);
 
-  dt.value = (icaltimetype*) initial_date;
+  /*
+   * Check if the event is all day. Notice that it can be all day even
+   * without the final date.
+   */
+  all_day = datetime_is_date (initial_date) && (final_date ? datetime_is_date (final_date) : TRUE);
+
+  /* Start date */
+  utc_start = g_date_time_to_utc (initial_date);
+
+  dt.value = datetime_to_icaltime (utc_start);
+  dt.value->is_date = all_day;
+  dt.tzid = format_utc_offset (g_date_time_get_utc_offset (initial_date));
   e_cal_component_set_dtstart (event, &dt);
 
-  if (final_date != NULL)
-    {
-      dt.value = (icaltimetype*) final_date;
-      e_cal_component_set_dtend (event, &dt);
-    }
-  else
-    {
-      icaltimetype *dt_end = gcal_dup_icaltime (initial_date);
-      icaltime_adjust (dt_end, 1, 0, 0, 0);
-      dt.value = dt_end;
-      e_cal_component_set_dtend (event, &dt);
-      g_free (dt_end);
-    }
+  e_cal_component_free_datetime (&dt);
+  g_date_time_unref (utc_start);
+
+  /* End date */
+  if (!final_date)
+    final_date = g_date_time_add_days (initial_date, 1);
+
+  utc_end = g_date_time_to_utc (final_date);
+
+  dt.value = datetime_to_icaltime (utc_end);
+  dt.value->is_date = all_day;
+  dt.tzid = format_utc_offset (g_date_time_get_utc_offset (final_date));
+  e_cal_component_set_dtend (event, &dt);
+
+  e_cal_component_free_datetime (&dt);
+  g_date_time_unref (utc_end);
 
   summ.altrep = NULL;
   summ.value = summary;
diff --git a/src/gcal-utils.h b/src/gcal-utils.h
index 0ac27fc..84cf654 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -88,8 +88,8 @@ gchar*          get_uuid_from_component                         (ESource
 gint            get_first_weekday                               (void);
 
 ECalComponent*  build_component_from_details                    (const gchar           *summary,
-                                                                 const icaltimetype    *initial_date,
-                                                                 const icaltimetype    *final_date);
+                                                                 GDateTime             *initial_date,
+                                                                 GDateTime             *final_date);
 
 gint            icaltime_compare_date                           (const icaltimetype    *date1,
                                                                  const icaltimetype    *date2);
diff --git a/src/gcal-window.c b/src/gcal-window.c
index 961cf33..82fddb2 100644
--- a/src/gcal-window.c
+++ b/src/gcal-window.c
@@ -40,8 +40,8 @@ typedef struct
 {
   gint               x;
   gint               y;
-  icaltimetype      *start_date;
-  icaltimetype      *end_date;
+  GDateTime         *start_date;
+  GDateTime         *end_date;
 } NewEventData;
 
 typedef struct
@@ -694,17 +694,17 @@ show_new_event_widget (GcalView *view,
 
   if (window->event_creation_data != NULL)
     {
-      g_free (window->event_creation_data->start_date);
-      g_free (window->event_creation_data->end_date);
+      g_date_time_unref (window->event_creation_data->start_date);
+      g_date_time_unref (window->event_creation_data->end_date);
       g_free (window->event_creation_data);
     }
 
   window->event_creation_data = g_new0 (NewEventData, 1);
   window->event_creation_data->x = x;
   window->event_creation_data->y = y;
-  window->event_creation_data->start_date = gcal_dup_icaltime (start_span);
+  window->event_creation_data->start_date = g_date_time_ref (start_span);
   if (end_span != NULL)
-    window->event_creation_data->end_date = gcal_dup_icaltime (end_span);
+    window->event_creation_data->end_date = g_date_time_ref (end_span);
   g_debug ("[show_new_event] position (%f, %f)", x, y);
 
   /* Setup new event widget data */
@@ -1111,7 +1111,7 @@ create_event_detailed_cb (GcalView *view,
   ECalComponent *comp;
   GcalEvent *event;
 
-  comp = build_component_from_details ("", (icaltimetype*) start_span, (icaltimetype*) end_span);
+  comp = build_component_from_details ("", start_span, end_span);
   event = gcal_event_new (gcal_manager_get_default_source (window->manager), comp);
 
   gcal_edit_dialog_set_event_is_new (GCAL_EDIT_DIALOG (window->edit_dialog), TRUE);
diff --git a/src/gcal-year-view.c b/src/gcal-year-view.c
index c2dfde7..d7d61a6 100644
--- a/src/gcal-year-view.c
+++ b/src/gcal-year-view.c
@@ -1031,29 +1031,33 @@ static void
 add_event_clicked_cb (GcalYearView *year_view,
                       GtkButton    *button)
 {
-  icaltimetype *start_date, *end_date = NULL;
+  GDateTime *start_date, *end_date = NULL;
 
   if (year_view->start_selected_date->day == 0)
     {
-      start_date = gcal_dup_icaltime (year_view->current_date);
+      start_date = g_date_time_new_local (year_view->current_date->year,
+                                          year_view->current_date->month,
+                                          year_view->current_date->day,
+                                          0, 0, 0);
     }
   else
     {
-      start_date = gcal_dup_icaltime (year_view->start_selected_date);
-      end_date = gcal_dup_icaltime (year_view->end_selected_date);
-      end_date->day += 1;
-      *end_date = icaltime_normalize (*end_date);
-      end_date->is_date = 1;
+      icaltimetype *dtstart, *dtend;
+
+      dtstart = year_view->start_selected_date;
+      dtend = year_view->end_selected_date;
+
+      start_date = g_date_time_new_local (dtstart->year, dtstart->month, dtstart->day, 0, 0, 0);
+      end_date = g_date_time_new_local (dtend->year, dtend->month, dtend->day + 1, 0, 0, 0);
     }
 
   if (year_view->popover_mode)
     gtk_widget_hide (year_view->popover);
 
-  start_date->is_date = 1;
   g_signal_emit_by_name (GCAL_VIEW (year_view), "create-event-detailed", start_date, end_date);
-  g_free (start_date);
-  if (end_date != NULL)
-    g_free (end_date);
+
+  g_clear_pointer (&start_date, g_date_time_unref);
+  g_clear_pointer (&end_date, g_date_time_unref);
 }
 
 static void


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