gtkfilesystem.c 24.1 KB
Newer Older
Owen Taylor's avatar
Owen Taylor committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* GTK - The GIMP Toolkit
 * gtkfilesystem.c: Abstract file system interfaces
 * Copyright (C) 2003, Red Hat, Inc.
 *
 * This library 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.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

Owen Taylor's avatar
Owen Taylor committed
21

Owen Taylor's avatar
Owen Taylor committed
22
#include "gtkfilesystem.h"
23
#include "gtkicontheme.h"
Owen Taylor's avatar
Owen Taylor committed
24

25 26
#include <string.h>

Owen Taylor's avatar
Owen Taylor committed
27 28 29 30 31
struct _GtkFileInfo
{
  GtkFileTime modification_time;
  gint64 size;
  gchar *display_name;
Owen Taylor's avatar
Owen Taylor committed
32
  gchar *display_key;
Owen Taylor's avatar
Owen Taylor committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  gchar *mime_type;
  guint is_folder : 1;
  guint is_hidden : 1;
};

static void gtk_file_system_base_init (gpointer g_class);
static void gtk_file_folder_base_init (gpointer g_class);

GQuark
gtk_file_system_error_quark (void)
{
  static GQuark quark = 0;
  if (quark == 0)
    quark = g_quark_from_static_string ("gtk-file-system-error-quark");
  return quark;
}

/*****************************************
 *             GtkFileInfo               *
 *****************************************/
GType
gtk_file_info_get_type (void)
{
  static GType our_type = 0;
  
  if (our_type == 0)
    our_type = g_boxed_type_register_static ("GtkFileInfo",
					     (GBoxedCopyFunc) gtk_file_info_copy,
					     (GBoxedFreeFunc) gtk_file_info_free);

  return our_type;
}

GtkFileInfo *
gtk_file_info_new  (void)
{
  GtkFileInfo *info;
  
  info = g_new0 (GtkFileInfo, 1);

  return info;
}

GtkFileInfo *
gtk_file_info_copy (GtkFileInfo *info)
{
  GtkFileInfo *new_info;

  g_return_val_if_fail (info != NULL, NULL);

  new_info = g_memdup (info, sizeof (GtkFileInfo));
  if (new_info->display_name)
    new_info->display_name = g_strdup (new_info->display_name);
  if (new_info->mime_type)
    new_info->mime_type = g_strdup (new_info->mime_type);

  return new_info;
}

void
gtk_file_info_free (GtkFileInfo *info)
{
  g_return_if_fail (info != NULL);

  if (info->display_name)
    g_free (info->display_name);
  if (info->mime_type)
    g_free (info->mime_type);
101 102
  if (info->display_key)
    g_free (info->display_key);
103 104

  g_free (info);
Owen Taylor's avatar
Owen Taylor committed
105 106 107 108 109 110 111 112 113 114
}

G_CONST_RETURN gchar *
gtk_file_info_get_display_name (const GtkFileInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);
  
  return info->display_name;
}

Owen Taylor's avatar
Owen Taylor committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/**
 * gtk_file_info_get_display_key:
 * @info: a #GtkFileInfo
 * 
 * Returns results of g_utf8_collate_key() on the display name
 * for @info. This is useful when sorting a bunch of #GtkFileInfo
 * structures since the collate key will be only computed once.
 * 
 * Return value: The collate key for the display name, or %NULL
 *   if the display name hasn't been set.
 **/
G_CONST_RETURN gchar *
gtk_file_info_get_display_key (const GtkFileInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);

  if (!info->display_key && info->display_name)
    {
      /* Since info->display_key is only a cache, we cast off the const
       */
      ((GtkFileInfo *)info)->display_key = g_utf8_collate_key (info->display_name, -1);
    }
	
  return info->display_key;
}

