gtkfilechooser.c 61.9 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
 * gtkfilechooser.c: Abstract interface for file selector GUIs
 * 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.
 */

21
#include <config.h>
Owen Taylor's avatar
Owen Taylor committed
22
#include "gtkfilechooser.h"
23
#include "gtkfilechooserprivate.h"
24
#include "gtkfilesystem.h"
25
#include "gtkintl.h"
26
#include "gtktypebuiltins.h"
27
#include "gtkprivate.h"
28
#include "gtkmarshalers.h"
29
#include "gtkalias.h"
Owen Taylor's avatar
Owen Taylor committed
30

31
static void gtk_file_chooser_class_init (gpointer g_iface);
Owen Taylor's avatar
Owen Taylor committed
32

33
static GtkFilePath *gtk_file_chooser_get_path         (GtkFileChooser *chooser);
34

Owen Taylor's avatar
Owen Taylor committed
35 36 37 38 39 40 41
GType
gtk_file_chooser_get_type (void)
{
  static GType file_chooser_type = 0;

  if (!file_chooser_type)
    {
Matthias Clasen's avatar
Matthias Clasen committed
42 43 44
      file_chooser_type = g_type_register_static_simple (G_TYPE_INTERFACE,
							 I_("GtkFileChooser"),
							 sizeof (GtkFileChooserIface),
45
							 (GClassInitFunc) gtk_file_chooser_class_init,
Matthias Clasen's avatar
Matthias Clasen committed
46 47
							 0, NULL, 0);
      
48
      g_type_interface_add_prerequisite (file_chooser_type, GTK_TYPE_WIDGET);
Owen Taylor's avatar
Owen Taylor committed
49 50 51 52 53
    }

  return file_chooser_type;
}

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static gboolean
confirm_overwrite_accumulator (GSignalInvocationHint *ihint,
			       GValue                *return_accu,
			       const GValue          *handler_return,
			       gpointer               dummy)
{
  gboolean continue_emission;
  GtkFileChooserConfirmation conf;

  conf = g_value_get_enum (handler_return);
  g_value_set_enum (return_accu, conf);
  continue_emission = (conf == GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM);

  return continue_emission;
}

Owen Taylor's avatar
Owen Taylor committed
70
static void
71
gtk_file_chooser_class_init (gpointer g_iface)
Owen Taylor's avatar
Owen Taylor committed
72
{
73
  GType iface_type = G_TYPE_FROM_INTERFACE (g_iface);
Owen Taylor's avatar
Owen Taylor committed
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  /**
   * GtkFileChooser::current-folder-changed
   * @chooser: the object which received the signal.
   *
   * This signal is emitted when the current folder in a #GtkFileChooser
   * changes.  This can happen due to the user performing some action that
   * changes folders, such as selecting a bookmark or visiting a folder on the
   * file list.  It can also happen as a result of calling a function to
   * explicitly change the current folder in a file chooser.
   *
   * Normally you do not need to connect to this signal, unless you need to keep
   * track of which folder a file chooser is showing.
   *
   * See also:  gtk_file_chooser_set_current_folder(),
   * gtk_file_chooser_get_current_folder(),
   * gtk_file_chooser_set_current_folder_uri(),
   * gtk_file_chooser_get_current_folder_uri().
   */
Matthias Clasen's avatar
Matthias Clasen committed
93
  g_signal_new (I_("current-folder-changed"),
94 95 96 97 98 99
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, current_folder_changed),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

  /**
   * GtkFileChooser::selection-changed
   * @chooser: the object which received the signal.
   *
   * This signal is emitted when there is a change in the set of selected files
   * in a #GtkFileChooser.  This can happen when the user modifies the selection
   * with the mouse or the keyboard, or when explicitly calling functions to
   * change the selection.
   *
   * Normally you do not need to connect to this signal, as it is easier to wait
   * for the file chooser to finish running, and then to get the list of
   * selected files using the functions mentioned below.
   *
   * See also: gtk_file_chooser_select_filename(),
   * gtk_file_chooser_unselect_filename(), gtk_file_chooser_get_filename(),
   * gtk_file_chooser_get_filenames(), gtk_file_chooser_select_uri(),
   * gtk_file_chooser_unselect_uri(), gtk_file_chooser_get_uri(),
   * gtk_file_chooser_get_uris().
   */
Matthias Clasen's avatar
Matthias Clasen committed
120
  g_signal_new (I_("selection-changed"),
121 122 123 124 125 126
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, selection_changed),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  /**
   * GtkFileChooser::update-preview
   * @chooser: the object which received the signal.
   *
   * This signal is emitted when the preview in a file chooser should be
   * regenerated.  For example, this can happen when the currently selected file
   * changes.  You should use this signal if you want your file chooser to have
   * a preview widget.
   *
   * Once you have installed a preview widget with
   * gtk_file_chooser_set_preview_widget(), you should update it when this
   * signal is emitted.  You can use the functions
   * gtk_file_chooser_get_preview_filename() or
   * gtk_file_chooser_get_preview_uri() to get the name of the file to preview.
   * Your widget may not be able to preview all kinds of files; your callback
   * must call gtk_file_chooser_set_preview_wiget_active() to inform the file
   * chooser about whether the preview was generated successfully or not.
   *
   * Please see the example code in <xref linkend="gtkfilechooser-preview"/>.
   *
   * See also: gtk_file_chooser_set_preview_widget(),
   * gtk_file_chooser_set_preview_widget_active(),
   * gtk_file_chooser_set_use_preview_label(),
   * gtk_file_chooser_get_preview_filename(),
   * gtk_file_chooser_get_preview_uri().
   */
Matthias Clasen's avatar
Matthias Clasen committed
154
  g_signal_new (I_("update-preview"),
155 156 157 158 159 160
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, update_preview),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

  /**
   * GtkFileChooser::file-activated
   * @chooser: the object which received the signal.
   *
   * This signal is emitted when the user "activates" a file in the file
   * chooser.  This can happen by double-clicking on a file in the file list, or
   * by pressing <keycap>Enter</keycap>.
   *
   * Normally you do not need to connect to this signal.  It is used internally
   * by #GtkFileChooserDialog to know when to activate the default button in the
   * dialog.
   *
   * See also: gtk_file_chooser_get_filename(),
   * gtk_file_chooser_get_filenames(), gtk_file_chooser_get_uri(),
   * gtk_file_chooser_get_uris().
   */
Matthias Clasen's avatar
Matthias Clasen committed
178
  g_signal_new (I_("file-activated"),
179 180 181 182 183 184
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, file_activated),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
185

186
  /* Documented in the docbook files */
Matthias Clasen's avatar
Matthias Clasen committed
187
  g_signal_new (I_("confirm-overwrite"),
188 189 190 191 192 193
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, confirm_overwrite),
		confirm_overwrite_accumulator, NULL,
		_gtk_marshal_ENUM__VOID,
		GTK_TYPE_FILE_CHOOSER_CONFIRMATION, 0);
