nautilus-search-popover.c 33.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* nautilus-search-popover.c
 *
 * Copyright (C) 2015 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
 *
 * This program 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 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "nautilus-enum-types.h"
#include "nautilus-search-popover.h"
21
#include "nautilus-mime-actions.h"
22 23

#include <glib/gi18n.h>
24 25 26
#include "nautilus-file.h"
#include "nautilus-ui-utilities.h"
#include "nautilus-global-preferences.h"
27

28 29
 #define SEARCH_FILTER_MAX_YEARS 5

30 31
struct _NautilusSearchPopover
{
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    GtkPopover parent;

    GtkWidget *around_revealer;
    GtkWidget *around_stack;
    GtkWidget *calendar;
    GtkWidget *clear_date_button;
    GtkWidget *dates_listbox;
    GtkWidget *date_entry;
    GtkWidget *date_stack;
    GtkWidget *select_date_button;
    GtkWidget *select_date_button_label;
    GtkWidget *type_label;
    GtkWidget *type_listbox;
    GtkWidget *type_stack;
    GtkWidget *last_used_button;
    GtkWidget *last_modified_button;
Alexandru Pandelea's avatar
Alexandru Pandelea committed
48 49
    GtkWidget *full_text_search_button;
    GtkWidget *filename_search_button;
50 51

    NautilusQuery *query;
Alexandru Pandelea's avatar
Alexandru Pandelea committed
52 53

    gboolean fts_enabled;
54 55
};

56 57
static void          show_date_selection_widgets (NautilusSearchPopover *popover,
                                                  gboolean               visible);
58

59
static void          show_other_types_dialog (NautilusSearchPopover *popover);
60

61 62
static void          update_date_label (NautilusSearchPopover *popover,
                                        GPtrArray             *date_range);
63 64 65

G_DEFINE_TYPE (NautilusSearchPopover, nautilus_search_popover, GTK_TYPE_POPOVER)

66 67 68 69
enum
{
    PROP_0,
    PROP_QUERY,
Alexandru Pandelea's avatar
Alexandru Pandelea committed
70
    PROP_FTS_ENABLED,
71
    LAST_PROP
72 73
};

74 75 76 77 78 79
enum
{
    MIME_TYPE,
    TIME_TYPE,
    DATE_RANGE,
    LAST_SIGNAL
80 81 82 83 84 85 86 87 88 89 90
};

static guint signals[LAST_SIGNAL];


/* Callbacks */

static void
calendar_day_selected (GtkCalendar           *calendar,
                       NautilusSearchPopover *popover)
{
91 92 93
    GDateTime *date;
    guint year, month, day;
    GPtrArray *date_range;
94

95
    gtk_calendar_get_date (calendar, &year, &month, &day);
96

97
    date = g_date_time_new_local (year, month + 1, day, 0, 0, 0);
98

99 100 101 102 103
    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);
    g_signal_emit_by_name (popover, "date-range", date_range);
104

105 106
    g_ptr_array_unref (date_range);
    g_date_time_unref (date);
107 108
}

109 110 111
/* 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.
 */
112 113 114 115
static void
setup_date (NautilusSearchPopover *popover,
            NautilusQuery         *query)
{
116 117
    GPtrArray *date_range;
    GDateTime *date_initial;
118

119
    date_range = nautilus_query_get_date_range (query);
120

121 122 123
    if (date_range)
    {
        date_initial = g_ptr_array_index (date_range, 0);
124

125
        g_signal_handlers_block_by_func (popover->calendar, calendar_day_selected, popover);
126

127 128 129
        gtk_calendar_select_month (GTK_CALENDAR (popover->calendar),
                                   g_date_time_get_month (date_initial) - 1,
                                   g_date_time_get_year (date_initial));
130

131 132
        gtk_calendar_select_day (GTK_CALENDAR (popover->calendar),
                                 g_date_time_get_day_of_month (date_initial));
133

134
        update_date_label (popover, date_range);
135

136 137
        g_signal_handlers_unblock_by_func (popover->calendar, calendar_day_selected, popover);
    }
138 139 140 141 142 143 144
}

static void
query_date_changed (GObject               *object,
                    GParamSpec            *pspec,
                    NautilusSearchPopover *popover)
{
145
    setup_date (popover, NAUTILUS_QUERY (object));
146 147 148 149 150 151
}

