/* file_encryptor.c generated by valac 0.56.19, the Vala compiler
 * generated from file_encryptor.vala, do not modify */

#include "openpgp.h"
#include "libdino.h"
#include <glib-object.h>
#include <glib.h>
#include <gpgme.h>
#include <gpgme_fix.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include "xmpp-vala.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

enum  {
	DINO_PLUGINS_OPEN_PGP_PGP_FILE_ENCRYPTOR_0_PROPERTY,
	DINO_PLUGINS_OPEN_PGP_PGP_FILE_ENCRYPTOR_NUM_PROPERTIES
};
static GParamSpec* dino_plugins_open_pgp_pgp_file_encryptor_properties[DINO_PLUGINS_OPEN_PGP_PGP_FILE_ENCRYPTOR_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 _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _dino_file_meta_unref0(var) ((var == NULL) ? NULL : (var = (dino_file_meta_unref (var), NULL)))
#define _dino_file_send_data_unref0(var) ((var == NULL) ? NULL : (var = (dino_file_send_data_unref (var), NULL)))

struct _DinoPluginsOpenPgpPgpFileEncryptorPrivate {
	DinoStreamInteractor* stream_interactor;
};

static gint DinoPluginsOpenPgpPgpFileEncryptor_private_offset;
static gpointer dino_plugins_open_pgp_pgp_file_encryptor_parent_class = NULL;
static DinoFileEncryptorIface * dino_plugins_open_pgp_pgp_file_encryptor_dino_file_encryptor_parent_iface = NULL;

static gboolean dino_plugins_open_pgp_pgp_file_encryptor_real_can_encrypt_file (DinoFileEncryptor* base,
                                                                         DinoEntitiesConversation* conversation,
                                                                         DinoEntitiesFileTransfer* file_transfer);
static DinoFileMeta* dino_plugins_open_pgp_pgp_file_encryptor_real_encrypt_file (DinoFileEncryptor* base,
                                                                          DinoEntitiesConversation* conversation,
                                                                          DinoEntitiesFileTransfer* file_transfer,
                                                                          GError** error);
static guint8* _vala_array_dup2 (guint8* self,
                          gssize length);
static void _g_free_gdestroy_notify (void* data);
static DinoFileSendData* dino_plugins_open_pgp_pgp_file_encryptor_real_preprocess_send_file (DinoFileEncryptor* base,
                                                                                      DinoEntitiesConversation* conversation,
                                                                                      DinoEntitiesFileTransfer* file_transfer,
                                                                                      DinoFileSendData* file_send_data,
                                                                                      DinoFileMeta* file_meta,
                                                                                      GError** error);
static void dino_plugins_open_pgp_pgp_file_encryptor_finalize (GObject * obj);
static GType dino_plugins_open_pgp_pgp_file_encryptor_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
dino_plugins_open_pgp_pgp_file_encryptor_get_instance_private (DinoPluginsOpenPgpPgpFileEncryptor* self)
{
	return G_STRUCT_MEMBER_P (self, DinoPluginsOpenPgpPgpFileEncryptor_private_offset);
}

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

DinoPluginsOpenPgpPgpFileEncryptor*
dino_plugins_open_pgp_pgp_file_encryptor_construct (GType object_type,
                                                    DinoStreamInteractor* stream_interactor)
{
	DinoPluginsOpenPgpPgpFileEncryptor * self = NULL;
	DinoStreamInteractor* _tmp0_;
	g_return_val_if_fail (stream_interactor != NULL, NULL);
	self = (DinoPluginsOpenPgpPgpFileEncryptor*) g_object_new (object_type, NULL);
	_tmp0_ = _g_object_ref0 (stream_interactor);
	_g_object_unref0 (self->priv->stream_interactor);
	self->priv->stream_interactor = _tmp0_;
	return self;
}

DinoPluginsOpenPgpPgpFileEncryptor*
dino_plugins_open_pgp_pgp_file_encryptor_new (DinoStreamInteractor* stream_interactor)
{
	return dino_plugins_open_pgp_pgp_file_encryptor_construct (DINO_PLUGINS_OPEN_PGP_TYPE_PGP_FILE_ENCRYPTOR, stream_interactor);
}

static gboolean
dino_plugins_open_pgp_pgp_file_encryptor_real_can_encrypt_file (DinoFileEncryptor* base,
                                                                DinoEntitiesConversation* conversation,
                                                                DinoEntitiesFileTransfer* file_transfer)
{
	DinoPluginsOpenPgpPgpFileEncryptor * self;
	DinoEntitiesEncryption _tmp0_;
	DinoEntitiesEncryption _tmp1_;
	gboolean result;
	self = (DinoPluginsOpenPgpPgpFileEncryptor*) base;
	g_return_val_if_fail (conversation != NULL, FALSE);
	g_return_val_if_fail (file_transfer != NULL, FALSE);
	_tmp0_ = dino_entities_conversation_get_encryption (conversation);
	_tmp1_ = _tmp0_;
	result = _tmp1_ == DINO_ENTITIES_ENCRYPTION_PGP;
	return result;
}

static guint8*
_vala_array_dup2 (guint8* self,
                  gssize length)
{
	if (length > 0) {
		return _vala_memdup2 (self, length * sizeof (guint8));
	}
	return NULL;
}

static void
_g_free_gdestroy_notify (void* data)
{
	g_free (data);
}

static DinoFileMeta*
dino_plugins_open_pgp_pgp_file_encryptor_real_encrypt_file (DinoFileEncryptor* base,
                                                            DinoEntitiesConversation* conversation,
                                                            DinoEntitiesFileTransfer* file_transfer,
                                                            GError** error)
{
	DinoPluginsOpenPgpPgpFileEncryptor * self;
	DinoFileMeta* file_meta = NULL;
	DinoFileMeta* _tmp0_;
	const gchar* _tmp34_;
	const gchar* _tmp35_;
	const gchar* _tmp36_;
	const gchar* _tmp37_;
	GError* _inner_error0_ = NULL;
	DinoFileMeta* result;
	self = (DinoPluginsOpenPgpPgpFileEncryptor*) base;
	g_return_val_if_fail (conversation != NULL, NULL);
	g_return_val_if_fail (file_transfer != NULL, NULL);
	_tmp0_ = dino_file_meta_new ();
	file_meta = _tmp0_;
	{
		struct _gpgme_key** keys = NULL;
		DinoStreamInteractor* _tmp1_;
		DinoModuleIdentity* _tmp2_;
		gpointer _tmp3_;
		DinoPluginsOpenPgpManager* _tmp4_;
		gint _tmp5_ = 0;
		struct _gpgme_key** _tmp6_;
		struct _gpgme_key** _tmp7_;
		gint _tmp7__length1;
		gint keys_length1;
		gint _keys_size_;
		guint8* enc_content = NULL;
		GFile* _tmp8_;
		GFile* _tmp9_;
		gchar* _tmp10_;
		gchar* _tmp11_;
		struct _gpgme_key** _tmp12_;
		gint _tmp12__length1;
		const gchar* _tmp13_;
		const gchar* _tmp14_;
		gint _tmp15_ = 0;
		guint8* _tmp16_;
		guint8* _tmp17_;
		gint _tmp17__length1;
		gint enc_content_length1;
		gint _enc_content_size_;
		guint8* _tmp18_;
		gint _tmp18__length1;
		guint8* _tmp19_;
		gint _tmp19__length1;
		GMemoryInputStream* _tmp20_;
		GMemoryInputStream* _tmp21_;
		gchar* _tmp22_;
		gchar* _tmp23_;
		gchar* _tmp24_;
		gchar* _tmp25_;
		DinoFileMeta* _tmp26_;
		guint8* _tmp27_;
		gint _tmp27__length1;
		_tmp1_ = self->priv->stream_interactor;
		_tmp2_ = dino_plugins_open_pgp_manager_IDENTITY;
		_tmp3_ = dino_stream_interactor_get_module (_tmp1_, DINO_PLUGINS_OPEN_PGP_TYPE_MANAGER, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp2_);
		_tmp4_ = (DinoPluginsOpenPgpManager*) _tmp3_;
		_tmp6_ = dino_plugins_open_pgp_manager_get_key_fprs (_tmp4_, conversation, &_tmp5_, &_inner_error0_);
		_tmp7_ = _tmp6_;
		_tmp7__length1 = _tmp5_;
		_g_object_unref0 (_tmp4_);
		keys = _tmp7_;
		keys_length1 = _tmp7__length1;
		_keys_size_ = keys_length1;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp8_ = dino_entities_file_transfer_get_file (file_transfer);
		_tmp9_ = _tmp8_;
		_tmp10_ = g_file_get_path (_tmp9_);
		_tmp11_ = _tmp10_;
		_tmp12_ = keys;
		_tmp12__length1 = keys_length1;
		_tmp13_ = dino_entities_file_transfer_get_file_name (file_transfer);
		_tmp14_ = _tmp13_;
		_tmp16_ = gpg_helper_encrypt_file (_tmp11_, _tmp12_, (gint) _tmp12__length1, GPGME_ENCRYPT_ALWAYS_TRUST, _tmp14_, &_tmp15_, &_inner_error0_);
		_tmp17_ = _tmp16_;
		_tmp17__length1 = _tmp15_;
		_g_free0 (_tmp11_);
		_g_object_unref0 (_tmp9_);
		enc_content = _tmp17_;
		enc_content_length1 = _tmp17__length1;
		_enc_content_size_ = enc_content_length1;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			keys = (_vala_array_free (keys, keys_length1, (GDestroyNotify) gpgme_key_unref_vapi), NULL);
			goto __catch0_g_error;
		}
		_tmp18_ = enc_content;
		_tmp18__length1 = enc_content_length1;
		_tmp19_ = (_tmp18_ != NULL) ? _vala_array_dup2 (_tmp18_, _tmp18__length1) : _tmp18_;
		_tmp19__length1 = _tmp18__length1;
		_tmp20_ = (GMemoryInputStream*) g_memory_input_stream_new_from_data (_tmp19_, _tmp19__length1, _g_free_gdestroy_notify);
		_tmp21_ = _tmp20_;
		dino_entities_file_transfer_set_input_stream (file_transfer, (GInputStream*) _tmp21_);
		_g_object_unref0 (_tmp21_);
		dino_entities_file_transfer_set_encryption (file_transfer, DINO_ENTITIES_ENCRYPTION_PGP);
		_tmp22_ = xmpp_random_uuid ();
		_tmp23_ = _tmp22_;
		_tmp24_ = g_strconcat (_tmp23_, ".pgp", NULL);
		_tmp25_ = _tmp24_;
		dino_entities_file_transfer_set_server_file_name (file_transfer, _tmp25_);
		_g_free0 (_tmp25_);
		_g_free0 (_tmp23_);
		_tmp26_ = file_meta;
		_tmp27_ = enc_content;
		_tmp27__length1 = enc_content_length1;
		_tmp26_->size = (gint64) _tmp27__length1;
		enc_content = (g_free (enc_content), NULL);
		keys = (_vala_array_free (keys, keys_length1, (GDestroyNotify) gpgme_key_unref_vapi), NULL);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp28_;
		const gchar* _tmp29_;
		gchar* _tmp30_;
		gchar* _tmp31_;
		GError* _tmp32_;
		GError* _tmp33_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp28_ = e;
		_tmp29_ = _tmp28_->message;
		_tmp30_ = g_strdup_printf ("PGP file encryption error: %s", _tmp29_);
		_tmp31_ = _tmp30_;
		_tmp32_ = g_error_new_literal (DINO_FILE_SEND_ERROR, DINO_FILE_SEND_ERROR_ENCRYPTION_FAILED, _tmp31_);
		_tmp33_ = _tmp32_;
		_g_free0 (_tmp31_);
		_inner_error0_ = _tmp33_;
		_g_error_free0 (e);
		goto __finally0;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		if (_inner_error0_->domain == DINO_FILE_SEND_ERROR) {
			g_propagate_error (error, _inner_error0_);
			_dino_file_meta_unref0 (file_meta);
			return NULL;
		} else {
			_dino_file_meta_unref0 (file_meta);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
			g_clear_error (&_inner_error0_);
			return NULL;
		}
	}
	_tmp34_ = dino_entities_file_transfer_get_file_name (file_transfer);
	_tmp35_ = _tmp34_;
	_tmp36_ = dino_entities_file_transfer_get_server_file_name (file_transfer);
	_tmp37_ = _tmp36_;
	g_debug ("file_encryptor.vala:30: Encrypting file %s as %s", _tmp35_, _tmp37_);
	result = file_meta;
	return result;
}

