gtkfilesystem.c 43.7 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2
 * gtkfilesystem.c: Filesystem abstraction functions.
Owen Taylor's avatar
Owen Taylor committed
3
 * Copyright (C) 2003, Red Hat, Inc.
4
 * Copyright (C) 2007-2008 Carlos Garnacho
Owen Taylor's avatar
Owen Taylor committed
5
 *
6 7 8 9
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
Owen Taylor's avatar
Owen Taylor committed
10
 *
11
 * This program is distributed in the hope that it will be useful,
Owen Taylor's avatar
Owen Taylor committed
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
Owen Taylor's avatar
Owen Taylor committed
15
 *
16 17 18 19 20
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 * Authors: Carlos Garnacho <carlos@imendio.com>
Owen Taylor's avatar
Owen Taylor committed
21 22
 */

Michael Natterer's avatar
Michael Natterer committed
23 24 25 26
#include "config.h"

#include <string.h>

27
#include <glib/gi18n-lib.h>
28

29
#include "gtkfilechooser.h"
Owen Taylor's avatar
Owen Taylor committed
30
#include "gtkfilesystem.h"
31
#include "gtkicontheme.h"
32
#include "gtkprivate.h"
Owen Taylor's avatar
Owen Taylor committed
33

Matthias Clasen's avatar
Matthias Clasen committed
34 35
#include "gtkalias.h"

36 37
/* #define DEBUG_MODE */
#ifdef DEBUG_MODE
38
#define DEBUG(x) g_debug (x);
39
#else
40
#define DEBUG(x)
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
#endif

#define GTK_FILE_SYSTEM_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_FILE_SYSTEM, GtkFileSystemPrivate))
#define GTK_FOLDER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_FOLDER, GtkFolderPrivate))
#define FILES_PER_QUERY 100

/* The pointers we return for a GtkFileSystemVolume are opaque tokens; they are
 * really pointers to GDrive, GVolume or GMount objects.  We need an extra
 * token for the fake "File System" volume.  So, we'll return a pointer to
 * this particular string.
 */
static const gchar *root_volume_token = N_("File System");
#define IS_ROOT_VOLUME(volume) ((gpointer) (volume) == (gpointer) root_volume_token)

enum {
  PROP_0,
  PROP_FILE,
  PROP_ENUMERATOR,
  PROP_ATTRIBUTES
};

enum {
  BOOKMARKS_CHANGED,
  VOLUMES_CHANGED,
  FS_LAST_SIGNAL
};

enum {
  FILES_ADDED,
  FILES_REMOVED,
  FILES_CHANGED,
  FINISHED_LOADING,
  DELETED,
  FOLDER_LAST_SIGNAL
};

static guint fs_signals [FS_LAST_SIGNAL] = { 0, };
static guint folder_signals [FOLDER_LAST_SIGNAL] = { 0, };

typedef struct GtkFileSystemPrivate GtkFileSystemPrivate;
typedef struct GtkFolderPrivate GtkFolderPrivate;
typedef struct AsyncFuncData AsyncFuncData;

struct GtkFileSystemPrivate
{
  GVolumeMonitor *volume_monitor;

  /* This list contains elements that can be
   * of type GDrive, GVolume and GMount
   */
  GSList *volumes;

  /* This list contains GtkFileSystemBookmark structs */
  GSList *bookmarks;

  GFileMonitor *bookmarks_monitor;
};

struct GtkFolderPrivate
{
  GFile *folder_file;
  GHashTable *children;
  GFileMonitor *directory_monitor;
  GFileEnumerator *enumerator;
  GCancellable *cancellable;
  gchar *attributes;

  guint finished_loading : 1;
};

struct AsyncFuncData
{
  GtkFileSystem *file_system;
  GFile *file;
  GtkFolder *folder;
  GCancellable *cancellable;
  gchar *attributes;

  gpointer callback;
  gpointer data;
};
122

123
struct GtkFileSystemBookmark
Owen Taylor's avatar
Owen Taylor committed
124
{
125 126
  GFile *file;
  gchar *label;
Owen Taylor's avatar
Owen Taylor committed
127 128
};

129
G_DEFINE_TYPE (GtkFileSystem, _gtk_file_system, G_TYPE_OBJECT)
130

131
G_DEFINE_TYPE (GtkFolder, _gtk_folder, G_TYPE_OBJECT)
132 133


134 135 136 137 138
static void gtk_folder_set_finished_loading (GtkFolder *folder,
					     gboolean   finished_loading);
static void gtk_folder_add_file             (GtkFolder *folder,
					     GFile     *file,
					     GFileInfo *info);
139

Owen Taylor's avatar
Owen Taylor committed
140

141 142
/* GtkFileSystemBookmark methods */
void
143
_gtk_file_system_bookmark_free (GtkFileSystemBookmark *bookmark)
Owen Taylor's avatar
Owen Taylor committed
144
{
145 146 147
  g_object_unref (bookmark->file);
  g_free (bookmark->label);
  g_slice_free (GtkFileSystemBookmark, bookmark);
Owen Taylor's avatar
Owen Taylor committed
148 149
}

150 151 152 153 154
/* GtkFileSystem methods */
static void
volumes_changed (GVolumeMonitor *volume_monitor,
		 gpointer        volume,
		 gpointer        user_data)
Owen Taylor's avatar
Owen Taylor committed
155
{
156
  GtkFileSystem *file_system;
Owen Taylor's avatar
Owen Taylor committed
157

158
  gdk_threads_enter ();
Owen Taylor's avatar
Owen Taylor committed
159

160 161 162
  file_system = GTK_FILE_SYSTEM (user_data);
  g_signal_emit (file_system, fs_signals[VOLUMES_CHANGED], 0, volume);
  gdk_threads_leave ();
Owen Taylor's avatar
Owen Taylor committed
163 164
}

165 166
static void
gtk_file_system_dispose (GObject *object)
Owen Taylor's avatar
Owen Taylor committed
167
{
168
  GtkFileSystemPrivate *priv;
Owen Taylor's avatar
Owen Taylor committed
169

170
  DEBUG ("dispose");
Owen Taylor's avatar
Owen Taylor committed
171

172
  priv = GTK_FILE_SYSTEM_GET_PRIVATE (object);
Owen Taylor's avatar
Owen Taylor committed
173

174 175 176 177 178 179
  if (priv->volumes)
    {
      g_slist_foreach (priv->volumes, (GFunc) g_object_unref, NULL);
      g_slist_free (priv->volumes);
      priv->volumes = NULL;
    }
Owen Taylor's avatar
Owen Taylor committed
180

181
  if (priv->volume_monitor)
Owen Taylor's avatar
Owen Taylor committed
182
    {
183 184 185
      g_signal_handlers_disconnect_by_func (priv->volume_monitor, volumes_changed, object);
      g_object_unref (priv->volume_monitor);
      priv->volume_monitor = NULL;
Owen Taylor's avatar
Owen Taylor committed
186
    }
187

188
  G_OBJECT_CLASS (_gtk_file_system_parent_class)->dispose (object);
Owen Taylor's avatar
Owen Taylor committed
189 190
}

