nautilus-list-model.c 52.2 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
/* fm-list-model.h - a GtkTreeModel for file lists.
 *
 *  Copyright (C) 2001, 2002 Anders Carlsson
 *  Copyright (C) 2003, Soeren Sandmann
 *  Copyright (C) 2004, Novell, Inc.
 *
 *  The Gnome Library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  The Gnome Library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with the Gnome Library; see the file COPYING.LIB.  If not,
 *  see <http://www.gnu.org/licenses/>.
 *
 *  Authors: Anders Carlsson <andersca@gnu.org>, Soeren Sandmann (sandmann@daimi.au.dk), Dave Camp <dave@ximian.com>
 */
Anders Carlsson's avatar
Anders Carlsson committed
23 24

#include <config.h>
25 26

#include "nautilus-list-model.h"
Anders Carlsson's avatar
Anders Carlsson committed
27 28

#include <string.h>
29
#include <glib.h>
30
#include <glib/gi18n.h>
31
#include <gtk/gtk.h>
Cosimo Cecchi's avatar
Cosimo Cecchi committed
32
#include <cairo-gobject.h>
33

34
#include <eel/eel-graphic-effects.h>
35
#include "nautilus-dnd.h"
Anders Carlsson's avatar
Anders Carlsson committed
36

37 38 39 40 41
enum
{
    SUBDIRECTORY_UNLOADED,
    GET_ICON_SCALE,
    LAST_SIGNAL
42 43
};

44
static GQuark attribute_name_q,
45 46
              attribute_modification_date_q,
              attribute_date_modified_q;
47

48 49 50
/* msec delay after Loading... dummy row turns into (empty) */
#define LOADING_TO_EMPTY_DELAY 100

51 52
static guint list_model_signals[LAST_SIGNAL] = { 0 };

53
static int nautilus_list_model_file_entry_compare_func (gconstpointer a,
54 55
                                                        gconstpointer b,
                                                        gpointer      user_data);
56 57
static void nautilus_list_model_tree_model_init (GtkTreeModelIface *iface);
static void nautilus_list_model_sortable_init (GtkTreeSortableIface *iface);
Anders Carlsson's avatar
Anders Carlsson committed
58

59
typedef struct
60 61 62 63
{
    GSequence *files;
    GHashTable *directory_reverse_map;     /* map from directory to GSequenceIter's */
    GHashTable *top_reverse_map;           /* map from files in top dir to GSequenceIter's */
Anders Carlsson's avatar
Anders Carlsson committed
64

65
    int stamp;
Anders Carlsson's avatar
Anders Carlsson committed
66

67 68
    GQuark sort_attribute;
    GtkSortType order;
Anders Carlsson's avatar
Anders Carlsson committed
69

70
    gboolean sort_directories_first;
Dave Camp's avatar
Dave Camp committed
71

72 73 74
    GtkTreeView *drag_view;
    int drag_begin_x;
    int drag_begin_y;
75

76
    GPtrArray *columns;
77

78
    GList *highlight_files;
79
} NautilusListModelPrivate;
Anders Carlsson's avatar
Anders Carlsson committed
80

81 82 83 84 85
typedef struct
{
    NautilusListModel *model;

    GList *path_list;
Dave Camp's avatar
Dave Camp committed
86 87
} DragDataGetInfo;

Alexander Larsson's avatar
Alexander Larsson committed
88 89
typedef struct FileEntry FileEntry;

90 91 92 93 94 95 96 97 98
struct FileEntry
{
    NautilusFile *file;
    GHashTable *reverse_map;            /* map from files to GSequenceIter's */
    NautilusDirectory *subdirectory;
    FileEntry *parent;
    GSequence *files;
    GSequenceIter *ptr;
    guint loaded : 1;
Alexander Larsson's avatar
Alexander Larsson committed
99 100
};

101
G_DEFINE_TYPE_WITH_CODE (NautilusListModel, nautilus_list_model, G_TYPE_OBJECT,
102 103 104
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
                                                nautilus_list_model_tree_model_init)
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
105 106
                                                nautilus_list_model_sortable_init)
                         G_ADD_PRIVATE (NautilusListModel));
107 108 109 110 111

static const GtkTargetEntry drag_types [] =
{
    { NAUTILUS_ICON_DND_GNOME_ICON_LIST_TYPE, 0, NAUTILUS_ICON_DND_GNOME_ICON_LIST },
    { NAUTILUS_ICON_DND_URI_LIST_TYPE, 0, NAUTILUS_ICON_DND_URI_LIST },
Dave Camp's avatar
Dave Camp committed
112 113
};

Alexander Larsson's avatar
Alexander Larsson committed
114 115 116
static void
file_entry_free (FileEntry *file_entry)
{
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    nautilus_file_unref (file_entry->file);
    if (file_entry->reverse_map)
    {
        g_hash_table_destroy (file_entry->reverse_map);
        file_entry->reverse_map = NULL;
    }
    if (file_entry->subdirectory != NULL)
    {
        nautilus_directory_unref (file_entry->subdirectory);
    }
    if (file_entry->files != NULL)
    {
        g_sequence_free (file_entry->files);
    }
    g_free (file_entry);
Alexander Larsson's avatar
Alexander Larsson committed
132 133
}

134
static GtkTreeModelFlags
135
nautilus_list_model_get_flags (GtkTreeModel *tree_model)
Anders Carlsson's avatar
Anders Carlsson committed
136
{
137
    return GTK_TREE_MODEL_ITERS_PERSIST;
Anders Carlsson's avatar
Anders Carlsson committed
138 139 140
}

static int
141
nautilus_list_model_get_n_columns (GtkTreeModel *tree_model)
Anders Carlsson's avatar
Anders Carlsson committed
142
{
143 144 145 146 147
    NautilusListModelPrivate *priv;

    priv = nautilus_list_model_get_instance_private (NAUTILUS_LIST_MODEL (tree_model));

    return NAUTILUS_LIST_MODEL_NUM_COLUMNS + priv->columns->len;
Anders Carlsson's avatar
Anders Carlsson committed
148 149 150
}

