/* 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 Spencer Kimball and Peter Mattis
 *
 * 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 <errno.h>

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

#ifdef GDK_WINDOWING_X11
#include <gdk/gdkx.h>
#endif

#ifdef G_OS_WIN32
#include <windows.h>
#include <fcntl.h>
#include <io.h>

#ifndef pipe
#define pipe(fds) _pipe(fds, 4096, _O_BINARY)
#endif
#else
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <unistd.h>
#endif

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

#include "gui-types.h"

#include "config/pikaguiconfig.h"

#include "core/pika.h"
#include "core/pika-parallel.h"
#include "core/pika-spawn.h"
#include "core/pika-utils.h"
#include "core/pikaasync.h"
#include "core/pikabrush.h"
#include "core/pikacancelable.h"
#include "core/pikacontainer.h"
#include "core/pikacontext.h"
#include "core/pikadatafactory.h"
#include "core/pikadrawable.h"
#include "core/pikagradient.h"
#include "core/pikaimage.h"
#include "core/pikaimagefile.h"
#include "core/pikalist.h"
#include "core/pikapalette.h"
#include "core/pikapattern.h"
#include "core/pikaprogress.h"
#include "core/pikawaitable.h"

#include "text/pikafont.h"

#include "pdb/pikapdb.h"
#include "pdb/pikaprocedure.h"

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

#include "widgets/pikaactiongroup.h"
#include "widgets/pikabrushselect.h"
#include "widgets/pikadialogfactory.h"
#include "widgets/pikadocked.h"
#include "widgets/pikafontselect.h"
#include "widgets/pikagradientselect.h"
#include "widgets/pikahelp.h"
#include "widgets/pikahelp-ids.h"
#include "widgets/pikamenufactory.h"
#include "widgets/pikapaletteselect.h"
#include "widgets/pikapatternselect.h"
#include "widgets/pikapickableselect.h"
#include "widgets/pikaprogressdialog.h"
#include "widgets/pikauimanager.h"
#include "widgets/pikawidgets-utils.h"

#include "display/pikadisplay.h"
#include "display/pikadisplay-foreach.h"
#include "display/pikadisplayshell.h"
#include "display/pikasinglewindowstrategy.h"
#include "display/pikamultiwindowstrategy.h"

#include "actions/plug-in-actions.h"

#include "menus/menus.h"

#include "dialogs/color-profile-import-dialog.h"
#include "dialogs/metadata-rotation-import-dialog.h"

#include "gui.h"
#include "gui-message.h"
#include "gui-vtable.h"
#include "icon-themes.h"
#include "themes.h"

#include "pika-intl.h"


/*  local function prototypes  */

static void           gui_ungrab                 (Pika                *pika);

static void           gui_set_busy               (Pika                *pika);
static void           gui_unset_busy             (Pika                *pika);

static void           gui_help                   (Pika                *pika,
                                                  PikaProgress        *progress,
                                                  const gchar         *help_domain,
                                                  const gchar         *help_id);
static const gchar  * gui_get_program_class      (Pika                *pika);
static gchar        * gui_get_display_name       (Pika                *pika,
                                                  gint                 display_id,
                                                  GObject            **monitor,
                                                  gint                *monitor_number);
static guint32        gui_get_user_time          (Pika                *pika);
static GFile        * gui_get_theme_dir          (Pika                *pika);
static GFile        * gui_get_icon_theme_dir     (Pika                *pika);
static PikaObject   * gui_get_window_strategy    (Pika                *pika);
static PikaDisplay  * gui_get_empty_display      (Pika                *pika);
static GBytes       * gui_display_get_window_id  (PikaDisplay         *display);
static PikaDisplay  * gui_display_create         (Pika                *pika,
                                                  PikaImage           *image,
                                                  PikaUnit             unit,
                                                  gdouble              scale,
                                                  GObject             *monitor);
static void           gui_display_delete         (PikaDisplay         *display);
static void           gui_displays_reconnect     (Pika                *pika,
                                                  PikaImage           *old_image,
                                                  PikaImage           *new_image);
static gboolean       gui_wait                   (Pika                *pika,
                                                  PikaWaitable        *waitable,
                                                  const gchar         *message);
