/* job-queue.c generated by valac 0.56.19, the Vala compiler
 * generated from job-queue.vala, do not modify */

/*
 * Copyright (c) 2024-2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 * Authors: Kamil Prusko <kamilprusko@gmail.com>
 */

#include "ft_core.h"
#include <gio/gio.h>
#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.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  {
	FT_JOB_QUEUE_0_PROPERTY,
	FT_JOB_QUEUE_NUM_PROPERTIES
};
static GParamSpec* ft_job_queue_properties[FT_JOB_QUEUE_NUM_PROPERTIES];
#define _g_async_queue_unref0(var) ((var == NULL) ? NULL : (var = (g_async_queue_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block19Data Block19Data;
typedef struct _Block20Data Block20Data;
typedef struct _FtJobQueueWaitData FtJobQueueWaitData;
enum  {
	FT_JOB_QUEUE_DRAINED_SIGNAL,
	FT_JOB_QUEUE_NUM_SIGNALS
};
static guint ft_job_queue_signals[FT_JOB_QUEUE_NUM_SIGNALS] = {0};
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

struct _FtJobQueuePrivate {
	GAsyncQueue* queue;
	gboolean running;
};

struct _Block19Data {
	int _ref_count_;
	FtJobQueue* self;
	FtJob* job;
};

struct _Block20Data {
	int _ref_count_;
	FtJobQueue* self;
	gulong drained_id;
	gpointer _async_data_;
};

struct _FtJobQueueWaitData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	FtJobQueue* self;
	Block20Data* _data20_;
	gboolean _tmp0_;
	GAsyncQueue* _tmp1_;
	gulong _tmp2_;
};

static gint FtJobQueue_private_offset;
static gpointer ft_job_queue_parent_class = NULL;

static GType ft_job_get_type_once (void);
static void ft_job_queue_run_job (FtJobQueue* self,
                           FtJob* job);
static Block19Data* block19_data_ref (Block19Data* _data19_);
static void block19_data_unref (void * _userdata_);
static void __lambda71_ (Block19Data* _data19_,
                  GObject* obj,
                  GAsyncResult* res);
static void ft_job_queue_pop (FtJobQueue* self,
                       gboolean may_block);
static void ___lambda71__gasync_ready_callback (GObject* source_object,
                                         GAsyncResult* res,
                                         gpointer self);
static void ft_job_queue_wait_data_free (gpointer _data);
static gboolean ft_job_queue_wait_co (FtJobQueueWaitData* _data_);
static Block20Data* block20_data_ref (Block20Data* _data20_);
static void block20_data_unref (void * _userdata_);
static void __lambda72_ (Block20Data* _data20_);
static void ___lambda72__ft_job_queue_drained (FtJobQueue* _sender,
                                        gpointer self);
static void ft_job_queue_real_dispose (GObject* base);
static GObject * ft_job_queue_constructor (GType type,
                                    guint n_construct_properties,
                                    GObjectConstructParam * construct_properties);
static void _g_object_unref0_ (gpointer var);
static void ft_job_queue_finalize (GObject * obj);
static GType ft_job_queue_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

void
ft_job_run (FtJob* self,
            GAsyncReadyCallback _callback_,
            gpointer _user_data_)
{
	FtJobIface* _iface_;
	_iface_ = FT_JOB_GET_INTERFACE (self);
	if (_iface_->run) {
		_iface_->run (self, _callback_, _user_data_);
	}
}

gboolean
ft_job_run_finish (FtJob* self,
                   GAsyncResult* _res_,
                   GError** error)
{
	FtJobIface* _iface_;
	_iface_ = FT_JOB_GET_INTERFACE (self);
	if (_iface_->run_finish) {
		return _iface_->run_finish (self, _res_, error);
	}
	return FALSE;
}

gboolean
ft_job_get_completed (FtJob* self)
{
	FtJobIface* _iface_;
	g_return_val_if_fail (self != NULL, FALSE);
	_iface_ = FT_JOB_GET_INTERFACE (self);
	if (_iface_->get_completed) {
		return _iface_->get_completed (self);
	}
	return FALSE;
}

void
ft_job_set_completed (FtJob* self,
                      gboolean value)
{
	FtJobIface* _iface_;
	g_return_if_fail (self != NULL);
	_iface_ = FT_JOB_GET_INTERFACE (self);
	if (_iface_->set_completed) {
		_iface_->set_completed (self, value);
	}
}

