nautilus-directory-async.c 134 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *  nautilus-directory-async.c: Nautilus directory model state machine.
 *
 *  Copyright (C) 1999, 2000, 2001 Eazel, Inc.
 *
 *  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/>.
 *
 *  Author: Darin Adler <darin@bentspoon.com>
 */
Darin Adler's avatar
Darin Adler committed
21 22 23

#include <config.h>

24
#include "nautilus-directory-notify.h"
25
#include "nautilus-directory-private.h"
Darin Adler's avatar
Darin Adler committed
26
#include "nautilus-file-attributes.h"
27
#include "nautilus-file-private.h"
28
#include "nautilus-file-utilities.h"
Alexander Larsson's avatar
Alexander Larsson committed
29
#include "nautilus-signaller.h"
30
#include "nautilus-global-preferences.h"
31
#include "nautilus-link.h"
32
#include "nautilus-profile.h"
33
#include "nautilus-metadata.h"
34
#include <eel/eel-glib-extensions.h>
35
#include <gtk/gtk.h>
36
#include <libxml/parser.h>
37
#include <stdio.h>
38
#include <stdlib.h>
Darin Adler's avatar
Darin Adler committed
39

40
/* turn this on to check if async. job calls are balanced */
41
#if 0
42
#define DEBUG_ASYNC_JOBS
43
#endif
44

45
#define DIRECTORY_LOAD_ITEMS_PER_CALLBACK 100
46 47 48 49

/* Keep async. jobs down to this number for all directories. */
#define MAX_ASYNC_JOBS 10

50 51 52 53 54
struct LinkInfoReadState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
    NautilusFile *file;
Alexander Larsson's avatar
Alexander Larsson committed
55 56
};

57 58 59 60 61
struct ThumbnailState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
    NautilusFile *file;
Alexander Larsson's avatar
Alexander Larsson committed
62 63
};

64 65 66 67 68
struct MountState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
    NautilusFile *file;
69 70
};

71 72 73 74 75
struct FilesystemInfoState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
    NautilusFile *file;
76 77
};

78 79 80 81 82 83 84 85
struct DirectoryLoadState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
    GFileEnumerator *enumerator;
    GHashTable *load_mime_list_hash;
    NautilusFile *load_directory_file;
    int load_file_count;
Alexander Larsson's avatar
Alexander Larsson committed
86 87
};

88 89 90 91 92 93 94
struct MimeListState
{
    NautilusDirectory *directory;
    NautilusFile *mime_list_file;
    GCancellable *cancellable;
    GFileEnumerator *enumerator;
    GHashTable *mime_list_hash;
Alexander Larsson's avatar
Alexander Larsson committed
95 96
};

97 98 99 100
struct GetInfoState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
Alexander Larsson's avatar
Alexander Larsson committed
101 102
};

103 104 105 106 107
struct NewFilesState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
    int count;
Alexander Larsson's avatar
Alexander Larsson committed
108 109
};

110 111 112 113 114 115 116
struct DirectoryCountState
{
    NautilusDirectory *directory;
    NautilusFile *count_file;
    GCancellable *cancellable;
    GFileEnumerator *enumerator;
    int file_count;
117 118
};

119 120 121 122 123 124 125 126 127
struct DeepCountState
{
    NautilusDirectory *directory;
    GCancellable *cancellable;
    GFileEnumerator *enumerator;
    GFile *deep_count_location;
    GList *deep_count_subdirectories;
    GArray *seen_deep_count_inodes;
    char *fs_id;
Alexander Larsson's avatar
Alexander Larsson committed
128 129 130 131
};



132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
typedef struct
{
    NautilusFile *file;     /* Which file, NULL means all. */
    union
    {
        NautilusDirectoryCallback directory;
        NautilusFileCallback file;
    } callback;
    gpointer callback_data;
    Request request;
    gboolean active;     /* Set to FALSE when the callback is triggered and
                          * scheduled to be called at idle, its still kept
                          * in the list so we can kill it when the file
                          * goes away.
                          */
147
} ReadyCallback;
148

149 150 151 152 153 154
typedef struct
{
    NautilusFile *file;     /* Which file, NULL means all. */
    gboolean monitor_hidden_files;     /* defines whether "all" includes hidden files */
    gconstpointer client;
    Request request;
155 156
} Monitor;

157 158 159 160 161 162
typedef struct
{
    NautilusDirectory *directory;
    NautilusInfoProvider *provider;
    NautilusOperationHandle *handle;
    NautilusOperationResult result;
163 164
} InfoProviderResponse;

165 166
typedef gboolean (*RequestCheck) (Request);
typedef gboolean (*FileCheck) (NautilusFile *);
167

Darin Adler's avatar
Darin Adler committed
168 169 170
/* Current number of async. jobs. */
static int async_job_count;
static GHashTable *waiting_directories;
171 172 173
#ifdef DEBUG_ASYNC_JOBS
static GHashTable *async_jobs;
#endif
Darin Adler's avatar
Darin Adler committed
174

175
/* Forward declarations for functions that need them. */
176 177 178 179 180 181 182 183 184 185 186
static void     deep_count_load (DeepCountState *state,
                                 GFile          *location);
static gboolean request_is_satisfied (NautilusDirectory *directory,
                                      NautilusFile      *file,
                                      Request            request);
static void     cancel_loading_attributes (NautilusDirectory     *directory,
                                           NautilusFileAttributes file_attributes);
static void     add_all_files_to_work_queue (NautilusDirectory *directory);
static void     link_info_done (NautilusDirectory *directory,
                                NautilusFile      *file,
                                const char        *uri,
187
                                const char        *name,
188
                                GIcon             *icon,
189
                                gboolean           is_launcher);
190 191 192 193 194 195
static void     move_file_to_low_priority_queue (NautilusDirectory *directory,
                                                 NautilusFile      *file);
static void     move_file_to_extension_queue (NautilusDirectory *directory,
                                              NautilusFile      *file);
static void     nautilus_directory_invalidate_file_attributes (NautilusDirectory     *directory,
                                                               NautilusFileAttributes file_attributes);
196

197 198 199 200 201
/* Some helpers for case-insensitive strings.
 * Move to nautilus-glib-extensions?
 */

static gboolean
202 203
istr_equal (gconstpointer v,
            gconstpointer v2)
204
{
205
    return g_ascii_strcasecmp (v, v2) == 0;
206 207 208 209 210
}

static guint
istr_hash (gconstpointer key)
{
211 212
    const char *p;
    guint h;
213

214 215 216 217 218 219 220
    h = 0;
    for (p = key; *p != '\0'; p++)
    {
        h = (h << 5) - h + g_ascii_tolower (*p);
    }

    return h;
221 222 223 224 225
}

static GHashTable *
istr_set_new (void)
{
226
    return g_hash_table_new_full (istr_hash, istr_equal, g_free, NULL);
227 228 229
}