static PikaProgress * gui_new_progress           (Pika                *pika,
                                                  PikaDisplay         *display);
static void           gui_free_progress          (Pika                *pika,
                                                  PikaProgress        *progress);
static gboolean       gui_pdb_dialog_new         (Pika                *pika,
                                                  PikaContext         *context,
                                                  PikaProgress        *progress,
                                                  GType                object_type,
                                                  GBytes              *parent_handle,
                                                  const gchar         *title,
                                                  const gchar         *callback_name,
                                                  PikaObject          *object,
                                                  va_list              args);
static gboolean       gui_pdb_dialog_set         (Pika                *pika,
                                                  GType                contents_type,
                                                  const gchar         *callback_name,
                                                  PikaObject          *object,
                                                  va_list              args);
static gboolean       gui_pdb_dialog_close       (Pika                *pika,
                                                  GType                contents_type,
                                                  const gchar         *callback_name);
static gboolean       gui_recent_list_add_file   (Pika                *pika,
                                                  GFile               *file,
                                                  const gchar         *mime_type);
static void           gui_recent_list_load       (Pika                *pika);

static GMountOperation
                    * gui_get_mount_operation    (Pika                *pika,
                                                  PikaProgress        *progress);

static PikaColorProfilePolicy
                      gui_query_profile_policy   (Pika                *pika,
                                                  PikaImage           *image,
                                                  PikaContext         *context,
                                                  PikaColorProfile   **dest_profile,
                                                  PikaColorRenderingIntent *intent,
                                                  gboolean            *bpc,
                                                  gboolean            *dont_ask);
static PikaMetadataRotationPolicy
                      gui_query_rotation_policy  (Pika                *pika,
                                                  PikaImage           *image,
                                                  PikaContext         *context,
                                                  gboolean            *dont_ask);

static void           gui_inhibit                (Pika                *pika);
static void           gui_image_disconnect       (PikaImage           *image,
                                                  Pika                *pika);


/*  public functions  */

void
gui_vtable_init (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  pika->gui.ungrab                 = gui_ungrab;
  pika->gui.set_busy               = gui_set_busy;
  pika->gui.unset_busy             = gui_unset_busy;
  pika->gui.show_message           = gui_message;
  pika->gui.help                   = gui_help;
  pika->gui.get_program_class      = gui_get_program_class;
  pika->gui.get_display_name       = gui_get_display_name;
  pika->gui.get_user_time          = gui_get_user_time;
  pika->gui.get_theme_dir          = gui_get_theme_dir;
  pika->gui.get_icon_theme_dir     = gui_get_icon_theme_dir;
  pika->gui.get_window_strategy    = gui_get_window_strategy;
  pika->gui.get_empty_display      = gui_get_empty_display;
  pika->gui.display_get_window_id  = gui_display_get_window_id;
  pika->gui.display_create         = gui_display_create;
  pika->gui.display_delete         = gui_display_delete;
  pika->gui.displays_reconnect     = gui_displays_reconnect;
  pika->gui.wait                   = gui_wait;
  pika->gui.progress_new           = gui_new_progress;
  pika->gui.progress_free          = gui_free_progress;
  pika->gui.pdb_dialog_new         = gui_pdb_dialog_new;
  pika->gui.pdb_dialog_set         = gui_pdb_dialog_set;
  pika->gui.pdb_dialog_close       = gui_pdb_dialog_close;
  pika->gui.recent_list_add_file   = gui_recent_list_add_file;
  pika->gui.recent_list_load       = gui_recent_list_load;
  pika->gui.get_mount_operation    = gui_get_mount_operation;
  pika->gui.query_profile_policy   = gui_query_profile_policy;
  pika->gui.query_rotation_policy  = gui_query_rotation_policy;
}


/*  private functions  */

static void
gui_ungrab (Pika *pika)
{
  GdkDisplay *display = gdk_display_get_default ();

  if (display)
    gdk_seat_ungrab (gdk_display_get_default_seat (display));
}

static void
gui_set_busy (Pika *pika)
{
  pika_displays_set_busy (pika);
  pika_dialog_factory_set_busy (pika_dialog_factory_get_singleton ());

  gdk_display_flush (gdk_display_get_default ());
}

