summaryrefslogtreecommitdiffstats
path: root/src/nautilus-query.c
blob: 9c43743fa7e97d2515d9ced7e6fb731c0a24538d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
 * Copyright (C) 2005 Novell, Inc.
 *
 * Nautilus is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * Nautilus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; see the file COPYING.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Author: Anders Carlsson <andersca@imendio.com>
 *
 */

#include "nautilus-query.h"

#include <eel/eel-glib-extensions.h>
#include <glib/gi18n.h>

#include "nautilus-enum-types.h"
#include "nautilus-file-utilities.h"
#include "nautilus-global-preferences.h"

#define RANK_SCALE_FACTOR 100
#define MIN_RANK 10.0
#define MAX_RANK 50.0

struct _NautilusQuery
{
    GObject parent;

    char *text;
    GFile *location;
    GPtrArray *mime_types;
    gboolean show_hidden;
    GPtrArray *date_range;
    NautilusQueryRecursive recursive;
    NautilusQuerySearchType search_type;
    NautilusQuerySearchContent search_content;

    gboolean searching;
    char **prepared_words;
    GMutex prepared_words_mutex;
};

static void  nautilus_query_class_init (NautilusQueryClass *class);
static void  nautilus_query_init (NautilusQuery *query);

G_DEFINE_TYPE (NautilusQuery, nautilus_query, G_TYPE_OBJECT);

enum
{
    PROP_0,
    PROP_DATE_RANGE,
    PROP_LOCATION,
    PROP_MIMETYPES,
    PROP_RECURSIVE,
    PROP_SEARCH_TYPE,
    PROP_SEARCHING,
    PROP_SHOW_HIDDEN,
    PROP_TEXT,
    LAST_PROP
};

static void
finalize (GObject *object)
{
    NautilusQuery *query;

    query = NAUTILUS_QUERY (object);

    g_free (query->text);
    g_strfreev (query->prepared_words);
    g_clear_object (&query->location);
    g_clear_pointer (&query->date_range, g_ptr_array_unref);
    g_mutex_clear (&query->prepared_words_mutex);

    G_OBJECT_CLASS (nautilus_query_parent_class)->finalize (object);
}