static void
clear_date_button_clicked (GtkButton             *button,
                           NautilusSearchPopover *popover)
{
152
    nautilus_search_popover_reset_date_range (popover);
153 154 155 156 157 158
}

static void
date_entry_activate (GtkEntry              *entry,
                     NautilusSearchPopover *popover)
{
159
    if (gtk_entry_get_text_length (entry) > 0)
160
    {
161 162 163
        GDateTime *now;
        GDateTime *date_time;
        GDate *date;
164

165 166
        date = g_date_new ();
        g_date_set_parse (date, gtk_entry_get_text (entry));
167

168 169
        /* Invalid date silently does nothing */
        if (!g_date_valid (date))
170
        {
171 172
            g_date_free (date);
            return;
173
        }
174

175 176 177 178 179 180 181
        now = g_date_time_new_now_local ();
        date_time = g_date_time_new_local (g_date_get_year (date),
                                           g_date_get_month (date),
                                           g_date_get_day (date),
                                           0,
                                           0,
                                           0);
182

183 184
        /* Future dates also silently fails */
        if (g_date_time_compare (date_time, now) != 1)
185
        {
186
            GPtrArray *date_range;
187

188 189 190 191 192 193
            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);
            g_signal_emit_by_name (popover, "date-range", date_range);
194

195
            g_ptr_array_unref (date_range);
196
        }
197

198 199 200
        g_date_time_unref (now);
        g_date_time_unref (date_time);
        g_date_free (date);
201 202 203 204 205 206 207 208
    }
}

static void
dates_listbox_row_activated (GtkListBox            *listbox,
                             GtkListBoxRow         *row,
                             NautilusSearchPopover *popover)
{
209 210 211
    GDateTime *date;
    GDateTime *now;
    GPtrArray *date_range = NULL;
212

213 214 215
    now = g_date_time_new_now_local ();
    date = g_object_get_data (G_OBJECT (row), "date");
    if (date)
216
    {
217 218 219
        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));
220
    }
221 222 223
    update_date_label (popover, date_range);
    show_date_selection_widgets (popover, FALSE);
    g_signal_emit_by_name (popover, "date-range", date_range);
224

225 226 227 228 229
    if (date_range)
    {
        g_ptr_array_unref (date_range);
    }
    g_date_time_unref (now);
230 231 232 233 234 235 236
}

static void
listbox_header_func (GtkListBoxRow         *row,
                     GtkListBoxRow         *before,
                     NautilusSearchPopover *popover)
{
237
    gboolean show_separator;
238

239
    show_separator = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (row), "show-separator"));
240

241
    if (show_separator)
242
    {
243
        GtkWidget *separator;
244

245 246
        separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
        gtk_widget_show (separator);
247

248
        gtk_list_box_row_set_header (row, separator);
249
    }
250 251 252 253 254 255
}

static void
select_date_button_clicked (GtkButton             *button,
                            NautilusSearchPopover *popover)
{
256 257 258 259
    /* Hide the type selection widgets when date selection
     * widgets are shown.
     */
    gtk_stack_set_visible_child_name (GTK_STACK (popover->type_stack), "type-button");
260

261
    show_date_selection_widgets (popover, TRUE);
262 263 264 265 266 267
}

static void
select_type_button_clicked (GtkButton             *button,
                            NautilusSearchPopover *popover)
{
268 269 270 271
    GtkListBoxRow *selected_row;

    selected_row = gtk_list_box_get_selected_row (GTK_LIST_BOX (popover->type_listbox));

272
    gtk_stack_set_visible_child_name (GTK_STACK (popover->type_stack), "type-list");
273 274 275 276
    if (selected_row != NULL)
    {
        gtk_widget_grab_focus (GTK_WIDGET (selected_row));
    }
277

278 279 280 281
    /* Hide the date selection widgets when the type selection
     * listbox is shown.
     */
    show_date_selection_widgets (popover, FALSE);
282 283 284 285 286 287 288 289
}

