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

/*
Peek Copyright (c) 2017-2018 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 <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gstdio.h>
#include <stdio.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_GIFSKI_POST_PROCESSOR_0_PROPERTY,
	PEEK_POST_PROCESSING_GIFSKI_POST_PROCESSOR_NUM_PROPERTIES
};
static GParamSpec* peek_post_processing_gifski_post_processor_properties[PEEK_POST_PROCESSING_GIFSKI_POST_PROCESSOR_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_array_unref0(var) ((var == NULL) ? NULL : (var = (g_array_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 _PeekPostProcessingGifskiPostProcessorProcessAsyncData PeekPostProcessingGifskiPostProcessorProcessAsyncData;

struct _PeekPostProcessingGifskiPostProcessorPrivate {
	PeekRecordingRecordingConfig* config;
};

struct _PeekPostProcessingGifskiPostProcessorProcessAsyncData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	PeekPostProcessingGifskiPostProcessor* self;
	GArray* files;
	GArray* result;
	gchar* extension;
	gchar* _tmp0_;
	gchar* output_file;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	const gchar* _tmp3_;
	PeekRecordingRecordingConfig* _tmp4_;
	gint _tmp5_;
	gint _tmp6_;
	GArray* argv;
	GArray* _tmp7_;
	GArray* _tmp8_;
	gchar* _tmp9_;
	GArray* _tmp10_;
	gchar* _tmp11_;
	GArray* _tmp12_;
	PeekRecordingRecordingConfig* _tmp13_;
	gint _tmp14_;
	gint _tmp15_;
	gchar* _tmp16_;
	GArray* _tmp17_;
	gchar* _tmp18_;
	GArray* _tmp19_;
	PeekRecordingRecordingConfig* _tmp20_;
	gint _tmp21_;
	gint _tmp22_;
	gchar* _tmp23_;
	GArray* _tmp24_;
	gchar* _tmp25_;
	GArray* _tmp26_;
	const gchar* _tmp27_;
	gchar* _tmp28_;
	gpointer* _tmp29_;
	gint _tmp29__length1;
	GFile** file_collection;
	gint file_collection_length1;
	gint _file_collection_size_;
	gint file_it;
	GFile* _tmp30_;
	GFile* file;
	GArray* _tmp31_;
	GFile* _tmp32_;
	gchar* _tmp33_;
	GArray* _tmp34_;
	gpointer* _tmp35_;
	gint _tmp35__length1;
	GError* e;
	const gchar* _tmp36_;
	GError* _tmp37_;
	GError* _tmp38_;
	GArray* _result_;
	GArray* _tmp39_;
	GArray* _tmp40_;
	const gchar* _tmp41_;
	GFile* _tmp42_;
	GError* _vala1_e;
	FILE* _tmp43_;
	GError* _tmp44_;
	const gchar* _tmp45_;
	GError* _tmp46_;
	const gchar* _tmp47_;
	GError* _tmp48_;
	GError* _inner_error0_;
};

static gint PeekPostProcessingGifskiPostProcessor_private_offset;
static gpointer peek_post_processing_gifski_post_processor_parent_class = NULL;

static void peek_post_processing_gifski_post_processor_real_process_async_data_free (gpointer _data);
static void peek_post_processing_gifski_post_processor_real_process_async (PeekPostProcessingCliPostProcessor* base,
                                                                    GArray* files,
                                                                    GAsyncReadyCallback _callback_,
                                                                    gpointer _user_data_);
static gboolean peek_post_processing_gifski_post_processor_real_process_async_co (PeekPostProcessingGifskiPostProcessorProcessAsyncData* _data_);
static void _g_free0_ (gpointer var);
static void _vala_gchar_free_function_content_of (gpointer data);
static void peek_post_processing_gifski_post_processor_process_async_ready (GObject* source_object,
                                                                     GAsyncResult* _res_,
                                                                     gpointer _user_data_);
static void _g_object_unref0_ (gpointer var);
static void _vala_GFile_free_function_content_of (gpointer data);
static void peek_post_processing_gifski_post_processor_finalize (GObject * obj);
static GType peek_post_processing_gifski_post_processor_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
peek_post_processing_gifski_post_processor_get_instance_private (PeekPostProcessingGifskiPostProcessor* self)
{
	return G_STRUCT_MEMBER_P (self, PeekPostProcessingGifskiPostProcessor_private_offset);
}

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

PeekPostProcessingGifskiPostProcessor*
peek_post_processing_gifski_post_processor_construct (GType object_type,
                                                      PeekRecordingRecordingConfig* config)
{
	PeekPostProcessingGifskiPostProcessor * self = NULL;
	PeekRecordingRecordingConfig* _tmp0_;
	g_return_val_if_fail (config != NULL, NULL);
	self = (PeekPostProcessingGifskiPostProcessor*) peek_post_processing_cli_post_processor_construct (object_type);
	_tmp0_ = _g_object_ref0 (config);
	_g_object_unref0 (self->priv->config);
	self->priv->config = _tmp0_;
	return self;
}

PeekPostProcessingGifskiPostProcessor*
peek_post_processing_gifski_post_processor_new (PeekRecordingRecordingConfig* config)
{
	return peek_post_processing_gifski_post_processor_construct (PEEK_POST_PROCESSING_TYPE_GIFSKI_POST_PROCESSOR, config);
}

static void
peek_post_processing_gifski_post_processor_real_process_async_data_free (gpointer _data)
{
	PeekPostProcessingGifskiPostProcessorProcessAsyncData* _data_;
	_data_ = _data;
	_g_array_unref0 (_data_->files);
	_g_array_unref0 (_data_->result);
	_g_object_unref0 (_data_->self);
	g_slice_free (PeekPostProcessingGifskiPostProcessorProcessAsyncData, _data_);
}

static gpointer
_g_array_ref0 (gpointer self)
{
	return self ? g_array_ref (self) : NULL;
}

static void
peek_post_processing_gifski_post_processor_real_process_async (PeekPostProcessingCliPostProcessor* base,
                                                               GArray* files,
                                                               GAsyncReadyCallback _callback_,
                                                               gpointer _user_data_)
{
	PeekPostProcessingGifskiPostProcessor * self;
	PeekPostProcessingGifskiPostProcessorProcessAsyncData* _data_;
	PeekPostProcessingGifskiPostProcessor* _tmp0_;
	GArray* _tmp1_;
	g_return_if_fail (files != NULL);
	self = (PeekPostProcessingGifskiPostProcessor*) base;
	_data_ = g_slice_new0 (PeekPostProcessingGifskiPostProcessorProcessAsyncData);
	_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_gifski_post_processor_real_process_async_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_array_ref0 (files);
	_g_array_unref0 (_data_->files);
	_data_->files = _tmp1_;
	peek_post_processing_gifski_post_processor_real_process_async_co (_data_);
}

static GArray*
peek_post_processing_gifski_post_processor_process_finish (PeekPostProcessingCliPostProcessor* base,
                                                           GAsyncResult* _res_,
                                                           GError** error)
{
	GArray* result;
	PeekPostProcessingGifskiPostProcessorProcessAsyncData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return NULL;
	}
	result = _data_->result;
	_data_->result = NULL;
	return result;
}

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

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

static void
peek_post_processing_gifski_post_processor_process_async_ready (GObject* source_object,
                                                                GAsyncResult* _res_,
                                                                gpointer _user_data_)
{
	PeekPostProcessingGifskiPostProcessorProcessAsyncData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	peek_post_processing_gifski_post_processor_real_process_async_co (_data_);
}

static gpointer
_g_error_copy0 (gpointer self)
{
	return self ? g_error_copy (self) : NULL;
}

static void
_g_object_unref0_ (gpointer var)
{
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}

static void
_vala_GFile_free_function_content_of (gpointer data)
{
	GFile* self;
	self = *((GFile**) data);
	_g_object_unref0_ (self);
}

static gboolean
peek_post_processing_gifski_post_processor_real_process_async_co (PeekPostProcessingGifskiPostProcessorProcessAsyncData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_tmp0_ = peek_utils_get_file_extension_for_format (PEEK_OUTPUT_FORMAT_GIF);
		_data_->extension = _data_->_tmp0_;
		_data_->_tmp1_ = _data_->extension;
		_data_->_tmp2_ = peek_utils_create_temp_file (_data_->_tmp1_, &_data_->_inner_error0_);
		_data_->output_file = _data_->_tmp2_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_free0 (_data_->extension);
			if (_data_->_inner_error0_->domain == G_FILE_ERROR) {
				goto __catch0_g_file_error;
			}
			g_critical ("file %s: line %d: unexpected 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_->_tmp3_ = _data_->output_file;
		_data_->_tmp4_ = _data_->self->priv->config;
		_data_->_tmp5_ = peek_recording_recording_config_get_gifski_quality (_data_->_tmp4_);
		_data_->_tmp6_ = _data_->_tmp5_;
		g_debug ("gifski-post-processor.vala:29: Running gifski\n" \
"    saving to: %s\n" \
"    quality: %d\n", _data_->_tmp3_, _data_->_tmp6_);
		_data_->_tmp7_ = g_array_new (TRUE, TRUE, sizeof (gchar*));
		g_array_set_clear_func (_data_->_tmp7_, (GDestroyNotify) _vala_gchar_free_function_content_of);
		_data_->argv = _data_->_tmp7_;
		_data_->_tmp8_ = _data_->argv;
		_data_->_tmp9_ = g_strdup ("gifski");
		g_array_append_val (_data_->_tmp8_, _data_->_tmp9_);
		_data_->_tmp10_ = _data_->argv;
		_data_->_tmp11_ = g_strdup ("--fps");
		g_array_append_val (_data_->_tmp10_, _data_->_tmp11_);
		_data_->_tmp12_ = _data_->argv;
		_data_->_tmp13_ = _data_->self->priv->config;
		_data_->_tmp14_ = peek_recording_recording_config_get_framerate (_data_->_tmp13_);
		_data_->_tmp15_ = _data_->_tmp14_;
		_data_->_tmp16_ = g_strdup_printf ("%i", _data_->_tmp15_);
		g_array_append_val (_data_->_tmp12_, _data_->_tmp16_);
		_data_->_tmp17_ = _data_->argv;
		_data_->_tmp18_ = g_strdup ("--quality");
		g_array_append_val (_data_->_tmp17_, _data_->_tmp18_);
		_data_->_tmp19_ = _data_->argv;
		_data_->_tmp20_ = _data_->self->priv->config;
		_data_->_tmp21_ = peek_recording_recording_config_get_gifski_quality (_data_->_tmp20_);
		_data_->_tmp22_ = _data_->_tmp21_;
		_data_->_tmp23_ = g_strdup_printf ("%i", _data_->_tmp22_);
		g_array_append_val (_data_->_tmp19_, _data_->_tmp23_);
		_data_->_tmp24_ = _data_->argv;
		_data_->_tmp25_ = g_strdup ("-o");
		g_array_append_val (_data_->_tmp24_, _data_->_tmp25_);
		_data_->_tmp26_ = _data_->argv;
		_data_->_tmp27_ = _data_->output_file;
		_data_->_tmp28_ = g_strdup (_data_->_tmp27_);
		g_array_append_val (_data_->_tmp26_, _data_->_tmp28_);
		_data_->_tmp29_ = _data_->files->data;
		_data_->_tmp29__length1 = _data_->files->len;
		{
			_data_->file_collection_length1 = 0;
			_data_->_file_collection_size_ = 0;
			_data_->file_collection = _data_->_tmp29_;
			_data_->file_collection_length1 = _data_->_tmp29__length1;
			for (_data_->file_it = 0; _data_->file_it < _data_->file_collection_length1; _data_->file_it = _data_->file_it + 1) {
				_data_->_tmp30_ = _g_object_ref0 (_data_->file_collection[_data_->file_it]);
				_data_->file = _data_->_tmp30_;
				{
					_data_->_tmp31_ = _data_->argv;
					_data_->_tmp32_ = _data_->file;
					_data_->_tmp33_ = g_file_get_path (_data_->_tmp32_);
					g_array_append_val (_data_->_tmp31_, _data_->_tmp33_);
					_g_object_unref0 (_data_->file);
				}
			}
		}
		{
			_data_->_tmp34_ = _data_->argv;
			_data_->_tmp35_ = _data_->_tmp34_->data;
			_data_->_tmp35__length1 = _data_->_tmp34_->len;
			_data_->_state_ = 1;
			peek_post_processing_cli_post_processor_spawn_command_async ((PeekPostProcessingCliPostProcessor*) _data_->self, _data_->_tmp35_, (gint) _data_->_tmp35__length1, peek_post_processing_gifski_post_processor_process_async_ready, _data_);
			return FALSE;
			_state_1:
			peek_post_processing_cli_post_processor_spawn_command_finish ((PeekPostProcessingCliPostProcessor*) _data_->self, _data_->_res_, &_data_->_inner_error0_);
			if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
				if (_data_->_inner_error0_->domain == PEEK_RECORDING_ERROR) {
					goto __catch1_peek_recording_error;
				}
				_g_array_unref0 (_data_->argv);
				_g_free0 (_data_->output_file);
				_g_free0 (_data_->extension);
				g_critical ("file %s: line %d: unexpected 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;
			}
		}
		goto __finally1;
		__catch1_peek_recording_error:
		{
			_data_->e = _data_->_inner_error0_;
			_data_->_inner_error0_ = NULL;
			_data_->_tmp36_ = _data_->output_file;
			g_remove (_data_->_tmp36_);
			_data_->_tmp37_ = _data_->e;
			_data_->_tmp38_ = _g_error_copy0 (_data_->_tmp37_);
			_data_->_inner_error0_ = _data_->_tmp38_;
			_g_error_free0 (_data_->e);
			goto __finally1;
		}
		__finally1:
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			_g_array_unref0 (_data_->argv);
			_g_free0 (_data_->output_file);
			_g_free0 (_data_->extension);
			if (_data_->_inner_error0_->domain == G_FILE_ERROR) {
				goto __catch0_g_file_error;
			}
			goto __finally0;
		}
		_data_->_tmp39_ = g_array_new (TRUE, TRUE, sizeof (GFile*));
		g_array_set_clear_func (_data_->_tmp39_, (GDestroyNotify) _vala_GFile_free_function_content_of);
		_data_->_result_ = _data_->_tmp39_;
		_data_->_tmp40_ = _data_->_result_;
		_data_->_tmp41_ = _data_->output_file;
		_data_->_tmp42_ = g_file_new_for_path (_data_->_tmp41_);
		g_array_append_val (_data_->_tmp40_, _data_->_tmp42_);
		_data_->result = _data_->_result_;
		_g_array_unref0 (_data_->argv);
		_g_free0 (_data_->output_file);
		_g_free0 (_data_->extension);
		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;
	}
	goto __finally0;
	__catch0_g_file_error:
	{
		_data_->_vala1_e = _data_->_inner_error0_;
		_data_->_inner_error0_ = NULL;
		_data_->_tmp43_ = stderr;
		_data_->_tmp44_ = _data_->_vala1_e;
		_data_->_tmp45_ = _data_->_tmp44_->message;
		fprintf (_data_->_tmp43_, "Error: %s\n", _data_->_tmp45_);
		_data_->_tmp46_ = _data_->_vala1_e;
		_data_->_tmp47_ = _data_->_tmp46_->message;
		_data_->_tmp48_ = g_error_new_literal (PEEK_RECORDING_ERROR, PEEK_RECORDING_ERROR_POSTPROCESSING_ABORTED, _data_->_tmp47_);
		_data_->_inner_error0_ = _data_->_tmp48_;
		_g_error_free0 (_data_->_vala1_e);
		goto __finally0;
	}
	__finally0:
	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;
	}
}

gboolean
peek_post_processing_gifski_post_processor_is_available (void)
{
	gboolean result;
	result = peek_utils_check_for_executable ("gifski");
	return result;
}

static void
peek_post_processing_gifski_post_processor_class_init (PeekPostProcessingGifskiPostProcessorClass * klass,
                                                       gpointer klass_data)
{
	peek_post_processing_gifski_post_processor_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PeekPostProcessingGifskiPostProcessor_private_offset);
	((PeekPostProcessingCliPostProcessorClass *) klass)->process_async = (void (*) (PeekPostProcessingCliPostProcessor*, GArray*, GAsyncReadyCallback, gpointer)) peek_post_processing_gifski_post_processor_real_process_async;
	((PeekPostProcessingCliPostProcessorClass *) klass)->process_finish = (GArray* (*) (PeekPostProcessingCliPostProcessor*, GAsyncResult*, GError**)) peek_post_processing_gifski_post_processor_process_finish;
	G_OBJECT_CLASS (klass)->finalize = peek_post_processing_gifski_post_processor_finalize;
}

static void
peek_post_processing_gifski_post_processor_instance_init (PeekPostProcessingGifskiPostProcessor * self,
                                                          gpointer klass)
{
	self->priv = peek_post_processing_gifski_post_processor_get_instance_private (self);
}

static void
peek_post_processing_gifski_post_processor_finalize (GObject * obj)
{
	PeekPostProcessingGifskiPostProcessor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PEEK_POST_PROCESSING_TYPE_GIFSKI_POST_PROCESSOR, PeekPostProcessingGifskiPostProcessor);
	_g_object_unref0 (self->priv->config);
	G_OBJECT_CLASS (peek_post_processing_gifski_post_processor_parent_class)->finalize (obj);
}

/**
  * Use gifski (https://gif.ski/) to generate an optimized GIF from a video input
  */