Owen Taylor's avatar
Owen Taylor committed
141 142 143 144 145 146 147 148
void
gtk_file_info_set_display_name (GtkFileInfo *info,
				const gchar *display_name)
{
  g_return_if_fail (info != NULL);

  if (info->display_name)
    g_free (info->display_name);
Owen Taylor's avatar
Owen Taylor committed
149 150 151 152 153
  if (info->display_key)
    {
      g_free (info->display_key);
      info->display_key = NULL;
    }
Owen Taylor's avatar
Owen Taylor committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241

  info->display_name = g_strdup (display_name);
}

gboolean
gtk_file_info_get_is_folder (const GtkFileInfo *info)
{
  g_return_val_if_fail (info != NULL, FALSE);
  
  return info->is_folder;
}

void
gtk_file_info_set_is_folder (GtkFileInfo *info,
			     gboolean     is_folder)
{
  g_return_if_fail (info != NULL);

  info->is_folder = is_folder != FALSE;
}

gboolean
gtk_file_info_get_is_hidden (const GtkFileInfo *info)
{
  g_return_val_if_fail (info != NULL, FALSE);
  
  return info->is_hidden;
}

void
gtk_file_info_set_is_hidden (GtkFileInfo *info,
			     gboolean     is_hidden)
{
  g_return_if_fail (info != NULL);

  info->is_hidden = is_hidden != FALSE;
}

G_CONST_RETURN gchar *
gtk_file_info_get_mime_type (const GtkFileInfo *info)
{
  g_return_val_if_fail (info != NULL, NULL);
  
  return info->mime_type;
}

void
gtk_file_info_set_mime_type (GtkFileInfo *info,
			     const gchar *mime_type)
{
  g_return_if_fail (info != NULL);
  
  if (info->mime_type)
    g_free (info->mime_type);

  info->mime_type = g_strdup (mime_type);
}

GtkFileTime
gtk_file_info_get_modification_time (const GtkFileInfo *info)
{
  g_return_val_if_fail (info != NULL, 0);

  return info->modification_time;
}

void
gtk_file_info_set_modification_time (GtkFileInfo *info,
				     GtkFileTime  modification_time)
{
  g_return_if_fail (info != NULL);
  
  info->modification_time = modification_time;
}

gint64
gtk_file_info_get_size (const GtkFileInfo *info)
{
  g_return_val_if_fail (info != NULL, 0);
  
  return info->size;
}

void
gtk_file_info_set_size (GtkFileInfo *info,
			gint64       size)
{
  g_return_if_fail (info != NULL);
242
  g_return_if_fail (size >= 0);
Owen Taylor's avatar
Owen Taylor committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
  
  info->size = size;
}


/*****************************************
 *             GtkFileSystem             *
 *****************************************/
GType
gtk_file_system_get_type (void)
{
  static GType file_system_type = 0;

  if (!file_system_type)
    {
      static const GTypeInfo file_system_info =
      {
	sizeof (GtkFileSystemIface),  /* class_size */
	gtk_file_system_base_init,    /* base_init */
	NULL,                         /* base_finalize */
      };

      file_system_type = g_type_register_static (G_TYPE_INTERFACE,
						 "GtkFileSystem",
						 &file_system_info, 0);
268 269

      g_type_interface_add_prerequisite (file_system_type, G_TYPE_OBJECT);
Owen Taylor's avatar
Owen Taylor committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283
    }

  return file_system_type;
}

static void
gtk_file_system_base_init (gpointer g_class)
{
  static gboolean initialized = FALSE;
  
  if (!initialized)
    {
      GType iface_type = G_TYPE_FROM_INTERFACE (g_class);

284 285 286 287 288 289 290
      g_signal_new ("volumes-changed",
		    iface_type,
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (GtkFileSystemIface, volumes_changed),
		    NULL, NULL,
		    g_cclosure_marshal_VOID__VOID,
		    G_TYPE_NONE, 0);
291 292 293 294 295 296 297
      g_signal_new ("bookmarks-changed",
		    iface_type,
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (GtkFileSystemIface, bookmarks_changed),
		    NULL, NULL,
		    g_cclosure_marshal_VOID__VOID,
		    G_TYPE_NONE, 0);
Owen Taylor's avatar
Owen Taylor committed
298 299 300 301 302

      initialized = TRUE;
    }
}