static void
toggle_calendar_icon_clicked (GtkEntry              *entry,
                              GtkEntryIconPosition   position,
                              GdkEvent              *event,
                              NautilusSearchPopover *popover)
{
290 291 292 293
    const gchar *current_visible_child;
    const gchar *child;
    const gchar *icon_name;
    const gchar *tooltip;
294

295
    current_visible_child = gtk_stack_get_visible_child_name (GTK_STACK (popover->around_stack));
296

297
    if (g_strcmp0 (current_visible_child, "date-list") == 0)
298
    {
299 300 301
        child = "date-calendar";
        icon_name = "view-list-symbolic";
        tooltip = _("Show a list to select the date");
302
    }
303
    else
304
    {
305 306 307
        child = "date-list";
        icon_name = "x-office-calendar-symbolic";
        tooltip = _("Show a calendar to select the date");
308
    }
309

310 311 312
    gtk_stack_set_visible_child_name (GTK_STACK (popover->around_stack), child);
    gtk_entry_set_icon_from_icon_name (entry, GTK_ENTRY_ICON_SECONDARY, icon_name);
    gtk_entry_set_icon_tooltip_text (entry, GTK_ENTRY_ICON_SECONDARY, tooltip);
313 314 315 316 317 318 319
}

static void
types_listbox_row_activated (GtkListBox            *listbox,
                             GtkListBoxRow         *row,
                             NautilusSearchPopover *popover)
{
320
    gint group;
321

322
    group = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (row), "mimetype-group"));
323

324 325 326 327
    /* The -1 group stands for the "Other Types" group, for which
     * we should show the mimetype dialog.
     */
    if (group == -1)
328
    {
329
        show_other_types_dialog (popover);
330
    }
331
    else
332
    {
333 334
        gtk_label_set_label (GTK_LABEL (popover->type_label),
                             nautilus_mime_types_group_get_name (group));
335

336
        g_signal_emit_by_name (popover, "mime-type", group, NULL);
337
    }
338

339
    gtk_stack_set_visible_child_name (GTK_STACK (popover->type_stack), "type-button");
340 341
}

342 343 344 345
static void
search_time_type_changed (GtkToggleButton       *button,
                          NautilusSearchPopover *popover)
{
346
    NautilusQuerySearchType type = -1;
347

348
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (popover->last_modified_button)))
349
    {
350
        type = NAUTILUS_QUERY_SEARCH_TYPE_LAST_MODIFIED;
351
    }
352
    else
353
    {
354
        type = NAUTILUS_QUERY_SEARCH_TYPE_LAST_ACCESS;
355 356
    }

357
    g_settings_set_enum (nautilus_preferences, "search-filter-time-type", type);
358

359
    g_signal_emit_by_name (popover, "time-type", type, NULL);
360 361
}

Alexandru Pandelea's avatar
Alexandru Pandelea committed
362 363 364 365 366 367 368 369
static void
search_fts_mode_changed (GtkToggleButton       *button,
                         NautilusSearchPopover *popover)
{
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (popover->full_text_search_button)) &&
        popover->fts_enabled == FALSE)
    {
        popover->fts_enabled = TRUE;
370
        g_settings_set_boolean (nautilus_preferences, NAUTILUS_PREFERENCES_FTS_ENABLED, TRUE);
Alexandru Pandelea's avatar
Alexandru Pandelea committed
371 372 373 374 375 376
        g_object_notify (G_OBJECT (popover), "fts-enabled");
    }
    else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (popover->filename_search_button)) &&
             popover->fts_enabled == TRUE)
    {
        popover->fts_enabled = FALSE;
377
        g_settings_set_boolean (nautilus_preferences, NAUTILUS_PREFERENCES_FTS_ENABLED, FALSE);
Alexandru Pandelea's avatar
Alexandru Pandelea committed
378 379 380 381
        g_object_notify (G_OBJECT (popover), "fts-enabled");
    }
}

382 383
/* Auxiliary methods */

384
static GtkWidget *
385 386 387
create_row_for_label (const gchar *text,
                      gboolean     show_separator)
{
388 389
    GtkWidget *row;
    GtkWidget *label;
390

391
    row = gtk_list_box_row_new ();
392

393
    g_object_set_data (G_OBJECT (row), "show-separator", GINT_TO_POINTER (show_separator));
394

395 396 397 398 399 400
    label = g_object_new (GTK_TYPE_LABEL,
                          "label", text,
                          "hexpand", TRUE,
                          "xalign", 0.0,
                          "margin-start", 6,
                          NULL);
401

402 403
    gtk_container_add (GTK_CONTAINER (row), label);
    gtk_widget_show_all (row);
404

405
    return row;
406 407 408 409 410
}

