/* rfc822-gmime-filter-plain.c generated by valac 0.56.3, the Vala compiler
 * generated from rfc822-gmime-filter-plain.vala, do not modify */

/* Copyright 2016 Software Freedom Conservancy Inc.
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later).  See the COPYING file in this distribution.
*/
/* Filter to mark quoted text in plain (non-flowed) text*/

#include <gmime/gmime.h>
#include <glib-object.h>
#include <glib.h>
#include "geary-engine.h"

#define GEARY_RF_C822_TYPE_FILTER_PLAIN (geary_rf_c822_filter_plain_get_type ())
#define GEARY_RF_C822_FILTER_PLAIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlain))
#define GEARY_RF_C822_FILTER_PLAIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlainClass))
#define GEARY_RF_C822_IS_FILTER_PLAIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEARY_RF_C822_TYPE_FILTER_PLAIN))
#define GEARY_RF_C822_IS_FILTER_PLAIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEARY_RF_C822_TYPE_FILTER_PLAIN))
#define GEARY_RF_C822_FILTER_PLAIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlainClass))

typedef struct _GearyRFC822FilterPlain GearyRFC822FilterPlain;
typedef struct _GearyRFC822FilterPlainClass GearyRFC822FilterPlainClass;
typedef struct _GearyRFC822FilterPlainPrivate GearyRFC822FilterPlainPrivate;
enum  {
	GEARY_RF_C822_FILTER_PLAIN_0_PROPERTY,
	GEARY_RF_C822_FILTER_PLAIN_NUM_PROPERTIES
};
static GParamSpec* geary_rf_c822_filter_plain_properties[GEARY_RF_C822_FILTER_PLAIN_NUM_PROPERTIES];

struct _GearyRFC822FilterPlain {
	GMimeFilter parent_instance;
	GearyRFC822FilterPlainPrivate * priv;
};

struct _GearyRFC822FilterPlainClass {
	GMimeFilterClass parent_class;
};

struct _GearyRFC822FilterPlainPrivate {
	gboolean in_prefix;
};

static gint GearyRFC822FilterPlain_private_offset;
static gpointer geary_rf_c822_filter_plain_parent_class = NULL;

VALA_EXTERN GType geary_rf_c822_filter_plain_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GearyRFC822FilterPlain* geary_rf_c822_filter_plain_new (void);
VALA_EXTERN GearyRFC822FilterPlain* geary_rf_c822_filter_plain_construct (GType object_type);
static void geary_rf_c822_filter_plain_real_reset (GMimeFilter* base);
static GMimeFilter* geary_rf_c822_filter_plain_real_copy (GMimeFilter* base);
static void geary_rf_c822_filter_plain_real_filter (GMimeFilter* base,
                                             guint8* inbuf,
                                             gsize inbuf_length1,
                                             gsize prespace,
                                             guint8** processed_buffer,
                                             gsize* processed_buffer_length1,
                                             gsize* outprespace);
static void geary_rf_c822_filter_plain_real_complete (GMimeFilter* base,
                                               guint8* inbuf,
                                               gsize inbuf_length1,
                                               gsize prespace,
                                               guint8** processed_buffer,
                                               gsize* processed_buffer_length1,
                                               gsize* outprespace);
static void geary_rf_c822_filter_plain_finalize (GObject * obj);
static GType geary_rf_c822_filter_plain_get_type_once (void);

static inline gpointer
geary_rf_c822_filter_plain_get_instance_private (GearyRFC822FilterPlain* self)
{
	return G_STRUCT_MEMBER_P (self, GearyRFC822FilterPlain_private_offset);
}

GearyRFC822FilterPlain*
geary_rf_c822_filter_plain_construct (GType object_type)
{
	GearyRFC822FilterPlain * self = NULL;
	self = (GearyRFC822FilterPlain*) g_object_new (object_type, NULL);
	g_mime_filter_reset (G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter));
	return self;
}

GearyRFC822FilterPlain*
geary_rf_c822_filter_plain_new (void)
{
	return geary_rf_c822_filter_plain_construct (GEARY_RF_C822_TYPE_FILTER_PLAIN);
}