static GType
peek_post_processing_gifski_post_processor_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PeekPostProcessingGifskiPostProcessorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) peek_post_processing_gifski_post_processor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PeekPostProcessingGifskiPostProcessor), 0, (GInstanceInitFunc) peek_post_processing_gifski_post_processor_instance_init, NULL };
	GType peek_post_processing_gifski_post_processor_type_id;
	peek_post_processing_gifski_post_processor_type_id = g_type_register_static (PEEK_POST_PROCESSING_TYPE_CLI_POST_PROCESSOR, "PeekPostProcessingGifskiPostProcessor", &g_define_type_info, 0);
	PeekPostProcessingGifskiPostProcessor_private_offset = g_type_add_instance_private (peek_post_processing_gifski_post_processor_type_id, sizeof (PeekPostProcessingGifskiPostProcessorPrivate));
	return peek_post_processing_gifski_post_processor_type_id;
}

GType
peek_post_processing_gifski_post_processor_get_type (void)
{
	static volatile gsize peek_post_processing_gifski_post_processor_type_id__once = 0;
	if (g_once_init_enter (&peek_post_processing_gifski_post_processor_type_id__once)) {
		GType peek_post_processing_gifski_post_processor_type_id;
		peek_post_processing_gifski_post_processor_type_id = peek_post_processing_gifski_post_processor_get_type_once ();
		g_once_init_leave (&peek_post_processing_gifski_post_processor_type_id__once, peek_post_processing_gifski_post_processor_type_id);
	}
	return peek_post_processing_gifski_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;
}

