/* app-external-append-operation.c generated by valac 0.56.3, the Vala compiler
 * generated from app-external-append-operation.vala, do not modify */

/*
 * Copyright 2016 Software Freedom Conservancy Inc.
 * Copyright 2019 Michael Gratton <mike@vee.net>
 *
 * 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>

#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_EXTERNAL_APPEND_OPERATION (geary_app_external_append_operation_get_type ())
#define GEARY_APP_EXTERNAL_APPEND_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEARY_APP_TYPE_EXTERNAL_APPEND_OPERATION, GearyAppExternalAppendOperation))
#define GEARY_APP_EXTERNAL_APPEND_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEARY_APP_TYPE_EXTERNAL_APPEND_OPERATION, GearyAppExternalAppendOperationClass))
#define GEARY_APP_IS_EXTERNAL_APPEND_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEARY_APP_TYPE_EXTERNAL_APPEND_OPERATION))
#define GEARY_APP_IS_EXTERNAL_APPEND_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEARY_APP_TYPE_EXTERNAL_APPEND_OPERATION))
#define GEARY_APP_EXTERNAL_APPEND_OPERATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEARY_APP_TYPE_EXTERNAL_APPEND_OPERATION, GearyAppExternalAppendOperationClass))

typedef struct _GearyAppExternalAppendOperation GearyAppExternalAppendOperation;
typedef struct _GearyAppExternalAppendOperationClass GearyAppExternalAppendOperationClass;
typedef struct _GearyAppExternalAppendOperationPrivate GearyAppExternalAppendOperationPrivate;
enum  {
	GEARY_APP_EXTERNAL_APPEND_OPERATION_0_PROPERTY,
	GEARY_APP_EXTERNAL_APPEND_OPERATION_NUM_PROPERTIES
};
static GParamSpec* geary_app_external_append_operation_properties[GEARY_APP_EXTERNAL_APPEND_OPERATION_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define GEARY_APP_TYPE_CONVERSATION_SET (geary_app_conversation_set_get_type ())
#define GEARY_APP_CONVERSATION_SET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEARY_APP_TYPE_CONVERSATION_SET, GearyAppConversationSet))
#define GEARY_APP_CONVERSATION_SET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEARY_APP_TYPE_CONVERSATION_SET, GearyAppConversationSetClass))
#define GEARY_APP_IS_CONVERSATION_SET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEARY_APP_TYPE_CONVERSATION_SET))
#define GEARY_APP_IS_CONVERSATION_SET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEARY_APP_TYPE_CONVERSATION_SET))
#define GEARY_APP_CONVERSATION_SET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEARY_APP_TYPE_CONVERSATION_SET, GearyAppConversationSetClass))

typedef struct _GearyAppConversationSet GearyAppConversationSet;
typedef struct _GearyAppConversationSetClass GearyAppConversationSetClass;
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _GearyAppExternalAppendOperationExecuteBatchData GearyAppExternalAppendOperationExecuteBatchData;

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 _GearyAppExternalAppendOperation {
	GearyAppBatchOperation parent_instance;
	GearyAppExternalAppendOperationPrivate * priv;
};

struct _GearyAppExternalAppendOperationClass {
	GearyAppBatchOperationClass parent_class;
};

struct _GearyAppExternalAppendOperationPrivate {
	GearyFolder* folder;
};

struct _GearyAppExternalAppendOperationExecuteBatchData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GearyAppExternalAppendOperation* self;
	GeeCollection* batch;
	gboolean _tmp0_;
	GearyAppConversationMonitor* _tmp1_;
	GeeCollection* _tmp2_;
	GeeCollection* _tmp3_;
	GearyFolder* _tmp4_;
	GearyFolderPath* _tmp5_;
	GearyFolderPath* _tmp6_;
	gboolean _tmp7_;
	GearyAppConversationMonitor* _tmp8_;
	GearyAppConversationSet* _tmp9_;
	GearyAppConversationSet* _tmp10_;
	gboolean _tmp11_;
	gboolean _tmp12_;
	gint _tmp13_;
	gint _tmp14_;
	GearyFolder* _tmp15_;
	gchar* _tmp16_;
	gchar* _tmp17_;
	GearyAppConversationMonitor* _tmp18_;
	GearyFolder* _tmp19_;
	GError* _inner_error0_;
};

static gint GearyAppExternalAppendOperation_private_offset;
static gpointer geary_app_external_append_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_external_append_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 GearyAppExternalAppendOperation* geary_app_external_append_operation_new (GearyAppConversationMonitor* monitor,
                                                                          GearyFolder* folder,
                                                                          GeeCollection* appended_ids);
VALA_EXTERN GearyAppExternalAppendOperation* geary_app_external_append_operation_construct (GType object_type,
                                                                                GearyAppConversationMonitor* monitor,
                                                                                GearyFolder* folder,
                                                                                GeeCollection* appended_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_external_append_operation_real_execute_batch_data_free (gpointer _data);
static void geary_app_external_append_operation_real_execute_batch (GearyAppBatchOperation* base,
                                                             GeeCollection* batch,
                                                             GAsyncReadyCallback _callback_,
                                                             gpointer _user_data_);
static gboolean geary_app_external_append_operation_real_execute_batch_co (GearyAppExternalAppendOperationExecuteBatchData* _data_);
VALA_EXTERN GeeCollection* geary_app_conversation_monitor_get_search_folder_blacklist (GearyAppConversationMonitor* self);
VALA_EXTERN GType geary_app_conversation_set_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GearyAppConversationSet* geary_app_conversation_monitor_get_conversations (GearyAppConversationMonitor* self);
VALA_EXTERN gboolean geary_app_conversation_set_get_is_empty (GearyAppConversationSet* self);
VALA_EXTERN void geary_app_conversation_monitor_external_load_by_sparse_id (GearyAppConversationMonitor* self,
                                                                GearyFolder* folder,
                                                                GeeCollection* ids,
                                                                GearyFolderListFlags flags,
                                                                GAsyncReadyCallback _callback_,
                                                                gpointer _user_data_);
VALA_EXTERN void geary_app_conversation_monitor_external_load_by_sparse_id_finish (GearyAppConversationMonitor* self,
                                                                       GAsyncResult* _res_,
                                                                       GError** error);
static void geary_app_external_append_operation_execute_batch_ready (GObject* source_object,
                                                              GAsyncResult* _res_,
                                                              gpointer _user_data_);
static void geary_app_external_append_operation_finalize (GObject * obj);
static GType geary_app_external_append_operation_get_type_once (void);

static inline gpointer
geary_app_external_append_operation_get_instance_private (GearyAppExternalAppendOperation* self)
{
	return G_STRUCT_MEMBER_P (self, GearyAppExternalAppendOperation_private_offset);
}

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

GearyAppExternalAppendOperation*
geary_app_external_append_operation_construct (GType object_type,
                                               GearyAppConversationMonitor* monitor,
                                               GearyFolder* folder,
                                               GeeCollection* appended_ids)
{
	GearyAppExternalAppendOperation * self = NULL;
	GearyFolder* _tmp0_;
	g_return_val_if_fail (GEARY_APP_IS_CONVERSATION_MONITOR (monitor), NULL);
	g_return_val_if_fail (GEARY_IS_FOLDER (folder), NULL);
	g_return_val_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (appended_ids, GEE_TYPE_COLLECTION), NULL);
	self = (GearyAppExternalAppendOperation*) geary_app_batch_operation_construct (object_type, GEARY_TYPE_EMAIL_IDENTIFIER, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, monitor, appended_ids);
	_tmp0_ = _g_object_ref0 (folder);
	_g_object_unref0 (self->priv->folder);
	self->priv->folder = _tmp0_;
	return self;
}

GearyAppExternalAppendOperation*
geary_app_external_append_operation_new (GearyAppConversationMonitor* monitor,
                                         GearyFolder* folder,
                                         GeeCollection* appended_ids)
{
	return geary_app_external_append_operation_construct (GEARY_APP_TYPE_EXTERNAL_APPEND_OPERATION, monitor, folder, appended_ids);
}

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

static void
geary_app_external_append_operation_real_execute_batch (GearyAppBatchOperation* base,
                                                        GeeCollection* batch,
                                                        GAsyncReadyCallback _callback_,
                                                        gpointer _user_data_)
{
	GearyAppExternalAppendOperation * self;
	GearyAppExternalAppendOperationExecuteBatchData* _data_;
	GearyAppExternalAppendOperation* _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_EXTERNAL_APPEND_OPERATION, GearyAppExternalAppendOperation);
	_data_ = g_slice_new0 (GearyAppExternalAppendOperationExecuteBatchData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, geary_app_external_append_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_external_append_operation_real_execute_batch_co (_data_);
}

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

static void
geary_app_external_append_operation_execute_batch_ready (GObject* source_object,
                                                         GAsyncResult* _res_,
                                                         gpointer _user_data_)
{
	GearyAppExternalAppendOperationExecuteBatchData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	geary_app_external_append_operation_real_execute_batch_co (_data_);
}

static gboolean
geary_app_external_append_operation_real_execute_batch_co (GearyAppExternalAppendOperationExecuteBatchData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp1_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
	_data_->_tmp2_ = geary_app_conversation_monitor_get_search_folder_blacklist (_data_->_tmp1_);
	_data_->_tmp3_ = _data_->_tmp2_;
	_data_->_tmp4_ = _data_->self->priv->folder;
	_data_->_tmp5_ = geary_folder_get_path (_data_->_tmp4_);
	_data_->_tmp6_ = _data_->_tmp5_;
	_data_->_tmp7_ = !gee_collection_contains (_data_->_tmp3_, _data_->_tmp6_);
	_g_object_unref0 (_data_->_tmp3_);
	if (_data_->_tmp7_) {
		_data_->_tmp8_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
		_data_->_tmp9_ = geary_app_conversation_monitor_get_conversations (_data_->_tmp8_);
		_data_->_tmp10_ = _data_->_tmp9_;
		_data_->_tmp11_ = geary_app_conversation_set_get_is_empty (_data_->_tmp10_);
		_data_->_tmp12_ = _data_->_tmp11_;
		_data_->_tmp0_ = !_data_->_tmp12_;
	} else {
		_data_->_tmp0_ = FALSE;
	}
	if (_data_->_tmp0_) {
		_data_->_tmp13_ = gee_collection_get_size (_data_->batch);
		_data_->_tmp14_ = _data_->_tmp13_;
		_data_->_tmp15_ = _data_->self->priv->folder;
		_data_->_tmp16_ = geary_logging_source_to_string (G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp15_, GEARY_LOGGING_TYPE_SOURCE, GearyLoggingSource));
		_data_->_tmp17_ = _data_->_tmp16_;
		g_debug ("app-external-append-operation.vala:26: Appending %d out of folder mess" \
"age(s) to %s", _data_->_tmp14_, _data_->_tmp17_);
		_g_free0 (_data_->_tmp17_);
		_data_->_tmp18_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
		_data_->_tmp19_ = _data_->self->priv->folder;
		_data_->_state_ = 1;
		geary_app_conversation_monitor_external_load_by_sparse_id (_data_->_tmp18_, _data_->_tmp19_, _data_->batch, GEARY_FOLDER_LIST_FLAGS_NONE, geary_app_external_append_operation_execute_batch_ready, _data_);
		return FALSE;
		_state_1:
		geary_app_conversation_monitor_external_load_by_sparse_id_finish (_data_->_tmp18_, _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;
}

static void
geary_app_external_append_operation_class_init (GearyAppExternalAppendOperationClass * klass,
                                                gpointer klass_data)
{
	geary_app_external_append_operation_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GearyAppExternalAppendOperation_private_offset);
	((GearyAppBatchOperationClass *) klass)->execute_batch = (void (*) (GearyAppBatchOperation*, GeeCollection*, GAsyncReadyCallback, gpointer)) geary_app_external_append_operation_real_execute_batch;
	((GearyAppBatchOperationClass *) klass)->execute_batch_finish = (void (*) (GearyAppBatchOperation*, GAsyncResult*, GError**)) geary_app_external_append_operation_execute_batch_finish;
	G_OBJECT_CLASS (klass)->finalize = geary_app_external_append_operation_finalize;
}

static void
geary_app_external_append_operation_instance_init (GearyAppExternalAppendOperation * self,
                                                   gpointer klass)
{
	self->priv = geary_app_external_append_operation_get_instance_private (self);
}

static void
geary_app_external_append_operation_finalize (GObject * obj)
{
	GearyAppExternalAppendOperation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GEARY_APP_TYPE_EXTERNAL_APPEND_OPERATION, GearyAppExternalAppendOperation);
	_g_object_unref0 (self->priv->folder);
	G_OBJECT_CLASS (geary_app_external_append_operation_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
geary_app_external_append_operation_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GearyAppExternalAppendOperationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) geary_app_external_append_operation_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GearyAppExternalAppendOperation), 0, (GInstanceInitFunc) geary_app_external_append_operation_instance_init, NULL };
	GType geary_app_external_append_operation_type_id;
	geary_app_external_append_operation_type_id = g_type_register_static (GEARY_APP_TYPE_BATCH_OPERATION, "GearyAppExternalAppendOperation", &g_define_type_info, 0);
	GearyAppExternalAppendOperation_private_offset = g_type_add_instance_private (geary_app_external_append_operation_type_id, sizeof (GearyAppExternalAppendOperationPrivate));
	return geary_app_external_append_operation_type_id;
}

GType
geary_app_external_append_operation_get_type (void)
{
	static gsize geary_app_external_append_operation_type_id__once = 0;
	if (g_once_init_enter (&geary_app_external_append_operation_type_id__once)) {
		GType geary_app_external_append_operation_type_id;
		geary_app_external_append_operation_type_id = geary_app_external_append_operation_get_type_once ();
		g_once_init_leave (&geary_app_external_append_operation_type_id__once, geary_app_external_append_operation_type_id);
	}
	return geary_app_external_append_operation_type_id__once;
}