static void
geary_rf_c822_filter_plain_real_reset (GMimeFilter* base)
{
	GearyRFC822FilterPlain * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlain);
	self->priv->in_prefix = TRUE;
}

static GMimeFilter*
geary_rf_c822_filter_plain_real_copy (GMimeFilter* base)
{
	GearyRFC822FilterPlain * self;
	GearyRFC822FilterPlain* new_filter = NULL;
	GearyRFC822FilterPlain* _tmp0_;
	GMimeFilter* result;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlain);
	_tmp0_ = geary_rf_c822_filter_plain_new ();
	new_filter = _tmp0_;
	new_filter->priv->in_prefix = self->priv->in_prefix;
	result = G_TYPE_CHECK_INSTANCE_CAST (new_filter, g_mime_filter_get_type (), GMimeFilter);
	return result;
}

static void
geary_rf_c822_filter_plain_real_filter (GMimeFilter* base,
                                        guint8* inbuf,
                                        gsize inbuf_length1,
                                        gsize prespace,
                                        guint8** processed_buffer,
                                        gsize* processed_buffer_length1,
                                        gsize* outprespace)
{
	GearyRFC822FilterPlain * self;
	guint8* _vala_processed_buffer = NULL;
	gsize _vala_processed_buffer_length1 = 0;
	gsize _vala_outprespace = 0UL;
	guint out_index = 0U;
	guint8* _tmp8_;
	gint _tmp8__length1;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlain);
	g_mime_filter_set_size (G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter), (gsize) inbuf_length1, FALSE);
	out_index = (guint) 0;
	{
		guint i = 0U;
		i = (guint) 0;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				guint8 c = 0U;
				guint8 _tmp2_;
				guint8* _tmp5_;
				gint _tmp5__length1;
				guint _tmp6_;
				guint8 _tmp7_;
				if (!_tmp0_) {
					guint _tmp1_;
					_tmp1_ = i;
					i = _tmp1_ + 1;
				}
				_tmp0_ = FALSE;
				if (!(i < ((guint) inbuf_length1))) {
					break;
				}
				_tmp2_ = inbuf[i];
				c = _tmp2_;
				if (self->priv->in_prefix) {
					if (c == ((guint8) '>')) {
						guint8* _tmp3_;
						gint _tmp3__length1;
						guint _tmp4_;
						_tmp3_ = G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter)->outbuf;
						_tmp3__length1 = G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter)->outsize;
						_tmp4_ = out_index;
						out_index = _tmp4_ + 1;
						_tmp3_[_tmp4_] = (guint8) GEARY_RF_C822_UTILS_QUOTE_MARKER;
						continue;
					}
					self->priv->in_prefix = FALSE;
				}
				if (c == ((guint8) '\n')) {
					self->priv->in_prefix = TRUE;
				}
				_tmp5_ = G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter)->outbuf;
				_tmp5__length1 = G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter)->outsize;
				_tmp6_ = out_index;
				out_index = _tmp6_ + 1;
				_tmp7_ = c;
				_tmp5_[_tmp6_] = _tmp7_;
			}
		}
	}
	_tmp8_ = G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter)->outbuf;
	_tmp8__length1 = G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter)->outsize;
	_vala_processed_buffer = _tmp8_ + 0;
	_vala_processed_buffer_length1 = ((gint) out_index) - 0;
	_vala_outprespace = G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter)->outpre;
	if (processed_buffer) {
		*processed_buffer = _vala_processed_buffer;
	}
	if (processed_buffer_length1) {
		*processed_buffer_length1 = _vala_processed_buffer_length1;
	}
	if (outprespace) {
		*outprespace = _vala_outprespace;
	}
}

