/* AsyncCommand.c generated by valac 0.12.1, 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;
	self = (DejaDupAsyncCommand*) g_object_new (object_type, "argv", argv, 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 (self, pid, status);
}


void deja_dup_async_command_run (DejaDupAsyncCommand* self) {
	gchar** _tmp0_ = NULL;
	gint _tmp1_;
	GPid _tmp2_;
	gboolean _tmp3_;
	gboolean _tmp4_;
	guint _tmp5_;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = deja_dup_async_command_get_argv (self, &_tmp1_);
	_tmp3_ = g_spawn_async (NULL, _tmp0_, 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, &_tmp2_, &_inner_error_);
	self->priv->pid = _tmp2_;
	_tmp4_ = _tmp3_;
	if (_inner_error_ != NULL) {
		goto __catch0_g_error;
	}
	if (!_tmp4_) {
		g_signal_emit_by_name (self, "done", FALSE);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		g_warning ("AsyncCommand.vala:60: %s\n", e->message);
		g_signal_emit_by_name (self, "done", FALSE);
		_g_error_free0 (e);
	}
	__finally0:
	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;
	}
	_tmp5_ = g_child_watch_add_full (G_PRIORITY_DEFAULT_IDLE, self->priv->pid, _deja_dup_async_command_handle_done_gchild_watch_func, g_object_ref (self), g_object_unref);
	self->priv->watch = _tmp5_;
}


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


gchar** deja_dup_async_command_get_argv (DejaDupAsyncCommand* self, int* result_length1) {
	gchar** result;
	gchar** _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_argv;
	if (result_length1) {
		*result_length1 = self->priv->_argv_length1;
	}
	result = _tmp0_;
	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++) {
		result[i] = g_strdup (self[i]);
	}
	return result;
}


static void deja_dup_async_command_set_argv (DejaDupAsyncCommand* self, gchar** value, int value_length1) {
	gchar** _tmp0_;
	gchar** _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = (_tmp1_ = value, (_tmp1_ == NULL) ? ((gpointer) _tmp1_) : _vala_array_dup1 (_tmp1_, value_length1));
	self->priv->_argv = (_vala_array_free (self->priv->_argv, self->priv->_argv_length1, (GDestroyNotify) g_free), NULL);
	self->priv->_argv = _tmp0_;
	self->priv->_argv_length1 = value_length1;
	self->priv->__argv_size_ = value_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 = DEJA_DUP_ASYNC_COMMAND (obj);
	_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;
	self = DEJA_DUP_ASYNC_COMMAND (obj);
	if (self->priv->watch > 0) {
		g_source_remove (self->priv->watch);
	}
	if (self->priv->pid > 0) {
		deja_dup_async_command_handle_done (self, self->priv->pid, 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;
	int length;
	self = DEJA_DUP_ASYNC_COMMAND (object);
	switch (property_id) {
		case DEJA_DUP_ASYNC_COMMAND_ARGV:
		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;
	gpointer boxed;
	self = DEJA_DUP_ASYNC_COMMAND (object);
	switch (property_id) {
		case DEJA_DUP_ASYNC_COMMAND_ARGV:
		boxed = g_value_get_boxed (value);
		deja_dup_async_command_set_argv (self, boxed, 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);
}