191 192
static void
gtk_file_system_finalize (GObject *object)
Owen Taylor's avatar
Owen Taylor committed
193
{
194
  GtkFileSystemPrivate *priv;
Owen Taylor's avatar
Owen Taylor committed
195

196 197 198 199 200 201
  DEBUG ("finalize");

  priv = GTK_FILE_SYSTEM_GET_PRIVATE (object);

  if (priv->bookmarks_monitor)
    g_object_unref (priv->bookmarks_monitor);
202

203
  if (priv->bookmarks)
Owen Taylor's avatar
Owen Taylor committed
204
    {
205
      g_slist_foreach (priv->bookmarks, (GFunc) _gtk_file_system_bookmark_free, NULL);
206
      g_slist_free (priv->bookmarks);
Owen Taylor's avatar
Owen Taylor committed
207
    }
Owen Taylor's avatar
Owen Taylor committed
208

209
  G_OBJECT_CLASS (_gtk_file_system_parent_class)->finalize (object);
Owen Taylor's avatar
Owen Taylor committed
210 211
}

212
static void
213
_gtk_file_system_class_init (GtkFileSystemClass *class)
Owen Taylor's avatar
Owen Taylor committed
214
{
215
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Owen Taylor's avatar
Owen Taylor committed
216

217 218
  object_class->dispose = gtk_file_system_dispose;
  object_class->finalize = gtk_file_system_finalize;
Owen Taylor's avatar
Owen Taylor committed
219

220 221 222 223 224 225 226 227
  fs_signals[BOOKMARKS_CHANGED] =
    g_signal_new ("bookmarks-changed",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFileSystemClass, bookmarks_changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
228

229 230 231 232 233 234 235 236 237 238
  fs_signals[VOLUMES_CHANGED] =
    g_signal_new ("volumes-changed",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFileSystemClass, volumes_changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  g_type_class_add_private (object_class, sizeof (GtkFileSystemPrivate));
Owen Taylor's avatar
Owen Taylor committed
239 240
}

241 242
static GFile *
get_bookmarks_file (void)
Owen Taylor's avatar
Owen Taylor committed
243
{
244 245
  GFile *file;
  gchar *filename;
Owen Taylor's avatar
Owen Taylor committed
246

247 248 249
  filename = g_build_filename (g_get_home_dir (), ".gtk-bookmarks", NULL);
  file = g_file_new_for_path (filename);
  g_free (filename);
Owen Taylor's avatar
Owen Taylor committed
250

251
  return file;
Owen Taylor's avatar
Owen Taylor committed
252 253
}

254 255
static GSList *
read_bookmarks (GFile *file)
Owen Taylor's avatar
Owen Taylor committed
256
{
257 258 259 260
  gchar *contents;
  gchar **lines, *space;
  GSList *bookmarks = NULL;
  gint i;
Owen Taylor's avatar
Owen Taylor committed
261

262 263 264
  if (!g_file_load_contents (file, NULL, &contents,
			     NULL, NULL, NULL))
    return NULL;
Owen Taylor's avatar
Owen Taylor committed
265

266
  lines = g_strsplit (contents, "\n", -1);
Owen Taylor's avatar
Owen Taylor committed
267

268 269 270
  for (i = 0; lines[i]; i++)
    {
      GtkFileSystemBookmark *bookmark;
Owen Taylor's avatar
Owen Taylor committed
271

272 273
      if (!*lines[i])
	continue;
Owen Taylor's avatar
Owen Taylor committed
274

275
      bookmark = g_slice_new0 (GtkFileSystemBookmark);
Owen Taylor's avatar
Owen Taylor committed
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
      if ((space = strchr (lines[i], ' ')) != NULL)
	{
	  space[0] = '\0';
	  bookmark->label = g_strdup (space + 1);
	}

      bookmark->file = g_file_new_for_uri (lines[i]);
      bookmarks = g_slist_prepend (bookmarks, bookmark);
    }

  bookmarks = g_slist_reverse (bookmarks);
  g_strfreev (lines);
  g_free (contents);

  return bookmarks;
Owen Taylor's avatar
Owen Taylor committed
292 293
}

294 295 296
static void
save_bookmarks (GFile  *bookmarks_file,
		GSList *bookmarks)
297
{
298 299 300 301 302 303 304 305 306 307 308 309 310
  GError *error = NULL;
  GString *contents;

  contents = g_string_new ("");

  while (bookmarks)
    {
      GtkFileSystemBookmark *bookmark;
      gchar *uri;

      bookmark = bookmarks->data;
      uri = g_file_get_uri (bookmark->file);
      g_string_append (contents, uri);
311

312 313 314 315 316 317 318 319 320 321 322 323 324 325
      if (bookmark->label)
	g_string_append_printf (contents, " %s", bookmark->label);

      g_string_append_c (contents, '\n');
      bookmarks = bookmarks->next;
      g_free (uri);
    }

  if (!g_file_replace_contents (bookmarks_file,
				contents->str,
				strlen (contents->str),
				NULL, FALSE, 0, NULL,
				NULL, &error))
    {
326
      g_critical ("%s", error->message);
327 328 329 330
      g_error_free (error);
    }

  g_string_free (contents, TRUE);
331 332
}

333 334 335 336 337 338
static void
bookmarks_file_changed (GFileMonitor      *monitor,
			GFile             *file,
			GFile             *other_file,
			GFileMonitorEvent  event,
			gpointer           data)
339
{
340 341 342 343 344 345 346 347 348 349
  GtkFileSystemPrivate *priv;

  priv = GTK_FILE_SYSTEM_GET_PRIVATE (data);

  switch (event)
    {
    case G_FILE_MONITOR_EVENT_CHANGED:
    case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
    case G_FILE_MONITOR_EVENT_CREATED:
    case G_FILE_MONITOR_EVENT_DELETED:
350
      g_slist_foreach (priv->bookmarks, (GFunc) _gtk_file_system_bookmark_free, NULL);
351 352 353 354 355 356 357 358 359 360 361 362
      g_slist_free (priv->bookmarks);

      priv->bookmarks = read_bookmarks (file);

      gdk_threads_enter ();
      g_signal_emit (data, fs_signals[BOOKMARKS_CHANGED], 0);
      gdk_threads_leave ();
      break;
    default:
      /* ignore at the moment */
      break;
    }
363 364
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
static gboolean
mount_referenced_by_volume_activation_root (GList *volumes, GMount *mount)
{
  GList *l;
  GFile *mount_root;
  gboolean ret;

  ret = FALSE;

  mount_root = g_mount_get_root (mount);

  for (l = volumes; l != NULL; l = l->next)
    {
      GVolume *volume = G_VOLUME (l->data);
      GFile *volume_activation_root;

      volume_activation_root = g_volume_get_activation_root (volume);
      if (volume_activation_root != NULL)
        {
          if (g_file_has_prefix (volume_activation_root, mount_root))
            {
              ret = TRUE;
              g_object_unref (volume_activation_root);
              break;
            }
          g_object_unref (volume_activation_root);
        }
    }

  g_object_unref (mount_root);
  return ret;
}

398 399
static void
get_volumes_list (GtkFileSystem *file_system)
400
{
401 402 403 404 405 406 407 408
  GtkFileSystemPrivate *priv;
  GList *l, *ll;
  GList *drives;
  GList *volumes;
  GList *mounts;
  GDrive *drive;
  GVolume *volume;
  GMount *mount;
409

410
  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
411

412
  if (priv->volumes)
413
    {
414 415 416 417 418 419 420 421 422 423 424 425
      g_slist_foreach (priv->volumes, (GFunc) g_object_unref, NULL);
      g_slist_free (priv->volumes);
      priv->volumes = NULL;
    }

  /* first go through all connected drives */
  drives = g_volume_monitor_get_connected_drives (priv->volume_monitor);

  for (l = drives; l != NULL; l = l->next)
    {
      drive = l->data;
      volumes = g_drive_get_volumes (drive);
426

427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
      if (volumes)
        {
          for (ll = volumes; ll != NULL; ll = ll->next)
            {
              volume = ll->data;
              mount = g_volume_get_mount (volume);

              if (mount)
                {
                  /* Show mounted volume */
                  priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (mount));
                  g_object_unref (mount);
                }
              else
                {
                  /* Do show the unmounted volumes in the sidebar;
                   * this is so the user can mount it (in case automounting
                   * is off).
                   *
                   * Also, even if automounting is enabled, this gives a visual
                   * cue that the user should remember to yank out the media if
                   * he just unmounted it.
                   */
                  priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (volume));
                }

	      g_object_unref (volume);
            }
455 456
  
           g_list_free (volumes);
457 458 459 460 461 462 463 464 465 466 467 468 469
        }
      else if (g_drive_is_media_removable (drive) && !g_drive_is_media_check_automatic (drive))
	{
	  /* If the drive has no mountable volumes and we cannot detect media change.. we
	   * display the drive in the sidebar so the user can manually poll the drive by
	   * right clicking and selecting "Rescan..."
	   *
	   * This is mainly for drives like floppies where media detection doesn't
	   * work.. but it's also for human beings who like to turn off media detection
	   * in the OS to save battery juice.
	   */

	  priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (drive));
470 471 472
	}
    }

473 474 475 476 477 478
  g_list_free (drives);

  /* add all volumes that is not associated with a drive */
  volumes = g_volume_monitor_get_volumes (priv->volume_monitor);

  for (l = volumes; l != NULL; l = l->next)
479
    {
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
      volume = l->data;
      drive = g_volume_get_drive (volume);

      if (drive)
        {
          g_object_unref (drive);
          continue;
        }

      mount = g_volume_get_mount (volume);

      if (mount)
        {
          /* show this mount */
          priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (mount));
          g_object_unref (mount);
        }
      else
        {
          /* see comment above in why we add an icon for a volume */
          priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (volume));
        }