static void
fill_fuzzy_dates_listbox (NautilusSearchPopover *popover)
{
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    GDateTime *maximum_dt, *now;
    GtkWidget *row;
    GDateTime *current_date;
    GPtrArray *date_range;
    gint days, max_days;

    days = 1;
    maximum_dt = g_date_time_new_from_unix_local (0);
    now = g_date_time_new_now_local ();
    max_days = SEARCH_FILTER_MAX_YEARS * 365;

    /* Add the no date filter element first */
    row = create_row_for_label (_("Any time"), TRUE);
    gtk_container_add (GTK_CONTAINER (popover->dates_listbox), row);

    /* This is a tricky loop. The main intention here is that each
     * timeslice (day, week, month) have 2 or 3 entries.
     *
     * For the first appearance of each timeslice, there is made a
     * check in order to be sure that there is no offset added to days.
     */
    while (days <= max_days)
433
    {
434 435 436
        gchar *label;
        gint normalized;
        gint step;
437

438
        if (days < 7)
439
        {
440 441 442
            /* days */
            normalized = days;
            step = 2;
443
        }
444
        else if (days < 30)
445
        {
446 447 448 449 450 451 452
            /* weeks */
            normalized = days / 7;
            if (normalized == 1)
            {
                days = 7;
            }
            step = 7;
453
        }
454
        else if (days < 365)
455
        {
456 457 458 459 460 461 462
            /* months */
            normalized = days / 30;
            if (normalized == 1)
            {
                days = 30;
            }
            step = 90;
463
        }
464
        else
465
        {
466 467 468 469 470 471 472
            /* years */
            normalized = days / 365;
            if (normalized == 1)
            {
                days = 365;
            }
            step = 365;
473
        }
474

475 476 477 478
        current_date = g_date_time_add_days (now, -days);
        date_range = g_ptr_array_new_full (2, (GDestroyNotify) g_date_time_unref);
        g_ptr_array_add (date_range, g_date_time_ref (current_date));
        g_ptr_array_add (date_range, g_date_time_ref (now));
479
        label = get_text_for_date_range (date_range, FALSE);
480
        row = create_row_for_label (label, normalized == 1);
481 482 483 484
        g_object_set_data_full (G_OBJECT (row),
                                "date",
                                g_date_time_ref (current_date),
                                (GDestroyNotify) g_date_time_unref);
485

486
        gtk_container_add (GTK_CONTAINER (popover->dates_listbox), row);
487

488 489 490
        g_free (label);
        g_date_time_unref (current_date);
        g_ptr_array_unref (date_range);
491

492
        days += step;
493
    }
494

495 496
    g_date_time_unref (maximum_dt);
    g_date_time_unref (now);
497 498 499 500 501
}

static void
fill_types_listbox (NautilusSearchPopover *popover)
{
502 503 504
    GtkWidget *row;
    int i;
    gint n_groups;
505

506 507 508
    n_groups = nautilus_mime_types_get_number_of_groups ();
    /* Mimetypes */
    for (i = 0; i < n_groups; i++)
509
    {
510 511 512 513 514
        /* On the third row, which is right below "Folders", there should be an
         * separator to logically group the types.
         */
        row = create_row_for_label (nautilus_mime_types_group_get_name (i), i == 3);
        g_object_set_data (G_OBJECT (row), "mimetype-group", GINT_TO_POINTER (i));
515

516
        gtk_container_add (GTK_CONTAINER (popover->type_listbox), row);
517
    }
518

519 520 521 522
    /* Other types */
    row = create_row_for_label (_("Other Type…"), TRUE);
    g_object_set_data (G_OBJECT (row), "mimetype-group", GINT_TO_POINTER (-1));
    gtk_container_add (GTK_CONTAINER (popover->type_listbox), row);
523 524 525 526 527 528
}