static GType
151 152 153
nautilus_list_model_get_column_type (GtkTreeModel *tree_model,
                                     int           index)
{
154 155 156 157 158 159
    NautilusListModel *model;
    NautilusListModelPrivate *priv;

    model = NAUTILUS_LIST_MODEL (tree_model);
    priv = nautilus_list_model_get_instance_private (model);

160 161 162
    switch (index)
    {
        case NAUTILUS_LIST_MODEL_FILE_COLUMN:
163 164 165
        {
            return NAUTILUS_TYPE_FILE;
        }
166 167

        case NAUTILUS_LIST_MODEL_SUBDIRECTORY_COLUMN:
168 169 170
        {
            return NAUTILUS_TYPE_DIRECTORY;
        }
171 172 173 174 175

        case NAUTILUS_LIST_MODEL_SMALL_ICON_COLUMN:
        case NAUTILUS_LIST_MODEL_STANDARD_ICON_COLUMN:
        case NAUTILUS_LIST_MODEL_LARGE_ICON_COLUMN:
        case NAUTILUS_LIST_MODEL_LARGER_ICON_COLUMN:
176 177 178
        {
            return CAIRO_GOBJECT_TYPE_SURFACE;
        }
179 180

        case NAUTILUS_LIST_MODEL_FILE_NAME_IS_EDITABLE_COLUMN:
181 182 183
        {
            return G_TYPE_BOOLEAN;
        }
184 185

        default:
186
            if (index < NAUTILUS_LIST_MODEL_NUM_COLUMNS + priv->columns->len)
187 188 189 190 191 192 193 194
            {
                return G_TYPE_STRING;
            }
            else
            {
                return G_TYPE_INVALID;
            }
    }
Anders Carlsson's avatar
Anders Carlsson committed
195 196
}

197
static void
198 199 200
nautilus_list_model_ptr_to_iter (NautilusListModel *model,
                                 GSequenceIter     *ptr,
                                 GtkTreeIter       *iter)
201
{
202 203 204 205
    NautilusListModelPrivate *priv;

    priv = nautilus_list_model_get_instance_private (model);

206
    g_assert (!g_sequence_iter_is_end (ptr));
207

208 209
    if (iter != NULL)
    {
210
        iter->stamp = priv->stamp;
211 212
        iter->user_data = ptr;
    }
213 214
}

Anders Carlsson's avatar
Anders Carlsson committed
215
static gboolean
216 217 218 219 220
nautilus_list_model_get_iter (GtkTreeModel *tree_model,
                              GtkTreeIter  *iter,
                              GtkTreePath  *path)
{
    NautilusListModel *model;
221
    NautilusListModelPrivate *priv;
222 223 224 225 226
    GSequence *files;
    GSequenceIter *ptr;
    FileEntry *file_entry;
    int i, d;

227
    model = NAUTILUS_LIST_MODEL (tree_model);
228
    priv = nautilus_list_model_get_instance_private (model);
229 230
    ptr = NULL;

231
    files = priv->files;
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    for (d = 0; d < gtk_tree_path_get_depth (path); d++)
    {
        i = gtk_tree_path_get_indices (path)[d];

        if (files == NULL || i >= g_sequence_get_length (files))
        {
            return FALSE;
        }

        ptr = g_sequence_get_iter_at_pos (files, i);
        file_entry = g_sequence_get (ptr);
        files = file_entry->files;
    }

    nautilus_list_model_ptr_to_iter (model, ptr, iter);

    return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
249 250 251
}

static GtkTreePath *
252 253 254 255 256
nautilus_list_model_get_path (GtkTreeModel *tree_model,
                              GtkTreeIter  *iter)
{
    GtkTreePath *path;
    NautilusListModel *model;
257
    NautilusListModelPrivate *priv;
258 259 260
    GSequenceIter *ptr;
    FileEntry *file_entry;

261
    model = NAUTILUS_LIST_MODEL (tree_model);
262
    priv = nautilus_list_model_get_instance_private (model);
263

264
    g_return_val_if_fail (iter->stamp == priv->stamp, NULL);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

    if (g_sequence_iter_is_end (iter->user_data))
    {
        /* FIXME is this right? */
        return NULL;
    }

    path = gtk_tree_path_new ();
    ptr = iter->user_data;
    while (ptr != NULL)
    {
        gtk_tree_path_prepend_index (path, g_sequence_iter_get_position (ptr));
        file_entry = g_sequence_get (ptr);
        if (file_entry->parent != NULL)
        {
            ptr = file_entry->parent->ptr;
        }
        else
        {
            ptr = NULL;
        }
    }

    return path;
Anders Carlsson's avatar
Anders Carlsson committed
289 290
}

Cosimo Cecchi's avatar
Cosimo Cecchi committed
291 292 293
static gint
nautilus_list_model_get_icon_scale (NautilusListModel *model)
{
294
    gint retval = -1;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
295

296 297
    g_signal_emit (model, list_model_signals[GET_ICON_SCALE], 0,
                   &retval);
Cosimo Cecchi's avatar
Cosimo Cecchi committed
298

299 300
    if (retval == -1)
    {
301
        retval = gdk_monitor_get_scale_factor (gdk_display_get_monitor (gdk_display_get_default (), 0));
302
    }
Cosimo Cecchi's avatar
Cosimo Cecchi committed
303

304
    return retval;
Cosimo Cecchi's avatar
Cosimo Cecchi committed
305 306
}

307 308 309
guint
nautilus_list_model_get_icon_size_for_zoom_level (NautilusListZoomLevel zoom_level)
{
310 311 312
    switch (zoom_level)
    {
        case NAUTILUS_LIST_ZOOM_LEVEL_SMALL:
313 314 315
        {
            return NAUTILUS_LIST_ICON_SIZE_SMALL;
        }
316 317

        case NAUTILUS_LIST_ZOOM_LEVEL_STANDARD:
318 319 320
        {
            return NAUTILUS_LIST_ICON_SIZE_STANDARD;
        }
321 322

        case NAUTILUS_LIST_ZOOM_LEVEL_LARGE:
323 324 325
        {
            return NAUTILUS_LIST_ICON_SIZE_LARGE;
        }
326 327 328 329 330

        case NAUTILUS_LIST_ZOOM_LEVEL_LARGER:
            return NAUTILUS_LIST_ICON_SIZE_LARGER;
    }
    g_return_val_if_reached (NAUTILUS_LIST_ICON_SIZE_STANDARD);
331 332
}

