/* gtkrichtext.c
 *
 * Copyright (C) 2006 Imendio AB
 * Contact: Michael Natterer <mitch@imendio.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>

#include "gtkrichtext.h"
#include "gtktextbufferserialize.h"
#include "gtkalias.h"
#include "gtkintl.h"


typedef struct
{
  gchar          *mime_type;
  gchar          *tagset_name;
  gboolean        can_create_tags;
  GdkAtom         atom;
  gpointer        function;
  gpointer        user_data;
  GDestroyNotify  user_data_destroy;
} GtkRichTextFormat;


static GList   * register_format   (GList             *formats,
                                    const gchar       *mime_type,
                                    const gchar       *tagset_name,
                                    gpointer           function,
                                    gpointer           user_data,
                                    GDestroyNotify     user_data_destroy,
                                    GdkAtom           *atom);
static GList   * unregister_format (GList             *formats,
                                    GdkAtom            atom);
static GdkAtom * get_formats       (GList             *formats,
                                    gint              *n_formats);
static void      free_format       (GtkRichTextFormat *format);
static void      free_format_list  (GList             *formats);
static GQuark    serialize_quark   (void);
static GQuark    deserialize_quark (void);


/*  public functions  */

GdkAtom
gtk_rich_text_register_serialize_format (GtkTextBuffer            *buffer,
                                         const gchar              *mime_type,
                                         const gchar              *tagset_name,
                                         GtkRichTextSerializeFunc  function,
                                         gpointer                  user_data,
                                         GDestroyNotify            user_data_destroy)
{
  GList   *formats;
  GdkAtom  atom;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), GDK_NONE);
  g_return_val_if_fail ((mime_type == NULL && function == NULL) ||
                        (mime_type != NULL && function == NULL), GDK_NONE);

  if (! function)
    function = _gtk_text_buffer_serialize_rich_text;

  formats = g_object_steal_qdata (G_OBJECT (buffer), serialize_quark ());

  formats = register_format (formats, mime_type, tagset_name,
                             (gpointer) function,
                             user_data, user_data_destroy,
                             &atom);

  g_object_set_qdata_full (G_OBJECT (buffer), serialize_quark (),
                           formats, (GDestroyNotify) free_format_list);

  return atom;
}

GdkAtom
gtk_rich_text_register_deserialize_format (GtkTextBuffer              *buffer,
                                           const gchar                *mime_type,
                                           const gchar                *tagset_name,
                                           GtkRichTextDeserializeFunc  function,
                                           gpointer                    user_data,
                                           GDestroyNotify              user_data_destroy)
{
  GList   *formats;
  GdkAtom  atom;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), GDK_NONE);
  g_return_val_if_fail ((mime_type == NULL && function == NULL) ||
                        (mime_type != NULL && function == NULL), GDK_NONE);

  if (! function)
    function = _gtk_text_buffer_deserialize_rich_text;

  formats = g_object_steal_qdata (G_OBJECT (buffer), deserialize_quark ());

  formats = register_format (formats, mime_type, tagset_name,
                             (gpointer) function,
                             user_data, user_data_destroy,
                             &atom);

  g_object_set_qdata_full (G_OBJECT (buffer), deserialize_quark (),
                           formats, (GDestroyNotify) free_format_list);

  return atom;
}

void
gtk_rich_text_unregister_serialize_format (GtkTextBuffer *buffer,
                                           GdkAtom        format)
{
  GList *formats;

  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (format != GDK_NONE);

  formats = g_object_steal_qdata (G_OBJECT (buffer), serialize_quark ());

  formats = unregister_format (formats, format);

  g_object_set_qdata_full (G_OBJECT (buffer), serialize_quark (),
                           formats, (GDestroyNotify) free_format_list);
}