static void
gui_unset_busy (Pika *pika)
{
  pika_displays_unset_busy (pika);
  pika_dialog_factory_unset_busy (pika_dialog_factory_get_singleton ());

  gdk_display_flush (gdk_display_get_default ());
}

static void
gui_help (Pika         *pika,
          PikaProgress *progress,
          const gchar  *help_domain,
          const gchar  *help_id)
{
  pika_help_show (pika, progress, help_domain, help_id);
}

static const gchar *
gui_get_program_class (Pika *pika)
{
  return gdk_get_program_class ();
}

static gint
get_monitor_number (GdkMonitor *monitor)
{
  GdkDisplay *display    = gdk_monitor_get_display (monitor);
  gint        n_monitors = gdk_display_get_n_monitors (display);
  gint        i;

  for (i = 0; i < n_monitors; i++)
    if (gdk_display_get_monitor (display, i) == monitor)
      return i;

  return 0;
}

static gchar *
gui_get_display_name (Pika     *pika,
                      gint      display_id,
                      GObject **monitor,
                      gint     *monitor_number)
{
  PikaDisplay *display = NULL;
  GdkDisplay  *gdk_display;

  if (display_id > 0)
    display = pika_display_get_by_id (pika, display_id);

  if (display)
    {
      PikaDisplayShell *shell = pika_display_get_shell (display);

      gdk_display = gtk_widget_get_display (GTK_WIDGET (shell));

      *monitor = G_OBJECT (pika_widget_get_monitor (GTK_WIDGET (shell)));
    }
  else
    {
      *monitor = G_OBJECT (gui_get_initial_monitor (pika));

      if (! *monitor)
        *monitor = G_OBJECT (pika_get_monitor_at_pointer ());

      gdk_display = gdk_monitor_get_display (GDK_MONITOR (*monitor));
    }

  *monitor_number = get_monitor_number (GDK_MONITOR (*monitor));

  return g_strdup (gdk_display_get_name (gdk_display));
}

static guint32
gui_get_user_time (Pika *pika)
{
#ifdef GDK_WINDOWING_X11
  if (GDK_IS_X11_DISPLAY (gdk_display_get_default ()))
    return gdk_x11_display_get_user_time (gdk_display_get_default ());
#endif

  return gtk_get_current_event_time ();
}

static GFile *
gui_get_theme_dir (Pika *pika)
{
  return themes_get_theme_dir (pika, PIKA_GUI_CONFIG (pika->config)->theme);
}

static GFile *
gui_get_icon_theme_dir (Pika *pika)
{
  return icon_themes_get_theme_dir (pika, PIKA_GUI_CONFIG (pika->config)->icon_theme);
}

static PikaObject *
gui_get_window_strategy (Pika *pika)
{
  if (PIKA_GUI_CONFIG (pika->config)->single_window_mode)
    return pika_single_window_strategy_get_singleton ();
  else
    return pika_multi_window_strategy_get_singleton ();
}

static PikaDisplay *
gui_get_empty_display (Pika *pika)
{
  PikaDisplay *display = NULL;

  if (pika_container_get_n_children (pika->displays) == 1)
    {
      display = (PikaDisplay *) pika_container_get_first_child (pika->displays);

      if (pika_display_get_image (display))
        {
          /* The display was not empty */
          display = NULL;
        }
    }

  return display;
}

static GBytes *
gui_display_get_window_id (PikaDisplay *display)
{
  PikaDisplay      *disp  = PIKA_DISPLAY (display);
  PikaDisplayShell *shell = pika_display_get_shell (disp);

  if (shell)
    {
      if (shell)
        return g_bytes_ref (shell->window_handle);
    }

  return NULL;
}

