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

/*
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.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 <gee.h>
#include <glib-object.h>
#include <folks/folks.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>

enum  {
	CONTACTS_DELETE_OPERATION_0_PROPERTY,
	CONTACTS_DELETE_OPERATION_REVERSABLE_PROPERTY,
	CONTACTS_DELETE_OPERATION_DESCRIPTION_PROPERTY,
	CONTACTS_DELETE_OPERATION_NUM_PROPERTIES
};
static GParamSpec* contacts_delete_operation_properties[CONTACTS_DELETE_OPERATION_NUM_PROPERTIES];
#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 _ContactsDeleteOperationExecuteData ContactsDeleteOperationExecuteData;
typedef struct _ContactsDeleteOperationUndoData ContactsDeleteOperationUndoData;

struct _ContactsDeleteOperationPrivate {
	GeeList* individuals;
	gchar* _description;
};

struct _ContactsDeleteOperationExecuteData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ContactsDeleteOperation* self;
	GeeList* _indiv_list;
	GeeList* _tmp0_;
	gint _indiv_size;
	GeeList* _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	gint _indiv_index;
	gint _tmp4_;
	gint _tmp5_;
	FolksIndividual* indiv;
	GeeList* _tmp6_;
	gpointer _tmp7_;
	GeeIterator* _persona_it;
	FolksIndividual* _tmp8_;
	GeeSet* _tmp9_;
	GeeSet* _tmp10_;
	GeeIterator* _tmp11_;
	GeeIterator* _tmp12_;
	FolksPersona* persona;
	GeeIterator* _tmp13_;
	gpointer _tmp14_;
	FolksPersona* _tmp15_;
	FolksPersonaStore* _tmp16_;
	FolksPersonaStore* _tmp17_;
	FolksPersona* _tmp18_;
	GError* _inner_error0_;
};

struct _ContactsDeleteOperationUndoData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	ContactsDeleteOperation* self;
	GError* _tmp0_;
	GError* _inner_error0_;
};

static gint ContactsDeleteOperation_private_offset;
static gpointer contacts_delete_operation_parent_class = NULL;
static ContactsOperationIface * contacts_delete_operation_contacts_operation_parent_iface = NULL;

static void contacts_delete_operation_real_execute_data_free (gpointer _data);
static void contacts_delete_operation_real_execute (ContactsOperation* base,
                                             GAsyncReadyCallback _callback_,
                                             gpointer _user_data_);
static gboolean contacts_delete_operation_real_execute_co (ContactsDeleteOperationExecuteData* _data_);
static void contacts_delete_operation_execute_ready (GObject* source_object,
                                              GAsyncResult* _res_,
                                              gpointer _user_data_);
static void contacts_delete_operation_real__undo_data_free (gpointer _data);
static void contacts_delete_operation_real__undo (ContactsOperation* base,
                                           GAsyncReadyCallback _callback_,
                                           gpointer _user_data_);
static gboolean contacts_delete_operation_real__undo_co (ContactsDeleteOperationUndoData* _data_);
static void contacts_delete_operation_finalize (GObject * obj);
static GType contacts_delete_operation_get_type_once (void);
static void _vala_contacts_delete_operation_get_property (GObject * object,
                                                   guint property_id,
                                                   GValue * value,
                                                   GParamSpec * pspec);

static inline gpointer
contacts_delete_operation_get_instance_private (ContactsDeleteOperation* self)
{
	return G_STRUCT_MEMBER_P (self, ContactsDeleteOperation_private_offset);
}

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

ContactsDeleteOperation*
contacts_delete_operation_construct (GType object_type,
                                     GeeList* individuals)
{
	ContactsDeleteOperation * self = NULL;
	GeeList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	gint _tmp4_;
	gchar* _tmp5_;
	g_return_val_if_fail (individuals != NULL, NULL);
	self = (ContactsDeleteOperation*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (individuals);
	_g_object_unref0 (self->priv->individuals);
	self->priv->individuals = _tmp0_;
	_tmp1_ = gee_collection_get_size ((GeeCollection*) individuals);
	_tmp2_ = _tmp1_;
	_tmp3_ = gee_collection_get_size ((GeeCollection*) individuals);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_strdup_printf (ngettext ("Deleting %d contact", "Deleting %d contacts", (gulong) _tmp2_), _tmp4_);
	_g_free0 (self->priv->_description);
	self->priv->_description = _tmp5_;
	return self;
}

ContactsDeleteOperation*
contacts_delete_operation_new (GeeList* individuals)
{
	return contacts_delete_operation_construct (CONTACTS_TYPE_DELETE_OPERATION, individuals);
}

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

static void
contacts_delete_operation_real_execute (ContactsOperation* base,
                                        GAsyncReadyCallback _callback_,
                                        gpointer _user_data_)
{
	ContactsDeleteOperation * self;
	ContactsDeleteOperationExecuteData* _data_;
	ContactsDeleteOperation* _tmp0_;
	self = (ContactsDeleteOperation*) base;
	_data_ = g_slice_new0 (ContactsDeleteOperationExecuteData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, contacts_delete_operation_real_execute_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	contacts_delete_operation_real_execute_co (_data_);
}

static void
contacts_delete_operation_execute_finish (ContactsOperation* base,
                                          GAsyncResult* _res_,
                                          GError** error)
{
	ContactsDeleteOperationExecuteData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

/**
   * Link individuals
   */
