Skip to content

Commit 3a22d4c

Browse files
yetistlukefromdc
authored andcommitted
Support querying files by modification time and size
Thanks for Leslie Zhai's patch.
1 parent e8f124d commit 3a22d4c

File tree

4 files changed

+344
-20
lines changed

4 files changed

+344
-20
lines changed

libcaja-private/caja-query.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ struct CajaQueryDetails
3636
char *location_uri;
3737
GList *mime_types;
3838
GList *tags;
39+
gint64 duration;
40+
gint64 size;
3941
};
4042

4143
static void caja_query_class_init (CajaQueryClass *class);
@@ -75,6 +77,8 @@ static void
7577
caja_query_init (CajaQuery *query)
7678
{
7779
query->details = g_new0 (CajaQueryDetails, 1);
80+
query->details->duration = 0;
81+
query->details->size = 0;
7882
}
7983

8084
CajaQuery *
@@ -379,6 +383,8 @@ caja_query_to_xml (CajaQuery *query)
379383
char *mimetype;
380384
char *tag;
381385
GList *l;
386+
gint64 duration;
387+
gint64 size;
382388

383389
xml = g_string_new ("");
384390
g_string_append (xml,
@@ -420,6 +426,17 @@ caja_query_to_xml (CajaQuery *query)
420426
g_string_append (xml, " </tags>\n");
421427
}
422428

429+
if (query->details->duration != 0)
430+
{
431+
g_string_append_printf(xml, " <duration>%ld</duration>",
432+
query->details->duration);
433+
}
434+
435+
if (query->details->size != 0)
436+
{
437+
g_string_append_printf(xml, " <size>%ld</size>", query->details->size);
438+
}
439+
423440
g_string_append (xml, "</query>\n");
424441

425442
return g_string_free (xml, FALSE);
@@ -445,3 +462,23 @@ caja_query_save (CajaQuery *query, char *file)
445462
}
446463
return res;
447464
}
465+
466+
void caja_query_set_duration(CajaQuery *query, gint64 sec)
467+
{
468+
query->details->duration = sec;
469+
}
470+
471+
gint64 caja_query_get_duration(CajaQuery *query)
472+
{
473+
return query->details->duration;
474+
}
475+
476+
void caja_query_set_size(CajaQuery *query, gint64 size)
477+
{
478+
query->details->size = size;
479+
}
480+
481+
gint64 caja_query_get_size(CajaQuery *query)
482+
{
483+
return query->details->size;
484+
}

libcaja-private/caja-query.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ void caja_query_set_mime_types (CajaQuery *query, GList *mime_type
6666
void caja_query_add_mime_type (CajaQuery *query, const char *mime_type);
6767

6868
char * caja_query_to_readable_string (CajaQuery *query);
69-
CajaQuery *caja_query_load (char *file);
69+
CajaQuery * caja_query_load (char *file);
7070
gboolean caja_query_save (CajaQuery *query, char *file);
7171

72+
gint64 caja_query_get_duration (CajaQuery *query);
73+
void caja_query_set_duration (CajaQuery *query, gint64 sec);
74+
75+
gint64 caja_query_get_size (CajaQuery *query);
76+
void caja_query_set_size (CajaQuery *query, gint64 size);
77+
7278
#endif /* CAJA_QUERY_H */

libcaja-private/caja-search-engine-simple.c

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ typedef struct
4848

4949
gint n_processed_files;
5050
GList *uri_hits;
51+
gint64 duration;
52+
gint64 size;
5153
} SearchThreadData;
5254

5355

@@ -124,6 +126,8 @@ search_thread_data_new (CajaSearchEngineSimple *engine,
124126

125127
data->tags = caja_query_get_tags (query);
126128
data->mime_types = caja_query_get_mime_types (query);
129+
data->duration = caja_query_get_duration (query);
130+
data->size = caja_query_get_size (query);
127131

128132
data->cancellable = g_cancellable_new ();
129133

@@ -350,34 +354,38 @@ visit_directory (GFile *dir, SearchThreadData *data)
350354
GList *l;
351355
const char *id;
352356
gboolean visited;
357+
GTimeVal result;
358+
time_t timestamp;
359+
gchar *attributes;
360+
GString *attr_string;
353361

354-
const char *attributes;
362+
attr_string = g_string_new (STD_ATTRIBUTES);
355363
if (data->mime_types != NULL) {
356-
if (data->tags != NULL) {
357-
attributes = STD_ATTRIBUTES ","
358-
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE ","
359-
G_FILE_ATTRIBUTE_XATTR_XDG_TAGS;
360-
} else {
361-
attributes = STD_ATTRIBUTES ","
362-
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE;
363-
}
364-
} else {
365-
if (data->tags != NULL) {
366-
attributes = STD_ATTRIBUTES ","
367-
G_FILE_ATTRIBUTE_XATTR_XDG_TAGS;
368-
} else {
369-
attributes = STD_ATTRIBUTES;
370-
}
364+
g_string_append (attr_string, "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
365+
}
366+
if (data->tags != NULL) {
367+
g_string_append (attr_string, "," G_FILE_ATTRIBUTE_XATTR_XDG_TAGS);
368+
}
369+
if (data->duration != 0) {
370+
g_string_append (attr_string, "," G_FILE_ATTRIBUTE_TIME_MODIFIED ","
371+
G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC);
372+
}
373+
if (data->size != 0) {
374+
g_string_append (attr_string, "," G_FILE_ATTRIBUTE_STANDARD_SIZE);
371375
}
372376

373-
enumerator = g_file_enumerate_children (dir, attributes, 0,
377+
attributes = g_string_free (attr_string, FALSE);
378+
enumerator = g_file_enumerate_children (dir, (const char*)attributes, 0,
374379
data->cancellable, NULL);
375380

381+
g_free (attributes);
376382
if (enumerator == NULL)
377383
{
378384
return;
379385
}
380386

387+
timestamp = time(NULL);
388+
381389
while ((info = g_file_enumerator_next_file (enumerator, data->cancellable, NULL)) != NULL)
382390
{
383391
if (g_file_info_get_is_hidden (info))
@@ -426,6 +434,28 @@ visit_directory (GFile *dir, SearchThreadData *data)
426434
hit = file_has_all_tags (info, data->tags);
427435
}
428436

437+
if (hit && data->duration != 0) {
438+
g_file_info_get_modification_time (info, &result);
439+
if (data->duration > 0) {
440+
if (timestamp - result.tv_sec < data->duration)
441+
hit = FALSE;
442+
} else {
443+
if (timestamp - result.tv_sec > ABS(data->duration))
444+
hit = FALSE;
445+
}
446+
}
447+
448+
if (hit && data->size != 0) {
449+
gint64 file_size = g_file_info_get_size (info);
450+
if (data->size > 0) {
451+
if (file_size < data->size)
452+
hit = FALSE;
453+
} else {
454+
if (ABS(data->size) < file_size)
455+
hit = FALSE;
456+
}
457+
}
458+
429459
child = g_file_get_child (dir, g_file_info_get_name (info));
430460

431461
if (hit)

0 commit comments

Comments
 (0)