static PikaDisplay *
gui_display_create (Pika      *pika,
                    PikaImage *image,
                    PikaUnit   unit,
                    gdouble    scale,
                    GObject   *monitor)
{
  PikaContext *context = pika_get_user_context (pika);
  PikaDisplay *display = PIKA_DISPLAY (gui_get_empty_display (pika));

  if (! monitor)
    monitor = G_OBJECT (pika_get_monitor_at_pointer ());

  if (display)
    {
      pika_display_fill (display, image, unit, scale);
    }
  else
    {
      GList *image_managers = pika_ui_managers_from_name ("<Image>");

      g_return_val_if_fail (image_managers != NULL, NULL);

      display = pika_display_new (pika, image, unit, scale,
                                  image_managers->data,
                                  pika_dialog_factory_get_singleton (),
                                  GDK_MONITOR (monitor));
   }

  if (pika_context_get_display (context) == display)
    {
      pika_context_set_image (context, image);
      pika_context_display_changed (context);
    }
  else
    {
      pika_context_set_display (context, display);
    }

  if (image)
    {
      g_signal_handlers_disconnect_by_func (image,
                                            G_CALLBACK (gui_inhibit),
                                            pika);
      g_signal_handlers_disconnect_by_func (image,
                                            G_CALLBACK (gui_image_disconnect),
                                            pika);
      g_signal_connect_swapped (image, "dirty",
                                G_CALLBACK (gui_inhibit),
                                pika);
      g_signal_connect_swapped (image, "clean",
                                G_CALLBACK (gui_inhibit),
                                pika);
      g_signal_connect_after (image, "disconnect",
                              G_CALLBACK (gui_image_disconnect),
                              pika);
    }

  return display;
}

static void
gui_display_delete (PikaDisplay *display)
{
  pika_display_close (display);
}

static void
gui_displays_reconnect (Pika      *pika,
                        PikaImage *old_image,
                        PikaImage *new_image)
{
  pika_displays_reconnect (pika, old_image, new_image);
}

static void
gui_wait_input_async (PikaAsync  *async,
                      const gint  input_pipe[2])
{
  guint8 buffer[1];

  while (read (input_pipe[0], buffer, sizeof (buffer)) == -1 &&
         errno == EINTR);

  pika_async_finish (async, NULL);
}

static gboolean
gui_wait (Pika         *pika,
          PikaWaitable *waitable,
          const gchar  *message)
{
  PikaProcedure  *procedure;
  PikaValueArray *args;
  PikaAsync      *input_async = NULL;
  GError         *error       = NULL;
  gint            input_pipe[2];
  gint            output_pipe[2];

  procedure = pika_pdb_lookup_procedure (pika->pdb, "plug-in-busy-dialog");

  if (! procedure)
    return FALSE;

  if (pipe (input_pipe))
    return FALSE;

  if (pipe (output_pipe))
    {
      close (input_pipe[0]);
      close (input_pipe[1]);

      return FALSE;
    }

  pika_spawn_set_cloexec (input_pipe[0]);
  pika_spawn_set_cloexec (output_pipe[1]);

  args = pika_procedure_get_arguments (procedure);
  pika_value_array_truncate (args, 5);

  g_value_set_enum    (pika_value_array_index (args, 0),
                       PIKA_RUN_INTERACTIVE);
  g_value_set_int     (pika_value_array_index (args, 1),
                       output_pipe[0]);
  g_value_set_int     (pika_value_array_index (args, 2),
                       input_pipe[1]);
  g_value_set_string  (pika_value_array_index (args, 3),
                       message);
  g_value_set_boolean (pika_value_array_index (args, 4),
                       PIKA_IS_CANCELABLE (waitable));

  pika_procedure_execute_async (procedure, pika,
                                pika_get_user_context (pika),
                                NULL, args, NULL, &error);

  pika_value_array_unref (args);

  close (input_pipe[1]);
  close (output_pipe[0]);

  if (error)
    {
      g_clear_error (&error);

      close (input_pipe[0]);
      close (output_pipe[1]);

      return FALSE;
    }

  if (PIKA_IS_CANCELABLE (waitable))
    {
      /* listens for a cancellation request */
      input_async = pika_parallel_run_async_independent (
        (PikaRunAsyncFunc) gui_wait_input_async,
        input_pipe);

      while (! pika_waitable_wait_for (waitable, 0.1 * G_TIME_SPAN_SECOND))
        {
          /* check for a cancellation request */
          if (pika_waitable_try_wait (PIKA_WAITABLE (input_async)))
            {
              pika_cancelable_cancel (PIKA_CANCELABLE (waitable));

              break;
            }
        }
    }

  pika_waitable_wait (waitable);

  /* signal completion to the plug-in */
  close (output_pipe[1]);

  if (input_async)
    {
      pika_waitable_wait (PIKA_WAITABLE (input_async));

      g_object_unref (input_async);
    }

  close (input_pipe[0]);

  return TRUE;
}