static void
230 231
istr_set_insert (GHashTable *table,
                 const char *istr)
232
{
233
    char *key;
234

235 236
    key = g_strdup (istr);
    g_hash_table_replace (table, key, key);
237 238 239
}

static void
240 241 242
add_istr_to_list (gpointer key,
                  gpointer value,
                  gpointer callback_data)
243
{
244
    GList **list;
245

246 247
    list = callback_data;
    *list = g_list_prepend (*list, g_strdup (key));
248 249 250 251 252
}

static GList *
istr_set_get_as_list (GHashTable *table)
{
253
    GList *list;
254

255 256 257
    list = NULL;
    g_hash_table_foreach (table, add_istr_to_list, &list);
    return list;
258 259 260 261 262
}

static void
istr_set_destroy (GHashTable *table)
{
263
    g_hash_table_destroy (table);
264 265
}

266 267
static void
request_counter_add_request (RequestCounter counter,
268
                             Request        request)
269
{
270
    guint i;
271

272 273 274 275 276 277 278
    for (i = 0; i < REQUEST_TYPE_LAST; i++)
    {
        if (REQUEST_WANTS_TYPE (request, i))
        {
            counter[i]++;
        }
    }
279 280 281 282
}

static void
request_counter_remove_request (RequestCounter counter,
283
                                Request        request)
284
{
285
    guint i;
286

287 288 289 290 291 292 293
    for (i = 0; i < REQUEST_TYPE_LAST; i++)
    {
        if (REQUEST_WANTS_TYPE (request, i))
        {
            counter[i]--;
        }
    }
294 295
}

296 297 298 299
#if 0
static void
nautilus_directory_verify_request_counts (NautilusDirectory *directory)
{
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    GList *l;
    RequestCounter counters;
    int i;
    gboolean fail;

    fail = FALSE;
    for (i = 0; i < REQUEST_TYPE_LAST; i++)
    {
        counters[i] = 0;
    }
    for (l = directory->details->monitor_list; l != NULL; l = l->next)
    {
        Monitor *monitor = l->data;
        request_counter_add_request (counters, monitor->request);
    }
    for (i = 0; i < REQUEST_TYPE_LAST; i++)
    {
        if (counters[i] != directory->details->monitor_counters[i])
        {
            g_warning ("monitor counter for %i is wrong, expecting %d but found %d",
                       i, counters[i], directory->details->monitor_counters[i]);
            fail = TRUE;
        }
    }
    for (i = 0; i < REQUEST_TYPE_LAST; i++)
    {
        counters[i] = 0;
    }
    for (l = directory->details->call_when_ready_list; l != NULL; l = l->next)
    {
        ReadyCallback *callback = l->data;
        request_counter_add_request (counters, callback->request);
    }
    for (i = 0; i < REQUEST_TYPE_LAST; i++)
    {
        if (counters[i] != directory->details->call_when_ready_counters[i])
        {
            g_warning ("call when ready counter for %i is wrong, expecting %d but found %d",
                       i, counters[i], directory->details->call_when_ready_counters[i]);
            fail = TRUE;
        }
    }
    g_assert (!fail);
343 344 345
}
#endif

Darin Adler's avatar
Darin Adler committed
346 347 348 349 350
/* Start a job. This is really just a way of limiting the number of
 * async. requests that we issue at any given time. Without this, the
 * number of requests is unbounded.
 */
static gboolean
351
async_job_start (NautilusDirectory *directory,
352
                 const char        *job)
Darin Adler's avatar
Darin Adler committed
353
{
354
#ifdef DEBUG_ASYNC_JOBS
355
    char *key;
356 357
#endif

358
    g_debug ("starting %s in %p", job, directory->details->location);
359

360 361 362 363 364 365 366 367 368
    g_assert (async_job_count >= 0);
    g_assert (async_job_count <= MAX_ASYNC_JOBS);

    if (async_job_count >= MAX_ASYNC_JOBS)
    {
        if (waiting_directories == NULL)
        {
            waiting_directories = g_hash_table_new (NULL, NULL);
        }
Darin Adler's avatar
Darin Adler committed
369

370 371 372
        g_hash_table_insert (waiting_directories,
                             directory,
                             directory);
Darin Adler's avatar
Darin Adler committed
373

374 375
        return FALSE;
    }
Darin Adler's avatar
Darin Adler committed
376

377
#ifdef DEBUG_ASYNC_JOBS
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
    {
        char *uri;
        if (async_jobs == NULL)
        {
            async_jobs = g_hash_table_new (g_str_hash, g_str_equal);
        }
        uri = nautilus_directory_get_uri (directory);
        key = g_strconcat (uri, ": ", job, NULL);
        if (g_hash_table_lookup (async_jobs, key) != NULL)
        {
            g_warning ("same job twice: %s in %s",
                       job, uri);
        }
        g_free (uri);
        g_hash_table_insert (async_jobs, key, directory);
    }
#endif

    async_job_count += 1;
    return TRUE;
Darin Adler's avatar
Darin Adler committed
398 399 400
}

/* End a job. */
401
static void
402
async_job_end (NautilusDirectory *directory,
403
               const char        *job)
Darin Adler's avatar
Darin Adler committed
404
{
405
#ifdef DEBUG_ASYNC_JOBS
406 407
    char *key;
    gpointer table_key, value;
408 409
#endif

410
    g_debug ("stopping %s in %p", job, directory->details->location);
411

412
    g_assert (async_job_count > 0);
Darin Adler's avatar
Darin Adler committed
413

414
#ifdef DEBUG_ASYNC_JOBS
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
    {
        char *uri;
        uri = nautilus_directory_get_uri (directory);
        g_assert (async_jobs != NULL);
        key = g_strconcat (uri, ": ", job, NULL);
        if (!g_hash_table_lookup_extended (async_jobs, key, &table_key, &value))
        {
            g_warning ("ending job we didn't start: %s in %s",
                       job, uri);
        }
        else
        {
            g_hash_table_remove (async_jobs, key);
            g_free (table_key);
        }
        g_free (uri);
        g_free (key);
    }
433 434
#endif

435
    async_job_count -= 1;
Darin Adler's avatar
Darin Adler committed
436 437
}

438
/* Helper to get one value from a hash table. */
Darin Adler's avatar
Darin Adler committed
439
static void
440 441 442
get_one_value_callback (gpointer key,
                        gpointer value,
                        gpointer callback_data)
Darin Adler's avatar
Darin Adler committed
443
{
444
    gpointer *returned_value;
Darin Adler's avatar
Darin Adler committed
445

446 447
    returned_value = callback_data;
    *returned_value = value;
Darin Adler's avatar
Darin Adler committed
448 449
}