static void
nautilus_query_get_property (GObject    *object,
                             guint       prop_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
    NautilusQuery *self = NAUTILUS_QUERY (object);

    switch (prop_id)
    {
        case PROP_DATE_RANGE:
        {
            g_value_set_pointer (value, self->date_range);
        }
        break;

        case PROP_LOCATION:
        {
            g_value_set_object (value, self->location);
        }
        break;

        case PROP_MIMETYPES:
        {
            g_value_set_pointer (value, self->mime_types);
        }
        break;

        case PROP_RECURSIVE:
        {
            g_value_set_enum (value, self->recursive);
        }
        break;

        case PROP_SEARCH_TYPE:
        {
            g_value_set_enum (value, self->search_type);
        }
        break;

        case PROP_SEARCHING:
        {
            g_value_set_boolean (value, self->searching);
        }
        break;

        case PROP_SHOW_HIDDEN:
        {
            g_value_set_boolean (value, self->show_hidden);
        }
        break;

        case PROP_TEXT:
        {
            g_value_set_string (value, self->text);
        }
        break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
nautilus_query_set_property (GObject      *object,
                             guint         prop_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
    NautilusQuery *self = NAUTILUS_QUERY (object);

    switch (prop_id)
    {
        case PROP_DATE_RANGE:
        {
            nautilus_query_set_date_range (self, g_value_get_pointer (value));
        }
        break;

        case PROP_LOCATION:
        {
            nautilus_query_set_location (self, g_value_get_object (value));
        }
        break;

        case PROP_MIMETYPES:
        {
            nautilus_query_set_mime_types (self, g_value_get_pointer (value));
        }
        break;

        case PROP_RECURSIVE:
        {
            nautilus_query_set_recursive (self, g_value_get_enum (value));
        }
        break;

        case PROP_SEARCH_TYPE:
        {
            nautilus_query_set_search_type (self, g_value_get_enum (value));
        }
        break;

        case PROP_SEARCHING:
        {
            nautilus_query_set_searching (self, g_value_get_boolean (value));
        }
        break;

        case PROP_SHOW_HIDDEN:
        {
            nautilus_query_set_show_hidden_files (self, g_value_get_boolean (value));
        }
        break;

        case PROP_TEXT:
        {
            nautilus_query_set_text (self, g_value_get_string (value));
        }
        break;

        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
nautilus_query_class_init (NautilusQueryClass *class)
{
    GObjectClass *gobject_class;

    gobject_class = G_OBJECT_CLASS (class);
    gobject_class->finalize = finalize;
    gobject_class->get_property = nautilus_query_get_property;
    gobject_class->set_property = nautilus_query_set_property;

    /**
     * NautilusQuery::date-range:
     *
     * The date range of the query.
     *
     */
    g_object_class_install_property (gobject_class,
                                     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:
     *
     * The location of the query.
     *
     */
    g_object_class_install_property (gobject_class,
                                     PROP_LOCATION,
                                     g_param_spec_object ("location",
                                                          "Location of the query",
                                                          "The location of the query",
                                                          G_TYPE_FILE,
                                                          G_PARAM_READWRITE));

    /**
     * NautilusQuery::mimetypes: (type GPtrArray) (element-type gchar*)
     *
     * MIME types the query holds. An empty array means "Any type".
     *
     */
    g_object_class_install_property (gobject_class,
                                     PROP_MIMETYPES,
                                     g_param_spec_pointer ("mimetypes",
                                                           "MIME types of the query",
                                                           "The MIME types of the query",
                                                           G_PARAM_READWRITE));

    /**
     * NautilusQuery::recursive:
     *
     * Whether the query is being performed on subdirectories or not.
     *
     */
    g_object_class_install_property (gobject_class,
                                     PROP_RECURSIVE,
                                     g_param_spec_enum ("recursive",
                                                        "Whether the query is being performed on subdirectories",
                                                        "Whether the query is being performed on subdirectories or not",
                                                        NAUTILUS_TYPE_QUERY_RECURSIVE,
                                                        NAUTILUS_QUERY_RECURSIVE_ALWAYS,
                                                        G_PARAM_READWRITE));

    /**
     * NautilusQuery::search-type:
     *
     * The search type of the query.
     *
     */
    g_object_class_install_property (gobject_class,
                                     PROP_SEARCH_TYPE,
                                     g_param_spec_enum ("search-type",
                                                        "Type of the query",
                                                        "The type of the query",
                                                        NAUTILUS_TYPE_QUERY_SEARCH_TYPE,
                                                        NAUTILUS_QUERY_SEARCH_TYPE_LAST_MODIFIED,
                                                        G_PARAM_READWRITE));

    /**
     * NautilusQuery::searching:
     *
     * Whether the query is being performed or not.
     *
     */
    g_object_class_install_property (gobject_class,
                                     PROP_SEARCHING,
                                     g_param_spec_boolean ("searching",
                                                           "Whether the query is being performed",
                                                           "Whether the query is being performed or not",
                                                           FALSE,
                                                           G_PARAM_READWRITE));

    /**
     * NautilusQuery::show-hidden:
     *
     * Whether the search should include hidden files.
     *
     */
    g_object_class_install_property (gobject_class,
                                     PROP_SHOW_HIDDEN,
                                     g_param_spec_boolean ("show-hidden",
                                                           "Show hidden files",
                                                           "Whether the search should show hidden files",
                                                           FALSE,
                                                           G_PARAM_READWRITE));

    /**
     * NautilusQuery::text:
     *
     * The search string.
     *
     */
    g_object_class_install_property (gobject_class,
                                     PROP_TEXT,
                                     g_param_spec_string ("text",
                                                          "Text of the search",
                                                          "The text string of the search",
                                                          NULL,
                                                          G_PARAM_READWRITE));
}

static void
nautilus_query_init (NautilusQuery *query)
{
    query->location = g_file_new_for_path (g_get_home_dir ());
    query->mime_types = g_ptr_array_new ();
    query->show_hidden = TRUE;
    query->search_type = g_settings_get_enum (nautilus_preferences, "search-filter-time-type");
    query->search_content = NAUTILUS_QUERY_SEARCH_CONTENT_SIMPLE;
    g_mutex_init (&query->prepared_words_mutex);
}

static gchar *
prepare_string_for_compare (const gchar *string)
{
    gchar *normalized, *res;

    normalized = g_utf8_normalize (string, -1, G_NORMALIZE_NFD);
    res = g_utf8_strdown (normalized, -1);
    g_free (normalized);

    return res;
}

gdouble
nautilus_query_matches_string (NautilusQuery *query,
                               const gchar   *string)
{
    gchar *prepared_string, *ptr;
    gboolean found;
    gdouble retval;
    gint idx, nonexact_malus;

    if (!query->text)
    {
        return -1;
    }

    g_mutex_lock (&query->prepared_words_mutex);
    if (!query->prepared_words)
    {
        prepared_string = prepare_string_for_compare (query->text);
        query->prepared_words = g_strsplit (prepared_string, " ", -1);
        g_free (prepared_string);
    }

    prepared_string = prepare_string_for_compare (string);
    found = TRUE;
    ptr = NULL;
    nonexact_malus = 0;

    for (idx = 0; query->prepared_words[idx] != NULL; idx++)
    {
        if ((ptr = strstr (prepared_string, query->prepared_words[idx])) == NULL)
        {
            found = FALSE;
            break;
        }

        nonexact_malus += strlen (ptr) - strlen (query->prepared_words[idx]);
    }
    g_mutex_unlock (&query->prepared_words_mutex);

    if (!found)
    {
        g_free (prepared_string);
        return -1;
    }

    /* The rank value depends on the numbers of letters before and after the match.
     * To make the prefix matches prefered over sufix ones, the number of letters
     * after the match is divided by a factor, so that it decreases the rank by a
     * smaller amount.
     */
    retval = MAX (MIN_RANK, MAX_RANK - (gdouble) (ptr - prepared_string) - (gdouble) nonexact_malus / RANK_SCALE_FACTOR);
    g_free (prepared_string);

    return retval;
}

NautilusQuery *
nautilus_query_new (void)
{
    return g_object_new (NAUTILUS_TYPE_QUERY, NULL);
}


char *
nautilus_query_get_text (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);

    return g_strdup (query->text);
}

void
nautilus_query_set_text (NautilusQuery *query,
                         const char    *text)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    g_free (query->text);
    query->text = g_strstrip (g_strdup (text));

    g_mutex_lock (&query->prepared_words_mutex);
    g_strfreev (query->prepared_words);
    query->prepared_words = NULL;
    g_mutex_unlock (&query->prepared_words_mutex);

    g_object_notify (G_OBJECT (query), "text");
}

GFile *
nautilus_query_get_location (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);

    return g_object_ref (query->location);
}

void
nautilus_query_set_location (NautilusQuery *query,
                             GFile         *location)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    if (g_set_object (&query->location, location))
    {
        g_object_notify (G_OBJECT (query), "location");
    }
}

/**
 * nautilus_query_get_mime_type:
 * @query: A #NautilusQuery
 *
 * Retrieves the current MIME Types filter from @query. Its content must not be
 * modified. It can be read by multiple threads.
 *
 * Returns: (transfer container) A #GPtrArray reference with MIME type name strings.
 */
GPtrArray *
nautilus_query_get_mime_types (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);

    return g_ptr_array_ref (query->mime_types);
}

