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

/*
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "contacts.h"
#include <gio/gio.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>

#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 _ContactsOperationUndoData ContactsOperationUndoData;

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 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 GType contacts_operation_get_type_once (void);

void
contacts_operation_execute (ContactsOperation* self,
                            GAsyncReadyCallback _callback_,
                            gpointer _user_data_)
{
	ContactsOperationIface* _iface_;
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->execute) {
		_iface_->execute (self, _callback_, _user_data_);
	}
}

void
contacts_operation_execute_finish (ContactsOperation* self,
                                   GAsyncResult* _res_,
                                   GError** error)
{
	ContactsOperationIface* _iface_;
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->execute_finish) {
		_iface_->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:51: 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_)
{
	ContactsOperationIface* _iface_;
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->undo) {
		_iface_->undo (self, _callback_, _user_data_);
	}
}

void
contacts_operation_undo_finish (ContactsOperation* self,
                                GAsyncResult* _res_,
                                GError** error)
{
	ContactsOperationIface* _iface_;
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->undo_finish) {
		_iface_->undo_finish (self, _res_, error);
	}
}

void
_contacts_operation_undo (ContactsOperation* self,
                          GAsyncReadyCallback _callback_,
                          gpointer _user_data_)
{
	ContactsOperationIface* _iface_;
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->_undo) {
		_iface_->_undo (self, _callback_, _user_data_);
	}
}

void
_contacts_operation_undo_finish (ContactsOperation* self,
                                 GAsyncResult* _res_,
                                 GError** error)
{
	ContactsOperationIface* _iface_;
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->_undo_finish) {
		_iface_->_undo_finish (self, _res_, error);
	}
}

gboolean
contacts_operation_get_reversable (ContactsOperation* self)
{
	ContactsOperationIface* _iface_;
	g_return_val_if_fail (self != NULL, FALSE);
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->get_reversable) {
		return _iface_->get_reversable (self);
	}
	return FALSE;
}

gchar*
contacts_operation_get_description (ContactsOperation* self)
{
	ContactsOperationIface* _iface_;
	g_return_val_if_fail (self != NULL, NULL);
	_iface_ = CONTACTS_OPERATION_GET_INTERFACE (self);
	if (_iface_->get_description) {
		return _iface_->get_description (self);
	}
	return NULL;
}

static void
contacts_operation_default_init (ContactsOperationIface * iface,
                                 gpointer iface_data)
{
	/**
	   * Whether undo() can be called on this object
	   */
	g_object_interface_install_property (iface, 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_interface_install_property (iface, g_param_spec_string ("description", "description", "description", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	iface->undo = contacts_operation_real_undo;
	iface->undo_finish = contacts_operation_real_undo_finish;
}

/**
 * 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().
 */
 G_GNUC_NO_INLINE static GType
contacts_operation_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContactsOperationIface), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_operation_default_init, (GClassFinalizeFunc) NULL, NULL, 0, 0, (GInstanceInitFunc) NULL, NULL };
	GType contacts_operation_type_id;
	contacts_operation_type_id = g_type_register_static (G_TYPE_INTERFACE, "ContactsOperation", &g_define_type_info, 0);
	g_type_interface_add_prerequisite (contacts_operation_type_id, G_TYPE_OBJECT);
	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;
}

