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

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    © 2008,2009 Michael Terry <mike@mterry.name>

    Déjà Dup 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.

    Déjà Dup 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 Déjà Dup.  If not, see <http://www.gnu.org/licenses/>.
*/

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


#define DEJA_DUP_TYPE_RECURSIVE_OP (deja_dup_recursive_op_get_type ())
#define DEJA_DUP_RECURSIVE_OP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_RECURSIVE_OP, DejaDupRecursiveOp))
#define DEJA_DUP_RECURSIVE_OP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_RECURSIVE_OP, DejaDupRecursiveOpClass))
#define DEJA_DUP_IS_RECURSIVE_OP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_RECURSIVE_OP))
#define DEJA_DUP_IS_RECURSIVE_OP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_RECURSIVE_OP))
#define DEJA_DUP_RECURSIVE_OP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_RECURSIVE_OP, DejaDupRecursiveOpClass))

typedef struct _DejaDupRecursiveOp DejaDupRecursiveOp;
typedef struct _DejaDupRecursiveOpClass DejaDupRecursiveOpClass;
typedef struct _DejaDupRecursiveOpPrivate DejaDupRecursiveOpPrivate;

#define DEJA_DUP_TYPE_RECURSIVE_MOVE (deja_dup_recursive_move_get_type ())
#define DEJA_DUP_RECURSIVE_MOVE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_RECURSIVE_MOVE, DejaDupRecursiveMove))
#define DEJA_DUP_RECURSIVE_MOVE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_RECURSIVE_MOVE, DejaDupRecursiveMoveClass))
#define DEJA_DUP_IS_RECURSIVE_MOVE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_RECURSIVE_MOVE))
#define DEJA_DUP_IS_RECURSIVE_MOVE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_RECURSIVE_MOVE))
#define DEJA_DUP_RECURSIVE_MOVE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_RECURSIVE_MOVE, DejaDupRecursiveMoveClass))

typedef struct _DejaDupRecursiveMove DejaDupRecursiveMove;
typedef struct _DejaDupRecursiveMoveClass DejaDupRecursiveMoveClass;
typedef struct _DejaDupRecursiveMovePrivate DejaDupRecursiveMovePrivate;
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _DejaDupRecursiveOp {
	GObject parent_instance;
	DejaDupRecursiveOpPrivate * priv;
	GFileType src_type;
	GFileType dst_type;
};

struct _DejaDupRecursiveOpClass {
	GObjectClass parent_class;
	void (*handle_file) (DejaDupRecursiveOp* self);
	void (*handle_dir) (DejaDupRecursiveOp* self);
	void (*finish_dir) (DejaDupRecursiveOp* self);
	DejaDupRecursiveOp* (*clone_for_info) (DejaDupRecursiveOp* self, GFileInfo* info);
};

struct _DejaDupRecursiveMove {
	DejaDupRecursiveOp parent_instance;
	DejaDupRecursiveMovePrivate * priv;
};

struct _DejaDupRecursiveMoveClass {
	DejaDupRecursiveOpClass parent_class;
};


static gpointer deja_dup_recursive_move_parent_class = NULL;

GType deja_dup_recursive_op_get_type (void);
GType deja_dup_recursive_move_get_type (void);
enum  {
	DEJA_DUP_RECURSIVE_MOVE_DUMMY_PROPERTY
};
DejaDupRecursiveMove* deja_dup_recursive_move_new (GFile* source, GFile* dest);
DejaDupRecursiveMove* deja_dup_recursive_move_construct (GType object_type, GFile* source, GFile* dest);
static void deja_dup_recursive_move_progress_callback (DejaDupRecursiveMove* self, gint64 current_num_bytes, gint64 total_num_bytes);
GFile* deja_dup_recursive_op_get_dst (DejaDupRecursiveOp* self);
GFile* deja_dup_recursive_op_get_src (DejaDupRecursiveOp* self);
static void _deja_dup_recursive_move_progress_callback_gfile_progress_callback (gint64 current_num_bytes, gint64 total_num_bytes, gpointer self);
static void deja_dup_recursive_move_real_handle_file (DejaDupRecursiveOp* base);
static void deja_dup_recursive_move_real_handle_dir (DejaDupRecursiveOp* base);
static void deja_dup_recursive_move_real_finish_dir (DejaDupRecursiveOp* base);
static DejaDupRecursiveOp* deja_dup_recursive_move_real_clone_for_info (DejaDupRecursiveOp* base, GFileInfo* info);