static void
show_date_selection_widgets (NautilusSearchPopover *popover,
                             gboolean               visible)
{
529 530 531 532 533 534
    gtk_stack_set_visible_child_name (GTK_STACK (popover->date_stack),
                                      visible ? "date-entry" : "date-button");
    gtk_stack_set_visible_child_name (GTK_STACK (popover->around_stack), "date-list");
    gtk_entry_set_icon_from_icon_name (GTK_ENTRY (popover->date_entry),
                                       GTK_ENTRY_ICON_SECONDARY,
                                       "x-office-calendar-symbolic");
535

536
    gtk_widget_set_visible (popover->around_revealer, visible);
537

538
    gtk_revealer_set_reveal_child (GTK_REVEALER (popover->around_revealer), visible);
539 540 541 542 543
}

static void
show_other_types_dialog (NautilusSearchPopover *popover)
{
544 545 546 547 548 549 550 551 552 553 554 555 556
    GList *mime_infos, *l;
    GtkWidget *dialog;
    GtkWidget *scrolled, *treeview;
    GtkListStore *store;
    GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;
    GtkWidget *toplevel;
    GtkTreeSelection *selection;

    mime_infos = g_content_types_get_registered ();

    store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
    for (l = mime_infos; l != NULL; l = l->next)
557
    {
558 559 560
        GtkTreeIter iter;
        char *mime_type = l->data;
        char *description;
561

562 563
        description = g_content_type_get_description (mime_type);
        if (description == NULL)
564
        {
565
            description = g_strdup (mime_type);
566
        }
567

568 569 570 571 572
        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter,
                            0, description,
                            1, mime_type,
                            -1);
573

574 575
        g_free (mime_type);
        g_free (description);
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
    g_list_free (mime_infos);

    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (popover));
    dialog = gtk_dialog_new_with_buttons (_("Select type"),
                                          GTK_WINDOW (toplevel),
                                          GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_USE_HEADER_BAR,
                                          _("_Cancel"), GTK_RESPONSE_CANCEL,
                                          _("Select"), GTK_RESPONSE_OK,
                                          NULL);
    gtk_window_set_default_size (GTK_WINDOW (dialog), 400, 600);

    scrolled = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
                                    GTK_POLICY_AUTOMATIC,
                                    GTK_POLICY_AUTOMATIC);

    gtk_widget_show (scrolled);
    gtk_container_set_border_width (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), 0);
    gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), scrolled, TRUE, TRUE, 0);

    treeview = gtk_tree_view_new ();
    gtk_tree_view_set_model (GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (store));
    gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (store), 0, GTK_SORT_ASCENDING);

    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
    gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);


    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("Name",
                                                       renderer,
                                                       "text",
                                                       0,
                                                       NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);

    gtk_widget_show (treeview);
    gtk_container_add (GTK_CONTAINER (scrolled), treeview);

    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK)
618
    {
619 620 621
        GtkTreeIter iter;
        char *mimetype;
        char *description;
622

623 624 625 626 627
        gtk_tree_selection_get_selected (selection, NULL, &iter);
        gtk_tree_model_get (GTK_TREE_MODEL (store), &iter,
                            0, &description,
                            1, &mimetype,
                            -1);
628

629
        gtk_label_set_label (GTK_LABEL (popover->type_label), description);
630

631
        g_signal_emit_by_name (popover, "mime-type", -1, mimetype);
632

633
        gtk_stack_set_visible_child_name (GTK_STACK (popover->type_stack), "type-button");
634
    }
635

636
    gtk_widget_destroy (dialog);
637 638 639 640
}

static void
update_date_label (NautilusSearchPopover *popover,
641
                   GPtrArray             *date_range)
642
{
643
    if (date_range)
644
    {
645 646 647 648 649
        gint days;
        GDateTime *initial_date;
        GDateTime *end_date;
        GDateTime *now;
        gchar *label;
650

651 652 653 654
        now = g_date_time_new_now_local ();
        initial_date = g_ptr_array_index (date_range, 0);
        end_date = g_ptr_array_index (date_range, 0);
        days = g_date_time_difference (end_date, initial_date) / G_TIME_SPAN_DAY;
655

656
        label = get_text_for_date_range (date_range, TRUE);
657

658
        gtk_entry_set_text (GTK_ENTRY (popover->date_entry), days < 1 ? label : "");
659

660 661
        gtk_widget_show (popover->clear_date_button);
        gtk_label_set_label (GTK_LABEL (popover->select_date_button_label), label);
662

663 664
        g_date_time_unref (now);
        g_free (label);
665
    }
666
    else
667
    {
668 669 670 671
        gtk_label_set_label (GTK_LABEL (popover->select_date_button_label),
                             _("Select Dates…"));
        gtk_entry_set_text (GTK_ENTRY (popover->date_entry), "");
        gtk_widget_hide (popover->clear_date_button);
672 673
    }
}
674