void
gtk_rich_text_unregister_deserialize_format (GtkTextBuffer *buffer,
                                             GdkAtom        format)
{
  GList *formats;

  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (format != GDK_NONE);

  formats = g_object_steal_qdata (G_OBJECT (buffer), deserialize_quark ());

  formats = unregister_format (formats, format);

  g_object_set_qdata_full (G_OBJECT (buffer), deserialize_quark (),
                           formats, (GDestroyNotify) free_format_list);
}

void
gtk_rich_text_deserialize_set_can_create_tags (GtkTextBuffer *buffer,
                                               GdkAtom        format,
                                               gboolean       can_create_tags)
{
  GList *formats;
  GList *list;
  gchar *format_name;

  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
  g_return_if_fail (format != GDK_NONE);

  formats = g_object_get_qdata (G_OBJECT (buffer), deserialize_quark ());

  for (list = formats; list; list = g_list_next (list))
    {
      GtkRichTextFormat *fmt = list->data;

      if (fmt->atom == format)
        {
          fmt->can_create_tags = can_create_tags ? TRUE : FALSE;
          return;
        }
    }

  format_name = gdk_atom_name (format);
  g_warning ("%s: \"%s\" is not registered as deserializable format "
             "with text buffer %p",
             G_STRFUNC, format_name ? format_name : "not a GdkAtom", buffer);
  g_free (format_name);
}

gboolean
gtk_rich_text_deserialize_get_can_create_tags (GtkTextBuffer *buffer,
                                               GdkAtom        format)
{
  GList *formats;
  GList *list;
  gchar *format_name;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
  g_return_val_if_fail (format != GDK_NONE, FALSE);

  formats = g_object_get_qdata (G_OBJECT (buffer), deserialize_quark ());

  for (list = formats; list; list = g_list_next (list))
    {
      GtkRichTextFormat *fmt = list->data;

      if (fmt->atom == format)
        {
          return fmt->can_create_tags;
        }
    }

  format_name = gdk_atom_name (format);
  g_warning ("%s: \"%s\" is not registered as deserializable format "
             "with text buffer %p",
             G_STRFUNC, format_name ? format_name : "not a GdkAtom", buffer);
  g_free (format_name);

  return FALSE;
}

GdkAtom *
gtk_rich_text_get_serialize_formats (GtkTextBuffer *buffer,
                                     gint          *n_formats)
{
  GList *formats;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (n_formats != NULL, NULL);

  formats = g_object_get_qdata (G_OBJECT (buffer), serialize_quark ());

  return get_formats (formats, n_formats);
}

GdkAtom *
gtk_rich_text_get_deserialize_formats (GtkTextBuffer *buffer,
                                       gint          *n_formats)
{
  GList *formats;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (n_formats != NULL, NULL);

  formats = g_object_get_qdata (G_OBJECT (buffer), deserialize_quark ());

  return get_formats (formats, n_formats);
}

guint8 *
gtk_rich_text_serialize (GtkTextBuffer     *buffer,
                         GdkAtom            format,
                         const GtkTextIter *start,
                         const GtkTextIter *end,
                         gsize             *length)
{
  GList *formats;
  GList *list;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
  g_return_val_if_fail (format != GDK_NONE, NULL);
  g_return_val_if_fail (start != NULL, NULL);
  g_return_val_if_fail (end != NULL, NULL);
  g_return_val_if_fail (length != NULL, NULL);

  *length = 0;

  formats = g_object_get_qdata (G_OBJECT (buffer), serialize_quark ());

  for (list = formats; list; list = g_list_next (list))
    {
      GtkRichTextFormat *fmt = list->data;

      if (fmt->atom == format)
        {
          GtkRichTextSerializeFunc function = fmt->function;

          return function (buffer, start, end, length, fmt->user_data);
        }
    }

  return NULL;
}