303 304 305 306 307 308 309 310
GSList *
gtk_file_system_list_volumes (GtkFileSystem  *file_system)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->list_volumes (file_system);
}

Owen Taylor's avatar
Owen Taylor committed
311
GtkFileFolder *
312 313 314 315
gtk_file_system_get_folder (GtkFileSystem     *file_system,
			    const GtkFilePath *path,
			    GtkFileInfoType    types,
			    GError           **error)
Owen Taylor's avatar
Owen Taylor committed
316 317
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
318
  g_return_val_if_fail (path != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
319 320
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

321
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->get_folder (file_system, path, types, error);
Owen Taylor's avatar
Owen Taylor committed
322 323 324
}

gboolean
325 326 327
gtk_file_system_create_folder(GtkFileSystem     *file_system,
			      const GtkFilePath *path,
			      GError           **error)
Owen Taylor's avatar
Owen Taylor committed
328 329
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE);
330
  g_return_val_if_fail (path != NULL, FALSE);
Owen Taylor's avatar
Owen Taylor committed
331 332
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

333
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->create_folder (file_system, path, error);
Owen Taylor's avatar
Owen Taylor committed
334 335
}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/**
 * gtk_file_system_get_volume_for_path:
 * @file_system: a #GtkFileSystem
 * @path: a #GtkFilePath
 * 
 * Queries the file system volume that corresponds to a specific path.
 * 
 * Return value: the #GtkFileSystemVolume that corresponds to the specified
 * @path.  You should free this value with gtk_file_system_volume_free().
 **/
GtkFileSystemVolume *
gtk_file_system_get_volume_for_path (GtkFileSystem     *file_system,
				     const GtkFilePath *path)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (path != NULL, NULL);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->get_volume_for_path (file_system, path);
}

356 357 358 359 360 361 362 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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
/**
 * gtk_file_system_volume_free:
 * @file_system: a #GtkFileSystem
 * @volume: a #GtkFileSystemVolume
 * 
 * Frees a #GtkFileSystemVolume structure as returned by
 * gtk_file_system_list_volumes().
 **/
void
gtk_file_system_volume_free (GtkFileSystem       *file_system,
			     GtkFileSystemVolume *volume)
{
  g_return_if_fail (GTK_IS_FILE_SYSTEM (file_system));
  g_return_if_fail (volume != NULL);

  GTK_FILE_SYSTEM_GET_IFACE (file_system)->volume_free (file_system, volume);
}

/**
 * gtk_file_system_volume_get_base_path:
 * @file_system: a #GtkFileSystem
 * @volume: a #GtkFileSystemVolume
 * 
 * Queries the base path for a volume.  For example, a CD-ROM device may yield a
 * path of "/mnt/cdrom".
 * 
 * Return value: a #GtkFilePath with the base mount path of the specified
 * @volume.
 **/
GtkFilePath *
gtk_file_system_volume_get_base_path (GtkFileSystem       *file_system,
				      GtkFileSystemVolume *volume)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (volume != NULL, NULL);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->volume_get_base_path (file_system, volume);
}

/**
 * gtk_file_system_volume_get_is_mounted:
 * @file_system: a #GtkFileSystem
 * @volume: a #GtkFileSystemVolume
 * 
 * Queries whether a #GtkFileSystemVolume is mounted or not.  If it is not, it
 * can be mounted with gtk_file_system_volume_mount().
 * 
 * Return value: TRUE if the @volume is mounted, FALSE otherwise.
 **/
gboolean
gtk_file_system_volume_get_is_mounted (GtkFileSystem       *file_system,
				       GtkFileSystemVolume *volume)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE);
  g_return_val_if_fail (volume != NULL, FALSE);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->volume_get_is_mounted (file_system, volume);
}

/**
 * gtk_file_system_volume_mount:
 * @file_system: a #GtkFileSystem
 * @volume: a #GtkFileSystemVolume
 * @error: location to store error, or %NULL
 * 
 * Tries to mount an unmounted volume.  This may cause the "volumes-changed"
 * signal in the @file_system to be emitted.
 * 
 * Return value: TRUE if the @volume was mounted successfully, FALSE otherwise.
 **/
