gtkfilechooser.c 61.6 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Owen Taylor's avatar
Owen Taylor committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 "gtkintl.h"
25
#include "gtktypebuiltins.h"
26
#include "gtkprivate.h"
27
#include "gtkmarshalers.h"
28
#include "gtkalias.h"
Owen Taylor's avatar
Owen Taylor committed
29

30
static void gtk_file_chooser_class_init (gpointer g_iface);
Owen Taylor's avatar
Owen Taylor committed
31 32 33 34 35 36 37 38

GType
gtk_file_chooser_get_type (void)
{
  static GType file_chooser_type = 0;

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

  return file_chooser_type;
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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
67
static void
68
gtk_file_chooser_class_init (gpointer g_iface)
Owen Taylor's avatar
Owen Taylor committed
69
{
70
  GType iface_type = G_TYPE_FROM_INTERFACE (g_iface);
Owen Taylor's avatar
Owen Taylor committed
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  /**
   * 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
90
  g_signal_new (I_("current-folder-changed"),
91 92 93 94 95 96
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, current_folder_changed),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

  /**
   * 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
117
  g_signal_new (I_("selection-changed"),
118 119 120 121 122 123
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, selection_changed),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

  /**
   * 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
140
   * must call gtk_file_chooser_set_preview_widget_active() to inform the file
141 142 143 144 145 146 147 148 149 150
   * 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
151
  g_signal_new (I_("update-preview"),
152 153 154 155 156 157
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, update_preview),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

  /**
   * 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
175
  g_signal_new (I_("file-activated"),
176 177 178 179 180 181
		iface_type,
		G_SIGNAL_RUN_LAST,
		G_STRUCT_OFFSET (GtkFileChooserIface, file_activated),
		NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);
182

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

255 256 257 258 259 260 261 262 263
  /**
   * 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
   */
264 265 266
  g_object_interface_install_property (g_iface,
				       g_param_spec_boolean ("do-overwrite-confirmation",
							     P_("Do overwrite confirmation"),
267
							     P_("Whether a file chooser in save mode "
268
								"will present an overwrite confirmation dialog "
269
								"if necessary."),
270 271
							     FALSE,
							     GTK_PARAM_READWRITE));
Owen Taylor's avatar
Owen Taylor committed
272 273
}

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

289 290 291 292 293
/**
 * gtk_file_chooser_set_action:
 * @chooser: a #GtkFileChooser
 * @action: the action that the file selector is performing
 * 
294
 * Sets the type of operation that the chooser is performing; the
295 296 297 298
 * 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.
299 300
 *
 * Since: 2.4
301
 **/
Owen Taylor's avatar
Owen Taylor committed
302 303 304 305 306 307 308 309 310
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);
}

311 312 313 314 315 316 317 318
/**
 * 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
319 320
 *
 * Since: 2.4
321
 **/
Owen Taylor's avatar
Owen Taylor committed
322 323 324 325 326 327 328 329 330 331 332 333
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;
}

334 335 336 337 338 339 340 341 342 343 344 345 346 347
/**
 * 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(),
348 349
 *
 * Since: 2.4
350
 **/
Owen Taylor's avatar
Owen Taylor committed
351 352 353 354 355 356
void
gtk_file_chooser_set_local_only (GtkFileChooser *chooser,
				 gboolean        local_only)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));

357
  g_object_set (chooser, "local-only", local_only, NULL);
Owen Taylor's avatar
Owen Taylor committed
358 359
}

360 361 362 363 364 365 366 367
/**
 * 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.
368 369
 *
 * Since: 2.4
370
 **/
Owen Taylor's avatar
Owen Taylor committed
371 372 373 374 375 376 377
gboolean
gtk_file_chooser_get_local_only (GtkFileChooser *chooser)
{
  gboolean local_only;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);

378
  g_object_get (chooser, "local-only", &local_only, NULL);
Owen Taylor's avatar
Owen Taylor committed
379 380 381 382

  return local_only;
}

383 384 385 386 387
/**
 * gtk_file_chooser_set_select_multiple:
 * @chooser: a #GtkFileChooser
 * @select_multiple: %TRUE if multiple files can be selected.
 * 
388 389
 * 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
Matthias Clasen's avatar
Matthias Clasen committed
390
 * GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.  
391 392
 *
 * Since: 2.4
393
 **/