194 195 196
  
  g_object_interface_install_property (g_iface,
				       g_param_spec_enum ("action",
197 198
							  P_("Action"),
							  P_("The type of operation that the file selector is performing"),
199 200
							  GTK_TYPE_FILE_CHOOSER_ACTION,
							  GTK_FILE_CHOOSER_ACTION_OPEN,
201
							  GTK_PARAM_READWRITE));
202
  g_object_interface_install_property (g_iface,
203 204 205 206
				       g_param_spec_string ("file-system-backend",
							    P_("File System Backend"),
							    P_("Name of file system backend to use"),
							    NULL, 
207
							    GTK_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
208 209
  g_object_interface_install_property (g_iface,
				       g_param_spec_object ("filter",
210 211
							    P_("Filter"),
							    P_("The current filter for selecting which files are displayed"),
212
							    GTK_TYPE_FILE_FILTER,
213
							    GTK_PARAM_READWRITE));
214 215
  g_object_interface_install_property (g_iface,
				       g_param_spec_boolean ("local-only",
216 217
							     P_("Local Only"),
							     P_("Whether the selected file(s) should be limited to local file: URLs"),
218
							     TRUE,
219
							     GTK_PARAM_READWRITE));
220 221
  g_object_interface_install_property (g_iface,
				       g_param_spec_object ("preview-widget",
222 223
							    P_("Preview widget"),
							    P_("Application supplied widget for custom previews."),
224
							    GTK_TYPE_WIDGET,
225
							    GTK_PARAM_READWRITE));
226 227
  g_object_interface_install_property (g_iface,
				       g_param_spec_boolean ("preview-widget-active",
228 229
							     P_("Preview Widget Active"),
							     P_("Whether the application supplied widget for custom previews should be shown."),
230
							     TRUE,
231
							     GTK_PARAM_READWRITE));
232 233 234 235 236
  g_object_interface_install_property (g_iface,
				       g_param_spec_boolean ("use-preview-label",
							     P_("Use Preview Label"),
							     P_("Whether to display a stock label with the name of the previewed file."),
							     TRUE,
237
							     GTK_PARAM_READWRITE));
238 239
  g_object_interface_install_property (g_iface,
				       g_param_spec_object ("extra-widget",
240 241
							    P_("Extra widget"),
							    P_("Application supplied widget for extra options."),
242
							    GTK_TYPE_WIDGET,
243
							    GTK_PARAM_READWRITE));
244 245
  g_object_interface_install_property (g_iface,
				       g_param_spec_boolean ("select-multiple",
246 247
							     P_("Select Multiple"),
							     P_("Whether to allow multiple files to be selected"),
248
							     FALSE,
249
							     GTK_PARAM_READWRITE));
250 251 252
  
  g_object_interface_install_property (g_iface,
				       g_param_spec_boolean ("show-hidden",
253 254
							     P_("Show Hidden"),
							     P_("Whether the hidden files and folders should be displayed"),
255
							     FALSE,
256
							     GTK_PARAM_READWRITE));
257

258 259 260 261 262 263 264 265 266
  /**
   * GtkFileChooser:do-overwrite-confirmation:
   * 
   * Whether a file chooser in %GTK_FILE_CHOOSER_ACTION_SAVE mode
   * will present an overwrite confirmation dialog if the user
   * selects a file name that already exists.
   *
   * Since: 2.8
   */
267 268 269
  g_object_interface_install_property (g_iface,
				       g_param_spec_boolean ("do-overwrite-confirmation",
							     P_("Do overwrite confirmation"),
270
							     P_("Whether a file chooser in save mode "
271
								"will present an overwrite confirmation dialog "
272
								"if necessary."),
273 274
							     FALSE,
							     GTK_PARAM_READWRITE));
Owen Taylor's avatar
Owen Taylor committed
275 276
}

277 278 279 280 281 282
/**
 * gtk_file_chooser_error_quark:
 *
 * Registers an error quark for #GtkFileChooser if necessary.
 * 
 * Return value: The error quark used for #GtkFileChooser errors.
283 284
 *
 * Since: 2.4
285 286 287 288
 **/
GQuark
gtk_file_chooser_error_quark (void)
{
289
  return g_quark_from_static_string ("gtk-file-chooser-error-quark");
290 291
}

292 293 294 295 296
/**
 * gtk_file_chooser_set_action:
 * @chooser: a #GtkFileChooser
 * @action: the action that the file selector is performing
 * 
297
 * Sets the type of operation that the chooser is performing; the
298 299 300 301
 * user interface is adapted to suit the selected action. For example,
 * an option to create a new folder might be shown if the action is
 * %GTK_FILE_CHOOSER_ACTION_SAVE but not if the action is
 * %GTK_FILE_CHOOSER_ACTION_OPEN.
302 303
 *
 * Since: 2.4
304
 **/
Owen Taylor's avatar
Owen Taylor committed
305 306 307 308 309 310 311 312 313
void
gtk_file_chooser_set_action (GtkFileChooser       *chooser,
			     GtkFileChooserAction  action)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));

  g_object_set (chooser, "action", action, NULL);
}