gboolean
gtk_file_system_volume_mount (GtkFileSystem        *file_system, 
			      GtkFileSystemVolume  *volume,
			      GError              **error)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE);
  g_return_val_if_fail (volume != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->volume_mount (file_system, volume, error);
}

/**
 * gtk_file_system_volume_get_display_name:
 * @file_system: a #GtkFileSystem
 * @volume: a #GtkFileSystemVolume
 * 
 * Queries the human-readable name for a @volume.  This string can be displayed
 * in a list of volumes that can be accessed, for example.
 * 
 * Return value: A string with the human-readable name for a #GtkFileSystemVolume.
 **/
char *
gtk_file_system_volume_get_display_name (GtkFileSystem        *file_system, 
					 GtkFileSystemVolume  *volume)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (volume != NULL, NULL);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->volume_get_display_name (file_system, volume);
}

/**
 * gtk_file_system_volume_render_icon:
 * @file_system: a #GtkFileSystem
 * @volume: a #GtkFileSystemVolume
 * @widget: Reference widget to render icons.
 * @pixel_size: Size of the icon.
 * @error: location to store error, or %NULL
 * 
 * Renders an icon suitable for a file #GtkFileSystemVolume.
 * 
 * Return value: A #GdkPixbuf containing an icon, or NULL if the icon could not
 * be rendered.  In the latter case, the @error value will be set as
 * appropriate.
 **/
GdkPixbuf *
gtk_file_system_volume_render_icon (GtkFileSystem        *file_system,
				    GtkFileSystemVolume  *volume,
				    GtkWidget            *widget,
				    gint                  pixel_size,
				    GError              **error)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (volume != NULL, NULL);
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
  g_return_val_if_fail (pixel_size > 0, NULL);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
483
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
484 485 486 487 488 489 490 491

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->volume_render_icon (file_system,
								      volume,
								      widget,
								      pixel_size,
								      error);
}

492 493 494 495 496 497 498 499 500 501 502 503 504 505
/**
 * gtk_file_system_get_parent:
 * @file_system: a #GtkFileSystem
 * @path: base path name
 * @parent: location to store parent path name
 * @error: location to store error, or %NULL
 * 
 * Gets the name of the parent folder of a file.
 * 
 * Return value: TRUE if the operation was successful; note that in this case @parent
 * can be returned as %NULL if the base @path has no parent folder (i.e. if it is
 * already a file system root).  If the operation fails, this function returns FALSE
 * and sets the @error value if it is specified.
 **/
Owen Taylor's avatar
Owen Taylor committed
506
gboolean
507 508 509 510
gtk_file_system_get_parent (GtkFileSystem     *file_system,
			    const GtkFilePath *path,
			    GtkFilePath      **parent,
			    GError           **error)
Owen Taylor's avatar
Owen Taylor committed
511
{
512
  GtkFilePath *tmp_parent = NULL;
Owen Taylor's avatar
Owen Taylor committed
513 514 515
  gboolean result;
  
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE);
516
  g_return_val_if_fail (path != NULL, FALSE);
Owen Taylor's avatar
Owen Taylor committed
517 518
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

519
  result = GTK_FILE_SYSTEM_GET_IFACE (file_system)->get_parent (file_system, path, &tmp_parent, error);
Owen Taylor's avatar
Owen Taylor committed
520 521 522 523
  g_assert (result || tmp_parent == NULL);

  if (parent)
    *parent = tmp_parent;
524
  else
525
    gtk_file_path_free (tmp_parent);
526
  
Owen Taylor's avatar
Owen Taylor committed
527 528 529
  return result;
}

530 531 532 533 534
GtkFilePath *
gtk_file_system_make_path (GtkFileSystem    *file_system,
			  const GtkFilePath *base_path,
			  const gchar       *display_name,
			  GError           **error)
Owen Taylor's avatar
Owen Taylor committed
535 536
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
537
  g_return_val_if_fail (base_path != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
538
  g_return_val_if_fail (display_name != NULL, NULL);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
539
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
540

541
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->make_path (file_system, base_path, display_name, error);
Owen Taylor's avatar
Owen Taylor committed
542 543
}

