Remove filename/URI API from GtkFileChooser

GtkFileChooser's API predates GIO by a few years, so it started off with
filenames and URI as character arrays. After introducing GIO as a
dependency, the API included GFile-based entry points.

It's much more appropriate to use GFile everywhere, as we want to
encourage people to use GIO instead of passing random bytes to low level
POSIX API.

See: #2455
This commit is contained in:
Emmanuele Bassi 2020-02-21 15:11:15 +00:00
parent b002572824
commit 5f070ff233
18 changed files with 512 additions and 1239 deletions

View File

@ -221,9 +221,12 @@ open_cb (GtkWidget *button,
GTK_FILE_CHOOSER_ACTION_OPEN, GTK_FILE_CHOOSER_ACTION_OPEN,
"_Load", "_Load",
"_Cancel"); "_Cancel");
gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE); gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), ".");
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd);
g_signal_connect (dialog, "response", G_CALLBACK (open_response_cb), self); g_signal_connect (dialog, "response", G_CALLBACK (open_response_cb), self);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog)); gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog));
} }
@ -290,14 +293,20 @@ save_response_cb (GtkNativeDialog *dialog,
if (response == GTK_RESPONSE_ACCEPT) if (response == GTK_RESPONSE_ACCEPT)
{ {
GListModel *model; GListModel *model;
char *text, *filename; GFile *file;
char *text;
GError *error = NULL; GError *error = NULL;
model = constraint_view_get_model (CONSTRAINT_VIEW (self->view)); model = constraint_view_get_model (CONSTRAINT_VIEW (self->view));
text = serialize_model (model); text = serialize_model (model);
file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); g_file_replace_contents (file, text, -1,
if (!g_file_set_contents (filename, text, -1, &error)) NULL, FALSE,
G_FILE_CREATE_NONE,
NULL,
NULL,
&error);
if (error != NULL)
{ {
GtkWidget *dialog; GtkWidget *dialog;
@ -312,7 +321,9 @@ save_response_cb (GtkNativeDialog *dialog,
gtk_widget_show (dialog); gtk_widget_show (dialog);
g_error_free (error); g_error_free (error);
} }
g_free (filename);
g_free (text);
g_object_unref (file);
} }
gtk_native_dialog_destroy (dialog); gtk_native_dialog_destroy (dialog);
@ -329,9 +340,12 @@ save_cb (GtkWidget *button,
GTK_FILE_CHOOSER_ACTION_SAVE, GTK_FILE_CHOOSER_ACTION_SAVE,
"_Save", "_Save",
"_Cancel"); "_Cancel");
gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE); gtk_native_dialog_set_modal (GTK_NATIVE_DIALOG (dialog), TRUE);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), ".");
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd);
g_signal_connect (dialog, "response", G_CALLBACK (save_response_cb), self); g_signal_connect (dialog, "response", G_CALLBACK (save_response_cb), self);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog)); gtk_native_dialog_show (GTK_NATIVE_DIALOG (dialog));
} }

View File

