[nautilus/wip/csoriano/search-popover: 10/13] search-popover: merge and clean up date formatting



commit 1e447d8d7400cc459f914820dc77396dfee5c34c
Author: Carlos Soriano <csoriano gnome org>
Date:   Wed Feb 3 11:42:58 2016 +0100

    search-popover: merge and clean up date formatting
    
    So we use the same function all over.

 libnautilus-private/nautilus-ui-utilities.c |  117 +++++++++++++++++++++++----
 libnautilus-private/nautilus-ui-utilities.h |    1 +
 src/nautilus-search-popover.c               |  103 +++++-------------------
 3 files changed, 124 insertions(+), 97 deletions(-)
---
diff --git a/libnautilus-private/nautilus-ui-utilities.c b/libnautilus-private/nautilus-ui-utilities.c
index 98e07ac..786b55b 100644
--- a/libnautilus-private/nautilus-ui-utilities.c
+++ b/libnautilus-private/nautilus-ui-utilities.c
@@ -1,4 +1,4 @@
-/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8; tab-width: 8 -*- */
+/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
 
 /* nautilus-ui-utilities.c - helper functions for GtkUIManager stuff
 
@@ -31,6 +31,7 @@
 #include <gtk/gtk.h>
 #include <libgd/gd.h>
 #include <string.h>
+#include <glib/gi18n.h>
 
 static GMenuModel *
 find_gmenu_model (GMenuModel  *model,
@@ -332,23 +333,109 @@ nautilus_file_date_in_between (guint64    unix_file_time,
                                GDateTime *initial_date,
                                GDateTime *end_date)
 {
-        GDateTime *date;
-        gboolean in_between;
+  GDateTime *date;
+  gboolean in_between;
 
-        /* Silently ignore errors */
-        if (unix_file_time == 0)
-                return FALSE;
+  /* Silently ignore errors */
+  if (unix_file_time == 0)
+    {
+      return FALSE;
+    }
 
-        date = g_date_time_new_from_unix_local (unix_file_time);
+  date = g_date_time_new_from_unix_local (unix_file_time);
 
-        /* For the end date, we want to make end_date inclusive,
-         * for that the difference between the start of the day and the in_between
-         * has to be more than -1 day
-         */
-        in_between = g_date_time_difference (date, initial_date) > 0 &&
-                     g_date_time_difference (end_date, date) / G_TIME_SPAN_DAY > -1;
+  /* For the end date, we want to make end_date inclusive,
+   * for that the difference between the start of the day and the in_between
+   * has to be more than -1 day
+   */
+  in_between = g_date_time_difference (date, initial_date) > 0 &&
+               g_date_time_difference (end_date, date) / G_TIME_SPAN_DAY > -1;
 
-        g_date_time_unref (date);
+  g_date_time_unref (date);
 
-        return in_between;
+  return in_between;
 }