static gpointer
_dino_file_send_data_ref0 (gpointer self)
{
	return self ? dino_file_send_data_ref (self) : NULL;
}

static DinoFileSendData*
dino_plugins_open_pgp_pgp_file_encryptor_real_preprocess_send_file (DinoFileEncryptor* base,
                                                                    DinoEntitiesConversation* conversation,
                                                                    DinoEntitiesFileTransfer* file_transfer,
                                                                    DinoFileSendData* file_send_data,
                                                                    DinoFileMeta* file_meta,
                                                                    GError** error)
{
	DinoPluginsOpenPgpPgpFileEncryptor * self;
	DinoHttpFileSendData* send_data = NULL;
	DinoHttpFileSendData* _tmp0_;
	DinoHttpFileSendData* _tmp1_;
	DinoHttpFileSendData* _tmp2_;
	DinoFileSendData* _tmp3_;
	DinoFileSendData* result;
	self = (DinoPluginsOpenPgpPgpFileEncryptor*) base;
	g_return_val_if_fail (conversation != NULL, NULL);
	g_return_val_if_fail (file_transfer != NULL, NULL);
	g_return_val_if_fail (file_send_data != NULL, NULL);
	g_return_val_if_fail (file_meta != NULL, NULL);
	_tmp0_ = _dino_file_send_data_ref0 (G_TYPE_CHECK_INSTANCE_TYPE (file_send_data, DINO_TYPE_HTTP_FILE_SEND_DATA) ? ((DinoHttpFileSendData*) file_send_data) : NULL);
	send_data = _tmp0_;
	_tmp1_ = send_data;
	if (_tmp1_ == NULL) {
		result = NULL;
		_dino_file_send_data_unref0 (send_data);
		return result;
	}
	_tmp2_ = send_data;
	dino_http_file_send_data_set_encrypt_message (_tmp2_, FALSE);
	_tmp3_ = _dino_file_send_data_ref0 (file_send_data);
	result = _tmp3_;
	_dino_file_send_data_unref0 (send_data);
	return result;
}

