nautilus-batch-rename-dialog.c 73.6 KB
Newer Older
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
/* nautilus-batch-rename-dialog.c
 *
 * Copyright (C) 2016 Alexandru Pandelea <alexandru.pandelea@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 2 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 <config.h>

#include "nautilus-batch-rename-dialog.h"
#include "nautilus-file.h"
#include "nautilus-error-reporting.h"
#include "nautilus-batch-rename-utilities.h"

#include <glib/gprintf.h>
#include <glib.h>
#include <string.h>
#include <glib/gi18n.h>

31
#define ROW_MARGIN_START 6
32
#define ROW_MARGIN_TOP_BOTTOM 4
33

34 35
struct _NautilusBatchRenameDialog
{
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
    GtkDialog parent;

    GtkWidget *grid;
    NautilusWindow *window;

    GtkWidget *cancel_button;
    GtkWidget *original_name_listbox;
    GtkWidget *arrow_listbox;
    GtkWidget *result_listbox;
    GtkWidget *name_entry;
    GtkWidget *rename_button;
    GtkWidget *find_entry;
    GtkWidget *mode_stack;
    GtkWidget *replace_entry;
    GtkWidget *format_mode_button;
    GtkWidget *replace_mode_button;
    GtkWidget *add_button;
    GtkWidget *add_popover;
    GtkWidget *numbering_order_label;
    GtkWidget *numbering_label;
    GtkWidget *scrolled_window;
    GtkWidget *numbering_order_popover;
    GtkWidget *numbering_order_button;
    GtkWidget *conflict_box;
    GtkWidget *conflict_label;
    GtkWidget *conflict_down;
    GtkWidget *conflict_up;

    GList *original_name_listbox_rows;
    GList *arrow_listbox_rows;
    GList *result_listbox_rows;
    GList *listbox_labels_new;
    GList *listbox_labels_old;
    GList *listbox_icons;
    GtkSizeGroup *size_group;

    GList *selection;
    GList *new_names;
    NautilusBatchRenameDialogMode mode;
    NautilusDirectory *directory;

    GActionGroup *action_group;

    GMenu *numbering_order_menu;
    GMenu *add_tag_menu;

    GHashTable *create_date;
    GList *selection_metadata;

    /* the index of the currently selected conflict */
    gint selected_conflict;
    /* total conflicts number */
    gint conflicts_number;

    GList *duplicates;
    GCancellable *conflict_cancellable;
    gboolean checking_conflicts;

    /* this hash table has information about the status
     * of all tags: availability, if it's currently used
     * and position */
    GHashTable *tag_info_table;

    GtkWidget *preselected_row1;
    GtkWidget *preselected_row2;

    gint row_height;
    gboolean rename_clicked;
104 105 106 107
};

typedef struct
{
108 109 110 111 112
    gboolean available;
    gboolean set;
    gint position;
    /* if the tag was just added, then we shouldn't update it's position */
    gboolean just_added;
113
    TagConstants tag_constants;
114 115
} TagData;

116

117
static void     update_display_text (NautilusBatchRenameDialog *dialog);
118 119 120 121

G_DEFINE_TYPE (NautilusBatchRenameDialog, nautilus_batch_rename_dialog, GTK_TYPE_DIALOG);

static void
122 123 124
change_numbering_order (GSimpleAction *action,
                        GVariant      *value,
                        gpointer       user_data)
125
{
126 127
    NautilusBatchRenameDialog *dialog;
    const gchar *target_name;
128
    guint i;
129 130 131 132 133

    dialog = NAUTILUS_BATCH_RENAME_DIALOG (user_data);

    target_name = g_variant_get_string (value, NULL);

134
    for (i = 0; i < G_N_ELEMENTS (sorts_constants); i++)
135
    {
136 137 138 139 140 141 142
        if (g_strcmp0 (sorts_constants[i].action_target_name, target_name) == 0)
        {
            gtk_label_set_label (GTK_LABEL (dialog->numbering_order_label),
                                 gettext (sorts_constants[i].label));
            dialog->selection = nautilus_batch_rename_dialog_sort (dialog->selection,
                                                                   sorts_constants[i].sort_mode,
                                                                   NULL);
143
          break;
144
        }
145 146 147 148 149 150 151 152
    }

    g_simple_action_set_state (action, value);

    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->numbering_order_button), FALSE);

    update_display_text (dialog);
}
153

154
static void
155 156
enable_action (NautilusBatchRenameDialog *self,
               const gchar               *action_name)
157
{
158
    GAction *action;
159

160 161 162
    action = g_action_map_lookup_action (G_ACTION_MAP (self->action_group),
                                         action_name);
    g_simple_action_set_enabled (G_SIMPLE_ACTION (action), TRUE);
163
}
164

165
static void
166 167
disable_action (NautilusBatchRenameDialog *self,
                const gchar               *action_name)
168 169
{
    GAction *action;
170

171
    action = g_action_map_lookup_action (G_ACTION_MAP (self->action_group),
172 173
                                         action_name);
    g_simple_action_set_enabled (G_SIMPLE_ACTION (action), FALSE);
174 175 176
}

static void
177 178
add_tag (NautilusBatchRenameDialog *self,
         TagConstants               tag_constants)
179
{
180
    g_autofree gchar *tag_text_representation = NULL;
181 182
    gint cursor_position;
    TagData *tag_data;
183

184
    g_object_get (self->name_entry, "cursor-position", &cursor_position, NULL);
185

186 187 188 189 190 191
    tag_text_representation = batch_rename_get_tag_text_representation (tag_constants);
    tag_data = g_hash_table_lookup (self->tag_info_table, tag_text_representation);
    tag_data->available = TRUE;
    tag_data->set = TRUE;
    tag_data->just_added = TRUE;
    tag_data->position = cursor_position;
192

193 194 195 196
    /* FIXME: We can add a tag when the cursor is inside a tag, which breaks this.
     * We need to check the cursor movement and update the actions acordingly or
     * even better add the tag at the end of the previous tag if this happens.
     */
197 198 199 200
    gtk_editable_insert_text (GTK_EDITABLE (self->name_entry),
                              tag_text_representation,
                              g_utf8_strlen (tag_text_representation, -1),
                              &cursor_position);
201
    tag_data->just_added = FALSE;
202
    gtk_editable_set_position (GTK_EDITABLE (self->name_entry), cursor_position);
203

204 205
    gtk_entry_grab_focus_without_selecting (GTK_ENTRY (self->name_entry));
}
206