314 315 316 317 318 319 320 321
/**
 * gtk_file_chooser_get_action:
 * @chooser: a #GtkFileChooser
 * 
 * Gets the type of operation that the file chooser is performing; see
 * gtk_file_chooser_set_action().
 * 
 * Return value: the action that the file selector is performing
322 323
 *
 * Since: 2.4
324
 **/
Owen Taylor's avatar
Owen Taylor committed
325 326 327 328 329 330 331 332 333 334 335 336
GtkFileChooserAction
gtk_file_chooser_get_action (GtkFileChooser *chooser)
{
  GtkFileChooserAction action;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);

  g_object_get (chooser, "action", &action, NULL);

  return action;
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350
/**
 * gtk_file_chooser_set_local_only:
 * @chooser: a #GtkFileChooser
 * @local_only: %TRUE if only local files can be selected
 * 
 * Sets whether only local files can be selected in the
 * file selector. If @local_only is %TRUE (the default),
 * then the selected file are files are guaranteed to be
 * accessible through the operating systems native file
 * file system and therefore the application only
 * needs to worry about the filename functions in
 * #GtkFileChooser, like gtk_file_chooser_get_filename(),
 * rather than the URI functions like
 * gtk_file_chooser_get_uri(),
351 352
 *
 * Since: 2.4
353
 **/
Owen Taylor's avatar
Owen Taylor committed
354 355 356 357 358 359
void
gtk_file_chooser_set_local_only (GtkFileChooser *chooser,
				 gboolean        local_only)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));

360
  g_object_set (chooser, "local-only", local_only, NULL);
Owen Taylor's avatar
Owen Taylor committed
361 362
}

363 364 365 366 367 368 369 370
/**
 * gtk_file_chooser_get_local_only:
 * @chooser: a #GtkFileChoosre
 * 
 * Gets whether only local files can be selected in the
 * file selector. See gtk_file_chooser_set_local_only()
 * 
 * Return value: %TRUE if only local files can be selected.
371 372
 *
 * Since: 2.4
373
 **/
Owen Taylor's avatar
Owen Taylor committed
374 375 376 377 378 379 380
gboolean
gtk_file_chooser_get_local_only (GtkFileChooser *chooser)
{
  gboolean local_only;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);

381
  g_object_get (chooser, "local-only", &local_only, NULL);
Owen Taylor's avatar
Owen Taylor committed
382 383 384 385

  return local_only;
}

386 387 388 389 390
/**
 * gtk_file_chooser_set_select_multiple:
 * @chooser: a #GtkFileChooser
 * @select_multiple: %TRUE if multiple files can be selected.
 * 
391 392 393 394
 * Sets whether multiple files can be selected in the file selector.  This is
 * only relevant if the action is set to be GTK_FILE_CHOOSER_ACTION_OPEN or
 * GTK_FILE_CHOOSER_ACTION_SAVE.  It cannot be set with either of the folder
 * actions.
395 396
 *
 * Since: 2.4
397
 **/
Owen Taylor's avatar
Owen Taylor committed
398 399 400 401 402 403
void
gtk_file_chooser_set_select_multiple (GtkFileChooser *chooser,
				      gboolean        select_multiple)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));

404
  g_object_set (chooser, "select-multiple", select_multiple, NULL);
Owen Taylor's avatar
Owen Taylor committed
405 406
}

407 408 409 410 411 412 413 414
/**
 * gtk_file_chooser_get_select_multiple:
 * @chooser: a #GtkFileChooser
 * 
 * Gets whether multiple files can be selected in the file
 * selector. See gtk_file_chooser_set_select_multiple().
 * 
 * Return value: %TRUE if multiple files can be selected.
415 416
 *
 * Since: 2.4
417
 **/
Owen Taylor's avatar
Owen Taylor committed
418 419 420 421 422 423 424
gboolean
gtk_file_chooser_get_select_multiple (GtkFileChooser *chooser)
{
  gboolean select_multiple;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);

425
  g_object_get (chooser, "select-multiple", &select_multiple, NULL);
Owen Taylor's avatar
Owen Taylor committed
426 427 428 429

  return select_multiple;
}

430 431 432 433 434 435 436
/**
 * gtk_file_chooser_get_filename:
 * @chooser: a #GtkFileChooser
 * 
 * Gets the filename for the currently selected file in
 * the file selector. If multiple files are selected,
 * one of the filenames will be returned at random.
437 438 439
 *
 * If the file chooser is in folder mode, this function returns the selected
 * folder.
440 441 442
 * 
 * Return value: The currently selected filename, or %NULL
 *  if no file is selected, or the selected file can't
443
 *  be represented with a local filename. Free with g_free().
444 445
 *
 * Since: 2.4
446
 **/
