[nautilus/wip/csoriano/search-popover: 37/43] search: use range of dates



commit 60bacb8edd472fa5c795601d4264694fad295698
Author: Carlos Soriano <csoriano gnome org>
Date:   Mon Feb 1 15:32:29 2016 +0100

    search: use range of dates
    
    This was not implemented. We want to differenciate between a range of
    days we want to search for and a specific date, as set on the mockups.
    
    The calendar selector will be specific dates, whether the days selector
    will be a "since".
    
    Also on the way fix various things and code preferences.

 libnautilus-private/nautilus-query.c               |   65 +++---
 libnautilus-private/nautilus-query.h               |    6 +-
 libnautilus-private/nautilus-search-engine-model.c |   29 ++--
 .../nautilus-search-engine-simple.c                |   23 ++-
 .../nautilus-search-engine-tracker.c               |   33 ++-
 libnautilus-private/nautilus-ui-utilities.c        |   26 +++
 libnautilus-private/nautilus-ui-utilities.h        |    4 +
 src/nautilus-query-editor.c                        |   23 ++-
 src/nautilus-search-popover.c                      |  221 +++++++++++---------
 9 files changed, 252 insertions(+), 178 deletions(-)
---
diff --git a/libnautilus-private/nautilus-query.c b/libnautilus-private/nautilus-query.c
index 402667a..83293e8 100644
--- a/libnautilus-private/nautilus-query.c
+++ b/libnautilus-private/nautilus-query.c
@@ -39,7 +39,7 @@ struct _NautilusQuery {
        GFile *location;
        GList *mime_types;
        gboolean show_hidden;
-        GDateTime *datetime;
+        GPtrArray *date_range;
         NautilusQuerySearchType search_type;
         NautilusQuerySearchContent search_content;
 
@@ -55,7 +55,7 @@ G_DEFINE_TYPE (NautilusQuery, nautilus_query, G_TYPE_OBJECT);
 
 enum {
         PROP_0,
-        PROP_DATE,
+        PROP_DATE_RANGE,
         PROP_LOCATION,
         PROP_MIMETYPES,
         PROP_RECURSIVE,
@@ -76,7 +76,7 @@ finalize (GObject *object)
         g_free (query->text);
         g_strfreev (query->prepared_words);
         g_clear_object (&query->location);
-        g_clear_pointer (&query->datetime, g_date_time_unref);
+        g_ptr_array_unref (query->date_range);
 
        G_OBJECT_CLASS (nautilus_query_parent_class)->finalize (object);
 }
@@ -90,8 +90,8 @@ nautilus_query_get_property (GObject    *object,
         NautilusQuery *self = NAUTILUS_QUERY (object);
 
         switch (prop_id) {
-        case PROP_DATE:
-                g_value_set_boxed (value, self->datetime);
+        case PROP_DATE_RANGE:
+                g_value_set_pointer (value, self->date_range);
                 break;
 
         case PROP_LOCATION:
@@ -102,9 +102,9 @@ nautilus_query_get_property (GObject    *object,
                 g_value_set_pointer (value, self->mime_types);
                 break;
 
-       case PROP_RECURSIVE:
-               g_value_set_boolean (value, self->recursive);
-               break;
+        case PROP_RECURSIVE:
+                g_value_set_boolean (value, self->recursive);
+                break;
 
         case PROP_SEARCH_TYPE:
                 g_value_set_enum (value, self->search_type);
@@ -136,8 +136,8 @@ nautilus_query_set_property (GObject      *object,
         NautilusQuery *self = NAUTILUS_QUERY (object);
 
         switch (prop_id) {
-        case PROP_DATE:
-                nautilus_query_set_date (self, g_value_get_boxed (value));
+        case PROP_DATE_RANGE:
+                nautilus_query_set_date_range (self, g_value_get_pointer (value));
                 break;
 
         case PROP_LOCATION:
@@ -148,9 +148,9 @@ nautilus_query_set_property (GObject      *object,
                 nautilus_query_set_mime_types (self, g_value_get_pointer (value));
                 break;
 
-       case PROP_RECURSIVE:
-               nautilus_query_set_recursive (self, g_value_get_boolean (value));
-               break;
+        case PROP_RECURSIVE:
+                nautilus_query_set_recursive (self, g_value_get_boolean (value));
+                break;
 
         case PROP_SEARCH_TYPE:
                 nautilus_query_set_search_type (self, g_value_get_enum (value));
@@ -183,18 +183,17 @@ nautilus_query_class_init (NautilusQueryClass *class)
         gobject_class->set_property = nautilus_query_set_property;
 
         /**
-         * NautilusQuery::date:
+         * NautilusQuery::date-range:
          *
-         * The initial date of the query.
+         * The date range of the query.
          *
          */
         g_object_class_install_property (gobject_class,
-                                         PROP_DATE,
-                                         g_param_spec_boxed ("date",
-                                                             "Date of the query",
-                                                             "The initial date of the query",
-                                                             G_TYPE_DATE_TIME,
-                                                             G_PARAM_READWRITE));
+                                         PROP_DATE_RANGE,
+                                         g_param_spec_pointer ("date-range",
+                                                               "Date range of the query",
+                                                               "The range date of the query",
+                                                               G_PARAM_READWRITE));
 
         /**
          * NautilusQuery::location:
@@ -507,28 +506,26 @@ nautilus_query_set_search_type (NautilusQuery           *query,
         }
 }
 
-GDateTime*
-nautilus_query_get_date (NautilusQuery *query)
+GPtrArray*
+nautilus_query_get_date_range (NautilusQuery *query)
 {
         g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);
 
-        return query->datetime;
+        return query->date_range;
 }
 
 void
-nautilus_query_set_date (NautilusQuery *query,
-                         GDateTime     *date)
+nautilus_query_set_date_range (NautilusQuery *query,
+                               GPtrArray     *date_range)
 {
         g_return_if_fail (NAUTILUS_IS_QUERY (query));
 
-        if (query->datetime != date) {
-                g_clear_pointer (&query->datetime, g_date_time_unref);
-                if (date) {
-                        query->datetime = g_date_time_ref (date);
-                }
-
-                g_object_notify (G_OBJECT (query), "date");
+        g_clear_pointer (&query->date_range, g_ptr_array_unref);
+        if (date_range) {
+                query->date_range = g_ptr_array_ref (date_range);
         }
+
+        g_object_notify (G_OBJECT (query), "date-range");
 }
 
 gboolean
@@ -585,7 +582,7 @@ nautilus_query_is_empty (NautilusQuery *query)
 
         }
 
-        if (!query->datetime &&
+        if (!query->date_range &&
             (!query->text || (query->text && query->text[0] == '\0')) &&
             !query->mime_types) {
                 return TRUE;
diff --git a/libnautilus-private/nautilus-query.h b/libnautilus-private/nautilus-query.h
index b21d492..c872ea9 100644
--- a/libnautilus-private/nautilus-query.h
+++ b/libnautilus-private/nautilus-query.h
@@ -64,9 +64,9 @@ NautilusQuerySearchType nautilus_query_get_search_type (NautilusQuery *query);
 void                    nautilus_query_set_search_type (NautilusQuery           *query,
                                                         NautilusQuerySearchType  type);
 
-GDateTime*     nautilus_query_get_date           (NautilusQuery *query);
-void           nautilus_query_set_date           (NautilusQuery *query,
-                                                  GDateTime     *date);
+GPtrArray*     nautilus_query_get_date_range     (NautilusQuery *query);
+void           nautilus_query_set_date_range     (NautilusQuery *query,
+                                                  GPtrArray     *date_range);
 
 gboolean       nautilus_query_get_recursive      (NautilusQuery *query);
 
diff --git a/libnautilus-private/nautilus-search-engine-model.c 
b/libnautilus-private/nautilus-search-engine-model.c
index 7f6d42e..8a6b7a2 100644
--- a/libnautilus-private/nautilus-search-engine-model.c
+++ b/libnautilus-private/nautilus-search-engine-model.c
@@ -129,11 +129,12 @@ model_directory_ready_cb (NautilusDirectory       *directory,
        gdouble match;
        gboolean found;
        NautilusSearchHit *hit;
-        GDateTime *date;
+        GDateTime *initial_date;
+        GDateTime *end_date;
+        GPtrArray *date_range;
 
        files = nautilus_directory_get_file_list (directory);
        mime_types = nautilus_query_get_mime_types (model->details->query);
-        date = nautilus_query_get_date (model->details->query);
        hits = NULL;
 
        for (l = files; l != NULL; l = l->next) {
@@ -155,28 +156,24 @@ model_directory_ready_cb (NautilusDirectory       *directory,
                        }
                }
 
-                if (found && date != NULL) {
+                date_range = nautilus_query_get_date_range (model->details->query);
+                if (found && date_range != NULL) {
                         NautilusQuerySearchType type;
-                        guint64 query_time, current_file_time;
-
+                        guint64 current_file_unix_time;
 
                         type = nautilus_query_get_search_type (model->details->query);
+                        initial_date = g_ptr_array_index (date_range, 0);
+                        end_date = g_ptr_array_index (date_range, 1);
 
                         if (type == NAUTILUS_QUERY_SEARCH_TYPE_LAST_ACCESS) {
-                                current_file_time = nautilus_file_get_atime (file);
+                                current_file_unix_time = nautilus_file_get_atime (file);
                         } else {
-                                current_file_time = nautilus_file_get_mtime (file);
+                                current_file_unix_time = nautilus_file_get_mtime (file);
                         }
 
-
-                        query_time = g_date_time_to_unix (date);
-
-                        if (current_file_time == 0) {
-                                /* Silently ignore errors */
-                                found = FALSE;
-                        } else {
-                                found = (query_time <= current_file_time);
-                        }
+                        found = nautilus_file_date_in_between (current_file_unix_time,
+                                                               initial_date,
+                                                               end_date);
                 }
 
                if (found) {
diff --git a/libnautilus-private/nautilus-search-engine-simple.c 
b/libnautilus-private/nautilus-search-engine-simple.c
index 317b8f1..e6deed9 100644
--- a/libnautilus-private/nautilus-search-engine-simple.c
+++ b/libnautilus-private/nautilus-search-engine-simple.c
@@ -213,6 +213,10 @@ visit_directory (GFile *dir, SearchThreadData *data)
        gboolean visited;
        guint64 atime;
        guint64 mtime;
+        GPtrArray *date_range;
+        GDateTime *initial_date;
+        GDateTime *end_date;
+
 
        enumerator = g_file_enumerate_children (dir,
                                                data->mime_types != NULL ?
@@ -257,11 +261,14 @@ visit_directory (GFile *dir, SearchThreadData *data)
 
                mtime = g_file_info_get_attribute_uint64 (info, "time::modified");
                atime = g_file_info_get_attribute_uint64 (info, "time::access");
-                if (found && nautilus_query_get_date (data->query) != NULL) {
+
+                date_range = nautilus_query_get_date_range (data->query);
+                if (found && date_range != NULL) {
                         NautilusQuerySearchType type;
-                        guint64 current_file_time, query_time;
-                       GDateTime *date;
+                        guint64 current_file_time;
 
+                        initial_date = g_ptr_array_index (date_range, 0);
+                        end_date = g_ptr_array_index (date_range, 1);
                         type = nautilus_query_get_search_type (data->query);
 
                         if (type == NAUTILUS_QUERY_SEARCH_TYPE_LAST_ACCESS) {
@@ -269,13 +276,11 @@ visit_directory (GFile *dir, SearchThreadData *data)
                         } else {
                                 current_file_time = mtime;
                         }
-
-                        date = nautilus_query_get_date (data->query);
-                        query_time = g_date_time_to_unix (date);
-
-                        found = (query_time <= current_file_time);
+                        found = nautilus_file_date_in_between (current_file_time,
+                                                               initial_date,
+                                                               end_date);
                 }
-               
+
                if (found) {
                        NautilusSearchHit *hit;
                        GDateTime *date;
diff --git a/libnautilus-private/nautilus-search-engine-tracker.c 
b/libnautilus-private/nautilus-search-engine-tracker.c
index 934805f..c086ccc 100644
--- a/libnautilus-private/nautilus-search-engine-tracker.c
+++ b/libnautilus-private/nautilus-search-engine-tracker.c
@@ -263,7 +263,7 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
        GList *mimetypes, *l;
        gint mime_count;
        gboolean recursive;
-        GDateTime *date;
+        GPtrArray *date_range;
 
        tracker = NAUTILUS_SEARCH_ENGINE_TRACKER (provider);
 
@@ -294,8 +294,6 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
         location = nautilus_query_get_location (tracker->details->query);
        location_uri = location ? g_file_get_uri (location) : NULL;
        mimetypes = nautilus_query_get_mime_types (tracker->details->query);
-        date = nautilus_query_get_date (tracker->details->query);
-
        mime_count = g_list_length (mimetypes);
 
        sparql = g_string_new ("SELECT DISTINCT nie:url(?urn) fts:rank(?urn) nfo:fileLastModified(?urn) 
nfo:fileLastAccessed(?urn)\n"
@@ -321,22 +319,38 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
 
        g_string_append_printf (sparql, "fn:contains(fn:lower-case(nfo:fileName(?urn)), '%s')", search_text);
 
-        if (date != NULL) {
+        date_range = nautilus_query_get_date_range (tracker->details->query);
+        if (date_range) {
                 NautilusQuerySearchType type;
-                gchar *date_format;
+                gchar *initial_date_format;
+                gchar *end_date_format;
+                GDateTime *initial_date;
+                GDateTime *end_date;
+                GDateTime *shifted_end_date;
+
+                initial_date = g_ptr_array_index (date_range, 0);
+                end_date = g_ptr_array_index (date_range, 1);
+                /* As we do for other searches, we want to make the end date inclusive.
+                 * For that, add a day to it */
+                shifted_end_date = g_date_time_add_days (end_date, 1);
 
                 type = nautilus_query_get_search_type (tracker->details->query);
-                date_format = g_date_time_format (date, "%Y-%m-%dT%H:%M:%S");
+                initial_date_format = g_date_time_format (initial_date, "%Y-%m-%dT%H:%M:%S");
+                end_date_format = g_date_time_format (shifted_end_date, "%Y-%m-%dT%H:%M:%S");
 
                 g_string_append (sparql, " && ");
 
                 if (type == NAUTILUS_QUERY_SEARCH_TYPE_LAST_ACCESS) {
-                        g_string_append_printf (sparql, "?atime >= \"%s\"^^xsd:dateTime", date_format);
+                        g_string_append_printf (sparql, "?atime >= \"%s\"^^xsd:dateTime", 
initial_date_format);
+                        g_string_append_printf (sparql, " && ?atime <= \"%s\"^^xsd:dateTime", 
end_date_format);
                 } else {
-                        g_string_append_printf (sparql, "?mtime >= \"%s\"^^xsd:dateTime", date_format);
+                        g_string_append_printf (sparql, "?mtime >= \"%s\"^^xsd:dateTime", 
initial_date_format);
+                        g_string_append_printf (sparql, " && ?mtime <= \"%s\"^^xsd:dateTime", 
end_date_format);
                 }
 
-                g_free (date_format);
+
+                g_free (initial_date_format);
+                g_free (end_date_format);
         }
 
        if (mime_count > 0) {
@@ -354,6 +368,7 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
        }
 
        g_string_append (sparql, ")} ORDER BY DESC (fts:rank(?urn))");
+                          g_print ("%s", sparql->str);
 
        tracker->details->cancellable = g_cancellable_new ();
        tracker_sparql_connection_query_async (tracker->details->connection,
diff --git a/libnautilus-private/nautilus-ui-utilities.c b/libnautilus-private/nautilus-ui-utilities.c
index e57b157..98e07ac 100644
--- a/libnautilus-private/nautilus-ui-utilities.c
+++ b/libnautilus-private/nautilus-ui-utilities.c
@@ -326,3 +326,29 @@ nautilus_ui_frame_video (GdkPixbuf **pixbuf)
                                      1, 1, GDK_INTERP_NEAREST, 255);
        }
 }
+
+gboolean
+nautilus_file_date_in_between (guint64    unix_file_time,
+                               GDateTime *initial_date,
+                               GDateTime *end_date)
+{
+        GDateTime *date;
+        gboolean in_between;
+
+        /* Silently ignore errors */
+        if (unix_file_time == 0)
+                return FALSE;
+
+        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;
+
+        g_date_time_unref (date);
+
+        return in_between;
+}
diff --git a/libnautilus-private/nautilus-ui-utilities.h b/libnautilus-private/nautilus-ui-utilities.h
index 283293c..b7943ed 100644
--- a/libnautilus-private/nautilus-ui-utilities.h
+++ b/libnautilus-private/nautilus-ui-utilities.h
@@ -47,4 +47,8 @@ char * nautilus_escape_action_name                 (const char        *action_na
 void   nautilus_ui_frame_image                     (GdkPixbuf        **pixbuf);
 void   nautilus_ui_frame_video                     (GdkPixbuf        **pixbuf);
 
+gboolean   nautilus_file_date_in_between           (guint64           file_unix_time,
+                                                    GDateTime         *initial_date,
+                                                    GDateTime         *end_date);
+
 #endif /* NAUTILUS_UI_UTILITIES_H */
diff --git a/src/nautilus-query-editor.c b/src/nautilus-query-editor.c
index 16dc119..fc1bda8 100644
--- a/src/nautilus-query-editor.c
+++ b/src/nautilus-query-editor.c
@@ -368,6 +368,23 @@ search_mode_changed_cb (GObject    *editor,
 }
 
 static void
+search_popover_date_range_changed_cb (NautilusSearchPopover *popover,
+                                      GPtrArray             *data,
+                                      NautilusQueryEditor   *editor)
+{
+        NautilusQueryEditorPrivate *priv;
+
+        priv = nautilus_query_editor_get_instance_private (NAUTILUS_QUERY_EDITOR (editor));
+        if (!priv->query)
+                create_query (editor);
+
+        g_print ("query editor range changed\n");
+        nautilus_query_set_date_range (priv->query, data);
+
+        nautilus_query_editor_changed (editor);
+}
+
+static void
 search_popover_changed_cb (NautilusSearchPopover *popover,
                            NautilusSearchFilter   filter,
                            gpointer               data,
@@ -380,10 +397,6 @@ search_popover_changed_cb (NautilusSearchPopover *popover,
                 create_query (editor);
 
         switch (filter) {
-        case NAUTILUS_SEARCH_FILTER_DATE:
-                nautilus_query_set_date (priv->query, data);
-                break;
-
         case NAUTILUS_SEARCH_FILTER_TYPE:
                 nautilus_query_set_mime_types (priv->query, data);
                 break;
@@ -455,6 +468,8 @@ setup_widgets (NautilusQueryEditor *editor)
                           G_CALLBACK (nautilus_query_editor_on_stop_search), editor);
         g_signal_connect (priv->popover, "changed",
                           G_CALLBACK (search_popover_changed_cb), editor);
+        g_signal_connect (priv->popover, "date-range",
+                          G_CALLBACK (search_popover_date_range_changed_cb), editor);
 
        /* show everything */
        gtk_widget_show_all (vbox);
diff --git a/src/nautilus-search-popover.c b/src/nautilus-search-popover.c
index 8a54eda..ea580b6 100644
--- a/src/nautilus-search-popover.c
+++ b/src/nautilus-search-popover.c
@@ -50,7 +50,7 @@ struct _NautilusSearchPopover
 const gchar*         get_text_for_day                            (gint                   days);
 
 static void          emit_date_changes_for_day                   (NautilusSearchPopover *popover,
-                                                                  gint                   days);
+                                                                  GPtrArray             *date_range);
 
 static void          show_date_selection_widgets                 (NautilusSearchPopover *popover,
                                                                   gboolean               visible);
@@ -58,7 +58,7 @@ static void          show_date_selection_widgets                 (NautilusSearch
 static void          show_other_types_dialog                     (NautilusSearchPopover *popover);
 
 static void          update_date_label                           (NautilusSearchPopover *popover,
-                                                                  guint                  days);
+                                                                  GPtrArray             *date_range);
 
 G_DEFINE_TYPE (NautilusSearchPopover, nautilus_search_popover, GTK_TYPE_POPOVER)
 
@@ -70,6 +70,7 @@ enum {
 
 enum {
   CHANGED,
+  DATE_RANGE,
   LAST_SIGNAL
 };
 
@@ -226,68 +227,52 @@ static void
 calendar_day_selected (GtkCalendar           *calendar,
                        NautilusSearchPopover *popover)
 {
-  GDateTime *now;
-  GDateTime *dt;
+  GDateTime *date;
   guint year, month, day;
-
-  now = g_date_time_new_now_local ();
+  GPtrArray *date_range;
 
   gtk_calendar_get_date (calendar, &year, &month, &day);
 
-  dt = g_date_time_new_local (year, month + 1, day, 0, 0, 0);
-
-  if (g_date_time_compare (dt, now) < 1)
-    {
-      guint days;
+  date = g_date_time_new_local (year, month + 1, day, 0, 0, 0);
 
-      days = g_date_time_difference (now, dt) / G_TIME_SPAN_DAY;
-
-      if (days > 0)
-        {
-          update_date_label (popover, days);
-          emit_date_changes_for_day (popover, days);
-        }
-  }
+  date_range = g_ptr_array_new_full (2, (GDestroyNotify) g_date_time_unref);
+  g_ptr_array_add (date_range, g_date_time_ref (date));
+  g_ptr_array_add (date_range, g_date_time_ref (date));
+  update_date_label (popover, date_range);
+  emit_date_changes_for_day (popover, date_range);
 
-  g_date_time_unref (now);
-  g_date_time_unref (dt);
+  g_ptr_array_unref (date_range);
+  g_date_time_unref (date);
 }
 
+/* Range on dates are partially implemented. For now just use it for differentation
+ * between a exact day or a range of a first day until now.
+ */
 static void
 setup_date (NautilusSearchPopover *popover,
             NautilusQuery         *query)
 {
-  GDateTime *dt;
-  GDateTime *now;
-
-  now = g_date_time_new_now_local ();
-  dt = nautilus_query_get_date (query);
-
-  /* Update date */
-  if (dt && g_date_time_compare (dt, now) < 1)
-    {
-      guint days;
+  GPtrArray *date_range;
+  GDateTime *date_initial;
 
-      days = g_date_time_difference (now, dt) / G_TIME_SPAN_DAY;
+  date_range = nautilus_query_get_date_range (query);
 
-      if (days > 0)
-        {
-          g_signal_handlers_block_by_func (popover->calendar, calendar_day_selected, popover);
+  if (date_range) {
+      date_initial = g_ptr_array_index (date_range, 0);
 
-          gtk_calendar_select_month (GTK_CALENDAR (popover->calendar),
-                                     g_date_time_get_month (dt) - 1,
-                                     g_date_time_get_year (dt));
+      g_signal_handlers_block_by_func (popover->calendar, calendar_day_selected, popover);
 
-          gtk_calendar_select_day (GTK_CALENDAR (popover->calendar),
-                                   g_date_time_get_day_of_month (dt));
+      gtk_calendar_select_month (GTK_CALENDAR (popover->calendar),
+                                 g_date_time_get_month (date_initial) - 1,
+                                 g_date_time_get_year (date_initial));
 
-          update_date_label (popover, days);
+      gtk_calendar_select_day (GTK_CALENDAR (popover->calendar),
+                               g_date_time_get_day_of_month (date_initial));
 
-          g_signal_handlers_unblock_by_func (popover->calendar, calendar_day_selected, popover);
-        }
-    }
+      update_date_label (popover, date_range);
 
-  g_clear_pointer (&now, g_date_time_unref);
+      g_signal_handlers_unblock_by_func (popover->calendar, calendar_day_selected, popover);
+  }
 }
 
 static void
@@ -352,8 +337,7 @@ date_entry_activate (GtkEntry              *entry,
   if (gtk_entry_get_text_length (entry) > 0)
     {
       GDateTime *now;
-      GDateTime *dt;
-      guint days;
+      GDateTime *date_time;
       GDate *date;
 
       date = g_date_new ();
@@ -367,7 +351,7 @@ date_entry_activate (GtkEntry              *entry,
         }
 
       now = g_date_time_new_now_local ();
-      dt = g_date_time_new_local (g_date_get_year (date),
+      date_time = g_date_time_new_local (g_date_get_year (date),
                                   g_date_get_month (date),
                                   g_date_get_day (date),
                                   0,
@@ -375,19 +359,22 @@ date_entry_activate (GtkEntry              *entry,
                                   0);
 
       /* Future dates also silently fails */
-      if (g_date_time_compare (dt, now) != 1)
+      if (g_date_time_compare (date_time, now) != 1)
         {
-          days = g_date_time_difference (now, dt) / G_TIME_SPAN_DAY;
-          if (days > 0)
-            {
-              update_date_label (popover, days);
-              show_date_selection_widgets (popover, FALSE);
-              emit_date_changes_for_day (popover, days);
-            }
+          GPtrArray *date_range;
+
+          date_range = g_ptr_array_new_full (2, (GDestroyNotify) g_date_time_unref);
+          g_ptr_array_add (date_range, g_date_time_ref (date_time));
+          g_ptr_array_add (date_range, g_date_time_ref (date_time));
+          update_date_label (popover, date_range);
+          show_date_selection_widgets (popover, FALSE);
+          emit_date_changes_for_day (popover, date_range);
+
+          g_ptr_array_unref (date_range);
         }
 
       g_date_time_unref (now);
-      g_date_time_unref (dt);
+      g_date_time_unref (date_time);
       g_date_free (date);
     }
 }
@@ -397,13 +384,25 @@ dates_listbox_row_activated (GtkListBox            *listbox,
                              GtkListBoxRow         *row,
                              NautilusSearchPopover *popover)
 {
-  gint days;
-
-  days = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (row), "days"));
+  GDateTime *date;
+  GDateTime *now;
+  GPtrArray *date_range = NULL;
 
-  update_date_label (popover, days);
+  now = g_date_time_new_now_local ();
+  date = g_object_get_data (G_OBJECT (row), "date");
+  if (date)
+    {
+      date_range = g_ptr_array_new_full (2, (GDestroyNotify) g_date_time_unref);
+      g_ptr_array_add (date_range, g_date_time_ref (date));
+      g_ptr_array_add (date_range, g_date_time_ref (now));
+    }
+  update_date_label (popover, date_range);
   show_date_selection_widgets (popover, FALSE);
-  emit_date_changes_for_day (popover, days);
+  emit_date_changes_for_day (popover, date_range);
+
+  if (date_range)
+    g_ptr_array_unref (date_range);
+  g_date_time_unref (now);
 }
 
 static void
@@ -569,25 +568,9 @@ create_row_for_label (const gchar *text,
 
 static void
 emit_date_changes_for_day (NautilusSearchPopover *popover,
-                           gint                   days)
+                           GPtrArray             *date_range)
 {
-  GDateTime *dt;
-
-  dt = NULL;
-
-  if (days > 0)
-    {
-      GDateTime *now;
-
-      now = g_date_time_new_now_local ();
-      dt = g_date_time_add_days (now, -days);
-
-      g_date_time_unref (now);
-    }
-
-  g_signal_emit (popover, signals[CHANGED], 0, NAUTILUS_SEARCH_FILTER_DATE, dt);
-
-  g_clear_pointer (&dt, g_date_time_unref);
+  g_signal_emit_by_name (popover, "date-range", date_range, NULL);
 }
 
 static void
@@ -595,6 +578,7 @@ fill_fuzzy_dates_listbox (NautilusSearchPopover *popover)
 {
   GDateTime *maximum_dt, *now;
   GtkWidget *row;
+  GDateTime *current_date;
   gint days, max_days;
 
   days = 0;
@@ -602,6 +586,7 @@ fill_fuzzy_dates_listbox (NautilusSearchPopover *popover)
   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 ();
 
   /* This is a tricky loop. The main intention here is that each
    * timeslice (day, week, month) have 2 or 3 entries. Years,
@@ -650,12 +635,21 @@ fill_fuzzy_dates_listbox (NautilusSearchPopover *popover)
 
       label = g_strdup_printf (get_text_for_day (days), normalized);
 
-      g_object_set_data (G_OBJECT (row), "days", GINT_TO_POINTER (days));
       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);
+        }
 
       gtk_container_add (GTK_CONTAINER (popover->dates_listbox), row);
 
       g_free (label);
+      g_date_time_unref (current_date);
 
       days += step;
     }
@@ -836,38 +830,49 @@ show_other_types_dialog (NautilusSearchPopover *popover)
 
 static void
 update_date_label (NautilusSearchPopover *popover,
-                   guint                  days)
+                   GPtrArray             *date_range)
 {
-  if (days > 0)
+  if (date_range)
     {
+      gint days;
+      GDateTime *initial_date;
+      GDateTime *end_date;
       GDateTime *now;
-      GDateTime *dt;
       gchar *formatted_date;
       gchar *label;
-      guint n;
+      guint normalized;
 
-      if (days < 7)
-        {
-          n = days;
-        }
-      else if (days < 30)
-        {
-          n = days / 7;
-        }
-      else if (days < 365)
+      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);
+      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)
         {
-          n = days / 30;
+          label = g_strdup (formatted_date);
         }
       else
         {
-          n = days / 365;
-        }
-
-      label = g_strdup_printf (get_text_for_day (days), n);
+          if (days < 7)
+            {
+             normalized = days;
+            }
+          else if (days < 30)
+            {
+              normalized = days / 7;
+            }
+          else if (days < 365)
+            {
+              normalized = days / 30;
+            }
+          else
+            {
+              normalized = days / 365;
+            }
 
-      now = g_date_time_new_now_local ();
-      dt = g_date_time_add_days (now, -days);
-      formatted_date = g_date_time_format (dt, "%x");
+          label = g_strdup_printf (get_text_for_day (days), normalized);
+        }
 
       gtk_entry_set_text (GTK_ENTRY (popover->date_entry), formatted_date);
 
@@ -875,7 +880,6 @@ update_date_label (NautilusSearchPopover *popover,
       gtk_label_set_label (GTK_LABEL (popover->select_date_button_label), label);
 
       g_date_time_unref (now);
-      g_date_time_unref (dt);
       g_free (formatted_date);
       g_free (label);
     }
@@ -985,6 +989,17 @@ nautilus_search_popover_class_init (NautilusSearchPopoverClass *klass)
                                    NAUTILUS_TYPE_SEARCH_FILTER,
                                    G_TYPE_POINTER);
 
+  signals[DATE_RANGE] = g_signal_new ("date-range",
+                                      NAUTILUS_TYPE_SEARCH_POPOVER,
+                                      G_SIGNAL_RUN_LAST,
+                                      0,
+                                      NULL,
+                                      NULL,
+                                      g_cclosure_marshal_generic,
+                                      G_TYPE_NONE,
+                                      1,
+                                      G_TYPE_POINTER);
+
   /**
    * NautilusSearchPopover::query:
    *


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