/* contacts-operation.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from contacts-operation.vala, do not modify */

/*
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "contacts.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gio/gio.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  {
	CONTACTS_OPERATION_0_PROPERTY,
	CONTACTS_OPERATION_UUID_PROPERTY,
	CONTACTS_OPERATION_REVERSABLE_PROPERTY,
	CONTACTS_OPERATION_DESCRIPTION_PROPERTY,
	CONTACTS_OPERATION_NUM_PROPERTIES
};
static GParamSpec* contacts_operation_properties[CONTACTS_OPERATION_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _ContactsOperationUndoData ContactsOperationUndoData;

struct _ContactsOperationPrivate {
	gchar* _uuid;
};

struct _ContactsOperationUndoData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ContactsOperation* self;
	gboolean _tmp0_;
	gboolean _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	GError* _inner_error0_;
};

static gint ContactsOperation_private_offset;
static gpointer contacts_operation_parent_class = NULL;

static void contacts_operation_real_undo_data_free (gpointer _data);
static void contacts_operation_real_undo (ContactsOperation* self,
                                   GAsyncReadyCallback _callback_,
                                   gpointer _user_data_);
static gboolean contacts_operation_real_undo_co (ContactsOperationUndoData* _data_);
static void contacts_operation_undo_ready (GObject* source_object,
                                    GAsyncResult* _res_,
                                    gpointer _user_data_);
static void contacts_operation_set_uuid (ContactsOperation* self,
                                  const gchar* value);
static void contacts_operation_finalize (GObject * obj);
static GType contacts_operation_get_type_once (void);
static void _vala_contacts_operation_get_property (GObject * object,
                                            guint property_id,
                                            GValue * value,
                                            GParamSpec * pspec);
static void _vala_contacts_operation_set_property (GObject * object,
                                            guint property_id,
                                            const GValue * value,
                                            GParamSpec * pspec);

static inline gpointer
contacts_operation_get_instance_private (ContactsOperation* self)
{
	return G_STRUCT_MEMBER_P (self, ContactsOperation_private_offset);
}

void
contacts_operation_execute (ContactsOperation* self,
                            GAsyncReadyCallback _callback_,
                            gpointer _user_data_)
{
	ContactsOperationClass* _klass_;
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->execute) {
		_klass_->execute (self, _callback_, _user_data_);
	}
}

void
contacts_operation_execute_finish (ContactsOperation* self,
                                   GAsyncResult* _res_,
                                   GError** error)
{
	ContactsOperationClass* _klass_;
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->execute_finish) {
		_klass_->execute_finish (self, _res_, error);
	}
}

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

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

static void
contacts_operation_real_undo (ContactsOperation* self,
                              GAsyncReadyCallback _callback_,
                              gpointer _user_data_)
{
	ContactsOperationUndoData* _data_;
	ContactsOperation* _tmp0_;
	_data_ = g_slice_new0 (ContactsOperationUndoData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, contacts_operation_real_undo_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	contacts_operation_real_undo_co (_data_);
}

static void
contacts_operation_real_undo_finish (ContactsOperation* self,
                                     GAsyncResult* _res_,
                                     GError** error)
{
	ContactsOperationUndoData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

/**
   * The is the public API undo. If you want, you can override it still, e.g.
   * to provide better warnings.
   */
static void
contacts_operation_undo_ready (GObject* source_object,
                               GAsyncResult* _res_,
                               gpointer _user_data_)
{
	ContactsOperationUndoData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	contacts_operation_real_undo_co (_data_);
}

static gboolean
contacts_operation_real_undo_co (ContactsOperationUndoData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = contacts_operation_get_reversable (_data_->self);
	_data_->_tmp1_ = _data_->_tmp0_;
	if (!_data_->_tmp1_) {
		_data_->_tmp2_ = contacts_operation_get_description (_data_->self);
		_data_->_tmp3_ = _data_->_tmp2_;
		_data_->_tmp4_ = _data_->_tmp3_;
		g_warning ("contacts-operation.vala:46: Can't undo '%s'", _data_->_tmp4_);
		_g_free0 (_data_->_tmp4_);
		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_->_state_ = 1;
	_contacts_operation_undo (_data_->self, contacts_operation_undo_ready, _data_);
	return FALSE;
	_state_1:
	_contacts_operation_undo_finish (_data_->self, _data_->_res_, &_data_->_inner_error0_);
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	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;
}

void
contacts_operation_undo (ContactsOperation* self,
                         GAsyncReadyCallback _callback_,
                         gpointer _user_data_)
{
	ContactsOperationClass* _klass_;
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->undo) {
		_klass_->undo (self, _callback_, _user_data_);
	}
}

void
contacts_operation_undo_finish (ContactsOperation* self,
                                GAsyncResult* _res_,
                                GError** error)
{
	ContactsOperationClass* _klass_;
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->undo_finish) {
		_klass_->undo_finish (self, _res_, error);
	}
}

void
_contacts_operation_undo (ContactsOperation* self,
                          GAsyncReadyCallback _callback_,
                          gpointer _user_data_)
{
	ContactsOperationClass* _klass_;
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->_undo) {
		_klass_->_undo (self, _callback_, _user_data_);
	}
}

void
_contacts_operation_undo_finish (ContactsOperation* self,
                                 GAsyncResult* _res_,
                                 GError** error)
{
	ContactsOperationClass* _klass_;
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->_undo_finish) {
		_klass_->_undo_finish (self, _res_, error);
	}
}

