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

/*

this is a part of trayclock
copyright 2008 jezra lickter http://www.jezra.net

trayclock is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

trayclock 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with trayclock.  If not, see <http://www.gnu.org/licenses/>.

*/

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


#define TYPE_CONFIGURATION (configuration_get_type ())
#define CONFIGURATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CONFIGURATION, Configuration))
#define CONFIGURATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CONFIGURATION, ConfigurationClass))
#define IS_CONFIGURATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CONFIGURATION))
#define IS_CONFIGURATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CONFIGURATION))
#define CONFIGURATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CONFIGURATION, ConfigurationClass))

typedef struct _Configuration Configuration;
typedef struct _ConfigurationClass ConfigurationClass;
typedef struct _ConfigurationPrivate ConfigurationPrivate;
#define _g_key_file_free0(var) ((var == NULL) ? NULL : (var = (g_key_file_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _ParamSpecConfiguration ParamSpecConfiguration;

typedef enum  {
	CONFIG_ERROR_CREATE_DIR_FAILED,
	CONFIG_ERROR_FILE_NOT_FOUND,
	CONFIG_ERROR_DOCUMENT_EMPTY,
	CONFIG_ERROR_READ_FAILED,
	CONFIG_ERROR_WRITE_FAILED,
	CONFIG_ERROR_NO_KEY_VALUE
} ConfigError;
#define CONFIG_ERROR config_error_quark ()
struct _Configuration {
	GTypeInstance parent_instance;
	volatile int ref_count;
	ConfigurationPrivate * priv;
};

struct _ConfigurationClass {
	GTypeClass parent_class;
	void (*finalize) (Configuration *self);
};

struct _ConfigurationPrivate {
	GKeyFile* keyfile;
	char* config_file;
	gboolean config_changed;
};

struct _ParamSpecConfiguration {
	GParamSpec parent_instance;
};


static gpointer configuration_parent_class = NULL;

GQuark config_error_quark (void);
gpointer configuration_ref (gpointer instance);
void configuration_unref (gpointer instance);
GParamSpec* param_spec_configuration (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags);
void value_set_configuration (GValue* value, gpointer v_object);
void value_take_configuration (GValue* value, gpointer v_object);
gpointer value_get_configuration (const GValue* value);
GType configuration_get_type (void);
#define CONFIGURATION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_CONFIGURATION, ConfigurationPrivate))
enum  {
	CONFIGURATION_DUMMY_PROPERTY
};
void configuration_read_config (Configuration* self, GError** error);
Configuration* configuration_new (const char* config_file_path);
Configuration* configuration_construct (GType object_type, const char* config_file_path);
void configuration_save_config (Configuration* self, GError** error);
void configuration_set_key_value (Configuration* self, const char* group, const char* key, const char* val);
char* configuration_get_value (Configuration* self, const char* group, const char* key);
static void configuration_finalize (Configuration* obj);



GQuark config_error_quark (void) {
	return g_quark_from_static_string ("config_error-quark");
}


Configuration* configuration_construct (GType object_type, const char* config_file_path) {
	GError * _inner_error_;
	Configuration* self;
	char* _tmp0_;
	GKeyFile* _tmp1_;
	g_return_val_if_fail (config_file_path != NULL, NULL);
	_inner_error_ = NULL;
	self = (Configuration*) g_type_create_instance (object_type);
	self->priv->config_changed = FALSE;
	self->priv->config_file = (_tmp0_ = g_strdup (config_file_path), _g_free0 (self->priv->config_file), _tmp0_);
	self->priv->keyfile = (_tmp1_ = g_key_file_new (), _g_key_file_free0 (self->priv->keyfile), _tmp1_);
	{
		configuration_read_config (self, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == CONFIG_ERROR) {
				goto __catch2_config_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 NULL;
		}
	}
	goto __finally2;
	__catch2_config_error:
	{
		GError * _error_;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		{
			fprintf (stdout, "%s\n", _error_->message);
			_g_error_free0 (_error_);
		}
	}
	__finally2:
	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 NULL;
	}
	return self;
}


Configuration* configuration_new (const char* config_file_path) {
	return configuration_construct (TYPE_CONFIGURATION, config_file_path);
}


