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

/*
 * Copyright (c) 2025 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

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

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _Block1Data Block1Data;
#define _g_regex_unref0(var) ((var == NULL) ? NULL : (var = (g_regex_unref (var), NULL)))
#define _g_hash_table_unref0(var) ((var == NULL) ? NULL : (var = (g_hash_table_unref (var), NULL)))
typedef struct _PortalCreateRequestData PortalCreateRequestData;

struct _Block1Data {
	int _ref_count_;
	gchar* handle_token;
	PortalRequestCallback callback;
	gpointer callback_target;
	gpointer _async_data_;
};

struct _PortalCreateRequestData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GDBusConnection* connection;
	PortalRequestCallback callback;
	gpointer callback_target;
	gchar* result;
	Block1Data* _data1_;
	guint request_id;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* sender;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* sender_escaped;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	guint _tmp10_;
	PortalRequest* request_proxy;
	const gchar* _tmp11_;
	const gchar* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	PortalRequest* _tmp15_;
	PortalRequest* _tmp16_;
	GHashTable* _tmp17_;
	GHashFunc _tmp18_;
	GEqualFunc _tmp19_;
	GHashTable* _tmp20_;
	GHashTable* _tmp21_;
	gchar* _tmp22_;
	PortalRequest* _tmp23_;
	gchar* _tmp24_;
	GError* _inner_error0_;
};

VALA_EXTERN guint portal_next_request_id;
guint portal_next_request_id = 1U;
VALA_EXTERN GHashTable* portal_requests;
GHashTable* portal_requests = NULL;

static void portal_create_request_data_free (gpointer _data);
static gboolean portal_create_request_co (PortalCreateRequestData* _data_);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static void portal_create_request_ready (GObject* source_object,
                                  GAsyncResult* _res_,
                                  gpointer _user_data_);
static void __lambda4_ (Block1Data* _data1_,
                 guint32 response,
                 GVariant* results);
static void ___lambda4__portal_request_response (PortalRequest* _sender,
                                          guint32 response,
                                          GVariant* results,
                                          gpointer self);
static void _g_free0_ (gpointer var);
static void _g_object_unref0_ (gpointer var);

static void
portal_create_request_data_free (gpointer _data)
{
	PortalCreateRequestData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->connection);
	_g_free0 (_data_->result);
	g_slice_free (PortalCreateRequestData, _data_);
}

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

void
portal_create_request (GDBusConnection* connection,
                       PortalRequestCallback callback,
                       gpointer callback_target,
                       GAsyncReadyCallback _callback_,
                       gpointer _user_data_)
{
	PortalCreateRequestData* _data_;
	GDBusConnection* _tmp0_;
	g_return_if_fail (connection != NULL);
	_data_ = g_slice_new0 (PortalCreateRequestData);
	_data_->_async_result = g_task_new (NULL, NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, portal_create_request_data_free);
	_tmp0_ = _g_object_ref0 (connection);
	_g_object_unref0 (_data_->connection);
	_data_->connection = _tmp0_;
	_data_->callback = callback;
	_data_->callback_target = callback_target;
	portal_create_request_co (_data_);
}

gchar*
portal_create_request_finish (GAsyncResult* _res_,
                              GError** error)
{
	gchar* result;
	PortalCreateRequestData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return NULL;
	}
	result = _data_->result;
	_data_->result = NULL;
	return result;
}

static Block1Data*
block1_data_ref (Block1Data* _data1_)
{
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}

static void
block1_data_unref (void * _userdata_)
{
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		_g_free0 (_data1_->handle_token);
		g_slice_free (Block1Data, _data1_);
	}
}

static gchar*
string_replace (const gchar* self,
                const gchar* old,
                const gchar* replacement)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (old != NULL, NULL);
	g_return_val_if_fail (replacement != NULL, NULL);
	if ((*((gchar*) self)) == '\0') {
		_tmp1_ = TRUE;
	} else {
		_tmp1_ = (*((gchar*) old)) == '\0';
	}
	if (_tmp1_) {
		_tmp0_ = TRUE;
	} else {
		_tmp0_ = g_strcmp0 (old, replacement) == 0;
	}
	if (_tmp0_) {
		gchar* _tmp2_;
		_tmp2_ = g_strdup (self);
		result = _tmp2_;
		return result;
	}
	{
		GRegex* regex = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		GRegex* _tmp5_;
		GRegex* _tmp6_;
		gchar* _tmp7_ = NULL;
		GRegex* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp3_ = g_regex_escape_string (old, -1);
		_tmp4_ = _tmp3_;
		_tmp5_ = g_regex_new (_tmp4_, 0, 0, &_inner_error0_);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp4_);
		regex = _tmp6_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp8_ = regex;
		_tmp9_ = g_regex_replace_literal (_tmp8_, self, (gssize) -1, 0, replacement, 0, &_inner_error0_);
		_tmp7_ = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_regex_unref0 (regex);
			if (_inner_error0_->domain == G_REGEX_ERROR) {
				goto __catch0_g_regex_error;
			}
			g_critical ("file %s: line %d: unexpected 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 NULL;
		}
		_tmp10_ = _tmp7_;
		_tmp7_ = NULL;
		result = _tmp10_;
		_g_free0 (_tmp7_);
		_g_regex_unref0 (regex);
		return result;
	}
	goto __finally0;
	__catch0_g_regex_error:
	{
		g_clear_error (&_inner_error0_);
		g_assert_not_reached ();
	}
	__finally0:
	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 NULL;
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

static void
portal_create_request_ready (GObject* source_object,
                             GAsyncResult* _res_,
                             gpointer _user_data_)
{
	PortalCreateRequestData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	portal_create_request_co (_data_);
}

static void
__lambda4_ (Block1Data* _data1_,
            guint32 response,
            GVariant* results)
{
	g_return_if_fail (results != NULL);
	portal_destroy_request (_data1_->handle_token);
	_data1_->callback (response, results, _data1_->callback_target);
}

static void
___lambda4__portal_request_response (PortalRequest* _sender,
                                     guint32 response,
                                     GVariant* results,
                                     gpointer self)
{
	__lambda4_ (self, response, results);
}

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

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

static gboolean
portal_create_request_co (PortalCreateRequestData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_data1_ = g_slice_new0 (Block1Data);
	_data_->_data1_->_ref_count_ = 1;
	_data_->_data1_->callback = _data_->callback;
	_data_->_data1_->callback_target = _data_->callback_target;
	_data_->_data1_->_async_data_ = _data_;
	_data_->request_id = portal_next_request_id;
	_data_->_tmp0_ = g_strdup_printf ("%u", _data_->request_id);
	_data_->_tmp1_ = _data_->_tmp0_;
	_data_->_tmp2_ = g_strconcat ("focustimer_", _data_->_tmp1_, NULL);
	_data_->_tmp3_ = _data_->_tmp2_;
	_g_free0 (_data_->_tmp1_);
	_data_->_data1_->handle_token = _data_->_tmp3_;
	_data_->_tmp4_ = g_dbus_connection_get_unique_name (_data_->connection);
	_data_->_tmp5_ = g_strdup (_data_->_tmp4_);
	_data_->sender = _data_->_tmp5_;
	_data_->_tmp6_ = string_replace (_data_->sender, ":", "");
	_data_->_tmp7_ = _data_->_tmp6_;
	_data_->_tmp8_ = string_replace (_data_->_tmp7_, ".", "_");
	_data_->_tmp9_ = _data_->_tmp8_;
	_g_free0 (_data_->_tmp7_);
	_data_->sender_escaped = _data_->_tmp9_;
	_data_->_tmp10_ = portal_next_request_id;
	portal_next_request_id = _data_->_tmp10_ + 1;
	_data_->_tmp11_ = string_to_string (_data_->sender_escaped);
	_data_->_tmp12_ = string_to_string (_data_->_data1_->handle_token);
	_data_->_tmp13_ = g_strconcat ("/org/freedesktop/portal/desktop/request/", _data_->_tmp11_, "/", _data_->_tmp12_, NULL);
	_data_->_tmp14_ = _data_->_tmp13_;
	_data_->_state_ = 1;
	g_async_initable_new_async (PORTAL_TYPE_REQUEST_PROXY, 0, NULL, portal_create_request_ready, _data_, "g-flags", 0, "g-name", "org.freedesktop.portal.Desktop", "g-bus-type", G_BUS_TYPE_SESSION, "g-object-path", _data_->_tmp14_, "g-interface-name", "org.freedesktop.portal.Request", NULL);
	return FALSE;
	_state_1:
	_data_->_tmp15_ = (PortalRequest*) g_async_initable_new_finish ((GAsyncInitable *) _data_->_source_object_, _data_->_res_, &_data_->_inner_error0_);
	_data_->_tmp16_ = (PortalRequest*) _data_->_tmp15_;
	_g_free0 (_data_->_tmp14_);
	_data_->request_proxy = _data_->_tmp16_;
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		_g_free0 (_data_->sender_escaped);
		_g_free0 (_data_->sender);
		block1_data_unref (_data_->_data1_);
		_data_->_data1_ = NULL;
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	g_signal_connect_data (_data_->request_proxy, "response", (GCallback) ___lambda4__portal_request_response, block1_data_ref (_data_->_data1_), (GClosureNotify) block1_data_unref, 0);
	_data_->_tmp17_ = portal_requests;
	if (_data_->_tmp17_ == NULL) {
		_data_->_tmp18_ = g_str_hash;
		_data_->_tmp19_ = g_str_equal;
		_data_->_tmp20_ = g_hash_table_new_full (_data_->_tmp18_, _data_->_tmp19_, _g_free0_, _g_object_unref0_);
		_g_hash_table_unref0 (portal_requests);
		portal_requests = _data_->_tmp20_;
	}
	_data_->_tmp21_ = portal_requests;
	_data_->_tmp22_ = g_strdup (_data_->_data1_->handle_token);
	_data_->_tmp23_ = _g_object_ref0 (_data_->request_proxy);
	g_hash_table_insert (_data_->_tmp21_, _data_->_tmp22_, _data_->_tmp23_);
	_data_->_tmp24_ = g_strdup (_data_->_data1_->handle_token);
	_data_->result = _data_->_tmp24_;
	_g_object_unref0 (_data_->request_proxy);
	_g_free0 (_data_->sender_escaped);
	_g_free0 (_data_->sender);
	block1_data_unref (_data_->_data1_);
	_data_->_data1_ = 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;
}

gboolean
portal_destroy_request (const gchar* handle_token)
{
	gboolean _tmp0_ = FALSE;
	GHashTable* _tmp1_;
	gboolean result;
	g_return_val_if_fail (handle_token != NULL, FALSE);
	_tmp1_ = portal_requests;
	if (_tmp1_ != NULL) {
		GHashTable* _tmp2_;
		_tmp2_ = portal_requests;
		_tmp0_ = g_hash_table_remove (_tmp2_, handle_token);
	} else {
		_tmp0_ = FALSE;
	}
	result = _tmp0_;
	return result;
}

