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

#include "eggfileformatchooser.h"

static void
setup_file_formats (EggFileFormatChooser *chooser)
{
  GdkPixbuf *ods, *xls, *html;
  gint icon_size;

  gtk_icon_size_lookup (GTK_ICON_SIZE_MENU, &icon_size, NULL);

  ods = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                  "gnome-mime-application-vnd."
                                  "oasis.opendocument.spreadsheet",
                                  icon_size, 0, NULL);
  xls = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                  "gnome-mime-application-vnd.ms-excel", 
                                  icon_size, 0, NULL);
  html = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                   "gnome-mime-text-html",
                                   icon_size, 0, NULL);

  egg_file_format_chooser_add_format (chooser, ods,
                                      "OpenDocument Spreadsheet", 
                                      ".ods", NULL);

  egg_file_format_chooser_add_format (chooser, xls,
                                      "Excel Spreadsheet", NULL);
  egg_file_format_chooser_add_sub_format (chooser, xls,
                                          "OpenXML Spreadsheet",
                                          ".xml", NULL);
  egg_file_format_chooser_add_sub_format (chooser, xls,
                                          "Excel 97 Spreadsheet",
                                          ".xls", NULL);

  egg_file_format_chooser_add_format (chooser, html,
                                      "HTML Document", NULL);
  egg_file_format_chooser_add_sub_format (chooser, html,
                                          "Full HTML Document",
                                          ".html", ".htm", NULL);
  egg_file_format_chooser_add_sub_format (chooser, html,
                                          "HTML Document Fragment",
                                          ".html", ".htm", NULL);
  g_object_unref (html);
  g_object_unref (xls);
  g_object_unref (ods);
}

static void
verify_format_guessing (EggFileFormatChooser *chooser)
{
  const struct {
    const gchar *filename;
    gint format, subformat;
  } tests[] = {
    { "test.ods", 0, -1 },
    { "test.xml", 1, 0 },
    { "test.xls", 1, 1 },
    { "test.htm", 2, 0 },
    { "test.html", 2, 0 },
    { "test.txt", -1, -1 },
    { "test", -1, -1 },
    { NULL, -1, -1 }
  };

  int i;

  for(i = 0; tests[i].filename; ++i)
    {
      gint format = -1;
      gint subformat = -1;

      format = egg_file_format_chooser_get_format (chooser, tests[i].filename, &subformat);

      g_print ("%s; format=%d; subformat=%d; %s\n", 
               tests[i].filename, format, subformat,
               subformat == tests[i].subformat &&
               format == tests[i].format ?
               "OK" : "FAIL");
    }
}

int
main (int argc, char *argv[]) 
{
  gint format = -1;
  gint subformat = -1;
  gchar *filename = "Dummy.ods";

  GtkWidget *file_chooser;
  GtkWidget *format_chooser;

  gtk_init (&argc, &argv);

  if (argc > 1)
    format = atoi (argv[1]);
  if (argc > 2)
    subformat = atoi (argv[2]);
  if (argc > 3)
    subformat = atoi (argv[3]);

  format_chooser = egg_file_format_chooser_new ();
  setup_file_formats (EGG_FILE_FORMAT_CHOOSER (format_chooser));
  verify_format_guessing (EGG_FILE_FORMAT_CHOOSER (format_chooser));

  egg_file_format_chooser_set_format (EGG_FILE_FORMAT_CHOOSER (format_chooser),
                                      format, subformat);

  file_chooser = gtk_file_chooser_dialog_new (
    "Testing EggFileFormatChooser", NULL,
    GTK_FILE_CHOOSER_ACTION_SAVE,
    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
    GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
    NULL);

  gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (file_chooser),
                                     filename);
  gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (file_chooser),
                                     format_chooser);


  if (GTK_RESPONSE_ACCEPT ==
      gtk_dialog_run (GTK_DIALOG (file_chooser)))
    filename = gtk_file_chooser_get_filename (
      GTK_FILE_CHOOSER (file_chooser));

  if (NULL != filename)
    {
      format = egg_file_format_chooser_get_format (
        EGG_FILE_FORMAT_CHOOSER (format_chooser),
        filename, &subformat);

      g_print ("%s; format: %d; subformat: %d\n", 
               filename, format, subformat);

      g_free (filename);
    }

  return 0;
}

/* vim: set sw=2 sta et: */