502 503
    }

504 505
  /* add mounts that has no volume (/etc/mtab mounts, ftp, sftp,...) */
  mounts = g_volume_monitor_get_mounts (priv->volume_monitor);
506

507 508 509 510 511 512 513 514 515 516 517
  for (l = mounts; l != NULL; l = l->next)
    {
      mount = l->data;
      volume = g_mount_get_volume (mount);

      if (volume)
        {
          g_object_unref (volume);
          continue;
        }

518 519 520 521 522 523 524 525
      /* if there's exists one or more volumes with an activation root inside the mount,
       * don't display the mount
       */
      if (mount_referenced_by_volume_activation_root (volumes, mount))
        {
          continue;
        }

526 527 528
      /* show this mount */
      priv->volumes = g_slist_prepend (priv->volumes, g_object_ref (mount));
    }
529

530 531
  g_list_free (volumes);

532 533
  g_list_free (mounts);
}
534 535

static void
536
_gtk_file_system_init (GtkFileSystem *file_system)
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
{
  GtkFileSystemPrivate *priv;
  GFile *bookmarks_file;
  GError *error = NULL;

  DEBUG ("init");

  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);

  /* Volumes */
  priv->volume_monitor = g_volume_monitor_get ();

  g_signal_connect (priv->volume_monitor, "mount-added",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "mount-removed",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "mount-changed",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "volume-added",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "volume-removed",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "volume-changed",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "drive-connected",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "drive-disconnected",
		    G_CALLBACK (volumes_changed), file_system);
  g_signal_connect (priv->volume_monitor, "drive-changed",
		    G_CALLBACK (volumes_changed), file_system);

  /* Bookmarks */
  bookmarks_file = get_bookmarks_file ();
  priv->bookmarks = read_bookmarks (bookmarks_file);
  priv->bookmarks_monitor = g_file_monitor_file (bookmarks_file,
						 G_FILE_MONITOR_NONE,
						 NULL, &error);
  if (error)
575 576 577 578
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }
579 580 581 582 583 584 585 586 587
  else
    g_signal_connect (priv->bookmarks_monitor, "changed",
		      G_CALLBACK (bookmarks_file_changed), file_system);

  g_object_unref (bookmarks_file);
}

/* GtkFileSystem public methods */
GtkFileSystem *
588
_gtk_file_system_new (void)
589
{
590
  return g_object_new (GTK_TYPE_FILE_SYSTEM, NULL);
591 592
}

