/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995, 1996, 1997 Spencer Kimball and Peter Mattis
 * Copyright (C) 1997 Josh MacDonald
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <string.h>

#include <gegl.h>
#include <gtk/gtk.h>

#include "libpikabase/pikabase.h"
#include "libpikawidgets/pikawidgets.h"

#include "dialogs-types.h"

#include "core/pika.h"
#include "core/pikaimage.h"
#include "core/pikaprogress.h"

#include "plug-in/pikapluginmanager-file.h"
#include "plug-in/pikapluginprocedure.h"

#include "file/file-save.h"
#include "file/pika-file.h"

#include "widgets/pikaactiongroup.h"
#include "widgets/pikaexportdialog.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikamessagebox.h"
#include "widgets/pikamessagedialog.h"
#include "widgets/pikasavedialog.h"

#include "display/pikadisplay.h"
#include "display/pikadisplayshell.h"

#include "file-save-dialog.h"

#include "pika-log.h"
#include "pika-intl.h"


typedef enum
{
  CHECK_URI_FAIL,
  CHECK_URI_OK,
  CHECK_URI_SWITCH_DIALOGS
} CheckUriResult;


/*  local function prototypes  */

static GtkFileChooserConfirmation
                 file_save_dialog_confirm_overwrite         (GtkWidget            *dialog,
                                                             Pika                 *pika);
static void      file_save_dialog_response                  (GtkWidget            *dialog,
                                                             gint                  response_id,
                                                             Pika                 *pika);
static CheckUriResult file_save_dialog_check_file           (GtkWidget            *save_dialog,
                                                             Pika                 *pika,
                                                             GFile               **ret_file,
                                                             gchar               **ret_basename,
                                                             PikaPlugInProcedure **ret_save_proc);
static gboolean  file_save_dialog_no_overwrite_confirmation (PikaFileDialog       *dialog,
                                                             Pika                 *pika);
static PikaPlugInProcedure *
                 file_save_dialog_find_procedure            (PikaFileDialog       *dialog,
                                                             GFile                *file);
static gboolean  file_save_dialog_switch_dialogs            (PikaFileDialog       *file_dialog,
                                                             Pika                 *pika,
                                                             const gchar          *basename);
static gboolean  file_save_dialog_use_extension             (GtkWidget            *save_dialog,
                                                             GFile                *file);


/*  public functions  */

GtkWidget *
file_save_dialog_new (Pika     *pika,
                      gboolean  export)
{
  GtkWidget *dialog;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (! export)
    {
      dialog = pika_save_dialog_new (pika);

      pika_file_dialog_load_state (PIKA_FILE_DIALOG (dialog),
                                   "pika-file-save-dialog-state");
    }
  else
    {
      dialog = pika_export_dialog_new (pika);

      pika_file_dialog_load_state (PIKA_FILE_DIALOG (dialog),
                                   "pika-file-export-dialog-state");
    }

  g_signal_connect (dialog, "confirm-overwrite",
                    G_CALLBACK (file_save_dialog_confirm_overwrite),
                    pika);

  g_signal_connect (dialog, "response",
                    G_CALLBACK (file_save_dialog_response),
                    pika);

  return dialog;
}


/*  private functions  */

static GtkFileChooserConfirmation
file_save_dialog_confirm_overwrite (GtkWidget *dialog,
                                    Pika      *pika)
{
  PikaFileDialog *file_dialog = PIKA_FILE_DIALOG (dialog);

  if (file_save_dialog_no_overwrite_confirmation (file_dialog, pika))
    /* The URI will not be accepted whatever happens, so don't
     * bother asking the user about overwriting files
     */
    return GTK_FILE_CHOOSER_CONFIRMATION_ACCEPT_FILENAME;
  else
    return GTK_FILE_CHOOSER_CONFIRMATION_CONFIRM;
}

