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

#include "xmpp-vala.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gee.h>

#define XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_HINT_NO_PERMANENT_STORE "no-permanent-store"
#define XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_HINT_NO_STORE "no-store"
#define XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_HINT_NO_COPY "no-copy"
#define XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_HINT_STORE "store"
#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  {
	XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_MODULE_0_PROPERTY,
	XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_MODULE_NUM_PROPERTIES
};
static GParamSpec* xmpp_xep_unique_stable_stanza_ids_module_properties[XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_MODULE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _xmpp_stanza_entry_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_stanza_entry_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

static gpointer xmpp_xep_unique_stable_stanza_ids_module_parent_class = NULL;
XmppModuleIdentity* xmpp_xep_unique_stable_stanza_ids_module_IDENTITY = NULL;

static void xmpp_xep_unique_stable_stanza_ids_module_real_attach (XmppXmppStreamModule* base,
                                                           XmppXmppStream* stream);
static void xmpp_xep_unique_stable_stanza_ids_module_real_detach (XmppXmppStreamModule* base,
                                                           XmppXmppStream* stream);
static gchar* xmpp_xep_unique_stable_stanza_ids_module_real_get_ns (XmppXmppStreamModule* base);
static gchar* xmpp_xep_unique_stable_stanza_ids_module_real_get_id (XmppXmppStreamModule* base);
static void xmpp_xep_unique_stable_stanza_ids_module_finalize (GObject * obj);
static GType xmpp_xep_unique_stable_stanza_ids_module_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static void
xmpp_xep_unique_stable_stanza_ids_module_real_attach (XmppXmppStreamModule* base,
                                                      XmppXmppStream* stream)
{
	XmppXepUniqueStableStanzaIDsModule * self;
	XmppModuleIdentity* _tmp0_;
	gpointer _tmp1_;
	XmppXepServiceDiscoveryModule* _tmp2_;
	self = (XmppXepUniqueStableStanzaIDsModule*) base;
	g_return_if_fail (stream != NULL);
	_tmp0_ = xmpp_xep_service_discovery_module_IDENTITY;
	_tmp1_ = xmpp_xmpp_stream_get_module (stream, XMPP_XEP_SERVICE_DISCOVERY_TYPE_MODULE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp0_);
	_tmp2_ = (XmppXepServiceDiscoveryModule*) _tmp1_;
	xmpp_xep_service_discovery_module_add_feature (_tmp2_, stream, XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_NS_URI);
	_g_object_unref0 (_tmp2_);
}

static void
xmpp_xep_unique_stable_stanza_ids_module_real_detach (XmppXmppStreamModule* base,
                                                      XmppXmppStream* stream)
{
	XmppXepUniqueStableStanzaIDsModule * self;
	XmppModuleIdentity* _tmp0_;
	gpointer _tmp1_;
	XmppXepServiceDiscoveryModule* _tmp2_;
	self = (XmppXepUniqueStableStanzaIDsModule*) base;
	g_return_if_fail (stream != NULL);
	_tmp0_ = xmpp_xep_service_discovery_module_IDENTITY;
	_tmp1_ = xmpp_xmpp_stream_get_module (stream, XMPP_XEP_SERVICE_DISCOVERY_TYPE_MODULE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp0_);
	_tmp2_ = (XmppXepServiceDiscoveryModule*) _tmp1_;
	xmpp_xep_service_discovery_module_remove_feature (_tmp2_, stream, XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_NS_URI);
	_g_object_unref0 (_tmp2_);
}

static gchar*
xmpp_xep_unique_stable_stanza_ids_module_real_get_ns (XmppXmppStreamModule* base)
{
	XmppXepUniqueStableStanzaIDsModule * self;
	gchar* _tmp0_;
	gchar* result;
	self = (XmppXepUniqueStableStanzaIDsModule*) base;
	_tmp0_ = g_strdup (XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_NS_URI);
	result = _tmp0_;
	return result;
}

static gchar*
xmpp_xep_unique_stable_stanza_ids_module_real_get_id (XmppXmppStreamModule* base)
{
	XmppXepUniqueStableStanzaIDsModule * self;
	XmppModuleIdentity* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* result;
	self = (XmppXepUniqueStableStanzaIDsModule*) base;
	_tmp0_ = xmpp_xep_unique_stable_stanza_ids_module_IDENTITY;
	_tmp1_ = xmpp_module_identity_get_id (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strdup (_tmp2_);
	result = _tmp3_;
	return result;
}

XmppXepUniqueStableStanzaIDsModule*
xmpp_xep_unique_stable_stanza_ids_module_construct (GType object_type)
{
	XmppXepUniqueStableStanzaIDsModule * self = NULL;
	self = (XmppXepUniqueStableStanzaIDsModule*) xmpp_xmpp_stream_module_construct (object_type);
	return self;
}

XmppXepUniqueStableStanzaIDsModule*
xmpp_xep_unique_stable_stanza_ids_module_new (void)
{
	return xmpp_xep_unique_stable_stanza_ids_module_construct (XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_TYPE_MODULE);
}

static void
xmpp_xep_unique_stable_stanza_ids_module_class_init (XmppXepUniqueStableStanzaIDsModuleClass * klass,
                                                     gpointer klass_data)
{
	XmppModuleIdentity* _tmp0_;
	xmpp_xep_unique_stable_stanza_ids_module_parent_class = g_type_class_peek_parent (klass);
	((XmppXmppStreamModuleClass *) klass)->attach = (void (*) (XmppXmppStreamModule*, XmppXmppStream*)) xmpp_xep_unique_stable_stanza_ids_module_real_attach;
	((XmppXmppStreamModuleClass *) klass)->detach = (void (*) (XmppXmppStreamModule*, XmppXmppStream*)) xmpp_xep_unique_stable_stanza_ids_module_real_detach;
	((XmppXmppStreamModuleClass *) klass)->get_ns = (gchar* (*) (XmppXmppStreamModule*)) xmpp_xep_unique_stable_stanza_ids_module_real_get_ns;
	((XmppXmppStreamModuleClass *) klass)->get_id = (gchar* (*) (XmppXmppStreamModule*)) xmpp_xep_unique_stable_stanza_ids_module_real_get_id;
	G_OBJECT_CLASS (klass)->finalize = xmpp_xep_unique_stable_stanza_ids_module_finalize;
	_tmp0_ = xmpp_module_identity_new (XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_TYPE_MODULE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_NS_URI, "0359_unique_and_stable_stanza_ids");
	xmpp_xep_unique_stable_stanza_ids_module_IDENTITY = _tmp0_;
}

static void
xmpp_xep_unique_stable_stanza_ids_module_instance_init (XmppXepUniqueStableStanzaIDsModule * self,
                                                        gpointer klass)
{
}

static void
xmpp_xep_unique_stable_stanza_ids_module_finalize (GObject * obj)
{
	XmppXepUniqueStableStanzaIDsModule * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_TYPE_MODULE, XmppXepUniqueStableStanzaIDsModule);
	G_OBJECT_CLASS (xmpp_xep_unique_stable_stanza_ids_module_parent_class)->finalize (obj);
}

static GType
xmpp_xep_unique_stable_stanza_ids_module_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (XmppXepUniqueStableStanzaIDsModuleClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) xmpp_xep_unique_stable_stanza_ids_module_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (XmppXepUniqueStableStanzaIDsModule), 0, (GInstanceInitFunc) xmpp_xep_unique_stable_stanza_ids_module_instance_init, NULL };
	GType xmpp_xep_unique_stable_stanza_ids_module_type_id;
	xmpp_xep_unique_stable_stanza_ids_module_type_id = g_type_register_static (XMPP_TYPE_XMPP_STREAM_MODULE, "XmppXepUniqueStableStanzaIDsModule", &g_define_type_info, 0);
	return xmpp_xep_unique_stable_stanza_ids_module_type_id;
}