Alexandru Pandelea's avatar
Alexandru Pandelea committed
675 676 677 678 679 680 681 682
void
nautilus_search_popover_set_fts_sensitive (NautilusSearchPopover *popover,
                                           gboolean               sensitive)
{
    gtk_widget_set_sensitive (popover->full_text_search_button, sensitive);
    gtk_widget_set_sensitive (popover->filename_search_button, sensitive);
}

683 684 685
static void
nautilus_search_popover_closed (GtkPopover *popover)
{
686 687
    NautilusSearchPopover *self = NAUTILUS_SEARCH_POPOVER (popover);
    GDateTime *now;
688

689 690 691
    /* Always switch back to the initial states */
    gtk_stack_set_visible_child_name (GTK_STACK (self->type_stack), "type-button");
    show_date_selection_widgets (self, FALSE);
692

693 694 695 696 697 698 699
    /* If we're closing an ongoing query, the popover must not
     * clear the current settings.
     */
    if (self->query)
    {
        return;
    }
700

701
    now = g_date_time_new_now_local ();
702

703 704
    /* Reselect today at the calendar */
    g_signal_handlers_block_by_func (self->calendar, calendar_day_selected, self);
705

706 707 708
    gtk_calendar_select_month (GTK_CALENDAR (self->calendar),
                               g_date_time_get_month (now) - 1,
                               g_date_time_get_year (now));
709

710 711
    gtk_calendar_select_day (GTK_CALENDAR (self->calendar),
                             g_date_time_get_day_of_month (now));
712

713
    g_signal_handlers_unblock_by_func (self->calendar, calendar_day_selected, self);
714 715 716 717 718 719 720 721
}

static void
nautilus_search_popover_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
722
    NautilusSearchPopover *self;
723

724
    self = NAUTILUS_SEARCH_POPOVER (object);
725

726
    switch (prop_id)
727
    {
728 729 730 731
        case PROP_QUERY:
        {
            g_value_set_object (value, self->query);
        }
732
        break;
733

Alexandru Pandelea's avatar
Alexandru Pandelea committed
734 735 736 737 738 739
        case PROP_FTS_ENABLED:
        {
            g_value_set_boolean (value, self->fts_enabled);
        }
        break;

740 741
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
742 743 744 745 746 747 748 749 750
    }
}

static void
nautilus_search_popover_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
751
    NautilusSearchPopover *self;
752

753
    self = NAUTILUS_SEARCH_POPOVER (object);
754

755
    switch (prop_id)
756
    {
757 758 759 760
        case PROP_QUERY:
        {
            nautilus_search_popover_set_query (self, g_value_get_object (value));
        }
761
        break;
762

Alexandru Pandelea's avatar
Alexandru Pandelea committed
763 764 765 766 767 768
        case PROP_FTS_ENABLED:
        {
            self->fts_enabled = g_value_get_boolean (value);
        }
        break;

769 770
        default:
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
771
    }
772 773 774 775 776 777
}


static void
nautilus_search_popover_class_init (NautilusSearchPopoverClass *klass)
{
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    GtkPopoverClass *popover_class = GTK_POPOVER_CLASS (klass);
    GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    object_class->get_property = nautilus_search_popover_get_property;
    object_class->set_property = nautilus_search_popover_set_property;

    popover_class->closed = nautilus_search_popover_closed;

    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,
796
                                        G_TYPE_PTR_ARRAY);