Anders Carlsson's avatar
Anders Carlsson committed
333
static void
334 335 336 337 338 339
nautilus_list_model_get_value (GtkTreeModel *tree_model,
                               GtkTreeIter  *iter,
                               int           column,
                               GValue       *value)
{
    NautilusListModel *model;
340
    NautilusListModelPrivate *priv;
341 342 343 344 345 346 347 348 349
    FileEntry *file_entry;
    NautilusFile *file;
    char *str;
    GdkPixbuf *icon, *rendered_icon;
    int icon_size, icon_scale;
    NautilusListZoomLevel zoom_level;
    NautilusFileIconFlags flags;
    cairo_surface_t *surface;

350
    model = NAUTILUS_LIST_MODEL (tree_model);
351
    priv = nautilus_list_model_get_instance_private (model);
352

353
    g_return_if_fail (priv->stamp == iter->stamp);
354 355 356 357 358 359 360 361
    g_return_if_fail (!g_sequence_iter_is_end (iter->user_data));

    file_entry = g_sequence_get (iter->user_data);
    file = file_entry->file;

    switch (column)
    {
        case NAUTILUS_LIST_MODEL_FILE_COLUMN:
362 363
        {
            g_value_init (value, NAUTILUS_TYPE_FILE);
364

365 366 367
            g_value_set_object (value, file);
        }
        break;
368 369

        case NAUTILUS_LIST_MODEL_SUBDIRECTORY_COLUMN:
370 371
        {
            g_value_init (value, NAUTILUS_TYPE_DIRECTORY);
372

373 374 375
            g_value_set_object (value, file_entry->subdirectory);
        }
        break;
376 377 378 379 380

        case NAUTILUS_LIST_MODEL_SMALL_ICON_COLUMN:
        case NAUTILUS_LIST_MODEL_STANDARD_ICON_COLUMN:
        case NAUTILUS_LIST_MODEL_LARGE_ICON_COLUMN:
        case NAUTILUS_LIST_MODEL_LARGER_ICON_COLUMN:
381 382 383 384
        {
            g_value_init (value, CAIRO_GOBJECT_TYPE_SURFACE);

            if (file != NULL)
385
            {
386 387 388
                zoom_level = nautilus_list_model_get_zoom_level_from_column_id (column);
                icon_size = nautilus_list_model_get_icon_size_for_zoom_level (zoom_level);
                icon_scale = nautilus_list_model_get_icon_scale (model);
389

390 391 392 393
                flags = NAUTILUS_FILE_ICON_FLAGS_USE_THUMBNAILS |
                        NAUTILUS_FILE_ICON_FLAGS_FORCE_THUMBNAIL_SIZE |
                        NAUTILUS_FILE_ICON_FLAGS_USE_EMBLEMS |
                        NAUTILUS_FILE_ICON_FLAGS_USE_ONE_EMBLEM;
394

395
                if (priv->drag_view != NULL)
396 397
                {
                    GtkTreePath *path_a, *path_b;
398

399
                    gtk_tree_view_get_drag_dest_row (priv->drag_view,
400 401 402
                                                     &path_a,
                                                     NULL);
                    if (path_a != NULL)
403
                    {
404
                        path_b = gtk_tree_model_get_path (tree_model, iter);
405

406
                        if (gtk_tree_path_compare (path_a, path_b) == 0)
407
                        {
408
                            flags |= NAUTILUS_FILE_ICON_FLAGS_FOR_DRAG_ACCEPT;
409
                        }
410 411 412

                        gtk_tree_path_free (path_a);
                        gtk_tree_path_free (path_b);
413
                    }
414
                }
415

416
                icon = nautilus_file_get_icon_pixbuf (file, icon_size, TRUE, icon_scale, flags);
417

418 419
                if (priv->highlight_files != NULL &&
                    g_list_find_custom (priv->highlight_files,
420 421 422
                                        file, (GCompareFunc) nautilus_file_compare_location))
                {
                    rendered_icon = eel_create_spotlight_pixbuf (icon);
423

424 425 426 427
                    if (rendered_icon != NULL)
                    {
                        g_object_unref (icon);
                        icon = rendered_icon;
428 429
                    }
                }
430 431 432 433

                surface = gdk_cairo_surface_create_from_pixbuf (icon, icon_scale, NULL);
                g_value_take_boxed (value, surface);
                g_object_unref (icon);
434
            }
435 436
        }
        break;
437 438

        case NAUTILUS_LIST_MODEL_FILE_NAME_IS_EDITABLE_COLUMN:
439 440
        {
            g_value_init (value, G_TYPE_BOOLEAN);
441

442 443 444
            g_value_set_boolean (value, file != NULL && nautilus_file_can_rename (file));
        }
        break;
445 446

        default:
447
            if (column >= NAUTILUS_LIST_MODEL_NUM_COLUMNS || column < NAUTILUS_LIST_MODEL_NUM_COLUMNS + priv->columns->len)
448 449 450
            {
                NautilusColumn *nautilus_column;
                GQuark attribute;
451
                nautilus_column = priv->columns->pdata[column - NAUTILUS_LIST_MODEL_NUM_COLUMNS];
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479

                g_value_init (value, G_TYPE_STRING);
                g_object_get (nautilus_column,
                              "attribute_q", &attribute,
                              NULL);
                if (file != NULL)
                {
                    str = nautilus_file_get_string_attribute_with_default_q (file,
                                                                             attribute);
                    g_value_take_string (value, str);
                }
                else if (attribute == attribute_name_q)
                {
                    if (file_entry->parent->loaded)
                    {
                        g_value_set_string (value, _("(Empty)"));
                    }
                    else
                    {
                        g_value_set_string (value, _("Loading…"));
                    }
                }
            }
            else
            {
                g_assert_not_reached ();
            }
    }
Anders Carlsson's avatar
Anders Carlsson committed
480 481 482
}

static gboolean
483 484
nautilus_list_model_iter_next (GtkTreeModel *tree_model,
                               GtkTreeIter  *iter)