+
+static const gchar*
+get_text_for_days_ago (gint days)
+{
+  if (days < 7)
+    {
+      /* days */
+      return ngettext ("%d day ago", "%d days ago", days);
+    }
+   else if (days < 30)
+    {
+      /* weeks */
+      return ngettext ("Last week", "%d weeks ago", days / 7);
+    }
+  else if (days < 365)
+    {
+      /* months */
+      return ngettext ("Last month", "%d months ago", days / 30);
+    }
+  else
+    {
+      /* years */
+      return ngettext ("Last year", "%d years ago", days / 365);
+    }
+}
+
+gchar*
+get_text_for_date_range (GPtrArray *date_range)
+{
+  gint days;
+  gint normalized;
+  GDateTime *initial_date;
+  GDateTime *end_date;
+  gchar *formatted_date;
+  gchar *label;
+
+  if (!date_range)
+    return NULL;
+
+  initial_date = g_ptr_array_index (date_range, 0);
+  end_date = g_ptr_array_index (date_range, 1);
+  days = g_date_time_difference (end_date, initial_date) / G_TIME_SPAN_DAY;
+  formatted_date = g_date_time_format (initial_date, "%x");
+
+  if (days < 1)
+    {
+      label = g_strdup (formatted_date);
+    }
+  else
+    {
+      if (days < 7)
+        {
+          /* days */
+          normalized = days;
+        }
+      else if (days < 30)
+        {
+          /* weeks */
+          normalized = days / 7;
+        }
+      else if (days < 365)
+        {
+          /* months */
+          normalized = days / 30;
+        }
+      else if (days < 1825)
+        {
+          /* years */
+          normalized = days / 365;
+        }
+      else
+        {
+          /* after the first 5 years, jump at a 5-year pace */
+          normalized = days / 365;
+        }
+
+      label = g_strdup_printf (get_text_for_days_ago (days), normalized);
+    }
+
+    g_free (formatted_date);
+
+  return label;
+}
+
diff --git a/libnautilus-private/nautilus-ui-utilities.h b/libnautilus-private/nautilus-ui-utilities.h
index b7943ed..936898c 100644
--- a/libnautilus-private/nautilus-ui-utilities.h
+++ b/libnautilus-private/nautilus-ui-utilities.h
@@ -50,5 +50,6 @@ void   nautilus_ui_frame_video                     (GdkPixbuf        **pixbuf);
 gboolean   nautilus_file_date_in_between           (guint64           file_unix_time,
                                                     GDateTime         *initial_date,
                                                     GDateTime         *end_date);
+gchar*     get_text_for_date_range                 (GPtrArray         *date_range);
 
 #endif /* NAUTILUS_UI_UTILITIES_H */
diff --git a/src/nautilus-search-popover.c b/src/nautilus-search-popover.c
index 081dd07..17e825f 100644
--- a/src/nautilus-search-popover.c
+++ b/src/nautilus-search-popover.c
@@ -21,6 +21,7 @@
 
 #include <glib/gi18n.h>
 #include <libnautilus-private/nautilus-file.h>
+#include <libnautilus-private/nautilus-ui-utilities.h>
 #include <libnautilus-private/nautilus-global-preferences.h>
 
 struct _NautilusSearchPopover
@@ -45,8 +46,6 @@ struct _NautilusSearchPopover
   NautilusQuery      *query;
 };
 
-const gchar*         get_text_for_day                            (gint                   days);
-
 static void          emit_date_changes_for_day                   (NautilusSearchPopover *popover,
                                                                   GPtrArray             *date_range);
 
@@ -540,15 +539,19 @@ fill_fuzzy_dates_listbox (NautilusSearchPopover *popover)
   GDateTime *maximum_dt, *now;
   GtkWidget *row;
   GDateTime *current_date;
+  GPtrArray *date_range;
   gint days, max_days;
 
-  days = 0;
-
+  days = 1;
   maximum_dt = g_date_time_new_from_unix_local (0);
   now = g_date_time_new_now_local ();
   max_days = (g_date_time_get_year (now) - g_date_time_get_year (maximum_dt)) * 365;
   current_date = g_date_time_new_now_local ();
 
+  /* Add the no date filter element first */
+  row = create_row_for_label ("Any time", TRUE);
+  gtk_container_add (GTK_CONTAINER (popover->dates_listbox), row);
+
   /* This is a tricky loop. The main intention here is that each
    * timeslice (day, week, month) have 2 or 3 entries. Years,
    * however, are exceptions and should show many entries.
@@ -556,14 +559,10 @@ fill_fuzzy_dates_listbox (NautilusSearchPopover *popover)
   while (days < max_days)
     {
       gchar *label;
-      gint normalized, step;
+      gint normalized;
+      gint step;
 
-      if (days == 0)
-        {
-          normalized = 0;
-          step = 1;
-        }
-      else if (days < 7)
+      if (days < 7)
         {
           /* days */
           normalized = days;
@@ -594,23 +593,19 @@ fill_fuzzy_dates_listbox (NautilusSearchPopover *popover)
           step = 1825;
         }
 