static void
dino_plugins_open_pgp_pgp_file_encryptor_class_init (DinoPluginsOpenPgpPgpFileEncryptorClass * klass,
                                                     gpointer klass_data)
{
	dino_plugins_open_pgp_pgp_file_encryptor_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &DinoPluginsOpenPgpPgpFileEncryptor_private_offset);
	G_OBJECT_CLASS (klass)->finalize = dino_plugins_open_pgp_pgp_file_encryptor_finalize;
}

static void
dino_plugins_open_pgp_pgp_file_encryptor_dino_file_encryptor_interface_init (DinoFileEncryptorIface * iface,
                                                                             gpointer iface_data)
{
	dino_plugins_open_pgp_pgp_file_encryptor_dino_file_encryptor_parent_iface = g_type_interface_peek_parent (iface);
	iface->can_encrypt_file = (gboolean (*) (DinoFileEncryptor*, DinoEntitiesConversation*, DinoEntitiesFileTransfer*)) dino_plugins_open_pgp_pgp_file_encryptor_real_can_encrypt_file;
	iface->encrypt_file = (DinoFileMeta* (*) (DinoFileEncryptor*, DinoEntitiesConversation*, DinoEntitiesFileTransfer*, GError**)) dino_plugins_open_pgp_pgp_file_encryptor_real_encrypt_file;
	iface->preprocess_send_file = (DinoFileSendData* (*) (DinoFileEncryptor*, DinoEntitiesConversation*, DinoEntitiesFileTransfer*, DinoFileSendData*, DinoFileMeta*, GError**)) dino_plugins_open_pgp_pgp_file_encryptor_real_preprocess_send_file;
}