DejaDupRecursiveMove* deja_dup_recursive_move_construct (GType object_type, GFile* source, GFile* dest) {
	DejaDupRecursiveMove * self;
	g_return_val_if_fail (source != NULL, NULL);
	g_return_val_if_fail (dest != NULL, NULL);
	self = (DejaDupRecursiveMove*) g_object_new (object_type, "src", source, "dst", dest, NULL);
	return self;
}


DejaDupRecursiveMove* deja_dup_recursive_move_new (GFile* source, GFile* dest) {
	return deja_dup_recursive_move_construct (DEJA_DUP_TYPE_RECURSIVE_MOVE, source, dest);
}


static void deja_dup_recursive_move_progress_callback (DejaDupRecursiveMove* self, gint64 current_num_bytes, gint64 total_num_bytes) {
	g_return_if_fail (self != NULL);
}


static void _deja_dup_recursive_move_progress_callback_gfile_progress_callback (gint64 current_num_bytes, gint64 total_num_bytes, gpointer self) {
	deja_dup_recursive_move_progress_callback (self, current_num_bytes, total_num_bytes);
}


static void deja_dup_recursive_move_real_handle_file (DejaDupRecursiveOp* base) {
	DejaDupRecursiveMove * self;
	GError * _inner_error_;
	self = (DejaDupRecursiveMove*) base;
	_inner_error_ = NULL;
	if (((DejaDupRecursiveOp*) self)->dst_type == G_FILE_TYPE_DIRECTORY) {
		{
			g_file_delete (deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), NULL, &_inner_error_);
			if (_inner_error_ != NULL) {
				goto __catch44_g_error;
			}
		}
		goto __finally44;
		__catch44_g_error:
		{
			GError * e;
			e = _inner_error_;
			_inner_error_ = NULL;
			{
				g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), e->message);
				_g_error_free0 (e);
				return;
			}
		}
		__finally44:
		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;
		}
	}
	{
		g_file_move (deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), (G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS) | G_FILE_COPY_OVERWRITE, NULL, _deja_dup_recursive_move_progress_callback_gfile_progress_callback, self, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (g_error_matches (_inner_error_, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED)) {
				goto __catch45_g_io_error_permission_denied;
			}
			goto __catch45_g_error;
		}
	}
	goto __finally45;
	__catch45_g_io_error_permission_denied:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			{
				g_file_copy (deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), (G_FILE_COPY_ALL_METADATA | G_FILE_COPY_NOFOLLOW_SYMLINKS) | G_FILE_COPY_OVERWRITE, NULL, _deja_dup_recursive_move_progress_callback_gfile_progress_callback, self, &_inner_error_);
				if (_inner_error_ != NULL) {
					goto __catch46_g_error;
				}
			}
			goto __finally46;
			__catch46_g_error:
			{
				GError * e;
				e = _inner_error_;
				_inner_error_ = NULL;
				{
					g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), e->message);
					_g_error_free0 (e);
				}
			}
			__finally46:
			if (_inner_error_ != NULL) {
				_g_error_free0 (e);
				_g_error_free0 (e);
				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;
			}
			_g_error_free0 (e);
		}
	}
	goto __finally45;
	__catch45_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), e->message);
			_g_error_free0 (e);
		}
	}
	__finally45:
	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;
	}
}


