From 3a22d4c63d42009024830f494fe0ec3679382631 Mon Sep 17 00:00:00 2001 From: Wu Xiaotian Date: Thu, 24 Jan 2019 15:45:36 +0800 Subject: Support querying files by modification time and size Thanks for Leslie Zhai's patch. --- libcaja-private/caja-query.c | 37 +++++++++++++++++ libcaja-private/caja-query.h | 8 +++- libcaja-private/caja-search-engine-simple.c | 64 +++++++++++++++++++++-------- 3 files changed, 91 insertions(+), 18 deletions(-) (limited to 'libcaja-private') diff --git a/libcaja-private/caja-query.c b/libcaja-private/caja-query.c index a5e783e4..465e5a45 100644 --- a/libcaja-private/caja-query.c +++ b/libcaja-private/caja-query.c @@ -36,6 +36,8 @@ struct CajaQueryDetails char *location_uri; GList *mime_types; GList *tags; + gint64 duration; + gint64 size; }; static void caja_query_class_init (CajaQueryClass *class); @@ -75,6 +77,8 @@ static void caja_query_init (CajaQuery *query) { query->details = g_new0 (CajaQueryDetails, 1); + query->details->duration = 0; + query->details->size = 0; } CajaQuery * @@ -379,6 +383,8 @@ caja_query_to_xml (CajaQuery *query) char *mimetype; char *tag; GList *l; + gint64 duration; + gint64 size; xml = g_string_new (""); g_string_append (xml, @@ -420,6 +426,17 @@ caja_query_to_xml (CajaQuery *query) g_string_append (xml, " \n"); } + if (query->details->duration != 0) + { + g_string_append_printf(xml, " %ld", + query->details->duration); + } + + if (query->details->size != 0) + { + g_string_append_printf(xml, " %ld", query->details->size); + } + g_string_append (xml, "\n"); return g_string_free (xml, FALSE); @@ -445,3 +462,23 @@ caja_query_save (CajaQuery *query, char *file) } return res; } + +void caja_query_set_duration(CajaQuery *query, gint64 sec) +{ + query->details->duration = sec; +} + +gint64 caja_query_get_duration(CajaQuery *query) +{ + return query->details->duration; +} + +void caja_query_set_size(CajaQuery *query, gint64 size) +{ + query->details->size = size; +} + +gint64 caja_query_get_size(CajaQuery *query) +{ + return query->details->size; +} diff --git a/libcaja-private/caja-query.h b/libcaja-private/caja-query.h index 8e0f1047..b4c5c035 100644 --- a/libcaja-private/caja-query.h +++ b/libcaja-private/caja-query.h @@ -66,7 +66,13 @@ void caja_query_set_mime_types (CajaQuery *query, GList *mime_type void caja_query_add_mime_type (CajaQuery *query, const char *mime_type); char * caja_query_to_readable_string (CajaQuery *query); -CajaQuery *caja_query_load (char *file); +CajaQuery * caja_query_load (char *file); gboolean caja_query_save (CajaQuery *query, char *file); +gint64 caja_query_get_duration (CajaQuery *query); +void caja_query_set_duration (CajaQuery *query, gint64 sec); + +gint64 caja_query_get_size (CajaQuery *query); +void caja_query_set_size (CajaQuery *query, gint64 size); + #endif /* CAJA_QUERY_H */ diff --git a/libcaja-private/caja-search-engine-simple.c b/libcaja-private/caja-search-engine-simple.c index 41021ef9..09ac0a03 100644 --- a/libcaja-private/caja-search-engine-simple.c +++ b/libcaja-private/caja-search-engine-simple.c @@ -48,6 +48,8 @@ typedef struct gint n_processed_files; GList *uri_hits; + gint64 duration; + gint64 size; } SearchThreadData; @@ -124,6 +126,8 @@ search_thread_data_new (CajaSearchEngineSimple *engine, data->tags = caja_query_get_tags (query); data->mime_types = caja_query_get_mime_types (query); + data->duration = caja_query_get_duration (query); + data->size = caja_query_get_size (query); data->cancellable = g_cancellable_new (); @@ -350,34 +354,38 @@ visit_directory (GFile *dir, SearchThreadData *data) GList *l; const char *id; gboolean visited; + GTimeVal result; + time_t timestamp; + gchar *attributes; + GString *attr_string; - const char *attributes; + attr_string = g_string_new (STD_ATTRIBUTES); if (data->mime_types != NULL) { - if (data->tags != NULL) { - attributes = STD_ATTRIBUTES "," - G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," - G_FILE_ATTRIBUTE_XATTR_XDG_TAGS; - } else { - attributes = STD_ATTRIBUTES "," - G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE; - } - } else { - if (data->tags != NULL) { - attributes = STD_ATTRIBUTES "," - G_FILE_ATTRIBUTE_XATTR_XDG_TAGS; - } else { - attributes = STD_ATTRIBUTES; - } + g_string_append (attr_string, "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE); + } + if (data->tags != NULL) { + g_string_append (attr_string, "," G_FILE_ATTRIBUTE_XATTR_XDG_TAGS); + } + if (data->duration != 0) { + g_string_append (attr_string, "," G_FILE_ATTRIBUTE_TIME_MODIFIED "," + G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC); + } + if (data->size != 0) { + g_string_append (attr_string, "," G_FILE_ATTRIBUTE_STANDARD_SIZE); } - enumerator = g_file_enumerate_children (dir, attributes, 0, + attributes = g_string_free (attr_string, FALSE); + enumerator = g_file_enumerate_children (dir, (const char*)attributes, 0, data->cancellable, NULL); + g_free (attributes); if (enumerator == NULL) { return; } + timestamp = time(NULL); + while ((info = g_file_enumerator_next_file (enumerator, data->cancellable, NULL)) != NULL) { if (g_file_info_get_is_hidden (info)) @@ -426,6 +434,28 @@ visit_directory (GFile *dir, SearchThreadData *data) hit = file_has_all_tags (info, data->tags); } + if (hit && data->duration != 0) { + g_file_info_get_modification_time (info, &result); + if (data->duration > 0) { + if (timestamp - result.tv_sec < data->duration) + hit = FALSE; + } else { + if (timestamp - result.tv_sec > ABS(data->duration)) + hit = FALSE; + } + } + + if (hit && data->size != 0) { + gint64 file_size = g_file_info_get_size (info); + if (data->size > 0) { + if (file_size < data->size) + hit = FALSE; + } else { + if (ABS(data->size) < file_size) + hit = FALSE; + } + } + child = g_file_get_child (dir, g_file_info_get_name (info)); if (hit) -- cgit v1.2.1