/**
 * nautilus_query_set_mime_types:
 * @query: A #NautilusQuery
 * @mime_types: (transfer none): A #GPtrArray of MIME type strings
 *
 * Set a new MIME types filter for @query. Once set, the filter must not be
 * modified, and it can only be replaced by setting another filter.
 *
 * Search engines that are already running for a previous filter will ignore the
 * new filter. So, the caller must ensure that the search will be reloaded
 * afterwards.
 */
void
nautilus_query_set_mime_types (NautilusQuery *query,
                               GPtrArray     *mime_types)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));
    g_return_if_fail (mime_types != NULL);

    g_clear_pointer (&query->mime_types, g_ptr_array_unref);
    query->mime_types = g_ptr_array_ref (mime_types);

    g_object_notify (G_OBJECT (query), "mimetypes");
}

gboolean
nautilus_query_get_show_hidden_files (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), FALSE);

    return query->show_hidden;
}

void
nautilus_query_set_show_hidden_files (NautilusQuery *query,
                                      gboolean       show_hidden)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    if (query->show_hidden != show_hidden)
    {
        query->show_hidden = show_hidden;
        g_object_notify (G_OBJECT (query), "show-hidden");
    }
}

char *
nautilus_query_to_readable_string (NautilusQuery *query)
{
    if (!query || !query->text || query->text[0] == '\0')
    {
        return g_strdup (_("Search"));
    }

    return g_strdup_printf (_("Search for “%s”"), query->text);
}