static void
dino_plugins_open_pgp_pgp_file_encryptor_instance_init (DinoPluginsOpenPgpPgpFileEncryptor * self,
                                                        gpointer klass)
{
	self->priv = dino_plugins_open_pgp_pgp_file_encryptor_get_instance_private (self);
}

static void
dino_plugins_open_pgp_pgp_file_encryptor_finalize (GObject * obj)
{
	DinoPluginsOpenPgpPgpFileEncryptor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DINO_PLUGINS_OPEN_PGP_TYPE_PGP_FILE_ENCRYPTOR, DinoPluginsOpenPgpPgpFileEncryptor);
	_g_object_unref0 (self->priv->stream_interactor);
	G_OBJECT_CLASS (dino_plugins_open_pgp_pgp_file_encryptor_parent_class)->finalize (obj);
}

static GType
dino_plugins_open_pgp_pgp_file_encryptor_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DinoPluginsOpenPgpPgpFileEncryptorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dino_plugins_open_pgp_pgp_file_encryptor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DinoPluginsOpenPgpPgpFileEncryptor), 0, (GInstanceInitFunc) dino_plugins_open_pgp_pgp_file_encryptor_instance_init, NULL };
	static const GInterfaceInfo dino_file_encryptor_info = { (GInterfaceInitFunc) dino_plugins_open_pgp_pgp_file_encryptor_dino_file_encryptor_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType dino_plugins_open_pgp_pgp_file_encryptor_type_id;
	dino_plugins_open_pgp_pgp_file_encryptor_type_id = g_type_register_static (G_TYPE_OBJECT, "DinoPluginsOpenPgpPgpFileEncryptor", &g_define_type_info, 0);
	g_type_add_interface_static (dino_plugins_open_pgp_pgp_file_encryptor_type_id, DINO_TYPE_FILE_ENCRYPTOR, &dino_file_encryptor_info);
	DinoPluginsOpenPgpPgpFileEncryptor_private_offset = g_type_add_instance_private (dino_plugins_open_pgp_pgp_file_encryptor_type_id, sizeof (DinoPluginsOpenPgpPgpFileEncryptorPrivate));
	return dino_plugins_open_pgp_pgp_file_encryptor_type_id;
}

GType
dino_plugins_open_pgp_pgp_file_encryptor_get_type (void)
{
	static volatile gsize dino_plugins_open_pgp_pgp_file_encryptor_type_id__once = 0;
	if (g_once_init_enter (&dino_plugins_open_pgp_pgp_file_encryptor_type_id__once)) {
		GType dino_plugins_open_pgp_pgp_file_encryptor_type_id;
		dino_plugins_open_pgp_pgp_file_encryptor_type_id = dino_plugins_open_pgp_pgp_file_encryptor_get_type_once ();
		g_once_init_leave (&dino_plugins_open_pgp_pgp_file_encryptor_type_id__once, dino_plugins_open_pgp_pgp_file_encryptor_type_id);
	}
	return dino_plugins_open_pgp_pgp_file_encryptor_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