void configuration_save_config (Configuration* self, GError** error) {
	GError * _inner_error_;
	char* config_text;
	g_return_if_fail (self != NULL);
	_inner_error_ = NULL;
	config_text = g_key_file_to_data (self->priv->keyfile, NULL, NULL);
	{
		g_file_set_contents (self->priv->config_file, config_text, -1, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_FILE_ERROR) {
				goto __catch3_g_file_error;
			}
			_g_free0 (config_text);
			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;
		}
		self->priv->config_changed = FALSE;
	}
	goto __finally3;
	__catch3_g_file_error:
	{
		GError * err;
		err = _inner_error_;
		_inner_error_ = NULL;
		{
			_inner_error_ = g_error_new_literal (CONFIG_ERROR, CONFIG_ERROR_WRITE_FAILED, err->message);
			{
				_g_error_free0 (err);
				_g_free0 (config_text);
				goto __finally3;
			}
			_g_error_free0 (err);
		}
	}
	__finally3:
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == CONFIG_ERROR) {
			g_propagate_error (error, _inner_error_);
			_g_free0 (config_text);
			return;
		} else {
			_g_free0 (config_text);
			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_free0 (config_text);
}


void configuration_read_config (Configuration* self, GError** error) {
	GError * _inner_error_;
	char* dirpath;
	g_return_if_fail (self != NULL);
	_inner_error_ = NULL;
	dirpath = g_path_get_dirname (self->priv->config_file);
	if (!g_file_test (dirpath, G_FILE_TEST_EXISTS)) {
		if (g_mkdir_with_parents (dirpath, 0744) == (-1)) {
			char* _tmp0_;
			GError* _tmp1_;
			_inner_error_ = (_tmp1_ = g_error_new_literal (CONFIG_ERROR, CONFIG_ERROR_CREATE_DIR_FAILED, _tmp0_ = g_strconcat ("failed to make ", dirpath, NULL)), _g_free0 (_tmp0_), _tmp1_);
			{
				if (_inner_error_->domain == CONFIG_ERROR) {
					g_propagate_error (error, _inner_error_);
					_g_free0 (dirpath);
					return;
				} else {
					_g_free0 (dirpath);
					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;
				}
			}
		}
	}
	if (!g_file_test (self->priv->config_file, G_FILE_TEST_EXISTS)) {
		{
			g_file_set_contents (self->priv->config_file, "", -1, &_inner_error_);
			if (_inner_error_ != NULL) {
				if (_inner_error_->domain == G_FILE_ERROR) {
					goto __catch4_g_file_error;
				}
				_g_free0 (dirpath);
				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 __finally4;
		__catch4_g_file_error:
		{
			GError * err;
			err = _inner_error_;
			_inner_error_ = NULL;
			{
				_inner_error_ = g_error_new_literal (CONFIG_ERROR, CONFIG_ERROR_WRITE_FAILED, err->message);
				{
					_g_error_free0 (err);
					_g_free0 (dirpath);
					goto __finally4;
				}
				_g_error_free0 (err);
			}
		}
		__finally4:
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == CONFIG_ERROR) {
				g_propagate_error (error, _inner_error_);
				_g_free0 (dirpath);
				return;
			} else {
				_g_free0 (dirpath);
				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_key_file_load_from_file (self->priv->keyfile, self->priv->config_file, G_KEY_FILE_NONE, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch5_g_error;
		}
	}
	goto __finally5;
	__catch5_g_error:
	{
		GError * err;
		err = _inner_error_;
		_inner_error_ = NULL;
		{
			_inner_error_ = g_error_new (CONFIG_ERROR, CONFIG_ERROR_READ_FAILED, "can not read %s: %s", self->priv->config_file, err->message);
			{
				_g_error_free0 (err);
				_g_free0 (dirpath);
				goto __finally5;
			}
			_g_error_free0 (err);
		}
	}
	__finally5:
	if (_inner_error_ != NULL) {
		if (_inner_error_->domain == CONFIG_ERROR) {
			g_propagate_error (error, _inner_error_);
			_g_free0 (dirpath);
			return;
		} else {
			_g_free0 (dirpath);
			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_free0 (dirpath);
}


void configuration_set_key_value (Configuration* self, const char* group, const char* key, const char* val) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (group != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (val != NULL);
	g_key_file_set_string (self->priv->keyfile, group, key, val);
	self->priv->config_changed = TRUE;
}


char* configuration_get_value (Configuration* self, const char* group, const char* key) {
	char* result = NULL;
	GError * _inner_error_;
	char* val;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (group != NULL, NULL);
	g_return_val_if_fail (key != NULL, NULL);
	_inner_error_ = NULL;
	val = NULL;
	{
		char* _tmp0_;
		char* _tmp1_;
		_tmp0_ = g_key_file_get_string (self->priv->keyfile, group, key, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_KEY_FILE_ERROR) {
				goto __catch6_g_key_file_error;
			}
			_g_free0 (val);
			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 NULL;
		}
		val = (_tmp1_ = _tmp0_, _g_free0 (val), _tmp1_);
	}
	goto __finally6;
	__catch6_g_key_file_error:
	{
		GError * err;
		err = _inner_error_;
		_inner_error_ = NULL;
		{
			char* _tmp2_;
			val = (_tmp2_ = NULL, _g_free0 (val), _tmp2_);
			_g_error_free0 (err);
		}
	}
	__finally6:
	if (_inner_error_ != NULL) {
		_g_free0 (val);
		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 NULL;
	}
	result = val;
	return result;
}


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


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


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


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


static gchar* value_configuration_collect_value (GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	if (collect_values[0].v_pointer) {
		Configuration* 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 = configuration_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}


static gchar* value_configuration_lcopy_value (const GValue* value, guint n_collect_values, GTypeCValue* collect_values, guint collect_flags) {
	Configuration** 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 = configuration_ref (value->data[0].v_pointer);
	}
	return NULL;
}


GParamSpec* param_spec_configuration (const gchar* name, const gchar* nick, const gchar* blurb, GType object_type, GParamFlags flags) {
	ParamSpecConfiguration* spec;
	g_return_val_if_fail (g_type_is_a (object_type, TYPE_CONFIGURATION), 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 value_get_configuration (const GValue* value) {
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_CONFIGURATION), NULL);
	return value->data[0].v_pointer;
}


void value_set_configuration (GValue* value, gpointer v_object) {
	Configuration* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_CONFIGURATION));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_CONFIGURATION));
		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;
		configuration_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		configuration_unref (old);
	}
}


