/* cli-post-processor.c generated by valac 0.56.17, the Vala compiler
 * generated from cli-post-processor.vala, do not modify */

/*
Peek Copyright (c) 2017 by Philipp Wolfer <ph.wolfer@gmail.com>

This file is part of Peek.

This software is licensed under the GNU General Public License
(version 3 or later). See the LICENSE file in this distribution.
*/

#include "../../application.h"
#include <gio/gio.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <glib-object.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

enum  {
	PEEK_POST_PROCESSING_CLI_POST_PROCESSOR_0_PROPERTY,
	PEEK_POST_PROCESSING_CLI_POST_PROCESSOR_NUM_PROPERTIES
};
static GParamSpec* peek_post_processing_cli_post_processor_properties[PEEK_POST_PROCESSING_CLI_POST_PROCESSOR_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _PeekPostProcessingCliPostProcessorSpawnCommandAsyncData PeekPostProcessingCliPostProcessorSpawnCommandAsyncData;

struct _PeekPostProcessingCliPostProcessorPrivate {
	GSubprocess* subprocess;
};

struct _PeekPostProcessingCliPostProcessorSpawnCommandAsyncData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	PeekPostProcessingCliPostProcessor* self;
	gchar** argv;
	gint argv_length1;
	gint result;
	GSubprocess* _tmp0_;
	GSubprocess* _tmp1_;
	GSubprocess* _tmp2_;
	GSubprocess* _tmp3_;
	GError* e;
	FILE* _tmp4_;
	GError* _tmp5_;
	const gchar* _tmp6_;
	gchar* message;
	GSubprocess* _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	GError* _tmp10_;
	gint status;
	GSubprocess* _tmp11_;
	gchar* _vala1_message;
	GSubprocess* _tmp12_;
	gchar* _tmp13_;
	const gchar* _tmp14_;
	GError* _tmp15_;
	GError* _inner_error0_;
};

static gint PeekPostProcessingCliPostProcessor_private_offset;
static gpointer peek_post_processing_cli_post_processor_parent_class = NULL;
static PeekPostProcessingPostProcessorIface * peek_post_processing_cli_post_processor_peek_post_processing_post_processor_parent_iface = NULL;

static void peek_post_processing_cli_post_processor_real_cancel (PeekPostProcessingPostProcessor* base);
static void peek_post_processing_cli_post_processor_spawn_command_async_data_free (gpointer _data);
static gboolean peek_post_processing_cli_post_processor_spawn_command_async_co (PeekPostProcessingCliPostProcessorSpawnCommandAsyncData* _data_);
static void peek_post_processing_cli_post_processor_spawn_command_async_ready (GObject* source_object,
                                                                        GAsyncResult* _res_,
                                                                        gpointer _user_data_);
static void peek_post_processing_cli_post_processor_finalize (GObject * obj);
static GType peek_post_processing_cli_post_processor_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
peek_post_processing_cli_post_processor_get_instance_private (PeekPostProcessingCliPostProcessor* self)
{
	return G_STRUCT_MEMBER_P (self, PeekPostProcessingCliPostProcessor_private_offset);
}

void
peek_post_processing_cli_post_processor_process_async (PeekPostProcessingCliPostProcessor* self,
                                                       GArray* files,
                                                       GAsyncReadyCallback _callback_,
                                                       gpointer _user_data_)
{
	PeekPostProcessingCliPostProcessorClass* _klass_;
	_klass_ = PEEK_POST_PROCESSING_CLI_POST_PROCESSOR_GET_CLASS (self);
	if (_klass_->process_async) {
		_klass_->process_async (self, files, _callback_, _user_data_);
	}
}

GArray*
peek_post_processing_cli_post_processor_process_finish (PeekPostProcessingCliPostProcessor* self,
                                                        GAsyncResult* _res_,
                                                        GError** error)
{
	PeekPostProcessingCliPostProcessorClass* _klass_;
	_klass_ = PEEK_POST_PROCESSING_CLI_POST_PROCESSOR_GET_CLASS (self);
	if (_klass_->process_finish) {
		return _klass_->process_finish (self, _res_, error);
	}
	return NULL;
}

static void
peek_post_processing_cli_post_processor_real_cancel (PeekPostProcessingPostProcessor* base)
{
	PeekPostProcessingCliPostProcessor * self;
	GSubprocess* _tmp0_;
	self = (PeekPostProcessingCliPostProcessor*) base;
	_tmp0_ = self->priv->subprocess;
	if (_tmp0_ != NULL) {
		GSubprocess* _tmp1_;
		_tmp1_ = self->priv->subprocess;
		g_subprocess_force_exit (_tmp1_);
	}
}