207 208 209 210 211 212 213 214
static void
add_metadata_tag (GSimpleAction *action,
                  GVariant      *value,
                  gpointer       user_data)
{
    NautilusBatchRenameDialog *self;
    const gchar *action_name;
    guint i;
215

216 217
    self = NAUTILUS_BATCH_RENAME_DIALOG (user_data);
    action_name = g_action_get_name (G_ACTION (action));
218

219
    for (i = 0; i < G_N_ELEMENTS (metadata_tags_constants); i++)
220
    {
221 222 223 224
        if (g_strcmp0 (metadata_tags_constants[i].action_name, action_name) == 0)
        {
            add_tag (self, metadata_tags_constants[i]);
            disable_action (self, metadata_tags_constants[i].action_name);
225

226 227
            break;
        }
228
    }
229 230 231
}

static void
232 233 234
add_numbering_tag (GSimpleAction *action,
                   GVariant      *value,
                   gpointer       user_data)
235
{
236
    NautilusBatchRenameDialog *self;
237
    const gchar *action_name;
238
    guint i;
239

240
    self = NAUTILUS_BATCH_RENAME_DIALOG (user_data);
241 242
    action_name = g_action_get_name (G_ACTION (action));

243
    for (i = 0; i < G_N_ELEMENTS (numbering_tags_constants); i++)
244
    {
245 246 247 248 249 250 251
        if (g_strcmp0 (numbering_tags_constants[i].action_name, action_name) == 0)
        {
            add_tag (self, numbering_tags_constants[i]);
        }
        /* We want to allow only one tag of numbering type, so we disable all
         * of them */
        disable_action (self, numbering_tags_constants[i].action_name);
252 253
    }
}
254

255 256
const GActionEntry dialog_entries[] =
{
257 258 259 260 261
    { "numbering-order-changed", NULL, "s", "'name-ascending'", change_numbering_order },
    { "add-numbering-no-zero-pad-tag", add_numbering_tag },
    { "add-numbering-one-zero-pad-tag", add_numbering_tag },
    { "add-numbering-two-zero-pad-tag", add_numbering_tag },
    { "add-original-file-name-tag", add_metadata_tag },
262 263
    { "add-creation-date-tag", add_metadata_tag },
    { "add-equipment-tag", add_metadata_tag },
264 265
    { "add-season-number-tag", add_metadata_tag },
    { "add-episode-number-tag", add_metadata_tag },
266 267 268 269 270
    { "add-video-album-tag", add_metadata_tag },
    { "add-track-number-tag", add_metadata_tag },
    { "add-artist-name-tag", add_metadata_tag },
    { "add-title-tag", add_metadata_tag },
    { "add-album-name-tag", add_metadata_tag },
271 272 273 274 275 276 277
};

static void
row_selected (GtkListBox    *box,
              GtkListBoxRow *listbox_row,
              gpointer       user_data)
{
278 279 280
    NautilusBatchRenameDialog *dialog;
    GtkListBoxRow *row;
    gint index;
281

282 283 284 285
    if (!GTK_IS_LIST_BOX_ROW (listbox_row))
    {
        return;
    }
286

287 288
    dialog = NAUTILUS_BATCH_RENAME_DIALOG (user_data);
    index = gtk_list_box_row_get_index (listbox_row);
289

290 291 292 293 294 295 296 297 298
    if (GTK_WIDGET (box) == dialog->original_name_listbox)
    {
        row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (dialog->arrow_listbox), index);
        gtk_list_box_select_row (GTK_LIST_BOX (dialog->arrow_listbox),
                                 row);
        row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (dialog->result_listbox), index);
        gtk_list_box_select_row (GTK_LIST_BOX (dialog->result_listbox),
                                 row);
    }
299

300 301 302 303 304 305 306 307 308
    if (GTK_WIDGET (box) == dialog->arrow_listbox)
    {
        row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (dialog->original_name_listbox), index);
        gtk_list_box_select_row (GTK_LIST_BOX (dialog->original_name_listbox),
                                 row);
        row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (dialog->result_listbox), index);
        gtk_list_box_select_row (GTK_LIST_BOX (dialog->result_listbox),
                                 row);
    }
309

310 311 312 313 314 315 316 317 318
    if (GTK_WIDGET (box) == dialog->result_listbox)
    {
        row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (dialog->arrow_listbox), index);
        gtk_list_box_select_row (GTK_LIST_BOX (dialog->arrow_listbox),
                                 row);
        row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (dialog->original_name_listbox), index);
        gtk_list_box_select_row (GTK_LIST_BOX (dialog->original_name_listbox),
                                 row);
    }
319 320 321
}

static gint
322 323
compare_int (gconstpointer a,
             gconstpointer b)
324
{
325 326
    int *number1 = (int *) a;
    int *number2 = (int *) b;
327

328
    return *number1 - *number2;
329 330 331 332 333
}

/* This function splits the entry text into a list of regular text and tags.
 * For instance, "[1, 2, 3]Paris[Creation date]" would result in:
 * "[1, 2, 3]", "Paris", "[Creation date]" */
334
static GList *
335
split_entry_text (NautilusBatchRenameDialog *self,
336 337
                  gchar                     *entry_text)
{
338 339 340
    GString *normal_text;
    GString *tag;
    GArray *tag_positions;
341 342
    g_autoptr (GList) tag_info_keys = NULL;
    GList *l;
343 344 345 346 347 348 349 350 351 352 353
    gint tags;
    gint i;
    gchar *substring;
    gint tag_end_position;
    GList *result = NULL;
    TagData *tag_data;

    tags = 0;
    tag_end_position = 0;
    tag_positions = g_array_new (FALSE, FALSE, sizeof (gint));

354
    tag_info_keys = g_hash_table_get_keys (self->tag_info_table);
355

356
    for (l = tag_info_keys; l != NULL; l = l->next)
357
    {
358 359 360 361 362 363
        tag_data = g_hash_table_lookup (self->tag_info_table, l->data);
        if (tag_data->set)
        {
            g_array_append_val (tag_positions, tag_data->position);
            tags++;
        }
364 365
    }

366
    g_array_sort (tag_positions, compare_int);
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

    for (i = 0; i < tags; i++)
    {
        tag = g_string_new ("");

        substring = g_utf8_substring (entry_text, tag_end_position,
                                      g_array_index (tag_positions, gint, i));
        normal_text = g_string_new (substring);
        g_free (substring);

        if (g_strcmp0 (normal_text->str, ""))
        {
            result = g_list_prepend (result, normal_text);
        }

382
        for (l = tag_info_keys; l != NULL; l = l->next)
383
        {
384
            g_autofree gchar *tag_text_representation = NULL;
385

386 387 388 389 390 391 392
            tag_data = g_hash_table_lookup (self->tag_info_table, l->data);
            if (tag_data->set && g_array_index (tag_positions, gint, i) == tag_data->position)
            {
                tag_text_representation = batch_rename_get_tag_text_representation (tag_data->tag_constants);
                tag_end_position = g_array_index (tag_positions, gint, i) +
                                   g_utf8_strlen (tag_text_representation, -1);
                tag = g_string_append (tag, tag_text_representation);
393

394 395
                break;
            }
396 397
        }

398 399
        result = g_list_prepend (result, tag);
    }
400

401
    normal_text = g_string_new (g_utf8_offset_to_pointer (entry_text, tag_end_position));
402

403 404 405 406
    if (g_strcmp0 (normal_text->str, "") != 0)
    {
        result = g_list_prepend (result, normal_text);
    }
407

408
    result = g_list_reverse (result);
409

410 411
    g_array_free (tag_positions, TRUE);
    return result;
412 413
}