797 798 799 800 801 802 803 804 805 806 807

    signals[MIME_TYPE] = g_signal_new ("mime-type",
                                       NAUTILUS_TYPE_SEARCH_POPOVER,
                                       G_SIGNAL_RUN_LAST,
                                       0,
                                       NULL,
                                       NULL,
                                       g_cclosure_marshal_generic,
                                       G_TYPE_NONE,
                                       2,
                                       G_TYPE_INT,
808
                                       G_TYPE_STRING);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833

    signals[TIME_TYPE] = g_signal_new ("time-type",
                                       NAUTILUS_TYPE_SEARCH_POPOVER,
                                       G_SIGNAL_RUN_LAST,
                                       0,
                                       NULL,
                                       NULL,
                                       g_cclosure_marshal_generic,
                                       G_TYPE_NONE,
                                       1,
                                       G_TYPE_INT);

    /**
     * NautilusSearchPopover::query:
     *
     * The current #NautilusQuery being edited.
     */
    g_object_class_install_property (object_class,
                                     PROP_QUERY,
                                     g_param_spec_object ("query",
                                                          "Query of the popover",
                                                          "The current query being edited",
                                                          NAUTILUS_TYPE_QUERY,
                                                          G_PARAM_READWRITE));

Alexandru Pandelea's avatar
Alexandru Pandelea committed
834 835 836 837 838 839 840 841
    g_object_class_install_property (object_class,
                                     PROP_FTS_ENABLED,
                                     g_param_spec_boolean ("fts-enabled",
                                                           "fts enabled",
                                                           "fts enabled",
                                                           FALSE,
                                                           G_PARAM_READWRITE));

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
    gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/nautilus/ui/nautilus-search-popover.ui");

    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, around_revealer);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, around_stack);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, clear_date_button);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, calendar);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, dates_listbox);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, date_entry);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, date_stack);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, select_date_button);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, select_date_button_label);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, type_label);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, type_listbox);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, type_stack);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, last_used_button);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, last_modified_button);
Alexandru Pandelea's avatar
Alexandru Pandelea committed
858 859
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, full_text_search_button);
    gtk_widget_class_bind_template_child (widget_class, NautilusSearchPopover, filename_search_button);
860 861 862 863 864 865 866 867 868 869

    gtk_widget_class_bind_template_callback (widget_class, calendar_day_selected);
    gtk_widget_class_bind_template_callback (widget_class, clear_date_button_clicked);
    gtk_widget_class_bind_template_callback (widget_class, date_entry_activate);
    gtk_widget_class_bind_template_callback (widget_class, dates_listbox_row_activated);
    gtk_widget_class_bind_template_callback (widget_class, select_date_button_clicked);
    gtk_widget_class_bind_template_callback (widget_class, select_type_button_clicked);
    gtk_widget_class_bind_template_callback (widget_class, toggle_calendar_icon_clicked);
    gtk_widget_class_bind_template_callback (widget_class, types_listbox_row_activated);
    gtk_widget_class_bind_template_callback (widget_class, search_time_type_changed);
Alexandru Pandelea's avatar
Alexandru Pandelea committed
870
    gtk_widget_class_bind_template_callback (widget_class, search_fts_mode_changed);
871 872 873 874 875
}

static void
nautilus_search_popover_init (NautilusSearchPopover *self)
{
876
    NautilusQuerySearchType filter_time_type;
877

878
    gtk_widget_init_template (GTK_WIDGET (self));
879

880 881 882 883 884
    /* Fuzzy dates listbox */
    gtk_list_box_set_header_func (GTK_LIST_BOX (self->dates_listbox),
                                  (GtkListBoxUpdateHeaderFunc) listbox_header_func,
                                  self,
                                  NULL);
885

886
    fill_fuzzy_dates_listbox (self);
887

888 889 890 891 892
    /* Types listbox */
    gtk_list_box_set_header_func (GTK_LIST_BOX (self->type_listbox),
                                  (GtkListBoxUpdateHeaderFunc) listbox_header_func,
                                  self,
                                  NULL);
893

894
    fill_types_listbox (self);
895

896 897 898
    gtk_list_box_select_row (GTK_LIST_BOX (self->type_listbox),
                             gtk_list_box_get_row_at_index (GTK_LIST_BOX (self->type_listbox), 0));

899 900
    filter_time_type = g_settings_get_enum (nautilus_preferences, "search-filter-time-type");
    if (filter_time_type == NAUTILUS_QUERY_SEARCH_TYPE_LAST_MODIFIED)
901
    {
902 903
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->last_modified_button), TRUE);
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->last_used_button), FALSE);
904
    }
905
    else