450
/* return a single value from a hash table. */
Darin Adler's avatar
Darin Adler committed
451 452 453
static gpointer
get_one_value (GHashTable *table)
{
454
    gpointer value;
Darin Adler's avatar
Darin Adler committed
455

456 457 458 459 460 461
    value = NULL;
    if (table != NULL)
    {
        g_hash_table_foreach (table, get_one_value_callback, &value);
    }
    return value;
Darin Adler's avatar
Darin Adler committed
462 463 464 465 466 467 468 469
}

/* Wake up directories that are "blocked" as long as there are job
 * slots available.
 */
static void
async_job_wake_up (void)
{
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    static gboolean already_waking_up = FALSE;
    gpointer value;

    g_assert (async_job_count >= 0);
    g_assert (async_job_count <= MAX_ASYNC_JOBS);

    if (already_waking_up)
    {
        return;
    }

    already_waking_up = TRUE;
    while (async_job_count < MAX_ASYNC_JOBS)
    {
        value = get_one_value (waiting_directories);
        if (value == NULL)
        {
            break;
        }
        g_hash_table_remove (waiting_directories, value);
        nautilus_directory_async_state_changed
            (NAUTILUS_DIRECTORY (value));
    }
    already_waking_up = FALSE;
Darin Adler's avatar
Darin Adler committed
494 495 496 497
}

static void
directory_count_cancel (NautilusDirectory *directory)
498
{
499 500 501 502 503
    if (directory->details->count_in_progress != NULL)
    {
        g_cancellable_cancel (directory->details->count_in_progress->cancellable);
        directory->details->count_in_progress = NULL;
    }
504 505
}

506
static void
Darin Adler's avatar
Darin Adler committed
507
deep_count_cancel (NautilusDirectory *directory)
508
{
509 510 511 512 513
    if (directory->details->deep_count_in_progress != NULL)
    {
        g_assert (NAUTILUS_IS_FILE (directory->details->deep_count_file));

        g_cancellable_cancel (directory->details->deep_count_in_progress->cancellable);
514

515
        directory->details->deep_count_file->details->deep_counts_status = NAUTILUS_REQUEST_NOT_STARTED;
516

517 518 519
        directory->details->deep_count_in_progress->directory = NULL;
        directory->details->deep_count_in_progress = NULL;
        directory->details->deep_count_file = NULL;
Darin Adler's avatar
Darin Adler committed
520

521 522
        async_job_end (directory, "deep count");
    }
523 524
}

525
static void
Darin Adler's avatar
Darin Adler committed
526
mime_list_cancel (NautilusDirectory *directory)
527
{
528 529 530 531
    if (directory->details->mime_list_in_progress != NULL)
    {
        g_cancellable_cancel (directory->details->mime_list_in_progress->cancellable);
    }
532 533
}

534
static void
535
link_info_cancel (NautilusDirectory *directory)
536
{
537 538 539 540 541 542 543
    if (directory->details->link_info_read_state != NULL)
    {
        g_cancellable_cancel (directory->details->link_info_read_state->cancellable);
        directory->details->link_info_read_state->directory = NULL;
        directory->details->link_info_read_state = NULL;
        async_job_end (directory, "link info");
    }
544 545
}

Alexander Larsson's avatar
Alexander Larsson committed
546 547 548
static void
thumbnail_cancel (NautilusDirectory *directory)
{
549 550 551 552 553 554 555
    if (directory->details->thumbnail_state != NULL)
    {
        g_cancellable_cancel (directory->details->thumbnail_state->cancellable);
        directory->details->thumbnail_state->directory = NULL;
        directory->details->thumbnail_state = NULL;
        async_job_end (directory, "thumbnail");
    }
Alexander Larsson's avatar
Alexander Larsson committed
556 557
}

558 559 560
static void
mount_cancel (NautilusDirectory *directory)
{
561 562 563 564 565 566 567
    if (directory->details->mount_state != NULL)
    {
        g_cancellable_cancel (directory->details->mount_state->cancellable);
        directory->details->mount_state->directory = NULL;
        directory->details->mount_state = NULL;
        async_job_end (directory, "mount");
    }
568 569
}

570
static void
Darin Adler's avatar
Darin Adler committed
571
file_info_cancel (NautilusDirectory *directory)
572
{
573 574 575 576 577 578
    if (directory->details->get_info_in_progress != NULL)
    {
        g_cancellable_cancel (directory->details->get_info_in_progress->cancellable);
        directory->details->get_info_in_progress->directory = NULL;
        directory->details->get_info_in_progress = NULL;
        directory->details->get_info_file = NULL;
Darin Adler's avatar
Darin Adler committed
579

580 581
        async_job_end (directory, "file info");
    }
582 583
}

584 585 586
static void
new_files_cancel (NautilusDirectory *directory)
{
587 588 589 590 591 592 593 594 595 596 597 598 599 600
    GList *l;
    NewFilesState *state;

    if (directory->details->new_files_in_progress != NULL)
    {
        for (l = directory->details->new_files_in_progress; l != NULL; l = l->next)
        {
            state = l->data;
            g_cancellable_cancel (state->cancellable);
            state->directory = NULL;
        }
        g_list_free (directory->details->new_files_in_progress);
        directory->details->new_files_in_progress = NULL;
    }
601 602
}

Darin Adler's avatar
Darin Adler committed
603
static int
604
monitor_key_compare (gconstpointer a,
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
                     gconstpointer data)
{
    const Monitor *monitor;
    const Monitor *compare_monitor;

    monitor = a;
    compare_monitor = data;

    if (monitor->client < compare_monitor->client)
    {
        return -1;
    }
    if (monitor->client > compare_monitor->client)
    {
        return +1;
    }

    if (monitor->file < compare_monitor->file)
    {
        return -1;
    }
    if (monitor->file > compare_monitor->file)
    {
        return +1;
    }

    return 0;
Darin Adler's avatar
Darin Adler committed
632 633 634
}

static GList *
635
find_monitor (NautilusDirectory *directory,
636 637
              NautilusFile      *file,
              gconstpointer      client)
Darin Adler's avatar
Darin Adler committed
638
{
639
    Monitor monitor;
Darin Adler's avatar
Darin Adler committed
640

641 642
    monitor.client = client;
    monitor.file = file;
643

644 645 646
    return g_list_find_custom (directory->details->monitor_list,
                               &monitor,
                               monitor_key_compare);
647
}
Darin Adler's avatar
Darin Adler committed
648

649 650
static void
remove_monitor_link (NautilusDirectory *directory,
651
                     GList             *link)
Darin Adler's avatar
Darin Adler committed
652
{
653
    Monitor *monitor;
654

655 656 657 658 659 660 661 662 663 664
    if (link != NULL)
    {
        monitor = link->data;
        request_counter_remove_request (directory->details->monitor_counters,
                                        monitor->request);
        directory->details->monitor_list =
            g_list_remove_link (directory->details->monitor_list, link);
        g_free (monitor);
        g_list_free_1 (link);
    }
Darin Adler's avatar
Darin Adler committed
665 666 667
}