447
gchar *
Owen Taylor's avatar
Owen Taylor committed
448 449
gtk_file_chooser_get_filename (GtkFileChooser *chooser)
{
450 451
  GtkFileSystem *file_system;
  GtkFilePath *path;
Owen Taylor's avatar
Owen Taylor committed
452 453 454 455
  gchar *result = NULL;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

456 457 458
  file_system = _gtk_file_chooser_get_file_system (chooser);
  path = gtk_file_chooser_get_path (chooser);
  if (path)
Owen Taylor's avatar
Owen Taylor committed
459
    {
460 461
      result = gtk_file_system_path_to_filename (file_system, path);
      gtk_file_path_free (path);
Owen Taylor's avatar
Owen Taylor committed
462 463 464 465 466
    }

  return result;
}

467 468 469 470 471
/**
 * gtk_file_chooser_set_filename:
 * @chooser: a #GtkFileChooser
 * @filename: the filename to set as current
 * 
472 473 474 475 476 477 478 479 480
 * Sets @filename as the current filename for the file chooser, by changing
 * to the file's parent folder and actually selecting the file in list.  If
 * the @chooser is in #GTK_FILE_CHOOSER_ACTION_SAVE mode, the file's base name
 * will also appear in the dialog's file name entry.
 *
 * If the file name isn't in the current folder of @chooser, then the current
 * folder of @chooser will be changed to the folder containing @filename. This
 * is equivalent to a sequence of gtk_file_chooser_unselect_all() followed by
 * gtk_file_chooser_select_filename().
481 482
 *
 * Note that the file must exist, or nothing will be done except
483 484 485 486 487 488 489 490
 * for the directory change.
 *
 * If you are implementing a <guimenuitem>File/Save As...</guimenuitem> dialog, you
 * should use this function if you already have a file name to which the user may save; for example,
 * when the user opens an existing file and then does <guimenuitem>File/Save As...</guimenuitem>
 * on it.  If you don't have a file name already &mdash; for example, if the user just created
 * a new file and is saving it for the first time, do not call this function.  Instead, use
 * something similar to this:
491
 *
492 493 494 495 496 497 498 499 500 501 502 503 504 505
 * <programlisting>
 * if (document_is_new)
 *   {
 *     /<!-- -->* the user just created a new document *<!-- -->/
 *     gtk_file_chooser_set_current_folder (chooser, default_folder_for_saving);
 *     gtk_file_chooser_set_current_name (chooser, "Untitled document");
 *   }
 * else
 *   {
 *     /<!-- -->* the user edited an existing document *<!-- -->/ 
 *     gtk_file_chooser_set_filename (chooser, existing_filename);
 *   }
 * </programlisting>
 * 
506 507 508
 * Return value: %TRUE if both the folder could be changed and the file was
 * selected successfully, %FALSE otherwise.
 *
509
 * Since: 2.4
510
 **/
511
gboolean
Owen Taylor's avatar
Owen Taylor committed
512
gtk_file_chooser_set_filename (GtkFileChooser *chooser,
513
			       const gchar    *filename)
Owen Taylor's avatar
Owen Taylor committed
514
{
515
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
516 517

  gtk_file_chooser_unselect_all (chooser);
518
  return gtk_file_chooser_select_filename (chooser, filename);
Owen Taylor's avatar
Owen Taylor committed
519 520
}

521 522 523 524 525 526 527 528
/**
 * gtk_file_chooser_select_filename:
 * @chooser: a #GtkFileChooser
 * @filename: the filename to select
 * 
 * Selects a filename. If the file name isn't in the current
 * folder of @chooser, then the current folder of @chooser will
 * be changed to the folder containing @filename.
529
 *
530 531 532
 * Return value: %TRUE if both the folder could be changed and the file was
 * selected successfully, %FALSE otherwise.
 *
533
 * Since: 2.4
534
 **/
535
gboolean
Owen Taylor's avatar
Owen Taylor committed
536
gtk_file_chooser_select_filename (GtkFileChooser *chooser,
537
				  const gchar    *filename)
Owen Taylor's avatar
Owen Taylor committed
538
{
539 540
  GtkFileSystem *file_system;
  GtkFilePath *path;
541
  gboolean result;
542
  
543 544
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);
545 546 547 548 549 550

  file_system = _gtk_file_chooser_get_file_system (chooser);

  path = gtk_file_system_filename_to_path (file_system, filename);
  if (path)
    {
551
      result = _gtk_file_chooser_select_path (chooser, path, NULL);
552 553
      gtk_file_path_free (path);
    }
554 555 556 557
  else
    result = FALSE;

  return result;
Owen Taylor's avatar
Owen Taylor committed
558 559
}

560 561 562 563 564 565 566 567
/**
 * gtk_file_chooser_unselect_filename:
 * @chooser: a #GtkFileChooser
 * @filename: the filename to unselect
 * 
 * Unselects a currently selected filename. If the filename
 * is not in the current directory, does not exist, or
 * is otherwise not currently selected, does nothing.
568 569
 *
 * Since: 2.4
570
 **/
