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

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*- */
/*
    This file is part of Déjà Dup.
    For copyright information, see AUTHORS.

    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 <stdlib.h>
#include <string.h>


#define DEJA_DUP_TYPE_ASYNC_COMMAND (deja_dup_async_command_get_type ())
#define DEJA_DUP_ASYNC_COMMAND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommand))
#define DEJA_DUP_ASYNC_COMMAND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommandClass))
#define DEJA_DUP_IS_ASYNC_COMMAND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DEJA_DUP_TYPE_ASYNC_COMMAND))
#define DEJA_DUP_IS_ASYNC_COMMAND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DEJA_DUP_TYPE_ASYNC_COMMAND))
#define DEJA_DUP_ASYNC_COMMAND_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommandClass))

typedef struct _DejaDupAsyncCommand DejaDupAsyncCommand;
typedef struct _DejaDupAsyncCommandClass DejaDupAsyncCommandClass;
typedef struct _DejaDupAsyncCommandPrivate DejaDupAsyncCommandPrivate;
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _DejaDupAsyncCommand {
	GObject parent_instance;
	DejaDupAsyncCommandPrivate * priv;
};

struct _DejaDupAsyncCommandClass {
	GObjectClass parent_class;
};

struct _DejaDupAsyncCommandPrivate {
	gchar** _argv;
	gint _argv_length1;
	gint __argv_size_;
	GPid pid;
	guint watch;
	GMainLoop* loop;
};


static gpointer deja_dup_async_command_parent_class = NULL;

GType deja_dup_async_command_get_type (void) G_GNUC_CONST;
#define DEJA_DUP_ASYNC_COMMAND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommandPrivate))
enum  {
	DEJA_DUP_ASYNC_COMMAND_DUMMY_PROPERTY,
	DEJA_DUP_ASYNC_COMMAND_ARGV
};
static void deja_dup_async_command_handle_done (DejaDupAsyncCommand* self, GPid pid, gint sig);
DejaDupAsyncCommand* deja_dup_async_command_new (gchar** argv, int argv_length1);
DejaDupAsyncCommand* deja_dup_async_command_construct (GType object_type, gchar** argv, int argv_length1);
void deja_dup_async_command_run (DejaDupAsyncCommand* self);
gchar** deja_dup_async_command_get_argv (DejaDupAsyncCommand* self, int* result_length1);
static void _deja_dup_async_command_handle_done_gchild_watch_func (GPid pid, gint status, gpointer self);
static void deja_dup_async_command_set_argv (DejaDupAsyncCommand* self, gchar** value, int value_length1);
static gchar** _vala_array_dup1 (gchar** self, int length);
static GObject * deja_dup_async_command_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void deja_dup_async_command_finalize (GObject* obj);
static void _vala_deja_dup_async_command_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_deja_dup_async_command_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);


DejaDupAsyncCommand* deja_dup_async_command_construct (GType object_type, gchar** argv, int argv_length1) {
	DejaDupAsyncCommand * self = NULL;
	gchar** _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	_tmp0_ = argv;
	_tmp0__length1 = argv_length1;
	self = (DejaDupAsyncCommand*) g_object_new (object_type, "argv", _tmp0_, NULL);
	return self;
}


DejaDupAsyncCommand* deja_dup_async_command_new (gchar** argv, int argv_length1) {
	return deja_dup_async_command_construct (DEJA_DUP_TYPE_ASYNC_COMMAND, argv, argv_length1);
}


static void _deja_dup_async_command_handle_done_gchild_watch_func (GPid pid, gint status, gpointer self) {
	deja_dup_async_command_handle_done ((DejaDupAsyncCommand*) self, pid, status);
}


void deja_dup_async_command_run (DejaDupAsyncCommand* self) {
	GPid _tmp7_ = 0;
	guint _tmp8_ = 0U;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	{
		gboolean _tmp0_ = FALSE;
		gchar** _tmp1_ = NULL;
		gint _tmp1__length1 = 0;
		gchar** _tmp2_ = NULL;
		gint _tmp2__length1 = 0;
		GPid _tmp3_ = 0;
		gboolean _tmp4_ = FALSE;
		_tmp1_ = deja_dup_async_command_get_argv (self, &_tmp1__length1);
		_tmp2_ = _tmp1_;
		_tmp2__length1 = _tmp1__length1;
		_tmp4_ = g_spawn_async (NULL, _tmp2_, NULL, ((G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL) | G_SPAWN_DO_NOT_REAP_CHILD) | G_SPAWN_SEARCH_PATH, NULL, NULL, &_tmp3_, &_inner_error_);
		self->priv->pid = _tmp3_;
		_tmp0_ = _tmp4_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch0_g_error;
		}
		if (!_tmp0_) {
			g_signal_emit_by_name (self, "done", FALSE);
			return;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp5_ = NULL;
		const gchar* _tmp6_ = NULL;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp5_ = e;
		_tmp6_ = _tmp5_->message;
		g_warning ("AsyncCommand.vala:62: %s\n", _tmp6_);
		g_signal_emit_by_name (self, "done", FALSE);
		_g_error_free0 (e);
		return;
	}
	__finally0:
	if (G_UNLIKELY (_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;
	}
	_tmp7_ = self->priv->pid;
	_tmp8_ = g_child_watch_add_full (G_PRIORITY_DEFAULT_IDLE, _tmp7_, _deja_dup_async_command_handle_done_gchild_watch_func, g_object_ref (self), g_object_unref);
	self->priv->watch = _tmp8_;
}


static void deja_dup_async_command_handle_done (DejaDupAsyncCommand* self, GPid pid, gint sig) {
	gint _tmp0_ = 0;
	GPid _tmp1_ = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = sig;
	g_signal_emit_by_name (self, "done", _tmp0_ == 0);
	_tmp1_ = pid;
	g_spawn_close_pid (_tmp1_);
	self->priv->pid = (GPid) 0;
}


gchar** deja_dup_async_command_get_argv (DejaDupAsyncCommand* self, int* result_length1) {
	gchar** result;
	gchar** _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	gchar** _tmp1_ = NULL;
	gint _tmp1__length1 = 0;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_argv;
	_tmp0__length1 = self->priv->_argv_length1;
	_tmp1_ = _tmp0_;
	_tmp1__length1 = _tmp0__length1;
	if (result_length1) {
		*result_length1 = _tmp1__length1;
	}
	result = _tmp1_;
	return result;
}


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


static void deja_dup_async_command_set_argv (DejaDupAsyncCommand* self, gchar** value, int value_length1) {
	gchar** _tmp0_ = NULL;
	gint _tmp0__length1 = 0;
	gchar** _tmp1_ = NULL;
	gint _tmp1__length1 = 0;
	g_return_if_fail (self != NULL);
	_tmp0_ = value;
	_tmp0__length1 = value_length1;
	_tmp1_ = (_tmp0_ != NULL) ? _vala_array_dup1 (_tmp0_, _tmp0__length1) : ((gpointer) _tmp0_);
	_tmp1__length1 = _tmp0__length1;
	self->priv->_argv = (_vala_array_free (self->priv->_argv, self->priv->_argv_length1, (GDestroyNotify) g_free), NULL);
	self->priv->_argv = _tmp1_;
	self->priv->_argv_length1 = _tmp1__length1;
	self->priv->__argv_size_ = self->priv->_argv_length1;
	g_object_notify ((GObject *) self, "argv");
}


static GObject * deja_dup_async_command_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DejaDupAsyncCommand * self;
	GMainLoop* _tmp0_ = NULL;
	parent_class = G_OBJECT_CLASS (deja_dup_async_command_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommand);
	_tmp0_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (self->priv->loop);
	self->priv->loop = _tmp0_;
	return obj;
}


static void deja_dup_async_command_class_init (DejaDupAsyncCommandClass * klass) {
	deja_dup_async_command_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (DejaDupAsyncCommandPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_deja_dup_async_command_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_deja_dup_async_command_set_property;
	G_OBJECT_CLASS (klass)->constructor = deja_dup_async_command_constructor;
	G_OBJECT_CLASS (klass)->finalize = deja_dup_async_command_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DEJA_DUP_ASYNC_COMMAND_ARGV, g_param_spec_boxed ("argv", "argv", "argv", G_TYPE_STRV, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	g_signal_new ("done", DEJA_DUP_TYPE_ASYNC_COMMAND, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN, G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
}


static void deja_dup_async_command_instance_init (DejaDupAsyncCommand * self) {
	self->priv = DEJA_DUP_ASYNC_COMMAND_GET_PRIVATE (self);
	self->priv->pid = (GPid) 0;
	self->priv->watch = (guint) 0;
}


static void deja_dup_async_command_finalize (GObject* obj) {
	DejaDupAsyncCommand * self;
	guint _tmp0_ = 0U;
	GPid _tmp2_ = 0;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommand);
	_tmp0_ = self->priv->watch;
	if (_tmp0_ > ((guint) 0)) {
		guint _tmp1_ = 0U;
		_tmp1_ = self->priv->watch;
		g_source_remove (_tmp1_);
	}
	_tmp2_ = self->priv->pid;
	if (_tmp2_ > ((GPid) 0)) {
		GPid _tmp3_ = 0;
		_tmp3_ = self->priv->pid;
		deja_dup_async_command_handle_done (self, _tmp3_, 1);
	}
	self->priv->_argv = (_vala_array_free (self->priv->_argv, self->priv->_argv_length1, (GDestroyNotify) g_free), NULL);
	_g_main_loop_unref0 (self->priv->loop);
	G_OBJECT_CLASS (deja_dup_async_command_parent_class)->finalize (obj);
}


GType deja_dup_async_command_get_type (void) {
	static volatile gsize deja_dup_async_command_type_id__volatile = 0;
	if (g_once_init_enter (&deja_dup_async_command_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DejaDupAsyncCommandClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_async_command_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupAsyncCommand), 0, (GInstanceInitFunc) deja_dup_async_command_instance_init, NULL };
		GType deja_dup_async_command_type_id;
		deja_dup_async_command_type_id = g_type_register_static (G_TYPE_OBJECT, "DejaDupAsyncCommand", &g_define_type_info, 0);
		g_once_init_leave (&deja_dup_async_command_type_id__volatile, deja_dup_async_command_type_id);
	}
	return deja_dup_async_command_type_id__volatile;
}


static void _vala_deja_dup_async_command_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	DejaDupAsyncCommand * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommand);
	switch (property_id) {
		case DEJA_DUP_ASYNC_COMMAND_ARGV:
		{
			int length;
			g_value_set_boxed (value, deja_dup_async_command_get_argv (self, &length));
		}
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_deja_dup_async_command_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	DejaDupAsyncCommand * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, DEJA_DUP_TYPE_ASYNC_COMMAND, DejaDupAsyncCommand);
	switch (property_id) {
		case DEJA_DUP_ASYNC_COMMAND_ARGV:
		{
			gpointer boxed;
			boxed = g_value_get_boxed (value);
			deja_dup_async_command_set_argv (self, boxed, (boxed == NULL) ? 0 : g_strv_length (boxed));
		}
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int 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, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



