Commit 0f8d84d5 authored by Michael Natterer's avatar Michael Natterer 😴

app: port all file_open() and file_save() functions to GFile

parent 3f528288
......@@ -20,6 +20,7 @@
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "actions-types.h"
......@@ -71,35 +72,25 @@ data_open_as_image_cmd_callback (GtkAction *action,
if (data && gimp_data_get_file (data))
{
gchar *uri = g_file_get_uri (gimp_data_get_file (data));
if (uri)
GFile *file = gimp_data_get_file (data);
GtkWidget *widget = GTK_WIDGET (view);
GimpImage *image;
GimpPDBStatusType status;
GError *error = NULL;
image = file_open_with_display (context->gimp, context, NULL,
file, FALSE,
G_OBJECT (gtk_widget_get_screen (widget)),
gimp_widget_get_monitor (widget),
&status, &error);
if (! image && status != GIMP_PDB_CANCEL)
{
GtkWidget *widget = GTK_WIDGET (view);
GimpImage *image;
GimpPDBStatusType status;
GError *error = NULL;
image = file_open_with_display (context->gimp, context, NULL,
uri, FALSE,
G_OBJECT (gtk_widget_get_screen (widget)),
gimp_widget_get_monitor (widget),
&status, &error);
if (! image && status != GIMP_PDB_CANCEL)
{
gchar *filename = file_utils_uri_display_name (uri);
gimp_message (context->gimp, G_OBJECT (view),
GIMP_MESSAGE_ERROR,
_("Opening '%s' failed:\n\n%s"),
filename, error->message);
g_clear_error (&error);
g_free (filename);
}
g_free (uri);
gimp_message (context->gimp, G_OBJECT (view),
GIMP_MESSAGE_ERROR,
_("Opening '%s' failed:\n\n%s"),
gimp_file_get_utf8_name (file), error->message);
g_clear_error (&error);
}
}
}
......
......@@ -22,6 +22,7 @@
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpthumb/gimpthumb.h"
#include "libgimpwidgets/gimpwidgets.h"
......@@ -145,7 +146,7 @@ documents_file_open_dialog_cmd_callback (GtkAction *action,
if (imagefile && gimp_container_have (container, GIMP_OBJECT (imagefile)))
{
file_file_open_dialog (context->gimp,
gimp_object_get_name (imagefile),
gimp_imagefile_get_file (imagefile),
GTK_WIDGET (editor));
}
}
......@@ -336,28 +337,24 @@ documents_open_image (GtkWidget *editor,
GimpContext *context,
GimpImagefile *imagefile)
{
const gchar *uri;
GFile *file;
GimpImage *image;
GimpPDBStatusType status;
GError *error = NULL;
uri = gimp_object_get_name (imagefile);
file = gimp_imagefile_get_file (imagefile);
image = file_open_with_display (context->gimp, context, NULL, uri, FALSE,
image = file_open_with_display (context->gimp, context, NULL, file, FALSE,
G_OBJECT (gtk_widget_get_screen (editor)),
gimp_widget_get_monitor (editor),
&status, &error);
if (! image && status != GIMP_PDB_CANCEL)
{
gchar *filename = file_utils_uri_display_name (uri);
gimp_message (context->gimp, G_OBJECT (editor), GIMP_MESSAGE_ERROR,
_("Opening '%s' failed:\n\n%s"),
filename, error->message);
gimp_file_get_utf8_name (file), error->message);
g_clear_error (&error);
g_free (filename);
}
}
......
......@@ -63,7 +63,7 @@ static void file_actions_close_all_update (GimpContainer *images,
GimpObject *unused,
GimpActionGroup *group);
static gchar * file_actions_create_label (const gchar *format,
const gchar *uri);
GFile *file);
static const GimpActionEntry file_actions[] =
......@@ -256,21 +256,29 @@ file_actions_update (GimpActionGroup *group,
Gimp *gimp = action_data_get_gimp (data);
GimpImage *image = action_data_get_image (data);
GimpDrawable *drawable = NULL;
const gchar *source = NULL;
const gchar *export = NULL;
GFile *source = NULL;
GFile *export = NULL;
gboolean show_overwrite = FALSE;
if (image)
{
const gchar *uri;
drawable = gimp_image_get_active_drawable (image);
source = gimp_image_get_imported_uri (image);
export = gimp_image_get_exported_uri (image);
uri = gimp_image_get_imported_uri (image);
if (uri)
source = g_file_new_for_uri (uri);
uri = gimp_image_get_exported_uri (image);
if (uri)
export = g_file_new_for_uri (uri);
}
show_overwrite =
(source &&
gimp_plug_in_manager_uri_has_exporter (gimp->plug_in_manager,
source));
gimp_plug_in_manager_file_has_exporter (gimp->plug_in_manager,
source));
#define SET_VISIBLE(action,condition) \
gimp_action_group_set_action_visible (group, action, (condition) != 0)
......@@ -413,9 +421,9 @@ file_actions_close_all_update (GimpContainer *images,
static gchar *
file_actions_create_label (const gchar *format,
const gchar *uri)
GFile *file)
{
gchar *basename = file_utils_uri_display_basename (uri);
gchar *basename = g_path_get_basename (gimp_file_get_utf8_name (file));
gchar *escaped_basename = gimp_escape_uline (basename);
gchar *label = g_strdup_printf (format, escaped_basename);
......
......@@ -22,6 +22,7 @@
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "actions-types.h"
......@@ -31,6 +32,7 @@
#include "core/gimp.h"
#include "core/gimpcontainer.h"
#include "core/gimpimage.h"
#include "core/gimpimagefile.h"
#include "core/gimpprogress.h"
#include "core/gimptemplate.h"
......@@ -70,7 +72,7 @@ static void file_open_dialog_show (Gimp *gimp,
GtkWidget *parent,
const gchar *title,
GimpImage *image,
const gchar *uri,
GFile *file,
gboolean open_as_layers);
static GtkWidget * file_save_dialog_show (Gimp *gimp,
GimpImage *image,
......@@ -176,6 +178,7 @@ file_open_recent_cmd_callback (GtkAction *action,
if (imagefile)
{
GFile *file;
GimpDisplay *display;
GtkWidget *widget;
GimpProgress *progress;
......@@ -188,27 +191,24 @@ file_open_recent_cmd_callback (GtkAction *action,
g_object_ref (display);
g_object_ref (imagefile);
file = gimp_imagefile_get_file (imagefile);
progress = gimp_display_get_image (display) ?
NULL : GIMP_PROGRESS (display);
image = file_open_with_display (gimp, action_data_get_context (data),
progress,
gimp_object_get_name (imagefile), FALSE,
file, FALSE,
G_OBJECT (gtk_widget_get_screen (widget)),
gimp_widget_get_monitor (widget),
&status, &error);
if (! image && status != GIMP_PDB_CANCEL)
{
gchar *filename =
file_utils_uri_display_name (gimp_object_get_name (imagefile));
gimp_message (gimp, G_OBJECT (display), GIMP_MESSAGE_ERROR,
_("Opening '%s' failed:\n\n%s"),
filename, error->message);
gimp_file_get_utf8_name (file), error->message);
g_clear_error (&error);
g_free (filename);
}
g_object_unref (imagefile);
......@@ -227,6 +227,7 @@ file_save_cmd_callback (GtkAction *action,
GtkWidget *widget;
GimpSaveMode save_mode;
const gchar *uri;
GFile *file = NULL;
gboolean saved = FALSE;
return_if_no_gimp (gimp, data);
return_if_no_display (display, data);
......@@ -241,6 +242,9 @@ file_save_cmd_callback (GtkAction *action,
uri = gimp_image_get_uri (image);
if (uri)
file = g_file_new_for_uri (uri);
switch (save_mode)
{
case GIMP_SAVE_MODE_SAVE:
......@@ -248,21 +252,21 @@ file_save_cmd_callback (GtkAction *action,
/* Only save if the image has been modified, or if it is new. */
if ((gimp_image_is_dirty (image) ||
! GIMP_GUI_CONFIG (image->gimp->config)->trust_dirty_flag) ||
uri == NULL)
file == NULL)
{
GimpPlugInProcedure *save_proc = gimp_image_get_save_proc (image);
if (uri && ! save_proc)
if (file && ! save_proc)
{
save_proc =
file_procedure_find (image->gimp->plug_in_manager->save_procs,
uri, NULL);
file, NULL);
}
if (uri && save_proc)
if (file && save_proc)
{
saved = file_save_dialog_save_image (GIMP_PROGRESS (display),
gimp, image, uri,
gimp, image, file,
save_proc,
GIMP_RUN_WITH_LAST_VALS,
TRUE, FALSE, FALSE, TRUE);
......@@ -300,6 +304,7 @@ file_save_cmd_callback (GtkAction *action,
case GIMP_SAVE_MODE_OVERWRITE:
{
const gchar *uri = NULL;
GFile *file = NULL;
GimpPlugInProcedure *export_proc = NULL;
gboolean overwrite = FALSE;
......@@ -324,33 +329,29 @@ file_save_cmd_callback (GtkAction *action,
overwrite = TRUE;
}
if (uri && ! export_proc)
if (uri)
file = g_file_new_for_uri (uri);
if (file && ! export_proc)
{
export_proc =
file_procedure_find (image->gimp->plug_in_manager->export_procs,
uri, NULL);
file, NULL);
}
if (uri && export_proc)
if (file && export_proc)
{
char *uri_copy;
/* The memory that 'uri' points to can be freed by
file_save_dialog_save_image(), when it eventually calls
gimp_image_set_imported_uri() to reset the imported uri,
resulting in garbage. So make a duplicate of it here. */
uri_copy = g_strdup (uri);
saved = file_save_dialog_save_image (GIMP_PROGRESS (display),
gimp, image, uri_copy,
gimp, image, file,
export_proc,
GIMP_RUN_WITH_LAST_VALS,
FALSE,
overwrite, ! overwrite,
TRUE);
g_free (uri_copy);
}
if (file)
g_object_unref (file);
}
break;
}
......@@ -496,13 +497,13 @@ file_quit_cmd_callback (GtkAction *action,
}
void
file_file_open_dialog (Gimp *gimp,
const gchar *uri,
GtkWidget *parent)
file_file_open_dialog (Gimp *gimp,
GFile *file,
GtkWidget *parent)
{
file_open_dialog_show (gimp, parent,
_("Open Image"),
NULL, uri, FALSE);
NULL, file, FALSE);
}
......@@ -513,7 +514,7 @@ file_open_dialog_show (Gimp *gimp,
GtkWidget *parent,
const gchar *title,
GimpImage *image,
const gchar *uri,
GFile *file,
gboolean open_as_layers)
{
GtkWidget *dialog;
......@@ -524,16 +525,28 @@ file_open_dialog_show (Gimp *gimp,
NULL /*ui_manager*/,
"gimp-file-open-dialog", -1, FALSE);
if (file)
g_object_ref (file);
if (dialog)
{
if (! uri && image)
uri = gimp_image_get_uri (image);
if (! file && image)
{
const gchar *uri = gimp_image_get_uri (image);
if (uri)
file = g_file_new_for_uri (uri);
}
if (! uri)
uri = g_object_get_data (G_OBJECT (gimp), GIMP_FILE_OPEN_LAST_URI_KEY);
if (! file)
{
file = g_object_get_data (G_OBJECT (gimp),
GIMP_FILE_OPEN_LAST_FILE_KEY);
if (file)
g_object_ref (file);
}
if (uri)
gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (dialog), uri);
if (file)
gtk_file_chooser_set_file (GTK_FILE_CHOOSER (dialog), file, NULL);
else if (gimp->default_folder)
gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER (dialog),
gimp->default_folder);
......@@ -546,6 +559,9 @@ file_open_dialog_show (Gimp *gimp,
gtk_window_present (GTK_WINDOW (dialog));
}
if (file)
g_object_unref (file);
}
static GtkWidget *
......@@ -765,6 +781,7 @@ file_revert_confirm_response (GtkWidget *dialog,
Gimp *gimp = old_image->gimp;
GimpImage *new_image;
const gchar *uri;
GFile *file;
GimpPDBStatusType status;
GError *error = NULL;
......@@ -773,9 +790,11 @@ file_revert_confirm_response (GtkWidget *dialog,
if (! uri)
uri = gimp_image_get_imported_uri (old_image);
file = g_file_new_for_uri (uri);
new_image = file_open_image (gimp, gimp_get_user_context (gimp),
GIMP_PROGRESS (display),
uri, uri, FALSE, NULL,
file, file, FALSE, NULL,
GIMP_RUN_INTERACTIVE,
&status, NULL, &error);
......@@ -789,14 +808,12 @@ file_revert_confirm_response (GtkWidget *dialog,
}
else if (status != GIMP_PDB_CANCEL)
{
gchar *filename = file_utils_uri_display_name (uri);
gimp_message (gimp, G_OBJECT (display), GIMP_MESSAGE_ERROR,
_("Reverting to '%s' failed:\n\n%s"),
filename, error->message);
gimp_file_get_utf8_name (file), error->message);
g_clear_error (&error);
g_free (filename);
}
g_object_unref (file);
}
}
......@@ -43,7 +43,7 @@ void file_quit_cmd_callback (GtkAction *action,
gpointer data);
void file_file_open_dialog (Gimp *gimp,
const gchar *uri,
GFile *file,
GtkWidget *parent);
......
......@@ -173,8 +173,8 @@ gimp_class_init (GimpClass *klass)
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GimpClass, image_opened),
NULL, NULL,
gimp_marshal_VOID__STRING,
G_TYPE_NONE, 1, G_TYPE_STRING);
gimp_marshal_VOID__OBJECT,
G_TYPE_NONE, 1, G_TYPE_FILE);
object_class->dispose = gimp_dispose;
object_class->finalize = gimp_finalize;
......@@ -1298,13 +1298,13 @@ gimp_message_literal (Gimp *gimp,
}
void
gimp_image_opened (Gimp *gimp,
const gchar *uri)
gimp_image_opened (Gimp *gimp,
GFile *file)
{
g_return_if_fail (GIMP_IS_GIMP (gimp));
g_return_if_fail (uri != NULL);
g_return_if_fail (G_IS_FILE (file));
g_signal_emit (gimp, gimp_signals[IMAGE_OPENED], 0, uri);
g_signal_emit (gimp, gimp_signals[IMAGE_OPENED], 0, file);
}
gchar *
......
......@@ -138,7 +138,7 @@ struct _GimpClass
/* emitted if an image is loaded and opened with a display */
void (* image_opened) (Gimp *gimp,
const gchar *uri);
GFile *file);
};
......@@ -215,7 +215,7 @@ void gimp_message_literal (Gimp *gimp,
const gchar *message);
void gimp_image_opened (Gimp *gimp,
const gchar *uri);
GFile *file);
gchar * gimp_get_temp_filename (Gimp *gimp,
const gchar *extension);
......
......@@ -58,6 +58,11 @@ struct _GimpImagePrivate
GimpMetadata *metadata; /* image's metadata */
GFile *file;
GFile *imported_file;
GFile *exported_file;
GFile *save_a_copy_file;
gint dirty; /* dirty flag -- # of ops */
guint dirty_time; /* time when image became dirty */
gint export_dirty; /* 'dirty' but for export */
......
......@@ -248,6 +248,14 @@ gimp_imagefile_new (Gimp *gimp,
return imagefile;
}
GFile *
gimp_imagefile_get_file (GimpImagefile *imagefile)
{
g_return_val_if_fail (GIMP_IS_IMAGEFILE (imagefile), NULL);
return GET_PRIVATE (imagefile)->file;
}
GimpThumbnail *
gimp_imagefile_get_thumbnail (GimpImagefile *imagefile)
{
......@@ -356,7 +364,6 @@ gimp_imagefile_create_thumbnail (GimpImagefile *imagefile,
if (image_state == GIMP_THUMB_STATE_REMOTE ||
image_state >= GIMP_THUMB_STATE_EXISTS)
{
GFile *file;
GimpImage *image;
gboolean success;
gint width = 0;
......@@ -365,16 +372,14 @@ gimp_imagefile_create_thumbnail (GimpImagefile *imagefile,
const Babl *format = NULL;
gint num_layers = -1;
file = g_file_new_for_uri (thumbnail->image_uri);
/* we only want to attempt thumbnailing on readable, regular files */
if (g_file_is_native (file))
if (g_file_is_native (private->file))
{
GFileInfo *file_info;
gboolean regular;
gboolean readable;
file_info = g_file_query_info (file,
file_info = g_file_query_info (private->file,
G_FILE_ATTRIBUTE_STANDARD_TYPE ","
G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
G_FILE_QUERY_INFO_NONE,
......@@ -387,21 +392,16 @@ gimp_imagefile_create_thumbnail (GimpImagefile *imagefile,
g_object_unref (file_info);
if (! (regular && readable))
{
g_object_unref (file);
return TRUE;
}
return TRUE;
}
g_object_unref (file);
g_object_ref (imagefile);
/* don't pass the error, we're only interested in errors from
* actual thumbnail saving
*/
image = file_open_thumbnail (private->gimp, context, progress,
thumbnail->image_uri, size,
private->file, size,
&mime_type, &width, &height,
&format, &num_layers, NULL);
......@@ -419,8 +419,8 @@ gimp_imagefile_create_thumbnail (GimpImagefile *imagefile,
* from actual thumbnail saving
*/
image = file_open_image (private->gimp, context, progress,
thumbnail->image_uri,
thumbnail->image_uri,
private->file,
private->file,
FALSE, NULL, GIMP_RUN_NONINTERACTIVE,
&status, &mime_type, NULL);
......
......@@ -58,6 +58,7 @@ GType gimp_imagefile_get_type (void) G_GNUC_CONST;
GimpImagefile * gimp_imagefile_new (Gimp *gimp,
GFile *file);
GFile * gimp_imagefile_get_file (GimpImagefile *imagefile);
GimpThumbnail * gimp_imagefile_get_thumbnail (GimpImagefile *imagefile);
GIcon * gimp_imagefile_get_gicon (GimpImagefile *imagefile);
......
......@@ -23,6 +23,7 @@
#include <gegl.h>
#include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "dialogs-types.h"
......@@ -53,11 +54,11 @@ static void file_open_dialog_response (GtkWidget *open_dialog
Gimp *gimp);
static GimpImage *file_open_dialog_open_image (GtkWidget *open_dialog,
Gimp *gimp,
const gchar *uri,
GFile *file,
GimpPlugInProcedure *load_proc);
static gboolean file_open_dialog_open_layers (GtkWidget *open_dialog,
GimpImage *image,
const gchar *uri,
GFile *file,
GimpPlugInProcedure *load_proc);
......@@ -100,7 +101,7 @@ file_open_dialog_response (GtkWidget *open_dialog,
Gimp *gimp)
{
GimpFileDialog *dialog = GIMP_FILE_DIALOG (open_dialog);
GSList *uris;
GSList *files;
GSList *list;
gboolean success = FALSE;
......@@ -116,11 +117,12 @@ file_open_dialog_response (GtkWidget *open_dialog,
return;
}
uris = gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (open_dialog));
files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (open_dialog));
if (uris)
g_object_set_data_full (G_OBJECT (gimp), GIMP_FILE_OPEN_LAST_URI_KEY,
g_strdup (uris->data), (GDestroyNotify) g_free);
if (files)
g_object_set_data_full (G_OBJECT (gimp), GIMP_FILE_OPEN_LAST_FILE_KEY,
g_object_ref (files->data),
(GDestroyNotify) g_object_unref);
gimp_file_dialog_set_sensitive (dialog, FALSE);
......@@ -133,15 +135,16 @@ file_open_dialog_response (GtkWidget *open_dialog,
if (! dialog->open_as_layers)
gtk_window_set_transient_for (GTK_WINDOW (open_dialog), NULL);
for (list = uris; list; list = g_slist_next (list))
for (list = files; list; list = g_slist_next (files))
{