Owen Taylor's avatar
Owen Taylor committed
394 395 396 397 398 399
void
gtk_file_chooser_set_select_multiple (GtkFileChooser *chooser,
				      gboolean        select_multiple)
{
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));

400
  g_object_set (chooser, "select-multiple", select_multiple, NULL);
Owen Taylor's avatar
Owen Taylor committed
401 402
}

403 404 405 406 407 408 409 410
/**
 * 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.
411 412
 *
 * Since: 2.4
413
 **/
Owen Taylor's avatar
Owen Taylor committed
414 415 416 417 418 419 420
gboolean
gtk_file_chooser_get_select_multiple (GtkFileChooser *chooser)
{
  gboolean select_multiple;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);

421
  g_object_get (chooser, "select-multiple", &select_multiple, NULL);
Owen Taylor's avatar
Owen Taylor committed
422 423 424 425

  return select_multiple;
}

426 427 428 429 430 431 432
/**
 * 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.
433 434 435
 *
 * If the file chooser is in folder mode, this function returns the selected
 * folder.
436 437 438
 * 
 * Return value: The currently selected filename, or %NULL
 *  if no file is selected, or the selected file can't
439
 *  be represented with a local filename. Free with g_free().
440 441
 *
 * Since: 2.4
442
 **/
443
gchar *
Owen Taylor's avatar
Owen Taylor committed
444 445
gtk_file_chooser_get_filename (GtkFileChooser *chooser)
{
446
  GFile *file;
Owen Taylor's avatar
Owen Taylor committed
447
  gchar *result = NULL;
448

Owen Taylor's avatar
Owen Taylor committed
449 450
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

451 452 453
  file = gtk_file_chooser_get_file (chooser);

  if (file)
Owen Taylor's avatar
Owen Taylor committed
454
    {
455
      result = g_file_get_path (file);
456
      g_object_unref (file);
Owen Taylor's avatar
Owen Taylor committed
457 458 459 460 461
    }

  return result;
}

462 463 464 465 466
/**
 * gtk_file_chooser_set_filename:
 * @chooser: a #GtkFileChooser
 * @filename: the filename to set as current
 * 
467 468 469 470 471 472 473 474 475
 * 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().
476 477
 *
 * Note that the file must exist, or nothing will be done except
478 479
 * for the directory change.
 *
Matthias Clasen's avatar
Matthias Clasen committed
480 481 482 483 484 485 486 487
 * 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:
 * |[
488 489
 * if (document_is_new)
 *   {
Matthias Clasen's avatar
Matthias Clasen committed
490
 *     /&ast; the user just created a new document &ast;/
491 492 493 494 495
 *     gtk_file_chooser_set_current_folder (chooser, default_folder_for_saving);
 *     gtk_file_chooser_set_current_name (chooser, "Untitled document");
 *   }
 * else
 *   {
Matthias Clasen's avatar
Matthias Clasen committed
496
 *     /&ast; the user edited an existing document &ast;/ 
497 498
 *     gtk_file_chooser_set_filename (chooser, existing_filename);
 *   }
Matthias Clasen's avatar
Matthias Clasen committed
499
 * ]|
500
 * 
501 502 503
 * Return value: %TRUE if both the folder could be changed and the file was
 * selected successfully, %FALSE otherwise.
 *
504
 * Since: 2.4
505
 **/
506
gboolean
Owen Taylor's avatar
Owen Taylor committed
507
gtk_file_chooser_set_filename (GtkFileChooser *chooser,
508
			       const gchar    *filename)
Owen Taylor's avatar
Owen Taylor committed
509
{
510
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
511 512

  gtk_file_chooser_unselect_all (chooser);
513
  return gtk_file_chooser_select_filename (chooser, filename);
Owen Taylor's avatar
Owen Taylor committed
514 515
}

516 517 518 519 520 521 522 523
/**
 * 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.
524
 *
525 526 527
 * Return value: %TRUE if both the folder could be changed and the file was
 * selected successfully, %FALSE otherwise.
 *
528
 * Since: 2.4
529
 **/
530
gboolean
Owen Taylor's avatar
Owen Taylor committed
531
gtk_file_chooser_select_filename (GtkFileChooser *chooser,
532
				  const gchar    *filename)
Owen Taylor's avatar
Owen Taylor committed
533
{
534
  GFile *file;
535
  gboolean result;
536
  
537 538
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);
539

540
  file = g_file_new_for_path (filename);
541
  result = gtk_file_chooser_select_file (chooser, file, NULL);
542
  g_object_unref (file);
