/* memory-icon.c generated by valac 0.20.1.71-45f6, the Vala compiler
 * generated from memory-icon.vala, do not modify */

/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
/*
 * Copyright (C) 2011 Philip Withnall
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Philip Withnall <philip@tecnocode.co.uk>
 */

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <gdk-pixbuf/gdk-pixbuf.h>


#define CONTACTS_TYPE_MEMORY_ICON (contacts_memory_icon_get_type ())
#define CONTACTS_MEMORY_ICON(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), CONTACTS_TYPE_MEMORY_ICON, ContactsMemoryIcon))
#define CONTACTS_MEMORY_ICON_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), CONTACTS_TYPE_MEMORY_ICON, ContactsMemoryIconClass))
#define CONTACTS_IS_MEMORY_ICON(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), CONTACTS_TYPE_MEMORY_ICON))
#define CONTACTS_IS_MEMORY_ICON_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), CONTACTS_TYPE_MEMORY_ICON))
#define CONTACTS_MEMORY_ICON_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), CONTACTS_TYPE_MEMORY_ICON, ContactsMemoryIconClass))

typedef struct _ContactsMemoryIcon ContactsMemoryIcon;
typedef struct _ContactsMemoryIconClass ContactsMemoryIconClass;
typedef struct _ContactsMemoryIconPrivate ContactsMemoryIconPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _ContactsMemoryIconLoadAsyncData ContactsMemoryIconLoadAsyncData;

struct _ContactsMemoryIcon {
	GObject parent_instance;
	ContactsMemoryIconPrivate * priv;
};

struct _ContactsMemoryIconClass {
	GObjectClass parent_class;
};

struct _ContactsMemoryIconPrivate {
	guint8* _image_data;
	gint _image_data_length1;
	gint __image_data_size_;
	gchar* _image_type;
};

struct _ContactsMemoryIconLoadAsyncData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	ContactsMemoryIcon* self;
	gint size;
	GCancellable* cancellable;
	gchar* type;
	GInputStream* result;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	guint8* _tmp2_;
	gint _tmp2__length1;
	guint8* _tmp3_;
	gint _tmp3__length1;
	GMemoryInputStream* _tmp4_;
};


static gpointer contacts_memory_icon_parent_class = NULL;
static GIconIface* contacts_memory_icon_g_icon_parent_iface = NULL;
static GLoadableIconIface* contacts_memory_icon_g_loadable_icon_parent_iface = NULL;

GType contacts_memory_icon_get_type (void) G_GNUC_CONST;
#define CONTACTS_MEMORY_ICON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CONTACTS_TYPE_MEMORY_ICON, ContactsMemoryIconPrivate))
enum  {
	CONTACTS_MEMORY_ICON_DUMMY_PROPERTY
};
ContactsMemoryIcon* contacts_memory_icon_new (const gchar* image_type, guint8* image_data, int image_data_length1);
ContactsMemoryIcon* contacts_memory_icon_construct (GType object_type, const gchar* image_type, guint8* image_data, int image_data_length1);
static guint8* _vala_array_dup6 (guint8* self, int length);
ContactsMemoryIcon* contacts_memory_icon_new_from_pixbuf (GdkPixbuf* pixbuf, GError** error);
ContactsMemoryIcon* contacts_memory_icon_construct_from_pixbuf (GType object_type, GdkPixbuf* pixbuf, GError** error);
static gboolean contacts_memory_icon_real_equal (GIcon* base, GIcon* icon2);
static guint contacts_memory_icon_real_hash (GIcon* base);
static GInputStream* contacts_memory_icon_real_load (GLoadableIcon* base, gint size, gchar** type, GCancellable* cancellable, GError** error);
static guint8* _vala_array_dup7 (guint8* self, int length);
static void _g_free_gdestroy_notify (void* data);
static void contacts_memory_icon_real_load_async_data_free (gpointer _data);
static void contacts_memory_icon_real_load_async (GLoadableIcon* base, gint size, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_);
static gboolean contacts_memory_icon_real_load_async_co (ContactsMemoryIconLoadAsyncData* _data_);
static guint8* _vala_array_dup8 (guint8* self, int length);
static void contacts_memory_icon_finalize (GObject* obj);