414
static GList *
415 416
batch_rename_dialog_get_new_names (NautilusBatchRenameDialog *dialog)
{
417 418 419
    GList *result = NULL;
    GList *selection;
    GList *text_chunks;
420 421
    g_autofree gchar *entry_text = NULL;
    g_autofree gchar *replace_text = NULL;
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

    selection = dialog->selection;
    text_chunks = NULL;

    if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_REPLACE)
    {
        entry_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->find_entry)));
    }
    else
    {
        entry_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->name_entry)));
    }

    replace_text = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->replace_entry)));

    if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_REPLACE)
    {
        result = batch_rename_dialog_get_new_names_list (dialog->mode,
                                                         selection,
                                                         NULL,
                                                         NULL,
                                                         entry_text,
                                                         replace_text);
    }
    else
    {
        text_chunks = split_entry_text (dialog, entry_text);

        result = batch_rename_dialog_get_new_names_list (dialog->mode,
                                                         selection,
                                                         text_chunks,
                                                         dialog->selection_metadata,
                                                         entry_text,
                                                         replace_text);
        g_list_free_full (text_chunks, string_free);
    }

    result = g_list_reverse (result);

    return result;
462 463 464 465 466 467
}

static void
begin_batch_rename (NautilusBatchRenameDialog *dialog,
                    GList                     *new_names)
{
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
    GList *new_names_list;
    GList *files;
    GList *files2;
    GList *new_names_list2;
    gchar *file_name;
    gchar *old_file_name;
    GString *new_file_name;
    GString *new_name;
    NautilusFile *file;

    /* in the following case:
     * file1 -> file2
     * file2 -> file3
     * file2 must be renamed first, so because of that, the list has to be reordered */
    for (new_names_list = new_names, files = dialog->selection;
         new_names_list != NULL && files != NULL;
         new_names_list = new_names_list->next, files = files->next)
    {
        old_file_name = nautilus_file_get_name (NAUTILUS_FILE (files->data));
        new_file_name = new_names_list->data;

        for (files2 = dialog->selection, new_names_list2 = new_names;
             files2 != NULL && new_names_list2 != NULL;
             files2 = files2->next, new_names_list2 = new_names_list2->next)
        {
            file_name = nautilus_file_get_name (NAUTILUS_FILE (files2->data));
            if (files2 != files && g_strcmp0 (file_name, new_file_name->str) == 0)
            {
                file = NAUTILUS_FILE (files2->data);
                new_name = new_names_list2->data;

                dialog->selection = g_list_remove_link (dialog->selection, files2);
                new_names = g_list_remove_link (new_names, new_names_list2);
501

502 503 504 505 506 507 508 509 510
                dialog->selection = g_list_prepend (dialog->selection, file);
                new_names = g_list_prepend (new_names, new_name);

                g_free (file_name);

                break;
            }

            g_free (file_name);
511 512
        }

513 514 515 516 517
        g_free (old_file_name);
    }

    /* do the actual rename here */
    nautilus_file_batch_rename (dialog->selection, new_names, NULL, NULL);
518

519
    gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (dialog->window)), NULL);
520 521 522
}

static void
523 524 525
listbox_header_func (GtkListBoxRow             *row,
                     GtkListBoxRow             *before,
                     NautilusBatchRenameDialog *dialog)
526
{
527
    gboolean show_separator;
528

529 530
    show_separator = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (row),
                                                         "show-separator"));
531

532 533 534
    if (show_separator)
    {
        GtkWidget *separator;
535

536 537
        separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
        gtk_widget_show (separator);
538

539 540
        gtk_list_box_row_set_header (row, separator);
    }
541 542 543 544 545 546 547
}

/* This is manually done instead of using GtkSizeGroup because of the computational
 * complexity of the later.*/
static void
update_rows_height (NautilusBatchRenameDialog *dialog)
{
548 549 550
    GList *l;
    gint current_row_natural_height;
    gint maximum_height;
551

552
    maximum_height = -1;
553

554 555 556 557 558 559
    /* check if maximum height has changed */
    for (l = dialog->listbox_labels_new; l != NULL; l = l->next)
    {
        gtk_widget_get_preferred_height (GTK_WIDGET (l->data),
                                         NULL,
                                         &current_row_natural_height);
560

561 562 563
        if (current_row_natural_height > maximum_height)
        {
            maximum_height = current_row_natural_height;
564
        }
565
    }
566

567 568 569 570 571
    for (l = dialog->listbox_labels_old; l != NULL; l = l->next)
    {
        gtk_widget_get_preferred_height (GTK_WIDGET (l->data),
                                         NULL,
                                         &current_row_natural_height);
572

573 574 575
        if (current_row_natural_height > maximum_height)
        {
            maximum_height = current_row_natural_height;
576
        }
577
    }
578

579 580 581 582 583
    for (l = dialog->listbox_icons; l != NULL; l = l->next)
    {
        gtk_widget_get_preferred_height (GTK_WIDGET (l->data),
                                         NULL,
                                         &current_row_natural_height);
584

585 586 587
        if (current_row_natural_height > maximum_height)
        {
            maximum_height = current_row_natural_height;
588
        }
589
    }
590

591 592
    if (maximum_height != dialog->row_height)
    {
593
        dialog->row_height = maximum_height + ROW_MARGIN_TOP_BOTTOM * 2;
594

595 596 597 598
        for (l = dialog->listbox_icons; l != NULL; l = l->next)
        {
            g_object_set (G_OBJECT (l->data), "height-request", dialog->row_height, NULL);
        }
599

600 601 602 603
        for (l = dialog->listbox_labels_new; l != NULL; l = l->next)
        {
            g_object_set (G_OBJECT (l->data), "height-request", dialog->row_height, NULL);
        }
604

605 606 607
        for (l = dialog->listbox_labels_old; l != NULL; l = l->next)
        {
            g_object_set (G_OBJECT (l->data), "height-request", dialog->row_height, NULL);
608
        }
609
    }
610 611
}

