/* app-insert-operation.c generated by valac 0.56.19, the Vala compiler
 * generated from app-insert-operation.vala, do not modify */

/*
 * Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution.
 */

#include "geary-engine.h"
#include <glib-object.h>
#include <gio/gio.h>
#include <glib.h>
#include <gee.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

#define GEARY_APP_TYPE_CONVERSATION_OPERATION (geary_app_conversation_operation_get_type ())
#define GEARY_APP_CONVERSATION_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation))
#define GEARY_APP_CONVERSATION_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperationClass))
#define GEARY_APP_IS_CONVERSATION_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEARY_APP_TYPE_CONVERSATION_OPERATION))
#define GEARY_APP_IS_CONVERSATION_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEARY_APP_TYPE_CONVERSATION_OPERATION))
#define GEARY_APP_CONVERSATION_OPERATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperationClass))

typedef struct _GearyAppConversationOperation GearyAppConversationOperation;
typedef struct _GearyAppConversationOperationClass GearyAppConversationOperationClass;
typedef struct _GearyAppConversationOperationPrivate GearyAppConversationOperationPrivate;

#define GEARY_APP_TYPE_BATCH_OPERATION (geary_app_batch_operation_get_type ())
#define GEARY_APP_BATCH_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEARY_APP_TYPE_BATCH_OPERATION, GearyAppBatchOperation))
#define GEARY_APP_BATCH_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEARY_APP_TYPE_BATCH_OPERATION, GearyAppBatchOperationClass))
#define GEARY_APP_IS_BATCH_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEARY_APP_TYPE_BATCH_OPERATION))
#define GEARY_APP_IS_BATCH_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEARY_APP_TYPE_BATCH_OPERATION))
#define GEARY_APP_BATCH_OPERATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEARY_APP_TYPE_BATCH_OPERATION, GearyAppBatchOperationClass))

typedef struct _GearyAppBatchOperation GearyAppBatchOperation;
typedef struct _GearyAppBatchOperationClass GearyAppBatchOperationClass;
typedef struct _GearyAppBatchOperationPrivate GearyAppBatchOperationPrivate;

#define GEARY_APP_TYPE_INSERT_OPERATION (geary_app_insert_operation_get_type ())
#define GEARY_APP_INSERT_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEARY_APP_TYPE_INSERT_OPERATION, GearyAppInsertOperation))
#define GEARY_APP_INSERT_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEARY_APP_TYPE_INSERT_OPERATION, GearyAppInsertOperationClass))
#define GEARY_APP_IS_INSERT_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEARY_APP_TYPE_INSERT_OPERATION))
#define GEARY_APP_IS_INSERT_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEARY_APP_TYPE_INSERT_OPERATION))
#define GEARY_APP_INSERT_OPERATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEARY_APP_TYPE_INSERT_OPERATION, GearyAppInsertOperationClass))