static void
668
remove_monitor (NautilusDirectory *directory,
669 670
                NautilusFile      *file,
                gconstpointer      client)
Darin Adler's avatar
Darin Adler committed
671
{
672
    remove_monitor_link (directory, find_monitor (directory, file, client));
Darin Adler's avatar
Darin Adler committed
673 674
}

675 676
Request
nautilus_directory_set_up_request (NautilusFileAttributes file_attributes)
677
{
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 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
    Request request;

    request = 0;

    if ((file_attributes & NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_COUNT) != 0)
    {
        REQUEST_SET_TYPE (request, REQUEST_DIRECTORY_COUNT);
    }

    if ((file_attributes & NAUTILUS_FILE_ATTRIBUTE_DEEP_COUNTS) != 0)
    {
        REQUEST_SET_TYPE (request, REQUEST_DEEP_COUNT);
    }

    if ((file_attributes & NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_MIME_TYPES) != 0)
    {
        REQUEST_SET_TYPE (request, REQUEST_MIME_LIST);
    }
    if ((file_attributes & NAUTILUS_FILE_ATTRIBUTE_INFO) != 0)
    {
        REQUEST_SET_TYPE (request, REQUEST_FILE_INFO);
    }

    if (file_attributes & NAUTILUS_FILE_ATTRIBUTE_LINK_INFO)
    {
        REQUEST_SET_TYPE (request, REQUEST_FILE_INFO);
        REQUEST_SET_TYPE (request, REQUEST_LINK_INFO);
    }

    if ((file_attributes & NAUTILUS_FILE_ATTRIBUTE_EXTENSION_INFO) != 0)
    {
        REQUEST_SET_TYPE (request, REQUEST_EXTENSION_INFO);
    }

    if (file_attributes & NAUTILUS_FILE_ATTRIBUTE_THUMBNAIL)
    {
        REQUEST_SET_TYPE (request, REQUEST_THUMBNAIL);
        REQUEST_SET_TYPE (request, REQUEST_FILE_INFO);
    }

    if (file_attributes & NAUTILUS_FILE_ATTRIBUTE_MOUNT)
    {
        REQUEST_SET_TYPE (request, REQUEST_MOUNT);
        REQUEST_SET_TYPE (request, REQUEST_FILE_INFO);
    }

    if (file_attributes & NAUTILUS_FILE_ATTRIBUTE_FILESYSTEM_INFO)
    {
        REQUEST_SET_TYPE (request, REQUEST_FILESYSTEM_INFO);
    }

    return request;
730 731
}

732
static void
733 734
mime_db_changed_callback (GObject           *ignore,
                          NautilusDirectory *dir)
735
{
736
    NautilusFileAttributes attrs;
737

738 739
    g_assert (dir != NULL);
    g_assert (dir->details != NULL);
740

741 742 743
    attrs = NAUTILUS_FILE_ATTRIBUTE_INFO |
            NAUTILUS_FILE_ATTRIBUTE_LINK_INFO |
            NAUTILUS_FILE_ATTRIBUTE_DIRECTORY_ITEM_MIME_TYPES;
744

745
    nautilus_directory_force_reload_internal (dir, attrs);
746 747
}

Darin Adler's avatar
Darin Adler committed
748
void
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 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
nautilus_directory_monitor_add_internal (NautilusDirectory         *directory,
                                         NautilusFile              *file,
                                         gconstpointer              client,
                                         gboolean                   monitor_hidden_files,
                                         NautilusFileAttributes     file_attributes,
                                         NautilusDirectoryCallback  callback,
                                         gpointer                   callback_data)
{
    Monitor *monitor;
    GList *file_list;
    char *file_uri = NULL;
    char *dir_uri = NULL;

    g_assert (NAUTILUS_IS_DIRECTORY (directory));

    if (file != NULL)
    {
        file_uri = nautilus_file_get_uri (file);
    }
    if (directory != NULL)
    {
        dir_uri = nautilus_directory_get_uri (directory);
    }
    nautilus_profile_start ("uri %s file-uri %s client %p", dir_uri, file_uri, client);
    g_free (dir_uri);
    g_free (file_uri);

    /* Replace any current monitor for this client/file pair. */
    remove_monitor (directory, file, client);

    /* Add the new monitor. */
    monitor = g_new (Monitor, 1);
    monitor->file = file;
    monitor->monitor_hidden_files = monitor_hidden_files;
    monitor->client = client;
    monitor->request = nautilus_directory_set_up_request (file_attributes);

    if (file == NULL)
    {
        REQUEST_SET_TYPE (monitor->request, REQUEST_FILE_LIST);
    }
    directory->details->monitor_list =
        g_list_prepend (directory->details->monitor_list, monitor);
    request_counter_add_request (directory->details->monitor_counters,
                                 monitor->request);

    if (callback != NULL)
    {
        file_list = nautilus_directory_get_file_list (directory);
        (*callback)(directory, file_list, callback_data);
        nautilus_file_list_free (file_list);
    }

    /* Start the "real" monitoring (FAM or whatever). */
    /* We always monitor the whole directory since in practice
     * nautilus almost always shows the whole directory anyway, and
     * it allows us to avoid one file monitor per file in a directory.
     */
    if (directory->details->monitor == NULL)
    {
        directory->details->monitor = nautilus_monitor_directory (directory->details->location);
    }


    if (REQUEST_WANTS_TYPE (monitor->request, REQUEST_FILE_INFO) &&
        directory->details->mime_db_monitor == 0)
    {
        directory->details->mime_db_monitor =
            g_signal_connect_object (nautilus_signaller_get_current (),
                                     "mime-data-changed",
                                     G_CALLBACK (mime_db_changed_callback), directory, 0);
    }

    /* Put the monitor file or all the files on the work queue. */
    if (file != NULL)
    {
        nautilus_directory_add_file_to_work_queue (directory, file);
    }
    else
    {
        add_all_files_to_work_queue (directory);
    }

    /* Kick off I/O. */
    nautilus_directory_async_state_changed (directory);
    nautilus_profile_end (NULL);
Darin Adler's avatar
Darin Adler committed
835 836
}

837
static void
838 839
set_file_unconfirmed (NautilusFile *file,
                      gboolean      unconfirmed)
840
{
841
    NautilusDirectory *directory;
842

843 844
    g_assert (NAUTILUS_IS_FILE (file));
    g_assert (unconfirmed == FALSE || unconfirmed == TRUE);
845

846 847 848 849 850
    if (file->details->unconfirmed == unconfirmed)
    {
        return;
    }
    file->details->unconfirmed = unconfirmed;
851

852 853 854 855 856 857 858 859 860
    directory = file->details->directory;
    if (unconfirmed)
    {
        directory->details->confirmed_file_count--;
    }
    else
    {
        directory->details->confirmed_file_count++;
    }
861 862
}