GError*
ft_job_get_error (FtJob* self)
{
	FtJobIface* _iface_;
	g_return_val_if_fail (self != NULL, NULL);
	_iface_ = FT_JOB_GET_INTERFACE (self);
	if (_iface_->get_error) {
		return _iface_->get_error (self);
	}
	return NULL;
}

void
ft_job_set_error (FtJob* self,
                  GError* value)
{
	FtJobIface* _iface_;
	g_return_if_fail (self != NULL);
	_iface_ = FT_JOB_GET_INTERFACE (self);
	if (_iface_->set_error) {
		_iface_->set_error (self, value);
	}
}

static void
ft_job_default_init (FtJobIface * iface,
                     gpointer iface_data)
{
	g_object_interface_install_property (iface, g_param_spec_boolean ("completed", "completed", "completed", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_interface_install_property (iface, g_param_spec_boxed ("error", "error", "error", G_TYPE_ERROR, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static GType
ft_job_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (FtJobIface), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ft_job_default_init, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
	GType ft_job_type_id;
	ft_job_type_id = g_type_register_static (G_TYPE_INTERFACE, "FtJob", &g_define_type_info, 0);
	g_type_interface_add_prerequisite (ft_job_type_id, G_TYPE_OBJECT);
	return ft_job_type_id;
}

GType
ft_job_get_type (void)
{
	static volatile gsize ft_job_type_id__once = 0;
	if (g_once_init_enter (&ft_job_type_id__once)) {
		GType ft_job_type_id;
		ft_job_type_id = ft_job_get_type_once ();
		g_once_init_leave (&ft_job_type_id__once, ft_job_type_id);
	}
	return ft_job_type_id__once;
}

static inline gpointer
ft_job_queue_get_instance_private (FtJobQueue* self)
{
	return G_STRUCT_MEMBER_P (self, FtJobQueue_private_offset);
}

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

static Block19Data*
block19_data_ref (Block19Data* _data19_)
{
	g_atomic_int_inc (&_data19_->_ref_count_);
	return _data19_;
}

static void
block19_data_unref (void * _userdata_)
{
	Block19Data* _data19_;
	_data19_ = (Block19Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data19_->_ref_count_)) {
		FtJobQueue* self;
		self = _data19_->self;
		_g_object_unref0 (_data19_->job);
		_g_object_unref0 (self);
		g_slice_free (Block19Data, _data19_);
	}
}

static void
__lambda71_ (Block19Data* _data19_,
             GObject* obj,
             GAsyncResult* res)
{
	FtJobQueue* self;
	gboolean _tmp2_;
	gboolean _tmp3_;
	gboolean _tmp5_ = FALSE;
	GError* _inner_error0_ = NULL;
	self = _data19_->self;
	g_return_if_fail (res != NULL);
	{
		ft_job_run_finish (_data19_->job, res, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* _tmp0_;
		GError* _tmp1_;
		g_clear_error (&_inner_error0_);
		_tmp0_ = ft_job_get_error (_data19_->job);
		_tmp1_ = _tmp0_;
		_vala_assert (_tmp1_ != NULL, "job.error != null");
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_tmp2_ = ft_job_get_completed (_data19_->job);
	_tmp3_ = _tmp2_;
	if (!_tmp3_) {
		const gchar* _tmp4_;
		_tmp4_ = g_type_name (G_TYPE_FROM_INSTANCE ((GObject*) _data19_->job));
		g_warning ("job-queue.vala:50: Job %s did not complete", _tmp4_);
	}
	self->priv->running = FALSE;
	ft_job_queue_pop (self, FALSE);
	if (!self->priv->running) {
		GAsyncQueue* _tmp6_;
		_tmp6_ = self->priv->queue;
		_tmp5_ = g_async_queue_length (_tmp6_) == 0;
	} else {
		_tmp5_ = FALSE;
	}
	if (_tmp5_) {
		g_signal_emit (self, ft_job_queue_signals[FT_JOB_QUEUE_DRAINED_SIGNAL], 0);
	}
}

static void
___lambda71__gasync_ready_callback (GObject* source_object,
                                    GAsyncResult* res,
                                    gpointer self)
{
	__lambda71_ (self, source_object, res);
	block19_data_unref (self);
}

static void
ft_job_queue_run_job (FtJobQueue* self,
                      FtJob* job)
{
	Block19Data* _data19_;
	FtJob* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (job != NULL);
	_vala_return_if_fail (!self->priv->running, "!this.running");
	_data19_ = g_slice_new0 (Block19Data);
	_data19_->_ref_count_ = 1;
	_data19_->self = g_object_ref (self);
	_tmp0_ = _g_object_ref0 (job);
	_g_object_unref0 (_data19_->job);
	_data19_->job = _tmp0_;
	self->priv->running = TRUE;
	ft_job_run (_data19_->job, ___lambda71__gasync_ready_callback, block19_data_ref (_data19_));
	block19_data_unref (_data19_);
	_data19_ = NULL;
}

static void
ft_job_queue_pop (FtJobQueue* self,
                  gboolean may_block)
{
	FtJob* _tmp0_ = NULL;
	FtJob* job = NULL;
	FtJob* _tmp5_;
	FtJob* _tmp6_;
	g_return_if_fail (self != NULL);
	if (may_block) {
		GAsyncQueue* _tmp1_;
		gpointer _tmp2_;
		_tmp1_ = self->priv->queue;
		_tmp2_ = g_async_queue_pop (_tmp1_);
		_g_object_unref0 (_tmp0_);
		_tmp0_ = (FtJob*) _tmp2_;
	} else {
		GAsyncQueue* _tmp3_;
		gpointer _tmp4_;
		_tmp3_ = self->priv->queue;
		_tmp4_ = g_async_queue_try_pop (_tmp3_);
		_g_object_unref0 (_tmp0_);
		_tmp0_ = (FtJob*) _tmp4_;
	}
	_tmp5_ = _tmp0_;
	_tmp0_ = NULL;
	job = _tmp5_;
	_tmp6_ = job;
	if (_tmp6_ != NULL) {
		FtJob* _tmp7_;
		_tmp7_ = job;
		ft_job_queue_run_job (self, _tmp7_);
	}
	_g_object_unref0 (job);
	_g_object_unref0 (_tmp0_);
}

void
ft_job_queue_push (FtJobQueue* self,
                   FtJob* job)
{
	GAsyncQueue* _tmp0_;
	FtJob* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (job != NULL);
	_tmp0_ = self->priv->queue;
	_tmp1_ = _g_object_ref0 (job);
	g_async_queue_push (_tmp0_, _tmp1_);
	if (!self->priv->running) {
		ft_job_queue_pop (self, FALSE);
	}
}

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

void
ft_job_queue_wait (FtJobQueue* self,
                   GAsyncReadyCallback _callback_,
                   gpointer _user_data_)
{
	FtJobQueueWaitData* _data_;
	FtJobQueue* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (FtJobQueueWaitData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, ft_job_queue_wait_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	ft_job_queue_wait_co (_data_);
}

void
ft_job_queue_wait_finish (FtJobQueue* self,
                          GAsyncResult* _res_)
{
	FtJobQueueWaitData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

/**
         * Wait until all jobs are completed.
         */
static Block20Data*
block20_data_ref (Block20Data* _data20_)
{
	g_atomic_int_inc (&_data20_->_ref_count_);
	return _data20_;
}

static void
block20_data_unref (void * _userdata_)
{
	Block20Data* _data20_;
	_data20_ = (Block20Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data20_->_ref_count_)) {
		FtJobQueue* self;
		self = _data20_->self;
		_g_object_unref0 (self);
		g_slice_free (Block20Data, _data20_);
	}
}

static void
__lambda72_ (Block20Data* _data20_)
{
	FtJobQueue* self;
	self = _data20_->self;
	g_signal_handler_disconnect ((GObject*) self, _data20_->drained_id);
	ft_job_queue_wait_co (_data20_->_async_data_);
}

static void
___lambda72__ft_job_queue_drained (FtJobQueue* _sender,
                                   gpointer self)
{
	__lambda72_ (self);
}

static gboolean
ft_job_queue_wait_co (FtJobQueueWaitData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data20_ = g_slice_new0 (Block20Data);
	_data_->_data20_->_ref_count_ = 1;
	_data_->_data20_->self = g_object_ref (_data_->self);
	_data_->_data20_->_async_data_ = _data_;
	_data_->_data20_->drained_id = (gulong) 0;
	if (!_data_->self->priv->running) {
		_data_->_tmp1_ = _data_->self->priv->queue;
		_data_->_tmp0_ = g_async_queue_length (_data_->_tmp1_) == 0;
	} else {
		_data_->_tmp0_ = FALSE;
	}
	if (_data_->_tmp0_) {
		block20_data_unref (_data_->_data20_);
		_data_->_data20_ = NULL;
		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;
	}
	_data_->_tmp2_ = g_signal_connect_data (_data_->self, "drained", (GCallback) ___lambda72__ft_job_queue_drained, block20_data_ref (_data_->_data20_), (GClosureNotify) block20_data_unref, 0);
	_data_->_data20_->drained_id = _data_->_tmp2_;
	_data_->_state_ = 1;
	return FALSE;
	_state_1:
	;
	block20_data_unref (_data_->_data20_);
	_data_->_data20_ = NULL;
	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;
}

static void
ft_job_queue_real_dispose (GObject* base)
{
	FtJobQueue * self;
	self = (FtJobQueue*) base;
	_g_async_queue_unref0 (self->priv->queue);
	self->priv->queue = NULL;
	G_OBJECT_CLASS (ft_job_queue_parent_class)->dispose (G_TYPE_CHECK_INSTANCE_CAST (self, G_TYPE_OBJECT, GObject));
}

FtJobQueue*
ft_job_queue_construct (GType object_type)
{
	FtJobQueue * self = NULL;
	self = (FtJobQueue*) g_object_new (object_type, NULL);
	return self;
}

FtJobQueue*
ft_job_queue_new (void)
{
	return ft_job_queue_construct (FT_TYPE_JOB_QUEUE);
}

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

static GObject *
ft_job_queue_constructor (GType type,
                          guint n_construct_properties,
                          GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	static GWeakRef FtJobQueue_singleton__ref;
	static GMutex FtJobQueue_singleton__lock;
	FtJobQueue * self;
	GAsyncQueue* _tmp0_;
	g_mutex_lock (&FtJobQueue_singleton__lock);
	obj = g_weak_ref_get (&FtJobQueue_singleton__ref);
	if (obj != NULL) {
		g_mutex_unlock (&FtJobQueue_singleton__lock);
		return obj;
	}
	parent_class = G_OBJECT_CLASS (ft_job_queue_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_JOB_QUEUE, FtJobQueue);
	_tmp0_ = g_async_queue_new_full (_g_object_unref0_);
	_g_async_queue_unref0 (self->priv->queue);
	self->priv->queue = _tmp0_;
	g_weak_ref_set (&FtJobQueue_singleton__ref, obj);
	g_mutex_unlock (&FtJobQueue_singleton__lock);
	return obj;
}

static void
ft_job_queue_class_init (FtJobQueueClass * klass,
                         gpointer klass_data)
{
	ft_job_queue_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &FtJobQueue_private_offset);
	((GObjectClass *) klass)->dispose = (void (*) (GObject*)) ft_job_queue_real_dispose;
	G_OBJECT_CLASS (klass)->constructor = ft_job_queue_constructor;
	G_OBJECT_CLASS (klass)->finalize = ft_job_queue_finalize;
	ft_job_queue_signals[FT_JOB_QUEUE_DRAINED_SIGNAL] = g_signal_new ("drained", FT_TYPE_JOB_QUEUE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
}

static void
ft_job_queue_instance_init (FtJobQueue * self,
                            gpointer klass)
{
	self->priv = ft_job_queue_get_instance_private (self);
	self->priv->running = FALSE;
}

static void
ft_job_queue_finalize (GObject * obj)
{
	FtJobQueue * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_JOB_QUEUE, FtJobQueue);
	_g_async_queue_unref0 (self->priv->queue);
	G_OBJECT_CLASS (ft_job_queue_parent_class)->finalize (obj);
}

/**
     * Run scheduled jobs sequentially. Currently we only run one type of jobs - actions / commands.
     * If there were more types, likely we would need to manage several queues / workers.
     */
static GType
ft_job_queue_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (FtJobQueueClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ft_job_queue_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FtJobQueue), 0, (GInstanceInitFunc) ft_job_queue_instance_init, NULL };
	GType ft_job_queue_type_id;
	ft_job_queue_type_id = g_type_register_static (G_TYPE_OBJECT, "FtJobQueue", &g_define_type_info, 0);
	FtJobQueue_private_offset = g_type_add_instance_private (ft_job_queue_type_id, sizeof (FtJobQueuePrivate));
	return ft_job_queue_type_id;
}

GType
ft_job_queue_get_type (void)
{
	static volatile gsize ft_job_queue_type_id__once = 0;
	if (g_once_init_enter (&ft_job_queue_type_id__once)) {
		GType ft_job_queue_type_id;
		ft_job_queue_type_id = ft_job_queue_get_type_once ();
		g_once_init_leave (&ft_job_queue_type_id__once, ft_job_queue_type_id);
	}
	return ft_job_queue_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;
}

