/* backend.c generated by valac 0.12.0, the Vala compiler
 * generated from backend.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gstdio.h>
#include <unistd.h>
#include <gobject/gvaluecollector.h>


#define KF_TEST_TYPE_BACKEND (kf_test_backend_get_type ())
#define KF_TEST_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), KF_TEST_TYPE_BACKEND, KfTestBackend))
#define KF_TEST_BACKEND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), KF_TEST_TYPE_BACKEND, KfTestBackendClass))
#define KF_TEST_IS_BACKEND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), KF_TEST_TYPE_BACKEND))
#define KF_TEST_IS_BACKEND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), KF_TEST_TYPE_BACKEND))
#define KF_TEST_BACKEND_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), KF_TEST_TYPE_BACKEND, KfTestBackendClass))

typedef struct _KfTestBackend KfTestBackend;
typedef struct _KfTestBackendClass KfTestBackendClass;
typedef struct _KfTestBackendPrivate KfTestBackendPrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_io_channel_unref0(var) ((var == NULL) ? NULL : (var = (g_io_channel_unref (var), NULL)))
typedef struct _KfTestParamSpecBackend KfTestParamSpecBackend;

struct _KfTestBackend {
	GTypeInstance parent_instance;
	volatile int ref_count;
	KfTestBackendPrivate * priv;
};

struct _KfTestBackendClass {
	GTypeClass parent_class;
	void (*finalize) (KfTestBackend *self);
};

struct _KfTestBackendPrivate {
	gchar* key_file_name;
};

struct _KfTestParamSpecBackend {
	GParamSpec parent_instance;
};


static gpointer kf_test_backend_parent_class = NULL;

gpointer kf_test_backend_ref (gpointer instance);
void kf_test_backend_unref (gpointer instance);
GParamSpec* kf_test_param_spec_backend (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void kf_test_value_set_backend (GValue* value, gpointer v_object);
void kf_test_value_take_backend (GValue* value, gpointer v_object);
gpointer kf_test_value_get_backend (const GValue* value);
GType kf_test_backend_get_type (void) G_GNUC_CONST;
#define KF_TEST_BACKEND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), KF_TEST_TYPE_BACKEND, KfTestBackendPrivate))
enum  {
	KF_TEST_BACKEND_DUMMY_PROPERTY
};
void kf_test_backend_set_up (KfTestBackend* self, const gchar* key_file_contents);
void kf_test_backend_tear_down (KfTestBackend* self);
KfTestBackend* kf_test_backend_new (void);
KfTestBackend* kf_test_backend_construct (GType object_type);
static void kf_test_backend_finalize (KfTestBackend* obj);


void kf_test_backend_set_up (KfTestBackend* self, const gchar* key_file_contents) {
	gint fd = 0;
	gchar* _tmp0_ = NULL;
	gint _tmp1_;
	gint _tmp2_;
	GIOChannel* _tmp3_ = NULL;
	GIOChannel* channel;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (key_file_contents != NULL);
	_tmp1_ = g_file_open_tmp ("folks-kf-test-XXXXXX", &_tmp0_, &_inner_error_);
	_g_free0 (self->priv->key_file_name);
	self->priv->key_file_name = _tmp0_;
	_tmp2_ = _tmp1_;
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == G_FILE_ERROR) {
			goto __catch0_g_file_error;
		}
		g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	fd = _tmp2_;
	goto __finally0;
	__catch0_g_file_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_error ("backend.vala:17: Error opening temporary file: %s", e->message);
		_g_error_free0 (e);
	}
	__finally0:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	_tmp3_ = g_io_channel_unix_new (fd);
	channel = _tmp3_;
	g_io_channel_write_chars (channel, (gchar*) key_file_contents, -1, NULL, &_inner_error_);
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == G_CONVERT_ERROR) {
			goto __catch1_g_convert_error;
		}
		if (_inner_error_->domain == G_IO_CHANNEL_ERROR) {
			goto __catch1_g_io_channel_error;
		}
		_g_io_channel_unref0 (channel);
		g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	goto __finally1;
	__catch1_g_convert_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_error ("backend.vala:28: Error converting for writing to temporary file '%s': " \
"%s\n" \
"%s", self->priv->key_file_name, e->message, key_file_contents);
		_g_error_free0 (e);
	}
	goto __finally1;
	__catch1_g_io_channel_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_error ("backend.vala:33: Error writing to temporary file '%s': %s", self->priv->key_file_name, e->message);
		_g_error_free0 (e);
	}
	__finally1:
	if (_inner_error_ != NULL) {
		_g_io_channel_unref0 (channel);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	g_io_channel_shutdown (channel, TRUE, &_inner_error_);
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == G_IO_CHANNEL_ERROR) {
			goto __catch2_g_io_channel_error;
		}
		_g_io_channel_unref0 (channel);
		g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	goto __finally2;
	__catch2_g_io_channel_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		_g_error_free0 (e);
	}
	__finally2:
	if (_inner_error_ != NULL) {
		_g_io_channel_unref0 (channel);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
	close (fd);
	g_setenv ("FOLKS_BACKEND_KEY_FILE_PATH", self->priv->key_file_name, TRUE);
	_g_io_channel_unref0 (channel);
}


void kf_test_backend_tear_down (KfTestBackend* self) {
	g_return_if_fail (self != NULL);
	if (self->priv->key_file_name != NULL) {
		g_remove (self->priv->key_file_name);
	}
	_g_free0 (self->priv->key_file_name);
	self->priv->key_file_name = NULL;
}


KfTestBackend* kf_test_backend_construct (GType object_type) {
	KfTestBackend* self = NULL;
	self = (KfTestBackend*) g_type_create_instance (object_type);
	return self;
}


KfTestBackend* kf_test_backend_new (void) {
	return kf_test_backend_construct (KF_TEST_TYPE_BACKEND);
}


static void kf_test_value_backend_init (GValue* value) {
	value->data[0].v_pointer = NULL;
}


static void kf_test_value_backend_free_value (GValue* value) {
	if (value->data[0].v_pointer) {
		kf_test_backend_unref (value->data[0].v_pointer);
	}
}


static void kf_test_value_backend_copy_value (const GValue* src_value, GValue* dest_value) {
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = kf_test_backend_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}


static gpointer kf_test_value_backend_peek_pointer (const GValue* value) {
	return value->data[0].v_pointer;
}


static gchar* kf_test_value_backend_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	if (collect_values[0].v_pointer) {
		KfTestBackend* object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = kf_test_backend_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}


static gchar* kf_test_value_backend_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	KfTestBackend** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = kf_test_backend_ref (value->data[0].v_pointer);
	}
	return NULL;
}


GParamSpec* kf_test_param_spec_backend (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
	KfTestParamSpecBackend* spec;
	g_return_val_if_fail (g_type_is_a (object_type, KF_TEST_TYPE_BACKEND), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}


gpointer kf_test_value_get_backend (const GValue* value) {
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, KF_TEST_TYPE_BACKEND), NULL);
	return value->data[0].v_pointer;
}


void kf_test_value_set_backend (GValue* value, gpointer v_object) {
	KfTestBackend* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, KF_TEST_TYPE_BACKEND));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, KF_TEST_TYPE_BACKEND));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		kf_test_backend_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		kf_test_backend_unref (old);
	}
}


void kf_test_value_take_backend (GValue* value, gpointer v_object) {
	KfTestBackend* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, KF_TEST_TYPE_BACKEND));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, KF_TEST_TYPE_BACKEND));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		kf_test_backend_unref (old);
	}
}


static void kf_test_backend_class_init (KfTestBackendClass * klass) {
	kf_test_backend_parent_class = g_type_class_peek_parent (klass);
	KF_TEST_BACKEND_CLASS (klass)->finalize = kf_test_backend_finalize;
	g_type_class_add_private (klass, sizeof (KfTestBackendPrivate));
}


static void kf_test_backend_instance_init (KfTestBackend * self) {
	self->priv = KF_TEST_BACKEND_GET_PRIVATE (self);
	self->ref_count = 1;
}


static void kf_test_backend_finalize (KfTestBackend* obj) {
	KfTestBackend * self;
	self = KF_TEST_BACKEND (obj);
	_g_free0 (self->priv->key_file_name);
}


GType kf_test_backend_get_type (void) {
	static volatile gsize kf_test_backend_type_id__volatile = 0;
	if (g_once_init_enter (&kf_test_backend_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { kf_test_value_backend_init, kf_test_value_backend_free_value, kf_test_value_backend_copy_value, kf_test_value_backend_peek_pointer, "p", kf_test_value_backend_collect_value, "p", kf_test_value_backend_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (KfTestBackendClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) kf_test_backend_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (KfTestBackend), 0, (GInstanceInitFunc) kf_test_backend_instance_init, &g_define_type_value_table };
		static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
		GType kf_test_backend_type_id;
		kf_test_backend_type_id = g_type_register_fundamental (g_type_fundamental_next (), "KfTestBackend", &g_define_type_info, &g_define_type_fundamental_info, 0);
		g_once_init_leave (&kf_test_backend_type_id__volatile, kf_test_backend_type_id);
	}
	return kf_test_backend_type_id__volatile;
}


gpointer kf_test_backend_ref (gpointer instance) {
	KfTestBackend* self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}


void kf_test_backend_unref (gpointer instance) {
	KfTestBackend* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		KF_TEST_BACKEND_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}