863 864 865 866 867
static gboolean show_hidden_files = TRUE;

static void
show_hidden_files_changed_callback (gpointer callback_data)
{
868
    show_hidden_files = g_settings_get_boolean (gtk_filechooser_preferences, NAUTILUS_PREFERENCES_SHOW_HIDDEN_FILES);
869 870
}

871
static gboolean
872 873
should_skip_file (NautilusDirectory *directory,
                  GFileInfo         *info)
874
{
875
    static gboolean show_hidden_files_changed_callback_installed = FALSE;
876

877 878 879 880 881 882 883
    /* Add the callback once for the life of our process */
    if (!show_hidden_files_changed_callback_installed)
    {
        g_signal_connect_swapped (gtk_filechooser_preferences,
                                  "changed::" NAUTILUS_PREFERENCES_SHOW_HIDDEN_FILES,
                                  G_CALLBACK (show_hidden_files_changed_callback),
                                  NULL);
884

885
        show_hidden_files_changed_callback_installed = TRUE;
886

887 888 889
        /* Peek for the first time */
        show_hidden_files_changed_callback (NULL);
    }
890

891 892 893 894 895 896
    if (!show_hidden_files &&
        (g_file_info_get_is_hidden (info) ||
         g_file_info_get_is_backup (info)))
    {
        return TRUE;
    }
897

898
    return FALSE;
899
}
900

Darin Adler's avatar
Darin Adler committed
901 902 903
static gboolean
dequeue_pending_idle_callback (gpointer callback_data)
{
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    NautilusDirectory *directory;
    GList *pending_file_info;
    GList *node, *next;
    NautilusFile *file;
    GList *changed_files, *added_files;
    GFileInfo *file_info;
    const char *mimetype, *name;
    DirectoryLoadState *dir_load_state;

    directory = NAUTILUS_DIRECTORY (callback_data);

    nautilus_directory_ref (directory);

    nautilus_profile_start ("nitems %d", g_list_length (directory->details->pending_file_info));

    directory->details->dequeue_pending_idle_id = 0;

    /* Handle the files in the order we saw them. */
    pending_file_info = g_list_reverse (directory->details->pending_file_info);
    directory->details->pending_file_info = NULL;

    /* If we are no longer monitoring, then throw away these. */
    if (!nautilus_directory_is_file_list_monitored (directory))
    {
        nautilus_directory_async_state_changed (directory);
        goto drain;
    }

    added_files = NULL;
    changed_files = NULL;

    dir_load_state = directory->details->directory_load_in_progress;

    /* Build a list of NautilusFile objects. */
    for (node = pending_file_info; node != NULL; node = node->next)
    {
        file_info = node->data;

        name = g_file_info_get_name (file_info);

        /* Update the file count. */
        /* FIXME bugzilla.gnome.org 45063: This could count a
         * file twice if we get it from both load_directory
         * and from new_files_callback. Not too hard to fix by
         * moving this into the actual callback instead of
         * waiting for the idle function.
         */
        if (dir_load_state &&
            !should_skip_file (directory, file_info))
        {
            dir_load_state->load_file_count += 1;

            /* Add the MIME type to the set. */
            mimetype = g_file_info_get_content_type (file_info);
            if (mimetype != NULL)
            {
                istr_set_insert (dir_load_state->load_mime_list_hash,
                                 mimetype);
            }
        }

        /* check if the file already exists */
        file = nautilus_directory_find_file_by_name (directory, name);
        if (file != NULL)
        {
            /* file already exists in dir, check if we still need to
             *  emit file_added or if it changed */
            set_file_unconfirmed (file, FALSE);
            if (!file->details->is_added)
            {
                /* We consider this newly added even if its in the list.
                 * This can happen if someone called nautilus_file_get_by_uri()
                 * on a file in the folder before the add signal was
                 * emitted */
                nautilus_file_ref (file);
                file->details->is_added = TRUE;
                added_files = g_list_prepend (added_files, file);
            }
            else if (nautilus_file_update_info (file, file_info))
            {
                /* File changed, notify about the change. */
                nautilus_file_ref (file);
                changed_files = g_list_prepend (changed_files, file);
            }
        }
        else
        {
            /* new file, create a nautilus file object and add it to the list */
            file = nautilus_file_new_from_info (directory, file_info);
            nautilus_directory_add_file (directory, file);
            file->details->is_added = TRUE;
            added_files = g_list_prepend (added_files, file);
        }
    }

    /* If we are done loading, then we assume that any unconfirmed
     * files are gone.
     */
    if (directory->details->directory_loaded)
    {
        for (node = directory->details->file_list;
             node != NULL; node = next)
        {
            file = NAUTILUS_FILE (node->data);
            next = node->next;

            if (file->details->unconfirmed)
            {
                nautilus_file_ref (file);
                changed_files = g_list_prepend (changed_files, file);

                nautilus_file_mark_gone (file);
            }
        }
    }

    /* Send the changed and added signals. */
    nautilus_directory_emit_change_signals (directory, changed_files);
    nautilus_file_list_free (changed_files);
    nautilus_directory_emit_files_added (directory, added_files);
    nautilus_file_list_free (added_files);

    if (directory->details->directory_loaded &&
        !directory->details->directory_loaded_sent_notification)
    {
        /* Send the done_loading signal. */
        nautilus_directory_emit_done_loading (directory);

        if (dir_load_state)
        {
            file = dir_load_state->load_directory_file;

            file->details->directory_count = dir_load_state->load_file_count;
            file->details->directory_count_is_up_to_date = TRUE;
            file->details->got_directory_count = TRUE;

            file->details->got_mime_list = TRUE;
            file->details->mime_list_is_up_to_date = TRUE;
            g_list_free_full (file->details->mime_list, g_free);
            file->details->mime_list = istr_set_get_as_list
                                           (dir_load_state->load_mime_list_hash);

            nautilus_file_changed (file);
        }

        nautilus_directory_async_state_changed (directory);

        directory->details->directory_loaded_sent_notification = TRUE;
    }

drain:
    g_list_free_full (pending_file_info, g_object_unref);

    /* Get the state machine running again. */
    nautilus_directory_async_state_changed (directory);

    nautilus_profile_end (NULL);

    nautilus_directory_unref (directory);
    return FALSE;
Darin Adler's avatar
Darin Adler committed
1064 1065 1066 1067 1068
}

void
nautilus_directory_schedule_dequeue_pending (NautilusDirectory *directory)
{
1069 1070 1071 1072 1073
    if (directory->details->dequeue_pending_idle_id == 0)
    {
        directory->details->dequeue_pending_idle_id
            = g_idle_add (dequeue_pending_idle_callback, directory);
    }
Darin Adler's avatar
Darin Adler committed
1074 1075 1076 1077
}

static void
directory_load_one (NautilusDirectory *directory,
1078
                    GFileInfo         *info)