593
GSList *
594
_gtk_file_system_list_volumes (GtkFileSystem *file_system)
595
{
596 597 598 599 600 601 602 603 604 605 606 607
  GtkFileSystemPrivate *priv;
  GSList *list;

  DEBUG ("list_volumes");

  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);

  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
  get_volumes_list (GTK_FILE_SYSTEM (file_system));

  list = g_slist_copy (priv->volumes);

608
#ifndef G_OS_WIN32
609 610
  /* Prepend root volume */
  list = g_slist_prepend (list, (gpointer) root_volume_token);
611
#endif
612 613

  return list;
614 615
}

616
GSList *
617
_gtk_file_system_list_bookmarks (GtkFileSystem *file_system)
618
{
619 620
  GtkFileSystemPrivate *priv;
  GSList *bookmarks, *files = NULL;
621

622 623 624 625 626 627
  DEBUG ("list_bookmarks");

  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
  bookmarks = priv->bookmarks;

  while (bookmarks)
628
    {
629
      GtkFileSystemBookmark *bookmark;
630

631 632 633 634
      bookmark = bookmarks->data;
      bookmarks = bookmarks->next;

      files = g_slist_prepend (files, g_object_ref (bookmark->file));
635 636
    }

637
  return g_slist_reverse (files);
638
}
Owen Taylor's avatar
Owen Taylor committed
639

640
gboolean
641 642 643 644 645 646
_gtk_file_system_parse (GtkFileSystem     *file_system,
		        GFile             *base_file,
		        const gchar       *str,
		        GFile            **folder,
		        gchar            **file_part,
		        GError           **error)
Owen Taylor's avatar
Owen Taylor committed
647
{
648 649 650 651 652 653 654 655 656 657 658
  GFile *file;
  gboolean result = FALSE;
  gboolean is_dir = FALSE;
  gchar *last_slash = NULL;

  DEBUG ("parse");

  if (str && *str)
    is_dir = (str [strlen (str) - 1] == G_DIR_SEPARATOR);

  last_slash = strrchr (str, G_DIR_SEPARATOR);
Owen Taylor's avatar
Owen Taylor committed
659

660 661 662 663 664 665
  if (str[0] == '~')
    file = g_file_parse_name (str);
  else
    file = g_file_resolve_relative_path (base_file, str);

  if (g_file_equal (base_file, file))
Owen Taylor's avatar
Owen Taylor committed
666
    {
667 668 669 670 671 672
      /* this is when user types '.', could be the
       * beginning of a hidden file, ./ or ../
       */
      *folder = g_object_ref (file);
      *file_part = g_strdup (str);
      result = TRUE;
Owen Taylor's avatar
Owen Taylor committed
673
    }
674 675 676 677 678 679 680 681 682 683 684 685
  else if (is_dir)
    {
      /* it's a dir, or at least it ends with the dir separator */
      *folder = g_object_ref (file);
      *file_part = g_strdup ("");
      result = TRUE;
    }
  else
    {
      GFile *parent_file;

      parent_file = g_file_get_parent (file);
Owen Taylor's avatar
Owen Taylor committed
686

687 688 689
      if (!parent_file)
	{
	  g_set_error (error,
690 691
		       GTK_FILE_CHOOSER_ERROR,
		       GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
		       "Could not get parent file");
	  *folder = NULL;
	  *file_part = NULL;
	}
      else
	{
	  *folder = parent_file;
	  result = TRUE;

	  if (last_slash)
	    *file_part = g_strdup (last_slash + 1);
	  else
	    *file_part = g_strdup (str);
	}
    }

  g_object_unref (file);

  return result;
Owen Taylor's avatar
Owen Taylor committed
711 712 713
}

static void
714
free_async_data (AsyncFuncData *async_data)
Owen Taylor's avatar
Owen Taylor committed
715
{
716 717 718 719 720 721 722 723 724
  g_object_unref (async_data->file_system);
  g_object_unref (async_data->file);
  g_object_unref (async_data->cancellable);

  if (async_data->folder)
    g_object_unref (async_data->folder);

  g_free (async_data->attributes);
  g_free (async_data);
Owen Taylor's avatar
Owen Taylor committed
725 726
}

727 728 729 730
static void
enumerate_children_callback (GObject      *source_object,
			     GAsyncResult *result,
			     gpointer      user_data)
731
{
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  GFileEnumerator *enumerator;
  AsyncFuncData *async_data;
  GtkFolder *folder = NULL;
  GFile *file;
  GError *error = NULL;

  file = G_FILE (source_object);
  async_data = (AsyncFuncData *) user_data;
  enumerator = g_file_enumerate_children_finish (file, result, &error);

  if (enumerator)
    {
      folder = g_object_new (GTK_TYPE_FOLDER,
			     "file", source_object,
			     "enumerator", enumerator,
			     "attributes", async_data->attributes,
			     NULL);
      g_object_unref (enumerator);
    }

  gdk_threads_enter ();
  ((GtkFileSystemGetFolderCallback) async_data->callback) (async_data->cancellable,
							   folder, error, async_data->data);
  gdk_threads_leave ();

  free_async_data (async_data);
758

759 760
  if (error)
    g_error_free (error);
761 762
}

763
GCancellable *
764 765 766 767 768
_gtk_file_system_get_folder (GtkFileSystem                  *file_system,
			     GFile                          *file,
			     const gchar                    *attributes,
			     GtkFileSystemGetFolderCallback  callback,
			     gpointer                        data)
Owen Taylor's avatar
Owen Taylor committed
769
{
770 771 772
  GCancellable *cancellable;
  AsyncFuncData *async_data;

Owen Taylor's avatar
Owen Taylor committed
773
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
774
  g_return_val_if_fail (G_IS_FILE (file), NULL);
Owen Taylor's avatar
Owen Taylor committed
775

776
  cancellable = g_cancellable_new ();
Owen Taylor's avatar
Owen Taylor committed
777

778 779 780 781 782
  async_data = g_new0 (AsyncFuncData, 1);
  async_data->file_system = g_object_ref (file_system);
  async_data->file = g_object_ref (file);
  async_data->cancellable = g_object_ref (cancellable);
  async_data->attributes = g_strdup (attributes);
783

784 785 786 787 788 789 790 791 792 793 794
  async_data->callback = callback;
  async_data->data = data;

  g_file_enumerate_children_async (file,
				   attributes,
				   G_FILE_QUERY_INFO_NONE,
				   G_PRIORITY_DEFAULT,
				   cancellable,
				   enumerate_children_callback,
				   async_data);
  return cancellable;
795 796
}

797 798 799 800
static void
query_info_callback (GObject      *source_object,
		     GAsyncResult *result,
		     gpointer      user_data)