@ -385,7 +385,11 @@ show_open_filechooser (NodeEditorWindow *self)
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT); gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), ".");
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd);
g_signal_connect (dialog, "response", G_CALLBACK (open_response_cb), self); g_signal_connect (dialog, "response", G_CALLBACK (open_response_cb), self);
gtk_widget_show (dialog); gtk_widget_show (dialog);
} }
@ -406,13 +410,20 @@ save_response_cb (GtkWidget *dialog,
if (response == GTK_RESPONSE_ACCEPT) if (response == GTK_RESPONSE_ACCEPT)
{ {
char *text, *filename; GFile *file;
char *text;
GError *error = NULL; GError *error = NULL;
text = get_current_text (self->text_buffer); text = get_current_text (self->text_buffer);
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
if (!g_file_set_contents (filename, text, -1, &error)) g_file_replace_contents (file, text, -1,
NULL, FALSE,
G_FILE_CREATE_NONE,
NULL,
NULL,
&error);
if (error != NULL)
{ {
GtkWidget *dialog; GtkWidget *dialog;
@ -427,7 +438,9 @@ save_response_cb (GtkWidget *dialog,
gtk_widget_show (dialog); gtk_widget_show (dialog);
g_error_free (error); g_error_free (error);
} }
g_free (filename);
g_free (text);
g_object_unref (file);
} }
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
@ -449,7 +462,11 @@ save_cb (GtkWidget *button,
gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT); gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
gtk_window_set_modal (GTK_WINDOW (dialog), TRUE); gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), ".");
GFile *cwd = g_file_new_for_path (".");
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), cwd, NULL);
g_object_unref (cwd);
g_signal_connect (dialog, "response", G_CALLBACK (save_response_cb), self); g_signal_connect (dialog, "response", G_CALLBACK (save_response_cb), self);
gtk_widget_show (dialog); gtk_widget_show (dialog);
} }
@ -523,10 +540,10 @@ export_image_response_cb (GtkWidget *dialog,
if (response == GTK_RESPONSE_ACCEPT) if (response == GTK_RESPONSE_ACCEPT)
{ {
char *filename; GFile *file;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
if (!gdk_texture_save_to_png (texture, filename)) if (!gdk_texture_save_to_png (texture, g_file_peek_path (file)))
{ {
GtkWidget *message_dialog; GtkWidget *message_dialog;
@ -538,7 +555,8 @@ export_image_response_cb (GtkWidget *dialog,
g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL); g_signal_connect (message_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (message_dialog); gtk_widget_show (message_dialog);
} }
g_free (filename);
g_object_unref (file);
} }
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);

View File

@ -1140,51 +1140,37 @@ gtk_file_chooser_set_create_folders
gtk_file_chooser_get_create_folders gtk_file_chooser_get_create_folders
gtk_file_chooser_set_current_name gtk_file_chooser_set_current_name
gtk_file_chooser_get_current_name gtk_file_chooser_get_current_name
gtk_file_chooser_get_filename <SUBSECTION>
gtk_file_chooser_set_filename gtk_file_chooser_get_file
gtk_file_chooser_select_filename gtk_file_chooser_set_file
gtk_file_chooser_unselect_filename gtk_file_chooser_select_file
gtk_file_chooser_unselect_file
gtk_file_chooser_select_all gtk_file_chooser_select_all
gtk_file_chooser_unselect_all gtk_file_chooser_unselect_all
gtk_file_chooser_get_filenames gtk_file_chooser_get_files
gtk_file_chooser_set_current_folder gtk_file_chooser_set_current_folder
gtk_file_chooser_get_current_folder gtk_file_chooser_get_current_folder
gtk_file_chooser_get_uri <SUBSECTION>
gtk_file_chooser_set_uri
gtk_file_chooser_select_uri
gtk_file_chooser_unselect_uri
gtk_file_chooser_get_uris
gtk_file_chooser_set_current_folder_uri
gtk_file_chooser_get_current_folder_uri
gtk_file_chooser_set_preview_widget gtk_file_chooser_set_preview_widget
gtk_file_chooser_get_preview_widget gtk_file_chooser_get_preview_widget
gtk_file_chooser_set_preview_widget_active gtk_file_chooser_set_preview_widget_active
gtk_file_chooser_get_preview_widget_active gtk_file_chooser_get_preview_widget_active
gtk_file_chooser_set_use_preview_label gtk_file_chooser_set_use_preview_label
gtk_file_chooser_get_use_preview_label gtk_file_chooser_get_use_preview_label
gtk_file_chooser_get_preview_filename gtk_file_chooser_get_preview_file
gtk_file_chooser_get_preview_uri
gtk_file_chooser_set_extra_widget gtk_file_chooser_set_extra_widget
gtk_file_chooser_get_extra_widget gtk_file_chooser_get_extra_widget
<SUBSECTION>
gtk_file_chooser_add_filter gtk_file_chooser_add_filter
gtk_file_chooser_remove_filter gtk_file_chooser_remove_filter
gtk_file_chooser_list_filters gtk_file_chooser_list_filters
gtk_file_chooser_set_filter gtk_file_chooser_set_filter
gtk_file_chooser_get_filter gtk_file_chooser_get_filter
<SUBSECTION>
gtk_file_chooser_add_shortcut_folder gtk_file_chooser_add_shortcut_folder
gtk_file_chooser_remove_shortcut_folder gtk_file_chooser_remove_shortcut_folder
gtk_file_chooser_list_shortcut_folders gtk_file_chooser_list_shortcut_folders
gtk_file_chooser_add_shortcut_folder_uri <SUBSECTION>
gtk_file_chooser_remove_shortcut_folder_uri
gtk_file_chooser_list_shortcut_folder_uris
gtk_file_chooser_get_current_folder_file
gtk_file_chooser_get_file
gtk_file_chooser_get_files
gtk_file_chooser_get_preview_file
gtk_file_chooser_select_file
gtk_file_chooser_set_current_folder_file
gtk_file_chooser_set_file
gtk_file_chooser_unselect_file
gtk_file_chooser_add_choice gtk_file_chooser_add_choice
gtk_file_chooser_remove_choice gtk_file_chooser_remove_choice
gtk_file_chooser_set_choice gtk_file_chooser_set_choice

File diff suppressed because it is too large Load Diff

View File

@ -156,52 +156,10 @@ void gtk_file_chooser_set_current_name (GtkFileChooser *chooser,
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
gchar *gtk_file_chooser_get_current_name (GtkFileChooser *chooser); gchar *gtk_file_chooser_get_current_name (GtkFileChooser *chooser);
/* Filename manipulation
*/
GDK_AVAILABLE_IN_ALL
gchar * gtk_file_chooser_get_filename (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_set_filename (GtkFileChooser *chooser,
const char *filename);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_select_filename (GtkFileChooser *chooser,
const char *filename);
GDK_AVAILABLE_IN_ALL
void gtk_file_chooser_unselect_filename (GtkFileChooser *chooser,
const char *filename);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_file_chooser_select_all (GtkFileChooser *chooser); void gtk_file_chooser_select_all (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_file_chooser_unselect_all (GtkFileChooser *chooser); void gtk_file_chooser_unselect_all (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
GSList * gtk_file_chooser_get_filenames (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_set_current_folder (GtkFileChooser *chooser,
const gchar *filename);
GDK_AVAILABLE_IN_ALL
gchar * gtk_file_chooser_get_current_folder (GtkFileChooser *chooser);
/* URI manipulation
*/
GDK_AVAILABLE_IN_ALL
gchar * gtk_file_chooser_get_uri (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_set_uri (GtkFileChooser *chooser,
const char *uri);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_select_uri (GtkFileChooser *chooser,
const char *uri);
GDK_AVAILABLE_IN_ALL
void gtk_file_chooser_unselect_uri (GtkFileChooser *chooser,
const char *uri);
GDK_AVAILABLE_IN_ALL
GSList * gtk_file_chooser_get_uris (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_set_current_folder_uri (GtkFileChooser *chooser,
const gchar *uri);
GDK_AVAILABLE_IN_ALL
gchar * gtk_file_chooser_get_current_folder_uri (GtkFileChooser *chooser);
/* GFile manipulation */ /* GFile manipulation */
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
@ -220,11 +178,11 @@ void gtk_file_chooser_unselect_file (GtkFileChooser *chooser,
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GSList * gtk_file_chooser_get_files (GtkFileChooser *chooser); GSList * gtk_file_chooser_get_files (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_set_current_folder_file (GtkFileChooser *chooser, gboolean gtk_file_chooser_set_current_folder (GtkFileChooser *chooser,
GFile *file, GFile *file,
GError **error); GError **error);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GFile * gtk_file_chooser_get_current_folder_file (GtkFileChooser *chooser); GFile * gtk_file_chooser_get_current_folder (GtkFileChooser *chooser);
/* Preview widget /* Preview widget
*/ */
@ -244,10 +202,6 @@ void gtk_file_chooser_set_use_preview_label (GtkFileChooser *chooser,
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_get_use_preview_label (GtkFileChooser *chooser); gboolean gtk_file_chooser_get_use_preview_label (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
char *gtk_file_chooser_get_preview_filename (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
char *gtk_file_chooser_get_preview_uri (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GFile *gtk_file_chooser_get_preview_file (GtkFileChooser *chooser); GFile *gtk_file_chooser_get_preview_file (GtkFileChooser *chooser);
@ -274,7 +228,7 @@ GSList *gtk_file_chooser_list_filters (GtkFileChooser *chooser);
*/ */
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_file_chooser_set_filter (GtkFileChooser *chooser, void gtk_file_chooser_set_filter (GtkFileChooser *chooser,
GtkFileFilter *filter); GtkFileFilter *filter);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GtkFileFilter *gtk_file_chooser_get_filter (GtkFileChooser *chooser); GtkFileFilter *gtk_file_chooser_get_filter (GtkFileChooser *chooser);
@ -282,26 +236,15 @@ GtkFileFilter *gtk_file_chooser_get_filter (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_add_shortcut_folder (GtkFileChooser *chooser, gboolean gtk_file_chooser_add_shortcut_folder (GtkFileChooser *chooser,
const char *folder, GFile *folder,
GError **error); GError **error);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_remove_shortcut_folder (GtkFileChooser *chooser, gboolean gtk_file_chooser_remove_shortcut_folder (GtkFileChooser *chooser,
const char *folder, GFile *folder,
GError **error); GError **error);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
GSList *gtk_file_chooser_list_shortcut_folders (GtkFileChooser *chooser); GSList *gtk_file_chooser_list_shortcut_folders (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_add_shortcut_folder_uri (GtkFileChooser *chooser,
const char *uri,
GError **error);
GDK_AVAILABLE_IN_ALL
gboolean gtk_file_chooser_remove_shortcut_folder_uri (GtkFileChooser *chooser,
const char *uri,
GError **error);
GDK_AVAILABLE_IN_ALL
GSList *gtk_file_chooser_list_shortcut_folder_uris (GtkFileChooser *chooser);
GDK_AVAILABLE_IN_ALL GDK_AVAILABLE_IN_ALL
void gtk_file_chooser_add_choice (GtkFileChooser *chooser, void gtk_file_chooser_add_choice (GtkFileChooser *chooser,
const char *id, const char *id,

View File

@ -80,11 +80,12 @@
* |[<!-- language="C" --> * |[<!-- language="C" -->
* { * {
* GtkWidget *button; * GtkWidget *button;
* GFile *cwd = g_file_new_for_path ("/etc");
* *
* button = gtk_file_chooser_button_new (_("Select a file"), * button = gtk_file_chooser_button_new (_("Select a file"),
* GTK_FILE_CHOOSER_ACTION_OPEN); * GTK_FILE_CHOOSER_ACTION_OPEN);
* gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button), * gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (button), cwd);
* "/etc"); * g_object_unref (cwd);
* } * }
* ]| * ]|
* *
@ -556,7 +557,7 @@ gtk_file_chooser_button_set_current_folder (GtkFileChooser *chooser,
g_signal_emit_by_name (button, "current-folder-changed"); g_signal_emit_by_name (button, "current-folder-changed");
if (priv->active) if (priv->active)
gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->chooser), file, NULL); gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (priv->chooser), file, NULL);
return TRUE; return TRUE;
} }
@ -688,7 +689,7 @@ gtk_file_chooser_button_add_shortcut_folder (GtkFileChooser *chooser,
delegate = g_object_get_qdata (G_OBJECT (chooser), delegate = g_object_get_qdata (G_OBJECT (chooser),
GTK_FILE_CHOOSER_DELEGATE_QUARK); GTK_FILE_CHOOSER_DELEGATE_QUARK);
retval = _gtk_file_chooser_add_shortcut_folder (delegate, file, error); retval = gtk_file_chooser_add_shortcut_folder (delegate, file, error);
if (retval) if (retval)
{ {
@ -728,7 +729,7 @@ gtk_file_chooser_button_remove_shortcut_folder (GtkFileChooser *chooser,
delegate = g_object_get_qdata (G_OBJECT (chooser), delegate = g_object_get_qdata (G_OBJECT (chooser),
GTK_FILE_CHOOSER_DELEGATE_QUARK); GTK_FILE_CHOOSER_DELEGATE_QUARK);
retval = _gtk_file_chooser_remove_shortcut_folder (delegate, file, error); retval = gtk_file_chooser_remove_shortcut_folder (delegate, file, error);
if (retval) if (retval)
{ {
@ -2550,7 +2551,7 @@ save_inactive_state (GtkFileChooserButton *button)
if (priv->selection_while_inactive) if (priv->selection_while_inactive)
g_object_unref (priv->selection_while_inactive); g_object_unref (priv->selection_while_inactive);
priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder_file (GTK_FILE_CHOOSER (priv->chooser)); priv->current_folder_while_inactive = gtk_file_chooser_get_current_folder (GTK_FILE_CHOOSER (priv->chooser));
priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->chooser)); priv->selection_while_inactive = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (priv->chooser));
} }
@ -2560,7 +2561,7 @@ restore_inactive_state (GtkFileChooserButton *button)
GtkFileChooserButtonPrivate *priv = gtk_file_chooser_button_get_instance_private (button); GtkFileChooserButtonPrivate *priv = gtk_file_chooser_button_get_instance_private (button);
if (priv->current_folder_while_inactive) if (priv->current_folder_while_inactive)
gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (priv->chooser), priv->current_folder_while_inactive, NULL); gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (priv->chooser), priv->current_folder_while_inactive, NULL);
if (priv->selection_while_inactive) if (priv->selection_while_inactive)
gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->chooser), priv->selection_while_inactive, NULL); gtk_file_chooser_select_file (GTK_FILE_CHOOSER (priv->chooser), priv->selection_while_inactive, NULL);

View File

@ -85,11 +85,9 @@
* res = gtk_dialog_run (GTK_DIALOG (dialog)); * res = gtk_dialog_run (GTK_DIALOG (dialog));
* if (res == GTK_RESPONSE_ACCEPT) * if (res == GTK_RESPONSE_ACCEPT)
* { * {
* char *filename;
* GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog); * GtkFileChooser *chooser = GTK_FILE_CHOOSER (dialog);
* filename = gtk_file_chooser_get_filename (chooser); * g_autoptr(GFile) filen = gtk_file_chooser_get_file (chooser);
* open_file (filename); * open_file (file);
* g_free (filename);
* } * }
* *
* gtk_widget_destroy (dialog); * gtk_widget_destroy (dialog);
@ -116,20 +114,15 @@
* gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE); * gtk_file_chooser_set_do_overwrite_confirmation (chooser, TRUE);
* *
* if (user_edited_a_new_document) * if (user_edited_a_new_document)
* gtk_file_chooser_set_current_name (chooser, * gtk_file_chooser_set_current_name (chooser, _("Untitled document"));
* _("Untitled document"));
* else * else
* gtk_file_chooser_set_filename (chooser, * gtk_file_chooser_set_file (chooser, existing_filename);
* existing_filename);
* *
* res = gtk_dialog_run (GTK_DIALOG (dialog)); * res = gtk_dialog_run (GTK_DIALOG (dialog));
* if (res == GTK_RESPONSE_ACCEPT) * if (res == GTK_RESPONSE_ACCEPT)
* { * {
* char *filename; * g_autoptr(GFile) file = gtk_file_chooser_get_file (chooser);
* * save_to_file (file);
* filename = gtk_file_chooser_get_filename (chooser);
* save_to_file (filename);
* g_free (filename);
* } * }
* *
* gtk_widget_destroy (dialog); * gtk_widget_destroy (dialog);
@ -145,7 +138,7 @@
* and suggest a name such as Untitled with gtk_file_chooser_set_current_name(). * and suggest a name such as Untitled with gtk_file_chooser_set_current_name().
* *
* - To save a file under a different name. Use #GTK_FILE_CHOOSER_ACTION_SAVE, * - To save a file under a different name. Use #GTK_FILE_CHOOSER_ACTION_SAVE,
* and set the existing filename with gtk_file_chooser_set_filename(). * and set the existing file with gtk_file_chooser_set_file().
* *
* - To choose a folder instead of a file. Use #GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER. * - To choose a folder instead of a file. Use #GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER.
* *
@ -156,7 +149,7 @@
* considered to be a good policy, as now the file chooser is * considered to be a good policy, as now the file chooser is
* able to make good suggestions on its own. In general, you * able to make good suggestions on its own. In general, you
* should only cause the file chooser to show a specific folder * should only cause the file chooser to show a specific folder
* when it is appropriate to use gtk_file_chooser_set_filename(), * when it is appropriate to use gtk_file_chooser_set_file(),
* i.e. when you are doing a Save As command and you already * i.e. when you are doing a Save As command and you already
* have a file saved somewhere. * have a file saved somewhere.
@ -362,18 +355,18 @@ file_chooser_widget_selection_changed (GtkWidget *widget,
{ {
GtkFileChooserDialogPrivate *priv = gtk_file_chooser_dialog_get_instance_private (dialog); GtkFileChooserDialogPrivate *priv = gtk_file_chooser_dialog_get_instance_private (dialog);
GtkWidget *button; GtkWidget *button;
GSList *uris; GSList *files;
gboolean sensitive; gboolean sensitive;
button = get_accept_action_widget (GTK_DIALOG (dialog), FALSE); button = get_accept_action_widget (GTK_DIALOG (dialog), FALSE);
if (button == NULL) if (button == NULL)
return; return;
uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (priv->widget)); files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (priv->widget));
sensitive = (uris != NULL); sensitive = (files != NULL);
gtk_widget_set_sensitive (button, sensitive); gtk_widget_set_sensitive (button, sensitive);
g_slist_free_full (uris, g_free); g_slist_free_full (files, g_object_unref);
} }
static void static void

View File

@ -644,8 +644,8 @@ gtk_file_chooser_native_set_current_folder (GtkFileChooser *chooser,
GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (chooser); GtkFileChooserNative *self = GTK_FILE_CHOOSER_NATIVE (chooser);
gboolean res; gboolean res;
res = gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (self->dialog), res = gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (self->dialog),
file, error); file, error);
if (res) if (res)

View File

@ -69,7 +69,7 @@ typedef struct {
char *cancel_label; char *cancel_label;
char *title; char *title;
GSList *shortcut_uris; GSList *shortcut_files;
GArray *choices_selections; GArray *choices_selections;
GFile *current_folder; GFile *current_folder;
@ -331,7 +331,7 @@ filechooser_win32_thread_data_free (FilechooserWin32ThreadData *data)
g_array_free (data->choices_selections, TRUE); g_array_free (data->choices_selections, TRUE);
data->choices_selections = NULL; data->choices_selections = NULL;
} }
g_slist_free_full (data->shortcut_uris, g_free); g_slist_free_full (data->shortcut_files, g_object_unref);
g_slist_free_full (data->files, g_object_unref); g_slist_free_full (data->files, g_object_unref);
if (data->self) if (data->self)
g_object_unref (data->self); g_object_unref (data->self);
@ -538,9 +538,9 @@ filechooser_win32_thread (gpointer _data)
g_free (label); g_free (label);
} }
for (l = data->shortcut_uris; l != NULL; l = l->next) for (l = data->shortcut_files; l != NULL; l = l->next)
{ {
IShellItem *item = get_shell_item_for_uri (l->data); IShellItem *item = get_shell_item_for_file (l->data);
if (item) if (item)
{ {
hr = IFileDialog_AddPlace (pfd, item, FDAP_BOTTOM); hr = IFileDialog_AddPlace (pfd, item, FDAP_BOTTOM);
@ -911,8 +911,8 @@ gtk_file_chooser_native_win32_show (GtkFileChooserNative *self)
self->mode_data = data; self->mode_data = data;
data->self = g_object_ref (self); data->self = g_object_ref (self);
data->shortcut_uris = data->shortcut_files =
gtk_file_chooser_list_shortcut_folder_uris (GTK_FILE_CHOOSER (self->dialog)); gtk_file_chooser_list_shortcut_folders (GTK_FILE_CHOOSER (self->dialog));
data->accept_label = translate_mnemonics (self->accept_label); data->accept_label = translate_mnemonics (self->accept_label);
data->cancel_label = translate_mnemonics (self->cancel_label); data->cancel_label = translate_mnemonics (self->cancel_label);

View File

@ -112,14 +112,6 @@ struct _GtkFileChooserIface
}; };
GtkFileSystem *_gtk_file_chooser_get_file_system (GtkFileChooser *chooser); GtkFileSystem *_gtk_file_chooser_get_file_system (GtkFileChooser *chooser);
gboolean _gtk_file_chooser_add_shortcut_folder (GtkFileChooser *chooser,
GFile *folder,
GError **error);
gboolean _gtk_file_chooser_remove_shortcut_folder (GtkFileChooser *chooser,
GFile *folder,
GError **error);
GSList * _gtk_file_chooser_list_shortcut_folder_files (GtkFileChooser *chooser);
G_END_DECLS G_END_DECLS

View File

@ -289,7 +289,7 @@ delegate_add_shortcut_folder (GtkFileChooser *chooser,
GFile *file, GFile *file,
GError **error) GError **error)
{ {
return _gtk_file_chooser_add_shortcut_folder (get_delegate (chooser), file, error); return gtk_file_chooser_add_shortcut_folder (get_delegate (chooser), file, error);
} }
static gboolean static gboolean
@ -297,13 +297,13 @@ delegate_remove_shortcut_folder (GtkFileChooser *chooser,
GFile *file, GFile *file,
GError **error) GError **error)
{ {
return _gtk_file_chooser_remove_shortcut_folder (get_delegate (chooser), file, error); return gtk_file_chooser_remove_shortcut_folder (get_delegate (chooser), file, error);
} }
static GSList * static GSList *
delegate_list_shortcut_folders (GtkFileChooser *chooser) delegate_list_shortcut_folders (GtkFileChooser *chooser)
{ {
return _gtk_file_chooser_list_shortcut_folder_files (get_delegate (chooser)); return gtk_file_chooser_list_shortcut_folders (get_delegate (chooser));
} }
static gboolean static gboolean
@ -311,13 +311,13 @@ delegate_set_current_folder (GtkFileChooser *chooser,
GFile *file, GFile *file,
GError **error) GError **error)
{ {
return gtk_file_chooser_set_current_folder_file (get_delegate (chooser), file, error); return gtk_file_chooser_set_current_folder (get_delegate (chooser), file, error);
} }
static GFile * static GFile *
delegate_get_current_folder (GtkFileChooser *chooser) delegate_get_current_folder (GtkFileChooser *chooser)
{ {
return gtk_file_chooser_get_current_folder_file (get_delegate (chooser)); return gtk_file_chooser_get_current_folder (get_delegate (chooser));
} }
static void static void