Darin Adler's avatar
Darin Adler committed
1079
{
1080 1081 1082 1083
    if (info == NULL)
    {
        return;
    }
1084

1085 1086 1087
    if (g_file_info_get_name (info) == NULL)
    {
        char *uri;
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
        uri = nautilus_directory_get_uri (directory);
        g_warning ("Got GFileInfo with NULL name in %s, ignoring. This shouldn't happen unless the gvfs backend is broken.\n", uri);
        g_free (uri);

        return;
    }

    /* Arrange for the "loading" part of the work. */
    g_object_ref (info);
    directory->details->pending_file_info
        = g_list_prepend (directory->details->pending_file_info, info);
    nautilus_directory_schedule_dequeue_pending (directory);
Darin Adler's avatar
Darin Adler committed
1101 1102 1103
}

static void
1104
directory_load_cancel (NautilusDirectory *directory)
Darin Adler's avatar
Darin Adler committed
1105
{
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
    NautilusFile *file;
    DirectoryLoadState *state;

    state = directory->details->directory_load_in_progress;
    if (state != NULL)
    {
        file = state->load_directory_file;
        file->details->loading_directory = FALSE;
        if (file->details->directory != directory)
        {
            nautilus_directory_async_state_changed (file->details->directory);
        }
Alexander Larsson's avatar
Alexander Larsson committed
1118

1119 1120 1121 1122 1123
        g_cancellable_cancel (state->cancellable);
        state->directory = NULL;
        directory->details->directory_load_in_progress = NULL;
        async_job_end (directory, "file list");
    }
1124 1125
}

1126 1127 1128
static void
file_list_cancel (NautilusDirectory *directory)
{
1129
    directory_load_cancel (directory);
1130

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    if (directory->details->dequeue_pending_idle_id != 0)
    {
        g_source_remove (directory->details->dequeue_pending_idle_id);
        directory->details->dequeue_pending_idle_id = 0;
    }

    if (directory->details->pending_file_info != NULL)
    {
        g_list_free_full (directory->details->pending_file_info, g_object_unref);
        directory->details->pending_file_info = NULL;
    }
1142 1143
}

1144 1145
static void
directory_load_done (NautilusDirectory *directory,
1146
                     GError            *error)
1147
{
1148
    GList *node;
1149

1150 1151
    nautilus_profile_start (NULL);
    g_object_ref (directory);
1152

1153 1154
    directory->details->directory_loaded = TRUE;
    directory->details->directory_loaded_sent_notification = FALSE;
Maciej Stachowiak's avatar
Maciej Stachowiak committed
1155

1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    if (error != NULL)
    {
        /* The load did not complete successfully. This means
         * we don't know the status of the files in this directory.
         * We clear the unconfirmed bit on each file here so that
         * they won't be marked "gone" later -- we don't know enough
         * about them to know whether they are really gone.
         */
        for (node = directory->details->file_list;
             node != NULL; node = node->next)
        {
            set_file_unconfirmed (NAUTILUS_FILE (node->data), FALSE);
        }
1169

1170 1171
        nautilus_directory_emit_load_error (directory, error);
    }
1172

1173 1174 1175 1176 1177 1178
    /* Call the idle function right away. */
    if (directory->details->dequeue_pending_idle_id != 0)
    {
        g_source_remove (directory->details->dequeue_pending_idle_id);
    }
    dequeue_pending_idle_callback (directory);
1179

1180
    directory_load_cancel (directory);
1181

1182 1183
    g_object_unref (directory);
    nautilus_profile_end (NULL);
Darin Adler's avatar
Darin Adler committed
1184 1185 1186
}

void
1187
nautilus_directory_monitor_remove_internal (NautilusDirectory *directory,
1188 1189
                                            NautilusFile      *file,
                                            gconstpointer      client)
Darin Adler's avatar
Darin Adler committed
1190
{
1191 1192 1193
    g_assert (NAUTILUS_IS_DIRECTORY (directory));
    g_assert (file == NULL || NAUTILUS_IS_FILE (file));
    g_assert (client != NULL);
Darin Adler's avatar
Darin Adler committed
1194

1195
    remove_monitor (directory, file, client);
1196

1197 1198 1199 1200 1201 1202
    if (directory->details->monitor != NULL
        && directory->details->monitor_list == NULL)
    {
        nautilus_monitor_cancel (directory->details->monitor);
        directory->details->monitor = NULL;
    }
1203

1204
    /* XXX - do we need to remove anything from the work queue? */
1205

1206
    nautilus_directory_async_state_changed (directory);
Darin Adler's avatar
Darin Adler committed
1207 1208
}

1209 1210
FileMonitors *
nautilus_directory_remove_file_monitors (NautilusDirectory *directory,
1211
                                         NautilusFile      *file)
1212
{
1213 1214
    GList *result, **list, *node, *next;
    Monitor *monitor;
1215

1216 1217 1218
    g_assert (NAUTILUS_IS_DIRECTORY (directory));
    g_assert (NAUTILUS_IS_FILE (file));
    g_assert (file->details->directory == directory);
1219

1220
    result = NULL;
1221

1222 1223 1224 1225 1226
    list = &directory->details->monitor_list;
    for (node = directory->details->monitor_list; node != NULL; node = next)
    {
        next = node->next;
        monitor = node->data;
1227

1228 1229 1230 1231 1232 1233 1234 1235
        if (monitor->file == file)
        {
            *list = g_list_remove_link (*list, node);
            result = g_list_concat (node, result);
            request_counter_remove_request (directory->details->monitor_counters,
                                            monitor->request);
        }
    }
1236

1237
    /* XXX - do we need to remove anything from the work queue? */
1238

1239
    nautilus_directory_async_state_changed (directory);
1240

1241
    return (FileMonitors *) result;
1242 1243 1244 1245
}

void
nautilus_directory_add_file_monitors (NautilusDirectory *directory,
1246 1247
                                      NautilusFile      *file,
                                      FileMonitors      *monitors)
1248
{
1249 1250 1251
    GList **list;
    GList *l;
    Monitor *monitor;
1252

1253 1254 1255
    g_assert (NAUTILUS_IS_DIRECTORY (directory));
    g_assert (NAUTILUS_IS_FILE (file));
    g_assert (file->details->directory == directory);
1256

1257 1258 1259 1260
    if (monitors == NULL)
    {
        return;
    }
1261

1262 1263 1264 1265 1266 1267
    for (l = (GList *) monitors; l != NULL; l = l->next)
    {
        monitor = l->data;
        request_counter_add_request (directory->details->monitor_counters,
                                     monitor->request);
    }
1268

1269 1270
    list = &directory->details->monitor_list;
    *list = g_list_concat (*list, (GList *) monitors);
1271

1272
    nautilus_directory_add_file_to_work_queue (directory, file);
1273

1274
    nautilus_directory_async_state_changed (directory);
1275 1276
}

