#include "eggfileformat.h"

enum
{
  PROP_NAME = 1,
  PROP_EXTENSIONS
};

struct _EggFileFormatPrivate
{
  GValueArray *extensions;
  gchar *name;
};

G_DEFINE_TYPE (EggFileFormat, egg_file_format, G_TYPE_OBJECT);

static void
egg_file_format_init (EggFileFormat *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, EGG_TYPE_FILE_FORMAT, 
                                            EggFileFormatPrivate);
}

static void
egg_file_format_dispose (GObject *object)
{
  EggFileFormat *self = EGG_FILE_FORMAT (object);

  if (self)
    {
      if (self->priv->extensions)
        {
          g_value_array_free (self->priv->extensions);
          self->priv->extensions = NULL;
        }

      g_free (self->priv->name);
      self->priv->name = NULL;
    }

  if (G_OBJECT_CLASS (egg_file_format_parent_class)->dispose)
      G_OBJECT_CLASS (egg_file_format_parent_class)->dispose (object);
}

static void
egg_file_format_get_property (GObject *object, guint property,
                              GValue *value, GParamSpec *spec)
{
  EggFileFormat *self = EGG_FILE_FORMAT (object);

  switch (property) 
    {
      case PROP_NAME:
        g_value_set_string (value, self->priv->name);
        return;
      case PROP_EXTENSIONS:
        g_value_set_boxed (value, self->priv->extensions);
        return;
    }

  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property, spec);
}

static void
egg_file_format_set_property (GObject *object, guint property,
                              const GValue *value, GParamSpec *spec)
{
  EggFileFormat *self = EGG_FILE_FORMAT (object);

  switch (property) 
    {
      case PROP_NAME:
        egg_file_format_set_name (self, g_value_get_string (value));
        return;

      case PROP_EXTENSIONS:
        if (self->priv->extensions)
          g_value_array_free (self->priv->extensions);

        self->priv->extensions = g_value_dup_boxed (value);
        return;
    }

  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property, spec);
}

static void
egg_file_format_class_init (EggFileFormatClass *cls)
{
  GObjectClass *object_class;

  g_type_class_add_private (cls, sizeof (EggFileFormatPrivate));

  object_class = G_OBJECT_CLASS (cls);
  object_class->dispose = egg_file_format_dispose;
  object_class->get_property = egg_file_format_get_property;
  object_class->set_property = egg_file_format_set_property;

  g_object_class_install_property (
    object_class, PROP_NAME, g_param_spec_string ("name", NULL, NULL,
    NULL, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
  g_object_class_install_property (
    object_class, PROP_EXTENSIONS, g_param_spec_value_array ("extensions", NULL, NULL,
    g_param_spec_string ("extension", NULL, NULL, NULL, G_PARAM_READWRITE),
    G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
}

EggFileFormat*
egg_file_format_new (const gchar *name,
                     const gchar *first_extension,
                     ...)
{
  GValueArray *extension_list;
  const gchar *extension;

  GValue value;
  va_list args;

  g_value_init (&value, G_TYPE_STRING);
  extension_list = g_value_array_new (4);
  va_start (args, first_extension);

  for (extension = first_extension; extension;
       extension = va_arg (args, const gchar*)) {
    g_value_set_string (&value, extension);
    g_value_array_append (extension_list, &value);
  }

  va_end (args);

  return g_object_new (EGG_TYPE_FILE_FORMAT, "name", name,
                       "extensions", extension_list, NULL);
}

void
egg_file_format_set_name (EggFileFormat *self,
                          const gchar   *name)
{
  g_return_if_fail (EGG_IS_FILE_FORMAT (self));
  g_return_if_fail (NULL != name);

  if (name != self->priv->name) 
    {
      g_free (self->priv->name);
      self->priv->name = g_strdup (name);
    }
}
const gchar*
egg_file_format_get_name (EggFileFormat *self)
{
  g_return_val_if_fail (EGG_IS_FILE_FORMAT (self), NULL);
  return self->priv->name;
}

const GValueArray*
egg_file_format_get_extensions (EggFileFormat *self)
{
  g_return_val_if_fail (EGG_IS_FILE_FORMAT (self), NULL);
  return self->priv->extensions;
}

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