static void deja_dup_recursive_move_real_handle_dir (DejaDupRecursiveOp* base) {
	DejaDupRecursiveMove * self;
	GError * _inner_error_;
	gboolean _tmp0_ = FALSE;
	self = (DejaDupRecursiveMove*) base;
	_inner_error_ = NULL;
	if (((DejaDupRecursiveOp*) self)->dst_type != G_FILE_TYPE_UNKNOWN) {
		_tmp0_ = ((DejaDupRecursiveOp*) self)->dst_type != G_FILE_TYPE_DIRECTORY;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		{
			g_file_delete (deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), NULL, &_inner_error_);
			if (_inner_error_ != NULL) {
				goto __catch47_g_error;
			}
		}
		goto __finally47;
		__catch47_g_error:
		{
			GError * e;
			e = _inner_error_;
			_inner_error_ = NULL;
			{
				g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), e->message);
				_g_error_free0 (e);
				return;
			}
		}
		__finally47:
		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;
		}
		((DejaDupRecursiveOp*) self)->dst_type = G_FILE_TYPE_UNKNOWN;
	}
	if (((DejaDupRecursiveOp*) self)->dst_type == G_FILE_TYPE_UNKNOWN) {
		{
			g_file_make_directory (deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), NULL, &_inner_error_);
			if (_inner_error_ != NULL) {
				goto __catch48_g_error;
			}
		}
		goto __finally48;
		__catch48_g_error:
		{
			GError * e;
			e = _inner_error_;
			_inner_error_ = NULL;
			{
				g_signal_emit_by_name ((DejaDupRecursiveOp*) self, "raise-error", deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), e->message);
				_g_error_free0 (e);
				return;
			}
		}
		__finally48:
		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;
		}
	}
}


static void deja_dup_recursive_move_real_finish_dir (DejaDupRecursiveOp* base) {
	DejaDupRecursiveMove * self;
	GError * _inner_error_;
	self = (DejaDupRecursiveMove*) base;
	_inner_error_ = NULL;
	{
		g_file_copy_attributes (deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA, NULL, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch49_g_error;
		}
	}
	goto __finally49;
	__catch49_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			_g_error_free0 (e);
		}
	}
	__finally49:
	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;
	}
	{
		g_file_delete (deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), NULL, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch50_g_error;
		}
	}
	goto __finally50;
	__catch50_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			_g_error_free0 (e);
		}
	}
	__finally50:
	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;
	}
}


static DejaDupRecursiveOp* deja_dup_recursive_move_real_clone_for_info (DejaDupRecursiveOp* base, GFileInfo* info) {
	DejaDupRecursiveMove * self;
	DejaDupRecursiveOp* result = NULL;
	char* child_name;
	GFile* src_child;
	GFile* dst_child;
	self = (DejaDupRecursiveMove*) base;
	g_return_val_if_fail (info != NULL, NULL);
	child_name = g_strdup (g_file_info_get_name (info));
	src_child = g_file_get_child (deja_dup_recursive_op_get_src ((DejaDupRecursiveOp*) self), child_name);
	dst_child = g_file_get_child (deja_dup_recursive_op_get_dst ((DejaDupRecursiveOp*) self), child_name);
	result = (DejaDupRecursiveOp*) deja_dup_recursive_move_new (src_child, dst_child);
	_g_free0 (child_name);
	_g_object_unref0 (src_child);
	_g_object_unref0 (dst_child);
	return result;
}


static void deja_dup_recursive_move_class_init (DejaDupRecursiveMoveClass * klass) {
	deja_dup_recursive_move_parent_class = g_type_class_peek_parent (klass);
	DEJA_DUP_RECURSIVE_OP_CLASS (klass)->handle_file = deja_dup_recursive_move_real_handle_file;
	DEJA_DUP_RECURSIVE_OP_CLASS (klass)->handle_dir = deja_dup_recursive_move_real_handle_dir;
	DEJA_DUP_RECURSIVE_OP_CLASS (klass)->finish_dir = deja_dup_recursive_move_real_finish_dir;
	DEJA_DUP_RECURSIVE_OP_CLASS (klass)->clone_for_info = deja_dup_recursive_move_real_clone_for_info;
}


static void deja_dup_recursive_move_instance_init (DejaDupRecursiveMove * self) {
}


GType deja_dup_recursive_move_get_type (void) {
	static volatile gsize deja_dup_recursive_move_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_recursive_move_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupRecursiveMoveClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_recursive_move_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupRecursiveMove), 0, (GInstanceInitFunc) deja_dup_recursive_move_instance_init, NULL };
		GType deja_dup_recursive_move_type_id;
		deja_dup_recursive_move_type_id = g_type_register_static (DEJA_DUP_TYPE_RECURSIVE_OP, "DejaDupRecursiveMove", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_recursive_move_type_id__volatile, deja_dup_recursive_move_type_id);
	}
	return deja_dup_recursive_move_type_id__volatile;
}




