#include "eggfileformatchooser.h"
#include "eggfileformat.h"

#include <glib/gi18n.h>
#include <gtk/gtk.h>
enum 
{
  MODEL_COLUMN_NAME,
  MODEL_COLUMN_EXTENSIONS,
  MODEL_COLUMN_FORMAT
};

struct _EggFileFormatChooserPrivate
{
  GtkListStore *model;
  GtkTreeSelection *selection;
};

G_DEFINE_TYPE (EggFileFormatChooser, 
	       egg_file_format_chooser,
               GTK_TYPE_EXPANDER);

static void
selection_changed (GtkTreeSelection     *selection,
                   EggFileFormatChooser *self)
{
  gchar *label;
  gchar *name;

  GtkTreeModel *model;
  GtkTreeIter iter;

  if (gtk_tree_selection_get_selected (selection, &model, &iter)) 
    {
      gtk_tree_model_get (model, &iter, 
                          MODEL_COLUMN_NAME, &name,
                          -1);

      label = g_strdup_printf (_("File Format: %s"), name);
      gtk_expander_set_label (GTK_EXPANDER (self), label);

      g_free (name);
      g_free (label);
    }
}

/* XXX This hack is needed, as gtk_expander_set_label seems 
 * not to work from egg_file_format_chooser_init */
static gboolean
select_default_file_format (gpointer data)
{
  EggFileFormatChooser *self = EGG_FILE_FORMAT_CHOOSER (data);
  egg_file_format_chooser_set_format (self, NULL);
  return FALSE;
}

static void
egg_file_format_chooser_init (EggFileFormatChooser *self)
{
  GtkWidget *scroller;
  GtkWidget *view;

  GtkTreeViewColumn *column;
  GtkTreeIter iter;

  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EGG_TYPE_FILE_FORMAT_CHOOSER, 
                                            EggFileFormatChooserPrivate);
  self->priv->model = gtk_list_store_new (3, G_TYPE_STRING, G_TYPE_STRING,
                                             EGG_TYPE_FILE_FORMAT);

  gtk_list_store_append (self->priv->model, &iter);  
  gtk_list_store_set (self->priv->model, &iter,
                      MODEL_COLUMN_NAME, _("By Extension"),
                      -1);

  view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (self->priv->model));
  self->priv->selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
  gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (view), TRUE);

  column = gtk_tree_view_column_new_with_attributes (
    _("File Format"), gtk_cell_renderer_text_new (),
    "text", 0, NULL);
  gtk_tree_view_column_set_expand (column, TRUE);
  gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

  column = gtk_tree_view_column_new_with_attributes (
    _("Extension(s)"), gtk_cell_renderer_text_new (),
    "text", 1, NULL);
  gtk_tree_view_column_set_expand (column, FALSE);
  gtk_tree_view_append_column (GTK_TREE_VIEW (view), column);

  gtk_tree_selection_set_mode (self->priv->selection, GTK_SELECTION_BROWSE);
  g_signal_connect (self->priv->selection, "changed", G_CALLBACK (selection_changed), self);
  g_idle_add (select_default_file_format, self);

  scroller = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroller),
                                  GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scroller),
                                       GTK_SHADOW_IN);
  gtk_widget_set_size_request (scroller, -1, 100);
  gtk_container_add (GTK_CONTAINER (scroller), view);
  gtk_widget_show_all (scroller);

  gtk_container_add (GTK_CONTAINER (self), scroller);
}

static void
egg_file_format_chooser_class_init (EggFileFormatChooserClass *cls)
{
  g_type_class_add_private (cls, sizeof (EggFileFormatChooserPrivate));
}

GtkWidget*
egg_file_format_chooser_new (void)
{
  return g_object_new (EGG_TYPE_FILE_FORMAT_CHOOSER, NULL);
}

void
egg_file_format_chooser_add_format (EggFileFormatChooser *self,
                                    EggFileFormat        *format)
{
  const GValueArray *extension_list;
  GString *extensions = NULL;
  GtkTreeIter iter;
  int i;

  g_return_if_fail (EGG_IS_FILE_FORMAT_CHOOSER (self));
  g_return_if_fail (EGG_IS_FILE_FORMAT (format));

  extension_list = egg_file_format_get_extensions (format);

  if (extension_list && extension_list->n_values > 0)
    {
      extensions = g_string_new (NULL);
      g_string_append (extensions, g_value_get_string (
                       &extension_list->values[0]));

      for (i = 1; i < extension_list->n_values; ++i) 
        {
          g_string_append (extensions, ", ");
          g_string_append (extensions, g_value_get_string (
                           &extension_list->values[i]));
        }
    }

  gtk_list_store_append (self->priv->model, &iter);  
  gtk_list_store_set (self->priv->model, &iter,
                      MODEL_COLUMN_NAME, egg_file_format_get_name (format),
                      MODEL_COLUMN_EXTENSIONS, extensions ? extensions->str : NULL,
                      MODEL_COLUMN_FORMAT, format,
                      -1);
}

void            
egg_file_format_chooser_set_format (EggFileFormatChooser *self,
                                    EggFileFormat        *format)
{
  GtkTreeModel *model;
  EggFileFormat *tmp;
  GtkTreeIter iter;

  g_return_if_fail (EGG_IS_FILE_FORMAT_CHOOSER (self));
  g_return_if_fail (NULL == format || EGG_IS_FILE_FORMAT (format));

  model = GTK_TREE_MODEL (self->priv->model);
  gtk_tree_selection_unselect_all (self->priv->selection);

  if (gtk_tree_model_iter_nth_child (model, &iter, NULL, 0))
    {
      do
        {
          gtk_tree_model_get (model, &iter, MODEL_COLUMN_FORMAT, &tmp, -1);

          if (tmp)
            g_object_unref (tmp);

          if (tmp == format)
            {
              gtk_tree_selection_select_iter (self->priv->selection, &iter);
              break;
            }
        }
      while (gtk_tree_model_iter_next (model, &iter));
    }
}

EggFileFormat*
egg_file_format_chooser_get_format (EggFileFormatChooser *self)
{
  EggFileFormat *format = NULL;
  GtkTreeModel *model;
  GtkTreeIter iter;

  g_return_val_if_fail (EGG_IS_FILE_FORMAT_CHOOSER (self), NULL);

  if (gtk_tree_selection_get_selected (self->priv->selection, &model, &iter))
    gtk_tree_model_get (model, &iter, MODEL_COLUMN_FORMAT, &format, -1);

  if (format)
    g_object_unref (format);

  return format;
}

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