[gnome-calendar/wip/gbsneto/gcal-event: 13/13] year-view: move comparing function from gcal-event-widget.c



commit 8511285525fa2e287c006b918f1d63de2d34ba3d
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sun Feb 14 06:25:26 2016 -0200

    year-view: move comparing function from gcal-event-widget.c
    
    The compare_for_single_day() method is reproducing the
    same functionality from gcal_event_compare(), and the only
    consumer is the Year View. So, for now, simplify the
    comparing function and put it in year view file.
    
    Also, this commit vastly improves and documents the year
    view's algorithm to add events to the sidebar list.

 src/gcal-event-widget.c |   51 --------------------------------
 src/gcal-event-widget.h |    3 --
 src/gcal-year-view.c    |   73 ++++++++++++++++++++++++++++++++--------------
 3 files changed, 51 insertions(+), 76 deletions(-)
---
diff --git a/src/gcal-event-widget.c b/src/gcal-event-widget.c
index 251046f..d68d0ad 100644
--- a/src/gcal-event-widget.c
+++ b/src/gcal-event-widget.c
@@ -865,54 +865,3 @@ gcal_event_widget_compare_by_start_date (GcalEventWidget *widget1,
 {
   return g_date_time_compare (widget1->dt_start, widget2->dt_start);
 }
-
-/**
- * gcal_event_widget_compare_for_single_day:
- * @widget1:
- * @widget2:
- *
- * Compare widgets by putting those that span over a day before the rest, and between those
- * who last less than a day by its start time/date
- *
- * Returns:
- **/
-gint
-gcal_event_widget_compare_for_single_day (GcalEventWidget *widget1,
-                                          GcalEventWidget *widget2)
-{
-  if (gcal_event_is_multiday (widget1->event) && gcal_event_is_multiday (widget2->event))
-    {
-      time_t time_s1, time_s2;
-      time_t time_e1, time_e2;
-      time_t result;
-
-      time_s1 = g_date_time_to_unix (widget1->dt_start);
-      time_s2 = g_date_time_to_unix (widget2->dt_start);
-      time_e1 = g_date_time_to_unix (widget1->dt_end);
-      time_e2 = g_date_time_to_unix (widget2->dt_end);
-
-      result = (time_e2 - time_s2) - (time_e1 - time_s1);
-      if (result != 0)
-        return result;
-      else
-        return g_date_time_compare (widget1->dt_start, widget2->dt_start);
-    }
-  else
-    {
-      if (gcal_event_is_multiday (widget1->event))
-        return -1;
-      else if (gcal_event_is_multiday (widget2->event))
-        return 1;
-      else
-        {
-          if (gcal_event_get_all_day (widget1->event) && gcal_event_get_all_day (widget2->event))
-            return 0;
-          else if (gcal_event_get_all_day (widget1->event))
-            return -1;
-          else if (gcal_event_get_all_day (widget2->event))
-            return 1;
-          else
-            return g_date_time_compare (widget1->dt_start, widget2->dt_start);
-        }
-    }
-}
diff --git a/src/gcal-event-widget.h b/src/gcal-event-widget.h
index d07b79f..7d23e8e 100644
--- a/src/gcal-event-widget.h
+++ b/src/gcal-event-widget.h
@@ -70,9 +70,6 @@ gint         gcal_event_widget_compare_by_length           (GcalEventWidget    *
 gint         gcal_event_widget_compare_by_start_date       (GcalEventWidget    *widget1,
                                                             GcalEventWidget    *widget2);
 
-gint         gcal_event_widget_compare_for_single_day      (GcalEventWidget    *widget1,
-                                                            GcalEventWidget    *widget2);
-
 G_END_DECLS
 
 #endif /* __GCAL_EVENT_WIDGET_H__ */
diff --git a/src/gcal-year-view.c b/src/gcal-year-view.c
index 8488654..f202d9b 100644
--- a/src/gcal-year-view.c
+++ b/src/gcal-year-view.c
@@ -119,6 +119,13 @@ G_DEFINE_TYPE_WITH_CODE (GcalYearView, gcal_year_view, GTK_TYPE_BOX,
                          G_IMPLEMENT_INTERFACE (E_TYPE_CAL_DATA_MODEL_SUBSCRIBER,
                                                 gcal_data_model_subscriber_interface_init));
 
+static gint
+compare_events (GcalEventWidget *widget1,
+                GcalEventWidget *widget2)
+{
+  return gcal_event_compare (gcal_event_widget_get_event (widget1), gcal_event_widget_get_event (widget2));
+}
+
 static guint
 get_last_week_of_year_dmy (gint       first_weekday,
                            GDateDay   day,
@@ -268,7 +275,6 @@ add_event_to_day_array (GcalYearView  *year_view,
   GTimeZone *tz;
 
   gint i;
-  gboolean child_widget_used = FALSE;
 
   child_widget = gcal_event_widget_new (event);
   gcal_event_widget_set_read_only (GCAL_EVENT_WIDGET (child_widget),
@@ -285,46 +291,66 @@ add_event_to_day_array (GcalYearView  *year_view,
                                  g_date_time_get_day_of_month (date) + 1,
                                  0, 0, 0);
 
-  gcal_event_widget_set_date_start (GCAL_EVENT_WIDGET (child_widget), date);
-  gcal_event_widget_set_date_end (GCAL_EVENT_WIDGET (child_widget), second_date);
-
   /* marking and cloning */
   for (i = 0; i < days_span; i++)
     {
-      GDateTime *aux;
       GtkWidget *cloned_child;
-      gint start_comparison, end_comparison;
+      GDateTime *aux;
+      gint start_comparison;
 
       cloned_child = child_widget;
       start_comparison = datetime_compare_date (dt_start, date);
 
-      if (start_comparison <= 0)
+      if (start_comparison > 0)
         {
-          if (child_widget_used)
-            {
-              cloned_child = gcal_event_widget_clone (GCAL_EVENT_WIDGET (child_widget));
-              gcal_event_widget_set_date_start (GCAL_EVENT_WIDGET (cloned_child), date);
-              gcal_event_widget_set_date_end (GCAL_EVENT_WIDGET (cloned_child), second_date);
-            }
-          else
-            {
-              child_widget_used = TRUE;
-            }
+          /*
+           * The start date of the event is ahead of the current
+           * date being comparent, so we don't add it now.
+           */
+          cloned_child = NULL;
+        }
+      else if (start_comparison == 0)
+        {
+          /*
+           * This is the first day the event happens, so we just use
+           * the previously created event widget.
+           */
+          cloned_child = child_widget;
         }
       else
         {
-          cloned_child = NULL;
+          /*
+           * We're in the middle of the event and the dates are between
+           * the event start date > current date > event end date, so
+           * we keep cloning the event widget until we reach the end of
+           * the event.
+           */
+          cloned_child = gcal_event_widget_clone (GCAL_EVENT_WIDGET (child_widget));
         }
 
       if (cloned_child != NULL)
         {
+          gint end_comparison;
+
+          /*
+           * Setup the event widget's custom dates, so the slanted edges are
+           * applied properly
+           */
+          gcal_event_widget_set_date_start (GCAL_EVENT_WIDGET (cloned_child), date);
+          gcal_event_widget_set_date_end (GCAL_EVENT_WIDGET (cloned_child), second_date);
+
           days_widgets_array[i] = g_list_insert_sorted (days_widgets_array[i],
                                                         cloned_child,
-                                                        (GCompareFunc) 
gcal_event_widget_compare_for_single_day);
+                                                        (GCompareFunc) compare_events);
 
-          end_comparison = g_date_time_compare (second_date, dt_end);
+          end_comparison = datetime_compare_date (second_date, dt_end);
 
-          if (end_comparison == 0)
+          /*
+           * If the end date surpassed the event's end date, we reached the
+           * end of the event's time span and shall stop adding the event to
+           * the list.
+           */
+          if (end_comparison >= 0)
             break;
         }
 
@@ -337,6 +363,9 @@ add_event_to_day_array (GcalYearView  *year_view,
       second_date = g_date_time_add_days (second_date, 1);
       g_clear_pointer (&aux, g_date_time_unref);
     }
+
+  g_clear_pointer (&second_date, g_date_time_unref);
+  g_clear_pointer (&date, g_date_time_unref);
 }
 
 static void
@@ -497,7 +526,7 @@ sidebar_sort_func (GtkListBoxRow *row1,
 
   result = row1_shift - row2_shift;
   if (result == 0)
-    return gcal_event_widget_compare_for_single_day (GCAL_EVENT_WIDGET (row1_child), GCAL_EVENT_WIDGET 
(row2_child));
+    return compare_events (GCAL_EVENT_WIDGET (row1_child), GCAL_EVENT_WIDGET (row2_child));
 
   return result;
 }


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