Anders Carlsson's avatar
Anders Carlsson committed
485
{
486
    NautilusListModel *model;
487
    NautilusListModelPrivate *priv;
Anders Carlsson's avatar
Anders Carlsson committed
488

489
    model = NAUTILUS_LIST_MODEL (tree_model);
490
    priv = nautilus_list_model_get_instance_private (model);
Anders Carlsson's avatar
Anders Carlsson committed
491

492
    g_return_val_if_fail (priv->stamp == iter->stamp, FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
493

494
    iter->user_data = g_sequence_iter_next (iter->user_data);
Anders Carlsson's avatar
Anders Carlsson committed
495

496
    return !g_sequence_iter_is_end (iter->user_data);
Anders Carlsson's avatar
Anders Carlsson committed
497 498 499
}

static gboolean
500 501 502
nautilus_list_model_iter_children (GtkTreeModel *tree_model,
                                   GtkTreeIter  *iter,
                                   GtkTreeIter  *parent)
Anders Carlsson's avatar
Anders Carlsson committed
503
{
504
    NautilusListModel *model;
505
    NautilusListModelPrivate *priv;
506 507 508
    GSequence *files;
    FileEntry *file_entry;

509
    model = NAUTILUS_LIST_MODEL (tree_model);
510
    priv = nautilus_list_model_get_instance_private (model);
511 512 513

    if (parent == NULL)
    {
514
        files = priv->files;
515 516 517 518 519 520 521 522 523 524 525 526
    }
    else
    {
        file_entry = g_sequence_get (parent->user_data);
        files = file_entry->files;
    }

    if (files == NULL || g_sequence_get_length (files) == 0)
    {
        return FALSE;
    }

527
    iter->stamp = priv->stamp;
528 529 530
    iter->user_data = g_sequence_get_begin_iter (files);

    return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
531 532 533
}

static gboolean
534 535
nautilus_list_model_iter_has_child (GtkTreeModel *tree_model,
                                    GtkTreeIter  *iter)
Anders Carlsson's avatar
Anders Carlsson committed
536
{
537
    FileEntry *file_entry;
538

539 540 541 542
    if (iter == NULL)
    {
        return !nautilus_list_model_is_empty (NAUTILUS_LIST_MODEL (tree_model));
    }
543

544
    file_entry = g_sequence_get (iter->user_data);
545

546
    return (file_entry->files != NULL && g_sequence_get_length (file_entry->files) > 0);
Anders Carlsson's avatar
Anders Carlsson committed
547 548 549
}

static int
550 551
nautilus_list_model_iter_n_children (GtkTreeModel *tree_model,
                                     GtkTreeIter  *iter)
Anders Carlsson's avatar
Anders Carlsson committed
552
{
553
    NautilusListModel *model;
554
    NautilusListModelPrivate *priv;
555 556 557
    GSequence *files;
    FileEntry *file_entry;

558
    model = NAUTILUS_LIST_MODEL (tree_model);
559
    priv = nautilus_list_model_get_instance_private (model);
560 561 562

    if (iter == NULL)
    {
563
        files = priv->files;
564 565 566 567 568 569 570 571
    }
    else
    {
        file_entry = g_sequence_get (iter->user_data);
        files = file_entry->files;
    }

    return g_sequence_get_length (files);
Anders Carlsson's avatar
Anders Carlsson committed
572 573 574
}

static gboolean
575 576 577 578
nautilus_list_model_iter_nth_child (GtkTreeModel *tree_model,
                                    GtkTreeIter  *iter,
                                    GtkTreeIter  *parent,
                                    int           n)
Anders Carlsson's avatar
Anders Carlsson committed
579
{
580
    NautilusListModel *model;
581
    NautilusListModelPrivate *priv;
582 583 584 585
    GSequenceIter *child;
    GSequence *files;
    FileEntry *file_entry;

586
    model = NAUTILUS_LIST_MODEL (tree_model);
587
    priv = nautilus_list_model_get_instance_private (model);
588 589 590 591 592 593 594 595

    if (parent != NULL)
    {
        file_entry = g_sequence_get (parent->user_data);
        files = file_entry->files;
    }
    else
    {
596
        files = priv->files;
597 598 599 600 601 602 603 604 605
    }

    child = g_sequence_get_iter_at_pos (files, n);

    if (g_sequence_iter_is_end (child))
    {
        return FALSE;
    }

606
    iter->stamp = priv->stamp;
607 608 609
    iter->user_data = child;

    return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
610 611 612
}

static gboolean
613 614 615
nautilus_list_model_iter_parent (GtkTreeModel *tree_model,
                                 GtkTreeIter  *iter,
                                 GtkTreeIter  *child)
Anders Carlsson's avatar
Anders Carlsson committed
616
{
617
    NautilusListModel *model;
618
    NautilusListModelPrivate *priv;
619
    FileEntry *file_entry;
620

621
    model = NAUTILUS_LIST_MODEL (tree_model);
622
    priv = nautilus_list_model_get_instance_private (model);
623

624
    file_entry = g_sequence_get (child->user_data);
625

626 627 628 629
    if (file_entry->parent == NULL)
    {
        return FALSE;
    }
Alexander Larsson's avatar
Alexander Larsson committed
630

631
    iter->stamp = priv->stamp;
632
    iter->user_data = file_entry->parent->ptr;
633

634
    return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
635 636
}

637
static GSequenceIter *
638 639 640 641
lookup_file (NautilusListModel *model,
             NautilusFile      *file,
             NautilusDirectory *directory)
{
642
    NautilusListModelPrivate *priv;
643 644 645
    FileEntry *file_entry;
    GSequenceIter *ptr, *parent_ptr;

646 647
    priv = nautilus_list_model_get_instance_private (model);

648 649 650
    parent_ptr = NULL;
    if (directory)
    {
651
        parent_ptr = g_hash_table_lookup (priv->directory_reverse_map,
652 653 654 655 656 657 658 659 660 661
                                          directory);
    }

    if (parent_ptr)
    {
        file_entry = g_sequence_get (parent_ptr);
        ptr = g_hash_table_lookup (file_entry->reverse_map, file);
    }
    else
    {
662
        ptr = g_hash_table_lookup (priv->top_reverse_map, file);
663 664 665 666 667 668 669 670
    }

    if (ptr)
    {
        g_assert (((FileEntry *) g_sequence_get (ptr))->file == file);
    }

    return ptr;
671 672 673
}


674 675 676 677 678
struct GetIters
{
    NautilusListModel *model;
    NautilusFile *file;
    GList *iters;
679 680 681 682
};

static void
dir_to_iters (struct GetIters *data,
683 684 685 686 687 688 689 690 691 692 693 694
              GHashTable      *reverse_map)
{
    GSequenceIter *ptr;

    ptr = g_hash_table_lookup (reverse_map, data->file);
    if (ptr)
    {
        GtkTreeIter *iter;
        iter = g_new0 (GtkTreeIter, 1);
        nautilus_list_model_ptr_to_iter (data->model, ptr, iter);
        data->iters = g_list_prepend (data->iters, iter);
    }
695 696 697
}

static void
698 699 700
file_to_iter_cb (gpointer key,
                 gpointer value,
                 gpointer user_data)
701
{
702 703
    struct GetIters *data;
    FileEntry *dir_file_entry;
704

705 706 707
    data = user_data;
    dir_file_entry = g_sequence_get ((GSequenceIter *) value);
    dir_to_iters (data, dir_file_entry->reverse_map);
708 709 710
}

GList *
711 712
nautilus_list_model_get_all_iters_for_file (NautilusListModel *model,
                                            NautilusFile      *file)
713
{
714
    struct GetIters data;
715
    NautilusListModelPrivate *priv;
716 717 718
    data.file = file;
    data.model = model;
    data.iters = NULL;
719

720 721 722 723
    priv = nautilus_list_model_get_instance_private (model);

    dir_to_iters (&data, priv->top_reverse_map);
    g_hash_table_foreach (priv->directory_reverse_map,
724
                          file_to_iter_cb, &data);
725

726
    return g_list_reverse (data.iters);
727 728 729
}

gboolean
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
nautilus_list_model_get_first_iter_for_file (NautilusListModel *model,
                                             NautilusFile      *file,
                                             GtkTreeIter       *iter)
{
    GList *list;
    gboolean res;

    res = FALSE;

    list = nautilus_list_model_get_all_iters_for_file (model, file);
    if (list != NULL)
    {
        res = TRUE;
        *iter = *(GtkTreeIter *) list->data;
    }
    g_list_free_full (list, g_free);

    return res;
748 749
}

Anders Carlsson's avatar
Anders Carlsson committed
750

751
gboolean
752 753 754 755
nautilus_list_model_get_tree_iter_from_file (NautilusListModel *model,
                                             NautilusFile      *file,
                                             NautilusDirectory *directory,
                                             GtkTreeIter       *iter)
756
{
757 758 759 760 761 762 763
    GSequenceIter *ptr;

    ptr = lookup_file (model, file, directory);
    if (!ptr)
    {
        return FALSE;
    }
Anders Carlsson's avatar
Anders Carlsson committed
764

765
    nautilus_list_model_ptr_to_iter (model, ptr, iter);
Anders Carlsson's avatar
Anders Carlsson committed
766

767
    return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
768 769
}

Alexander Larsson's avatar
Alexander Larsson committed
770
static int
771
nautilus_list_model_file_entry_compare_func (gconstpointer a,
772 773 774 775 776 777
                                             gconstpointer b,
                                             gpointer      user_data)
{
    FileEntry *file_entry1;
    FileEntry *file_entry2;
    NautilusListModel *model;
778
    NautilusListModelPrivate *priv;
779 780
    int result;

781
    model = NAUTILUS_LIST_MODEL (user_data);
782
    priv = nautilus_list_model_get_instance_private (model);
783 784 785 786 787 788 789

    file_entry1 = (FileEntry *) a;
    file_entry2 = (FileEntry *) b;

    if (file_entry1->file != NULL && file_entry2->file != NULL)
    {
        result = nautilus_file_compare_for_sort_by_attribute_q (file_entry1->file, file_entry2->file,
790 791 792
                                                                priv->sort_attribute,
                                                                priv->sort_directories_first,
                                                                (priv->order == GTK_SORT_DESCENDING));
793 794 795 796 797 798 799 800 801 802 803
    }
    else if (file_entry1->file == NULL)
    {
        return -1;
    }
    else
    {
        return 1;
    }

    return result;
Alexander Larsson's avatar
Alexander Larsson committed
804 805
}

806
int
807
nautilus_list_model_compare_func (NautilusListModel *model,
808 809
                                  NautilusFile      *file1,
                                  NautilusFile      *file2)
Anders Carlsson's avatar
Anders Carlsson committed
810
{
811
    NautilusListModelPrivate *priv;
812
    int result;
813

814
    priv = nautilus_list_model_get_instance_private (model);
815
    result = nautilus_file_compare_for_sort_by_attribute_q (file1, file2,
816 817 818
                                                            priv->sort_attribute,
                                                            priv->sort_directories_first,
                                                            (priv->order == GTK_SORT_DESCENDING));
Anders Carlsson's avatar
Anders Carlsson committed
819

820
    return result;
Anders Carlsson's avatar
Anders Carlsson committed
821 822 823
}

static void
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 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
nautilus_list_model_sort_file_entries (NautilusListModel *model,
                                       GSequence         *files,
                                       GtkTreePath       *path)
{
    GSequenceIter **old_order;
    GtkTreeIter iter;
    int *new_order;
    int length;
    int i;
    FileEntry *file_entry;
    gboolean has_iter;

    length = g_sequence_get_length (files);

    if (length <= 1)
    {
        return;
    }

    /* generate old order of GSequenceIter's */
    old_order = g_new (GSequenceIter *, length);
    for (i = 0; i < length; ++i)
    {
        GSequenceIter *ptr = g_sequence_get_iter_at_pos (files, i);

        file_entry = g_sequence_get (ptr);
        if (file_entry->files != NULL)
        {
            gtk_tree_path_append_index (path, i);
            nautilus_list_model_sort_file_entries (model, file_entry->files, path);
            gtk_tree_path_up (path);
        }

        old_order[i] = ptr;
    }

    /* sort */
    g_sequence_sort (files, nautilus_list_model_file_entry_compare_func, model);

    /* generate new order */
    new_order = g_new (int, length);
    /* Note: new_order[newpos] = oldpos */
    for (i = 0; i < length; ++i)
    {
        new_order[g_sequence_iter_get_position (old_order[i])] = i;
    }

    /* Let the world know about our new order */

    g_assert (new_order != NULL);

    has_iter = FALSE;
    if (gtk_tree_path_get_depth (path) != 0)
    {
        gboolean get_iter_result;
        has_iter = TRUE;
        get_iter_result = gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path);
        g_assert (get_iter_result);
    }

    gtk_tree_model_rows_reordered (GTK_TREE_MODEL (model),
                                   path, has_iter ? &iter : NULL, new_order);

    g_free (old_order);
    g_free (new_order);
Anders Carlsson's avatar
Anders Carlsson committed
889 890
}

