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

#include "eggfileformatchooser.h"

#define ICON_NAME_ODS   "gnome-mime-application-vnd.oasis.opendocument.spreadsheet"
#define ICON_NAME_EXCEL "gnome-mime-application-vnd.ms-excel"
#define ICON_NAME_HTML  "gnome-mime-text-html"

static void
setup_file_formats (EggFileFormatChooser *chooser)
{
  guint dummy, ods, excel, html;

  dummy = egg_file_format_chooser_add_format (
    chooser, 0, "Dummy Format (shall be removed)",
    NULL, NULL);

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

  excel = egg_file_format_chooser_add_format (
    chooser, 0, "Excel Spreadsheet",
    ICON_NAME_EXCEL, NULL);

  egg_file_format_chooser_add_format (
      chooser, excel, "Excel 2003 Spreadsheet (OpenXML)",
      ICON_NAME_EXCEL, ".xml", NULL);
  egg_file_format_chooser_add_format (
      chooser, excel, "Excel 97 Spreadsheet",
      ICON_NAME_EXCEL, ".xls", NULL);

  html = egg_file_format_chooser_add_format (
    chooser, 0, "HTML Document",
    ICON_NAME_HTML, NULL);

  egg_file_format_chooser_add_format (
      chooser, html, "Full HTML Document",
      ICON_NAME_EXCEL, ".html", ".htm", NULL);
  egg_file_format_chooser_add_format (
      chooser, html, "HTML Document Fragment",
      ICON_NAME_EXCEL, ".html", ".htm", NULL);

  egg_file_format_chooser_remove_format (chooser, dummy);

  g_assert (2 == ods);
  g_assert (3 == excel);
  g_assert (6 == html);
}

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

  int i;

  for(i = 0; tests[i].filename; ++i)
    {
      guint format =
        egg_file_format_chooser_get_format (chooser, tests[i].filename);

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

static void
selection_changed (EggFileFormatChooser *chooser)
{
  guint format = egg_file_format_chooser_get_format (chooser, NULL);
  g_print ("selected format: %d\n", format);
}

int
main (int argc, char *argv[]) 
{
  gchar *filename = "Dummy.ods";
  guint format = 0;

  GtkWidget *file_chooser;
  GtkWidget *format_chooser;

  gtk_init (&argc, &argv);

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

  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));

  g_signal_connect (format_chooser, "selection-changed",
                    G_CALLBACK (selection_changed), NULL);

  egg_file_format_chooser_set_format (EGG_FILE_FORMAT_CHOOSER (format_chooser),
                                      format);

  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));
  else
    filename = NULL;

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

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

      g_free (filename);
    }

  return 0;
}

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