static PikaProgress *
gui_new_progress (Pika        *pika,
                  PikaDisplay *display)
{
  g_return_val_if_fail (display == NULL || PIKA_IS_DISPLAY (display), NULL);

  if (display)
    return PIKA_PROGRESS (display);

  return PIKA_PROGRESS (pika_progress_dialog_new ());
}

static void
gui_free_progress (Pika          *pika,
                   PikaProgress  *progress)
{
  g_return_if_fail (PIKA_IS_PROGRESS (progress));

  if (PIKA_IS_PROGRESS_DIALOG (progress))
    gtk_widget_destroy (GTK_WIDGET (progress));
}

static gboolean
gui_pdb_dialog_present (GtkWindow *window)
{
  gtk_window_present (window);

  return FALSE;
}

static gboolean
gui_pdb_dialog_new (Pika          *pika,
                    PikaContext   *context,
                    PikaProgress  *progress,
                    GType          contents_type,
                    GBytes        *parent_handle,
                    const gchar   *title,
                    const gchar   *callback_name,
                    PikaObject    *object,
                    va_list        args)
{
  GType        dialog_type = G_TYPE_NONE;
  const gchar *dialog_role = NULL;
  const gchar *help_id     = NULL;

  if (contents_type == PIKA_TYPE_BRUSH)
    {
      dialog_type = PIKA_TYPE_BRUSH_SELECT;
      dialog_role = "pika-brush-selection";
      help_id     = PIKA_HELP_BRUSH_DIALOG;
    }
  else if (contents_type == PIKA_TYPE_FONT)
    {
      dialog_type = PIKA_TYPE_FONT_SELECT;
      dialog_role = "pika-font-selection";
      help_id     = PIKA_HELP_FONT_DIALOG;
    }
  else if (contents_type == PIKA_TYPE_GRADIENT)
    {
      dialog_type = PIKA_TYPE_GRADIENT_SELECT;
      dialog_role = "pika-gradient-selection";
      help_id     = PIKA_HELP_GRADIENT_DIALOG;
    }
  else if (contents_type == PIKA_TYPE_PALETTE)
    {
      dialog_type = PIKA_TYPE_PALETTE_SELECT;
      dialog_role = "pika-palette-selection";
      help_id     = PIKA_HELP_PALETTE_DIALOG;
    }
  else if (contents_type == PIKA_TYPE_PATTERN)
    {
      dialog_type = PIKA_TYPE_PATTERN_SELECT;
      dialog_role = "pika-pattern-selection";
      help_id     = PIKA_HELP_PATTERN_DIALOG;
    }
  else if (g_type_is_a (contents_type, PIKA_TYPE_DRAWABLE))
    {
      dialog_type = PIKA_TYPE_PICKABLE_SELECT;
      dialog_role = "pika-pickable-selection";
    }
  else
    {
      g_return_val_if_reached (FALSE);
    }

  if (dialog_type != G_TYPE_NONE)
    {
      if (! object && ! g_type_is_a (contents_type, PIKA_TYPE_DRAWABLE))
        object = pika_context_get_by_type (context, contents_type);

      if (object || g_type_is_a (contents_type, PIKA_TYPE_DRAWABLE))
        {
          gint        n_properties = 0;
          gchar     **names        = NULL;
          GValue     *values       = NULL;
          GtkWidget  *dialog;
          GtkWidget  *view;
          gboolean    use_header_bar;

          g_object_get (gtk_settings_get_default (),
                        "gtk-dialogs-use-header", &use_header_bar,
                        NULL);

          names = pika_properties_append (dialog_type,
                                          &n_properties, names, &values,
                                          "title",          title,
                                          "role",           dialog_role,
                                          "help-func",      pika_standard_help_func,
                                          "help-id",        help_id,
                                          "pdb",            pika->pdb,
                                          "context",        context,
                                          "select-type",    contents_type,
                                          "initial-object", object,
                                          "callback-name",  callback_name,
                                          "menu-factory",   menus_get_global_menu_factory (pika),
                                          "use-header-bar", use_header_bar,
                                          NULL);

          names = pika_properties_append_valist (dialog_type,
                                                 &n_properties, names, &values,
                                                 args);

          dialog = (GtkWidget *)
            g_object_new_with_properties (dialog_type,
                                          n_properties,
                                          (const gchar **) names,
                                          (const GValue *) values);

          pika_properties_free (n_properties, names, values);

          view = PIKA_PDB_DIALOG (dialog)->view;
          if (view)
            pika_docked_set_show_button_bar (PIKA_DOCKED (view), FALSE);

          if (progress)
            pika_window_set_transient_for (GTK_WINDOW (dialog), progress);

          gtk_widget_show (dialog);

          /*  workaround for bug #360106  */
            {
              GSource  *source = g_timeout_source_new (100);
              GClosure *closure;

              closure = g_cclosure_new_object (G_CALLBACK (gui_pdb_dialog_present),
                                               G_OBJECT (dialog));

              g_source_set_closure (source, closure);
              g_source_attach (source, NULL);
              g_source_unref (source);
            }

          if (parent_handle != NULL)
            pika_window_set_transient_for_handle (GTK_WINDOW (dialog), parent_handle);

          return TRUE;
        }
    }

  return FALSE;
}