static guint8* _vala_array_dup6 (guint8* self, int length) {
	return g_memdup (self, length * sizeof (guint8));
}


ContactsMemoryIcon* contacts_memory_icon_construct (GType object_type, const gchar* image_type, guint8* image_data, int image_data_length1) {
	ContactsMemoryIcon * self = NULL;
	guint8* _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	guint8* _tmp1_ = NULL;
	gint _tmp1__length1 = 0;
	const gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	self = (ContactsMemoryIcon*) g_object_new (object_type, NULL);
	_tmp0_ = image_data;
	_tmp0__length1 = image_data_length1;
	_tmp1_ = (_tmp0_ != NULL) ? _vala_array_dup6 (_tmp0_, _tmp0__length1) : ((gpointer) _tmp0_);
	_tmp1__length1 = _tmp0__length1;
	self->priv->_image_data = (g_free (self->priv->_image_data), NULL);
	self->priv->_image_data = _tmp1_;
	self->priv->_image_data_length1 = _tmp1__length1;
	self->priv->__image_data_size_ = self->priv->_image_data_length1;
	_tmp2_ = image_type;
	_tmp3_ = g_strdup (_tmp2_);
	_g_free0 (self->priv->_image_type);
	self->priv->_image_type = _tmp3_;
	return self;
}


ContactsMemoryIcon* contacts_memory_icon_new (const gchar* image_type, guint8* image_data, int image_data_length1) {
	return contacts_memory_icon_construct (CONTACTS_TYPE_MEMORY_ICON, image_type, image_data, image_data_length1);
}


ContactsMemoryIcon* contacts_memory_icon_construct_from_pixbuf (GType object_type, GdkPixbuf* pixbuf, GError** error) {
	ContactsMemoryIcon * self = NULL;
	guint8* buffer = NULL;
	gint buffer_length1 = 0;
	gint _buffer_size_ = 0;
	gboolean _tmp0_ = FALSE;
	GdkPixbuf* _tmp1_ = NULL;
	guint8* _tmp2_ = NULL;
	gsize _tmp3_;
	gboolean _tmp4_ = FALSE;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (pixbuf != NULL, NULL);
	_tmp1_ = pixbuf;
	_tmp4_ = gdk_pixbuf_save_to_buffer (_tmp1_, (gchar**) (&_tmp2_), &_tmp3_, "png", &_inner_error_, NULL, NULL);
	buffer = (g_free (buffer), NULL);
	buffer = _tmp2_;
	buffer_length1 = _tmp3_;
	_buffer_size_ = buffer_length1;
	_tmp0_ = _tmp4_;
	if (_inner_error_ != NULL) {
		g_propagate_error (error, _inner_error_);
		buffer = (g_free (buffer), NULL);
		_g_object_unref0 (self);
		return NULL;
	}
	if (_tmp0_) {
		self = (ContactsMemoryIcon*) contacts_memory_icon_construct (object_type, "image/png", buffer, buffer_length1);
	}
	buffer = (g_free (buffer), NULL);
	return self;
}