static void
contacts_delete_operation_execute_ready (GObject* source_object,
                                         GAsyncResult* _res_,
                                         gpointer _user_data_)
{
	ContactsDeleteOperationExecuteData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	contacts_delete_operation_real_execute_co (_data_);
}

static gboolean
contacts_delete_operation_real_execute_co (ContactsDeleteOperationExecuteData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_tmp0_ = _data_->self->priv->individuals;
		_data_->_indiv_list = _data_->_tmp0_;
		_data_->_tmp1_ = _data_->_indiv_list;
		_data_->_tmp2_ = gee_collection_get_size ((GeeCollection*) _data_->_tmp1_);
		_data_->_tmp3_ = _data_->_tmp2_;
		_data_->_indiv_size = _data_->_tmp3_;
		_data_->_indiv_index = -1;
		while (TRUE) {
			_data_->_indiv_index = _data_->_indiv_index + 1;
			_data_->_tmp4_ = _data_->_indiv_index;
			_data_->_tmp5_ = _data_->_indiv_size;
			if (!(_data_->_tmp4_ < _data_->_tmp5_)) {
				break;
			}
			_data_->_tmp6_ = _data_->_indiv_list;
			_data_->_tmp7_ = gee_list_get (_data_->_tmp6_, _data_->_indiv_index);
			_data_->indiv = (FolksIndividual*) _data_->_tmp7_;
			{
				_data_->_tmp8_ = _data_->indiv;
				_data_->_tmp9_ = folks_individual_get_personas (_data_->_tmp8_);
				_data_->_tmp10_ = _data_->_tmp9_;
				_data_->_tmp11_ = gee_iterable_iterator ((GeeIterable*) _data_->_tmp10_);
				_data_->_persona_it = _data_->_tmp11_;
				while (TRUE) {
					_data_->_tmp12_ = _data_->_persona_it;
					if (!gee_iterator_next (_data_->_tmp12_)) {
						break;
					}
					_data_->_tmp13_ = _data_->_persona_it;
					_data_->_tmp14_ = gee_iterator_get (_data_->_tmp13_);
					_data_->persona = (FolksPersona*) _data_->_tmp14_;
					_data_->_tmp15_ = _data_->persona;
					_data_->_tmp16_ = folks_persona_get_store (_data_->_tmp15_);
					_data_->_tmp17_ = _data_->_tmp16_;
					_data_->_tmp18_ = _data_->persona;
					_data_->_state_ = 1;
					folks_persona_store_remove_persona (_data_->_tmp17_, _data_->_tmp18_, contacts_delete_operation_execute_ready, _data_);
					return FALSE;
					_state_1:
					folks_persona_store_remove_persona_finish (_data_->_tmp17_, _data_->_res_, &_data_->_inner_error0_);
					if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
						g_task_return_error (_data_->_async_result, _data_->_inner_error0_);
						_g_object_unref0 (_data_->persona);
						_g_object_unref0 (_data_->_persona_it);
						_g_object_unref0 (_data_->indiv);
						g_object_unref (_data_->_async_result);
						return FALSE;
					}
					_g_object_unref0 (_data_->persona);
				}
				_g_object_unref0 (_data_->_persona_it);
			}
			_g_object_unref0 (_data_->indiv);
		}
	}
	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
contacts_delete_operation_real__undo_data_free (gpointer _data)
{
	ContactsDeleteOperationUndoData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (ContactsDeleteOperationUndoData, _data_);
}