static void
file_save_dialog_response (GtkWidget *dialog,
                           gint       response_id,
                           Pika      *pika)
{
  PikaFileDialog      *file_dialog = PIKA_FILE_DIALOG (dialog);
  GFile               *file;
  gchar               *basename;
  PikaPlugInProcedure *save_proc;

  if (PIKA_IS_SAVE_DIALOG (dialog))
    {
      pika_file_dialog_save_state (file_dialog, "pika-file-save-dialog-state");
    }
  else /* PIKA_IS_EXPORT_DIALOG (dialog) */
    {
      pika_file_dialog_save_state (file_dialog, "pika-file-export-dialog-state");
    }

  if (response_id != GTK_RESPONSE_OK)
    {
      if (! file_dialog->busy && response_id != GTK_RESPONSE_HELP)
        gtk_widget_destroy (dialog);

      return;
    }

  g_object_ref (file_dialog);
  g_object_ref (file_dialog->image);

  switch (file_save_dialog_check_file (dialog, pika,
                                       &file, &basename, &save_proc))
    {
    case CHECK_URI_FAIL:
      break;

    case CHECK_URI_OK:
      {
        PikaImage    *image              = file_dialog->image;
        PikaProgress *progress           = PIKA_PROGRESS (dialog);
        PikaDisplay  *display_to_close   = NULL;
        gboolean      xcf_compression    = FALSE;
        gboolean      is_save_dialog     = PIKA_IS_SAVE_DIALOG (dialog);
        gboolean      close_after_saving = FALSE;
        gboolean      save_a_copy        = FALSE;

        if (is_save_dialog)
          {
            close_after_saving = PIKA_SAVE_DIALOG (dialog)->close_after_saving;
            display_to_close   = PIKA_DISPLAY (PIKA_SAVE_DIALOG (dialog)->display_to_close);
            save_a_copy        = PIKA_SAVE_DIALOG (dialog)->save_a_copy;
          }

        pika_file_dialog_set_sensitive (file_dialog, FALSE);

        if (PIKA_IS_SAVE_DIALOG (dialog))
          {
            xcf_compression = PIKA_SAVE_DIALOG (dialog)->compression;
          }

        /* Hide the file dialog while exporting, avoid dialogs piling
         * up, even more as some formats have preview features, so the
         * file dialog is just blocking the view.
         */
        if  (PIKA_IS_EXPORT_DIALOG (dialog))
          {
            gtk_widget_hide (dialog);
            progress = PIKA_PROGRESS (PIKA_EXPORT_DIALOG (dialog)->display);
          }

        g_signal_connect (dialog, "destroy",
                          G_CALLBACK (gtk_widget_destroyed),
                          &dialog);

        if (file_save_dialog_save_image (progress,
                                         pika,
                                         image,
                                         file,
                                         save_proc,
                                         PIKA_RUN_INTERACTIVE,
                                         is_save_dialog && ! save_a_copy,
                                         FALSE,
                                         PIKA_IS_EXPORT_DIALOG (dialog),
                                         xcf_compression,
                                         FALSE))
          {
            /* Save was successful, now store the URI in a couple of
             * places that depend on it being the user that made a
             * save. Lower-level URI management is handled in
             * file_save()
             */
            if (is_save_dialog)
              {
                if (save_a_copy)
                  pika_image_set_save_a_copy_file (image, file);

                g_object_set_data_full (G_OBJECT (image->pika),
                                        PIKA_FILE_SAVE_LAST_FILE_KEY,
                                        g_object_ref (file),
                                        (GDestroyNotify) g_object_unref);
              }
            else
              {
                g_object_set_data_full (G_OBJECT (image->pika),
                                        PIKA_FILE_EXPORT_LAST_FILE_KEY,
                                        g_object_ref (file),
                                        (GDestroyNotify) g_object_unref);
              }

            /*  make sure the menus are updated with the keys we've just set  */
            pika_image_flush (image);

            /* Handle close-after-saving */
            if (close_after_saving && display_to_close &&
                ! pika_image_is_dirty (pika_display_get_image (display_to_close)))
              {
                pika_display_close (display_to_close);
              }

            if (dialog)
              gtk_widget_destroy (dialog);
          }
        else
          {
            if (dialog)
              {
                GFile *parent_dir = g_file_get_parent (file);

                /* XXX Not sure why, but after reshowing the file
                 * chooser dialog, the displayed name is correct, but
                 * the parent directory is the current working dir.
                 * Force it to be the expected folder.
                 */
                gtk_file_chooser_set_current_folder_file (GTK_FILE_CHOOSER (dialog),
                                                          parent_dir, NULL);
                gtk_widget_show (dialog);
                g_object_unref (parent_dir);
              }
          }

        g_object_unref (file);
        g_free (basename);

        if (dialog)
          {
            pika_file_dialog_set_sensitive (file_dialog, TRUE);
            g_signal_handlers_disconnect_by_func (dialog,
                                                  G_CALLBACK (gtk_widget_destroyed),
                                                  &dialog);
          }
      }
      break;

    case CHECK_URI_SWITCH_DIALOGS:
      file_dialog->busy = TRUE; /* prevent destruction */
      gtk_dialog_response (GTK_DIALOG (dialog), FILE_SAVE_RESPONSE_OTHER_DIALOG);
      file_dialog->busy = FALSE;

      gtk_widget_destroy (dialog);
      break;
    }

  g_object_unref (file_dialog->image);
  g_object_unref (file_dialog);
}