Alexander Larsson's avatar
Alexander Larsson committed
891
static void
892
nautilus_list_model_sort (NautilusListModel *model)
Alexander Larsson's avatar
Alexander Larsson committed
893
{
894
    GtkTreePath *path;
895
    NautilusListModelPrivate *priv;
Alexander Larsson's avatar
Alexander Larsson committed
896

897
    path = gtk_tree_path_new ();
898
    priv = nautilus_list_model_get_instance_private (model);
Alexander Larsson's avatar
Alexander Larsson committed
899

900
    nautilus_list_model_sort_file_entries (model, priv->files, path);
Alexander Larsson's avatar
Alexander Larsson committed
901

902
    gtk_tree_path_free (path);
Alexander Larsson's avatar
Alexander Larsson committed
903 904
}

Anders Carlsson's avatar
Anders Carlsson committed
905
static gboolean
906
nautilus_list_model_get_sort_column_id (GtkTreeSortable *sortable,
907 908 909 910
                                        gint            *sort_column_id,
                                        GtkSortType     *order)
{
    NautilusListModel *model;
911
    NautilusListModelPrivate *priv;
912 913
    int id;

914
    model = NAUTILUS_LIST_MODEL (sortable);
915 916
    priv = nautilus_list_model_get_instance_private (model);
    id = nautilus_list_model_get_sort_column_id_from_attribute (model, priv->sort_attribute);
917 918 919 920 921 922 923 924 925 926 927 928 929

    if (id == -1)
    {
        return FALSE;
    }

    if (sort_column_id != NULL)
    {
        *sort_column_id = id;
    }

    if (order != NULL)
    {
930
        *order = priv->order;
931 932 933
    }

    return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
934 935 936
}