ContactsOperation*
contacts_operation_construct (GType object_type)
{
	ContactsOperation * self = NULL;
	self = (ContactsOperation*) g_object_new (object_type, NULL);
	return self;
}

const gchar*
contacts_operation_get_uuid (ContactsOperation* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_uuid;
	result = _tmp0_;
	return result;
}

static void
contacts_operation_set_uuid (ContactsOperation* self,
                             const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = contacts_operation_get_uuid (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_uuid);
		self->priv->_uuid = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, contacts_operation_properties[CONTACTS_OPERATION_UUID_PROPERTY]);
	}
}

gboolean
contacts_operation_get_reversable (ContactsOperation* self)
{
	ContactsOperationClass* _klass_;
	g_return_val_if_fail (self != NULL, FALSE);
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->get_reversable) {
		return _klass_->get_reversable (self);
	}
	return FALSE;
}

gchar*
contacts_operation_get_description (ContactsOperation* self)
{
	ContactsOperationClass* _klass_;
	g_return_val_if_fail (self != NULL, NULL);
	_klass_ = CONTACTS_OPERATION_GET_CLASS (self);
	if (_klass_->get_description) {
		return _klass_->get_description (self);
	}
	return NULL;
}

static void
contacts_operation_class_init (ContactsOperationClass * klass,
                               gpointer klass_data)
{
	contacts_operation_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContactsOperation_private_offset);
	((ContactsOperationClass *) klass)->undo = (void (*) (ContactsOperation*, GAsyncReadyCallback, gpointer)) contacts_operation_real_undo;
	((ContactsOperationClass *) klass)->undo_finish = (void (*) (ContactsOperation*, GAsyncResult*, GError**)) contacts_operation_real_undo_finish;
	G_OBJECT_CLASS (klass)->get_property = _vala_contacts_operation_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_contacts_operation_set_property;
	G_OBJECT_CLASS (klass)->finalize = contacts_operation_finalize;
	/** The UUID of the operation */
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTACTS_OPERATION_UUID_PROPERTY, contacts_operation_properties[CONTACTS_OPERATION_UUID_PROPERTY] = g_param_spec_string ("uuid", "uuid", "uuid", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	/**
	   * Whether undo() can be called on this object
	   */
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTACTS_OPERATION_REVERSABLE_PROPERTY, contacts_operation_properties[CONTACTS_OPERATION_REVERSABLE_PROPERTY] = g_param_spec_boolean ("reversable", "reversable", "reversable", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	/**
	   * A user-facing string that tells us what the operation does
	   */
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTACTS_OPERATION_DESCRIPTION_PROPERTY, contacts_operation_properties[CONTACTS_OPERATION_DESCRIPTION_PROPERTY] = g_param_spec_string ("description", "description", "description", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
contacts_operation_instance_init (ContactsOperation * self,
                                  gpointer klass)
{
	gchar* _tmp0_;
	self->priv = contacts_operation_get_instance_private (self);
	_tmp0_ = g_uuid_string_random ();
	self->priv->_uuid = _tmp0_;
}

static void
contacts_operation_finalize (GObject * obj)
{
	ContactsOperation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_OPERATION, ContactsOperation);
	_g_free0 (self->priv->_uuid);
	G_OBJECT_CLASS (contacts_operation_parent_class)->finalize (obj);
}

/**
 * Contacts.Operation is a simple interface to describe actions that can be
 * executed and possibly undone later on (for example, using a button on an
 * in-app notification).
 *
 * Since some operations might not be able undoable later onwards, there is a
 * property `reversable` that you should check first before calling undo().
 *
 * Note that you probably shouldn't be calling the execute() method directly,
 * but use the API provided by {@link OperationQueue} instead.
 */
 G_GNUC_NO_INLINE static GType
contacts_operation_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContactsOperationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_operation_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContactsOperation), 0, (GInstanceInitFunc) contacts_operation_instance_init, NULL };
	GType contacts_operation_type_id;
	contacts_operation_type_id = g_type_register_static (G_TYPE_OBJECT, "ContactsOperation", &g_define_type_info, G_TYPE_FLAG_ABSTRACT);
	ContactsOperation_private_offset = g_type_add_instance_private (contacts_operation_type_id, sizeof (ContactsOperationPrivate));
	return contacts_operation_type_id;
}

GType
contacts_operation_get_type (void)
{
	static volatile gsize contacts_operation_type_id__once = 0;
	if (g_once_init_enter (&contacts_operation_type_id__once)) {
		GType contacts_operation_type_id;
		contacts_operation_type_id = contacts_operation_get_type_once ();
		g_once_init_leave (&contacts_operation_type_id__once, contacts_operation_type_id);
	}
	return contacts_operation_type_id__once;
}

static void
_vala_contacts_operation_get_property (GObject * object,
                                       guint property_id,
                                       GValue * value,
                                       GParamSpec * pspec)
{
	ContactsOperation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, CONTACTS_TYPE_OPERATION, ContactsOperation);
	switch (property_id) {
		case CONTACTS_OPERATION_UUID_PROPERTY:
		g_value_set_string (value, contacts_operation_get_uuid (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_contacts_operation_set_property (GObject * object,
                                       guint property_id,
                                       const GValue * value,
                                       GParamSpec * pspec)
{
	ContactsOperation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, CONTACTS_TYPE_OPERATION, ContactsOperation);
	switch (property_id) {
		case CONTACTS_OPERATION_UUID_PROPERTY:
		contacts_operation_set_uuid (self, g_value_get_string (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