801
{
802 803 804 805
  AsyncFuncData *async_data;
  GError *error = NULL;
  GFileInfo *file_info;
  GFile *file;
806

807
  DEBUG ("query_info_callback");
808

809 810 811
  file = G_FILE (source_object);
  async_data = (AsyncFuncData *) user_data;
  file_info = g_file_query_info_finish (file, result, &error);
Owen Taylor's avatar
Owen Taylor committed
812

813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
  if (async_data->callback)
    {
      gdk_threads_enter ();
      ((GtkFileSystemGetInfoCallback) async_data->callback) (async_data->cancellable,
							     file_info, error, async_data->data);
      gdk_threads_leave ();
    }

  if (file_info)
    g_object_unref (file_info);

  if (error)
    g_error_free (error);

  free_async_data (async_data);
Owen Taylor's avatar
Owen Taylor committed
828 829
}

830
GCancellable *
831 832 833 834 835
_gtk_file_system_get_info (GtkFileSystem                *file_system,
			   GFile                        *file,
			   const gchar                  *attributes,
			   GtkFileSystemGetInfoCallback  callback,
			   gpointer                      data)
836
{
837 838 839
  GCancellable *cancellable;
  AsyncFuncData *async_data;

840
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
841
  g_return_val_if_fail (G_IS_FILE (file), NULL);
842

843
  cancellable = g_cancellable_new ();
844

845 846 847 848 849 850 851
  async_data = g_new0 (AsyncFuncData, 1);
  async_data->file_system = g_object_ref (file_system);
  async_data->file = g_object_ref (file);
  async_data->cancellable = g_object_ref (cancellable);

  async_data->callback = callback;
  async_data->data = data;
852

853 854 855 856 857 858 859 860 861
  g_file_query_info_async (file,
			   attributes,
			   G_FILE_QUERY_INFO_NONE,
			   G_PRIORITY_DEFAULT,
			   cancellable,
			   query_info_callback,
			   async_data);

  return cancellable;
862 863
}

864 865 866 867
static void
drive_poll_for_media_cb (GObject      *source_object,
                         GAsyncResult *result,
                         gpointer      user_data)
868
{
869 870 871 872 873
  AsyncFuncData *async_data;
  GError *error = NULL;

  g_drive_poll_for_media_finish (G_DRIVE (source_object), result, &error);
  async_data = (AsyncFuncData *) user_data;
874

875 876 877 878 879 880 881 882
  gdk_threads_enter ();
  ((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable,
							     (GtkFileSystemVolume *) source_object,
							     error, async_data->data);
  gdk_threads_leave ();

  if (error)
    g_error_free (error);
883 884
}

885 886 887 888
static void
volume_mount_cb (GObject      *source_object,
		 GAsyncResult *result,
		 gpointer      user_data)
889
{
890 891 892 893 894
  AsyncFuncData *async_data;
  GError *error = NULL;

  g_volume_mount_finish (G_VOLUME (source_object), result, &error);
  async_data = (AsyncFuncData *) user_data;
895

896 897 898 899 900 901 902 903
  gdk_threads_enter ();
  ((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable,
							     (GtkFileSystemVolume *) source_object,
							     error, async_data->data);
  gdk_threads_leave ();

  if (error)
    g_error_free (error);
904 905
}

906
GCancellable *
907 908 909 910 911
_gtk_file_system_mount_volume (GtkFileSystem                    *file_system,
			       GtkFileSystemVolume              *volume,
			       GMountOperation                  *mount_operation,
			       GtkFileSystemVolumeMountCallback  callback,
			       gpointer                          data)
912
{
913 914 915 916 917 918 919 920 921 922 923 924 925 926
  GCancellable *cancellable;
  AsyncFuncData *async_data;
  gboolean handled = FALSE;

  DEBUG ("volume_mount");

  cancellable = g_cancellable_new ();

  async_data = g_new0 (AsyncFuncData, 1);
  async_data->file_system = g_object_ref (file_system);
  async_data->cancellable = g_object_ref (cancellable);

  async_data->callback = callback;
  async_data->data = data;
927

928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
  if (G_IS_DRIVE (volume))
    {
      /* this path happens for drives that are not polled by the OS and where the last media
       * check indicated that no media was available. So the thing to do here is to
       * invoke poll_for_media() on the drive
       */
      g_drive_poll_for_media (G_DRIVE (volume), cancellable, drive_poll_for_media_cb, async_data);
      handled = TRUE;
    }
  else if (G_IS_VOLUME (volume))
    {
      g_volume_mount (G_VOLUME (volume), G_MOUNT_MOUNT_NONE, mount_operation, cancellable, volume_mount_cb, async_data);
      handled = TRUE;
    }

  if (!handled)
    free_async_data (async_data);

  return cancellable;
947 948
}

949 950 951 952
static void
enclosing_volume_mount_cb (GObject      *source_object,
			   GAsyncResult *result,
			   gpointer      user_data)
953
{
954 955 956 957 958 959
  GtkFileSystemVolume *volume;
  AsyncFuncData *async_data;
  GError *error = NULL;

  async_data = (AsyncFuncData *) user_data;
  g_file_mount_enclosing_volume_finish (G_FILE (source_object), result, &error);
960
  volume = _gtk_file_system_get_volume_for_file (async_data->file_system, G_FILE (source_object));
961

962 963 964 965 966
  /* Silently drop G_IO_ERROR_ALREADY_MOUNTED error for gvfs backends without visible mounts. */
  /* Better than doing query_info with additional I/O every time. */
  if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_ALREADY_MOUNTED))
    g_clear_error (&error);

967 968 969 970 971 972 973
  gdk_threads_enter ();
  ((GtkFileSystemVolumeMountCallback) async_data->callback) (async_data->cancellable, volume,
							     error, async_data->data);
  gdk_threads_leave ();

  if (error)
    g_error_free (error);
974 975
}

976
GCancellable *
977 978 979 980 981
_gtk_file_system_mount_enclosing_volume (GtkFileSystem                     *file_system,
					 GFile                             *file,
					 GMountOperation                   *mount_operation,
					 GtkFileSystemVolumeMountCallback   callback,
					 gpointer                           data)