static void
937 938 939
nautilus_list_model_set_sort_column_id (GtkTreeSortable *sortable,
                                        gint             sort_column_id,
                                        GtkSortType      order)
Anders Carlsson's avatar
Anders Carlsson committed
940
{
941
    NautilusListModel *model;
942
    NautilusListModelPrivate *priv;
Anders Carlsson's avatar
Anders Carlsson committed
943

944
    model = NAUTILUS_LIST_MODEL (sortable);
945
    priv = nautilus_list_model_get_instance_private (model);
Anders Carlsson's avatar
Anders Carlsson committed
946

947
    priv->sort_attribute = nautilus_list_model_get_attribute_from_sort_column_id (model, sort_column_id);
Anders Carlsson's avatar
Anders Carlsson committed
948

949
    priv->order = order;
Anders Carlsson's avatar
Anders Carlsson committed
950

951 952
    nautilus_list_model_sort (model);
    gtk_tree_sortable_sort_column_changed (sortable);
Anders Carlsson's avatar
Anders Carlsson committed
953 954 955
}

static gboolean
956
nautilus_list_model_has_default_sort_func (GtkTreeSortable *sortable)
Anders Carlsson's avatar
Anders Carlsson committed
957
{
958
    return FALSE;
Anders Carlsson's avatar
Anders Carlsson committed
959 960
}

961
static void
962 963 964
add_dummy_row (NautilusListModel *model,
               FileEntry         *parent_entry)
{
965
    NautilusListModelPrivate *priv;
966 967 968 969
    FileEntry *dummy_file_entry;
    GtkTreeIter iter;
    GtkTreePath *path;

970
    priv = nautilus_list_model_get_instance_private (model);
971 972 973 974
    dummy_file_entry = g_new0 (FileEntry, 1);
    dummy_file_entry->parent = parent_entry;
    dummy_file_entry->ptr = g_sequence_insert_sorted (parent_entry->files, dummy_file_entry,
                                                      nautilus_list_model_file_entry_compare_func, model);
975
    iter.stamp = priv->stamp;
976 977 978 979 980
    iter.user_data = dummy_file_entry->ptr;

    path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
    gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
    gtk_tree_path_free (path);
981 982
}

Alexander Larsson's avatar
Alexander Larsson committed
983
gboolean
984 985 986 987
nautilus_list_model_add_file (NautilusListModel *model,
                              NautilusFile      *file,
                              NautilusDirectory *directory)
{
988
    NautilusListModelPrivate *priv;
989 990 991 992 993 994 995 996
    GtkTreeIter iter;
    GtkTreePath *path;
    FileEntry *file_entry;
    GSequenceIter *ptr, *parent_ptr;
    GSequence *files;
    gboolean replace_dummy;
    GHashTable *parent_hash;

997 998 999
    priv = nautilus_list_model_get_instance_private (model);

    parent_ptr = g_hash_table_lookup (priv->directory_reverse_map,
1000 1001 1002 1003 1004 1005 1006 1007 1008
                                      directory);
    if (parent_ptr)
    {
        file_entry = g_sequence_get (parent_ptr);
        ptr = g_hash_table_lookup (file_entry->reverse_map, file);
    }
    else
    {
        file_entry = NULL;
1009
        ptr = g_hash_table_lookup (priv->top_reverse_map, file);
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
    }

    if (ptr != NULL)
    {
        g_warning ("file already in tree (parent_ptr: %p)!!!\n", parent_ptr);
        return FALSE;
    }

    file_entry = g_new0 (FileEntry, 1);
    file_entry->file = nautilus_file_ref (file);
    file_entry->parent = NULL;
    file_entry->subdirectory = NULL;
    file_entry->files = NULL;

1024 1025
    files = priv->files;
    parent_hash = priv->top_reverse_map;
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045

    replace_dummy = FALSE;

    if (parent_ptr != NULL)
    {
        file_entry->parent = g_sequence_get (parent_ptr);
        /* At this point we set loaded. Either we saw
         * "done" and ignored it waiting for this, or we do this
         * earlier, but then we replace the dummy row anyway,
         * so it doesn't matter */
        file_entry->parent->loaded = 1;
        parent_hash = file_entry->parent->reverse_map;
        files = file_entry->parent->files;
        if (g_sequence_get_length (files) == 1)
        {
            GSequenceIter *dummy_ptr = g_sequence_get_iter_at_pos (files, 0);
            FileEntry *dummy_entry = g_sequence_get (dummy_ptr);
            if (dummy_entry->file == NULL)
            {
                /* replace the dummy loading entry */
1046
                priv->stamp++;
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
                g_sequence_remove (dummy_ptr);

                replace_dummy = TRUE;
            }
        }
    }


    file_entry->ptr = g_sequence_insert_sorted (files, file_entry,
                                                nautilus_list_model_file_entry_compare_func, model);

    g_hash_table_insert (parent_hash, file, file_entry->ptr);

1060
    iter.stamp = priv->stamp;
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
    iter.user_data = file_entry->ptr;

    path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
    if (replace_dummy)
    {
        gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
    }
    else
    {
        gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
    }

    if (nautilus_file_is_directory (file))
    {
        file_entry->files = g_sequence_new ((GDestroyNotify) file_entry_free);

        add_dummy_row (model, file_entry);

        gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (model),
                                              path, &iter);
    }
    gtk_tree_path_free (path);

    return TRUE;
Anders Carlsson's avatar
Anders Carlsson committed
1085 1086 1087
}

void
1088 1089 1090 1091
nautilus_list_model_file_changed (NautilusListModel *model,
                                  NautilusFile      *file,
                                  NautilusDirectory *directory)
{
1092
    NautilusListModelPrivate *priv;
1093 1094 1095 1096 1097 1098 1099 1100 1101
    FileEntry *parent_file_entry;
    GtkTreeIter iter;
    GtkTreePath *path, *parent_path;
    GSequenceIter *ptr;
    int pos_before, pos_after, length, i, old;
    int *new_order;
    gboolean has_iter;
    GSequence *files;

1102 1103
    priv = nautilus_list_model_get_instance_private (model);

1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126
    ptr = lookup_file (model, file, directory);
    if (!ptr)
    {
        return;
    }


    pos_before = g_sequence_iter_get_position (ptr);

    g_sequence_sort_changed (ptr, nautilus_list_model_file_entry_compare_func, model);

    pos_after = g_sequence_iter_get_position (ptr);

    if (pos_before != pos_after)
    {
        /* The file moved, we need to send rows_reordered */

        parent_file_entry = ((FileEntry *) g_sequence_get (ptr))->parent;

        if (parent_file_entry == NULL)
        {
            has_iter = FALSE;
            parent_path = gtk_tree_path_new ();
1127
            files = priv->files;
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 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
        }
        else
        {
            has_iter = TRUE;
            nautilus_list_model_ptr_to_iter (model, parent_file_entry->ptr, &iter);
            parent_path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
            files = parent_file_entry->files;
        }

        length = g_sequence_get_length (files);
        new_order = g_new (int, length);
        /* Note: new_order[newpos] = oldpos */
        for (i = 0, old = 0; i < length; ++i)
        {
            if (i == pos_after)
            {
                new_order[i] = pos_before;
            }
            else
            {
                if (old == pos_before)
                {
                    old++;
                }
                new_order[i] = old++;
            }
        }

        gtk_tree_model_rows_reordered (GTK_TREE_MODEL (model),
                                       parent_path, has_iter ? &iter : NULL, new_order);

        gtk_tree_path_free (parent_path);
        g_free (new_order);
    }

    nautilus_list_model_ptr_to_iter (model, ptr, &iter);
    path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &iter);
    gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
    gtk_tree_path_free (path);