View File

@ -913,7 +913,7 @@ change_folder_and_display_error (GtkFileChooserWidget *impl,
* list_row_activated() * list_row_activated()
* fetches path from model; path belongs to the model (*) * fetches path from model; path belongs to the model (*)
* calls change_folder_and_display_error() * calls change_folder_and_display_error()
* calls gtk_file_chooser_set_current_folder_file() * calls gtk_file_chooser_set_current_folder()
* changing folders fails, sets model to NULL, thus freeing the path in (*) * changing folders fails, sets model to NULL, thus freeing the path in (*)
*/ */
@ -2832,7 +2832,7 @@ switch_to_home_dir (GtkFileChooserWidget *impl)
home_file = g_file_new_for_path (home); home_file = g_file_new_for_path (home);
gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (impl), home_file, NULL); /* NULL-GError */ gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), home_file, NULL); /* NULL-GError */
g_object_unref (home_file); g_object_unref (home_file);
} }
@ -3734,10 +3734,12 @@ settings_save (GtkFileChooserWidget *impl)
static void static void
switch_to_cwd (GtkFileChooserWidget *impl) switch_to_cwd (GtkFileChooserWidget *impl)
{ {
char *current_working_dir; char *current_working_dir = g_get_current_dir ();
GFile *cwd = g_file_new_for_path (current_working_dir);
current_working_dir = g_get_current_dir (); gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), cwd, NULL);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), current_working_dir);
g_object_unref (cwd);
g_free (current_working_dir); g_free (current_working_dir);
} }
@ -5599,7 +5601,7 @@ gtk_file_chooser_widget_select_file (GtkFileChooser *chooser,
parent_file = g_file_get_parent (file); parent_file = g_file_get_parent (file);
if (!parent_file) if (!parent_file)
return gtk_file_chooser_set_current_folder_file (chooser, file, error); return gtk_file_chooser_set_current_folder (chooser, file, error);
fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->browse_files_tree_view))); fsmodel = GTK_FILE_SYSTEM_MODEL (gtk_tree_view_get_model (GTK_TREE_VIEW (priv->browse_files_tree_view)));
@ -5639,7 +5641,7 @@ gtk_file_chooser_widget_select_file (GtkFileChooser *chooser,
{ {
gboolean result; gboolean result;
result = gtk_file_chooser_set_current_folder_file (chooser, parent_file, error); result = gtk_file_chooser_set_current_folder (chooser, parent_file, error);
g_object_unref (parent_file); g_object_unref (parent_file);
return result; return result;
} }
@ -5948,7 +5950,7 @@ gtk_file_chooser_widget_get_files (GtkFileChooser *chooser)
{ {
GFile *current_folder; GFile *current_folder;
current_folder = gtk_file_chooser_get_current_folder_file (chooser); current_folder = gtk_file_chooser_get_current_folder (chooser);
if (current_folder) if (current_folder)
info.result = g_slist_prepend (info.result, current_folder); info.result = g_slist_prepend (info.result, current_folder);
@ -7801,6 +7803,7 @@ static void
desktop_folder_handler (GtkFileChooserWidget *impl) desktop_folder_handler (GtkFileChooserWidget *impl)
{ {
const char *name; const char *name;
GFile *file;
/* "To disable a directory, point it to the homedir." /* "To disable a directory, point it to the homedir."
* See http://freedesktop.org/wiki/Software/xdg-user-dirs * See http://freedesktop.org/wiki/Software/xdg-user-dirs
@ -7809,7 +7812,9 @@ desktop_folder_handler (GtkFileChooserWidget *impl)
if (!g_strcmp0 (name, g_get_home_dir ())) if (!g_strcmp0 (name, g_get_home_dir ()))
return; return;
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), name); file = g_file_new_for_path (name);
gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (impl), file, NULL);
g_object_unref (file);
} }
/* Handler for the "search-shortcut" keybinding signal */ /* Handler for the "search-shortcut" keybinding signal */

View File

@ -68,7 +68,7 @@ struct GtkPrinterOptionWidgetPrivate
GtkWidget *button; GtkWidget *button;
/* the last location for save to file, that the user selected */ /* the last location for save to file, that the user selected */
gchar *last_location; GFile *last_location;
}; };
enum { enum {
@ -485,42 +485,49 @@ dialog_response_callback (GtkDialog *dialog,
GtkPrinterOptionWidget *widget) GtkPrinterOptionWidget *widget)
{ {
GtkPrinterOptionWidgetPrivate *priv = widget->priv; GtkPrinterOptionWidgetPrivate *priv = widget->priv;
gchar *uri = NULL; GFile *new_location = NULL;
gchar *new_location = NULL; char *uri = NULL;
if (response_id == GTK_RESPONSE_ACCEPT) if (response_id == GTK_RESPONSE_ACCEPT)
{ {
gchar *filename; GFileInfo *info;
gchar *filename_utf8;
gchar *filename_short;
new_location = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog)); new_location = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
info = g_file_query_info (new_location,
"standard::display-name",
0,
NULL,
NULL);
if (info != NULL)
{
const char *filename_utf8 = g_file_info_get_display_name (info);
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); char *filename_short = trim_long_filename (filename_utf8);
filename_utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL); gtk_button_set_label (GTK_BUTTON (priv->button), filename_short);
filename_short = trim_long_filename (filename_utf8);
gtk_button_set_label (GTK_BUTTON (priv->button), filename_short); g_free (filename_short);
g_free (filename_short); g_object_unref (info);
g_free (filename_utf8); }
g_free (filename);
g_object_unref (new_location);
} }
gtk_widget_destroy (GTK_WIDGET (dialog)); gtk_widget_destroy (GTK_WIDGET (dialog));
if (new_location) if (new_location)
uri = new_location; uri = g_file_get_uri (new_location);
else else
uri = priv->last_location; uri = g_file_get_uri (priv->last_location);
if (uri) if (uri != NULL)
{ {
gtk_printer_option_set (priv->source, uri); gtk_printer_option_set (priv->source, uri);
emit_changed (widget); emit_changed (widget);
g_free (uri);
} }
g_free (new_location); g_object_unref (new_location);
g_free (priv->last_location); g_clear_object (&priv->last_location);
priv->last_location = NULL;
/* unblock the handler which was blocked in the filesave_choose_cb function */ /* unblock the handler which was blocked in the filesave_choose_cb function */
g_signal_handler_unblock (priv->source, priv->source_changed_handler); g_signal_handler_unblock (priv->source, priv->source_changed_handler);
@ -531,7 +538,6 @@ filesave_choose_cb (GtkWidget *button,
GtkPrinterOptionWidget *widget) GtkPrinterOptionWidget *widget)
{ {
GtkPrinterOptionWidgetPrivate *priv = widget->priv; GtkPrinterOptionWidgetPrivate *priv = widget->priv;
gchar *last_location = NULL;
GtkWidget *dialog; GtkWidget *dialog;
GtkWindow *toplevel; GtkWindow *toplevel;
@ -550,23 +556,21 @@ filesave_choose_cb (GtkWidget *button,
gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), FALSE); gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), FALSE);
/* select the current filename in the dialog */ /* select the current filename in the dialog */
if (priv->source != NULL) if (priv->source != NULL && priv->source->value != NULL)
{ {
priv->last_location = last_location = g_strdup (priv->source->value); priv->last_location = g_file_new_for_uri (priv->source->value);
if (last_location) if (priv->last_location)
{ {
GFile *file; char *basename;
gchar *basename; char *basename_utf8;
gchar *basename_utf8;
gtk_file_chooser_select_uri (GTK_FILE_CHOOSER (dialog), last_location); gtk_file_chooser_select_file (GTK_FILE_CHOOSER (dialog), priv->last_location, NULL);
file = g_file_new_for_uri (last_location);
basename = g_file_get_basename (file); basename = g_file_get_basename (priv->last_location);
basename_utf8 = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL); basename_utf8 = g_filename_to_utf8 (basename, -1, NULL, NULL, NULL);
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), basename_utf8); gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), basename_utf8);
g_free (basename_utf8); g_free (basename_utf8);
g_free (basename); g_free (basename);
g_object_unref (file);
} }
} }