/* IMPORTANT: When changing this function, keep
 * file_save_dialog_no_overwrite_confirmation() up to date. It is
 * difficult to move logic to a common place due to how the dialog is
 * implemented in GTK+ in combination with how we use it.
 */
static CheckUriResult
file_save_dialog_check_file (GtkWidget            *dialog,
                             Pika                 *pika,
                             GFile               **ret_file,
                             gchar               **ret_basename,
                             PikaPlugInProcedure **ret_save_proc)
{
  PikaFileDialog      *file_dialog = PIKA_FILE_DIALOG (dialog);
  GFile               *file;
  gchar               *uri;
  gchar               *basename;
  GFile               *basename_file;
  PikaPlugInProcedure *save_proc;
  PikaPlugInProcedure *uri_proc;
  PikaPlugInProcedure *basename_proc;

  file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));

  if (! file)
    return CHECK_URI_FAIL;

  basename      = g_path_get_basename (pika_file_get_utf8_name (file));
  basename_file = g_file_new_for_uri (basename);

  save_proc     = file_dialog->file_proc;
  uri_proc      = file_save_dialog_find_procedure (file_dialog, file);
  basename_proc = file_save_dialog_find_procedure (file_dialog, basename_file);

  g_object_unref (basename_file);

  uri = g_file_get_uri (file);

  PIKA_LOG (SAVE_DIALOG, "URI = %s", uri);
  PIKA_LOG (SAVE_DIALOG, "basename = %s", basename);
  PIKA_LOG (SAVE_DIALOG, "selected save_proc: %s",
            save_proc ?
            pika_procedure_get_label (PIKA_PROCEDURE (save_proc)) : "NULL");
  PIKA_LOG (SAVE_DIALOG, "URI save_proc: %s",
            uri_proc ?
            pika_procedure_get_label (PIKA_PROCEDURE (uri_proc)) : "NULL");
  PIKA_LOG (SAVE_DIALOG, "basename save_proc: %s",
            basename_proc ?
            pika_procedure_get_label (PIKA_PROCEDURE (basename_proc)) : "NULL");

  g_free (uri);

  /*  first check if the user entered an extension at all  */
  if (! basename_proc)
    {
      PIKA_LOG (SAVE_DIALOG, "basename has no valid extension");

      if (! strchr (basename, '.'))
        {
          const gchar *ext = NULL;

          PIKA_LOG (SAVE_DIALOG, "basename has no '.', trying to add extension");

          if (! save_proc && PIKA_IS_SAVE_DIALOG (dialog))
            {
              ext = "xcf";
            }
          else if (save_proc && save_proc->extensions_list)
            {
              ext = save_proc->extensions_list->data;
            }

          if (ext)
            {
              gchar *ext_basename;
              gchar *dirname;
              gchar *filename;
              gchar *utf8;

              PIKA_LOG (SAVE_DIALOG, "appending .%s to basename", ext);

              ext_basename = g_strconcat (basename, ".", ext, NULL);

              g_free (basename);
              basename = ext_basename;

              dirname  = g_path_get_dirname (pika_file_get_utf8_name (file));
              filename = g_build_filename (dirname, basename, NULL);
              g_free (dirname);

              utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
              gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog),
                                                 utf8);
              g_free (utf8);

              g_free (filename);

              PIKA_LOG (SAVE_DIALOG,
                        "set basename to %s, rerunning response and bailing out",
                        basename);

              /*  call the response callback again, so the
               *  overwrite-confirm logic can check the changed uri
               */
              gtk_dialog_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);

              goto fail;
            }
          else
            {
              PIKA_LOG (SAVE_DIALOG,
                        "save_proc has no extensions, continuing without");

              /*  there may be file formats with no extension at all, use
               *  the selected proc in this case.
               */
              basename_proc = save_proc;

              if (! uri_proc)
                uri_proc = basename_proc;
            }

          if (! basename_proc)
            {
              PIKA_LOG (SAVE_DIALOG,
                        "unable to figure save_proc, bailing out");

              if (file_save_dialog_switch_dialogs (file_dialog, pika, basename))
                {
                  goto switch_dialogs;
                }

              goto fail;
            }
        }
      else if (save_proc && ! save_proc->extensions_list)
        {
          PIKA_LOG (SAVE_DIALOG,
                    "basename has '.', but save_proc has no extensions, "
                    "accepting random extension");

          /*  accept any random extension if the file format has
           *  no extensions at all
           */
          basename_proc = save_proc;

          if (! uri_proc)
            uri_proc = basename_proc;
        }
    }

  /*  then check if the selected format matches the entered extension  */
  if (! save_proc)
    {
      PIKA_LOG (SAVE_DIALOG, "no save_proc was selected from the list");

      if (! basename_proc)
        {
          PIKA_LOG (SAVE_DIALOG,
                    "basename has no useful extension, bailing out");

          if (file_save_dialog_switch_dialogs (file_dialog, pika, basename))
            {
              goto switch_dialogs;
            }

          goto fail;
        }

      PIKA_LOG (SAVE_DIALOG, "use URI's proc '%s' so indirect saving works",
                pika_procedure_get_label (PIKA_PROCEDURE (uri_proc)));

      /*  use the URI's proc if no save proc was selected  */
      save_proc = uri_proc;
    }
  else
    {
      PIKA_LOG (SAVE_DIALOG, "save_proc '%s' was selected from the list",
                pika_procedure_get_label (PIKA_PROCEDURE (save_proc)));

      if (save_proc != basename_proc)
        {
          PIKA_LOG (SAVE_DIALOG, "however the basename's proc is '%s'",
                    pika_procedure_get_label (PIKA_PROCEDURE (basename_proc)));

          if (uri_proc != basename_proc)
            {
              PIKA_LOG (SAVE_DIALOG,
                        "that's impossible for remote URIs, bailing out");

              /*  remote URI  */

              pika_message (pika, G_OBJECT (dialog), PIKA_MESSAGE_WARNING,
                            _("Saving remote files needs to determine the "
                              "file format from the file extension. "
                              "Please enter a file extension that matches "
                              "the selected file format or enter no file "
                              "extension at all."));

              goto fail;
            }
          else
            {
              PIKA_LOG (SAVE_DIALOG,
                        "ask the user if she really wants that filename");

              /*  local URI  */

              if (! file_save_dialog_use_extension (dialog, file))
                {
                  goto fail;
                }
            }
        }
      else if (save_proc != uri_proc)
        {
          PIKA_LOG (SAVE_DIALOG,
                    "use URI's proc '%s' so indirect saving works",
                    pika_procedure_get_label (PIKA_PROCEDURE (uri_proc)));

          /*  need to use the URI's proc for saving because e.g.
           *  the GIF plug-in can't save a GIF to sftp://
           */
          save_proc = uri_proc;
        }
    }

  if (! save_proc)
    {
      g_warning ("%s: EEEEEEK", G_STRFUNC);

      return CHECK_URI_FAIL;
    }

  *ret_file      = file;
  *ret_basename  = basename;
  *ret_save_proc = save_proc;

  return CHECK_URI_OK;

 fail:

  g_object_unref (file);
  g_free (basename);

  return CHECK_URI_FAIL;

 switch_dialogs:

  g_object_unref (file);
  g_free (basename);

  return CHECK_URI_SWITCH_DIALOGS;
}

