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

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

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_WM_CONFIG_BACKUP (wm_config_backup_get_type ())
#define WM_CONFIG_BACKUP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_WM_CONFIG_BACKUP, WMConfigBackup))
#define WM_CONFIG_BACKUP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_WM_CONFIG_BACKUP, WMConfigBackupClass))
#define IS_WM_CONFIG_BACKUP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_WM_CONFIG_BACKUP))
#define IS_WM_CONFIG_BACKUP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_WM_CONFIG_BACKUP))
#define WM_CONFIG_BACKUP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_WM_CONFIG_BACKUP, WMConfigBackupClass))

typedef struct _WMConfigBackup WMConfigBackup;
typedef struct _WMConfigBackupClass WMConfigBackupClass;
typedef struct _WMConfigBackupPrivate WMConfigBackupPrivate;
enum  {
	WM_CONFIG_BACKUP_0_PROPERTY,
	WM_CONFIG_BACKUP_NUM_PROPERTIES
};
static GParamSpec* wm_config_backup_properties[WM_CONFIG_BACKUP_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
#define _g_array_unref0(var) ((var == NULL) ? NULL : (var = (g_array_unref (var), NULL)))

struct _WMConfigBackup {
	GObject parent_instance;
	WMConfigBackupPrivate * priv;
};

struct _WMConfigBackupClass {
	GObjectClass parent_class;
};

static gpointer wm_config_backup_parent_class = NULL;
static gchar* wm_config_backup_backup_dir;
static gchar* wm_config_backup_backup_dir = NULL;

VALA_EXTERN GType wm_config_backup_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (WMConfigBackup, g_object_unref)
static gchar* wm_config_backup_get_old_backup_dir (void);
static gchar* wm_config_backup_get_new_backup_dir (void);
static void wm_config_backup_try_migrate_backup_dir (void);
static gchar* wm_config_backup_get_backup_dir (void);
VALA_EXTERN gboolean wm_config_backup_create_backup (const gchar* config_path,
                                         const gchar* wm_name,
                                         GError** error);
VALA_EXTERN gchar** wm_config_backup_list_backups (const gchar* wm_name,
                                       gint* result_length1);
static void _g_free0_ (gpointer var);
static inline void _g_list_free__g_free0_ (GList* self);
static void _vala_gchar_free_function_content_of (gpointer data);
static gchar** _vala_array_dup1 (gchar** self,
                          gssize length);
VALA_EXTERN gboolean wm_config_backup_restore_backup (const gchar* backup_path,
                                          const gchar* config_path,
                                          GError** error);
VALA_EXTERN gboolean wm_config_backup_restore_latest (const gchar* wm_name,
                                          const gchar* config_path);
VALA_EXTERN WMConfigBackup* wm_config_backup_new (void);
VALA_EXTERN WMConfigBackup* wm_config_backup_construct (GType object_type);
static void wm_config_backup_finalize (GObject * obj);
static GType wm_config_backup_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

static gchar*
wm_config_backup_get_old_backup_dir (void)
{
	gchar* config_dir = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* result;
	_tmp0_ = g_get_user_config_dir ();
	_tmp1_ = g_strdup (_tmp0_);
	config_dir = _tmp1_;
	_tmp2_ = config_dir;
	if (_tmp2_ == NULL) {
		const gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp3_ = g_get_home_dir ();
		_tmp4_ = g_build_filename (_tmp3_, ".config", NULL);
		_g_free0 (config_dir);
		config_dir = _tmp4_;
	}
	_tmp5_ = config_dir;
	_tmp6_ = g_build_filename (_tmp5_, "labwc-kbd", "backups", NULL);
	result = _tmp6_;
	_g_free0 (config_dir);
	return result;
}

static gchar*
wm_config_backup_get_new_backup_dir (void)
{
	gchar* config_dir = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp5_;
	gchar* _tmp6_;
	gchar* result;
	_tmp0_ = g_get_user_config_dir ();
	_tmp1_ = g_strdup (_tmp0_);
	config_dir = _tmp1_;
	_tmp2_ = config_dir;
	if (_tmp2_ == NULL) {
		const gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp3_ = g_get_home_dir ();
		_tmp4_ = g_build_filename (_tmp3_, ".config", NULL);
		_g_free0 (config_dir);
		config_dir = _tmp4_;
	}
	_tmp5_ = config_dir;
	_tmp6_ = g_build_filename (_tmp5_, "wl-kbd-config", "backups", NULL);
	result = _tmp6_;
	_g_free0 (config_dir);
	return result;
}

static void
wm_config_backup_try_migrate_backup_dir (void)
{
	gchar* old_dir = NULL;
	gchar* _tmp0_;
	gchar* new_dir = NULL;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp3_;
	GError* _inner_error0_ = NULL;
	_tmp0_ = wm_config_backup_get_old_backup_dir ();
	old_dir = _tmp0_;
	_tmp1_ = wm_config_backup_get_new_backup_dir ();
	new_dir = _tmp1_;
	_tmp2_ = old_dir;
	if (!g_file_test (_tmp2_, G_FILE_TEST_IS_DIR)) {
		_g_free0 (new_dir);
		_g_free0 (old_dir);
		return;
	}
	_tmp3_ = new_dir;
	if (g_file_test (_tmp3_, G_FILE_TEST_IS_DIR)) {
		_g_free0 (new_dir);
		_g_free0 (old_dir);
		return;
	}
	{
		const gchar* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		GFile* _tmp7_;
		GFile* _tmp8_;
		const gchar* _tmp9_;
		GFile* _tmp10_;
		GFile* _tmp11_;
		const gchar* _tmp12_;
		GFile* _tmp13_;
		GFile* _tmp14_;
		_tmp4_ = new_dir;
		_tmp5_ = g_path_get_dirname (_tmp4_);
		_tmp6_ = _tmp5_;
		_tmp7_ = g_file_new_for_path (_tmp6_);
		_tmp8_ = _tmp7_;
		g_file_make_directory_with_parents (_tmp8_, NULL, &_inner_error0_);
		_g_object_unref0 (_tmp8_);
		_g_free0 (_tmp6_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp9_ = old_dir;
		_tmp10_ = g_file_new_for_path (_tmp9_);
		_tmp11_ = _tmp10_;
		_tmp12_ = new_dir;
		_tmp13_ = g_file_new_for_path (_tmp12_);
		_tmp14_ = _tmp13_;
		g_file_move (_tmp11_, _tmp14_, G_FILE_COPY_NONE, NULL, NULL, NULL, &_inner_error0_);
		_g_object_unref0 (_tmp14_);
		_g_object_unref0 (_tmp11_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp15_;
		const gchar* _tmp16_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp15_ = e;
		_tmp16_ = _tmp15_->message;
		g_warning ("wm_config_backup.vala:37: Failed to migrate backup dir: %s", _tmp16_);
		_g_error_free0 (e);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_free0 (new_dir);
		_g_free0 (old_dir);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_free0 (new_dir);
	_g_free0 (old_dir);
}

static gchar*
wm_config_backup_get_backup_dir (void)
{
	const gchar* _tmp0_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* result;
	_tmp0_ = wm_config_backup_backup_dir;
	if (_tmp0_ == NULL) {
		gchar* _tmp1_;
		wm_config_backup_try_migrate_backup_dir ();
		_tmp1_ = wm_config_backup_get_new_backup_dir ();
		_g_free0 (wm_config_backup_backup_dir);
		wm_config_backup_backup_dir = _tmp1_;
	}
	_tmp2_ = wm_config_backup_backup_dir;
	_tmp3_ = g_strdup (_tmp2_);
	result = _tmp3_;
	return result;
}

gboolean
wm_config_backup_create_backup (const gchar* config_path,
                                const gchar* wm_name,
                                GError** error)
{
	gchar* dir_path = NULL;
	gchar* _tmp0_;
	GFile* dir = NULL;
	const gchar* _tmp1_;
	GFile* _tmp2_;
	GFile* _tmp3_;
	gchar* timestamp = NULL;
	GDateTime* _tmp6_;
	GDateTime* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* backup_name = NULL;
	const gchar* _tmp10_;
	gchar* _tmp11_;
	gchar* backup_path = NULL;
	gchar* _tmp12_;
	gchar* _tmp13_;
	const gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	GFile* source = NULL;
	GFile* _tmp17_;
	GFile* dest = NULL;
	const gchar* _tmp18_;
	GFile* _tmp19_;
	GFile* _tmp20_;
	GFile* _tmp21_;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (config_path != NULL, FALSE);
	g_return_val_if_fail (wm_name != NULL, FALSE);
	if (!g_file_test (config_path, G_FILE_TEST_EXISTS)) {
		result = FALSE;
		return result;
	}
	_tmp0_ = wm_config_backup_get_backup_dir ();
	dir_path = _tmp0_;
	_tmp1_ = dir_path;
	_tmp2_ = g_file_new_for_path (_tmp1_);
	dir = _tmp2_;
	_tmp3_ = dir;
	if (!g_file_query_exists (_tmp3_, NULL)) {
		GFile* _tmp4_;
		_tmp4_ = dir;
		g_file_make_directory_with_parents (_tmp4_, NULL, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			gboolean _tmp5_ = FALSE;
			g_propagate_error (error, _inner_error0_);
			_g_object_unref0 (dir);
			_g_free0 (dir_path);
			return _tmp5_;
		}
	}
	_tmp6_ = g_date_time_new_now_local ();
	_tmp7_ = _tmp6_;
	_tmp8_ = g_date_time_format (_tmp7_, "%Y%m%d_%H%M%S");
	_tmp9_ = _tmp8_;
	_g_date_time_unref0 (_tmp7_);
	timestamp = _tmp9_;
	_tmp10_ = timestamp;
	_tmp11_ = g_strdup_printf ("%s_%s.backup", wm_name, _tmp10_);
	backup_name = _tmp11_;
	_tmp12_ = wm_config_backup_get_backup_dir ();
	_tmp13_ = _tmp12_;
	_tmp14_ = backup_name;
	_tmp15_ = g_build_filename (_tmp13_, _tmp14_, NULL);
	_tmp16_ = _tmp15_;
	_g_free0 (_tmp13_);
	backup_path = _tmp16_;
	_tmp17_ = g_file_new_for_path (config_path);
	source = _tmp17_;
	_tmp18_ = backup_path;
	_tmp19_ = g_file_new_for_path (_tmp18_);
	dest = _tmp19_;
	_tmp20_ = source;
	_tmp21_ = dest;
	g_file_copy (_tmp20_, _tmp21_, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp22_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (dest);
		_g_object_unref0 (source);
		_g_free0 (backup_path);
		_g_free0 (backup_name);
		_g_free0 (timestamp);
		_g_object_unref0 (dir);
		_g_free0 (dir_path);
		return _tmp22_;
	}
	result = TRUE;
	_g_object_unref0 (dest);
	_g_object_unref0 (source);
	_g_free0 (backup_path);
	_g_free0 (backup_name);
	_g_free0 (timestamp);
	_g_object_unref0 (dir);
	_g_free0 (dir_path);
	return result;
}

static void
_g_free0_ (gpointer var)
{
	var = (g_free (var), NULL);
}

static inline void
_g_list_free__g_free0_ (GList* self)
{
	g_list_free_full (self, (GDestroyNotify) _g_free0_);
}

static void
_vala_gchar_free_function_content_of (gpointer data)
{
	gchar* self;
	self = *((gchar**) data);
	_g_free0_ (self);
}

static gchar**
_vala_array_dup1 (gchar** self,
                  gssize length)
{
	if (length >= 0) {
		gchar** result;
		gssize i;
		result = g_new0 (gchar*, length + 1);
		for (i = 0; i < length; i++) {
			gchar* _tmp0_;
			_tmp0_ = g_strdup (self[i]);
			result[i] = _tmp0_;
		}
		return result;
	}
	return NULL;
}

gchar**
wm_config_backup_list_backups (const gchar* wm_name,
                               gint* result_length1)
{
	GList* _result_ = NULL;
	gchar** dirs = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar** _tmp2_;
	gint dirs_length1;
	gint _dirs_size_;
	gchar** _tmp3_;
	gint _tmp3__length1;
	GArray* array = NULL;
	GArray* _tmp29_;
	GList* _tmp30_;
	GArray* _tmp34_;
	gpointer* _tmp35_;
	gint _tmp35__length1;
	gchar** _tmp36_;
	gint _tmp36__length1;
	gchar** _tmp37_;
	gint _tmp37__length1;
	GError* _inner_error0_ = NULL;
	gchar** result;
	g_return_val_if_fail (wm_name != NULL, NULL);
	_result_ = NULL;
	_tmp0_ = wm_config_backup_get_backup_dir ();
	_tmp1_ = wm_config_backup_get_old_backup_dir ();
	_tmp2_ = g_new0 (gchar*, 2 + 1);
	_tmp2_[0] = _tmp0_;
	_tmp2_[1] = _tmp1_;
	dirs = _tmp2_;
	dirs_length1 = 2;
	_dirs_size_ = dirs_length1;
	_tmp3_ = dirs;
	_tmp3__length1 = dirs_length1;
	{
		gchar** dir_path_collection = NULL;
		gint dir_path_collection_length1 = 0;
		gint _dir_path_collection_size_ = 0;
		gint dir_path_it = 0;
		dir_path_collection = _tmp3_;
		dir_path_collection_length1 = _tmp3__length1;
		for (dir_path_it = 0; dir_path_it < dir_path_collection_length1; dir_path_it = dir_path_it + 1) {
			gchar* _tmp4_;
			gchar* dir_path = NULL;
			_tmp4_ = g_strdup (dir_path_collection[dir_path_it]);
			dir_path = _tmp4_;
			{
				GFile* dir = NULL;
				const gchar* _tmp5_;
				GFile* _tmp6_;
				GFile* _tmp7_;
				_tmp5_ = dir_path;
				_tmp6_ = g_file_new_for_path (_tmp5_);
				dir = _tmp6_;
				_tmp7_ = dir;
				if (!g_file_query_exists (_tmp7_, NULL)) {
					_g_object_unref0 (dir);
					_g_free0 (dir_path);
					continue;
				}
				{
					GFileEnumerator* enumerator = NULL;
					GFile* _tmp8_;
					GFileEnumerator* _tmp9_;
					GFileInfo* info = NULL;
					_tmp8_ = dir;
					_tmp9_ = g_file_enumerate_children (_tmp8_, "name", G_FILE_QUERY_INFO_NONE, NULL, &_inner_error0_);
					enumerator = _tmp9_;
					if (G_UNLIKELY (_inner_error0_ != NULL)) {
						goto __catch0_g_error;
					}
					while (TRUE) {
						GFileInfo* _tmp10_ = NULL;
						GFileEnumerator* _tmp11_;
						GFileInfo* _tmp12_;
						GFileInfo* _tmp13_;
						GFileInfo* _tmp14_;
						gchar* name = NULL;
						GFileInfo* _tmp15_;
						const gchar* _tmp16_;
						gchar* _tmp17_;
						gboolean _tmp18_ = FALSE;
						const gchar* _tmp19_;
						gchar* _tmp20_;
						gchar* _tmp21_;
						gboolean _tmp22_;
						_tmp11_ = enumerator;
						_tmp12_ = g_file_enumerator_next_file (_tmp11_, NULL, &_inner_error0_);
						_tmp10_ = _tmp12_;
						if (G_UNLIKELY (_inner_error0_ != NULL)) {
							_g_object_unref0 (info);
							_g_object_unref0 (enumerator);
							goto __catch0_g_error;
						}
						_tmp13_ = _tmp10_;
						_tmp10_ = NULL;
						_g_object_unref0 (info);
						info = _tmp13_;
						_tmp14_ = info;
						if (!(_tmp14_ != NULL)) {
							_g_object_unref0 (_tmp10_);
							break;
						}
						_tmp15_ = info;
						_tmp16_ = g_file_info_get_name (_tmp15_);
						_tmp17_ = g_strdup (_tmp16_);
						name = _tmp17_;
						_tmp19_ = name;
						_tmp20_ = g_strconcat (wm_name, "_", NULL);
						_tmp21_ = _tmp20_;
						_tmp22_ = g_str_has_prefix (_tmp19_, _tmp21_);
						_g_free0 (_tmp21_);
						if (_tmp22_) {
							const gchar* _tmp23_;
							_tmp23_ = name;
							_tmp18_ = g_str_has_suffix (_tmp23_, ".backup");
						} else {
							_tmp18_ = FALSE;
						}
						if (_tmp18_) {
							const gchar* _tmp24_;
							const gchar* _tmp25_;
							gchar* _tmp26_;
							_tmp24_ = dir_path;
							_tmp25_ = name;
							_tmp26_ = g_build_filename (_tmp24_, _tmp25_, NULL);
							_result_ = g_list_append (_result_, _tmp26_);
						}
						_g_free0 (name);
						_g_object_unref0 (_tmp10_);
					}
					_g_object_unref0 (info);
					_g_object_unref0 (enumerator);
				}
				goto __finally0;
				__catch0_g_error:
				{
					GError* e = NULL;
					GError* _tmp27_;
					const gchar* _tmp28_;
					e = _inner_error0_;
					_inner_error0_ = NULL;
					_tmp27_ = e;
					_tmp28_ = _tmp27_->message;
					g_warning ("wm_config_backup.vala:95: Failed to list backups: %s", _tmp28_);
					_g_error_free0 (e);
				}
				__finally0:
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					_g_object_unref0 (dir);
					_g_free0 (dir_path);
					dirs = (_vala_array_free (dirs, dirs_length1, (GDestroyNotify) g_free), NULL);
					(_result_ == NULL) ? NULL : (_result_ = (_g_list_free__g_free0_ (_result_), NULL));
					g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
					g_clear_error (&_inner_error0_);
					return NULL;
				}
				_g_object_unref0 (dir);
				_g_free0 (dir_path);
			}
		}
	}
	_tmp29_ = g_array_new (TRUE, TRUE, sizeof (gchar*));
	g_array_set_clear_func (_tmp29_, (GDestroyNotify) _vala_gchar_free_function_content_of);
	array = _tmp29_;
	_tmp30_ = _result_;
	{
		GList* s_collection = NULL;
		GList* s_it = NULL;
		s_collection = _tmp30_;
		for (s_it = s_collection; s_it != NULL; s_it = s_it->next) {
			const gchar* s = NULL;
			s = (const gchar*) s_it->data;
			{
				GArray* _tmp31_;
				const gchar* _tmp32_;
				gchar* _tmp33_;
				_tmp31_ = array;
				_tmp32_ = s;
				_tmp33_ = g_strdup (_tmp32_);
				g_array_append_val (_tmp31_, _tmp33_);
			}
		}
	}
	_tmp34_ = array;
	_tmp35_ = _tmp34_->data;
	_tmp35__length1 = _tmp34_->len;
	_tmp36_ = (_tmp35_ != NULL) ? _vala_array_dup1 (_tmp35_, _tmp35__length1) : _tmp35_;
	_tmp36__length1 = _tmp35__length1;
	_tmp37_ = _tmp36_;
	_tmp37__length1 = _tmp36__length1;
	if (result_length1) {
		*result_length1 = _tmp37__length1;
	}
	result = _tmp37_;
	_g_array_unref0 (array);
	dirs = (_vala_array_free (dirs, dirs_length1, (GDestroyNotify) g_free), NULL);
	(_result_ == NULL) ? NULL : (_result_ = (_g_list_free__g_free0_ (_result_), NULL));
	return result;
}

gboolean
wm_config_backup_restore_backup (const gchar* backup_path,
                                 const gchar* config_path,
                                 GError** error)
{
	GFile* source = NULL;
	GFile* _tmp0_;
	GFile* dest = NULL;
	GFile* _tmp1_;
	GFile* _tmp2_;
	GFile* _tmp3_;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (backup_path != NULL, FALSE);
	g_return_val_if_fail (config_path != NULL, FALSE);
	if (!g_file_test (backup_path, G_FILE_TEST_EXISTS)) {
		result = FALSE;
		return result;
	}
	_tmp0_ = g_file_new_for_path (backup_path);
	source = _tmp0_;
	_tmp1_ = g_file_new_for_path (config_path);
	dest = _tmp1_;
	_tmp2_ = source;
	_tmp3_ = dest;
	g_file_copy (_tmp2_, _tmp3_, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		gboolean _tmp4_ = FALSE;
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (dest);
		_g_object_unref0 (source);
		return _tmp4_;
	}
	result = TRUE;
	_g_object_unref0 (dest);
	_g_object_unref0 (source);
	return result;
}

gboolean
wm_config_backup_restore_latest (const gchar* wm_name,
                                 const gchar* config_path)
{
	gchar** backups = NULL;
	gint _tmp0_ = 0;
	gchar** _tmp1_;
	gint backups_length1;
	gint _backups_size_;
	gchar** _tmp2_;
	gint _tmp2__length1;
	gchar* latest = NULL;
	gchar** _tmp3_;
	gint _tmp3__length1;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	gboolean _tmp20_ = FALSE;
	GError* _inner_error0_ = NULL;
	gboolean result;
	g_return_val_if_fail (wm_name != NULL, FALSE);
	g_return_val_if_fail (config_path != NULL, FALSE);
	_tmp1_ = wm_config_backup_list_backups (wm_name, &_tmp0_);
	backups = _tmp1_;
	backups_length1 = _tmp0_;
	_backups_size_ = backups_length1;
	_tmp2_ = backups;
	_tmp2__length1 = backups_length1;
	if (_tmp2__length1 == 0) {
		result = FALSE;
		backups = (_vala_array_free (backups, backups_length1, (GDestroyNotify) g_free), NULL);
		return result;
	}
	_tmp3_ = backups;
	_tmp3__length1 = backups_length1;
	_tmp4_ = _tmp3_[0];
	_tmp5_ = g_strdup (_tmp4_);
	latest = _tmp5_;
	{
		gint i = 0;
		i = 1;
		{
			gboolean _tmp6_ = FALSE;
			_tmp6_ = TRUE;
			while (TRUE) {
				gchar** _tmp8_;
				gint _tmp8__length1;
				GCompareFunc _tmp9_;
				gchar** _tmp10_;
				gint _tmp10__length1;
				const gchar* _tmp11_;
				const gchar* _tmp12_;
				if (!_tmp6_) {
					gint _tmp7_;
					_tmp7_ = i;
					i = _tmp7_ + 1;
				}
				_tmp6_ = FALSE;
				_tmp8_ = backups;
				_tmp8__length1 = backups_length1;
				if (!(i < _tmp8__length1)) {
					break;
				}
				_tmp9_ = ((GCompareFunc) g_strcmp0);
				_tmp10_ = backups;
				_tmp10__length1 = backups_length1;
				_tmp11_ = _tmp10_[i];
				_tmp12_ = latest;
				if (_tmp9_ (_tmp11_, _tmp12_) > 0) {
					gchar** _tmp13_;
					gint _tmp13__length1;
					const gchar* _tmp14_;
					gchar* _tmp15_;
					_tmp13_ = backups;
					_tmp13__length1 = backups_length1;
					_tmp14_ = _tmp13_[i];
					_tmp15_ = g_strdup (_tmp14_);
					_g_free0 (latest);
					latest = _tmp15_;
				}
			}
		}
	}
	{
		gboolean _tmp16_ = FALSE;
		const gchar* _tmp17_;
		_tmp17_ = latest;
		_tmp16_ = wm_config_backup_restore_backup (_tmp17_, config_path, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		result = _tmp16_;
		_g_free0 (latest);
		backups = (_vala_array_free (backups, backups_length1, (GDestroyNotify) g_free), NULL);
		return result;
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp18_;
		const gchar* _tmp19_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp18_ = e;
		_tmp19_ = _tmp18_->message;
		g_warning ("wm_config_backup.vala:133: Failed to restore backup: %s", _tmp19_);
		result = FALSE;
		_g_error_free0 (e);
		_g_free0 (latest);
		backups = (_vala_array_free (backups, backups_length1, (GDestroyNotify) g_free), NULL);
		return result;
	}
	__finally0:
	_g_free0 (latest);
	backups = (_vala_array_free (backups, backups_length1, (GDestroyNotify) g_free), NULL);
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return _tmp20_;
}

WMConfigBackup*
wm_config_backup_construct (GType object_type)
{
	WMConfigBackup * self = NULL;
	self = (WMConfigBackup*) g_object_new (object_type, NULL);
	return self;
}

WMConfigBackup*
wm_config_backup_new (void)
{
	return wm_config_backup_construct (TYPE_WM_CONFIG_BACKUP);
}

static void
wm_config_backup_class_init (WMConfigBackupClass * klass,
                             gpointer klass_data)
{
	wm_config_backup_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = wm_config_backup_finalize;
}

static void
wm_config_backup_instance_init (WMConfigBackup * self,
                                gpointer klass)
{
}

static void
wm_config_backup_finalize (GObject * obj)
{
	WMConfigBackup * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_WM_CONFIG_BACKUP, WMConfigBackup);
	G_OBJECT_CLASS (wm_config_backup_parent_class)->finalize (obj);
}

static GType
wm_config_backup_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (WMConfigBackupClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) wm_config_backup_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (WMConfigBackup), 0, (GInstanceInitFunc) wm_config_backup_instance_init, NULL };
	GType wm_config_backup_type_id;
	wm_config_backup_type_id = g_type_register_static (G_TYPE_OBJECT, "WMConfigBackup", &g_define_type_info, 0);
	return wm_config_backup_type_id;
}

GType
wm_config_backup_get_type (void)
{
	static volatile gsize wm_config_backup_type_id__once = 0;
	if (g_once_init_enter (&wm_config_backup_type_id__once)) {
		GType wm_config_backup_type_id;
		wm_config_backup_type_id = wm_config_backup_get_type_once ();
		g_once_init_leave (&wm_config_backup_type_id__once, wm_config_backup_type_id);
	}
	return wm_config_backup_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