612
static GtkWidget *
613 614 615 616
create_original_name_row_for_label (NautilusBatchRenameDialog *dialog,
                                    const gchar               *old_text,
                                    gboolean                   show_separator)
{
617 618
    GtkWidget *row;
    GtkWidget *label_old;
619

620
    row = gtk_list_box_row_new ();
621

622
    g_object_set_data (G_OBJECT (row), "show-separator", GINT_TO_POINTER (show_separator));
623

624 625 626
    label_old = gtk_label_new (old_text);
    gtk_label_set_xalign (GTK_LABEL (label_old), 0.0);
    gtk_widget_set_hexpand (label_old, TRUE);
627
    gtk_widget_set_margin_start (label_old, ROW_MARGIN_START);
628

629
    gtk_label_set_ellipsize (GTK_LABEL (label_old), PANGO_ELLIPSIZE_END);
630

631
    dialog->listbox_labels_old = g_list_prepend (dialog->listbox_labels_old, label_old);
632

633 634
    gtk_container_add (GTK_CONTAINER (row), label_old);
    gtk_widget_show_all (row);
635

636
    return row;
637 638
}

639
static GtkWidget *
640 641 642 643
create_result_row_for_label (NautilusBatchRenameDialog *dialog,
                             const gchar               *new_text,
                             gboolean                   show_separator)
{
644 645
    GtkWidget *row;
    GtkWidget *label_new;
646

647
    row = gtk_list_box_row_new ();
648

649
    g_object_set_data (G_OBJECT (row), "show-separator", GINT_TO_POINTER (show_separator));
650

651 652 653
    label_new = gtk_label_new (new_text);
    gtk_label_set_xalign (GTK_LABEL (label_new), 0.0);
    gtk_widget_set_hexpand (label_new, TRUE);
654
    gtk_widget_set_margin_start (label_new, ROW_MARGIN_START);
655

656
    gtk_label_set_ellipsize (GTK_LABEL (label_new), PANGO_ELLIPSIZE_END);
657

658
    dialog->listbox_labels_new = g_list_prepend (dialog->listbox_labels_new, label_new);
659

660 661
    gtk_container_add (GTK_CONTAINER (row), label_new);
    gtk_widget_show_all (row);
662

663
    return row;
664 665
}

666
static GtkWidget *
667 668 669
create_arrow_row_for_label (NautilusBatchRenameDialog *dialog,
                            gboolean                   show_separator)
{
670 671
    GtkWidget *row;
    GtkWidget *icon;
672

673
    row = gtk_list_box_row_new ();
674

675
    g_object_set_data (G_OBJECT (row), "show-separator", GINT_TO_POINTER (show_separator));
676

677 678 679 680 681
    if (gtk_widget_get_direction (row) == GTK_TEXT_DIR_RTL)
        icon = gtk_label_new ("←");
    else
        icon = gtk_label_new ("→");

682 683
    gtk_label_set_xalign (GTK_LABEL (icon), 1.0);
    gtk_widget_set_hexpand (icon, FALSE);
684
    gtk_widget_set_margin_start (icon, ROW_MARGIN_START);
685

686
    dialog->listbox_icons = g_list_prepend (dialog->listbox_icons, icon);
687

688 689
    gtk_container_add (GTK_CONTAINER (row), icon);
    gtk_widget_show_all (row);
690

691
    return row;
692 693 694 695 696
}

static void
prepare_batch_rename (NautilusBatchRenameDialog *dialog)
{
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
    GdkCursor *cursor;
    GdkDisplay *display;

    /* wait for checking conflicts to finish, to be sure that
     * the rename can actually take place */
    if (dialog->checking_conflicts)
    {
        dialog->rename_clicked = TRUE;
        return;
    }

    if (!gtk_widget_is_sensitive (dialog->rename_button))
    {
        return;
    }

    display = gtk_widget_get_display (GTK_WIDGET (dialog->window));
    cursor = gdk_cursor_new_from_name (display, "progress");
    gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (dialog->window)),
                           cursor);
    g_object_unref (cursor);

    display = gtk_widget_get_display (GTK_WIDGET (dialog));
    cursor = gdk_cursor_new_from_name (display, "progress");
    gdk_window_set_cursor (gtk_widget_get_window (GTK_WIDGET (dialog)),
                           cursor);
    g_object_unref (cursor);

    gtk_widget_hide (GTK_WIDGET (dialog));
    begin_batch_rename (dialog, dialog->new_names);

    if (dialog->conflict_cancellable)
    {
        g_cancellable_cancel (dialog->conflict_cancellable);
    }

    gtk_widget_destroy (GTK_WIDGET (dialog));
734 735 736 737 738 739 740
}

static void
batch_rename_dialog_on_response (NautilusBatchRenameDialog *dialog,
                                 gint                       response_id,
                                 gpointer                   user_data)
{
741 742 743 744 745 746 747 748 749
    if (response_id == GTK_RESPONSE_OK)
    {
        prepare_batch_rename (dialog);
    }
    else
    {
        if (dialog->conflict_cancellable)
        {
            g_cancellable_cancel (dialog->conflict_cancellable);
750
        }
751 752 753

        gtk_widget_destroy (GTK_WIDGET (dialog));
    }
754 755 756 757 758
}