typedef struct _GearyAppInsertOperation GearyAppInsertOperation;
typedef struct _GearyAppInsertOperationClass GearyAppInsertOperationClass;
typedef struct _GearyAppInsertOperationPrivate GearyAppInsertOperationPrivate;
enum  {
	GEARY_APP_INSERT_OPERATION_0_PROPERTY,
	GEARY_APP_INSERT_OPERATION_NUM_PROPERTIES
};
static GParamSpec* geary_app_insert_operation_properties[GEARY_APP_INSERT_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 _GearyAppInsertOperationExecuteBatchData GearyAppInsertOperationExecuteBatchData;

struct _GearyAppConversationOperation {
	GearyBaseObject parent_instance;
	GearyAppConversationMonitor* monitor;
	GearyAppConversationOperationPrivate * priv;
};

struct _GearyAppConversationOperationClass {
	GearyBaseObjectClass parent_class;
	void (*execute_async) (GearyAppConversationOperation* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
	void (*execute_finish) (GearyAppConversationOperation* self, GAsyncResult* _res_, GError** error);
};

struct _GearyAppBatchOperation {
	GearyAppConversationOperation parent_instance;
	GearyAppBatchOperationPrivate * priv;
};

struct _GearyAppBatchOperationClass {
	GearyAppConversationOperationClass parent_class;
	void (*execute_batch) (GearyAppBatchOperation* self, GeeCollection* batch, GAsyncReadyCallback _callback_, gpointer _user_data_);
	void (*execute_batch_finish) (GearyAppBatchOperation* self, GAsyncResult* _res_, GError** error);
};

struct _GearyAppInsertOperation {
	GearyAppBatchOperation parent_instance;
	GearyAppInsertOperationPrivate * priv;
};

struct _GearyAppInsertOperationClass {
	GearyAppBatchOperationClass parent_class;
};

struct _GearyAppInsertOperationExecuteBatchData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GearyAppInsertOperation* self;
	GeeCollection* batch;
	gboolean needs_more;
	GearyAppConversationMonitor* _tmp0_;
	gboolean _tmp1_;
	gboolean _tmp2_;
	GearyEmailIdentifier* lowest;
	GearyAppConversationMonitor* _tmp3_;
	GearyEmailIdentifier* _tmp4_;
	GearyEmailIdentifier* _tmp5_;
	GearyEmailIdentifier* _tmp6_;
	GeeIterator* iter;
	GeeIterator* _tmp7_;
	GeeIterator* _tmp8_;
	GearyEmailIdentifier* inserted;
	GeeIterator* _tmp9_;
	gpointer _tmp10_;
	gboolean _tmp11_;
	GearyEmailIdentifier* _tmp12_;
	GearyEmailIdentifier* _tmp13_;
	GeeIterator* _tmp14_;
	gboolean _tmp15_;
	gboolean _tmp16_;
	gint _tmp17_;
	gint _tmp18_;
	GearyAppConversationMonitor* _tmp19_;
	GearyFolder* _tmp20_;
	GearyFolder* _tmp21_;
	gchar* _tmp22_;
	gchar* _tmp23_;
	GearyAppConversationMonitor* _tmp24_;
	GearyAppConversationMonitor* _tmp25_;
	GearyFolder* _tmp26_;
	GearyFolder* _tmp27_;
	gchar* _tmp28_;
	gchar* _tmp29_;
	GError* _inner_error0_;
};

static gpointer geary_app_insert_operation_parent_class = NULL;

VALA_EXTERN GType geary_app_conversation_operation_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType geary_app_batch_operation_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType geary_app_insert_operation_get_type (void) G_GNUC_CONST ;
VALA_EXTERN void geary_app_batch_operation_execute_batch (GearyAppBatchOperation* self,
                                              GeeCollection* batch,
                                              GAsyncReadyCallback _callback_,
                                              gpointer _user_data_);
VALA_EXTERN void geary_app_batch_operation_execute_batch_finish (GearyAppBatchOperation* self,
                                                     GAsyncResult* _res_,
                                                     GError** error);
VALA_EXTERN GearyAppInsertOperation* geary_app_insert_operation_new (GearyAppConversationMonitor* monitor,
                                                         GeeCollection* inserted_ids);
VALA_EXTERN GearyAppInsertOperation* geary_app_insert_operation_construct (GType object_type,
                                                               GearyAppConversationMonitor* monitor,
                                                               GeeCollection* inserted_ids);
VALA_EXTERN GearyAppBatchOperation* geary_app_batch_operation_construct (GType object_type,
                                                             GType t_type,
                                                             GBoxedCopyFunc t_dup_func,
                                                             GDestroyNotify t_destroy_func,
                                                             GearyAppConversationMonitor* monitor,
                                                             GeeCollection* full);
static void geary_app_insert_operation_real_execute_batch_data_free (gpointer _data);
static void geary_app_insert_operation_real_execute_batch (GearyAppBatchOperation* base,
                                                    GeeCollection* batch,
                                                    GAsyncReadyCallback _callback_,
                                                    gpointer _user_data_);
static gboolean geary_app_insert_operation_real_execute_batch_co (GearyAppInsertOperationExecuteBatchData* _data_);
VALA_EXTERN GearyEmailIdentifier* geary_app_conversation_monitor_get_window_lowest (GearyAppConversationMonitor* self);
VALA_EXTERN void geary_app_conversation_monitor_load_by_sparse_id (GearyAppConversationMonitor* self,
                                                       GeeCollection* ids,
                                                       GearyFolderListFlags flags,
                                                       GAsyncReadyCallback _callback_,
                                                       gpointer _user_data_);
VALA_EXTERN void geary_app_conversation_monitor_load_by_sparse_id_finish (GearyAppConversationMonitor* self,
                                                              GAsyncResult* _res_,
                                                              GError** error);
static void geary_app_insert_operation_execute_batch_ready (GObject* source_object,
                                                     GAsyncResult* _res_,
                                                     gpointer _user_data_);
static GType geary_app_insert_operation_get_type_once (void);

GearyAppInsertOperation*
geary_app_insert_operation_construct (GType object_type,
                                      GearyAppConversationMonitor* monitor,
                                      GeeCollection* inserted_ids)
{
	GearyAppInsertOperation * self = NULL;
	g_return_val_if_fail (GEARY_APP_IS_CONVERSATION_MONITOR (monitor), NULL);
	g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (inserted_ids, GEE_TYPE_COLLECTION), NULL);
	self = (GearyAppInsertOperation*) geary_app_batch_operation_construct (object_type, GEARY_TYPE_EMAIL_IDENTIFIER, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, monitor, inserted_ids);
	return self;
}

