[nautilus/wip/gbsneto/search-popover: 7/7] search-engines: implement search by access/modification time



commit 289e0d818a7681142dbc6c7a4baf660a47b93eae
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Tue Oct 6 07:09:11 2015 -0300

    search-engines: implement search by access/modification time
    
    The search engines currently aren't able to perform searches
    against modification or access times.
    
    This commit implements it for simple, model & tracker search
    engines.

 libnautilus-private/nautilus-search-engine-model.c |   40 ++++++++++++++++++++
 .../nautilus-search-engine-simple.c                |   22 +++++++++++
 .../nautilus-search-engine-tracker.c               |   24 +++++++++++-
 3 files changed, 84 insertions(+), 2 deletions(-)
---
diff --git a/libnautilus-private/nautilus-search-engine-model.c 
b/libnautilus-private/nautilus-search-engine-model.c
index 8648cf5..c122433 100644
--- a/libnautilus-private/nautilus-search-engine-model.c
+++ b/libnautilus-private/nautilus-search-engine-model.c
@@ -118,9 +118,11 @@ model_directory_ready_cb (NautilusDirectory        *directory,
        gdouble match;
        gboolean found;
        NautilusSearchHit *hit;
+        GDateTime *dt;
 
        files = nautilus_directory_get_file_list (directory);
        mime_types = nautilus_query_get_mime_types (model->details->query);
+        dt = nautilus_query_get_date (model->details->query);
        hits = NULL;
 
        for (l = files; l != NULL; l = l->next) {
@@ -141,6 +143,44 @@ model_directory_ready_cb (NautilusDirectory        *directory,
                        }
                }
 
+                if (found && dt != NULL) {
+                        NautilusQuerySearchType type;
+                        guint64 query_time, current_file_time;
+                        const gchar *attrib;
+                        GFileInfo *info;
+                        GError *error;
+                        GFile *location;
+
+                        type = nautilus_query_get_search_type (model->details->query);
+                        location = nautilus_file_get_location (file);
+
+                        if (type == NAUTILUS_QUERY_SEARCH_TYPE_LAST_ACCESS) {
+                                attrib = G_FILE_ATTRIBUTE_TIME_ACCESS;
+                        } else {
+                                attrib = G_FILE_ATTRIBUTE_TIME_MODIFIED;
+                        }
+
+                        query_time = g_date_time_to_unix (dt);
+
+                        /* Query current file's attribute */
+                        info = g_file_query_info (location,
+                                                  attrib,
+                                                  G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+                                                  NULL,
+                                                  &error);
+
+                        if (error) {
+                                /* Silently ignore errors */
+                                g_clear_error (&error);
+                                found = FALSE;
+                        } else {
+                                current_file_time = g_file_info_get_attribute_uint64 (info, attrib);
+                                found = (query_time <= current_file_time);
+                        }
+
+                        g_clear_object (&location);
+                }
+
                if (found) {
                        uri = nautilus_file_get_uri (file);
                        hit = nautilus_search_hit_new (uri);
diff --git a/libnautilus-private/nautilus-search-engine-simple.c 
b/libnautilus-private/nautilus-search-engine-simple.c
index f3557d6..c161b39 100644
--- a/libnautilus-private/nautilus-search-engine-simple.c
+++ b/libnautilus-private/nautilus-search-engine-simple.c
@@ -195,6 +195,7 @@ send_batch (SearchThreadData *thread_data)
        G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN "," \
        G_FILE_ATTRIBUTE_STANDARD_TYPE "," \
        G_FILE_ATTRIBUTE_TIME_MODIFIED "," \
+        G_FILE_ATTRIBUTE_TIME_ACCESS "," \
        G_FILE_ATTRIBUTE_ID_FILE
 
 static void
@@ -250,6 +251,27 @@ visit_directory (GFile *dir, SearchThreadData *data)
                                }
                        }
                }
+
+                if (found && nautilus_query_get_date (data->query) != NULL) {
+                        NautilusQuerySearchType type;
+                        GDateTime *date;
+                        guint64 current_file_time, query_time;
+                        const gchar *attrib;
+
+                        type = nautilus_query_get_search_type (data->query);
+
+                        if (type == NAUTILUS_QUERY_SEARCH_TYPE_LAST_ACCESS) {
+                                attrib = G_FILE_ATTRIBUTE_TIME_ACCESS;
+                        } else {
+                                attrib = G_FILE_ATTRIBUTE_TIME_MODIFIED;
+                        }
+
+                        date = nautilus_query_get_date (data->query);
+                        query_time = g_date_time_to_unix (date);
+                        current_file_time = g_file_info_get_attribute_uint64 (info, attrib);
+
+                        found = (query_time <= current_file_time);
+                }
                
                if (found) {
                        NautilusSearchHit *hit;
diff --git a/libnautilus-private/nautilus-search-engine-tracker.c 
b/libnautilus-private/nautilus-search-engine-tracker.c
index 7b0f801..a44ae90 100644
--- a/libnautilus-private/nautilus-search-engine-tracker.c
+++ b/libnautilus-private/nautilus-search-engine-tracker.c
@@ -255,6 +255,7 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
        GList *mimetypes, *l;
        gint mime_count;
        gboolean recursive;
+        GDateTime *dt;
 
        tracker = NAUTILUS_SEARCH_ENGINE_TRACKER (provider);
 
@@ -283,10 +284,11 @@ 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);
+        dt = 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) 
tracker:coalesce(nfo:fileLastModified(?urn), nie:contentLastModified(?urn)) 
tracker:coalesce(nfo:fileLastAccessed(?urn), nie:contentAccessed(?urn)) "
+       sparql = g_string_new ("SELECT DISTINCT nie:url(?urn) fts:rank(?urn) 
tracker:coalesce(nfo:fileLastModified(?urn), nie:contentLastModified(?urn)) AS ?mtime 
tracker:coalesce(nfo:fileLastAccessed(?urn), nie:contentAccessed(?urn)) AS ?atime \n"
                               "WHERE {"
                               "  ?urn a nfo:FileDataObject ;"
                               "  tracker:available true ; ");
@@ -305,6 +307,24 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
 
        g_string_append_printf (sparql, "fn:contains(fn:lower-case(nfo:fileName(?urn)), '%s')", search_text);
 
+        if (dt != NULL) {
+                NautilusQuerySearchType type;
+                gchar *dt_format;
+
+                type = nautilus_query_get_search_type (tracker->details->query);
+                dt_format = g_date_time_format (dt, "%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", dt_format);
+                } else {
+                        g_string_append_printf (sparql, "?mtime <= \"%s\"^^xsd:dateTime", dt_format);
+                }
+
+                g_free (dt_format);
+        }
+
        if (mime_count > 0) {
                g_string_append (sparql, " && (");
 
@@ -316,7 +336,7 @@ nautilus_search_engine_tracker_start (NautilusSearchProvider *provider)
                        g_string_append_printf (sparql, "fn:contains(?mime, '%s')",
                                                (gchar *) l->data);
                }
-               g_string_append (sparql, ")");
+               g_string_append (sparql, ")\n");
        }
 
        g_string_append (sparql, ")} ORDER BY DESC (fts:rank(?urn))");


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