static void
fill_display_listbox (NautilusBatchRenameDialog *dialog)
{
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
    GtkWidget *row;
    GList *l1;
    GList *l2;
    NautilusFile *file;
    GString *new_name;
    gchar *name;

    dialog->original_name_listbox_rows = NULL;
    dialog->arrow_listbox_rows = NULL;
    dialog->result_listbox_rows = NULL;

    gtk_size_group_add_widget (dialog->size_group, dialog->result_listbox);
    gtk_size_group_add_widget (dialog->size_group, dialog->original_name_listbox);

    for (l1 = dialog->new_names, l2 = dialog->selection; l1 != NULL && l2 != NULL; l1 = l1->next, l2 = l2->next)
    {
        file = NAUTILUS_FILE (l2->data);
        new_name = l1->data;

        name = nautilus_file_get_name (file);
        row = create_original_name_row_for_label (dialog, name, TRUE);
        gtk_container_add (GTK_CONTAINER (dialog->original_name_listbox), row);
        dialog->original_name_listbox_rows = g_list_prepend (dialog->original_name_listbox_rows,
782 783
                                                             row);

784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
        row = create_arrow_row_for_label (dialog, TRUE);
        gtk_container_add (GTK_CONTAINER (dialog->arrow_listbox), row);
        dialog->arrow_listbox_rows = g_list_prepend (dialog->arrow_listbox_rows,
                                                     row);

        row = create_result_row_for_label (dialog, new_name->str, TRUE);
        gtk_container_add (GTK_CONTAINER (dialog->result_listbox), row);
        dialog->result_listbox_rows = g_list_prepend (dialog->result_listbox_rows,
                                                      row);

        g_free (name);
    }

    dialog->original_name_listbox_rows = g_list_reverse (dialog->original_name_listbox_rows);
    dialog->arrow_listbox_rows = g_list_reverse (dialog->arrow_listbox_rows);
    dialog->result_listbox_rows = g_list_reverse (dialog->result_listbox_rows);
    dialog->listbox_labels_old = g_list_reverse (dialog->listbox_labels_old);
    dialog->listbox_labels_new = g_list_reverse (dialog->listbox_labels_new);
    dialog->listbox_icons = g_list_reverse (dialog->listbox_icons);
803 804 805 806 807
}

static void
select_nth_conflict (NautilusBatchRenameDialog *dialog)
{
808 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 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
    GList *l;
    GString *conflict_file_name;
    GString *display_text;
    GString *new_name;
    gint nth_conflict_index;
    gint nth_conflict;
    gint name_occurences;
    GtkAdjustment *adjustment;
    GtkAllocation allocation;
    ConflictData *conflict_data;

    nth_conflict = dialog->selected_conflict;
    l = g_list_nth (dialog->duplicates, nth_conflict);
    conflict_data = l->data;

    /* the conflict that has to be selected */
    conflict_file_name = g_string_new (conflict_data->name);
    display_text = g_string_new ("");

    nth_conflict_index = conflict_data->index;

    l = g_list_nth (dialog->original_name_listbox_rows, nth_conflict_index);
    gtk_list_box_select_row (GTK_LIST_BOX (dialog->original_name_listbox),
                             l->data);

    l = g_list_nth (dialog->arrow_listbox_rows, nth_conflict_index);
    gtk_list_box_select_row (GTK_LIST_BOX (dialog->arrow_listbox),
                             l->data);

    l = g_list_nth (dialog->result_listbox_rows, nth_conflict_index);
    gtk_list_box_select_row (GTK_LIST_BOX (dialog->result_listbox),
                             l->data);

    /* scroll to the selected row */
    adjustment = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (dialog->scrolled_window));
    gtk_widget_get_allocation (GTK_WIDGET (l->data), &allocation);
    gtk_adjustment_set_value (adjustment, (allocation.height + 1) * nth_conflict_index);

    name_occurences = 0;
    for (l = dialog->new_names; l != NULL; l = l->next)
    {
        new_name = l->data;
        if (g_string_equal (new_name, conflict_file_name))
        {
            name_occurences++;
        }
    }
    if (name_occurences > 1)
    {
        g_string_append_printf (display_text,
858
                                _("“%s” would not be a unique new name."),
859 860 861 862 863
                                conflict_file_name->str);
    }
    else
    {
        g_string_append_printf (display_text,
864
                                _("“%s” would conflict with an existing file."),
865 866 867 868 869 870 871
                                conflict_file_name->str);
    }

    gtk_label_set_label (GTK_LABEL (dialog->conflict_label),
                         display_text->str);

    g_string_free (conflict_file_name, TRUE);
872 873 874 875 876
}

static void
select_next_conflict_down (NautilusBatchRenameDialog *dialog)
{
877
    dialog->selected_conflict++;
878

879 880 881 882
    if (dialog->selected_conflict == 1)
    {
        gtk_widget_set_sensitive (dialog->conflict_up, TRUE);
    }
883

884 885 886 887
    if (dialog->selected_conflict == dialog->conflicts_number - 1)
    {
        gtk_widget_set_sensitive (dialog->conflict_down, FALSE);
    }
888

889
    select_nth_conflict (dialog);
890 891 892 893 894
}

static void
select_next_conflict_up (NautilusBatchRenameDialog *dialog)
{
895
    dialog->selected_conflict--;
896

897 898 899 900
    if (dialog->selected_conflict == 0)
    {
        gtk_widget_set_sensitive (dialog->conflict_up, FALSE);
    }
901

902 903 904 905
    if (dialog->selected_conflict == dialog->conflicts_number - 2)
    {
        gtk_widget_set_sensitive (dialog->conflict_down, TRUE);
    }
906

907
    select_nth_conflict (dialog);
908 909 910 911 912
}

static void
update_conflict_row_background (NautilusBatchRenameDialog *dialog)
{
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
    GList *l1;
    GList *l2;
    GList *l3;
    GList *duplicates;
    gint index;
    GtkStyleContext *context;
    ConflictData *conflict_data;

    index = 0;

    duplicates = dialog->duplicates;

    for (l1 = dialog->original_name_listbox_rows,
         l2 = dialog->arrow_listbox_rows,
         l3 = dialog->result_listbox_rows;
         l1 != NULL && l2 != NULL && l3 != NULL;
         l1 = l1->next, l2 = l2->next, l3 = l3->next)
    {
        context = gtk_widget_get_style_context (GTK_WIDGET (l1->data));

        if (gtk_style_context_has_class (context, "conflict-row"))
        {
            gtk_style_context_remove_class (context, "conflict-row");
936

937 938
            context = gtk_widget_get_style_context (GTK_WIDGET (l2->data));
            gtk_style_context_remove_class (context, "conflict-row");
939

940 941 942
            context = gtk_widget_get_style_context (GTK_WIDGET (l3->data));
            gtk_style_context_remove_class (context, "conflict-row");
        }
943

944 945 946 947 948 949 950
        if (duplicates != NULL)
        {
            conflict_data = duplicates->data;
            if (conflict_data->index == index)
            {
                context = gtk_widget_get_style_context (GTK_WIDGET (l1->data));
                gtk_style_context_add_class (context, "conflict-row");
951

952 953
                context = gtk_widget_get_style_context (GTK_WIDGET (l2->data));
                gtk_style_context_add_class (context, "conflict-row");
954

955 956
                context = gtk_widget_get_style_context (GTK_WIDGET (l3->data));
                gtk_style_context_add_class (context, "conflict-row");
957

958 959
                duplicates = duplicates->next;
            }
960
        }
961 962
        index++;
    }
963 964 965 966 967
}