544 545 546
/**
 * gtk_file_system_parse:
 * @file_system: a #GtkFileSystem
547
 * @base_path: reference folder with respect to which relative
548 549 550 551 552 553 554
 *            paths should be interpreted.
 * @str: the string to parse
 * @folder: location to store folder portion of result, or %NULL
 * @file_part: location to store file portion of result, or %NULL
 * @error: location to store error, or %NULL
 * 
 * Given a string entered by a user, parse it (possibly using
555 556
 * heuristics) into a folder path and a UTF-8 encoded
 * filename part. (Suitable for passing to gtk_file_system_make_path())
557 558 559 560 561 562 563 564 565 566 567
 *
 * Note that the returned filename point may point to a subfolder
 * of the returned folder. Adding a trailing path separator is needed
 * to enforce the interpretation as a folder name.
 *
 * If parsing fails because the syntax of @str is not understood,
 * and error of type GTK_FILE_SYSTEM_ERROR_BAD_FILENAME will
 * be set in @error and %FALSE returned.
 *
 * If parsing fails because a path was encountered that doesn't
 * exist on the filesystem, then an error of type
568
 * %GTK_FILE_SYSTEM_ERROR_NONEXISTENT will be set in @error
569 570 571 572 573 574 575
 * and %FALSE returned. (This only applies to parsing relative paths,
 * not to interpretation of @file_part. No check is made as
 * to whether @file_part exists.)
 *
 * Return value: %TRUE if the parsing succeeds, otherwise, %FALSE.
 **/
gboolean
576 577 578 579 580 581 582 583
gtk_file_system_parse (GtkFileSystem     *file_system,
		       const GtkFilePath *base_path,
		       const gchar       *str,
		       GtkFilePath      **folder,
		       gchar            **file_part,
		       GError           **error)
{
  GtkFilePath *tmp_folder = NULL;
584 585 586 587
  gchar *tmp_file_part = NULL;
  gboolean result;

  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE);
588
  g_return_val_if_fail (base_path != NULL, FALSE);
589 590 591
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);


592 593 594
  result = GTK_FILE_SYSTEM_GET_IFACE (file_system)->parse (file_system, base_path, str,
							   &tmp_folder, &tmp_file_part,
							   error);
595 596 597 598 599
  g_assert (result || (tmp_folder == NULL && tmp_file_part == NULL));

  if (folder)
    *folder = tmp_folder;
  else
600
    gtk_file_path_free (tmp_folder);
601 602 603 604 605 606 607 608

  if (file_part)
    *file_part = tmp_file_part;
  else
    g_free (tmp_file_part);

  return result;
}
Owen Taylor's avatar
Owen Taylor committed
609

610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

gchar *
gtk_file_system_path_to_uri (GtkFileSystem     *file_system,
			     const GtkFilePath *path)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (path != NULL, NULL);
  
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->path_to_uri (file_system, path);
}

gchar *
gtk_file_system_path_to_filename (GtkFileSystem     *file_system,
				  const GtkFilePath *path)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (path != NULL, NULL);
  
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->path_to_filename (file_system, path);
}

GtkFilePath *
gtk_file_system_uri_to_path (GtkFileSystem *file_system,
			     const gchar    *uri)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (uri != NULL, NULL);
  
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->uri_to_path (file_system, uri);
}

GtkFilePath *
gtk_file_system_filename_to_path (GtkFileSystem *file_system,
				  const gchar   *filename)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (filename != NULL, NULL);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->filename_to_path (file_system, filename);
}

651 652 653 654 655 656 657 658 659
GdkPixbuf *
gtk_file_system_render_icon (GtkFileSystem      *file_system,
			     const GtkFilePath  *path,
			     GtkWidget          *widget,
			     gint                pixel_size,
			     GError            **error)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);
  g_return_val_if_fail (path != NULL, NULL);
660
  g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
