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

/**
 * Copyright (C) 2009-2012 Michael 'Mickey' Lauer <mlauer@vanille-media.de>
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gstdio.h>
#include <errno.h>


#define FSO_FRAMEWORK_TYPE_OVERLAY_FILE (fso_framework_overlay_file_get_type ())
#define FSO_FRAMEWORK_OVERLAY_FILE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), FSO_FRAMEWORK_TYPE_OVERLAY_FILE, FsoFrameworkOverlayFile))
#define FSO_FRAMEWORK_OVERLAY_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), FSO_FRAMEWORK_TYPE_OVERLAY_FILE, FsoFrameworkOverlayFileClass))
#define FSO_FRAMEWORK_IS_OVERLAY_FILE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), FSO_FRAMEWORK_TYPE_OVERLAY_FILE))
#define FSO_FRAMEWORK_IS_OVERLAY_FILE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), FSO_FRAMEWORK_TYPE_OVERLAY_FILE))
#define FSO_FRAMEWORK_OVERLAY_FILE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), FSO_FRAMEWORK_TYPE_OVERLAY_FILE, FsoFrameworkOverlayFileClass))

typedef struct _FsoFrameworkOverlayFile FsoFrameworkOverlayFile;
typedef struct _FsoFrameworkOverlayFileClass FsoFrameworkOverlayFileClass;
typedef struct _FsoFrameworkOverlayFilePrivate FsoFrameworkOverlayFilePrivate;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _FsoFrameworkOverlayFile {
	GObject parent_instance;
	FsoFrameworkOverlayFilePrivate * priv;
};

struct _FsoFrameworkOverlayFileClass {
	GObjectClass parent_class;
};

struct _FsoFrameworkOverlayFilePrivate {
	gchar* filename;
};


static gpointer fso_framework_overlay_file_parent_class = NULL;

GType fso_framework_overlay_file_get_type (void) G_GNUC_CONST;
#define FSO_FRAMEWORK_OVERLAY_FILE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), FSO_FRAMEWORK_TYPE_OVERLAY_FILE, FsoFrameworkOverlayFilePrivate))
enum  {
	FSO_FRAMEWORK_OVERLAY_FILE_DUMMY_PROPERTY
};
FsoFrameworkOverlayFile* fso_framework_overlay_file_new (const gchar* filename, const gchar* newcontent);
FsoFrameworkOverlayFile* fso_framework_overlay_file_construct (GType object_type, const gchar* filename, const gchar* newcontent);
static void fso_framework_overlay_file_finalize (GObject* obj);


static const gchar* string_to_string (const gchar* self) {
	const gchar* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}


FsoFrameworkOverlayFile* fso_framework_overlay_file_construct (GType object_type, const gchar* filename, const gchar* newcontent) {
	FsoFrameworkOverlayFile * self = NULL;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (filename != NULL, NULL);
	g_return_val_if_fail (newcontent != NULL, NULL);
	self = (FsoFrameworkOverlayFile*) g_object_new (object_type, NULL);
	{
		gchar* contents = NULL;
		gsize length = 0UL;
		const gchar* _tmp0_;
		gchar* _tmp1_ = NULL;
		gsize _tmp2_ = 0UL;
		const gchar* _tmp3_;
		gchar* _tmp4_;
		gchar* _tmp5_;
		const gchar* _tmp6_;
		gsize _tmp7_;
		const gchar* _tmp8_;
		const gchar* _tmp9_;
		const gchar* _tmp10_;
		gint _tmp11_;
		gint _tmp12_;
		const gchar* _tmp13_;
		gchar* _tmp14_;
		_tmp0_ = filename;
		g_file_get_contents (_tmp0_, &_tmp1_, &_tmp2_, &_inner_error_);
		_g_free0 (contents);
		contents = _tmp1_;
		length = _tmp2_;
		if (_inner_error_ != NULL) {
			_g_free0 (contents);
			if (_inner_error_->domain == G_FILE_ERROR) {
				goto __catch3_g_file_error;
			}
			_g_free0 (contents);
			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;
		}
		_tmp3_ = filename;
		_tmp4_ = g_strconcat (_tmp3_, ".saved", NULL);
		_tmp5_ = _tmp4_;
		_tmp6_ = contents;
		_tmp7_ = length;
		g_file_set_contents (_tmp5_, _tmp6_, (gssize) _tmp7_, &_inner_error_);
		_g_free0 (_tmp5_);
		if (_inner_error_ != NULL) {
			_g_free0 (contents);
			if (_inner_error_->domain == G_FILE_ERROR) {
				goto __catch3_g_file_error;
			}
			_g_free0 (contents);
			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;
		}
		_tmp8_ = filename;
		_tmp9_ = newcontent;
		_tmp10_ = newcontent;
		_tmp11_ = strlen (_tmp10_);
		_tmp12_ = _tmp11_;
		g_file_set_contents (_tmp8_, _tmp9_, (gssize) _tmp12_, &_inner_error_);
		if (_inner_error_ != NULL) {
			_g_free0 (contents);
			if (_inner_error_->domain == G_FILE_ERROR) {
				goto __catch3_g_file_error;
			}
			_g_free0 (contents);
			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;
		}
		_tmp13_ = filename;
		_tmp14_ = g_strdup (_tmp13_);
		_g_free0 (self->priv->filename);
		self->priv->filename = _tmp14_;
		_g_free0 (contents);
	}
	goto __finally3;
	__catch3_g_file_error:
	{
		GError* e = NULL;
		const gchar* _tmp15_;
		const gchar* _tmp16_ = NULL;
		gint _tmp17_;
		const gchar* _tmp18_ = NULL;
		const gchar* _tmp19_ = NULL;
		gchar* _tmp20_ = NULL;
		gchar* _tmp21_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp15_ = filename;
		_tmp16_ = string_to_string (_tmp15_);
		_tmp17_ = errno;
		_tmp18_ = g_strerror (_tmp17_);
		_tmp19_ = string_to_string (_tmp18_);
		_tmp20_ = g_strconcat ("Could not save ", _tmp16_, ": ", _tmp19_, NULL);
		_tmp21_ = _tmp20_;
		g_warning ("overlay.vala:40: %s", _tmp21_);
		_g_free0 (_tmp21_);
		_g_error_free0 (e);
	}
	__finally3:
	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;
}


FsoFrameworkOverlayFile* fso_framework_overlay_file_new (const gchar* filename, const gchar* newcontent) {
	return fso_framework_overlay_file_construct (FSO_FRAMEWORK_TYPE_OVERLAY_FILE, filename, newcontent);
}


static void fso_framework_overlay_file_class_init (FsoFrameworkOverlayFileClass * klass) {
	fso_framework_overlay_file_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (FsoFrameworkOverlayFilePrivate));
	G_OBJECT_CLASS (klass)->finalize = fso_framework_overlay_file_finalize;
}


static void fso_framework_overlay_file_instance_init (FsoFrameworkOverlayFile * self) {
	self->priv = FSO_FRAMEWORK_OVERLAY_FILE_GET_PRIVATE (self);
}


static void fso_framework_overlay_file_finalize (GObject* obj) {
	FsoFrameworkOverlayFile * self;
	const gchar* _tmp0_;
	GError * _inner_error_ = NULL;
	self = FSO_FRAMEWORK_OVERLAY_FILE (obj);
	_tmp0_ = self->priv->filename;
	if (_tmp0_ != NULL) {
		{
			gchar* contents = NULL;
			gsize length = 0UL;
			const gchar* _tmp1_;
			gchar* _tmp2_;
			gchar* _tmp3_;
			gchar* _tmp4_ = NULL;
			gsize _tmp5_ = 0UL;
			const gchar* _tmp6_;
			const gchar* _tmp7_;
			gsize _tmp8_;
			const gchar* _tmp9_;
			gchar* _tmp10_;
			gchar* _tmp11_;
			_tmp1_ = self->priv->filename;
			_tmp2_ = g_strconcat (_tmp1_, ".saved", NULL);
			_tmp3_ = _tmp2_;
			g_file_get_contents (_tmp3_, &_tmp4_, &_tmp5_, &_inner_error_);
			_g_free0 (contents);
			contents = _tmp4_;
			length = _tmp5_;
			_g_free0 (_tmp3_);
			if (_inner_error_ != NULL) {
				_g_free0 (contents);
				if (_inner_error_->domain == G_FILE_ERROR) {
					goto __catch2_g_file_error;
				}
				_g_free0 (contents);
				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_);
			}
			_tmp6_ = self->priv->filename;
			_tmp7_ = contents;
			_tmp8_ = length;
			g_file_set_contents (_tmp6_, _tmp7_, (gssize) _tmp8_, &_inner_error_);
			if (_inner_error_ != NULL) {
				_g_free0 (contents);
				if (_inner_error_->domain == G_FILE_ERROR) {
					goto __catch2_g_file_error;
				}
				_g_free0 (contents);
				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_);
			}
			_tmp9_ = self->priv->filename;
			_tmp10_ = g_strconcat (_tmp9_, ".saved", NULL);
			_tmp11_ = _tmp10_;
			g_remove (_tmp11_);
			_g_free0 (_tmp11_);
			_g_free0 (contents);
		}
		goto __finally2;
		__catch2_g_file_error:
		{
			GError* e = NULL;
			const gchar* _tmp12_;
			const gchar* _tmp13_ = NULL;
			gint _tmp14_;
			const gchar* _tmp15_ = NULL;
			const gchar* _tmp16_ = NULL;
			gchar* _tmp17_ = NULL;
			gchar* _tmp18_;
			e = _inner_error_;
			_inner_error_ = NULL;
			_tmp12_ = self->priv->filename;
			_tmp13_ = string_to_string (_tmp12_);
			_tmp14_ = errno;
			_tmp15_ = g_strerror (_tmp14_);
			_tmp16_ = string_to_string (_tmp15_);
			_tmp17_ = g_strconcat ("Could not restore ", _tmp13_, ": ", _tmp16_, NULL);
			_tmp18_ = _tmp17_;
			g_warning ("overlay.vala:58: %s", _tmp18_);
			_g_free0 (_tmp18_);
			_g_error_free0 (e);
		}
		__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_);
		}
	}
	_g_free0 (self->priv->filename);
	G_OBJECT_CLASS (fso_framework_overlay_file_parent_class)->finalize (obj);
}


/**
 * @class FsoFramework.OverlayFile
 **/
GType fso_framework_overlay_file_get_type (void) {
	static volatile gsize fso_framework_overlay_file_type_id__volatile = 0;
	if (g_once_init_enter (&fso_framework_overlay_file_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (FsoFrameworkOverlayFileClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) fso_framework_overlay_file_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FsoFrameworkOverlayFile), 0, (GInstanceInitFunc) fso_framework_overlay_file_instance_init, NULL };
		GType fso_framework_overlay_file_type_id;
		fso_framework_overlay_file_type_id = g_type_register_static (G_TYPE_OBJECT, "FsoFrameworkOverlayFile", &g_define_type_info, 0);
		g_once_init_leave (&fso_framework_overlay_file_type_id__volatile, fso_framework_overlay_file_type_id);
	}
	return fso_framework_overlay_file_type_id__volatile;
}