543 544

  return result;
Owen Taylor's avatar
Owen Taylor committed
545 546
}

547 548 549 550 551 552 553 554
/**
 * 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.
555 556
 *
 * Since: 2.4
557
 **/
Owen Taylor's avatar
Owen Taylor committed
558 559 560 561
void
gtk_file_chooser_unselect_filename (GtkFileChooser *chooser,
				    const char     *filename)
{
562 563
  GFile *file;

Owen Taylor's avatar
Owen Taylor committed
564 565
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
  g_return_if_fail (filename != NULL);
566

567
  file = g_file_new_for_path (filename);
568
  gtk_file_chooser_unselect_file (chooser, file);
569
  g_object_unref (file);
Owen Taylor's avatar
Owen Taylor committed
570 571
}

572
/* Converts a list of GFile* to a list of strings using the specified function */
573
static GSList *
574 575
files_to_strings (GSList  *files,
		  gchar * (*convert_func) (GFile *file))
576 577 578 579 580
{
  GSList *strings;

  strings = NULL;

581
  for (; files; files = files->next)
582
    {
583
      GFile *file;
584 585
      gchar *string;

586 587
      file = files->data;
      string = (* convert_func) (file);
588 589 590 591 592 593 594 595

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

  return g_slist_reverse (strings);
}

596 597 598 599
/**
 * gtk_file_chooser_get_filenames:
 * @chooser: a #GtkFileChooser
 * 
600 601 602 603
 * 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())
604
 * 
605
 * Return value: a #GSList containing the filenames of all selected
606
 *   files and subfolders in the current folder. Free the returned list
607
 *   with g_slist_free(), and the filenames with g_free().
608 609
 *
 * Since: 2.4
610
 **/
Owen Taylor's avatar
Owen Taylor committed
611 612 613
GSList *
gtk_file_chooser_get_filenames (GtkFileChooser *chooser)
{
614
  GSList *files, *result;
615
  
Owen Taylor's avatar
Owen Taylor committed
616 617
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

618
  files = gtk_file_chooser_get_files (chooser);
619 620 621 622

  result = files_to_strings (files, g_file_get_path);
  g_slist_foreach (files, (GFunc) g_object_unref, NULL);
  g_slist_free (files);
623

624
  return result;
Owen Taylor's avatar
Owen Taylor committed
625 626
}

627 628 629 630 631 632 633 634
/**
 * 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.
635
 *
636 637 638
 * Return value: %TRUE if the folder could be changed successfully, %FALSE
 * otherwise.
 *
639
 * Since: 2.4
640
 **/
641
gboolean
Owen Taylor's avatar
Owen Taylor committed
642 643 644
gtk_file_chooser_set_current_folder (GtkFileChooser *chooser,
				     const gchar    *filename)
{
645
  GFile *file;
646
  gboolean result;
647
  
648 649
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (filename != NULL, FALSE);
650

651
  file = g_file_new_for_path (filename);
652
  result = gtk_file_chooser_set_current_folder_file (chooser, file, NULL);
653
  g_object_unref (file);
654 655

  return result;
Owen Taylor's avatar
Owen Taylor committed
656 657
}

658 659 660 661 662 663
/**
 * 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().
664 665 666 667 668 669 670 671
 *
 * 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.
672
 * 
Federico Mena Quintero's avatar
Federico Mena Quintero committed
673 674 675 676 677
 * 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.
678 679
 *
 * Since: 2.4
680
 **/
Owen Taylor's avatar
Owen Taylor committed
681 682 683
gchar *
gtk_file_chooser_get_current_folder (GtkFileChooser *chooser)
{
684
  GFile *file;
685 686
  gchar *filename;
  
Owen Taylor's avatar
Owen Taylor committed
687 688
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

689
  file = gtk_file_chooser_get_current_folder_file (chooser);
690
  if (!file)
691 692
    return NULL;

693 694
  filename = g_file_get_path (file);
  g_object_unref (file);
695 696

  return filename;
Owen Taylor's avatar
Owen Taylor committed
697 698
}

699 700 701 702 703 704 705 706 707 708
/**
 * 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.
 *
709 710 711 712
 * 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.
713 714
 *
 * Since: 2.4
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
 **/
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.
 * 
734 735 736
 * If the file chooser is in folder mode, this function returns the selected
 * folder.
 * 
737 738
 * Return value: The currently selected URI, or %NULL
 *  if no file is selected. Free with g_free()
739 740
 *
 * Since: 2.4
741
 **/
Owen Taylor's avatar
Owen Taylor committed
742 743 744
gchar *
gtk_file_chooser_get_uri (GtkFileChooser *chooser)
{
745
  GFile *file;
Owen Taylor's avatar
Owen Taylor committed
746 747 748 749
  gchar *result = NULL;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

750 751
  file = gtk_file_chooser_get_file (chooser);
  if (file)
Owen Taylor's avatar
Owen Taylor committed
752
    {
753 754
      result = g_file_get_uri (file);
      g_object_unref (file);
Owen Taylor's avatar
Owen Taylor committed
755 756 757 758 759
    }

  return result;
}

760 761 762 763 764
/**
 * gtk_file_chooser_set_uri:
 * @chooser: a #GtkFileChooser
 * @uri: the URI to set as current
 * 
765 766 767 768
 * 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.
769
 *
770 771 772 773 774
 * 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().
 *
Matthias Clasen's avatar
Matthias Clasen committed
775 776 777 778 779 780 781 782 783 784
 * 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:
 * |[
785 786
 * if (document_is_new)
 *   {
Matthias Clasen's avatar
Matthias Clasen committed
787
 *     /&ast; the user just created a new document &ast;/
788 789 790 791 792
 *     gtk_file_chooser_set_current_folder_uri (chooser, default_folder_for_saving);
 *     gtk_file_chooser_set_current_name (chooser, "Untitled document");
 *   }
 * else
 *   {
Matthias Clasen's avatar
Matthias Clasen committed
793
 *     /&ast; the user edited an existing document &ast;/ 
794 795
 *     gtk_file_chooser_set_uri (chooser, existing_uri);
 *   }
Matthias Clasen's avatar
Matthias Clasen committed
796
 * ]|
797
 *
798 799 800
 * Return value: %TRUE if both the folder could be changed and the URI was
 * selected successfully, %FALSE otherwise.
 *
801
 * Since: 2.4
802
 **/
803
gboolean
Owen Taylor's avatar
Owen Taylor committed
804 805 806
gtk_file_chooser_set_uri (GtkFileChooser *chooser,
			  const char     *uri)
{
807
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
808 809

  gtk_file_chooser_unselect_all (chooser);
810
  return gtk_file_chooser_select_uri (chooser, uri);
Owen Taylor's avatar
Owen Taylor committed
811 812
}

813 814 815 816 817 818 819 820
/**
 * 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.
821
 *
822 823 824
 * Return value: %TRUE if both the folder could be changed and the URI was
 * selected successfully, %FALSE otherwise.
 *
825
 * Since: 2.4
826
 **/
827
gboolean
Owen Taylor's avatar
Owen Taylor committed
828 829 830
gtk_file_chooser_select_uri (GtkFileChooser *chooser,
			     const char     *uri)
{
831
  GFile *file;
832
  gboolean result;
Owen Taylor's avatar
Owen Taylor committed
833
  
834 835
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
  g_return_val_if_fail (uri != NULL, FALSE);
836

837
  file = g_file_new_for_uri (uri);
838
  result = gtk_file_chooser_select_file (chooser, file, NULL);
839
  g_object_unref (file);
840 841

  return result;
Owen Taylor's avatar
Owen Taylor committed
842 843
}

844 845 846 847 848 849 850 851
/**
 * 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.
852 853
 *
 * Since: 2.4
854
 **/
Owen Taylor's avatar
Owen Taylor committed
855 856 857 858
void
gtk_file_chooser_unselect_uri (GtkFileChooser *chooser,
			       const char     *uri)
{
859 860
  GFile *file;

861 862 863
  g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
  g_return_if_fail (uri != NULL);

864
  file = g_file_new_for_uri (uri);
865
  gtk_file_chooser_unselect_file (chooser, file);
866
  g_object_unref (file);
Owen Taylor's avatar
Owen Taylor committed
867 868
}

869 870 871 872 873
/**
 * 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
874 875
 *
 * Since: 2.4
876
 **/
Owen Taylor's avatar
Owen Taylor committed
877 878 879 880 881 882 883 884
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);
}