Darin Adler's avatar
Darin Adler committed
1277
static int
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 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
ready_callback_key_compare (gconstpointer a,
                            gconstpointer b)
{
    const ReadyCallback *callback_a, *callback_b;

    callback_a = a;
    callback_b = b;

    if (callback_a->file < callback_b->file)
    {
        return -1;
    }
    if (callback_a->file > callback_b->file)
    {
        return 1;
    }
    if (callback_a->file == NULL)
    {
        /* ANSI C doesn't allow ordered compares of function pointers, so we cast them to
         * normal pointers to make some overly pedantic compilers (*cough* HP-UX *cough*)
         * compile this. Of course, on any compiler where ordered function pointers actually
         * break this probably won't work, but at least it will compile on platforms where it
         * works, but stupid compilers won't let you use it.
         */
        if ((void *) callback_a->callback.directory < (void *) callback_b->callback.directory)
        {
            return -1;
        }
        if ((void *) callback_a->callback.directory > (void *) callback_b->callback.directory)
        {
            return 1;
        }
    }
    else
    {
        if ((void *) callback_a->callback.file < (void *) callback_b->callback.file)
        {
            return -1;
        }
        if ((void *) callback_a->callback.file > (void *) callback_b->callback.file)
        {
            return 1;
        }
    }
    if (callback_a->callback_data < callback_b->callback_data)
    {
        return -1;
    }
    if (callback_a->callback_data > callback_b->callback_data)
    {
        return 1;
    }
    return 0;
Darin Adler's avatar
Darin Adler committed
1331 1332
}

1333
static int
1334 1335
ready_callback_key_compare_only_active (gconstpointer a,
                                        gconstpointer b)
1336
{
1337
    const ReadyCallback *callback_a;
1338

1339
    callback_a = a;
1340

1341 1342 1343 1344 1345
    /* Non active callbacks never match */
    if (!callback_a->active)
    {
        return -1;
    }
1346

1347
    return ready_callback_key_compare (a, b);
1348 1349
}

1350
static void
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
ready_callback_call (NautilusDirectory   *directory,
                     const ReadyCallback *callback)
{
    GList *file_list;

    /* Call the callback. */
    if (callback->file != NULL)
    {
        if (callback->callback.file)
        {
            (*callback->callback.file)(callback->file,
                                       callback->callback_data);
        }
    }
    else if (callback->callback.directory != NULL)
    {
        if (directory == NULL ||
            !REQUEST_WANTS_TYPE (callback->request, REQUEST_FILE_LIST))
        {
            file_list = NULL;
        }
        else
        {
            file_list = nautilus_directory_get_file_list (directory);
        }

        /* Pass back the file list if the user was waiting for it. */
        (*callback->callback.directory)(directory,
                                        file_list,
                                        callback->callback_data);

        nautilus_file_list_free (file_list);
    }
1384 1385
}

Darin Adler's avatar
Darin Adler committed
1386
void
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 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 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
nautilus_directory_call_when_ready_internal (NautilusDirectory         *directory,
                                             NautilusFile              *file,
                                             NautilusFileAttributes     file_attributes,
                                             gboolean                   wait_for_file_list,
                                             NautilusDirectoryCallback  directory_callback,
                                             NautilusFileCallback       file_callback,
                                             gpointer                   callback_data)
{
    ReadyCallback callback;

    g_assert (directory == NULL || NAUTILUS_IS_DIRECTORY (directory));
    g_assert (file == NULL || NAUTILUS_IS_FILE (file));
    g_assert (file != NULL || directory_callback != NULL);

    /* Construct a callback object. */
    callback.active = TRUE;
    callback.file = file;
    if (file == NULL)
    {
        callback.callback.directory = directory_callback;
    }
    else
    {
        callback.callback.file = file_callback;
    }
    callback.callback_data = callback_data;
    callback.request = nautilus_directory_set_up_request (file_attributes);
    if (wait_for_file_list)
    {
        REQUEST_SET_TYPE (callback.request, REQUEST_FILE_LIST);
    }

    /* Handle the NULL case. */
    if (directory == NULL)
    {
        ready_callback_call (NULL, &callback);
        return;
    }

    /* Check if the callback is already there. */
    if (g_list_find_custom (directory->details->call_when_ready_list,
                            &callback,
                            ready_callback_key_compare_only_active) != NULL)
    {
        if (file_callback != NULL && directory_callback != NULL)
        {
            g_warning ("tried to add a new callback while an old one was pending");
        }
        /* NULL callback means, just read it. Conflicts are ok. */
        return;
    }

    /* Add the new callback to the list. */
    directory->details->call_when_ready_list = g_list_prepend
                                                   (directory->details->call_when_ready_list,
                                                   g_memdup (&callback, sizeof (callback)));
    request_counter_add_request (directory->details->call_when_ready_counters,
                                 callback.request);

    /* Put the callback file or all the files on the work queue. */
    if (file != NULL)
    {
        nautilus_directory_add_file_to_work_queue (directory, file);
    }
    else
    {
        add_all_files_to_work_queue (directory);
    }

    nautilus_directory_async_state_changed (directory);
}

gboolean
nautilus_directory_check_if_ready_internal (NautilusDirectory      *directory,
                                            NautilusFile           *file,
                                            NautilusFileAttributes  file_attributes)
{
    Request request;

    g_assert (NAUTILUS_IS_DIRECTORY (directory));

    request = nautilus_directory_set_up_request (file_attributes);
    return request_is_satisfied (directory, file, request);
1470
}
1471

1472 1473
static void
remove_callback_link_keep_data (NautilusDirectory *directory,
1474
                                GList             *link)
1475
{
1476 1477 1478 1479 1480 1481
    ReadyCallback *callback;

    callback = link->data;

    directory->details->call_when_ready_list = g_list_remove_link
                                                   (directory->details->call_when_ready_list, link);
1482

1483 1484 1485
    request_counter_remove_request (directory->details->call_when_ready_counters,
                                    callback->request);
    g_list_free_1 (link);
1486 1487 1488 1489
}

static void
remove_callback_link (NautilusDirectory *directory,
1490
                      GList             *link)
1491
{
1492 1493 1494 1495 1496
    ReadyCallback *callback;

    callback = link->data;
    remove_callback_link_keep_data (directory, link);
    g_free (callback);
Darin Adler's avatar
Darin Adler committed
1497 1498 1499
}