Owen Taylor's avatar
Owen Taylor committed
571 572 573 574
void
gtk_file_chooser_unselect_filename (GtkFileChooser *chooser,
				    const char     *filename)
{
575 576 577
  GtkFileSystem *file_system;
  GtkFilePath *path;
  
Owen Taylor's avatar
Owen Taylor committed
578 579
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
  g_return_if_fail (filename != NULL);
580 581 582 583 584 585 586 587 588

  file_system = _gtk_file_chooser_get_file_system (chooser);

  path = gtk_file_system_filename_to_path (file_system, filename);
  if (path)
    {
      _gtk_file_chooser_unselect_path (chooser, path);
      gtk_file_path_free (path);
    }
Owen Taylor's avatar
Owen Taylor committed
589 590
}

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
/* Converts a list of GtkFilePath* to a list of strings using the specified function */
static GSList *
file_paths_to_strings (GtkFileSystem *fs,
		       GSList        *paths,
		       gchar *      (*convert_func) (GtkFileSystem *fs, const GtkFilePath *path))
{
  GSList *strings;

  strings = NULL;

  for (; paths; paths = paths->next)
    {
      GtkFilePath *path;
      gchar *string;

      path = paths->data;
      string = (* convert_func) (fs, path);

      if (string)
	strings = g_slist_prepend (strings, string);
    }

  return g_slist_reverse (strings);
}

616 617 618 619
/**
 * gtk_file_chooser_get_filenames:
 * @chooser: a #GtkFileChooser
 * 
620 621 622 623
 * Lists all the selected files and subfolders in the current folder of
 * @chooser. The returned names are full absolute paths. If files in the current
 * folder cannot be represented as local filenames they will be ignored. (See
 * gtk_file_chooser_get_uris())
624
 * 
625
 * Return value: a #GSList containing the filenames of all selected
626
 *   files and subfolders in the current folder. Free the returned list
627
 *   with g_slist_free(), and the filenames with g_free().
628 629
 *
 * Since: 2.4
630
 **/
Owen Taylor's avatar
Owen Taylor committed
631 632 633
GSList *
gtk_file_chooser_get_filenames (GtkFileChooser *chooser)
{
634 635
  GtkFileSystem *file_system;
  GSList *paths;
636
  GSList *result;
637
  
Owen Taylor's avatar
Owen Taylor committed
638 639
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

640 641 642
  file_system = _gtk_file_chooser_get_file_system (chooser);
  paths = _gtk_file_chooser_get_paths (chooser);

643
  result = file_paths_to_strings (file_system, paths, gtk_file_system_path_to_filename);
644
  gtk_file_paths_free (paths);
645
  return result;
Owen Taylor's avatar
Owen Taylor committed
646 647
}

648 649 650 651 652 653 654 655
/**
 * gtk_file_chooser_set_current_folder:
 * @chooser: a #GtkFileChooser
 * @filename: the full path of the new current folder
 * 
 * Sets the current folder for @chooser from a local filename.
 * The user will be shown the full contents of the current folder,
 * plus user interface elements for navigating to other folders.
656
 *
657 658 659
 * Return value: %TRUE if the folder could be changed successfully, %FALSE
 * otherwise.
 *
660
 * Since: 2.4
661
 **/
662
gboolean
Owen Taylor's avatar
Owen Taylor committed
663 664 665
gtk_file_chooser_set_current_folder (GtkFileChooser *chooser,
				     const gchar    *filename)
{
666 667
  GtkFileSystem *file_system;
  GtkFilePath *path;
668
  gboolean result;
669
  
670 671
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);
672 673 674 675 676 677

  file_system = _gtk_file_chooser_get_file_system (chooser);

  path = gtk_file_system_filename_to_path (file_system, filename);
  if (path)
    {
678
      result = _gtk_file_chooser_set_current_folder_path (chooser, path, NULL);
679 680
      gtk_file_path_free (path);
    }
681 682 683 684
  else
    result = FALSE;

  return result;
Owen Taylor's avatar
Owen Taylor committed
685 686
}

687 688 689 690 691 692
/**
 * gtk_file_chooser_get_current_folder:
 * @chooser: a #GtkFileChooser
 * 
 * Gets the current folder of @chooser as a local filename.
 * See gtk_file_chooser_set_current_folder().
693 694 695 696 697 698 699 700
 *
 * Note that this is the folder that the file chooser is currently displaying
 * (e.g. "/home/username/Documents"), which is <emphasis>not the same</emphasis>
 * as the currently-selected folder if the chooser is in
 * #GTK_FILE_CHOOSER_SELECT_FOLDER mode
 * (e.g. "/home/username/Documents/selected-folder/".  To get the
 * currently-selected folder in that mode, use gtk_file_chooser_get_uri() as the
 * usual way to get the selection.
701
 * 
Federico Mena Quintero's avatar
Federico Mena Quintero committed
702 703 704 705 706
 * Return value: the full path of the current folder, or %NULL if the current
 * path cannot be represented as a local filename.  Free with g_free().  This
 * function will also return %NULL if the file chooser was unable to load the
 * last folder that was requested from it; for example, as would be for calling
 * gtk_file_chooser_set_current_folder() on a nonexistent folder.
707 708
 *
 * Since: 2.4
709
 **/
Owen Taylor's avatar
Owen Taylor committed
710 711 712
gchar *
gtk_file_chooser_get_current_folder (GtkFileChooser *chooser)
{
713 714 715 716
  GtkFileSystem *file_system;
  GtkFilePath *path;
  gchar *filename;
  
Owen Taylor's avatar
Owen Taylor committed
717 718
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

719 720
  file_system = _gtk_file_chooser_get_file_system (chooser);

721
  path = _gtk_file_chooser_get_current_folder_path (chooser);
722 723 724
  if (!path)
    return NULL;

725 726 727 728
  filename = gtk_file_system_path_to_filename (file_system, path);
  gtk_file_path_free (path);

  return filename;
Owen Taylor's avatar
Owen Taylor committed
729 730
}

