/*
 * SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com)
 * SPDX-License-Identifier: LGPL-2.0-or-later
 * SPDX-FileContributor: Michael Zucchi <notzed@ximian.com>
 */

#include "camel-mime-filter-index.h"
#include "camel-text-index.h"

struct _CamelMimeFilterIndexPrivate {
	CamelIndex *index;
	CamelIndexName *name;
};

G_DEFINE_TYPE_WITH_PRIVATE (CamelMimeFilterIndex, camel_mime_filter_index, CAMEL_TYPE_MIME_FILTER)

static void
mime_filter_index_dispose (GObject *object)
{
	CamelMimeFilterIndexPrivate *priv;

	priv = CAMEL_MIME_FILTER_INDEX (object)->priv;
	g_clear_object (&priv->name);
	g_clear_object (&priv->index);

	/* Chain up to parent's dispose() method. */
	G_OBJECT_CLASS (camel_mime_filter_index_parent_class)->dispose (object);
}

static void
mime_filter_index_filter (CamelMimeFilter *mime_filter,
                          const gchar *in,
                          gsize len,
                          gsize prespace,
                          gchar **out,
                          gsize *outlenptr,
                          gsize *outprespace)
{
	CamelMimeFilterIndexPrivate *priv;

	priv = CAMEL_MIME_FILTER_INDEX (mime_filter)->priv;

	if (priv->index == NULL || priv->name == NULL) {
		goto donothing;
	}

	camel_index_name_add_buffer (priv->name, in, len);

donothing:
	*out = (gchar *) in;
	*outlenptr = len;
	*outprespace = prespace;
}

static void
mime_filter_index_complete (CamelMimeFilter *mime_filter,
                            const gchar *in,
                            gsize len,
                            gsize prespace,
                            gchar **out,
                            gsize *outlenptr,
                            gsize *outprespace)
{
	CamelMimeFilterIndexPrivate *priv;

	priv = CAMEL_MIME_FILTER_INDEX (mime_filter)->priv;

	if (priv->index == NULL || priv->name == NULL) {
		goto donothing;
	}

	camel_index_name_add_buffer (priv->name, in, len);
	camel_index_name_add_buffer (priv->name, NULL, 0);

donothing:
	*out = (gchar *) in;
	*outlenptr = len;
	*outprespace = prespace;
}

static void
camel_mime_filter_index_class_init (CamelMimeFilterIndexClass *class)
{
	GObjectClass *object_class;
	CamelMimeFilterClass *mime_filter_class;

	object_class = G_OBJECT_CLASS (class);
	object_class->dispose = mime_filter_index_dispose;

	mime_filter_class = CAMEL_MIME_FILTER_CLASS (class);
	mime_filter_class->filter = mime_filter_index_filter;
	mime_filter_class->complete = mime_filter_index_complete;
}

static void
camel_mime_filter_index_init (CamelMimeFilterIndex *filter)
{
	filter->priv = camel_mime_filter_index_get_instance_private (filter);
}

/**
 * camel_mime_filter_index_new:
 * @index: a #CamelIndex object
 *
 * Create a new #CamelMimeFilterIndex based on @index.
 *
 * Returns: a new #CamelMimeFilterIndex object
 **/
CamelMimeFilter *
camel_mime_filter_index_new (CamelIndex *index)
{
	CamelMimeFilter *new;
	CamelMimeFilterIndexPrivate *priv;

	new = g_object_new (CAMEL_TYPE_MIME_FILTER_INDEX, NULL);

	priv = CAMEL_MIME_FILTER_INDEX (new)->priv;

	if (index != NULL)
		priv->index = g_object_ref (index);

	return new;
}

/* Set the match name for any indexed words */

/**
 * camel_mime_filter_index_set_name:
 * @filter: a #CamelMimeFilterIndex object
 * @name: a #CamelIndexName object
 *
 * Set the match name for any indexed words.
 **/
void
camel_mime_filter_index_set_name (CamelMimeFilterIndex *filter,
                                  CamelIndexName *name)
{
	g_return_if_fail (CAMEL_IS_MIME_FILTER_INDEX (filter));

	if (name != NULL) {
		g_return_if_fail (CAMEL_IS_INDEX_NAME (name));
		g_object_ref (name);
	}

	if (filter->priv->name != NULL)
		g_object_unref (filter->priv->name);

	filter->priv->name = name;
}

/**
 * camel_mime_filter_index_set_index:
 * @filter: a #CamelMimeFilterIndex object
 * @index: a #CamelIndex object
 *
 * Set @index on @filter.
 **/
void
camel_mime_filter_index_set_index (CamelMimeFilterIndex *filter,
                                   CamelIndex *index)
{
	g_return_if_fail (CAMEL_IS_MIME_FILTER_INDEX (filter));

	if (index != NULL) {
		g_return_if_fail (CAMEL_IS_INDEX (index));
		g_object_ref (index);
	}

	if (filter->priv->index) {
		gchar *out;
		gsize outlen, outspace;

		camel_mime_filter_complete (
			CAMEL_MIME_FILTER (filter),
			"", 0, 0, &out, &outlen, &outspace);
		g_object_unref (filter->priv->index);
	}

	filter->priv->index = index;
}