static void
geary_rf_c822_filter_plain_real_complete (GMimeFilter* base,
                                          guint8* inbuf,
                                          gsize inbuf_length1,
                                          gsize prespace,
                                          guint8** processed_buffer,
                                          gsize* processed_buffer_length1,
                                          gsize* outprespace)
{
	GearyRFC822FilterPlain * self;
	guint8* _vala_processed_buffer = NULL;
	gsize _vala_processed_buffer_length1 = 0;
	gsize _vala_outprespace = 0UL;
	guint8* _tmp0_ = NULL;
	gsize _tmp1_ = 0;
	gsize _tmp2_ = 0UL;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlain);
	g_mime_filter_filter (G_TYPE_CHECK_INSTANCE_CAST (self, g_mime_filter_get_type (), GMimeFilter), inbuf, (gsize) inbuf_length1, prespace, &_tmp0_, &_tmp1_, &_tmp2_);
	_vala_processed_buffer = _tmp0_;
	_vala_processed_buffer_length1 = _tmp1_;
	_vala_outprespace = _tmp2_;
	if (processed_buffer) {
		*processed_buffer = _vala_processed_buffer;
	}
	if (processed_buffer_length1) {
		*processed_buffer_length1 = _vala_processed_buffer_length1;
	}
	if (outprespace) {
		*outprespace = _vala_outprespace;
	}
}

static void
geary_rf_c822_filter_plain_class_init (GearyRFC822FilterPlainClass * klass,
                                       gpointer klass_data)
{
	geary_rf_c822_filter_plain_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GearyRFC822FilterPlain_private_offset);
	((GMimeFilterClass *) klass)->reset = (void (*) (GMimeFilter*)) geary_rf_c822_filter_plain_real_reset;
	((GMimeFilterClass *) klass)->copy = (GMimeFilter* (*) (GMimeFilter*)) geary_rf_c822_filter_plain_real_copy;
	((GMimeFilterClass *) klass)->filter = (void (*) (GMimeFilter*, guint8*, gsize, gsize, guint8**, gsize*, gsize*)) geary_rf_c822_filter_plain_real_filter;
	((GMimeFilterClass *) klass)->complete = (void (*) (GMimeFilter*, guint8*, gsize, gsize, guint8**, gsize*, gsize*)) geary_rf_c822_filter_plain_real_complete;
	G_OBJECT_CLASS (klass)->finalize = geary_rf_c822_filter_plain_finalize;
}

static void
geary_rf_c822_filter_plain_instance_init (GearyRFC822FilterPlain * self,
                                          gpointer klass)
{
	self->priv = geary_rf_c822_filter_plain_get_instance_private (self);
}

static void
geary_rf_c822_filter_plain_finalize (GObject * obj)
{
	GearyRFC822FilterPlain * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GEARY_RF_C822_TYPE_FILTER_PLAIN, GearyRFC822FilterPlain);
	G_OBJECT_CLASS (geary_rf_c822_filter_plain_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
geary_rf_c822_filter_plain_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GearyRFC822FilterPlainClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) geary_rf_c822_filter_plain_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GearyRFC822FilterPlain), 0, (GInstanceInitFunc) geary_rf_c822_filter_plain_instance_init, NULL };
	GType geary_rf_c822_filter_plain_type_id;
	geary_rf_c822_filter_plain_type_id = g_type_register_static (g_mime_filter_get_type (), "GearyRFC822FilterPlain", &g_define_type_info, 0);
	GearyRFC822FilterPlain_private_offset = g_type_add_instance_private (geary_rf_c822_filter_plain_type_id, sizeof (GearyRFC822FilterPlainPrivate));
	return geary_rf_c822_filter_plain_type_id;
}

GType
geary_rf_c822_filter_plain_get_type (void)
{
	static gsize geary_rf_c822_filter_plain_type_id__once = 0;
	if (g_once_init_enter (&geary_rf_c822_filter_plain_type_id__once)) {
		GType geary_rf_c822_filter_plain_type_id;
		geary_rf_c822_filter_plain_type_id = geary_rf_c822_filter_plain_get_type_once ();
		g_once_init_leave (&geary_rf_c822_filter_plain_type_id__once, geary_rf_c822_filter_plain_type_id);
	}
	return geary_rf_c822_filter_plain_type_id__once;
}