ContactsMemoryIcon* contacts_memory_icon_new_from_pixbuf (GdkPixbuf* pixbuf, GError** error) {
	return contacts_memory_icon_construct_from_pixbuf (CONTACTS_TYPE_MEMORY_ICON, pixbuf, error);
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static gboolean contacts_memory_icon_real_equal (GIcon* base, GIcon* icon2) {
	ContactsMemoryIcon * self;
	gboolean result = FALSE;
	ContactsMemoryIcon* icon = NULL;
	GIcon* _tmp0_ = NULL;
	ContactsMemoryIcon* _tmp1_ = NULL;
	gboolean _tmp2_ = FALSE;
	guint8* _tmp3_ = NULL;
	gint _tmp3__length1 = 0;
	ContactsMemoryIcon* _tmp4_ = NULL;
	guint8* _tmp5_ = NULL;
	gint _tmp5__length1 = 0;
	gboolean _tmp11_ = FALSE;
	self = (ContactsMemoryIcon*) base;
	_tmp0_ = icon2;
	_tmp1_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (G_TYPE_CHECK_INSTANCE_CAST (_tmp0_, G_TYPE_ICON, GIcon), CONTACTS_TYPE_MEMORY_ICON, ContactsMemoryIcon));
	icon = _tmp1_;
	_tmp3_ = self->priv->_image_data;
	_tmp3__length1 = self->priv->_image_data_length1;
	_tmp4_ = icon;
	_tmp5_ = _tmp4_->priv->_image_data;
	_tmp5__length1 = _tmp4_->priv->_image_data_length1;
	if (_tmp3__length1 == _tmp5__length1) {
		guint8* _tmp6_ = NULL;
		gint _tmp6__length1 = 0;
		ContactsMemoryIcon* _tmp7_ = NULL;
		guint8* _tmp8_ = NULL;
		gint _tmp8__length1 = 0;
		guint8* _tmp9_ = NULL;
		gint _tmp9__length1 = 0;
		gint _tmp10_ = 0;
		_tmp6_ = self->priv->_image_data;
		_tmp6__length1 = self->priv->_image_data_length1;
		_tmp7_ = icon;
		_tmp8_ = _tmp7_->priv->_image_data;
		_tmp8__length1 = _tmp7_->priv->_image_data_length1;
		_tmp9_ = self->priv->_image_data;
		_tmp9__length1 = self->priv->_image_data_length1;
		_tmp10_ = memcmp (_tmp6_, _tmp8_, (gsize) _tmp9__length1);
		_tmp2_ = _tmp10_ == 0;
	} else {
		_tmp2_ = FALSE;
	}
	_tmp11_ = _tmp2_;
	result = _tmp11_;
	_g_object_unref0 (icon);
	return result;
}


static guint contacts_memory_icon_real_hash (GIcon* base) {
	ContactsMemoryIcon * self;
	guint result = 0U;
	guint _tmp0_ = 0U;
	const gchar* _tmp1_ = NULL;
	guint hash = 0U;
	guint _tmp4_ = 0U;
	self = (ContactsMemoryIcon*) base;
	_tmp1_ = self->priv->_image_type;
	if (_tmp1_ != NULL) {
		const gchar* _tmp2_ = NULL;
		guint _tmp3_ = 0U;
		_tmp2_ = self->priv->_image_type;
		_tmp3_ = g_str_hash ((const gchar*) _tmp2_);
		_tmp0_ = _tmp3_;
	} else {
		_tmp0_ = (guint) 0;
	}
	_tmp4_ = _tmp0_;
	hash = _tmp4_;
	{
		guint i = 0U;
		i = (guint) 0;
		{
			gboolean _tmp5_ = FALSE;
			_tmp5_ = TRUE;
			while (TRUE) {
				gboolean _tmp6_ = FALSE;
				guint _tmp8_ = 0U;
				guint8* _tmp9_ = NULL;
				gint _tmp9__length1 = 0;
				guint _tmp10_ = 0U;
				guint _tmp11_ = 0U;
				guint8* _tmp12_ = NULL;
				gint _tmp12__length1 = 0;
				guint _tmp13_ = 0U;
				guint8 _tmp14_ = 0U;
				_tmp6_ = _tmp5_;
				if (!_tmp6_) {
					guint _tmp7_ = 0U;
					_tmp7_ = i;
					i = _tmp7_ + 1;
				}
				_tmp5_ = FALSE;
				_tmp8_ = i;
				_tmp9_ = self->priv->_image_data;
				_tmp9__length1 = self->priv->_image_data_length1;
				if (!(_tmp8_ < ((guint) _tmp9__length1))) {
					break;
				}
				_tmp10_ = hash;
				_tmp11_ = hash;
				_tmp12_ = self->priv->_image_data;
				_tmp12__length1 = self->priv->_image_data_length1;
				_tmp13_ = i;
				_tmp14_ = _tmp12_[_tmp13_];
				hash = ((_tmp10_ << 5) + _tmp11_) + _tmp14_;
			}
		}
	}
	result = hash;
	return result;
}