View File

@ -185,14 +185,22 @@ get_current_text (GtkTextBuffer *buffer)
static void static void
save_to_file (GtkInspectorCssEditor *ce, save_to_file (GtkInspectorCssEditor *ce,
const gchar *filename) GFile *file)
{ {
gchar *text;
GError *error = NULL; GError *error = NULL;
char *text;
text = get_current_text (ce->priv->text); text = get_current_text (ce->priv->text);
if (!g_file_set_contents (filename, text, -1, &error)) g_file_replace_contents (file, text, -1,
NULL,
FALSE,
G_FILE_CREATE_NONE,
NULL,
NULL,
&error);
if (error != NULL)
{ {
GtkWidget *dialog; GtkWidget *dialog;
@ -220,11 +228,9 @@ save_response (GtkWidget *dialog,
if (response == GTK_RESPONSE_ACCEPT) if (response == GTK_RESPONSE_ACCEPT)
{ {
gchar *filename; GFile *file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
save_to_file (ce, file);
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); g_object_unref (file);
save_to_file (ce, filename);
g_free (filename);
} }
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);

View File

@ -3,7 +3,7 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
static GtkWidget *main_window; static GtkWidget *main_window;
static char *filename = NULL; static GFile *filename = NULL;
static GtkPageSetup *page_setup = NULL; static GtkPageSetup *page_setup = NULL;
static GtkPrintSettings *settings = NULL; static GtkPrintSettings *settings = NULL;
static gboolean file_changed = FALSE; static gboolean file_changed = FALSE;
@ -20,7 +20,7 @@ update_title (GtkWindow *window)
if (filename == NULL) if (filename == NULL)
basename = g_strdup ("Untitled"); basename = g_strdup ("Untitled");
else else
basename = g_path_get_basename (filename); basename = g_file_get_basename (filename);
title = g_strdup_printf ("Simple Editor with printing - %s", basename); title = g_strdup_printf ("Simple Editor with printing - %s", basename);
g_free (basename); g_free (basename);
@ -81,7 +81,8 @@ get_text (void)
} }
static void static void
set_text (const char *text, gsize len) set_text (const char *text,
gsize len)
{ {
gtk_text_buffer_set_text (buffer, text, len); gtk_text_buffer_set_text (buffer, text, len);
file_changed = FALSE; file_changed = FALSE;
@ -89,7 +90,7 @@ set_text (const char *text, gsize len)
} }
static void static void
load_file (const char *open_filename) load_file (GFile *open_filename)
{ {
GtkWidget *error_dialog; GtkWidget *error_dialog;
char *contents; char *contents;
@ -98,37 +99,45 @@ load_file (const char *open_filename)
error_dialog = NULL; error_dialog = NULL;
error = NULL; error = NULL;
if (g_file_get_contents (open_filename, &contents, &len, &error)) g_file_load_contents (open_filename, NULL, &contents, &len, NULL, &error);
if (error == NULL)
{ {
if (g_utf8_validate (contents, len, NULL)) if (g_utf8_validate (contents, len, NULL))
{ {
filename = g_strdup (open_filename); g_clear_object (&filename);
filename = g_object_ref (open_filename);
set_text (contents, len); set_text (contents, len);
g_free (contents); g_free (contents);
} }
else else
{ {
GFileInfo *info = g_file_query_info (open_filename, "standard::display-name", 0, NULL, &error);
const char *display_name = g_file_info_get_display_name (info);
error_dialog = gtk_message_dialog_new (GTK_WINDOW (main_window), error_dialog = gtk_message_dialog_new (GTK_WINDOW (main_window),
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR, GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE, GTK_BUTTONS_CLOSE,
"Error loading file %s:\n%s", "Error loading file %s:\n%s",
open_filename, display_name,
"Not valid utf8"); "Not valid utf8");
g_object_unref (info);
} }
} }
else else
{ {
GFileInfo *info = g_file_query_info (open_filename, "standard::display-name", 0, NULL, &error);
const char *display_name = g_file_info_get_display_name (info);
error_dialog = gtk_message_dialog_new (GTK_WINDOW (main_window), error_dialog = gtk_message_dialog_new (GTK_WINDOW (main_window),
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR, GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE, GTK_BUTTONS_CLOSE,
"Error loading file %s:\n%s", "Error loading file %s:\n%s",
open_filename, display_name,
error->message); error->message);
g_object_unref (info);
g_error_free (error); g_error_free (error);
} }
if (error_dialog) if (error_dialog)
{ {
g_signal_connect (error_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL); g_signal_connect (error_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
@ -138,38 +147,49 @@ load_file (const char *open_filename)
static void static void
save_file (const char *save_filename) save_file (GFile *save_filename)
{ {
char *text = get_text (); char *text = get_text ();
GtkWidget *error_dialog; GtkWidget *error_dialog;
GError *error; GError *error;
error = NULL; error = NULL;
if (g_file_set_contents (save_filename, g_file_replace_contents (save_filename,
text, -1, &error)) text, -1,
NULL, FALSE,
G_FILE_CREATE_NONE,
NULL,
NULL,
&error);
if (error != NULL)
{ {
if (save_filename != filename) if (save_filename != filename)
{ {
g_free (filename); g_clear_object (&filename);
filename = g_strdup (save_filename); filename = g_object_ref (save_filename);
} }
file_changed = FALSE; file_changed = FALSE;
update_ui (); update_ui ();
} }
else else
{ {
GFileInfo *info = g_file_query_info (save_filename, "standard::display-name", 0, NULL, NULL);
const char *display_name = g_file_info_get_display_name (info);
error_dialog = gtk_message_dialog_new (GTK_WINDOW (main_window), error_dialog = gtk_message_dialog_new (GTK_WINDOW (main_window),
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR, GTK_MESSAGE_ERROR,
GTK_BUTTONS_CLOSE, GTK_BUTTONS_CLOSE,
"Error saving to file %s:\n%s", "Error saving to file %s:\n%s",
filename, display_name,
error->message); error->message);
g_signal_connect (error_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL); g_signal_connect (error_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL);
gtk_widget_show (error_dialog); gtk_widget_show (error_dialog);
g_error_free (error); g_error_free (error);
g_object_unref (info);
} }
} }
@ -481,7 +501,6 @@ activate_save_as (GSimpleAction *action,
{ {
GtkWidget *dialog; GtkWidget *dialog;
gint response; gint response;
char *save_filename;
dialog = gtk_file_chooser_dialog_new ("Select file", dialog = gtk_file_chooser_dialog_new ("Select file",
GTK_WINDOW (main_window), GTK_WINDOW (main_window),
@ -494,9 +513,9 @@ activate_save_as (GSimpleAction *action,
if (response == GTK_RESPONSE_OK) if (response == GTK_RESPONSE_OK)
{ {
save_filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); GFile *save_filename = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
save_file (save_filename); save_file (save_filename);
g_free (save_filename); g_object_unref (save_filename);
} }
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
@ -520,7 +539,6 @@ activate_open (GSimpleAction *action,
{ {
GtkWidget *dialog; GtkWidget *dialog;
gint response; gint response;
char *open_filename;
dialog = gtk_file_chooser_dialog_new ("Select file", dialog = gtk_file_chooser_dialog_new ("Select file",
GTK_WINDOW (main_window), GTK_WINDOW (main_window),
@ -533,9 +551,9 @@ activate_open (GSimpleAction *action,
if (response == GTK_RESPONSE_OK) if (response == GTK_RESPONSE_OK)
{ {
open_filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); GFile *open_filename = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
load_file (open_filename); load_file (open_filename);
g_free (open_filename); g_object_unref (open_filename);
} }
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
@ -689,7 +707,11 @@ command_line (GApplication *application,
argv = g_application_command_line_get_arguments (command_line, &argc); argv = g_application_command_line_get_arguments (command_line, &argc);
if (argc == 2) if (argc == 2)
load_file (argv[1]); {
GFile *file = g_file_new_for_commandline_arg (argv[1]);
load_file (file);
g_object_unref (file);
}
return 0; return 0;
} }

View File

@ -44,28 +44,38 @@ static GtkFileChooserAction action;
static void static void
print_current_folder (GtkFileChooser *chooser) print_current_folder (GtkFileChooser *chooser)
{ {
gchar *uri; GFile *cwd;
uri = gtk_file_chooser_get_current_folder_uri (chooser); cwd = gtk_file_chooser_get_current_folder (chooser);
g_print ("Current folder changed :\n %s\n", uri ? uri : "(null)"); if (cwd != NULL)
g_free (uri); {
char *uri = g_file_get_uri (cwd);
g_print ("Current folder changed :\n %s\n", uri ? uri : "(null)");
g_free (uri);
g_object_unref (cwd);
}
else
{
g_print ("Current folder changed :\n none\n");
}
} }
static void static void
print_selected (GtkFileChooser *chooser) print_selected (GtkFileChooser *chooser)
{ {
GSList *uris = gtk_file_chooser_get_uris (chooser); GSList *uris = gtk_file_chooser_get_files (chooser);
GSList *tmp_list; GSList *tmp_list;
g_print ("Selection changed :\n"); g_print ("Selection changed :\n");
for (tmp_list = uris; tmp_list; tmp_list = tmp_list->next) for (tmp_list = uris; tmp_list; tmp_list = tmp_list->next)
{ {
gchar *uri = tmp_list->data; GFile *file = tmp_list->data;
g_print (" %s\n", uri); char *uri = g_file_get_uri (file);
g_print (" %s\n", uri ? uri : "(null)");
g_free (uri); g_free (uri);
} }
g_print ("\n"); g_print ("\n");
g_slist_free (uris); g_slist_free_full (uris, g_object_unref);
} }
static void static void
@ -79,7 +89,7 @@ response_cb (GtkDialog *dialog,
{ {
GSList *list; GSList *list;
list = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (dialog)); list = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (dialog));
if (list) if (list)
{ {
@ -89,11 +99,13 @@ response_cb (GtkDialog *dialog,
for (l = list; l; l = l->next) for (l = list; l; l = l->next)
{ {
g_print ("%s\n", (char *) l->data); GFile *file = l->data;
g_free (l->data); char *uri = g_file_get_uri (file);
g_print (" %s\n", uri ? uri : "(null)");
g_free (uri);
} }
g_slist_free (list); g_slist_free_full (list, g_object_unref);
} }
else else
g_print ("No selected files\n"); g_print ("No selected files\n");
@ -341,7 +353,8 @@ static void
set_current_folder (GtkFileChooser *chooser, set_current_folder (GtkFileChooser *chooser,
const char *name) const char *name)
{ {
if (!gtk_file_chooser_set_current_folder (chooser, name)) GFile *file = g_file_new_for_path (name);
if (!gtk_file_chooser_set_current_folder (chooser, file, NULL))
{ {
GtkWidget *dialog; GtkWidget *dialog;
@ -354,6 +367,7 @@ set_current_folder (GtkFileChooser *chooser,
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
} }
g_object_unref (file);
} }
static void static void
@ -374,7 +388,8 @@ static void
set_filename (GtkFileChooser *chooser, set_filename (GtkFileChooser *chooser,
const char *name) const char *name)
{ {
if (!gtk_file_chooser_set_filename (chooser, name)) GFile *file = g_file_new_for_path (name);
if (!gtk_file_chooser_set_file (chooser, file, NULL))
{ {
GtkWidget *dialog; GtkWidget *dialog;
@ -387,6 +402,7 @@ set_filename (GtkFileChooser *chooser,
gtk_dialog_run (GTK_DIALOG (dialog)); gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog); gtk_widget_destroy (dialog);
} }
g_object_unref (file);
} }
static void static void
@ -409,7 +425,7 @@ get_selection_cb (GtkButton *button,
{ {
GSList *selection; GSList *selection;
selection = gtk_file_chooser_get_uris (chooser); selection = gtk_file_chooser_get_files (chooser);
g_print ("Selection: "); g_print ("Selection: ");
@ -421,16 +437,19 @@ get_selection_cb (GtkButton *button,
for (l = selection; l; l = l->next) for (l = selection; l; l = l->next)
{ {
char *uri = l->data; GFile *file = l->data;
char *uri = g_file_get_uri (file);
g_print ("%s\n", uri); g_print ("%s\n", uri);
g_free (uri);
if (l->next) if (l->next)
g_print (" "); g_print (" ");
} }
} }
g_slist_free_full (selection, g_free); g_slist_free_full (selection, g_object_unref);
} }
static void static void
@ -534,6 +553,7 @@ main (int argc, char **argv)
char *action_arg = NULL; char *action_arg = NULL;
char *initial_filename = NULL; char *initial_filename = NULL;
char *initial_folder = NULL; char *initial_folder = NULL;
GFile *file;
GError *error = NULL; GError *error = NULL;
GOptionEntry options[] = { GOptionEntry options[] = {
{ "action", 'a', 0, G_OPTION_ARG_STRING, &action_arg, "Filechooser action", "ACTION" }, { "action", 'a', 0, G_OPTION_ARG_STRING, &action_arg, "Filechooser action", "ACTION" },
@ -680,7 +700,7 @@ main (int argc, char **argv)
G_CALLBACK (update_preview_cb), NULL); G_CALLBACK (update_preview_cb), NULL);
#endif #endif
/* Extra widget */ /* Choices */
gtk_file_chooser_add_choice (GTK_FILE_CHOOSER (dialog), "choice1", gtk_file_chooser_add_choice (GTK_FILE_CHOOSER (dialog), "choice1",
"Choose one:", "Choose one:",
@ -690,12 +710,13 @@ main (int argc, char **argv)
/* Shortcuts */ /* Shortcuts */
gtk_file_chooser_add_shortcut_folder_uri (GTK_FILE_CHOOSER (dialog), file = g_file_new_for_uri ("file:///usr/share/pixmaps");
"file:///usr/share/pixmaps", gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), file, NULL);
NULL); g_object_unref (file);
gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog),
g_get_user_special_dir (G_USER_DIRECTORY_MUSIC), file = g_file_new_for_path (g_get_user_special_dir (G_USER_DIRECTORY_MUSIC));
NULL); gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (dialog), file, NULL);
g_object_unref (file);
/* Initial filename or folder */ /* Initial filename or folder */

View File

@ -45,23 +45,31 @@ static void
print_selected_path_clicked_cb (GtkWidget *button, print_selected_path_clicked_cb (GtkWidget *button,
gpointer user_data) gpointer user_data)
{ {
gchar *folder, *filename; GFile *folder, *filename;
char *folder_uri, *filename_uri;
folder = gtk_file_chooser_get_current_folder (user_data); folder = gtk_file_chooser_get_current_folder (user_data);
filename = gtk_file_chooser_get_filename (user_data); filename = gtk_file_chooser_get_file (user_data);
folder_uri = g_file_get_uri (folder);
filename_uri = g_file_get_uri (filename);
g_message ("Currently Selected:\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n", g_message ("Currently Selected:\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
folder, filename); folder_uri, filename_uri);
g_free (folder); g_free (folder_uri);
g_free (filename); g_free (filename_uri);
g_object_unref (folder);
g_object_unref (filename);
} }
static void static void
add_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button, add_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
gpointer user_data) gpointer user_data)
{ {
GFile *path = g_file_new_for_path (gtk_src_dir);
GError *err = NULL; GError *err = NULL;
if (!gtk_file_chooser_add_shortcut_folder (user_data, gtk_src_dir, &err)) if (!gtk_file_chooser_add_shortcut_folder (user_data, path, &err))
{ {
g_message ("Couldn't add `%s' as shortcut folder: %s", gtk_src_dir, g_message ("Couldn't add `%s' as shortcut folder: %s", gtk_src_dir,
err->message); err->message);
@ -71,15 +79,18 @@ add_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
{ {
g_message ("Added `%s' as shortcut folder.", gtk_src_dir); g_message ("Added `%s' as shortcut folder.", gtk_src_dir);
} }
g_object_unref (path);
} }
static void static void
del_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button, del_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
gpointer user_data) gpointer user_data)
{ {
GFile *path = g_file_new_for_path (gtk_src_dir);
GError *err = NULL; GError *err = NULL;
if (!gtk_file_chooser_remove_shortcut_folder (user_data, gtk_src_dir, &err)) if (!gtk_file_chooser_remove_shortcut_folder (user_data, path, &err))
{ {
g_message ("Couldn't remove `%s' as shortcut folder: %s", gtk_src_dir, g_message ("Couldn't remove `%s' as shortcut folder: %s", gtk_src_dir,
err->message); err->message);
@ -89,6 +100,8 @@ del_pwds_parent_as_shortcut_clicked_cb (GtkWidget *button,
{ {
g_message ("Removed `%s' as shortcut folder.", gtk_src_dir); g_message ("Removed `%s' as shortcut folder.", gtk_src_dir);
} }
g_object_unref (path);
} }
static void static void
@ -155,54 +168,76 @@ static void
chooser_current_folder_changed_cb (GtkFileChooser *chooser, chooser_current_folder_changed_cb (GtkFileChooser *chooser,
gpointer user_data) gpointer user_data)
{ {
gchar *folder, *filename; GFile *folder, *filename;
char *folder_uri, *filename_uri;
folder = gtk_file_chooser_get_current_folder_uri (chooser); folder = gtk_file_chooser_get_current_folder (chooser);
filename = gtk_file_chooser_get_uri (chooser); filename = gtk_file_chooser_get_file (chooser);
folder_uri = g_file_get_uri (folder);
filename_uri = g_file_get_uri (filename);
g_message ("%s::current-folder-changed\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n", g_message ("%s::current-folder-changed\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
G_OBJECT_TYPE_NAME (chooser), folder, filename); G_OBJECT_TYPE_NAME (chooser), folder_uri, filename_uri);
g_free (folder); g_free (folder_uri);
g_free (filename); g_free (filename_uri);
g_object_unref (folder);
g_object_unref (filename);
} }
static void static void
chooser_selection_changed_cb (GtkFileChooser *chooser, chooser_selection_changed_cb (GtkFileChooser *chooser,
gpointer user_data) gpointer user_data)
{ {
gchar *filename; GFile *filename;
char *uri;
filename = gtk_file_chooser_get_uri (chooser); filename = gtk_file_chooser_get_file (chooser);
uri = g_file_get_uri (filename);
g_message ("%s::selection-changed\n\tSelection:`%s'\nDone.\n", g_message ("%s::selection-changed\n\tSelection:`%s'\nDone.\n",
G_OBJECT_TYPE_NAME (chooser), filename); G_OBJECT_TYPE_NAME (chooser), uri);
g_free (filename); g_free (uri);
g_object_unref (filename);
} }
static void static void
chooser_file_activated_cb (GtkFileChooser *chooser, chooser_file_activated_cb (GtkFileChooser *chooser,
gpointer user_data) gpointer user_data)
{ {
gchar *folder, *filename; GFile *folder, *filename;
char *folder_uri, *filename_uri;
folder = gtk_file_chooser_get_current_folder_uri (chooser); folder = gtk_file_chooser_get_current_folder (chooser);
filename = gtk_file_chooser_get_uri (chooser); filename = gtk_file_chooser_get_file (chooser);
folder_uri = g_file_get_uri (folder);
filename_uri = g_file_get_uri (filename);
g_message ("%s::file-activated\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n", g_message ("%s::file-activated\n\tFolder: `%s'\n\tFilename: `%s'\nDone.\n",
G_OBJECT_TYPE_NAME (chooser), folder, filename); G_OBJECT_TYPE_NAME (chooser), folder_uri, filename_uri);
g_free (folder); g_free (folder_uri);
g_free (filename); g_free (filename_uri);
g_object_unref (folder);
g_object_unref (filename);
} }
static void static void
chooser_update_preview_cb (GtkFileChooser *chooser, chooser_update_preview_cb (GtkFileChooser *chooser,
gpointer user_data) gpointer user_data)
{ {
gchar *filename; GFile *filename;
filename = gtk_file_chooser_get_preview_uri (chooser); filename = gtk_file_chooser_get_preview_file (chooser);
if (filename != NULL) if (filename != NULL)
{ {
char *uri = g_file_get_uri (filename);
g_message ("%s::update-preview\n\tPreview Filename: `%s'\nDone.\n", g_message ("%s::update-preview\n\tPreview Filename: `%s'\nDone.\n",
G_OBJECT_TYPE_NAME (chooser), filename); G_OBJECT_TYPE_NAME (chooser), uri);
g_free (filename); g_free (uri);
g_object_unref (filename);
} }
} }
@ -214,6 +249,7 @@ add_new_filechooser_button (const gchar *mnemonic,
GtkSizeGroup *label_group) GtkSizeGroup *label_group)
{ {
GtkWidget *hbox, *label, *chooser, *button; GtkWidget *hbox, *label, *chooser, *button;
GFile *path;
hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12); hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
gtk_container_add (GTK_CONTAINER (group_box), hbox); gtk_container_add (GTK_CONTAINER (group_box), hbox);
@ -227,8 +263,12 @@ add_new_filechooser_button (const gchar *mnemonic,
" - testfilechooserbutton", NULL), " - testfilechooserbutton", NULL),
action); action);
gtk_widget_set_hexpand (chooser, TRUE); gtk_widget_set_hexpand (chooser, TRUE);
gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL);
gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), gtk_src_dir, NULL); path = g_file_new_for_path (gtk_src_dir);
gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (chooser), path, NULL);
gtk_file_chooser_remove_shortcut_folder (GTK_FILE_CHOOSER (chooser), path, NULL);
g_object_unref (path);
gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser); gtk_label_set_mnemonic_widget (GTK_LABEL (label), chooser);
g_signal_connect (chooser, "current-folder-changed", g_signal_connect (chooser, "current-folder-changed",
G_CALLBACK (chooser_current_folder_changed_cb), NULL); G_CALLBACK (chooser_current_folder_changed_cb), NULL);

View File

@ -5709,14 +5709,17 @@ native_response (GtkNativeDialog *self,
char *response; char *response;
GtkFileFilter *filter; GtkFileFilter *filter;
uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (self)); uris = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (self));
filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (self)); filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (self));
s = g_string_new (""); s = g_string_new ("");
for (l = uris; l != NULL; l = l->next) for (l = uris; l != NULL; l = l->next)
{ {
g_string_prepend (s, l->data); char *uri = g_file_get_uri (l->data);
g_string_prepend (s, uri);
g_string_prepend (s, "\n"); g_string_prepend (s, "\n");
g_free (uri);
} }
g_slist_free_full (uris, g_object_unref);
switch (response_id) switch (response_id)
{ {
@ -5826,12 +5829,12 @@ native_visible_notify_hide (GObject *object,
gtk_widget_set_sensitive (hide_button, visible); gtk_widget_set_sensitive (hide_button, visible);
} }
static char * static GFile *
get_some_file (void) get_some_file (void)
{ {
GFile *dir = g_file_new_for_path (g_get_current_dir ()); GFile *dir = g_file_new_for_path (g_get_current_dir ());
GFileEnumerator *e; GFileEnumerator *e;
char *res = NULL; GFile *res = NULL;
e = g_file_enumerate_children (dir, "*", 0, NULL, NULL); e = g_file_enumerate_children (dir, "*", 0, NULL, NULL);
if (e) if (e)
@ -5846,8 +5849,7 @@ get_some_file (void)
if (g_file_info_get_file_type (info) == G_FILE_TYPE_REGULAR) if (g_file_info_get_file_type (info) == G_FILE_TYPE_REGULAR)
{ {
GFile *child = g_file_enumerator_get_child (e, info); GFile *child = g_file_enumerator_get_child (e, info);
res = g_file_get_path (child); res = g_steal_pointer (&child);
g_object_unref (child);
} }
g_object_unref (info); g_object_unref (info);
} }
@ -5882,9 +5884,8 @@ native_action_changed (GtkWidget *combo,
{ {
if (save_as) if (save_as)
{ {
char *file = get_some_file (); GFile *file = get_some_file ();
gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (native), file); gtk_file_chooser_set_file (GTK_FILE_CHOOSER (native), file, NULL);
g_free (file);
} }
else else
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (native), "newname.txt"); gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (native), "newname.txt");
@ -5962,6 +5963,8 @@ create_native_dialogs (GtkWidget *widget)
if (!window) if (!window)
{ {
GFile *path;
window = gtk_window_new (); window = gtk_window_new ();
gtk_window_set_display (GTK_WINDOW (window), gtk_window_set_display (GTK_WINDOW (window),
gtk_widget_get_display (widget)); gtk_widget_get_display (widget));
@ -5974,9 +5977,9 @@ create_native_dialogs (GtkWidget *widget)
g_signal_connect_swapped (G_OBJECT (window), "destroy", G_CALLBACK (destroy_native), native); g_signal_connect_swapped (G_OBJECT (window), "destroy", G_CALLBACK (destroy_native), native);
gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (native), path = g_file_new_for_path (g_get_current_dir ());
g_get_current_dir (), gtk_file_chooser_add_shortcut_folder (GTK_FILE_CHOOSER (native), path, NULL);
NULL); g_object_unref (path);
gtk_window_set_title (GTK_WINDOW(window), "Native dialog parent"); gtk_window_set_title (GTK_WINDOW(window), "Native dialog parent");