/*
 * IMPORTANT: Keep this up to date with file_save_dialog_check_uri().
 */
static gboolean
file_save_dialog_no_overwrite_confirmation (PikaFileDialog *file_dialog,
                                            Pika           *pika)
{
  GFile               *file;
  gchar               *basename;
  GFile               *basename_file;
  PikaPlugInProcedure *basename_proc;
  PikaPlugInProcedure *save_proc;
  gboolean             uri_will_change;
  gboolean             unknown_ext;

  file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (file_dialog));

  if (! file)
    return FALSE;

  basename      = g_path_get_basename (pika_file_get_utf8_name (file));
  basename_file = g_file_new_for_uri (basename);

  save_proc     = file_dialog->file_proc;
  basename_proc = file_save_dialog_find_procedure (file_dialog, basename_file);

  g_object_unref (basename_file);

  uri_will_change = (! basename_proc &&
                     ! strchr (basename, '.') &&
                     (! save_proc || save_proc->extensions_list));

  unknown_ext     = (! save_proc &&
                     ! basename_proc);

  g_free (basename);
  g_object_unref (file);

  return uri_will_change || unknown_ext;
}

static PikaPlugInProcedure *
file_save_dialog_find_procedure (PikaFileDialog *file_dialog,
                                 GFile          *file)
{
  PikaPlugInManager      *manager = file_dialog->pika->plug_in_manager;
  PikaFileProcedureGroup  group;

  if (PIKA_IS_SAVE_DIALOG (file_dialog))
    group = PIKA_FILE_PROCEDURE_GROUP_SAVE;
  else
    group = PIKA_FILE_PROCEDURE_GROUP_EXPORT;

  return pika_plug_in_manager_file_procedure_find (manager, group, file, NULL);
}