static guint8* _vala_array_dup7 (guint8* self, int length) {
	return g_memdup (self, length * sizeof (guint8));
}


static void _g_free_gdestroy_notify (void* data) {
	g_free (data);
}


static GInputStream* contacts_memory_icon_real_load (GLoadableIcon* base, gint size, gchar** type, GCancellable* cancellable, GError** error) {
	ContactsMemoryIcon * self;
	gchar* _vala_type = NULL;
	GInputStream* result = NULL;
	const gchar* _tmp0_ = NULL;
	gchar* _tmp1_ = NULL;
	guint8* _tmp2_ = NULL;
	gint _tmp2__length1 = 0;
	guint8* _tmp3_ = NULL;
	gint _tmp3__length1 = 0;
	GMemoryInputStream* _tmp4_ = NULL;
	self = (ContactsMemoryIcon*) base;
	_tmp0_ = self->priv->_image_type;
	_tmp1_ = g_strdup (_tmp0_);
	_g_free0 (_vala_type);
	_vala_type = _tmp1_;
	_tmp2_ = self->priv->_image_data;
	_tmp2__length1 = self->priv->_image_data_length1;
	_tmp3_ = (_tmp2_ != NULL) ? _vala_array_dup7 (_tmp2_, _tmp2__length1) : ((gpointer) _tmp2_);
	_tmp3__length1 = _tmp2__length1;
	_tmp4_ = (GMemoryInputStream*) g_memory_input_stream_new_from_data (_tmp3_, _tmp3__length1, _g_free_gdestroy_notify);
	result = (GInputStream*) _tmp4_;
	if (type) {
		*type = _vala_type;
	} else {
		_g_free0 (_vala_type);
	}
	return result;
}


static void contacts_memory_icon_real_load_async_data_free (gpointer _data) {
	ContactsMemoryIconLoadAsyncData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->cancellable);
	_g_object_unref0 (_data_->result);
	_g_object_unref0 (_data_->self);
	g_slice_free (ContactsMemoryIconLoadAsyncData, _data_);
}


static void contacts_memory_icon_real_load_async (GLoadableIcon* base, gint size, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	ContactsMemoryIcon * self;
	ContactsMemoryIconLoadAsyncData* _data_;
	ContactsMemoryIcon* _tmp0_ = NULL;
	gint _tmp1_ = 0;
	GCancellable* _tmp2_ = NULL;
	GCancellable* _tmp3_ = NULL;
	self = (ContactsMemoryIcon*) base;
	_data_ = g_slice_new0 (ContactsMemoryIconLoadAsyncData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, contacts_memory_icon_real_load_async);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, contacts_memory_icon_real_load_async_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = size;
	_data_->size = _tmp1_;
	_tmp2_ = cancellable;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	_g_object_unref0 (_data_->cancellable);
	_data_->cancellable = _tmp3_;
	contacts_memory_icon_real_load_async_co (_data_);
}


static GInputStream* contacts_memory_icon_real_load_finish (GLoadableIcon* base, GAsyncResult* _res_, gchar** type, GError** error) {
	GInputStream* result;
	ContactsMemoryIconLoadAsyncData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
	if (type) {
		*type = _data_->type;
	} else {
		_g_free0 (_data_->type);
	}
	_data_->type = NULL;
	result = _data_->result;
	_data_->result = NULL;
	return result;
}


static guint8* _vala_array_dup8 (guint8* self, int length) {
	return g_memdup (self, length * sizeof (guint8));
}