static void
peek_post_processing_cli_post_processor_spawn_command_async_data_free (gpointer _data)
{
	PeekPostProcessingCliPostProcessorSpawnCommandAsyncData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (PeekPostProcessingCliPostProcessorSpawnCommandAsyncData, _data_);
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

void
peek_post_processing_cli_post_processor_spawn_command_async (PeekPostProcessingCliPostProcessor* self,
                                                             gchar** argv,
                                                             gint argv_length1,
                                                             GAsyncReadyCallback _callback_,
                                                             gpointer _user_data_)
{
	PeekPostProcessingCliPostProcessorSpawnCommandAsyncData* _data_;
	PeekPostProcessingCliPostProcessor* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (PeekPostProcessingCliPostProcessorSpawnCommandAsyncData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, peek_post_processing_cli_post_processor_spawn_command_async_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_data_->argv = argv;
	_data_->argv_length1 = argv_length1;
	peek_post_processing_cli_post_processor_spawn_command_async_co (_data_);
}

gint
peek_post_processing_cli_post_processor_spawn_command_finish (PeekPostProcessingCliPostProcessor* self,
                                                              GAsyncResult* _res_,
                                                              GError** error)
{
	gint result;
	PeekPostProcessingCliPostProcessorSpawnCommandAsyncData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		gint _tmp0_ = 0;
		return _tmp0_;
	}
	result = _data_->result;
	return result;
}

static void
peek_post_processing_cli_post_processor_spawn_command_async_ready (GObject* source_object,
                                                                   GAsyncResult* _res_,
                                                                   gpointer _user_data_)
{
	PeekPostProcessingCliPostProcessorSpawnCommandAsyncData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	peek_post_processing_cli_post_processor_spawn_command_async_co (_data_);
}

static gboolean
peek_post_processing_cli_post_processor_spawn_command_async_co (PeekPostProcessingCliPostProcessorSpawnCommandAsyncData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_tmp1_ = g_subprocess_newv (_data_->argv, G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_MERGE, &_data_->_inner_error0_);
		_data_->_tmp0_ = _data_->_tmp1_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp2_ = _data_->_tmp0_;
		_data_->_tmp0_ = NULL;
		_g_object_unref0 (_data_->self->priv->subprocess);
		_data_->self->priv->subprocess = _data_->_tmp2_;
		_data_->_tmp3_ = _data_->self->priv->subprocess;
		_data_->_state_ = 1;
		g_subprocess_wait_async (_data_->_tmp3_, NULL, peek_post_processing_cli_post_processor_spawn_command_async_ready, _data_);
		return FALSE;
		_state_1:
		g_subprocess_wait_finish (_data_->_tmp3_, _data_->_res_, &_data_->_inner_error0_);
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_object_unref0 (_data_->_tmp0_);
			goto __catch0_g_error;
		}
		_g_object_unref0 (_data_->_tmp0_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		_data_->e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp4_ = stderr;
		_data_->_tmp5_ = _data_->e;
		_data_->_tmp6_ = _data_->_tmp5_->message;
		fprintf (_data_->_tmp4_, "Error: %s\n", _data_->_tmp6_);
		_data_->_tmp7_ = _data_->self->priv->subprocess;
		_data_->_tmp8_ = peek_utils_get_command_failed_message (_data_->argv, (gint) _data_->argv_length1, _data_->_tmp7_);
		_data_->message = _data_->_tmp8_;
		_data_->_tmp9_ = _data_->message;
		_data_->_tmp10_ = g_error_new_literal (PEEK_RECORDING_ERROR, PEEK_RECORDING_ERROR_POSTPROCESSING_ABORTED, _data_->_tmp9_);
		_data_->_inner_error0_ = _data_->_tmp10_;
		_g_free0 (_data_->message);
		_g_error_free0 (_data_->e);
		goto __finally0;
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		if (_data_->_inner_error0_->domain == PEEK_RECORDING_ERROR) {
			g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
			g_object_unref (_data_->_async_result);
			return FALSE;
		} else {
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
			g_clear_error (&_data_->_inner_error0_);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
	}
	_data_->_tmp11_ = _data_->self->priv->subprocess;
	_data_->status = g_subprocess_get_status (_data_->_tmp11_);
	if (!peek_utils_is_exit_status_success (_data_->status)) {
		_data_->_tmp12_ = _data_->self->priv->subprocess;
		_data_->_tmp13_ = peek_utils_get_command_failed_message (_data_->argv, (gint) _data_->argv_length1, _data_->_tmp12_);
		_data_->_vala1_message = _data_->_tmp13_;
		_g_object_unref0 (_data_->self->priv->subprocess);
		_data_->self->priv->subprocess = NULL;
		_data_->_tmp14_ = _data_->_vala1_message;
		_data_->_tmp15_ = g_error_new_literal (PEEK_RECORDING_ERROR, PEEK_RECORDING_ERROR_POSTPROCESSING_ABORTED, _data_->_tmp14_);
		_data_->_inner_error0_ = _data_->_tmp15_;
		if (_data_->_inner_error0_->domain == PEEK_RECORDING_ERROR) {
			g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
			_g_free0 (_data_->_vala1_message);
			g_object_unref (_data_->_async_result);
			return FALSE;
		} else {
			_g_free0 (_data_->_vala1_message);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
			g_clear_error (&_data_->_inner_error0_);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
		_g_free0 (_data_->_vala1_message);
	}
	_g_object_unref0 (_data_->self->priv->subprocess);
	_data_->self->priv->subprocess = NULL;
	_data_->result = _data_->status;
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

PeekPostProcessingCliPostProcessor*
peek_post_processing_cli_post_processor_construct (GType object_type)
{
	PeekPostProcessingCliPostProcessor * self = NULL;
	self = (PeekPostProcessingCliPostProcessor*) g_object_new (object_type, NULL);
	return self;
}

static void
peek_post_processing_cli_post_processor_class_init (PeekPostProcessingCliPostProcessorClass * klass,
                                                    gpointer klass_data)
{
	peek_post_processing_cli_post_processor_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PeekPostProcessingCliPostProcessor_private_offset);
	G_OBJECT_CLASS (klass)->finalize = peek_post_processing_cli_post_processor_finalize;
}

static void
peek_post_processing_cli_post_processor_peek_post_processing_post_processor_interface_init (PeekPostProcessingPostProcessorIface * iface,
                                                                                            gpointer iface_data)
{
	peek_post_processing_cli_post_processor_peek_post_processing_post_processor_parent_iface = g_type_interface_peek_parent (iface);
	iface->process_async = (void (*) (PeekPostProcessingPostProcessor*, GArray*, GAsyncReadyCallback, gpointer)) peek_post_processing_cli_post_processor_process_async;
	iface->process_finish = (GArray* (*) (PeekPostProcessingPostProcessor*, GAsyncResult*, GError**)) peek_post_processing_cli_post_processor_process_finish;
	iface->cancel = (void (*) (PeekPostProcessingPostProcessor*)) peek_post_processing_cli_post_processor_real_cancel;
}

static void
peek_post_processing_cli_post_processor_instance_init (PeekPostProcessingCliPostProcessor * self,
                                                       gpointer klass)
{
	self->priv = peek_post_processing_cli_post_processor_get_instance_private (self);
}

static void
peek_post_processing_cli_post_processor_finalize (GObject * obj)
{
	PeekPostProcessingCliPostProcessor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PEEK_POST_PROCESSING_TYPE_CLI_POST_PROCESSOR, PeekPostProcessingCliPostProcessor);
	_g_object_unref0 (self->priv->subprocess);
	G_OBJECT_CLASS (peek_post_processing_cli_post_processor_parent_class)->finalize (obj);
}

/**
  * Common base class for post processors calling external CLI tools.
  */
static GType
peek_post_processing_cli_post_processor_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PeekPostProcessingCliPostProcessorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) peek_post_processing_cli_post_processor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PeekPostProcessingCliPostProcessor), 0, (GInstanceInitFunc) peek_post_processing_cli_post_processor_instance_init, NULL };
	static const GInterfaceInfo peek_post_processing_post_processor_info = { (GInterfaceInitFunc) peek_post_processing_cli_post_processor_peek_post_processing_post_processor_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType peek_post_processing_cli_post_processor_type_id;
	peek_post_processing_cli_post_processor_type_id = g_type_register_static (G_TYPE_OBJECT, "PeekPostProcessingCliPostProcessor", &g_define_type_info, G_TYPE_FLAG_ABSTRACT);
	g_type_add_interface_static (peek_post_processing_cli_post_processor_type_id, PEEK_POST_PROCESSING_TYPE_POST_PROCESSOR, &peek_post_processing_post_processor_info);
	PeekPostProcessingCliPostProcessor_private_offset = g_type_add_instance_private (peek_post_processing_cli_post_processor_type_id, sizeof (PeekPostProcessingCliPostProcessorPrivate));
	return peek_post_processing_cli_post_processor_type_id;
}

GType
peek_post_processing_cli_post_processor_get_type (void)
{
	static volatile gsize peek_post_processing_cli_post_processor_type_id__once = 0;
	if (g_once_init_enter (&peek_post_processing_cli_post_processor_type_id__once)) {
		GType peek_post_processing_cli_post_processor_type_id;
		peek_post_processing_cli_post_processor_type_id = peek_post_processing_cli_post_processor_get_type_once ();
		g_once_init_leave (&peek_post_processing_cli_post_processor_type_id__once, peek_post_processing_cli_post_processor_type_id);
	}
	return peek_post_processing_cli_post_processor_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