static gboolean
gui_pdb_dialog_set (Pika          *pika,
                    GType          contents_type,
                    const gchar   *callback_name,
                    PikaObject    *object,
                    va_list        args)
{
  PikaPdbDialogClass *klass     = NULL;
  PikaContainer      *container = NULL;
  PikaPdbDialog      *dialog;

  if (contents_type == PIKA_TYPE_BRUSH)
    {
      klass = g_type_class_peek (PIKA_TYPE_BRUSH_SELECT);
      container = pika_data_factory_get_container (pika->brush_factory);
    }
  else if (contents_type == PIKA_TYPE_FONT)
    {
      klass = g_type_class_peek (PIKA_TYPE_FONT_SELECT);
      container = pika_data_factory_get_container (pika->font_factory);
    }
  else if (contents_type == PIKA_TYPE_GRADIENT)
    {
      klass = g_type_class_peek (PIKA_TYPE_GRADIENT_SELECT);
      container = pika_data_factory_get_container (pika->gradient_factory);
    }
  else if (contents_type == PIKA_TYPE_PALETTE)
    {
      klass = g_type_class_peek (PIKA_TYPE_PALETTE_SELECT);
      container = pika_data_factory_get_container (pika->palette_factory);
    }
  else if (contents_type == PIKA_TYPE_PATTERN)
    {
      klass = g_type_class_peek (PIKA_TYPE_PATTERN_SELECT);
      container = pika_data_factory_get_container (pika->pattern_factory);
    }
  else if (contents_type == PIKA_TYPE_DRAWABLE)
    {
      klass = g_type_class_peek (PIKA_TYPE_PICKABLE_SELECT);
    }

  g_return_val_if_fail (klass != NULL, FALSE);

  dialog = pika_pdb_dialog_get_by_callback (klass, callback_name);

  if (dialog != NULL                       &&
      dialog->select_type == contents_type &&
      (container == NULL || pika_container_get_child_index (container, object) != -1))
    {
      const gchar *prop_name = va_arg (args, const gchar *);

      if (g_type_is_a (contents_type, PIKA_TYPE_RESOURCE))
        {
          g_return_val_if_fail (container != NULL, FALSE);
          pika_context_set_by_type (dialog->context, dialog->select_type, object);
        }
      else
        {
          g_return_val_if_fail (klass->set_object != NULL, FALSE);
          klass->set_object (dialog, object);
        }

      if (prop_name)
        g_object_set_valist (G_OBJECT (dialog), prop_name, args);

      gtk_window_present (GTK_WINDOW (dialog));

      return TRUE;
    }

  return FALSE;
}