GearyAppInsertOperation*
geary_app_insert_operation_new (GearyAppConversationMonitor* monitor,
                                GeeCollection* inserted_ids)
{
	return geary_app_insert_operation_construct (GEARY_APP_TYPE_INSERT_OPERATION, monitor, inserted_ids);
}

static void
geary_app_insert_operation_real_execute_batch_data_free (gpointer _data)
{
	GearyAppInsertOperationExecuteBatchData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->batch);
	_g_object_unref0 (_data_->self);
	g_slice_free (GearyAppInsertOperationExecuteBatchData, _data_);
}

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

static void
geary_app_insert_operation_real_execute_batch (GearyAppBatchOperation* base,
                                               GeeCollection* batch,
                                               GAsyncReadyCallback _callback_,
                                               gpointer _user_data_)
{
	GearyAppInsertOperation * self;
	GearyAppInsertOperationExecuteBatchData* _data_;
	GearyAppInsertOperation* _tmp0_;
	GeeCollection* _tmp1_;
	g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (batch, GEE_TYPE_COLLECTION));
	self = G_TYPE_CHECK_INSTANCE_CAST (base, GEARY_APP_TYPE_INSERT_OPERATION, GearyAppInsertOperation);
	_data_ = g_slice_new0 (GearyAppInsertOperationExecuteBatchData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, geary_app_insert_operation_real_execute_batch_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	_tmp1_ = _g_object_ref0 (batch);
	_g_object_unref0 (_data_->batch);
	_data_->batch = _tmp1_;
	geary_app_insert_operation_real_execute_batch_co (_data_);
}

static void
geary_app_insert_operation_execute_batch_finish (GearyAppBatchOperation* base,
                                                 GAsyncResult* _res_,
                                                 GError** error)
{
	GearyAppInsertOperationExecuteBatchData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), error);
	if (NULL == _data_) {
		return;
	}
}

static void
geary_app_insert_operation_execute_batch_ready (GObject* source_object,
                                                GAsyncResult* _res_,
                                                gpointer _user_data_)
{
	GearyAppInsertOperationExecuteBatchData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	geary_app_insert_operation_real_execute_batch_co (_data_);
}