static void
contacts_delete_operation_real__undo (ContactsOperation* base,
                                      GAsyncReadyCallback _callback_,
                                      gpointer _user_data_)
{
	ContactsDeleteOperation * self;
	ContactsDeleteOperationUndoData* _data_;
	ContactsDeleteOperation* _tmp0_;
	self = (ContactsDeleteOperation*) base;
	_data_ = g_slice_new0 (ContactsDeleteOperationUndoData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, contacts_delete_operation_real__undo_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	contacts_delete_operation_real__undo_co (_data_);
}

static void
_contacts_delete_operation_undo_finish (ContactsOperation* base,
                                        GAsyncResult* _res_,
                                        GError** error)
{
	ContactsDeleteOperationUndoData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static gboolean
contacts_delete_operation_real__undo_co (ContactsDeleteOperationUndoData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = g_error_new_literal (G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED, "Undoing not supported");
	_data_->_inner_error0_ = _data_->_tmp0_;
	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;
}

static gboolean
contacts_delete_operation_real_get_reversable (ContactsOperation* base)
{
	gboolean result;
	ContactsDeleteOperation* self;
	self = (ContactsDeleteOperation*) base;
	result = FALSE;
	return result;
}

static gchar*
contacts_delete_operation_real_get_description (ContactsOperation* base)
{
	gchar* result;
	ContactsDeleteOperation* self;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	self = (ContactsDeleteOperation*) base;
	_tmp0_ = self->priv->_description;
	_tmp1_ = g_strdup (_tmp0_);
	result = _tmp1_;
	return result;
}

static void
contacts_delete_operation_class_init (ContactsDeleteOperationClass * klass,
                                      gpointer klass_data)
{
	contacts_delete_operation_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContactsDeleteOperation_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_contacts_delete_operation_get_property;
	G_OBJECT_CLASS (klass)->finalize = contacts_delete_operation_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTACTS_DELETE_OPERATION_REVERSABLE_PROPERTY, contacts_delete_operation_properties[CONTACTS_DELETE_OPERATION_REVERSABLE_PROPERTY] = g_param_spec_boolean ("reversable", "reversable", "reversable", FALSE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTACTS_DELETE_OPERATION_DESCRIPTION_PROPERTY, contacts_delete_operation_properties[CONTACTS_DELETE_OPERATION_DESCRIPTION_PROPERTY] = g_param_spec_string ("description", "description", "description", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
contacts_delete_operation_contacts_operation_interface_init (ContactsOperationIface * iface,
                                                             gpointer iface_data)
{
	contacts_delete_operation_contacts_operation_parent_iface = g_type_interface_peek_parent (iface);
	iface->execute = (void (*) (ContactsOperation*, GAsyncReadyCallback, gpointer)) contacts_delete_operation_real_execute;
	iface->execute_finish = (void (*) (ContactsOperation*, GAsyncResult*, GError**)) contacts_delete_operation_execute_finish;
	iface->_undo = (void (*) (ContactsOperation*, GAsyncReadyCallback, gpointer)) contacts_delete_operation_real__undo;
	iface->_undo_finish = (void (*) (ContactsOperation*, GAsyncResult*, GError**)) _contacts_delete_operation_undo_finish;
	iface->get_reversable = contacts_delete_operation_real_get_reversable;
	iface->get_description = contacts_delete_operation_real_get_description;
}

static void
contacts_delete_operation_instance_init (ContactsDeleteOperation * self,
                                         gpointer klass)
{
	self->priv = contacts_delete_operation_get_instance_private (self);
}

static void
contacts_delete_operation_finalize (GObject * obj)
{
	ContactsDeleteOperation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_DELETE_OPERATION, ContactsDeleteOperation);
	_g_object_unref0 (self->priv->individuals);
	_g_free0 (self->priv->_description);
	G_OBJECT_CLASS (contacts_delete_operation_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
contacts_delete_operation_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContactsDeleteOperationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_delete_operation_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContactsDeleteOperation), 0, (GInstanceInitFunc) contacts_delete_operation_instance_init, NULL };
	static const GInterfaceInfo contacts_operation_info = { (GInterfaceInitFunc) contacts_delete_operation_contacts_operation_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType contacts_delete_operation_type_id;
	contacts_delete_operation_type_id = g_type_register_static (G_TYPE_OBJECT, "ContactsDeleteOperation", &g_define_type_info, 0);
	g_type_add_interface_static (contacts_delete_operation_type_id, CONTACTS_TYPE_OPERATION, &contacts_operation_info);
	ContactsDeleteOperation_private_offset = g_type_add_instance_private (contacts_delete_operation_type_id, sizeof (ContactsDeleteOperationPrivate));
	return contacts_delete_operation_type_id;
}

GType
contacts_delete_operation_get_type (void)
{
	static volatile gsize contacts_delete_operation_type_id__once = 0;
	if (g_once_init_enter (&contacts_delete_operation_type_id__once)) {
		GType contacts_delete_operation_type_id;
		contacts_delete_operation_type_id = contacts_delete_operation_get_type_once ();
		g_once_init_leave (&contacts_delete_operation_type_id__once, contacts_delete_operation_type_id);
	}
	return contacts_delete_operation_type_id__once;
}

static void
_vala_contacts_delete_operation_get_property (GObject * object,
                                              guint property_id,
                                              GValue * value,
                                              GParamSpec * pspec)
{
	ContactsDeleteOperation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, CONTACTS_TYPE_DELETE_OPERATION, ContactsDeleteOperation);
	switch (property_id) {
		case CONTACTS_DELETE_OPERATION_REVERSABLE_PROPERTY:
		g_value_set_boolean (value, contacts_operation_get_reversable ((ContactsOperation*) self));
		break;
		case CONTACTS_DELETE_OPERATION_DESCRIPTION_PROPERTY:
		g_value_take_string (value, contacts_operation_get_description ((ContactsOperation*) self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