static void
update_listbox (NautilusBatchRenameDialog *dialog)
{
968 969 970 971 972 973
    GList *l1;
    GList *l2;
    NautilusFile *file;
    gchar *old_name;
    GtkLabel *label;
    GString *new_name;
974

975 976 977 978
    for (l1 = dialog->new_names, l2 = dialog->listbox_labels_new; l1 != NULL && l2 != NULL; l1 = l1->next, l2 = l2->next)
    {
        label = GTK_LABEL (l2->data);
        new_name = l1->data;
979

980 981
        gtk_label_set_label (label, new_name->str);
    }
982

983 984 985 986
    for (l1 = dialog->selection, l2 = dialog->listbox_labels_old; l1 != NULL && l2 != NULL; l1 = l1->next, l2 = l2->next)
    {
        label = GTK_LABEL (l2->data);
        file = NAUTILUS_FILE (l1->data);
987

988
        old_name = nautilus_file_get_name (file);
989

990 991 992 993 994 995 996 997 998
        if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_FORMAT)
        {
            gtk_label_set_label (label, old_name);
        }
        else
        {
            new_name = batch_rename_replace_label_text (old_name,
                                                        gtk_entry_get_text (GTK_ENTRY (dialog->find_entry)));
            gtk_label_set_markup (GTK_LABEL (label), new_name->str);
999

1000
            g_string_free (new_name, TRUE);
1001 1002
        }

1003 1004
        g_free (old_name);
    }
1005

1006
    update_rows_height (dialog);
1007

1008 1009 1010 1011
    /* check if there are name conflicts and display them if they exist */
    if (dialog->duplicates != NULL)
    {
        update_conflict_row_background (dialog);
1012

1013
        gtk_widget_set_sensitive (dialog->rename_button, FALSE);
1014

1015
        gtk_widget_show (dialog->conflict_box);
1016

1017 1018
        dialog->selected_conflict = 0;
        dialog->conflicts_number = g_list_length (dialog->duplicates);
1019

1020
        select_nth_conflict (dialog);
1021

1022
        gtk_widget_set_sensitive (dialog->conflict_up, FALSE);
1023

1024 1025 1026
        if (g_list_length (dialog->duplicates) == 1)
        {
            gtk_widget_set_sensitive (dialog->conflict_down, FALSE);
1027
        }
1028 1029 1030
        else
        {
            gtk_widget_set_sensitive (dialog->conflict_down, TRUE);
1031
        }
1032 1033 1034 1035
    }
    else
    {
        gtk_widget_hide (dialog->conflict_box);
1036

1037 1038 1039 1040 1041
        /* re-enable the rename button if there are no more name conflicts */
        if (dialog->duplicates == NULL && !gtk_widget_is_sensitive (dialog->rename_button))
        {
            update_conflict_row_background (dialog);
            gtk_widget_set_sensitive (dialog->rename_button, TRUE);
1042
        }
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    }

    /* if the rename button was clicked and there's no conflict, then start renaming */
    if (dialog->rename_clicked && dialog->duplicates == NULL)
    {
        prepare_batch_rename (dialog);
    }

    if (dialog->rename_clicked && dialog->duplicates != NULL)
    {
        dialog->rename_clicked = FALSE;
    }
1055 1056 1057 1058 1059 1060 1061
}

void
check_conflict_for_files (NautilusBatchRenameDialog *dialog,
                          NautilusDirectory         *directory,
                          GList                     *files)
{
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    gchar *current_directory;
    gchar *parent_uri;
    gchar *name;
    NautilusFile *file;
    GString *new_name;
    GString *file_name;
    GList *l1, *l2;
    GHashTable *directory_files_table;
    GHashTable *new_names_table;
    GHashTable *names_conflicts_table;
    gboolean exists;
    gboolean have_conflict;
    gboolean tag_present;
    gboolean same_parent_directory;
    ConflictData *conflict_data;

    current_directory = nautilus_directory_get_uri (directory);

    directory_files_table = g_hash_table_new_full (g_str_hash,
                                                   g_str_equal,
                                                   (GDestroyNotify) g_free,
                                                   NULL);
    new_names_table = g_hash_table_new_full (g_str_hash,
                                             g_str_equal,
                                             (GDestroyNotify) g_free,
                                             (GDestroyNotify) g_free);
    names_conflicts_table = g_hash_table_new_full (g_str_hash,
                                                   g_str_equal,
                                                   (GDestroyNotify) g_free,
                                                   (GDestroyNotify) g_free);

    /* names_conflicts_table is used for knowing which names from the list are not unique,
     * so that they can easily be reached when needed */
    for (l1 = dialog->new_names, l2 = dialog->selection;
         l1 != NULL && l2 != NULL;
         l1 = l1->next, l2 = l2->next)
    {
        new_name = l1->data;
        file = NAUTILUS_FILE (l2->data);
        parent_uri = nautilus_file_get_parent_uri (file);

        tag_present = g_hash_table_lookup (new_names_table, new_name->str) != NULL;
        same_parent_directory = g_strcmp0 (parent_uri, current_directory) == 0;

        if (same_parent_directory)
        {
            if (!tag_present)
            {
                g_hash_table_insert (new_names_table,
                                     g_strdup (new_name->str),
                                     nautilus_file_get_parent_uri (file));
            }
            else
            {
                g_hash_table_insert (names_conflicts_table,
                                     g_strdup (new_name->str),
                                     nautilus_file_get_parent_uri (file));
            }
        }

        g_free (parent_uri);
    }

    for (l1 = files; l1 != NULL; l1 = l1->next)
    {
        file = NAUTILUS_FILE (l1->data);
        g_hash_table_insert (directory_files_table,
                             nautilus_file_get_name (file),
                             GINT_TO_POINTER (TRUE));
    }

    for (l1 = dialog->selection, l2 = dialog->new_names; l1 != NULL && l2 != NULL; l1 = l1->next, l2 = l2->next)
    {
        file = NAUTILUS_FILE (l1->data);

        name = nautilus_file_get_name (file);
        file_name = g_string_new (name);
        g_free (name);

        parent_uri = nautilus_file_get_parent_uri (file);

        new_name = l2->data;

        have_conflict = FALSE;

        /* check for duplicate only if the parent of the current file is
         * the current directory and the name of the file has changed */
        if (g_strcmp0 (parent_uri, current_directory) == 0 &&
            !g_string_equal (new_name, file_name))
        {
            exists = GPOINTER_TO_INT (g_hash_table_lookup (directory_files_table, new_name->str));
1153

1154 1155 1156 1157 1158 1159 1160 1161
            if (exists == TRUE &&
                !file_name_conflicts_with_results (dialog->selection, dialog->new_names, new_name, parent_uri))
            {
                conflict_data = g_new (ConflictData, 1);
                conflict_data->name = g_strdup (new_name->str);
                conflict_data->index = g_list_index (dialog->selection, l1->data);
                dialog->duplicates = g_list_prepend (dialog->duplicates,
                                                     conflict_data);
1162

1163 1164
                have_conflict = TRUE;
            }
1165
        }
1166

1167 1168 1169 1170
        if (!have_conflict)
        {
            tag_present = g_hash_table_lookup (names_conflicts_table, new_name->str) != NULL;
            same_parent_directory = g_strcmp0 (nautilus_file_get_parent_uri (file), current_directory) == 0;
1171

1172 1173 1174 1175 1176 1177 1178
            if (tag_present && same_parent_directory)
            {
                conflict_data = g_new (ConflictData, 1);
                conflict_data->name = g_strdup (new_name->str);
                conflict_data->index = g_list_index (dialog->selection, l1->data);
                dialog->duplicates = g_list_prepend (dialog->duplicates,
                                                     conflict_data);
1179

1180 1181
                have_conflict = TRUE;
            }
1182 1183
        }

1184 1185 1186
        g_string_free (file_name, TRUE);
        g_free (parent_uri);
    }
1187

1188 1189 1190 1191
    g_free (current_directory);
    g_hash_table_destroy (directory_files_table);
    g_hash_table_destroy (new_names_table);
    g_hash_table_destroy (names_conflicts_table);
1192 1193
}

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
static gboolean
file_names_list_has_duplicates_finish (NautilusBatchRenameDialog  *self,
                                       GAsyncResult               *res,
                                       GError                    **error)
{
  g_return_val_if_fail (g_task_is_valid (res, self), FALSE);

  return g_task_propagate_boolean (G_TASK (res), error);
}