-      label = g_strdup_printf (get_text_for_day (days), normalized);
-
+      current_date = g_date_time_add_days (now, -days);
+      date_range = g_ptr_array_new_full (2, (GDestroyNotify) g_date_time_unref);
+      g_ptr_array_add (date_range, g_date_time_ref (current_date));
+      g_ptr_array_add (date_range, g_date_time_ref (now));
+      label = get_text_for_date_range (date_range);
       row = create_row_for_label (label, normalized == 1);
-      if (days != 0)
-        {
-          current_date = g_date_time_add_days (now, -days);
-          g_object_set_data (G_OBJECT (row), "date", g_date_time_ref (current_date));
-        }
-      else
-        {
-          g_object_set_data (G_OBJECT (row), "date", NULL);
-        }
+      g_object_set_data (G_OBJECT (row), "date", g_date_time_ref (current_date));
 
       gtk_container_add (GTK_CONTAINER (popover->dates_listbox), row);
 
       g_free (label);
       g_date_time_unref (current_date);
+      g_ptr_array_unref (date_range);
 
       days += step;
     }
@@ -644,35 +639,6 @@ fill_types_listbox (NautilusSearchPopover *popover)
   gtk_container_add (GTK_CONTAINER (popover->type_listbox), row);
 }
 
-const gchar*
-get_text_for_day (gint days)
-{
-  if (days == 0)
-    {
-      return _("Any time");
-    }
-  else if (days < 7)
-    {
-      /* days */
-      return ngettext ("%d day ago", "%d days ago", days);
-    }
-   else if (days < 30)
-    {
-      /* weeks */
-      return ngettext ("Last week", "%d weeks ago", days / 7);
-    }
-  else if (days < 365)
-    {
-      /* months */
-      return ngettext ("Last month", "%d months ago", days / 30);
-    }
-  else
-    {
-      /* years */
-      return ngettext ("Last year", "%d years ago", days / 365);
-    }
-}
-
 static void
 show_date_selection_widgets (NautilusSearchPopover *popover,
                              gboolean               visible)
@@ -799,55 +765,28 @@ update_date_label (NautilusSearchPopover *popover,
       GDateTime *initial_date;
       GDateTime *end_date;
       GDateTime *now;
-      gchar *formatted_date;
       gchar *label;
-      guint normalized;
 
       now = g_date_time_new_now_local ();
       initial_date = g_ptr_array_index (date_range, 0);
-      end_date = g_ptr_array_index (date_range, 1);
+      end_date = g_ptr_array_index (date_range, 0);
       days = g_date_time_difference (end_date, initial_date) / G_TIME_SPAN_DAY;
-      formatted_date = g_date_time_format (initial_date, "%x");
 
-      if (days < 1)
-        {
-          label = g_strdup (formatted_date);
-        }
-      else
-        {
-          if (days < 7)
-            {
-             normalized = days;
-            }
-          else if (days < 30)
-            {
-              normalized = days / 7;
-            }
-          else if (days < 365)
-            {
-              normalized = days / 30;
-            }
-          else
-            {
-              normalized = days / 365;
-            }
-
-          label = g_strdup_printf (get_text_for_day (days), normalized);
-        }
+      label = get_text_for_date_range (date_range);
 
-      gtk_entry_set_text (GTK_ENTRY (popover->date_entry), formatted_date);
+      gtk_entry_set_text (GTK_ENTRY (popover->date_entry), days < 1 ? label : "");
 
       gtk_widget_show (popover->clear_date_button);
       gtk_label_set_label (GTK_LABEL (popover->select_date_button_label), label);
 
       g_date_time_unref (now);
-      g_free (formatted_date);
       g_free (label);
     }
   else
     {
       gtk_label_set_label (GTK_LABEL (popover->select_date_button_label),
                            _("Select Dates..."));
+      gtk_entry_set_text (GTK_ENTRY (popover->date_entry), "");
       gtk_widget_hide (popover->clear_date_button);
     }
 }


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