731 732 733 734 735 736 737 738 739 740
/**
 * gtk_file_chooser_set_current_name:
 * @chooser: a #GtkFileChooser
 * @name: the filename to use, as a UTF-8 string
 * 
 * Sets the current name in the file selector, as if entered
 * by the user. Note that the name passed in here is a UTF-8
 * string rather than a filename. This function is meant for
 * such uses as a suggested name in a "Save As..." dialog.
 *
741 742 743 744
 * If you want to preselect a particular existing file, you should use
 * gtk_file_chooser_set_filename() or gtk_file_chooser_set_uri() instead.
 * Please see the documentation for those functions for an example of using
 * gtk_file_chooser_set_current_name() as well.
745 746
 *
 * Since: 2.4
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
 **/
void
gtk_file_chooser_set_current_name  (GtkFileChooser *chooser,
				    const gchar    *name)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
  g_return_if_fail (name != NULL);
  
  GTK_FILE_CHOOSER_GET_IFACE (chooser)->set_current_name (chooser, name);
}

/**
 * gtk_file_chooser_get_uri:
 * @chooser: a #GtkFileChooser
 * 
 * Gets the URI for the currently selected file in
 * the file selector. If multiple files are selected,
 * one of the filenames will be returned at random.
 * 
766 767 768
 * If the file chooser is in folder mode, this function returns the selected
 * folder.
 * 
769 770
 * Return value: The currently selected URI, or %NULL
 *  if no file is selected. Free with g_free()
771 772
 *
 * Since: 2.4
773
 **/
Owen Taylor's avatar
Owen Taylor committed
774 775 776
gchar *
gtk_file_chooser_get_uri (GtkFileChooser *chooser)
{
777 778
  GtkFileSystem *file_system;
  GtkFilePath *path;
Owen Taylor's avatar
Owen Taylor committed
779 780 781 782
  gchar *result = NULL;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

783 784 785
  file_system = _gtk_file_chooser_get_file_system (chooser);
  path = gtk_file_chooser_get_path (chooser);
  if (path)
Owen Taylor's avatar
Owen Taylor committed
786
    {
787 788
      result = gtk_file_system_path_to_uri (file_system, path);
      gtk_file_path_free (path);
Owen Taylor's avatar
Owen Taylor committed
789 790 791 792 793
    }

  return result;
}

794 795 796 797 798
/**
 * gtk_file_chooser_set_uri:
 * @chooser: a #GtkFileChooser
 * @uri: the URI to set as current
 * 
799 800 801 802
 * Sets the file referred to by @uri as the current file for the file chooser,
 * by changing to the URI's parent folder and actually selecting the URI in the
 * list.  If the @chooser is #GTK_FILE_CHOOSER_ACTION_SAVE mode, the URI's base
 * name will also appear in the dialog's file name entry.
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
 * If the URI isn't in the current folder of @chooser, then the current folder
 * of @chooser will be changed to the folder containing @uri. This is equivalent
 * to a sequence of gtk_file_chooser_unselect_all() followed by
 * gtk_file_chooser_select_uri().
 *
 * Note that the URI must exist, or nothing will be done except
 * for the directory change.
 * If you are implementing a <guimenuitem>File/Save As...</guimenuitem> dialog, you
 * should use this function if you already have a file name to which the user may save; for example,
 * when the user opens an existing file and then does <guimenuitem>File/Save As...</guimenuitem>
 * on it.  If you don't have a file name already &mdash; for example, if the user just created
 * a new file and is saving it for the first time, do not call this function.  Instead, use
 * something similar to this:
 *
 * <programlisting>
 * if (document_is_new)
 *   {
 *     /<!-- -->* the user just created a new document *<!-- -->/
 *     gtk_file_chooser_set_current_folder_uri (chooser, default_folder_for_saving);
 *     gtk_file_chooser_set_current_name (chooser, "Untitled document");
 *   }
 * else
 *   {
 *     /<!-- -->* the user edited an existing document *<!-- -->/ 
 *     gtk_file_chooser_set_uri (chooser, existing_uri);
 *   }
 * </programlisting>
831
 *
832 833 834
 * Return value: %TRUE if both the folder could be changed and the URI was
 * selected successfully, %FALSE otherwise.
 *
835
 * Since: 2.4
836
 **/
837
gboolean
Owen Taylor's avatar
Owen Taylor committed
838 839 840
gtk_file_chooser_set_uri (GtkFileChooser *chooser,
			  const char     *uri)
{
841
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
842 843

  gtk_file_chooser_unselect_all (chooser);
844
  return gtk_file_chooser_select_uri (chooser, uri);
Owen Taylor's avatar
Owen Taylor committed
845 846
}

847 848 849 850 851 852 853 854
/**
 * gtk_file_chooser_select_uri:
 * @chooser: a #GtkFileChooser
 * @uri: the URI to select
 * 
 * Selects the file to by @uri. If the URI doesn't refer to a
 * file in the current folder of @chooser, then the current folder of
 * @chooser will be changed to the folder containing @filename.
855
 *
856 857 858
 * Return value: %TRUE if both the folder could be changed and the URI was
 * selected successfully, %FALSE otherwise.
 *
859
 * Since: 2.4
860
 **/
861
gboolean
Owen Taylor's avatar
Owen Taylor committed
862 863 864
gtk_file_chooser_select_uri (GtkFileChooser *chooser,
			     const char     *uri)
{
865 866
  GtkFileSystem *file_system;
  GtkFilePath *path;
867
  gboolean result;
Owen Taylor's avatar
Owen Taylor committed
868
  
869 870
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (uri != NULL, FALSE);
871 872 873 874 875 876

  file_system = _gtk_file_chooser_get_file_system (chooser);

  path = gtk_file_system_uri_to_path (file_system, uri);
  if (path)
    {
877
      result = _gtk_file_chooser_select_path (chooser, path, NULL);
878 879
      gtk_file_path_free (path);
    }
880 881 882 883
  else
    result = FALSE;

  return result;
Owen Taylor's avatar
Owen Taylor committed
884 885
}