static gboolean
file_save_other_dialog_activated (GtkWidget   *label,
                                  const gchar *uri,
                                  GtkDialog   *dialog)
{
  gtk_dialog_response (dialog, FILE_SAVE_RESPONSE_OTHER_DIALOG);

  return TRUE;
}

static gboolean
file_save_dialog_switch_dialogs (PikaFileDialog *file_dialog,
                                 Pika           *pika,
                                 const gchar    *basename)
{
  PikaPlugInProcedure    *proc_in_other_group;
  PikaFileProcedureGroup  other_group;
  GFile                  *file;
  gboolean                switch_dialogs = FALSE;

  file = g_file_new_for_uri (basename);

  if (PIKA_IS_EXPORT_DIALOG (file_dialog))
    other_group = PIKA_FILE_PROCEDURE_GROUP_SAVE;
  else
    other_group = PIKA_FILE_PROCEDURE_GROUP_EXPORT;

  proc_in_other_group =
    pika_plug_in_manager_file_procedure_find (pika->plug_in_manager,
                                              other_group, file, NULL);

  g_object_unref (file);

  if (proc_in_other_group)
    {
      GtkWidget   *dialog;
      const gchar *primary;
      const gchar *message;
      const gchar *link;

      if (PIKA_IS_EXPORT_DIALOG (file_dialog))
        {
          primary = _("The given filename cannot be used for exporting");
          message = _("You can use this dialog to export to various file formats. "
                      "If you want to save the image to the PIKA XCF format, use "
                      "File→Save instead.");
          link    = _("Take me to the Save dialog");
        }
      else
        {
          primary = _("The given filename cannot be used for saving");
          message = _("You can use this dialog to save to the PIKA XCF "
                      "format. Use File→Export to export to other file formats.");
          link    = _("Take me to the Export dialog");
        }

      dialog = pika_message_dialog_new (_("Extension Mismatch"),
                                        PIKA_ICON_DIALOG_WARNING,
                                        GTK_WIDGET (file_dialog),
                                        GTK_DIALOG_DESTROY_WITH_PARENT,
                                        pika_standard_help_func, NULL,

                                        _("_OK"), GTK_RESPONSE_OK,

                                        NULL);

      pika_message_box_set_primary_text (PIKA_MESSAGE_DIALOG (dialog)->box,
                                         "%s", primary);

      pika_message_box_set_text (PIKA_MESSAGE_DIALOG (dialog)->box,
                                 "%s", message);

      if (PIKA_IS_EXPORT_DIALOG (file_dialog) ||
          (! PIKA_SAVE_DIALOG (file_dialog)->save_a_copy &&
          ! PIKA_SAVE_DIALOG (file_dialog)->close_after_saving))
        {
          GtkWidget *label;
          gchar     *markup;

          markup = g_strdup_printf ("<a href=\"other-dialog\">%s</a>", link);
          label = gtk_label_new (markup);
          g_free (markup);

          gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
          gtk_label_set_xalign (GTK_LABEL (label), 0.0);
          gtk_box_pack_start (GTK_BOX (PIKA_MESSAGE_DIALOG (dialog)->box), label,
                              FALSE, FALSE, 0);
          gtk_widget_show (label);

          g_signal_connect (label, "activate-link",
                            G_CALLBACK (file_save_other_dialog_activated),
                            dialog);
        }

      gtk_dialog_set_response_sensitive (GTK_DIALOG (file_dialog),
                                         GTK_RESPONSE_CANCEL, FALSE);
      gtk_dialog_set_response_sensitive (GTK_DIALOG (file_dialog),
                                         GTK_RESPONSE_OK, FALSE);

      g_object_ref (dialog);

      if (pika_dialog_run (PIKA_DIALOG (dialog)) == FILE_SAVE_RESPONSE_OTHER_DIALOG)
        {
          switch_dialogs = TRUE;
        }

      gtk_widget_destroy (dialog);
      g_object_unref (dialog);

      gtk_dialog_set_response_sensitive (GTK_DIALOG (file_dialog),
                                         GTK_RESPONSE_CANCEL, TRUE);
      gtk_dialog_set_response_sensitive (GTK_DIALOG (file_dialog),
                                         GTK_RESPONSE_OK, TRUE);
    }
  else
    {
      pika_message (pika, G_OBJECT (file_dialog), PIKA_MESSAGE_WARNING,
                    _("The given filename does not have any known "
                      "file extension. Please enter a known file "
                      "extension or select a file format from the "
                      "file format list."));
    }

  return switch_dialogs;
}