1204
static void
1205 1206 1207
on_file_names_list_has_duplicates (GObject      *object,
                                   GAsyncResult *res,
                                   gpointer      user_data)
1208
{
1209 1210 1211
    NautilusBatchRenameDialog *self;
    GError *error = NULL;
    gboolean success;
1212

1213 1214
    self = NAUTILUS_BATCH_RENAME_DIALOG (object);
    success = file_names_list_has_duplicates_finish (self, res, &error);
1215

1216
    if (!success)
1217 1218 1219
    {
        return;
    }
1220

1221 1222 1223
    self->duplicates = g_list_reverse (self->duplicates);
    self->checking_conflicts = FALSE;
    update_listbox (self);
1224 1225
}

1226 1227 1228 1229 1230 1231 1232 1233
typedef struct
{
    GList *directories;
    GMutex wait_ready_mutex;
    GCond wait_ready_condition;
    gboolean directory_conflicts_ready;
} CheckConflictsData;

1234
static void
1235 1236 1237
on_directory_conflicts_ready (NautilusDirectory *conflict_directory,
                              GList             *files,
                              gpointer           callback_data)
1238
{
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
    NautilusBatchRenameDialog *self;
    GTask *task;
    CheckConflictsData *task_data;
    GCancellable *cancellable;

    task = G_TASK (callback_data);
    task_data = g_task_get_task_data (task);
    cancellable = g_task_get_cancellable (task);
    self = NAUTILUS_BATCH_RENAME_DIALOG (g_task_get_source_object (task));
    if (!g_cancellable_is_cancelled (cancellable))
    {
        check_conflict_for_files (self, conflict_directory, files);
    }

    g_mutex_lock (&task_data->wait_ready_mutex);
    task_data->directory_conflicts_ready = TRUE;
    g_cond_signal (&task_data->wait_ready_condition);
    g_mutex_unlock (&task_data->wait_ready_mutex);
}

static void
1260 1261 1262 1263
file_names_list_has_duplicates_async_thread (GTask        *task,
                                             gpointer      object,
                                             gpointer      data,
                                             GCancellable *cancellable)
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
{
    NautilusBatchRenameDialog *self;
    CheckConflictsData *task_data;
    NautilusDirectory *conflict_directory;
    GList *directories;
    GList *l;
    const gchar *directory_conflict_uri = NULL;

    self = g_task_get_source_object (task);
    task_data = g_task_get_task_data (task);
1274
    self->duplicates = NULL;
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313

    g_mutex_init (&task_data->wait_ready_mutex);
    g_cond_init (&task_data->wait_ready_condition);

    directories = batch_rename_files_get_distinct_parents (self->selection);
    /* check if this is the last call of the callback */
    for (l = directories; l != NULL; l = l->next)
    {
        if (g_task_return_error_if_cancelled (task))
        {
            return;
        }

        g_mutex_lock (&task_data->wait_ready_mutex);
        task_data->directory_conflicts_ready = FALSE;

        directory_conflict_uri = l->data;
        conflict_directory = nautilus_directory_get_by_uri (directory_conflict_uri);

        nautilus_directory_call_when_ready (conflict_directory,
                                            NAUTILUS_FILE_ATTRIBUTE_INFO,
                                            TRUE,
                                            on_directory_conflicts_ready,
                                            task);

        /* We need to block this thread until the call_when_ready call is done,
         * if not the GTask would finalize. */
        while (!task_data->directory_conflicts_ready)
        {
            g_cond_wait (&task_data->wait_ready_condition,
                         &task_data->wait_ready_mutex);
        }

        g_mutex_unlock (&task_data->wait_ready_mutex);

        nautilus_directory_unref (conflict_directory);
    }

  g_task_return_boolean (task, TRUE);
1314 1315
}

1316 1317 1318 1319 1320 1321 1322 1323 1324 1325
static void
destroy_conflicts_task_data (gpointer data)
{
    CheckConflictsData *task_data = data;

    if (task_data->directories)
    {
        g_list_free (task_data->directories);
    }

1326 1327
    g_mutex_clear (&task_data->wait_ready_mutex);
    g_cond_clear (&task_data->wait_ready_condition);
1328 1329 1330 1331 1332 1333 1334
}

static void
file_names_list_has_duplicates_async (NautilusBatchRenameDialog *dialog,
                                      GAsyncReadyCallback        callback,
                                      gpointer                   user_data)
{
1335 1336 1337
    g_autoptr (GTask) task = NULL;
    CheckConflictsData *task_data;

1338 1339 1340
    if (dialog->checking_conflicts == TRUE)
    {
        g_cancellable_cancel (dialog->conflict_cancellable);
1341
        g_clear_object (&dialog->conflict_cancellable);
1342
    }
1343

1344
    dialog->conflict_cancellable = g_cancellable_new ();
1345

1346
    dialog->checking_conflicts = TRUE;
1347

1348 1349 1350 1351
    task = g_task_new (dialog, dialog->conflict_cancellable, callback, user_data);
    task_data = g_new0(CheckConflictsData, 1);
    g_task_set_task_data (task, task_data, destroy_conflicts_task_data);
    g_task_run_in_thread (task, file_names_list_has_duplicates_async_thread);
1352 1353
}