906
    {
907 908
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->last_modified_button), FALSE);
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->last_used_button), TRUE);
909
    }
Alexandru Pandelea's avatar
Alexandru Pandelea committed
910

911 912
    self->fts_enabled = g_settings_get_boolean (nautilus_preferences,
                                                NAUTILUS_PREFERENCES_FTS_ENABLED);
Alexandru Pandelea's avatar
Alexandru Pandelea committed
913 914 915 916 917 918 919 920
    if (self->fts_enabled)
    {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->full_text_search_button), TRUE);
    }
    else
    {
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->filename_search_button), TRUE);
    }
921 922
}

923
GtkWidget *
924 925
nautilus_search_popover_new (void)
{
926
    return g_object_new (NAUTILUS_TYPE_SEARCH_POPOVER, NULL);
927 928 929 930 931 932 933 934 935 936
}

/**
 * nautilus_search_popover_get_query:
 * @popover: a #NautilusSearchPopover
 *
 * Gets the current query for @popover.
 *
 * Returns: (transfer none): the current #NautilusQuery from @popover.
 */
937
NautilusQuery *
938 939
nautilus_search_popover_get_query (NautilusSearchPopover *popover)
{
940
    g_return_val_if_fail (NAUTILUS_IS_SEARCH_POPOVER (popover), NULL);
941

942
    return popover->query;
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
}

/**
 * nautilus_search_popover_set_query:
 * @popover: a #NautilusSearchPopover
 * @query (nullable): a #NautilusQuery
 *
 * Sets the current query for @popover.
 *
 * Returns:
 */
void
nautilus_search_popover_set_query (NautilusSearchPopover *popover,
                                   NautilusQuery         *query)
{
958
    NautilusQuery *previous_query;
959

960
    g_return_if_fail (NAUTILUS_IS_SEARCH_POPOVER (popover));
961

962
    previous_query = popover->query;
963

964
    if (popover->query != query)
965
    {
966 967
        /* Disconnect signals and bindings from the old query */
        if (previous_query)
968
        {
969
            g_signal_handlers_disconnect_by_func (previous_query, query_date_changed, popover);
970
        }
971

972
        g_set_object (&popover->query, query);
973

974
        if (query)
975
        {
976 977
            /* Date */
            setup_date (popover, query);
978

979 980 981 982
            g_signal_connect (query,
                              "notify::date",
                              G_CALLBACK (query_date_changed),
                              popover);
983
        }
984
        else
985
        {
986 987
            nautilus_search_popover_reset_mime_types (popover);
            nautilus_search_popover_reset_date_range (popover);
988 989
        }
    }
990
}
991 992 993 994

void
nautilus_search_popover_reset_mime_types (NautilusSearchPopover *popover)
{
995
    g_return_if_fail (NAUTILUS_IS_SEARCH_POPOVER (popover));
996

997 998
    gtk_list_box_select_row (GTK_LIST_BOX (popover->type_listbox),
                             gtk_list_box_get_row_at_index (GTK_LIST_BOX (popover->type_listbox), 0));
999

1000 1001 1002 1003
    gtk_label_set_label (GTK_LABEL (popover->type_label),
                         nautilus_mime_types_group_get_name (0));
    g_signal_emit_by_name (popover, "mime-type", 0, NULL);
    gtk_stack_set_visible_child_name (GTK_STACK (popover->type_stack), "type-button");
1004 1005 1006 1007 1008
}

void
nautilus_search_popover_reset_date_range (NautilusSearchPopover *popover)
{
1009
    g_return_if_fail (NAUTILUS_IS_SEARCH_POPOVER (popover));
1010

1011 1012
    gtk_list_box_select_row (GTK_LIST_BOX (popover->dates_listbox),
                             gtk_list_box_get_row_at_index (GTK_LIST_BOX (popover->dates_listbox), 0));
1013

1014 1015 1016
    update_date_label (popover, NULL);
    show_date_selection_widgets (popover, FALSE);
    g_signal_emit_by_name (popover, "date-range", NULL);
1017
}
Alexandru Pandelea's avatar
Alexandru Pandelea committed
1018 1019 1020 1021 1022 1023

gboolean
nautilus_search_popover_get_fts_enabled (NautilusSearchPopover *popover)
{
    return popover->fts_enabled;
}