#include <gtk/gtk.h>
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-desktop-thumbnail.h>

static void
update_preview (GtkFileChooser *chooser,
                GnomeDesktopThumbnailFactory *thumb_factory)
{
  gchar *uri;

  uri = gtk_file_chooser_get_uri (chooser);
  if (uri) {
    const gchar *mime_type = NULL;
    GdkPixbuf   *pixbuf = NULL;
    GtkWidget   *preview;
    GFileInfo   *file_info;
    GFile       *file;

    preview = gtk_file_chooser_get_preview_widget (chooser);

    file = g_file_new_for_uri (uri);
    file_info = g_file_query_info (file,
                                   "standard::*",
                                   G_FILE_QUERY_INFO_NONE,
                                   NULL, NULL);

    g_object_unref (file);

    if (file_info != NULL &&
        g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY) {
      mime_type = g_file_info_get_content_type (file_info);

      g_object_unref (file_info);
    }

    if (mime_type != NULL) {
      pixbuf = gnome_desktop_thumbnail_factory_generate_thumbnail (thumb_factory, uri, mime_type);
    }

    /* https://bugzilla.gnome.org/show_bug.cgi?id=778451 */
    gtk_dialog_set_response_sensitive (GTK_DIALOG (chooser),
                                       GTK_RESPONSE_ACCEPT,
                                       (pixbuf != NULL));

    if (pixbuf != NULL) {
      gtk_image_set_from_pixbuf (GTK_IMAGE (preview), pixbuf);

      g_object_unref (pixbuf);
    }
  }

  gtk_file_chooser_set_preview_widget_active (chooser, TRUE);
}

static void
activate (GtkApplication* app,
          gpointer        user_data)
{
  GnomeDesktopThumbnailFactory *thumb_factory = (GnomeDesktopThumbnailFactory *)user_data;
  GtkWidget                    *dialog;
  GtkWidget                    *preview;

  dialog = gtk_file_chooser_dialog_new ("Open File",
                                        NULL,
                                        GTK_FILE_CHOOSER_ACTION_OPEN,
                                        "_Cancel",
                                        GTK_RESPONSE_CANCEL,
                                        "_Open",
                                        GTK_RESPONSE_ACCEPT,
                                        NULL);

  preview = gtk_image_new ();
  gtk_widget_set_size_request (preview, 128, -1);
  gtk_file_chooser_set_preview_widget (GTK_FILE_CHOOSER (dialog), preview);
  gtk_file_chooser_set_use_preview_label (GTK_FILE_CHOOSER (dialog), FALSE);
  gtk_widget_show (preview);

  g_signal_connect_after (dialog,
                          "selection-changed",
                          G_CALLBACK (update_preview),
                          thumb_factory);

  gtk_dialog_run (GTK_DIALOG (dialog));  
}

int
main (int    argc,
      char **argv)
{
  GnomeDesktopThumbnailFactory *thumb_factory;
  GtkApplication               *app;
  int                           status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  thumb_factory = gnome_desktop_thumbnail_factory_new (GNOME_DESKTOP_THUMBNAIL_SIZE_NORMAL);

  g_signal_connect (app, "activate", G_CALLBACK (activate), thumb_factory);

  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}