Anders Carlsson's avatar
Anders Carlsson committed
1167 1168 1169
}

gboolean
1170
nautilus_list_model_is_empty (NautilusListModel *model)
Anders Carlsson's avatar
Anders Carlsson committed
1171
{
1172 1173 1174 1175 1176
    NautilusListModelPrivate *priv;

    priv = nautilus_list_model_get_instance_private (model);

    return (g_sequence_get_length (priv->files) == 0);
Anders Carlsson's avatar
Anders Carlsson committed
1177 1178 1179
}

static void
1180 1181 1182
nautilus_list_model_remove (NautilusListModel *model,
                            GtkTreeIter       *iter)
{
1183
    NautilusListModelPrivate *priv;
1184 1185 1186 1187 1188
    GSequenceIter *ptr, *child_ptr;
    FileEntry *file_entry, *child_file_entry, *parent_file_entry;
    GtkTreePath *path;
    GtkTreeIter parent_iter;

1189
    priv = nautilus_list_model_get_instance_private (model);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    ptr = iter->user_data;
    file_entry = g_sequence_get (ptr);

    if (file_entry->files != NULL)
    {
        while (g_sequence_get_length (file_entry->files) > 0)
        {
            child_ptr = g_sequence_get_begin_iter (file_entry->files);
            child_file_entry = g_sequence_get (child_ptr);
            if (child_file_entry->file != NULL)
            {
                nautilus_list_model_remove_file (model,
                                                 child_file_entry->file,
                                                 file_entry->subdirectory);
            }
            else
            {
                path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), iter);
                gtk_tree_path_append_index (path, 0);
1209
                priv->stamp++;
1210 1211 1212 1213 1214 1215
                g_sequence_remove (child_ptr);
                gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
                gtk_tree_path_free (path);
            }

            /* the parent iter didn't actually change */
1216
            iter->stamp = priv->stamp;
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
        }
    }

    if (file_entry->file != NULL)       /* Don't try to remove dummy row */
    {
        if (file_entry->parent != NULL)
        {
            g_hash_table_remove (file_entry->parent->reverse_map, file_entry->file);
        }
        else
        {
1228
            g_hash_table_remove (priv->top_reverse_map, file_entry->file);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247
        }
    }

    parent_file_entry = file_entry->parent;
    if (parent_file_entry && g_sequence_get_length (parent_file_entry->files) == 1 &&
        file_entry->file != NULL)
    {
        /* this is the last non-dummy child, add a dummy node */
        /* We need to do this before removing the last file to avoid
         * collapsing the row.
         */
        add_dummy_row (model, parent_file_entry);
    }

    if (file_entry->subdirectory != NULL)
    {
        g_signal_emit (model,
                       list_model_signals[SUBDIRECTORY_UNLOADED], 0,
                       file_entry->subdirectory);
1248
        g_hash_table_remove (priv->directory_reverse_map,
1249 1250 1251 1252 1253 1254
                             file_entry->subdirectory);
    }

    path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), iter);

    g_sequence_remove (ptr);
1255
    priv->stamp++;
1256 1257 1258 1259 1260 1261
    gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);

    gtk_tree_path_free (path);

    if (parent_file_entry && g_sequence_get_length (parent_file_entry->files) == 0)
    {
1262
        parent_iter.stamp = priv->stamp;
1263 1264 1265 1266 1267 1268
        parent_iter.user_data = parent_file_entry->ptr;
        path = gtk_tree_model_get_path (GTK_TREE_MODEL (model), &parent_iter);
        gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (model),
                                              path, &parent_iter);
        gtk_tree_path_free (path);
    }
Anders Carlsson's avatar
Anders Carlsson committed
1269 1270 1271
}

void
1272 1273 1274
nautilus_list_model_remove_file (NautilusListModel *model,
                                 NautilusFile      *file,
                                 NautilusDirectory *directory)
Anders Carlsson's avatar
Anders Carlsson committed
1275
{
1276
    GtkTreeIter iter;
Anders Carlsson's avatar
Anders Carlsson committed
1277

1278 1279 1280 1281
    if (nautilus_list_model_get_tree_iter_from_file (model, file, directory, &iter))
    {
        nautilus_list_model_remove (model, &iter);
    }
Anders Carlsson's avatar
Anders Carlsson committed
1282 1283
}

Alexander Larsson's avatar
Alexander Larsson committed
1284
static void
1285 1286 1287
nautilus_list_model_clear_directory (NautilusListModel *model,
                                     GSequence         *files)
{
1288
    NautilusListModelPrivate *priv;
1289 1290 1291
    GtkTreeIter iter;
    FileEntry *file_entry;

1292 1293
    priv = nautilus_list_model_get_instance_private (model);

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
    while (g_sequence_get_length (files) > 0)
    {
        iter.user_data = g_sequence_get_begin_iter (files);

        file_entry = g_sequence_get (iter.user_data);
        if (file_entry->files != NULL)
        {
            nautilus_list_model_clear_directory (model, file_entry->files);
        }

1304
        iter.stamp = priv->stamp;
1305 1306
        nautilus_list_model_remove (model, &iter);
    }
Anders Carlsson's avatar
Anders Carlsson committed
1307 1308
}

Alexander Larsson's avatar
Alexander Larsson committed
1309
void
1310
nautilus_list_model_clear (NautilusListModel *model)
Alexander Larsson's avatar
Alexander Larsson committed
1311
{
1312 1313
    NautilusListModelPrivate *priv;

1314
    g_return_if_fail (model != NULL);
Alexander Larsson's avatar
Alexander Larsson committed
1315

1316 1317 1318
    priv = nautilus_list_model_get_instance_private (model);

    nautilus_list_model_clear_directory (model, priv->files);
Alexander Larsson's avatar
Alexander Larsson committed
1319 1320
}