void
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
nautilus_directory_cancel_callback_internal (NautilusDirectory         *directory,
                                             NautilusFile              *file,
                                             NautilusDirectoryCallback  directory_callback,
                                             NautilusFileCallback       file_callback,
                                             gpointer                   callback_data)
{
    ReadyCallback callback;
    GList *node;

    if (directory == NULL)
    {
        return;
    }

    g_assert (NAUTILUS_IS_DIRECTORY (directory));
    g_assert (file == NULL || NAUTILUS_IS_FILE (file));
    g_assert (file != NULL || directory_callback != NULL);
    g_assert (file == NULL || file_callback != NULL);

    /* Construct a callback object. */
    callback.file = file;
    if (file == NULL)
    {
        callback.callback.directory = directory_callback;
    }
    else
    {
        callback.callback.file = file_callback;
    }
    callback.callback_data = callback_data;

    /* Remove all queued callback from the list (including non-active). */
    do
    {
        node = g_list_find_custom (directory->details->call_when_ready_list,
                                   &callback,
                                   ready_callback_key_compare);
        if (node != NULL)
        {
            remove_callback_link (directory, node);

            nautilus_directory_async_state_changed (directory);
        }
    }
    while (node != NULL);
Darin Adler's avatar
Darin Adler committed
1545 1546
}

Alexander Larsson's avatar
Alexander Larsson committed
1547 1548
static void
new_files_state_unref (NewFilesState *state)
1549
{
1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
    state->count--;

    if (state->count == 0)
    {
        if (state->directory)
        {
            state->directory->details->new_files_in_progress =
                g_list_remove (state->directory->details->new_files_in_progress,
                               state);
        }
1560

1561 1562 1563
        g_object_unref (state->cancellable);
        g_free (state);
    }
1564 1565
}

Darin Adler's avatar
Darin Adler committed
1566
static void
1567 1568 1569
new_files_callback (GObject      *source_object,
                    GAsyncResult *res,
                    gpointer      user_data)
Darin Adler's avatar
Darin Adler committed
1570
{
1571 1572 1573 1574 1575
    NautilusDirectory *directory;
    GFileInfo *info;
    NewFilesState *state;

    state = user_data;
Darin Adler's avatar
Darin Adler committed
1576

1577 1578 1579 1580 1581 1582
    if (state->directory == NULL)
    {
        /* Operation was cancelled. Bail out */
        new_files_state_unref (state);
        return;
    }
1583

1584
    directory = nautilus_directory_ref (state->directory);
1585

1586 1587 1588 1589 1590 1591 1592
    /* Queue up the new file. */
    info = g_file_query_info_finish (G_FILE (source_object), res, NULL);
    if (info != NULL)
    {
        directory_load_one (directory, info);
        g_object_unref (info);
    }
Alexander Larsson's avatar
Alexander Larsson committed
1593

1594 1595 1596
    new_files_state_unref (state);

    nautilus_directory_unref (directory);
Darin Adler's avatar
Darin Adler committed
1597 1598 1599
}

void
1600
nautilus_directory_get_info_for_new_files (NautilusDirectory *directory,
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
                                           GList             *location_list)
{
    NewFilesState *state;
    GFile *location;
    GList *l;

    if (location_list == NULL)
    {
        return;
    }

    state = g_new (NewFilesState, 1);
    state->directory = directory;
    state->cancellable = g_cancellable_new ();
    state->count = 0;

    for (l = location_list; l != NULL; l = l->next)
    {
        location = l->data;

        state->count++;

        g_file_query_info_async (location,
                                 NAUTILUS_FILE_DEFAULT_ATTRIBUTES,
                                 0,
                                 G_PRIORITY_DEFAULT,
                                 state->cancellable,
                                 new_files_callback, state);
    }

    directory->details->new_files_in_progress
        = g_list_prepend (directory->details->new_files_in_progress,
                          state);
Darin Adler's avatar
Darin Adler committed
1634
}
1635 1636 1637 1638

void
nautilus_async_destroying_file (NautilusFile *file)
{
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743
    NautilusDirectory *directory;
    gboolean changed;
    GList *node, *next;
    ReadyCallback *callback;
    Monitor *monitor;

    directory = file->details->directory;
    changed = FALSE;

    /* Check for callbacks. */
    for (node = directory->details->call_when_ready_list; node != NULL; node = next)
    {
        next = node->next;
        callback = node->data;

        if (callback->file == file)
        {
            /* Client should have cancelled callback. */
            if (callback->active)
            {
                g_warning ("destroyed file has call_when_ready pending");
            }
            remove_callback_link (directory, node);
            changed = TRUE;
        }
    }

    /* Check for monitors. */
    for (node = directory->details->monitor_list; node != NULL; node = next)
    {
        next = node->next;
        monitor = node->data;

        if (monitor->file == file)
        {
            /* Client should have removed monitor earlier. */
            g_warning ("destroyed file still being monitored");
            remove_monitor_link (directory, node);
            changed = TRUE;
        }
    }

    /* Check if it's a file that's currently being worked on.
     * If so, make that NULL so it gets canceled right away.
     */
    if (directory->details->count_in_progress != NULL &&
        directory->details->count_in_progress->count_file == file)
    {
        directory->details->count_in_progress->count_file = NULL;
        changed = TRUE;
    }
    if (directory->details->deep_count_file == file)
    {
        directory->details->deep_count_file = NULL;
        changed = TRUE;
    }
    if (directory->details->mime_list_in_progress != NULL &&
        directory->details->mime_list_in_progress->mime_list_file == file)
    {
        directory->details->mime_list_in_progress->mime_list_file = NULL;
        changed = TRUE;
    }
    if (directory->details->get_info_file == file)
    {
        directory->details->get_info_file = NULL;
        changed = TRUE;
    }
    if (directory->details->link_info_read_state != NULL &&
        directory->details->link_info_read_state->file == file)
    {
        directory->details->link_info_read_state->file = NULL;
        changed = TRUE;
    }
    if (directory->details->extension_info_file == file)
    {
        directory->details->extension_info_file = NULL;
        changed = TRUE;
    }

    if (directory->details->thumbnail_state != NULL &&
        directory->details->thumbnail_state->file == file)
    {
        directory->details->thumbnail_state->file = NULL;
        changed = TRUE;
    }

    if (directory->details->mount_state != NULL &&
        directory->details->mount_state->file == file)
    {
        directory->details->mount_state->file = NULL;
        changed = TRUE;
    }

    if (directory->details->filesystem_info_state != NULL &&
        directory->details->filesystem_info_state->file == file)
    {
        directory->details->filesystem_info_state->file = NULL;
        changed = TRUE;
    }

    /* Let the directory take care of the rest. */
    if (changed)
    {
        nautilus_directory_async_state_changed (directory);
    }
1744 1745 1746 1747 1748
}

static gboolean
lacks_directory_count (NautilusFile *file)
{
1749 1750
    return !file->details->directory_count_is_up_to_date
           && nautilus_file_should_show_directory_item_count (file);
1751 1752
}

1753
static gboolean
1754
should_get_directory_count_now (NautilusFile *file)
1755
{
1756 1757
    return lacks_directory_count (file)
           && !file->details->loading_directory;
1758 1759
}

Darin Adler's avatar