982
{
983 984
  GCancellable *cancellable;
  AsyncFuncData *async_data;
985

986
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
987
  g_return_val_if_fail (G_IS_FILE (file), NULL);
988

989
  DEBUG ("mount_enclosing_volume");
990

991
  cancellable = g_cancellable_new ();
992

993 994 995 996
  async_data = g_new0 (AsyncFuncData, 1);
  async_data->file_system = g_object_ref (file_system);
  async_data->file = g_object_ref (file);
  async_data->cancellable = g_object_ref (cancellable);
997

998 999
  async_data->callback = callback;
  async_data->data = data;
1000

1001 1002 1003 1004 1005 1006 1007
  g_file_mount_enclosing_volume (file,
				 G_MOUNT_MOUNT_NONE,
				 mount_operation,
				 cancellable,
				 enclosing_volume_mount_cb,
				 async_data);
  return cancellable;
1008 1009
}

Owen Taylor's avatar
Owen Taylor committed
1010
gboolean
1011 1012 1013 1014
_gtk_file_system_insert_bookmark (GtkFileSystem  *file_system,
				  GFile          *file,
				  gint            position,
				  GError        **error)
Owen Taylor's avatar
Owen Taylor committed
1015
{
1016 1017 1018 1019 1020
  GtkFileSystemPrivate *priv;
  GSList *bookmarks;
  GtkFileSystemBookmark *bookmark;
  gboolean result = TRUE;
  GFile *bookmarks_file;
Owen Taylor's avatar
Owen Taylor committed
1021

1022 1023
  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
  bookmarks = priv->bookmarks;
1024

1025 1026 1027 1028
  while (bookmarks)
    {
      bookmark = bookmarks->data;
      bookmarks = bookmarks->next;
Owen Taylor's avatar
Owen Taylor committed
1029

1030 1031 1032 1033 1034 1035 1036
      if (g_file_equal (bookmark->file, file))
	{
	  /* File is already in bookmarks */
	  result = FALSE;
	  break;
	}
    }
Owen Taylor's avatar
Owen Taylor committed
1037

1038 1039 1040 1041 1042
  if (!result)
    {
      gchar *uri = g_file_get_uri (file);

      g_set_error (error,
1043 1044
		   GTK_FILE_CHOOSER_ERROR,
		   GTK_FILE_CHOOSER_ERROR_ALREADY_EXISTS,
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
		   "%s already exists in the bookmarks list",
		   uri);

      g_free (uri);

      return FALSE;
    }

  bookmark = g_slice_new0 (GtkFileSystemBookmark);
  bookmark->file = g_object_ref (file);

  priv->bookmarks = g_slist_insert (priv->bookmarks, bookmark, position);
Owen Taylor's avatar
Owen Taylor committed
1057

1058 1059 1060 1061 1062 1063 1064
  bookmarks_file = get_bookmarks_file ();
  save_bookmarks (bookmarks_file, priv->bookmarks);
  g_object_unref (bookmarks_file);

  g_signal_emit (file_system, fs_signals[BOOKMARKS_CHANGED], 0);

  return TRUE;
Owen Taylor's avatar
Owen Taylor committed
1065 1066
}

1067
gboolean
1068 1069 1070
_gtk_file_system_remove_bookmark (GtkFileSystem  *file_system,
				  GFile          *file,
				  GError        **error)
1071
{
1072 1073 1074 1075 1076
  GtkFileSystemPrivate *priv;
  GtkFileSystemBookmark *bookmark;
  GSList *bookmarks;
  gboolean result = FALSE;
  GFile *bookmarks_file;
1077

1078
  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
1079

1080 1081
  if (!priv->bookmarks)
    return FALSE;
Owen Taylor's avatar
Owen Taylor committed
1082

1083
  bookmarks = priv->bookmarks;
1084

1085 1086 1087 1088 1089 1090 1091 1092
  while (bookmarks)
    {
      bookmark = bookmarks->data;

      if (g_file_equal (bookmark->file, file))
	{
	  result = TRUE;
	  priv->bookmarks = g_slist_remove_link (priv->bookmarks, bookmarks);
1093
	  _gtk_file_system_bookmark_free (bookmark);
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	  g_slist_free_1 (bookmarks);
	  break;
	}

      bookmarks = bookmarks->next;
    }

  if (!result)
    {
      gchar *uri = g_file_get_uri (file);

      g_set_error (error,
1106 1107
		   GTK_FILE_CHOOSER_ERROR,
		   GTK_FILE_CHOOSER_ERROR_NONEXISTENT,
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
		   "%s does not exist in the bookmarks list",
		   uri);

      g_free (uri);

      return FALSE;
    }

  bookmarks_file = get_bookmarks_file ();
  save_bookmarks (bookmarks_file, priv->bookmarks);
  g_object_unref (bookmarks_file);

  g_signal_emit (file_system, fs_signals[BOOKMARKS_CHANGED], 0);

  return TRUE;
1123 1124 1125
}

gchar *
1126 1127
_gtk_file_system_get_bookmark_label (GtkFileSystem *file_system,
				     GFile         *file)
1128
{
1129 1130 1131
  GtkFileSystemPrivate *priv;
  GSList *bookmarks;
  gchar *label = NULL;
1132

1133
  DEBUG ("get_bookmark_label");
1134

1135 1136
  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
  bookmarks = priv->bookmarks;
1137

1138 1139 1140
  while (bookmarks)
    {
      GtkFileSystemBookmark *bookmark;
1141

1142 1143
      bookmark = bookmarks->data;
      bookmarks = bookmarks->next;
1144

1145 1146 1147 1148 1149 1150
      if (g_file_equal (file, bookmark->file))
	{
	  label = g_strdup (bookmark->label);
	  break;
	}
    }
1151

1152
  return label;
1153 1154
}

1155
void
1156 1157 1158
_gtk_file_system_set_bookmark_label (GtkFileSystem *file_system,
				     GFile         *file,
				     const gchar   *label)
1159
{
1160 1161 1162 1163 1164 1165
  GtkFileSystemPrivate *priv;
  gboolean changed = FALSE;
  GFile *bookmarks_file;
  GSList *bookmarks;

  DEBUG ("set_bookmark_label");
1166

1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
  bookmarks = priv->bookmarks;

  while (bookmarks)
    {
      GtkFileSystemBookmark *bookmark;

      bookmark = bookmarks->data;
      bookmarks = bookmarks->next;

      if (g_file_equal (file, bookmark->file))
	{
1179
          g_free (bookmark->label);
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	  bookmark->label = g_strdup (label);
	  changed = TRUE;
	  break;
	}
    }

  bookmarks_file = get_bookmarks_file ();
  save_bookmarks (bookmarks_file, priv->bookmarks);
  g_object_unref (bookmarks_file);

  if (changed)
    g_signal_emit_by_name (file_system, "bookmarks-changed", 0);
1192 1193
}