static gboolean
geary_app_insert_operation_real_execute_batch_co (GearyAppInsertOperationExecuteBatchData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
	_data_->_tmp1_ = geary_app_conversation_monitor_get_should_load_more (_data_->_tmp0_);
	_data_->_tmp2_ = _data_->_tmp1_;
	_data_->needs_more = _data_->_tmp2_;
	_data_->_tmp3_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
	_data_->_tmp4_ = geary_app_conversation_monitor_get_window_lowest (_data_->_tmp3_);
	_data_->_tmp5_ = _data_->_tmp4_;
	_data_->lowest = _data_->_tmp5_;
	_data_->_tmp6_ = _data_->lowest;
	if (_data_->_tmp6_ != NULL) {
		_data_->_tmp7_ = gee_iterable_iterator (G_TYPE_CHECK_INSTANCE_CAST (_data_->batch, GEE_TYPE_ITERABLE, GeeIterable));
		_data_->iter = _data_->_tmp7_;
		while (TRUE) {
			_data_->_tmp8_ = _data_->iter;
			if (!gee_iterator_next (_data_->_tmp8_)) {
				break;
			}
			_data_->_tmp9_ = _data_->iter;
			_data_->_tmp10_ = gee_iterator_get (_data_->_tmp9_);
			_data_->inserted = (GearyEmailIdentifier*) _data_->_tmp10_;
			if (!_data_->needs_more) {
				_data_->_tmp12_ = _data_->lowest;
				_data_->_tmp13_ = _data_->inserted;
				_data_->_tmp11_ = geary_email_identifier_natural_sort_comparator (_data_->_tmp12_, _data_->_tmp13_) > 0;
			} else {
				_data_->_tmp11_ = FALSE;
			}
			if (_data_->_tmp11_) {
				_data_->_tmp14_ = _data_->iter;
				gee_iterator_remove (_data_->_tmp14_);
			}
			_g_object_unref0 (_data_->inserted);
		}
		_g_object_unref0 (_data_->iter);
	}
	_data_->_tmp15_ = gee_collection_get_is_empty (_data_->batch);
	_data_->_tmp16_ = _data_->_tmp15_;
	if (!_data_->_tmp16_) {
		_data_->_tmp17_ = gee_collection_get_size (_data_->batch);
		_data_->_tmp18_ = _data_->_tmp17_;
		_data_->_tmp19_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
		_data_->_tmp20_ = geary_app_conversation_monitor_get_base_folder (_data_->_tmp19_);
		_data_->_tmp21_ = _data_->_tmp20_;
		_data_->_tmp22_ = geary_logging_source_to_string (G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp21_, GEARY_LOGGING_TYPE_SOURCE, GearyLoggingSource));
		_data_->_tmp23_ = _data_->_tmp22_;
		g_debug ("app-insert-operation.vala:38: Inserting %u messages into %s", (guint) _data_->_tmp18_, _data_->_tmp23_);
		_g_free0 (_data_->_tmp23_);
		_data_->_tmp24_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
		_data_->_state_ = 1;
		geary_app_conversation_monitor_load_by_sparse_id (_data_->_tmp24_, _data_->batch, GEARY_FOLDER_LIST_FLAGS_NONE, geary_app_insert_operation_execute_batch_ready, _data_);
		return FALSE;
		_state_1:
		geary_app_conversation_monitor_load_by_sparse_id_finish (_data_->_tmp24_, _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_->lowest);
			g_object_unref (_data_->_async_result);
			return FALSE;
		}
	} else {
		_data_->_tmp25_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
		_data_->_tmp26_ = geary_app_conversation_monitor_get_base_folder (_data_->_tmp25_);
		_data_->_tmp27_ = _data_->_tmp26_;
		_data_->_tmp28_ = geary_logging_source_to_string (G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp27_, GEARY_LOGGING_TYPE_SOURCE, GearyLoggingSource));
		_data_->_tmp29_ = _data_->_tmp28_;
		g_debug ("app-insert-operation.vala:43: Inserting no messages into %s, none need" \
"ed", _data_->_tmp29_);
		_g_free0 (_data_->_tmp29_);
	}
	_g_object_unref0 (_data_->lowest);
	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
geary_app_insert_operation_class_init (GearyAppInsertOperationClass * klass,
                                       gpointer klass_data)
{
	geary_app_insert_operation_parent_class = g_type_class_peek_parent (klass);
	((GearyAppBatchOperationClass *) klass)->execute_batch = (void (*) (GearyAppBatchOperation*, GeeCollection*, GAsyncReadyCallback, gpointer)) geary_app_insert_operation_real_execute_batch;
	((GearyAppBatchOperationClass *) klass)->execute_batch_finish = (void (*) (GearyAppBatchOperation*, GAsyncResult*, GError**)) geary_app_insert_operation_execute_batch_finish;
}

static void
geary_app_insert_operation_instance_init (GearyAppInsertOperation * self,
                                          gpointer klass)
{
}

/**
 * Handles an insertion of messages from a monitor's base folder.
 */
 G_GNUC_NO_INLINE static GType
geary_app_insert_operation_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GearyAppInsertOperationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) geary_app_insert_operation_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GearyAppInsertOperation), 0, (GInstanceInitFunc) geary_app_insert_operation_instance_init, NULL };
	GType geary_app_insert_operation_type_id;
	geary_app_insert_operation_type_id = g_type_register_static (GEARY_APP_TYPE_BATCH_OPERATION, "GearyAppInsertOperation", &g_define_type_info, 0);
	return geary_app_insert_operation_type_id;
}

GType
geary_app_insert_operation_get_type (void)
{
	static gsize geary_app_insert_operation_type_id__once = 0;
	if (g_once_init_enter (&geary_app_insert_operation_type_id__once)) {
		GType geary_app_insert_operation_type_id;
		geary_app_insert_operation_type_id = geary_app_insert_operation_get_type_once ();
		g_once_init_leave (&geary_app_insert_operation_type_id__once, geary_app_insert_operation_type_id);
	}
	return geary_app_insert_operation_type_id__once;
}