661 662 663 664 665
  g_return_val_if_fail (pixel_size > 0, NULL);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->render_icon (file_system, path, widget, pixel_size, error);
}

666
/**
667 668 669 670
 * gtk_file_system_add_bookmark:
 * @file_system: a #GtkFileSystem
 * @bookmark: path of the bookmark to add
 * @error: location to store error, or %NULL
671
 * 
672 673
 * Adds a bookmark folder to the user's bookmarks list.  If the operation succeeds,
 * the "bookmarks_changed" signal will be emitted.
674
 * 
675 676
 * Return value: TRUE if the operation succeeds, FALSE otherwise.  In the latter case,
 * the @error value will be set.
677 678
 **/
gboolean
679 680 681
gtk_file_system_add_bookmark (GtkFileSystem     *file_system,
			      const GtkFilePath *path,
			      GError           **error)
682 683
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE);
684
  g_return_val_if_fail (path != NULL, FALSE);
685

686
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->add_bookmark (file_system, path, error);
687 688 689
}

/**
690
 * gtk_file_system_remove_bookmark:
691
 * @file_system: a #GtkFileSystem
692 693
 * @bookmark: path of the bookmark to remove
 * @error: location to store error, or %NULL
694
 * 
695 696 697 698 699
 * Removes a bookmark folder from the user's bookmarks list.  If the operation
 * succeeds, the "bookmarks_changed" signal will be emitted.
 * 
 * Return value: TRUE if the operation succeeds, FALSE otherwise.  In the latter
 * case, the @error value will be set.
700
 **/
701 702 703 704
gboolean
gtk_file_system_remove_bookmark (GtkFileSystem     *file_system,
				 const GtkFilePath *path,
				 GError           **error)
705
{
706 707
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), FALSE);
  g_return_val_if_fail (path != NULL, FALSE);
708

709
  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->remove_bookmark (file_system, path, error);
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
}

/**
 * gtk_file_system_list_bookmarks:
 * @file_system: a #GtkFileSystem
 * 
 * Queries the list of bookmarks in the file system.
 * 
 * Return value: A list of #GtkFilePath, or NULL if there are no configured
 * bookmarks.  You should use gtk_file_paths_free() to free this list.
 *
 * See also: gtk_file_system_get_supports_bookmarks()
 **/
GSList *
gtk_file_system_list_bookmarks (GtkFileSystem *file_system)
{
  g_return_val_if_fail (GTK_IS_FILE_SYSTEM (file_system), NULL);

  return GTK_FILE_SYSTEM_GET_IFACE (file_system)->list_bookmarks (file_system);
}

Owen Taylor's avatar
Owen Taylor committed
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
/*****************************************
 *             GtkFileFolder             *
 *****************************************/
GType
gtk_file_folder_get_type (void)
{
  static GType file_folder_type = 0;

  if (!file_folder_type)
    {
      static const GTypeInfo file_folder_info =
      {
	sizeof (GtkFileFolderIface),  /* class_size */
	gtk_file_folder_base_init,    /* base_init */
	NULL,                         /* base_finalize */
      };

      file_folder_type = g_type_register_static (G_TYPE_INTERFACE,
						 "GtkFileFolder",
						 &file_folder_info, 0);
751 752
      
      g_type_interface_add_prerequisite (file_folder_type, G_TYPE_OBJECT);
Owen Taylor's avatar
Owen Taylor committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
    }

  return file_folder_type;
}