gboolean
gtk_rich_text_deserialize (GtkTextBuffer  *buffer,
                           GdkAtom         format,
                           GtkTextIter    *iter,
                           const guint8   *data,
                           gsize           length,
                           GError        **error)
{
  GList    *formats;
  GList    *list;
  gboolean  retval = FALSE;

  g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
  g_return_val_if_fail (format != GDK_NONE, FALSE);
  g_return_val_if_fail (iter != NULL, FALSE);
  g_return_val_if_fail (data != NULL, FALSE);
  g_return_val_if_fail (length > 0, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  formats = g_object_get_qdata (G_OBJECT (buffer), deserialize_quark ());

  for (list = formats; list; list = g_list_next (list))
    {
      GtkRichTextFormat *fmt = list->data;

      if (fmt->atom == format)
        {
          GtkRichTextDeserializeFunc function = fmt->function;

          retval = function (buffer, iter, data, length,
                             fmt->can_create_tags,
                             fmt->user_data,
                             error);

          if (!retval && error != NULL && *error == NULL)
            g_set_error (error, 0, 0,
                         _("Unknown error when trying to deserialize %s"),
                         gdk_atom_name (format));

          break;
        }
    }

  if (!retval && error != NULL && *error == NULL)
    g_set_error (error, 0, 0,
                 _("No seserialize function found for format %s"),
                 gdk_atom_name (format));

  return retval;
}


/*  private functions  */

static GList *
register_format (GList          *formats,
                 const gchar    *mime_type,
                 const gchar    *tagset_name,
                 gpointer        function,
                 gpointer        user_data,
                 GDestroyNotify  user_data_destroy,
                 GdkAtom        *atom)
{
  GtkRichTextFormat *format;

  if (! mime_type)
    mime_type = "application/x-gtk-text-buffer-rich-text";

  if (tagset_name)
    {
      gchar *tmp = g_strdup_printf ("%s;format=%s", mime_type, tagset_name);
      *atom = gdk_atom_intern (tmp, FALSE);
      g_free (tmp);
    }
  else
    {
      *atom = gdk_atom_intern (mime_type, FALSE);
    }

  formats = unregister_format (formats, *atom);

  format = g_new0 (GtkRichTextFormat, 1);

  format->mime_type         = g_strdup (mime_type);
  format->tagset_name       = g_strdup (tagset_name);
  format->can_create_tags   = FALSE;
  format->atom              = *atom;
  format->function          = function;
  format->user_data         = user_data;
  format->user_data_destroy = user_data_destroy;

  return g_list_append (formats, format);
}

static GList *
unregister_format (GList   *formats,
                   GdkAtom  atom)
{
  GList *list;

  for (list = formats; list; list = g_list_next (list))
    {
      GtkRichTextFormat *format = list->data;

      if (format->atom == atom)
        {
          free_format (format);

          return g_list_delete_link (formats, list);
        }
    }

  return formats;
}

static GdkAtom *
get_formats (GList *formats,
             gint  *n_formats)
{
  GdkAtom *array;
  GList   *list;
  gint     i;

  *n_formats = g_list_length (formats);
  array = g_new0 (GdkAtom, *n_formats);

  for (list = formats, i = 0; list; list = g_list_next (list), i++)
    {
      GtkRichTextFormat *format = list->data;

      array[i] = format->atom;
    }

  return array;
}

static void
free_format (GtkRichTextFormat *format)
{
  if (format->user_data_destroy)
    format->user_data_destroy (format->user_data);

  g_free (format->mime_type);
  g_free (format->tagset_name);
  g_free (format);
}

static void
free_format_list (GList *formats)
{
  g_list_foreach (formats, (GFunc) free_format, NULL);
  g_list_free (formats);
}

static GQuark
serialize_quark (void)
{
  static GQuark quark = 0;

  if (! quark)
    quark = g_quark_from_static_string ("gtk-text-buffer-serialize-formats");

  return quark;
}

static GQuark
deserialize_quark (void)
{
  static GQuark quark = 0;

  if (! quark)
    quark = g_quark_from_static_string ("gtk-text-buffer-deserialize-formats");

  return quark;
}

#define __GTK_RICH_TEXT_C__
#include "gtkaliasdef.c"