1321
NautilusFile *
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
nautilus_list_model_file_for_path (NautilusListModel *model,
                                   GtkTreePath       *path)
{
    NautilusFile *file;
    GtkTreeIter iter;

    file = NULL;
    if (gtk_tree_model_get_iter (GTK_TREE_MODEL (model),
                                 &iter, path))
    {
        gtk_tree_model_get (GTK_TREE_MODEL (model),
                            &iter,
                            NAUTILUS_LIST_MODEL_FILE_COLUMN, &file,
                            -1);
    }
    return file;
1338 1339
}

1340
gboolean
1341 1342 1343 1344
nautilus_list_model_load_subdirectory (NautilusListModel  *model,
                                       GtkTreePath        *path,
                                       NautilusDirectory **directory)
{
1345
    NautilusListModelPrivate *priv;
1346 1347 1348 1349
    GtkTreeIter iter;
    FileEntry *file_entry;
    NautilusDirectory *subdirectory;

1350 1351
    priv = nautilus_list_model_get_instance_private (model);

1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
    if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
    {
        return FALSE;
    }

    file_entry = g_sequence_get (iter.user_data);
    if (file_entry->file == NULL ||
        file_entry->subdirectory != NULL)
    {
        return FALSE;
    }

    subdirectory = nautilus_directory_get_for_file (file_entry->file);

1366
    if (g_hash_table_lookup (priv->directory_reverse_map, subdirectory) != NULL)
1367 1368 1369 1370 1371 1372 1373
    {
        nautilus_directory_unref (subdirectory);
        g_warning ("Already in directory_reverse_map, failing\n");
        return FALSE;
    }

    file_entry->subdirectory = subdirectory,
1374
    g_hash_table_insert (priv->directory_reverse_map,
1375 1376 1377 1378 1379 1380 1381 1382
                         subdirectory, file_entry->ptr);
    file_entry->reverse_map = g_hash_table_new (g_direct_hash, g_direct_equal);

    /* Return a ref too */
    nautilus_directory_ref (subdirectory);
    *directory = subdirectory;

    return TRUE;
1383 1384 1385 1386
}

/* removes all children of the subfolder and unloads the subdirectory */
void
1387 1388 1389
nautilus_list_model_unload_subdirectory (NautilusListModel *model,
                                         GtkTreeIter       *iter)
{
1390
    NautilusListModelPrivate *priv;
1391 1392 1393 1394
    GSequenceIter *child_ptr;
    FileEntry *file_entry, *child_file_entry;
    GtkTreeIter child_iter;

1395 1396
    priv = nautilus_list_model_get_instance_private (model);

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428
    file_entry = g_sequence_get (iter->user_data);
    if (file_entry->file == NULL ||
        file_entry->subdirectory == NULL)
    {
        return;
    }

    file_entry->loaded = 0;

    /* Remove all children */
    while (g_sequence_get_length (file_entry->files) > 0)
    {
        child_ptr = g_sequence_get_begin_iter (file_entry->files);
        child_file_entry = g_sequence_get (child_ptr);
        if (child_file_entry->file == NULL)
        {
            /* Don't delete the dummy node */
            break;
        }
        else
        {
            nautilus_list_model_ptr_to_iter (model, child_ptr, &child_iter);
            nautilus_list_model_remove (model, &child_iter);
        }
    }

    /* Emit unload signal */
    g_signal_emit (model,
                   list_model_signals[SUBDIRECTORY_UNLOADED], 0,
                   file_entry->subdirectory);

    /* actually unload */
1429
    g_hash_table_remove (priv->directory_reverse_map,
1430 1431 1432 1433 1434 1435 1436
                         file_entry->subdirectory);
    nautilus_directory_unref (file_entry->subdirectory);
    file_entry->subdirectory = NULL;

    g_assert (g_hash_table_size (file_entry->reverse_map) == 0);
    g_hash_table_destroy (file_entry->reverse_map);
    file_entry->reverse_map = NULL;
1437 1438 1439 1440
}



Anders Carlsson's avatar
Anders Carlsson committed
1441
void
1442 1443
nautilus_list_model_set_should_sort_directories_first (NautilusListModel *model,
                                                       gboolean           sort_directories_first)
Anders Carlsson's avatar
Anders Carlsson committed
1444
{
1445 1446 1447 1448 1449
    NautilusListModelPrivate *priv;

    priv = nautilus_list_model_get_instance_private (model);

    if (priv->sort_directories_first == sort_directories_first)
1450 1451 1452
    {
        return;
    }
Anders Carlsson's avatar
Anders Carlsson committed
1453

1454
    priv->sort_directories_first = sort_directories_first;
1455
    nautilus_list_model_sort (model);
Anders Carlsson's avatar
Anders Carlsson committed
1456 1457 1458
}

int
1459
nautilus_list_model_get_sort_column_id_from_attribute (NautilusListModel *model,
1460 1461
                                                       GQuark             attribute)
{
1462
    NautilusListModelPrivate *priv;
1463 1464 1465 1466 1467 1468 1469
    guint i;

    if (attribute == 0)
    {
        return -1;
    }

1470 1471
    priv = nautilus_list_model_get_instance_private (model);

1472 1473 1474 1475 1476 1477 1478 1479
    /* Hack - the preferences dialog sets modification_date for some
     * rather than date_modified for some reason.  Make sure that
     * works. */
    if (attribute == attribute_modification_date_q)
    {
        attribute = attribute_date_modified_q;
    }

1480
    for (i = 0; i < priv->columns->len; i++)
1481 1482 1483 1484
    {
        NautilusColumn *column;
        GQuark column_attribute;

1485
        column = NAUTILUS_COLUMN (priv->columns->pdata[i]);
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
        g_object_get (G_OBJECT (column),
                      "attribute_q", &column_attribute,
                      NULL);
        if (column_attribute == attribute)
        {
            return NAUTILUS_LIST_MODEL_NUM_COLUMNS + i;
        }
    }

    return -1;
Anders Carlsson's avatar
Anders Carlsson committed
1496 1497
}

1498
GQuark
1499
nautilus_list_model_get_attribute_from_sort_column_id (NautilusListModel *model,
1500
                                                       int                sort_column_id)
Diego González's avatar
Diego González committed
1501
{
1502
    NautilusListModelPrivate *priv;
1503 1504 1505
    NautilusColumn *column;
    int index;
    GQuark attribute;
Diego González's avatar
Diego González committed
1506

1507
    priv = nautilus_list_model_get_instance_private (model);
1508
    index = sort_column_id - NAUTILUS_LIST_MODEL_NUM_COLUMNS;
Anders Carlsson's avatar
Anders Carlsson committed
1509