static void
gtk_file_folder_base_init (gpointer g_class)
{
  static gboolean initialized = FALSE;
  
  if (!initialized)
    {
      GType iface_type = G_TYPE_FROM_INTERFACE (g_class);

      g_signal_new ("deleted",
		    iface_type,
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (GtkFileFolderIface, deleted),
		    NULL, NULL,
		    g_cclosure_marshal_VOID__VOID,
		    G_TYPE_NONE, 0);
774
      g_signal_new ("files-added",
Owen Taylor's avatar
Owen Taylor committed
775 776
		    iface_type,
		    G_SIGNAL_RUN_LAST,
Owen Taylor's avatar
Owen Taylor committed
777
		    G_STRUCT_OFFSET (GtkFileFolderIface, files_added),
Owen Taylor's avatar
Owen Taylor committed
778
		    NULL, NULL,
Owen Taylor's avatar
Owen Taylor committed
779 780 781
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE, 1,
		    G_TYPE_POINTER);
782
      g_signal_new ("files-changed",
Owen Taylor's avatar
Owen Taylor committed
783 784
		    iface_type,
		    G_SIGNAL_RUN_LAST,
785
		    G_STRUCT_OFFSET (GtkFileFolderIface, files_changed),
Owen Taylor's avatar
Owen Taylor committed
786
		    NULL, NULL,
787
		    g_cclosure_marshal_VOID__POINTER,
Owen Taylor's avatar
Owen Taylor committed
788
		    G_TYPE_NONE, 1,
789
		    G_TYPE_POINTER);
790
      g_signal_new ("files-removed",
Owen Taylor's avatar
Owen Taylor committed
791 792
		    iface_type,
		    G_SIGNAL_RUN_LAST,
793
		    G_STRUCT_OFFSET (GtkFileFolderIface, files_removed),
Owen Taylor's avatar
Owen Taylor committed
794
		    NULL, NULL,
795
		    g_cclosure_marshal_VOID__POINTER,
Owen Taylor's avatar
Owen Taylor committed
796
		    G_TYPE_NONE, 1,
797
		    G_TYPE_POINTER);
Owen Taylor's avatar
Owen Taylor committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818

      initialized = TRUE;
    }
}

gboolean
gtk_file_folder_list_children (GtkFileFolder    *folder,
			       GSList          **children,
			       GError          **error)
{
  gboolean result;
  GSList *tmp_children = NULL;
  
  g_return_val_if_fail (GTK_IS_FILE_FOLDER (folder), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  result = GTK_FILE_FOLDER_GET_IFACE (folder)->list_children (folder, &tmp_children, error);
  g_assert (result || tmp_children == NULL);

  if (children)
    *children = tmp_children;
819
  else
820
    gtk_file_paths_free (tmp_children);
Owen Taylor's avatar
Owen Taylor committed
821 822 823 824 825

  return result;
}

GtkFileInfo *
826 827 828
gtk_file_folder_get_info (GtkFileFolder     *folder,
			  const GtkFilePath *path,
			  GError           **error)
Owen Taylor's avatar
Owen Taylor committed
829 830
{
  g_return_val_if_fail (GTK_IS_FILE_FOLDER (folder), NULL);
831
  g_return_val_if_fail (path != NULL, NULL);
Owen Taylor's avatar
Owen Taylor committed
832 833
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

834 835 836 837 838 839 840 841 842
  return GTK_FILE_FOLDER_GET_IFACE (folder)->get_info (folder, path, error);
}

GSList *
gtk_file_paths_sort (GSList *paths)
{
  return g_slist_sort (paths, (GCompareFunc)strcmp);
}

843 844
/**
 * gtk_file_paths_copy:
845
 * @paths: A #GSList of #GtkFilePath structures.
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
 * 
 * Copies a list of #GtkFilePath structures.
 * 
 * Return value: A copy of @paths.  Since the contents of the list are copied as
 * well, you should use gtk_file_paths_free() to free the result.
 **/
GSList *
gtk_file_paths_copy (GSList *paths)
{
  GSList *head, *tail, *l;

  head = tail = NULL;

  for (l = paths; l; l = l->next)
    {
      GtkFilePath *path;
      GSList *node;

      path = l->data;
      node = g_slist_alloc ();

      if (tail)
	tail->next = node;
      else
	head = node;

      node->data = gtk_file_path_copy (path);
      tail = node;
    }

  return head;
}

879 880 881 882 883 884 885 886 887
void
gtk_file_paths_free (GSList *paths)
{
  GSList *tmp_list;

  for (tmp_list = paths; tmp_list; tmp_list = tmp_list->next)
    gtk_file_path_free (tmp_list->data);

  g_slist_free (paths);
Owen Taylor's avatar
Owen Taylor committed
888
}