static gboolean
file_save_dialog_use_extension (GtkWidget *save_dialog,
                                GFile     *file)
{
  GtkWidget *dialog;
  gboolean   use_name = FALSE;

  dialog = pika_message_dialog_new (_("Extension Mismatch"),
                                    PIKA_ICON_DIALOG_QUESTION,
                                    save_dialog, GTK_DIALOG_DESTROY_WITH_PARENT,
                                    pika_standard_help_func, NULL,

                                    _("_Cancel"), GTK_RESPONSE_CANCEL,
                                    _("_Save"),   GTK_RESPONSE_OK,

                                    NULL);

  pika_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);

  pika_message_box_set_primary_text (PIKA_MESSAGE_DIALOG (dialog)->box,
                                     _("The given file extension does "
                                       "not match the chosen file type."));

  pika_message_box_set_text (PIKA_MESSAGE_DIALOG (dialog)->box,
                             _("Do you want to save the image using this "
                               "name anyway?"));

  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
                                     GTK_RESPONSE_CANCEL, FALSE);
  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
                                     GTK_RESPONSE_OK, FALSE);

  g_object_ref (dialog);

  use_name = (pika_dialog_run (PIKA_DIALOG (dialog)) == GTK_RESPONSE_OK);

  gtk_widget_destroy (dialog);
  g_object_unref (dialog);

  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
                                     GTK_RESPONSE_CANCEL, TRUE);
  gtk_dialog_set_response_sensitive (GTK_DIALOG (save_dialog),
                                     GTK_RESPONSE_OK, TRUE);

  return use_name;
}

gboolean
file_save_dialog_save_image (PikaProgress        *progress,
                             Pika                *pika,
                             PikaImage           *image,
                             GFile               *file,
                             PikaPlugInProcedure *save_proc,
                             PikaRunMode          run_mode,
                             gboolean             change_saved_state,
                             gboolean             export_backward,
                             gboolean             export_forward,
                             gboolean             xcf_compression,
                             gboolean             verbose_cancel)
{
  PikaPDBStatusType  status;
  GError            *error   = NULL;
  GList             *list;
  gboolean           success = FALSE;

  for (list = pika_action_groups_from_name ("file");
       list;
       list = g_list_next (list))
    {
      pika_action_group_set_action_sensitive (list->data, "file-quit", FALSE, NULL);
    }

  pika_image_set_xcf_compression (image, xcf_compression);

  status = file_save (pika, image, progress, file,
                      save_proc, run_mode,
                      change_saved_state, export_backward, export_forward,
                      &error);

  switch (status)
    {
    case PIKA_PDB_SUCCESS:
      success = TRUE;
      break;

    case PIKA_PDB_CANCEL:
      if (verbose_cancel)
        pika_message_literal (pika,
                              G_OBJECT (progress), PIKA_MESSAGE_INFO,
                              _("Saving canceled"));
      break;

    default:
      {
        pika_message (pika, G_OBJECT (progress), PIKA_MESSAGE_ERROR,
                      _("Saving '%s' failed:\n\n%s"),
                      pika_file_get_utf8_name (file),
                      error ? error->message : _("Unknown error"));
        g_clear_error (&error);
      }
      break;
    }

  for (list = pika_action_groups_from_name ("file");
       list;
       list = g_list_next (list))
    {
      pika_action_group_set_action_sensitive (list->data, "file-quit", TRUE, NULL);
    }

  return success;
}