886 887 888 889 890 891 892 893
/**
 * gtk_file_chooser_unselect_uri:
 * @chooser: a #GtkFileChooser
 * @uri: the URI to unselect
 * 
 * Unselects the file referred to by @uri. If the file
 * is not in the current directory, does not exist, or
 * is otherwise not currently selected, does nothing.
894 895
 *
 * Since: 2.4
896
 **/
Owen Taylor's avatar
Owen Taylor committed
897 898 899 900
void
gtk_file_chooser_unselect_uri (GtkFileChooser *chooser,
			       const char     *uri)
{
901 902
  GtkFileSystem *file_system;
  GtkFilePath *path;
Owen Taylor's avatar
Owen Taylor committed
903
  
904 905 906 907 908 909 910 911 912 913 914
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
  g_return_if_fail (uri != NULL);

  file_system = _gtk_file_chooser_get_file_system (chooser);

  path = gtk_file_system_uri_to_path (file_system, uri);
  if (path)
    {
      _gtk_file_chooser_unselect_path (chooser, path);
      gtk_file_path_free (path);
    }
Owen Taylor's avatar
Owen Taylor committed
915 916
}

917 918 919 920 921
/**
 * gtk_file_chooser_select_all:
 * @chooser: a #GtkFileChooser
 * 
 * Selects all the files in the current folder of a file chooser.
Matthias Clasen's avatar
Matthias Clasen committed
922 923
 *
 * Since: 2.4
924
 **/
Owen Taylor's avatar
Owen Taylor committed
925 926 927 928 929 930 931 932
void
gtk_file_chooser_select_all (GtkFileChooser *chooser)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
  
  GTK_FILE_CHOOSER_GET_IFACE (chooser)->select_all (chooser);
}

933 934 935 936 937
/**
 * gtk_file_chooser_unselect_all:
 * @chooser: a #GtkFileChooser
 * 
 * Unselects all the files in the current folder of a file chooser.
Matthias Clasen's avatar
Matthias Clasen committed
938 939
 *
 * Since: 2.4
940
 **/
Owen Taylor's avatar
Owen Taylor committed
941 942 943 944 945 946 947 948 949
void
gtk_file_chooser_unselect_all (GtkFileChooser *chooser)
{

  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
  
  GTK_FILE_CHOOSER_GET_IFACE (chooser)->unselect_all (chooser);
}

950
/**
951
 * gtk_file_chooser_get_uris:
952 953
 * @chooser: a #GtkFileChooser
 * 
954
 * Lists all the selected files and subfolders in the current folder of
955 956
 * @chooser. The returned names are full absolute URIs.
 * 
957
 * Return value: a #GSList containing the URIs of all selected
958
 *   files and subfolders in the current folder. Free the returned list
959
 *   with g_slist_free(), and the filenames with g_free().
960 961
 *
 * Since: 2.4
962
 **/
Owen Taylor's avatar
Owen Taylor committed
963 964 965
GSList *
gtk_file_chooser_get_uris (GtkFileChooser *chooser)
{
966 967
  GtkFileSystem *file_system;
  GSList *paths;
968
  GSList *result;
Owen Taylor's avatar
Owen Taylor committed
969
  
970 971 972 973 974
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

  file_system = _gtk_file_chooser_get_file_system (chooser);
  paths = _gtk_file_chooser_get_paths (chooser);

975
  result = file_paths_to_strings (file_system, paths, gtk_file_system_path_to_uri);
976
  gtk_file_paths_free (paths);
977
  return result;
Owen Taylor's avatar
Owen Taylor committed
978 979
}

980 981 982 983 984 985 986 987
/**
 * gtk_file_chooser_set_current_folder_uri:
 * @chooser: a #GtkFileChooser
 * @uri: the URI for the new current folder
 * 
 * Sets the current folder for @chooser from an URI.
 * The user will be shown the full contents of the current folder,
 * plus user interface elements for navigating to other folders.
988
 *
989 990 991
 * Return value: %TRUE if the folder could be changed successfully, %FALSE
 * otherwise.
 *
992
 * Since: 2.4
993
 **/
994
gboolean
Owen Taylor's avatar
Owen Taylor committed
995 996 997
gtk_file_chooser_set_current_folder_uri (GtkFileChooser *chooser,
					 const gchar    *uri)
{
998 999
  GtkFileSystem *file_system;
  GtkFilePath *path;
1000
  gboolean result;
1001
  
1002 1003
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (uri != NULL, FALSE);
Owen Taylor's avatar
Owen Taylor committed
1004

1005
  file_system = _gtk_file_chooser_get_file_system (chooser);
Owen Taylor's avatar
Owen Taylor committed
1006

1007 1008 1009
  path = gtk_file_system_uri_to_path (file_system, uri);
  if (path)
    {
1010
      result = _gtk_file_chooser_set_current_folder_path (chooser, path, NULL);
1011 1012
      gtk_file_path_free (path);
    }
1013 1014 1015 1016
  else
    result = FALSE;

  return result;
1017
}
Owen Taylor's avatar
Owen Taylor committed
1018

