/* app-remove-operation.c generated by valac 0.56.3, the Vala compiler
 * generated from app-remove-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_REMOVE_OPERATION (geary_app_remove_operation_get_type ())
#define GEARY_APP_REMOVE_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEARY_APP_TYPE_REMOVE_OPERATION, GearyAppRemoveOperation))
#define GEARY_APP_REMOVE_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEARY_APP_TYPE_REMOVE_OPERATION, GearyAppRemoveOperationClass))
#define GEARY_APP_IS_REMOVE_OPERATION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEARY_APP_TYPE_REMOVE_OPERATION))
#define GEARY_APP_IS_REMOVE_OPERATION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEARY_APP_TYPE_REMOVE_OPERATION))
#define GEARY_APP_REMOVE_OPERATION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEARY_APP_TYPE_REMOVE_OPERATION, GearyAppRemoveOperationClass))

typedef struct _GearyAppRemoveOperation GearyAppRemoveOperation;
typedef struct _GearyAppRemoveOperationClass GearyAppRemoveOperationClass;
typedef struct _GearyAppRemoveOperationPrivate GearyAppRemoveOperationPrivate;
enum  {
	GEARY_APP_REMOVE_OPERATION_0_PROPERTY,
	GEARY_APP_REMOVE_OPERATION_NUM_PROPERTIES
};
static GParamSpec* geary_app_remove_operation_properties[GEARY_APP_REMOVE_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))

#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;
typedef struct _GearyAppRemoveOperationExecuteBatchData GearyAppRemoveOperationExecuteBatchData;

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 _GearyAppRemoveOperation {
	GearyAppBatchOperation parent_instance;
	GearyAppRemoveOperationPrivate * priv;
};

struct _GearyAppRemoveOperationClass {
	GearyAppBatchOperationClass parent_class;
};

struct _GearyAppRemoveOperationPrivate {
	GearyFolder* source_folder;
};

struct _GearyAppRemoveOperationExecuteBatchData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GearyAppRemoveOperation* self;
	GeeCollection* batch;
	gint _tmp0_;
	gint _tmp1_;
	GearyFolder* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	GeeSet* removed;
	GeeHashSet* _tmp5_;
	GeeMultiMap* trimmed;
	GeeHashMultiMap* _tmp6_;
	GearyAppConversationMonitor* _tmp7_;
	GearyAppConversationSet* _tmp8_;
	GearyAppConversationSet* _tmp9_;
	GearyFolder* _tmp10_;
	GearyFolderPath* _tmp11_;
	GearyFolderPath* _tmp12_;
	GeeSet* _tmp13_;
	GeeMultiMap* _tmp14_;
	GeeCollection* _tmp15_;
	GearyFolder* _tmp16_;
	GearyAppConversationMonitor* _tmp17_;
	GearyFolder* _tmp18_;
	GearyFolder* _tmp19_;
	GearyAppConversationMonitor* _tmp20_;
	GeeSet* _tmp21_;
	GeeMultiMap* _tmp22_;
	GearyAppConversationMonitor* _tmp23_;
};

static gint GearyAppRemoveOperation_private_offset;
static gpointer geary_app_remove_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_remove_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 GearyAppRemoveOperation* geary_app_remove_operation_new (GearyAppConversationMonitor* monitor,
                                                         GearyFolder* source_folder,
                                                         GeeCollection* removed_ids);
VALA_EXTERN GearyAppRemoveOperation* geary_app_remove_operation_construct (GType object_type,
                                                               GearyAppConversationMonitor* monitor,
                                                               GearyFolder* source_folder,
                                                               GeeCollection* removed_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_remove_operation_real_execute_batch_data_free (gpointer _data);
static void geary_app_remove_operation_real_execute_batch (GearyAppBatchOperation* base,
                                                    GeeCollection* batch,
                                                    GAsyncReadyCallback _callback_,
                                                    gpointer _user_data_);
static gboolean geary_app_remove_operation_real_execute_batch_co (GearyAppRemoveOperationExecuteBatchData* _data_);
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 void geary_app_conversation_set_remove_all_emails_by_identifier (GearyAppConversationSet* self,
                                                                 GearyFolderPath* source_path,
                                                                 GeeCollection* ids,
                                                                 GeeCollection* removed,
                                                                 GeeMultiMap* trimmed);
VALA_EXTERN void geary_app_conversation_monitor_removed (GearyAppConversationMonitor* self,
                                             GeeCollection* removed,
                                             GeeMultiMap* trimmed,
                                             GeeCollection* base_folder_removed);
VALA_EXTERN void geary_app_conversation_monitor_check_window_count (GearyAppConversationMonitor* self);
static void geary_app_remove_operation_finalize (GObject * obj);
static GType geary_app_remove_operation_get_type_once (void);

static inline gpointer
geary_app_remove_operation_get_instance_private (GearyAppRemoveOperation* self)
{
	return G_STRUCT_MEMBER_P (self, GearyAppRemoveOperation_private_offset);
}

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

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

GearyAppRemoveOperation*
geary_app_remove_operation_new (GearyAppConversationMonitor* monitor,
                                GearyFolder* source_folder,
                                GeeCollection* removed_ids)
{
	return geary_app_remove_operation_construct (GEARY_APP_TYPE_REMOVE_OPERATION, monitor, source_folder, removed_ids);
}

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

static void
geary_app_remove_operation_real_execute_batch (GearyAppBatchOperation* base,
                                               GeeCollection* batch,
                                               GAsyncReadyCallback _callback_,
                                               gpointer _user_data_)
{
	GearyAppRemoveOperation * self;
	GearyAppRemoveOperationExecuteBatchData* _data_;
	GearyAppRemoveOperation* _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_REMOVE_OPERATION, GearyAppRemoveOperation);
	_data_ = g_slice_new0 (GearyAppRemoveOperationExecuteBatchData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, geary_app_remove_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_remove_operation_real_execute_batch_co (_data_);
}

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

static gboolean
geary_app_remove_operation_real_execute_batch_co (GearyAppRemoveOperationExecuteBatchData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = gee_collection_get_size (_data_->batch);
	_data_->_tmp1_ = _data_->_tmp0_;
	_data_->_tmp2_ = _data_->self->priv->source_folder;
	_data_->_tmp3_ = geary_logging_source_to_string (G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp2_, GEARY_LOGGING_TYPE_SOURCE, GearyLoggingSource));
	_data_->_tmp4_ = _data_->_tmp3_;
	g_debug ("app-remove-operation.vala:24: Removing %d messages(s) from %s", _data_->_tmp1_, _data_->_tmp4_);
	_g_free0 (_data_->_tmp4_);
	_data_->_tmp5_ = gee_hash_set_new (GEARY_APP_TYPE_CONVERSATION, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL, NULL, NULL, NULL);
	_data_->removed = G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp5_, GEE_TYPE_SET, GeeSet);
	_data_->_tmp6_ = gee_hash_multi_map_new (GEARY_APP_TYPE_CONVERSATION, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, GEARY_TYPE_EMAIL, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
	_data_->trimmed = G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp6_, GEE_TYPE_MULTI_MAP, GeeMultiMap);
	_data_->_tmp7_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
	_data_->_tmp8_ = geary_app_conversation_monitor_get_conversations (_data_->_tmp7_);
	_data_->_tmp9_ = _data_->_tmp8_;
	_data_->_tmp10_ = _data_->self->priv->source_folder;
	_data_->_tmp11_ = geary_folder_get_path (_data_->_tmp10_);
	_data_->_tmp12_ = _data_->_tmp11_;
	_data_->_tmp13_ = _data_->removed;
	_data_->_tmp14_ = _data_->trimmed;
	geary_app_conversation_set_remove_all_emails_by_identifier (_data_->_tmp9_, _data_->_tmp12_, _data_->batch, G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp13_, GEE_TYPE_COLLECTION, GeeCollection), _data_->_tmp14_);
	_data_->_tmp16_ = _data_->self->priv->source_folder;
	_data_->_tmp17_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
	_data_->_tmp18_ = geary_app_conversation_monitor_get_base_folder (_data_->_tmp17_);
	_data_->_tmp19_ = _data_->_tmp18_;
	if (_data_->_tmp16_ == _data_->_tmp19_) {
		_data_->_tmp15_ = _data_->batch;
	} else {
		_data_->_tmp15_ = NULL;
	}
	_data_->_tmp20_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
	_data_->_tmp21_ = _data_->removed;
	_data_->_tmp22_ = _data_->trimmed;
	geary_app_conversation_monitor_removed (_data_->_tmp20_, G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp21_, GEE_TYPE_COLLECTION, GeeCollection), _data_->_tmp22_, _data_->_tmp15_);
	_data_->_tmp23_ = G_TYPE_CHECK_INSTANCE_CAST (_data_->self, GEARY_APP_TYPE_CONVERSATION_OPERATION, GearyAppConversationOperation)->monitor;
	geary_app_conversation_monitor_check_window_count (_data_->_tmp23_);
	_g_object_unref0 (_data_->trimmed);
	_g_object_unref0 (_data_->removed);
	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_remove_operation_class_init (GearyAppRemoveOperationClass * klass,
                                       gpointer klass_data)
{
	geary_app_remove_operation_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GearyAppRemoveOperation_private_offset);
	((GearyAppBatchOperationClass *) klass)->execute_batch = (void (*) (GearyAppBatchOperation*, GeeCollection*, GAsyncReadyCallback, gpointer)) geary_app_remove_operation_real_execute_batch;
	((GearyAppBatchOperationClass *) klass)->execute_batch_finish = (void (*) (GearyAppBatchOperation*, GAsyncResult*, GError**)) geary_app_remove_operation_execute_batch_finish;
	G_OBJECT_CLASS (klass)->finalize = geary_app_remove_operation_finalize;
}

static void
geary_app_remove_operation_instance_init (GearyAppRemoveOperation * self,
                                          gpointer klass)
{
	self->priv = geary_app_remove_operation_get_instance_private (self);
}

static void
geary_app_remove_operation_finalize (GObject * obj)
{
	GearyAppRemoveOperation * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GEARY_APP_TYPE_REMOVE_OPERATION, GearyAppRemoveOperation);
	_g_object_unref0 (self->priv->source_folder);
	G_OBJECT_CLASS (geary_app_remove_operation_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
geary_app_remove_operation_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GearyAppRemoveOperationClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) geary_app_remove_operation_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GearyAppRemoveOperation), 0, (GInstanceInitFunc) geary_app_remove_operation_instance_init, NULL };
	GType geary_app_remove_operation_type_id;
	geary_app_remove_operation_type_id = g_type_register_static (GEARY_APP_TYPE_BATCH_OPERATION, "GearyAppRemoveOperation", &g_define_type_info, 0);
	GearyAppRemoveOperation_private_offset = g_type_add_instance_private (geary_app_remove_operation_type_id, sizeof (GearyAppRemoveOperationPrivate));
	return geary_app_remove_operation_type_id;
}

GType
geary_app_remove_operation_get_type (void)
{
	static gsize geary_app_remove_operation_type_id__once = 0;
	if (g_once_init_enter (&geary_app_remove_operation_type_id__once)) {
		GType geary_app_remove_operation_type_id;
		geary_app_remove_operation_type_id = geary_app_remove_operation_get_type_once ();
		g_once_init_leave (&geary_app_remove_operation_type_id__once, geary_app_remove_operation_type_id);
	}
	return geary_app_remove_operation_type_id__once;
}