static gboolean
gui_pdb_dialog_close (Pika        *pika,
                      GType        contents_type,
                      const gchar *callback_name)
{
  PikaPdbDialogClass *klass = NULL;

  if (contents_type == PIKA_TYPE_BRUSH)
    klass = g_type_class_peek (PIKA_TYPE_BRUSH_SELECT);
  else if (contents_type == PIKA_TYPE_FONT)
    klass = g_type_class_peek (PIKA_TYPE_FONT_SELECT);
  else if (contents_type == PIKA_TYPE_GRADIENT)
    klass = g_type_class_peek (PIKA_TYPE_GRADIENT_SELECT);
  else if (contents_type == PIKA_TYPE_PALETTE)
    klass = g_type_class_peek (PIKA_TYPE_PALETTE_SELECT);
  else if (contents_type == PIKA_TYPE_PATTERN)
    klass = g_type_class_peek (PIKA_TYPE_PATTERN_SELECT);
  else if (contents_type == PIKA_TYPE_DRAWABLE)
    klass = g_type_class_peek (PIKA_TYPE_PICKABLE_SELECT);

  if (klass)
    {
      PikaPdbDialog *dialog;

      dialog = pika_pdb_dialog_get_by_callback (klass, callback_name);

      if (dialog && dialog->select_type == contents_type)
        {
          gtk_widget_destroy (GTK_WIDGET (dialog));
          return TRUE;
        }
    }

  return FALSE;
}

static gboolean
gui_recent_list_add_file (Pika        *pika,
                          GFile       *file,
                          const gchar *mime_type)
{
  GtkRecentData  recent;
  const gchar   *groups[2] = { "Graphics", NULL };
  gchar         *uri;
  gboolean       success;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);

  /* use last part of the URI */
  recent.display_name = NULL;

  /* no special description */
  recent.description  = NULL;
  recent.mime_type    = (mime_type ?
                         (gchar *) mime_type : "application/octet-stream");
  recent.app_name     = "Photo and Image Kooker Application";
  recent.app_exec     = PIKA_COMMAND " %u";
  recent.groups       = (gchar **) groups;
  recent.is_private   = FALSE;

  uri = g_file_get_uri (file);

  success = gtk_recent_manager_add_full (gtk_recent_manager_get_default (),
                                         uri, &recent);

  g_free (uri);

  return success;
}

static gint
gui_recent_list_compare (gconstpointer a,
                         gconstpointer b)
{
  return (gtk_recent_info_get_modified ((GtkRecentInfo *) a) -
          gtk_recent_info_get_modified ((GtkRecentInfo *) b));
}

static void
gui_recent_list_load (Pika *pika)
{
  GList *items;
  GList *list;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  pika_container_freeze (pika->documents);
  pika_container_clear (pika->documents);

  items = gtk_recent_manager_get_items (gtk_recent_manager_get_default ());

  items = g_list_sort (items, gui_recent_list_compare);

  for (list = items; list; list = list->next)
    {
      GtkRecentInfo *info = list->data;

      if (gtk_recent_info_has_application (info,
                                           "Photo and Image Kooker Application"))
        {
          const gchar *mime_type = gtk_recent_info_get_mime_type (info);

          if (mime_type &&
              pika_plug_in_manager_file_procedure_find_by_mime_type (pika->plug_in_manager,
                                                                     PIKA_FILE_PROCEDURE_GROUP_OPEN,
                                                                     mime_type))
            {
              PikaImagefile *imagefile;
              GFile         *file;

              file = g_file_new_for_uri (gtk_recent_info_get_uri (info));
              imagefile = pika_imagefile_new (pika, file);
              g_object_unref (file);

              pika_imagefile_set_mime_type (imagefile, mime_type);

              pika_container_add (pika->documents, PIKA_OBJECT (imagefile));
              g_object_unref (imagefile);
            }
        }

      gtk_recent_info_unref (info);
    }

  g_list_free (items);

  pika_container_thaw (pika->documents);
}

static GMountOperation *
gui_get_mount_operation (Pika         *pika,
                         PikaProgress *progress)
{
  GtkWidget *toplevel = NULL;

  if (GTK_IS_WIDGET (progress))
    toplevel = gtk_widget_get_toplevel (GTK_WIDGET (progress));

  return gtk_mount_operation_new (GTK_WINDOW (toplevel));
}