static gboolean contacts_memory_icon_real_load_async_co (ContactsMemoryIconLoadAsyncData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = NULL;
	_data_->_tmp0_ = _data_->self->priv->_image_type;
	_data_->_tmp1_ = NULL;
	_data_->_tmp1_ = g_strdup (_data_->_tmp0_);
	_g_free0 (_data_->type);
	_data_->type = _data_->_tmp1_;
	_data_->_tmp2_ = NULL;
	_data_->_tmp2__length1 = 0;
	_data_->_tmp2_ = _data_->self->priv->_image_data;
	_data_->_tmp2__length1 = _data_->self->priv->_image_data_length1;
	_data_->_tmp3_ = NULL;
	_data_->_tmp3__length1 = 0;
	_data_->_tmp3_ = (_data_->_tmp2_ != NULL) ? _vala_array_dup8 (_data_->_tmp2_, _data_->_tmp2__length1) : ((gpointer) _data_->_tmp2_);
	_data_->_tmp3__length1 = _data_->_tmp2__length1;
	_data_->_tmp4_ = NULL;
	_data_->_tmp4_ = (GMemoryInputStream*) g_memory_input_stream_new_from_data (_data_->_tmp3_, _data_->_tmp3__length1, _g_free_gdestroy_notify);
	_data_->result = (GInputStream*) _data_->_tmp4_;
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


static void contacts_memory_icon_class_init (ContactsMemoryIconClass * klass) {
	contacts_memory_icon_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (ContactsMemoryIconPrivate));
	G_OBJECT_CLASS (klass)->finalize = contacts_memory_icon_finalize;
}


static void contacts_memory_icon_g_icon_interface_init (GIconIface * iface) {
	contacts_memory_icon_g_icon_parent_iface = g_type_interface_peek_parent (iface);
	iface->equal = (gboolean (*)(GIcon*, GIcon*)) contacts_memory_icon_real_equal;
	iface->hash = (guint (*)(GIcon*)) contacts_memory_icon_real_hash;
}


static void contacts_memory_icon_g_loadable_icon_interface_init (GLoadableIconIface * iface) {
	contacts_memory_icon_g_loadable_icon_parent_iface = g_type_interface_peek_parent (iface);
	iface->load = (GInputStream* (*)(GLoadableIcon*, gint, gchar**, GCancellable*, GError**)) contacts_memory_icon_real_load;
	iface->load_async = (GInputStream* (*)(GLoadableIcon*, gint, GCancellable*, gchar**, GError**)) contacts_memory_icon_real_load_async;
	iface->load_finish = contacts_memory_icon_real_load_finish;
}


static void contacts_memory_icon_instance_init (ContactsMemoryIcon * self) {
	self->priv = CONTACTS_MEMORY_ICON_GET_PRIVATE (self);
}


static void contacts_memory_icon_finalize (GObject* obj) {
	ContactsMemoryIcon * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_MEMORY_ICON, ContactsMemoryIcon);
	self->priv->_image_data = (g_free (self->priv->_image_data), NULL);
	_g_free0 (self->priv->_image_type);
	G_OBJECT_CLASS (contacts_memory_icon_parent_class)->finalize (obj);
}


/**
 * A wrapper around a blob of image data (with an associated content type) which
 * presents it as a {@link GLib.LoadableIcon}. This allows inlined avatars to be
 * returned as {@link GLib.LoadableIcon}s.
 */
GType contacts_memory_icon_get_type (void) {
	static volatile gsize contacts_memory_icon_type_id__volatile = 0;
	if (g_once_init_enter (&contacts_memory_icon_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ContactsMemoryIconClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_memory_icon_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContactsMemoryIcon), 0, (GInstanceInitFunc) contacts_memory_icon_instance_init, NULL };
		static const GInterfaceInfo g_icon_info = { (GInterfaceInitFunc) contacts_memory_icon_g_icon_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		static const GInterfaceInfo g_loadable_icon_info = { (GInterfaceInitFunc) contacts_memory_icon_g_loadable_icon_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		GType contacts_memory_icon_type_id;
		contacts_memory_icon_type_id = g_type_register_static (G_TYPE_OBJECT, "ContactsMemoryIcon", &g_define_type_info, 0);
		g_type_add_interface_static (contacts_memory_icon_type_id, G_TYPE_ICON, &g_icon_info);
		g_type_add_interface_static (contacts_memory_icon_type_id, g_loadable_icon_get_type (), &g_loadable_icon_info);
		g_once_init_leave (&contacts_memory_icon_type_id__volatile, contacts_memory_icon_type_id);
	}
	return contacts_memory_icon_type_id__volatile;
}