885 886 887 888 889
/**
 * 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
890 891
 *
 * Since: 2.4
892
 **/
Owen Taylor's avatar
Owen Taylor committed
893 894 895 896 897 898 899 900 901
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);
}

902
/**
903
 * gtk_file_chooser_get_uris:
904 905
 * @chooser: a #GtkFileChooser
 * 
906
 * Lists all the selected files and subfolders in the current folder of
907 908
 * @chooser. The returned names are full absolute URIs.
 * 
909
 * Return value: a #GSList containing the URIs of all selected
910
 *   files and subfolders in the current folder. Free the returned list
911
 *   with g_slist_free(), and the filenames with g_free().
912 913
 *
 * Since: 2.4
914
 **/
Owen Taylor's avatar
Owen Taylor committed
915 916 917
GSList *
gtk_file_chooser_get_uris (GtkFileChooser *chooser)
{
918
  GSList *files, *result;
Owen Taylor's avatar
Owen Taylor committed
919
  
920 921
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

922
  files = gtk_file_chooser_get_files (chooser);
923 924 925 926

  result = files_to_strings (files, g_file_get_uri);
  g_slist_foreach (files, (GFunc) g_object_unref, NULL);
  g_slist_free (files);
927

928
  return result;
Owen Taylor's avatar
Owen Taylor committed
929 930
}