1019 1020 1021 1022 1023 1024
/**
 * gtk_file_chooser_get_current_folder_uri:
 * @chooser: a #GtkFileChooser
 * 
 * Gets the current folder of @chooser as an URI.
 * See gtk_file_chooser_set_current_folder_uri().
1025 1026 1027 1028 1029 1030 1031 1032
 *
 * Note that this is the folder that the file chooser is currently displaying
 * (e.g. "file:///home/username/Documents"), which is <emphasis>not the same</emphasis>
 * as the currently-selected folder if the chooser is in
 * #GTK_FILE_CHOOSER_SELECT_FOLDER mode
 * (e.g. "file:///home/username/Documents/selected-folder/".  To get the
 * currently-selected folder in that mode, use gtk_file_chooser_get_uri() as the
 * usual way to get the selection.
1033
 * 
Federico Mena Quintero's avatar
Federico Mena Quintero committed
1034 1035 1036 1037
 * Return value: the URI for the current folder.  Free with g_free().  This
 * function will also return %NULL if the file chooser was unable to load the
 * last folder that was requested from it; for example, as would be for calling
 * gtk_file_chooser_set_current_folder_uri() on a nonexistent folder.
1038 1039
 *
 * Since: 2.4
1040
 */
Owen Taylor's avatar
Owen Taylor committed
1041 1042
gchar *
gtk_file_chooser_get_current_folder_uri (GtkFileChooser *chooser)
1043 1044 1045 1046 1047 1048 1049 1050 1051
{
  GtkFileSystem *file_system;
  GtkFilePath *path;
  gchar *uri;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

  file_system = _gtk_file_chooser_get_file_system (chooser);

1052
  path = _gtk_file_chooser_get_current_folder_path (chooser);
1053 1054 1055 1056 1057 1058
  uri = gtk_file_system_path_to_uri (file_system, path);
  gtk_file_path_free (path);

  return uri;
}

1059 1060 1061 1062
/**
 * _gtk_file_chooser_set_current_folder_path:
 * @chooser: a #GtkFileChooser
 * @path: the #GtkFilePath for the new folder
1063
 * @error: location to store error, or %NULL.
1064 1065
 * 
 * Sets the current folder for @chooser from a #GtkFilePath.
1066
 * Internal function, see gtk_file_chooser_set_current_folder_uri().
1067
 *
1068 1069 1070
 * Return value: %TRUE if the folder could be changed successfully, %FALSE
 * otherwise.
 *
1071
 * Since: 2.4
1072
 **/
1073
gboolean
1074
_gtk_file_chooser_set_current_folder_path (GtkFileChooser    *chooser,
1075 1076
					   const GtkFilePath *path,
					   GError           **error)
1077
{
1078 1079 1080
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (path != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1081

1082
  return GTK_FILE_CHOOSER_GET_IFACE (chooser)->set_current_folder (chooser, path, error);
1083 1084
}

1085 1086 1087 1088 1089 1090 1091 1092
/**
 * _gtk_file_chooser_get_current_folder_path:
 * @chooser: a #GtkFileChooser
 * 
 * Gets the current folder of @chooser as #GtkFilePath.
 * See gtk_file_chooser_get_current_folder_uri().
 * 
 * Return value: the #GtkFilePath for the current folder.
1093 1094 1095
 * Free with gtk_file_path_free().
 *
 * Since: 2.4
1096
 */
1097
GtkFilePath *
1098
_gtk_file_chooser_get_current_folder_path (GtkFileChooser *chooser)
Owen Taylor's avatar
Owen Taylor committed
1099 1100 1101 1102 1103 1104
{
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

  return GTK_FILE_CHOOSER_GET_IFACE (chooser)->get_current_folder (chooser);  
}

1105 1106 1107 1108
/**
 * _gtk_file_chooser_select_path:
 * @chooser: a #GtkFileChooser
 * @path: the path to select
1109
 * @error: location to store error, or %NULL
1110 1111 1112
 * 
 * Selects the file referred to by @path. An internal function. See
 * _gtk_file_chooser_select_uri().
1113
 *
1114 1115 1116
 * Return value: %TRUE if both the folder could be changed and the path was
 * selected successfully, %FALSE otherwise.
 *
1117
 * Since: 2.4
1118
 **/
1119
gboolean
1120
_gtk_file_chooser_select_path (GtkFileChooser    *chooser,
1121 1122
			       const GtkFilePath *path,
			       GError           **error)
1123
{
1124 1125 1126
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (path != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1127

1128
  return GTK_FILE_CHOOSER_GET_IFACE (chooser)->select_path (chooser, path, error);
1129 1130
}

1131 1132 1133 1134 1135 1136 1137
/**
 * _gtk_file_chooser_unselect_path:
 * @chooser: a #GtkFileChooser
 * @path: the filename to path
 * 
 * Unselects the file referred to by @path. An internal
 * function. See _gtk_file_chooser_unselect_uri().
1138 1139
 *
 * Since: 2.4
1140
 **/
1141 1142 1143 1144 1145 1146 1147 1148 1149
void
_gtk_file_chooser_unselect_path (GtkFileChooser    *chooser,
				 const GtkFilePath *path)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));

  GTK_FILE_CHOOSER_GET_IFACE (chooser)->unselect_path (chooser, path);
}

1150 1151 1152 1153
/**
 * _gtk_file_chooser_get_paths:
 * @chooser: a #GtkFileChooser
 * 
1154 1155
 * Lists all the selected files and subfolders in the current folder of @chooser
 * as #GtkFilePath. An internal function, see gtk_file_chooser_get_uris().
1156
 * 
1157 1158 1159
 * Return value: a #GSList containing a #GtkFilePath for each selected
 *   file and subfolder in the current folder.  Free the returned list