NautilusQuerySearchContent
nautilus_query_get_search_content (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), -1);

    return query->search_content;
}

void
nautilus_query_set_search_content (NautilusQuery              *query,
                                   NautilusQuerySearchContent  content)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    if (query->search_content != content)
    {
        query->search_content = content;
        g_object_notify (G_OBJECT (query), "search-type");
    }
}

NautilusQuerySearchType
nautilus_query_get_search_type (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), -1);

    return query->search_type;
}

void
nautilus_query_set_search_type (NautilusQuery           *query,
                                NautilusQuerySearchType  type)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    if (query->search_type != type)
    {
        query->search_type = type;
        g_object_notify (G_OBJECT (query), "search-type");
    }
}

/**
 * nautilus_query_get_date_range:
 * @query: a #NautilusQuery
 *
 * Retrieves the #GptrArray composed of #GDateTime representing the date range.
 * This function is thread safe.
 *
 * Returns: (transfer full): the #GptrArray composed of #GDateTime representing the date range.
 */
GPtrArray *
nautilus_query_get_date_range (NautilusQuery *query)
{
    static GMutex mutex;

    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), NULL);

    g_mutex_lock (&mutex);
    if (query->date_range)
    {
        g_ptr_array_ref (query->date_range);
    }
    g_mutex_unlock (&mutex);

    return query->date_range;
}

void
nautilus_query_set_date_range (NautilusQuery *query,
                               GPtrArray     *date_range)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    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
nautilus_query_get_searching (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query), FALSE);

    return query->searching;
}

void
nautilus_query_set_searching (NautilusQuery *query,
                              gboolean       searching)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    searching = !!searching;

    if (query->searching != searching)
    {
        query->searching = searching;

        g_object_notify (G_OBJECT (query), "searching");
    }
}

NautilusQueryRecursive
nautilus_query_get_recursive (NautilusQuery *query)
{
    g_return_val_if_fail (NAUTILUS_IS_QUERY (query),
                          NAUTILUS_QUERY_RECURSIVE_ALWAYS);

    return query->recursive;
}

void
nautilus_query_set_recursive (NautilusQuery          *query,
                              NautilusQueryRecursive  recursive)
{
    g_return_if_fail (NAUTILUS_IS_QUERY (query));

    if (query->recursive != recursive)
    {
        query->recursive = recursive;

        g_object_notify (G_OBJECT (query), "recursive");
    }
}

gboolean
nautilus_query_is_empty (NautilusQuery *query)
{
    if (!query)
    {
        return TRUE;
    }

    if (!query->date_range &&
        (!query->text || (query->text && query->text[0] == '\0')) &&
        query->mime_types->len == 0)
    {
        return TRUE;
    }

    return FALSE;
}