1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
static gboolean
have_unallowed_character (NautilusBatchRenameDialog *dialog)
{
    GList *names;
    GString *new_name;
    const gchar *entry_text;
    gboolean have_unallowed_character_slash;
    gboolean have_unallowed_character_dot;
    gboolean have_unallowed_character_dotdot;

    have_unallowed_character_slash = FALSE;
    have_unallowed_character_dot = FALSE;
    have_unallowed_character_dotdot = FALSE;
1367

1368
    if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_FORMAT)
1369
    {
1370
        entry_text = gtk_entry_get_text (GTK_ENTRY (dialog->name_entry));
1371 1372 1373
    }
    else
    {
1374
        entry_text = gtk_entry_get_text (GTK_ENTRY (dialog->replace_entry));
1375 1376
    }

1377
    if (strstr (entry_text, "/") != NULL)
1378
    {
1379
        have_unallowed_character_slash = TRUE;
1380 1381
    }

1382
    if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_FORMAT && g_strcmp0 (entry_text, ".") == 0)
1383
    {
1384
        have_unallowed_character_dot = TRUE;
1385
    }
1386
    else if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_REPLACE)
1387
    {
1388
        for (names = dialog->new_names; names != NULL; names = names->next)
1389
        {
1390 1391 1392 1393 1394 1395 1396
            new_name = names->data;

            if (g_strcmp0 (new_name->str, ".") == 0)
            {
                have_unallowed_character_dot = TRUE;
                break;
            }
1397 1398 1399
        }
    }

1400
    if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_FORMAT && g_strcmp0 (entry_text, "..") == 0)
1401
    {
1402
        have_unallowed_character_dotdot = TRUE;
1403
    }
1404
    else if (dialog->mode == NAUTILUS_BATCH_RENAME_DIALOG_REPLACE)
1405
    {
1406
        for (names = dialog->new_names; names != NULL; names = names->next)
1407
        {
1408 1409 1410 1411 1412 1413 1414
            new_name = names->data;

            if (g_strcmp0 (new_name->str, "..") == 0)
            {
                have_unallowed_character_dotdot = TRUE;
                break;
            }
1415 1416 1417
        }
    }

1418
    if (have_unallowed_character_slash)
1419
    {
1420 1421
        gtk_label_set_label (GTK_LABEL (dialog->conflict_label),
                             "\"/\" is an unallowed character");
1422
    }
1423 1424

    if (have_unallowed_character_dot)
1425
    {
1426 1427
        gtk_label_set_label (GTK_LABEL (dialog->conflict_label),
                             "\".\" is an unallowed file name");
1428 1429
    }

1430
    if (have_unallowed_character_dotdot)
1431 1432 1433 1434
    {
        gtk_label_set_label (GTK_LABEL (dialog->conflict_label),
                             "\"..\" is an unallowed file name");
    }
1435

1436 1437 1438 1439 1440
    if (have_unallowed_character_slash || have_unallowed_character_dot || have_unallowed_character_dotdot)
    {
        gtk_widget_set_sensitive (dialog->rename_button, FALSE);
        gtk_widget_set_sensitive (dialog->conflict_down, FALSE);
        gtk_widget_set_sensitive (dialog->conflict_up, FALSE);
1441

1442
        gtk_widget_show (dialog->conflict_box);
1443

1444 1445 1446 1447 1448
        return TRUE;
    }
    else
    {
        gtk_widget_hide (dialog->conflict_box);
1449

1450 1451
        return FALSE;
    }
1452 1453
}

1454 1455
static gboolean
numbering_tag_is_some_added (NautilusBatchRenameDialog *self)
1456
{
1457
    guint i;
1458 1459
    TagData *tag_data;

1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
    for (i = 0; i < G_N_ELEMENTS (numbering_tags_constants); i++)
    {
        g_autofree gchar *tag_text_representation = NULL;

        tag_text_representation = batch_rename_get_tag_text_representation (numbering_tags_constants[i]);
        tag_data = g_hash_table_lookup (self->tag_info_table, tag_text_representation);
        if (tag_data->set)
        {
          return TRUE;
        }
    }

    return FALSE;
}
1474

1475 1476 1477
static void
update_display_text (NautilusBatchRenameDialog *dialog)
{
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492
    if (dialog->conflict_cancellable != NULL)
    {
        g_cancellable_cancel (dialog->conflict_cancellable);
    }

    if(dialog->selection == NULL)
    {
        return;
    }

    if (dialog->duplicates != NULL)
    {
        g_list_free_full (dialog->duplicates, conflict_data_free);
        dialog->duplicates = NULL;
    }
1493

1494 1495 1496 1497 1498
    if (dialog->new_names != NULL)
    {
        g_list_free_full (dialog->new_names, string_free);
    }

1499
    if (!numbering_tag_is_some_added (dialog))
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
    {
        gtk_label_set_label (GTK_LABEL (dialog->numbering_label), "");
        gtk_widget_hide (dialog->numbering_order_button);
    }
    else
    {
        gtk_label_set_label (GTK_LABEL (dialog->numbering_label), _("Automatic Numbering Order"));
        gtk_widget_show (dialog->numbering_order_button);
    }

    dialog->new_names = batch_rename_dialog_get_new_names (dialog);

1512 1513 1514 1515 1516
    if (have_unallowed_character (dialog))
    {
        return;
    }

1517 1518 1519
    file_names_list_has_duplicates_async (dialog,
                                          on_file_names_list_has_duplicates,
                                          NULL);
1520 1521 1522 1523 1524
}

static void
batch_rename_dialog_mode_changed (NautilusBatchRenameDialog *dialog)
{
1525 1526 1527
    if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->format_mode_button)))
    {
        gtk_stack_set_visible_child_name (GTK_STACK (dialog->mode_stack), "format");
1528

1529
        dialog->mode = NAUTILUS_BATCH_RENAME_DIALOG_FORMAT;
1530

1531 1532 1533 1534 1535
        gtk_entry_grab_focus_without_selecting (GTK_ENTRY (dialog->name_entry));
    }
    else
    {
        gtk_stack_set_visible_child_name (GTK_STACK (dialog->mode_stack), "replace");
1536