static PikaColorProfilePolicy
gui_query_profile_policy (Pika                      *pika,
                          PikaImage                 *image,
                          PikaContext               *context,
                          PikaColorProfile         **dest_profile,
                          PikaColorRenderingIntent  *intent,
                          gboolean                  *bpc,
                          gboolean                  *dont_ask)
{
  return color_profile_import_dialog_run (image, context, NULL,
                                          dest_profile,
                                          intent, bpc,
                                          dont_ask);
}

static PikaMetadataRotationPolicy
gui_query_rotation_policy (Pika        *pika,
                           PikaImage   *image,
                           PikaContext *context,
                           gboolean    *dont_ask)
{
  return metadata_rotation_import_dialog_run (image, context,
                                              NULL, dont_ask);
}

static void
gui_inhibit (Pika *pika)
{
  static gboolean  in_test      = TRUE;
  static gint      cookie       = 0;
  static gint      n_images     = 0;
  static gint64    last_failure = 0;
  GtkApplication  *app;
  PikaContainer   *images  = NULL;

  if (in_test)
    {
      /* Do not call inhibit code while unit-testing the UI code. */
      in_test = (g_getenv ("PIKA_TESTING_ABS_TOP_SRCDIR") != NULL);
      if (in_test)
        return;
    }

  app = GTK_APPLICATION (g_application_get_default ());

  if (pika_displays_dirty (pika))
    {
      gint n_dirty_images;

      images         = pika_displays_get_dirty_images (pika);
      n_dirty_images = pika_container_get_n_children (images);

      if (cookie && n_images == n_dirty_images)
        {
          g_object_unref (images);
          return;
        }

      n_images = n_dirty_images;
    }

  if (gtk_application_is_inhibited (app, GTK_APPLICATION_INHIBIT_LOGOUT))
    {
      gtk_application_uninhibit (app, cookie);
      cookie = 0;
    }

  if (last_failure != 0 && g_get_monotonic_time () - last_failure < G_TIME_SPAN_MINUTE * 10)
    {
      /* Don't repeatedly try to inhibit when we are constantly failing.
       * Especially as in some case, it may lock the thread.
       */
      g_clear_object (&images);
      return;
    }
  last_failure = 0;

  if (pika_displays_dirty (pika))
    {
      PikaImage *image;
      GList     *list;
      GtkWindow *window = NULL;
      gchar     *reason;

      image = (PikaImage *) pika_container_get_first_child (images);
      g_object_unref (images);

      for (list = pika_get_display_iter (pika); list; list = list->next)
        {
          PikaDisplay *display = list->data;

          if (pika_display_get_image (display) == image)
            {
              PikaDisplayShell *shell;
              GtkWidget        *toplevel;

              shell    = pika_display_get_shell (display);
              toplevel = gtk_widget_get_toplevel (GTK_WIDGET (shell));

              if (GTK_IS_WINDOW (toplevel))
                {
                  window = GTK_WINDOW (toplevel);
                  break;
                }
            }
        }

      /* TRANSLATORS: unless your language msgstr[0] applies to 1 only (as in
       * English), replace "one" with %d.
       */
      reason = g_strdup_printf (ngettext ("There is one image with unsaved changes!",
                                          "There are %d images with unsaved changes!",
                                          n_images),
                                n_images);
      cookie = gtk_application_inhibit (app, window,
                                        GTK_APPLICATION_INHIBIT_LOGOUT,
                                        reason);
      g_free (reason);

      if (cookie == 0)
        last_failure = g_get_monotonic_time ();
    }

  if (cookie == 0)
    n_images = 0;
}

static void
gui_image_disconnect (PikaImage *image,
                      Pika      *pika)
{
  gui_inhibit (pika);

  g_signal_handlers_disconnect_by_func (image,
                                        G_CALLBACK (gui_inhibit),
                                        pika);
  g_signal_handlers_disconnect_by_func (image,
                                        G_CALLBACK (gui_image_disconnect),
                                        pika);
}