931 932 933 934 935 936 937 938
/**
 * 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.
939
 *
940 941 942
 * Return value: %TRUE if the folder could be changed successfully, %FALSE
 * otherwise.
 *
943
 * Since: 2.4
944
 **/
945
gboolean
Owen Taylor's avatar
Owen Taylor committed
946 947 948
gtk_file_chooser_set_current_folder_uri (GtkFileChooser *chooser,
					 const gchar    *uri)
{
949
  GFile *file;
950
  gboolean result;
951
  
952 953
  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
954

955
  file = g_file_new_for_uri (uri);
956
  result = gtk_file_chooser_set_current_folder_file (chooser, file, NULL);
957
  g_object_unref (file);
958 959

  return result;
960
}
Owen Taylor's avatar
Owen Taylor committed
961

962 963 964 965 966 967
/**
 * 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().
968 969 970 971 972 973 974 975
 *
 * 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.
976
 * 
Federico Mena Quintero's avatar
Federico Mena Quintero committed
977 978 979 980
 * 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.
981 982
 *
 * Since: 2.4
983
 */
Owen Taylor's avatar
Owen Taylor committed
984 985
gchar *
gtk_file_chooser_get_current_folder_uri (GtkFileChooser *chooser)
986
{
987
  GFile *file;
988 989 990 991
  gchar *uri;
  
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

992
  file = gtk_file_chooser_get_current_folder_file (chooser);
993
  if (!file)
994 995
    return NULL;

996 997
  uri = g_file_get_uri (file);
  g_object_unref (file);
998 999 1000 1001

  return uri;
}

1002
/**
1003
 * gtk_file_chooser_set_current_folder_file:
1004
 * @chooser: a #GtkFileChooser
1005
 * @file: the #GFile for the new folder
1006
 * @error: location to store error, or %NULL.
1007
 * 
1008
 * Sets the current folder for @chooser from a #GFile.
1009
 * Internal function, see gtk_file_chooser_set_current_folder_uri().
1010
 *
1011 1012 1013
 * Return value: %TRUE if the folder could be changed successfully, %FALSE
 * otherwise.
 *
1014
 * Since: 2.14
1015
 **/
1016
gboolean
1017 1018 1019
gtk_file_chooser_set_current_folder_file (GtkFileChooser  *chooser,
                                          GFile           *file,
                                          GError         **error)
1020
{
1021
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), FALSE);
1022
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
1023
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1024

1025
  return GTK_FILE_CHOOSER_GET_IFACE (chooser)->set_current_folder (chooser, file, error);
1026 1027
}

1028
/**
1029
 * gtk_file_chooser_get_current_folder_file:
1030 1031
 * @chooser: a #GtkFileChooser
 * 
1032
 * Gets the current folder of @chooser as #GFile.
1033 1034
 * See gtk_file_chooser_get_current_folder_uri().
 * 
1035
 * Return value: the #GFile for the current folder.
1036
 *
1037
 * Since: 2.14
1038
 */
1039
GFile *
1040
gtk_file_chooser_get_current_folder_file (GtkFileChooser *chooser)
Owen Taylor's avatar
Owen Taylor committed
1041 1042 1043 1044 1045 1046
{
  g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);

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

1047
/**
1048
 * gtk_file_chooser_select_file:
1049
 * @chooser: a #GtkFileChooser
1050
 * @file: the file to select
1051
 * @error: location to store error, or %NULL