void value_take_configuration (GValue* value, gpointer v_object) {
	Configuration* old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, TYPE_CONFIGURATION));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, TYPE_CONFIGURATION));
		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) {
		configuration_unref (old);
	}
}


static void configuration_class_init (ConfigurationClass * klass) {
	configuration_parent_class = g_type_class_peek_parent (klass);
	CONFIGURATION_CLASS (klass)->finalize = configuration_finalize;
	g_type_class_add_private (klass, sizeof (ConfigurationPrivate));
}


static void configuration_instance_init (Configuration * self) {
	self->priv = CONFIGURATION_GET_PRIVATE (self);
	self->ref_count = 1;
}


static void configuration_finalize (Configuration* obj) {
	Configuration * self;
	GError * _inner_error_;
	self = CONFIGURATION (obj);
	_inner_error_ = NULL;
	{
		if (self->priv->config_changed) {
			configuration_save_config (self, &_inner_error_);
			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_);
			}
		}
	}
	_g_key_file_free0 (self->priv->keyfile);
	_g_free0 (self->priv->config_file);
}


GType configuration_get_type (void) {
	static volatile gsize configuration_type_id__volatile = 0;
	if (g_once_init_enter (&configuration_type_id__volatile)) {
		static const GTypeValueTable g_define_type_value_table = { value_configuration_init, value_configuration_free_value, value_configuration_copy_value, value_configuration_peek_pointer, "p", value_configuration_collect_value, "p", value_configuration_lcopy_value };
		static const GTypeInfo g_define_type_info = { sizeof (ConfigurationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) configuration_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (Configuration), 0, (GInstanceInitFunc) configuration_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 configuration_type_id;
		configuration_type_id = g_type_register_fundamental (g_type_fundamental_next (), "Configuration", &g_define_type_info, &g_define_type_fundamental_info, 0);
		g_once_init_leave (&configuration_type_id__volatile, configuration_type_id);
	}
	return configuration_type_id__volatile;
}


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


void configuration_unref (gpointer instance) {
	Configuration* self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		CONFIGURATION_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}