GType
xmpp_xep_unique_stable_stanza_ids_module_get_type (void)
{
	static volatile gsize xmpp_xep_unique_stable_stanza_ids_module_type_id__once = 0;
	if (g_once_init_enter (&xmpp_xep_unique_stable_stanza_ids_module_type_id__once)) {
		GType xmpp_xep_unique_stable_stanza_ids_module_type_id;
		xmpp_xep_unique_stable_stanza_ids_module_type_id = xmpp_xep_unique_stable_stanza_ids_module_get_type_once ();
		g_once_init_leave (&xmpp_xep_unique_stable_stanza_ids_module_type_id__once, xmpp_xep_unique_stable_stanza_ids_module_type_id);
	}
	return xmpp_xep_unique_stable_stanza_ids_module_type_id__once;
}

void
xmpp_xep_unique_stable_stanza_ids_set_origin_id (XmppMessageStanza* message,
                                                 const gchar* origin_id)
{
	XmppStanzaNode* hint_node = NULL;
	XmppStanzaNode* _tmp0_;
	XmppStanzaNode* _tmp1_;
	XmppStanzaNode* _tmp2_;
	XmppStanzaNode* _tmp3_;
	XmppStanzaNode* _tmp4_;
	XmppStanzaNode* _tmp5_;
	XmppStanzaNode* _tmp6_;
	XmppStanzaNode* _tmp7_;
	XmppStanzaNode* _tmp8_;
	g_return_if_fail (message != NULL);
	g_return_if_fail (origin_id != NULL);
	_tmp0_ = xmpp_stanza_node_new_build ("origin-id", XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_NS_URI, NULL, NULL);
	_tmp1_ = _tmp0_;
	_tmp2_ = xmpp_stanza_node_add_self_xmlns (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = xmpp_stanza_node_put_attribute (_tmp3_, "id", origin_id, NULL);
	_tmp5_ = _tmp4_;
	_xmpp_stanza_entry_unref0 (_tmp3_);
	_xmpp_stanza_entry_unref0 (_tmp1_);
	hint_node = _tmp5_;
	_tmp6_ = ((XmppStanza*) message)->stanza;
	_tmp7_ = xmpp_stanza_node_put_node (_tmp6_, hint_node);
	_tmp8_ = _tmp7_;
	_xmpp_stanza_entry_unref0 (_tmp8_);
	_xmpp_stanza_entry_unref0 (hint_node);
}

gchar*
xmpp_xep_unique_stable_stanza_ids_get_origin_id (XmppMessageStanza* message)
{
	XmppStanzaNode* node = NULL;
	XmppStanzaNode* _tmp0_;
	XmppStanzaNode* _tmp1_;
	XmppStanzaNode* _tmp2_;
	XmppStanzaNode* _tmp3_;
	const gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* result;
	g_return_val_if_fail (message != NULL, NULL);
	_tmp0_ = ((XmppStanza*) message)->stanza;
	_tmp1_ = xmpp_stanza_node_get_subnode (_tmp0_, "origin-id", XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_NS_URI, FALSE);
	node = _tmp1_;
	_tmp2_ = node;
	if (_tmp2_ == NULL) {
		result = NULL;
		_xmpp_stanza_entry_unref0 (node);
		return result;
	}
	_tmp3_ = node;
	_tmp4_ = xmpp_stanza_node_get_attribute (_tmp3_, "id", NULL);
	_tmp5_ = g_strdup (_tmp4_);
	result = _tmp5_;
	_xmpp_stanza_entry_unref0 (node);
	return result;
}

gchar*
xmpp_xep_unique_stable_stanza_ids_get_stanza_id (XmppMessageStanza* message,
                                                 XmppJid* by)
{
	gchar* by_str = NULL;
	gchar* _tmp0_;
	gchar* result;
	g_return_val_if_fail (message != NULL, NULL);
	g_return_val_if_fail (by != NULL, NULL);
	_tmp0_ = xmpp_jid_to_string (by);
	by_str = _tmp0_;
	{
		GeeList* _node_list = NULL;
		XmppStanzaNode* _tmp1_;
		GeeList* _tmp2_;
		gint _node_size = 0;
		GeeList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		gint _node_index = 0;
		_tmp1_ = ((XmppStanza*) message)->stanza;
		_tmp2_ = xmpp_stanza_node_get_subnodes (_tmp1_, "stanza-id", XMPP_XEP_UNIQUE_STABLE_STANZA_IDS_NS_URI, FALSE);
		_node_list = _tmp2_;
		_tmp3_ = _node_list;
		_tmp4_ = gee_collection_get_size ((GeeCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		_node_size = _tmp5_;
		_node_index = -1;
		while (TRUE) {
			gint _tmp6_;
			gint _tmp7_;
			XmppStanzaNode* node = NULL;
			GeeList* _tmp8_;
			gpointer _tmp9_;
			XmppStanzaNode* _tmp10_;
			const gchar* _tmp11_;
			const gchar* _tmp12_;
			_node_index = _node_index + 1;
			_tmp6_ = _node_index;
			_tmp7_ = _node_size;
			if (!(_tmp6_ < _tmp7_)) {
				break;
			}
			_tmp8_ = _node_list;
			_tmp9_ = gee_list_get (_tmp8_, _node_index);
			node = (XmppStanzaNode*) _tmp9_;
			_tmp10_ = node;
			_tmp11_ = xmpp_stanza_node_get_attribute (_tmp10_, "by", NULL);
			_tmp12_ = by_str;
			if (g_strcmp0 (_tmp11_, _tmp12_) == 0) {
				XmppStanzaNode* _tmp13_;
				const gchar* _tmp14_;
				gchar* _tmp15_;
				_tmp13_ = node;
				_tmp14_ = xmpp_stanza_node_get_attribute (_tmp13_, "id", NULL);
				_tmp15_ = g_strdup (_tmp14_);
				result = _tmp15_;
				_xmpp_stanza_entry_unref0 (node);
				_g_object_unref0 (_node_list);
				_g_free0 (by_str);
				return result;
			}
			_xmpp_stanza_entry_unref0 (node);
		}
		_g_object_unref0 (_node_list);
	}
	result = NULL;
	_g_free0 (by_str);
	return result;
}

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;
}