1194
GtkFileSystemVolume *
1195 1196
_gtk_file_system_get_volume_for_file (GtkFileSystem *file_system,
				      GFile         *file)
1197
{
1198 1199 1200 1201
  GtkFileSystemPrivate *priv;
  GMount *mount;

  DEBUG ("get_volume_for_file");
1202

1203 1204 1205 1206 1207 1208 1209
  priv = GTK_FILE_SYSTEM_GET_PRIVATE (file_system);
  mount = g_file_find_enclosing_mount (file, NULL, NULL);

  if (!mount && g_file_is_native (file))
    return (GtkFileSystemVolume *) root_volume_token;

  return (GtkFileSystemVolume *) mount;
1210 1211
}

1212 1213 1214 1215 1216 1217
/* GtkFolder methods */
static void
gtk_folder_set_property (GObject      *object,
			 guint         prop_id,
			 const GValue *value,
			 GParamSpec   *pspec)
1218
{
1219
  GtkFolderPrivate *priv;
1220

1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
  priv = GTK_FOLDER_GET_PRIVATE (object);

  switch (prop_id)
    {
    case PROP_FILE:
      priv->folder_file = g_value_dup_object (value);
      break;
    case PROP_ENUMERATOR:
      priv->enumerator = g_value_dup_object (value);
      break;
    case PROP_ATTRIBUTES:
      priv->attributes = g_value_dup_string (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
1238 1239
}

1240 1241 1242 1243 1244
static void
gtk_folder_get_property (GObject    *object,
			 guint       prop_id,
			 GValue     *value,
			 GParamSpec *pspec)
1245
{
1246
  GtkFolderPrivate *priv;
1247

1248
  priv = GTK_FOLDER_GET_PRIVATE (object);
1249

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
  switch (prop_id)
    {
    case PROP_FILE:
      g_value_set_object (value, priv->folder_file);
      break;
    case PROP_ENUMERATOR:
      g_value_set_object (value, priv->enumerator);
      break;
    case PROP_ATTRIBUTES:
      g_value_set_string (value, priv->attributes);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
1265 1266
}

1267 1268 1269 1270
static void
query_created_file_info_callback (GObject      *source_object,
				  GAsyncResult *result,
				  gpointer      user_data)
1271
{
1272 1273 1274 1275 1276 1277 1278
  GFile *file = G_FILE (source_object);
  GError *error = NULL;
  GFileInfo *info;
  GtkFolder *folder;
  GSList *files;

  info = g_file_query_info_finish (file, result, &error);
1279

1280 1281 1282 1283 1284
  if (error)
    {
      g_error_free (error);
      return;
    }
1285

1286
  folder = GTK_FOLDER (user_data);
1287
  gtk_folder_add_file (folder, file, info);
1288 1289 1290 1291 1292 1293

  files = g_slist_prepend (NULL, file);
  g_signal_emit (folder, folder_signals[FILES_ADDED], 0, files);
  g_slist_free (files);

  g_object_unref (info);
1294 1295
}

1296 1297 1298 1299 1300 1301
static void
directory_monitor_changed (GFileMonitor      *monitor,
			   GFile             *file,
			   GFile             *other_file,
			   GFileMonitorEvent  event,
			   gpointer           data)
Owen Taylor's avatar
Owen Taylor committed
1302
{
1303 1304 1305
  GtkFolderPrivate *priv;
  GtkFolder *folder;
  GSList *files;
Owen Taylor's avatar
Owen Taylor committed
1306

1307 1308 1309 1310 1311 1312 1313
  folder = GTK_FOLDER (data);
  priv = GTK_FOLDER_GET_PRIVATE (folder);
  files = g_slist_prepend (NULL, file);

  gdk_threads_enter ();

  switch (event)
Owen Taylor's avatar
Owen Taylor committed
1314
    {
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
    case G_FILE_MONITOR_EVENT_CREATED:
      g_file_query_info_async (file,
			       priv->attributes,
			       G_FILE_QUERY_INFO_NONE,
			       G_PRIORITY_DEFAULT,
			       priv->cancellable,
			       query_created_file_info_callback,
			       folder);
      break;
    case G_FILE_MONITOR_EVENT_DELETED:
      if (g_file_equal (file, priv->folder_file))
	g_signal_emit (folder, folder_signals[DELETED], 0);
      else
	g_signal_emit (folder, folder_signals[FILES_REMOVED], 0, files);
      break;
    default:
      break;
Owen Taylor's avatar
Owen Taylor committed
1332 1333
    }

1334 1335 1336
  gdk_threads_leave ();

  g_slist_free (files);
Owen Taylor's avatar
Owen Taylor committed
1337 1338 1339
}

static void
1340 1341 1342
enumerator_files_callback (GObject      *source_object,
			   GAsyncResult *result,
			   gpointer      user_data)
Owen Taylor's avatar
Owen Taylor committed
1343
{
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
  GFileEnumerator *enumerator;
  GtkFolderPrivate *priv;
  GtkFolder *folder;
  GError *error = NULL;
  GSList *files = NULL;
  GList *file_infos, *f;

  enumerator = G_FILE_ENUMERATOR (source_object);
  file_infos = g_file_enumerator_next_files_finish (enumerator, result, &error);

  if (error)
    {
1356 1357 1358
      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
        g_warning ("%s", error->message);

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
      g_error_free (error);
      return;
    }

  folder = GTK_FOLDER (user_data);
  priv = GTK_FOLDER_GET_PRIVATE (folder);

  if (!file_infos)
    {
      g_file_enumerator_close_async (enumerator,
				     G_PRIORITY_DEFAULT,
				     NULL, NULL, NULL);

1372
      gtk_folder_set_finished_loading (folder, TRUE);
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
      return;
    }

  g_file_enumerator_next_files_async (enumerator, FILES_PER_QUERY,
				      G_PRIORITY_DEFAULT,
				      priv->cancellable,
				      enumerator_files_callback,
				      folder);

  for (f = file_infos; f; f = f->next)
Owen Taylor's avatar
Owen Taylor committed
1383
    {
1384 1385 1386 1387 1388
      GFileInfo *info;
      GFile *child_file;

      info = f->data;
      child_file = g_file_get_child (priv->folder_file, g_file_info_get_name (info));
1389
      gtk_folder_add_file (folder, child_file, info);
1390
      files = g_slist_prepend (files, child_file);
Owen Taylor's avatar
Owen Taylor committed
1391
    }
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401

  gdk_threads_enter ();
  g_signal_emit (folder, folder_signals[FILES_ADDED], 0, files);
  gdk_threads_leave ();

  g_list_foreach (file_infos, (GFunc) g_object_unref, NULL);
  g_list_free (file_infos);

  g_slist_foreach (files, (GFunc) g_object_unref, NULL);
  g_slist_free (files);
Owen Taylor's avatar
Owen Taylor committed
1402 1403
}

1404 1405
static void
gtk_folder_constructed (GObject *object)
Owen Taylor's avatar
Owen Taylor committed
1406
{
1407 1408
  GtkFolderPrivate *priv;
  GError *error = NULL;
Owen Taylor's avatar
Owen Taylor committed
1409

1410 1411
  priv = GTK_FOLDER_GET_PRIVATE (object);
  priv->directory_monitor = g_file_monitor_directory (priv->folder_file, G_FILE_MONITOR_NONE, NULL, &error);
Owen Taylor's avatar
Owen Taylor committed
1412

1413
  if (error)
1414 1415 1416 1417
    {
      g_warning ("%s", error->message);
      g_error_free (error);
    }
1418
  else
1419 1420
    g_signal_connect (priv->directory_monitor, "changed",
		      G_CALLBACK (directory_monitor_changed), object);
Owen Taylor's avatar
Owen Taylor committed
1421

1422 1423 1424 1425 1426 1427 1428 1429 1430
  g_file_enumerator_next_files_async (priv->enumerator,
				      FILES_PER_QUERY,
				      G_PRIORITY_DEFAULT,
				      priv->cancellable,
				      enumerator_files_callback,
				      object);
  /* This isn't needed anymore */
  g_object_unref (priv->enumerator);
  priv->enumerator = NULL;
Owen Taylor's avatar
Owen Taylor committed
1431 1432
}

1433 1434
static void
gtk_folder_finalize (GObject *object)
Owen Taylor's avatar
Owen Taylor committed
1435
{
1436
  GtkFolderPrivate *priv;
Owen Taylor's avatar
Owen Taylor committed
1437

1438
  priv = GTK_FOLDER_GET_PRIVATE (object);
1439

1440
  g_hash_table_unref (priv->children);
Matthias Clasen's avatar
Matthias Clasen committed
1441

1442 1443
  if (priv->folder_file)
    g_object_unref (priv->folder_file);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1444

1445 1446
  if (priv->directory_monitor)
    g_object_unref (priv->directory_monitor);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1447

1448 1449 1450
  g_cancellable_cancel (priv->cancellable);
  g_object_unref (priv->cancellable);
  g_free (priv->attributes);
1451

1452
  G_OBJECT_CLASS (_gtk_folder_parent_class)->finalize (object);
1453
}
1454

1455
static void
1456
_gtk_folder_class_init (GtkFolderClass *class)
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 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
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  object_class->set_property = gtk_folder_set_property;
  object_class->get_property = gtk_folder_get_property;
  object_class->constructed = gtk_folder_constructed;
  object_class->finalize = gtk_folder_finalize;

  g_object_class_install_property (object_class,
				   PROP_FILE,
				   g_param_spec_object ("file",
							"File",
							"GFile for the folder",
							G_TYPE_FILE,
							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (object_class,
				   PROP_ENUMERATOR,
				   g_param_spec_object ("enumerator",
							"Enumerator",
							"GFileEnumerator to list files",
							G_TYPE_FILE_ENUMERATOR,
							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (object_class,
				   PROP_ATTRIBUTES,
				   g_param_spec_string ("attributes",
							"Attributes",
							"Attributes to query for",
							NULL,
							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
  folder_signals[FILES_ADDED] =
    g_signal_new ("files-added",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFolderClass, files_added),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__POINTER,
		  G_TYPE_NONE, 1, G_TYPE_POINTER);
  folder_signals[FILES_REMOVED] =
    g_signal_new ("files-removed",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFolderClass, files_removed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__POINTER,
		  G_TYPE_NONE, 1, G_TYPE_POINTER);
  folder_signals[FILES_CHANGED] =
    g_signal_new ("files-changed",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFolderClass, files_changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__POINTER,
		  G_TYPE_NONE, 1, G_TYPE_POINTER);
  folder_signals[FINISHED_LOADING] =
    g_signal_new ("finished-loading",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFolderClass, finished_loading),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  folder_signals[DELETED] =
    g_signal_new ("deleted",
		  G_TYPE_FROM_CLASS (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkFolderClass, deleted),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  g_type_class_add_private (object_class, sizeof (GtkFolderPrivate));
1528 1529 1530
}

static void
1531
_gtk_folder_init (GtkFolder *folder)
1532
{
1533 1534 1535 1536 1537 1538 1539 1540 1541
  GtkFolderPrivate *priv;

  priv = GTK_FOLDER_GET_PRIVATE (folder);

  priv->children = g_hash_table_new_full (g_file_hash,
					  (GEqualFunc) g_file_equal,
					  (GDestroyNotify) g_object_unref,
					  (GDestroyNotify) g_object_unref);
  priv->cancellable = g_cancellable_new ();
1542 1543
}

1544
static void
1545 1546
gtk_folder_set_finished_loading (GtkFolder *folder,
				 gboolean   finished_loading)
1547
{
1548
  GtkFolderPrivate *priv;
1549

1550 1551
  priv = GTK_FOLDER_GET_PRIVATE (folder);
  priv->finished_loading = (finished_loading == TRUE);
1552

1553 1554 1555
  gdk_threads_enter ();
  g_signal_emit (folder, folder_signals[FINISHED_LOADING], 0);
  gdk_threads_leave ();
1556 1557
}

1558
static void
1559 1560 1561
gtk_folder_add_file (GtkFolder *folder,
		     GFile     *file,
		     GFileInfo *info)
1562
{
1563
  GtkFolderPrivate *priv;
1564

1565
  priv = GTK_FOLDER_GET_PRIVATE (folder);
1566

1567 1568 1569 1570
  g_hash_table_insert (priv->children,
		       g_object_ref (file),
		       g_object_ref (info));
}
1571

1572
GSList *
1573
_gtk_folder_list_children (GtkFolder *folder)
1574 1575 1576 1577
{
  GtkFolderPrivate *priv;
  GList *files, *elem;
  GSList *children = NULL;
1578

1579 1580 1581
  priv = GTK_FOLDER_GET_PRIVATE (folder);
  files = g_hash_table_get_keys (priv->children);
  children = NULL;
1582

1583 1584 1585 1586
  for (elem = files; elem; elem = elem->next)
    children = g_slist_prepend (children, g_object_ref (elem->data));

  g_list_free (files);
1587

1588
  return children;
1589 1590
}