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

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

#define XMPP_XEP_CHAT_STATE_NOTIFICATIONS_NS_URI "http://jabber.org/protocol/chatstates"
#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_CHAT_STATE_NOTIFICATIONS_MODULE_0_PROPERTY,
	XMPP_XEP_CHAT_STATE_NOTIFICATIONS_MODULE_NUM_PROPERTIES
};
static GParamSpec* xmpp_xep_chat_state_notifications_module_properties[XMPP_XEP_CHAT_STATE_NOTIFICATIONS_MODULE_NUM_PROPERTIES];
#define _xmpp_jid_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_jid_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#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)))
enum  {
	XMPP_XEP_CHAT_STATE_NOTIFICATIONS_MODULE_CHAT_STATE_RECEIVED_SIGNAL,
	XMPP_XEP_CHAT_STATE_NOTIFICATIONS_MODULE_NUM_SIGNALS
};
static guint xmpp_xep_chat_state_notifications_module_signals[XMPP_XEP_CHAT_STATE_NOTIFICATIONS_MODULE_NUM_SIGNALS] = {0};

static gpointer xmpp_xep_chat_state_notifications_module_parent_class = NULL;
XmppModuleIdentity* xmpp_xep_chat_state_notifications_module_IDENTITY = NULL;

static void xmpp_xep_chat_state_notifications_module_real_attach (XmppXmppStreamModule* base,
                                                           XmppXmppStream* stream);
static void xmpp_xep_chat_state_notifications_module_on_received_message (XmppXepChatStateNotificationsModule* self,
                                                                   XmppXmppStream* stream,
                                                                   XmppMessageStanza* message);
static void _xmpp_xep_chat_state_notifications_module_on_received_message_xmpp_message_module_received_message (XmppMessageModule* _sender,
                                                                                                         XmppXmppStream* stream,
                                                                                                         XmppMessageStanza* message,
                                                                                                         gpointer self);
static void xmpp_xep_chat_state_notifications_module_real_detach (XmppXmppStreamModule* base,
                                                           XmppXmppStream* stream);
static gchar* xmpp_xep_chat_state_notifications_module_real_get_ns (XmppXmppStreamModule* base);
static gchar* xmpp_xep_chat_state_notifications_module_real_get_id (XmppXmppStreamModule* base);
static gboolean _vala_string_array_contains (const gchar* * stack,
                                      gssize stack_length,
                                      const const gchar* needle);
static void g_cclosure_user_marshal_VOID__OBJECT_XMPP_JID_STRING_OBJECT (GClosure * closure,
                                                                  GValue * return_value,
                                                                  guint n_param_values,
                                                                  const GValue * param_values,
                                                                  gpointer invocation_hint,
                                                                  gpointer marshal_data);
static void xmpp_xep_chat_state_notifications_module_finalize (GObject * obj);
static GType xmpp_xep_chat_state_notifications_module_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

const gchar* XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATES[5] = {XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATE_ACTIVE, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATE_INACTIVE, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATE_GONE, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATE_COMPOSING, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATE_PAUSED};

/**
    * "A message stanza that does not contain standard messaging content [...] SHOULD be a state other than <active/>" (0085, 5.6)
    */
static gpointer
_xmpp_jid_ref0 (gpointer self)
{
	return self ? xmpp_jid_ref (self) : NULL;
}

void
xmpp_xep_chat_state_notifications_module_send_state (XmppXepChatStateNotificationsModule* self,
                                                     XmppXmppStream* stream,
                                                     XmppJid* jid,
                                                     const gchar* message_type,
                                                     const gchar* state)
{
	XmppMessageStanza* message = NULL;
	XmppJid* _tmp0_;
	gchar* _tmp1_;
	XmppMessageStanza* _tmp2_ = NULL;
	XmppModuleIdentity* _tmp3_;
	gpointer _tmp4_;
	XmppMessageModule* _tmp5_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (stream != NULL);
	g_return_if_fail (jid != NULL);
	g_return_if_fail (message_type != NULL);
	g_return_if_fail (state != NULL);
	_tmp0_ = _xmpp_jid_ref0 (jid);
	_tmp1_ = g_strdup (message_type);
	_tmp2_ = xmpp_message_stanza_new (NULL);
	xmpp_stanza_set_to ((XmppStanza*) _tmp2_, _tmp0_);
	_xmpp_jid_unref0 (_tmp0_);
	xmpp_stanza_set_type_ ((XmppStanza*) _tmp2_, _tmp1_);
	_g_free0 (_tmp1_);
	message = _tmp2_;
	xmpp_xep_chat_state_notifications_add_state_to_message (message, state);
	xmpp_xep_message_processing_hints_set_message_hint (message, XMPP_XEP_MESSAGE_PROCESSING_HINTS_HINT_NO_STORE);
	_tmp3_ = xmpp_message_module_IDENTITY;
	_tmp4_ = xmpp_xmpp_stream_get_module (stream, XMPP_TYPE_MESSAGE_MODULE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp3_);
	_tmp5_ = (XmppMessageModule*) _tmp4_;
	xmpp_message_module_send_message (_tmp5_, stream, message, NULL, NULL);
	_g_object_unref0 (_tmp5_);
	_g_object_unref0 (message);
}

static void
_xmpp_xep_chat_state_notifications_module_on_received_message_xmpp_message_module_received_message (XmppMessageModule* _sender,
                                                                                                    XmppXmppStream* stream,
                                                                                                    XmppMessageStanza* message,
                                                                                                    gpointer self)
{
	xmpp_xep_chat_state_notifications_module_on_received_message ((XmppXepChatStateNotificationsModule*) self, stream, message);
}

static void
xmpp_xep_chat_state_notifications_module_real_attach (XmppXmppStreamModule* base,
                                                      XmppXmppStream* stream)
{
	XmppXepChatStateNotificationsModule * self;
	XmppModuleIdentity* _tmp0_;
	gpointer _tmp1_;
	XmppXepServiceDiscoveryModule* _tmp2_;
	XmppModuleIdentity* _tmp3_;
	gpointer _tmp4_;
	XmppMessageModule* _tmp5_;
	self = (XmppXepChatStateNotificationsModule*) 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_CHAT_STATE_NOTIFICATIONS_NS_URI);
	_g_object_unref0 (_tmp2_);
	_tmp3_ = xmpp_message_module_IDENTITY;
	_tmp4_ = xmpp_xmpp_stream_get_module (stream, XMPP_TYPE_MESSAGE_MODULE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp3_);
	_tmp5_ = (XmppMessageModule*) _tmp4_;
	g_signal_connect_object (_tmp5_, "received-message", (GCallback) _xmpp_xep_chat_state_notifications_module_on_received_message_xmpp_message_module_received_message, self, 0);
	_g_object_unref0 (_tmp5_);
}

static void
xmpp_xep_chat_state_notifications_module_real_detach (XmppXmppStreamModule* base,
                                                      XmppXmppStream* stream)
{
	XmppXepChatStateNotificationsModule * self;
	XmppModuleIdentity* _tmp0_;
	gpointer _tmp1_;
	XmppXepServiceDiscoveryModule* _tmp2_;
	XmppModuleIdentity* _tmp3_;
	gpointer _tmp4_;
	XmppMessageModule* _tmp5_;
	guint _tmp6_;
	self = (XmppXepChatStateNotificationsModule*) 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_CHAT_STATE_NOTIFICATIONS_NS_URI);
	_g_object_unref0 (_tmp2_);
	_tmp3_ = xmpp_message_module_IDENTITY;
	_tmp4_ = xmpp_xmpp_stream_get_module (stream, XMPP_TYPE_MESSAGE_MODULE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp3_);
	_tmp5_ = (XmppMessageModule*) _tmp4_;
	g_signal_parse_name ("received-message", XMPP_TYPE_MESSAGE_MODULE, &_tmp6_, NULL, FALSE);
	g_signal_handlers_disconnect_matched (_tmp5_, G_SIGNAL_MATCH_ID | G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, _tmp6_, 0, NULL, (GCallback) _xmpp_xep_chat_state_notifications_module_on_received_message_xmpp_message_module_received_message, self);
	_g_object_unref0 (_tmp5_);
}

static gchar*
xmpp_xep_chat_state_notifications_module_real_get_ns (XmppXmppStreamModule* base)
{
	XmppXepChatStateNotificationsModule * self;
	gchar* _tmp0_;
	gchar* result;
	self = (XmppXepChatStateNotificationsModule*) base;
	_tmp0_ = g_strdup (XMPP_XEP_CHAT_STATE_NOTIFICATIONS_NS_URI);
	result = _tmp0_;
	return result;
}

static gchar*
xmpp_xep_chat_state_notifications_module_real_get_id (XmppXmppStreamModule* base)
{
	XmppXepChatStateNotificationsModule * self;
	XmppModuleIdentity* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* result;
	self = (XmppXepChatStateNotificationsModule*) base;
	_tmp0_ = xmpp_xep_chat_state_notifications_module_IDENTITY;
	_tmp1_ = xmpp_module_identity_get_id (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strdup (_tmp2_);
	result = _tmp3_;
	return result;
}

static gboolean
_vala_string_array_contains (const gchar* * stack,
                             gssize stack_length,
                             const const gchar* needle)
{
	gssize i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}

static void
xmpp_xep_chat_state_notifications_module_on_received_message (XmppXepChatStateNotificationsModule* self,
                                                              XmppXmppStream* stream,
                                                              XmppMessageStanza* message)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (stream != NULL);
	g_return_if_fail (message != NULL);
	if (!xmpp_stanza_is_error ((XmppStanza*) message)) {
		GeeList* nodes = NULL;
		XmppStanzaNode* _tmp0_;
		GeeList* _tmp1_;
		_tmp0_ = ((XmppStanza*) message)->stanza;
		_tmp1_ = xmpp_stanza_node_get_all_subnodes (_tmp0_);
		nodes = _tmp1_;
		{
			GeeList* _node_list = NULL;
			GeeList* _tmp2_;
			gint _node_size = 0;
			GeeList* _tmp3_;
			gint _tmp4_;
			gint _tmp5_;
			gint _node_index = 0;
			_tmp2_ = nodes;
			_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_;
				gboolean _tmp10_ = FALSE;
				XmppStanzaNode* _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_;
				_tmp11_ = node;
				_tmp12_ = ((XmppStanzaEntry*) _tmp11_)->ns_uri;
				if (g_strcmp0 (_tmp12_, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_NS_URI) == 0) {
					XmppStanzaNode* _tmp13_;
					const gchar* _tmp14_;
					_tmp13_ = node;
					_tmp14_ = ((XmppStanzaEntry*) _tmp13_)->name;
					_tmp10_ = _vala_string_array_contains (XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATES, G_N_ELEMENTS (XMPP_XEP_CHAT_STATE_NOTIFICATIONS_STATES), _tmp14_);
				} else {
					_tmp10_ = FALSE;
				}
				if (_tmp10_) {
					XmppJid* _tmp15_;
					XmppJid* _tmp16_;
					XmppJid* _tmp17_;
					XmppStanzaNode* _tmp18_;
					const gchar* _tmp19_;
					_tmp15_ = xmpp_stanza_get_from ((XmppStanza*) message);
					_tmp16_ = _tmp15_;
					_tmp17_ = _tmp16_;
					_tmp18_ = node;
					_tmp19_ = ((XmppStanzaEntry*) _tmp18_)->name;
					g_signal_emit (self, xmpp_xep_chat_state_notifications_module_signals[XMPP_XEP_CHAT_STATE_NOTIFICATIONS_MODULE_CHAT_STATE_RECEIVED_SIGNAL], 0, stream, _tmp17_, _tmp19_, message);
					_xmpp_jid_unref0 (_tmp17_);
				}
				_xmpp_stanza_entry_unref0 (node);
			}
		}
		_g_object_unref0 (nodes);
	}
}

XmppXepChatStateNotificationsModule*
xmpp_xep_chat_state_notifications_module_construct (GType object_type)
{
	XmppXepChatStateNotificationsModule * self = NULL;
	self = (XmppXepChatStateNotificationsModule*) xmpp_xmpp_stream_module_construct (object_type);
	return self;
}

XmppXepChatStateNotificationsModule*
xmpp_xep_chat_state_notifications_module_new (void)
{
	return xmpp_xep_chat_state_notifications_module_construct (XMPP_XEP_CHAT_STATE_NOTIFICATIONS_TYPE_MODULE);
}

static void
g_cclosure_user_marshal_VOID__OBJECT_XMPP_JID_STRING_OBJECT (GClosure * closure,
                                                             GValue * return_value,
                                                             guint n_param_values,
                                                             const GValue * param_values,
                                                             gpointer invocation_hint,
                                                             gpointer marshal_data)
{
	typedef void (*GMarshalFunc_VOID__OBJECT_XMPP_JID_STRING_OBJECT) (gpointer data1, gpointer arg_1, gpointer arg_2, const char* arg_3, gpointer arg_4, gpointer data2);
	register GMarshalFunc_VOID__OBJECT_XMPP_JID_STRING_OBJECT callback;
	register GCClosure * cc;
	register gpointer data1;
	register gpointer data2;
	cc = (GCClosure *) closure;
	g_return_if_fail (n_param_values == 5);
	if (G_CCLOSURE_SWAP_DATA (closure)) {
		data1 = closure->data;
		data2 = param_values->data[0].v_pointer;
	} else {
		data1 = param_values->data[0].v_pointer;
		data2 = closure->data;
	}
	callback = (GMarshalFunc_VOID__OBJECT_XMPP_JID_STRING_OBJECT) (marshal_data ? marshal_data : cc->callback);
	callback (data1, g_value_get_object (param_values + 1), xmpp_value_get_jid (param_values + 2), g_value_get_string (param_values + 3), g_value_get_object (param_values + 4), data2);
}

static void
xmpp_xep_chat_state_notifications_module_class_init (XmppXepChatStateNotificationsModuleClass * klass,
                                                     gpointer klass_data)
{
	XmppModuleIdentity* _tmp0_;
	xmpp_xep_chat_state_notifications_module_parent_class = g_type_class_peek_parent (klass);
	((XmppXmppStreamModuleClass *) klass)->attach = (void (*) (XmppXmppStreamModule*, XmppXmppStream*)) xmpp_xep_chat_state_notifications_module_real_attach;
	((XmppXmppStreamModuleClass *) klass)->detach = (void (*) (XmppXmppStreamModule*, XmppXmppStream*)) xmpp_xep_chat_state_notifications_module_real_detach;
	((XmppXmppStreamModuleClass *) klass)->get_ns = (gchar* (*) (XmppXmppStreamModule*)) xmpp_xep_chat_state_notifications_module_real_get_ns;
	((XmppXmppStreamModuleClass *) klass)->get_id = (gchar* (*) (XmppXmppStreamModule*)) xmpp_xep_chat_state_notifications_module_real_get_id;
	G_OBJECT_CLASS (klass)->finalize = xmpp_xep_chat_state_notifications_module_finalize;
	xmpp_xep_chat_state_notifications_module_signals[XMPP_XEP_CHAT_STATE_NOTIFICATIONS_MODULE_CHAT_STATE_RECEIVED_SIGNAL] = g_signal_new ("chat-state-received", XMPP_XEP_CHAT_STATE_NOTIFICATIONS_TYPE_MODULE, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_user_marshal_VOID__OBJECT_XMPP_JID_STRING_OBJECT, G_TYPE_NONE, 4, XMPP_TYPE_XMPP_STREAM, XMPP_TYPE_JID, G_TYPE_STRING, XMPP_TYPE_MESSAGE_STANZA);
	_tmp0_ = xmpp_module_identity_new (XMPP_XEP_CHAT_STATE_NOTIFICATIONS_TYPE_MODULE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_NS_URI, "0085_chat_state_notifications");
	xmpp_xep_chat_state_notifications_module_IDENTITY = _tmp0_;
}

static void
xmpp_xep_chat_state_notifications_module_instance_init (XmppXepChatStateNotificationsModule * self,
                                                        gpointer klass)
{
}

static void
xmpp_xep_chat_state_notifications_module_finalize (GObject * obj)
{
	XmppXepChatStateNotificationsModule * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_TYPE_MODULE, XmppXepChatStateNotificationsModule);
	G_OBJECT_CLASS (xmpp_xep_chat_state_notifications_module_parent_class)->finalize (obj);
}

static GType
xmpp_xep_chat_state_notifications_module_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (XmppXepChatStateNotificationsModuleClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) xmpp_xep_chat_state_notifications_module_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (XmppXepChatStateNotificationsModule), 0, (GInstanceInitFunc) xmpp_xep_chat_state_notifications_module_instance_init, NULL };
	GType xmpp_xep_chat_state_notifications_module_type_id;
	xmpp_xep_chat_state_notifications_module_type_id = g_type_register_static (XMPP_TYPE_XMPP_STREAM_MODULE, "XmppXepChatStateNotificationsModule", &g_define_type_info, 0);
	return xmpp_xep_chat_state_notifications_module_type_id;
}

GType
xmpp_xep_chat_state_notifications_module_get_type (void)
{
	static volatile gsize xmpp_xep_chat_state_notifications_module_type_id__once = 0;
	if (g_once_init_enter (&xmpp_xep_chat_state_notifications_module_type_id__once)) {
		GType xmpp_xep_chat_state_notifications_module_type_id;
		xmpp_xep_chat_state_notifications_module_type_id = xmpp_xep_chat_state_notifications_module_get_type_once ();
		g_once_init_leave (&xmpp_xep_chat_state_notifications_module_type_id__once, xmpp_xep_chat_state_notifications_module_type_id);
	}
	return xmpp_xep_chat_state_notifications_module_type_id__once;
}

void
xmpp_xep_chat_state_notifications_add_state_to_message (XmppMessageStanza* message,
                                                        const gchar* state)
{
	XmppStanzaNode* _tmp0_;
	XmppStanzaNode* _tmp1_;
	XmppStanzaNode* _tmp2_;
	XmppStanzaNode* _tmp3_;
	XmppStanzaNode* _tmp4_;
	XmppStanzaNode* _tmp5_;
	XmppStanzaNode* _tmp6_;
	g_return_if_fail (message != NULL);
	g_return_if_fail (state != NULL);
	_tmp0_ = ((XmppStanza*) message)->stanza;
	_tmp1_ = xmpp_stanza_node_new_build (state, XMPP_XEP_CHAT_STATE_NOTIFICATIONS_NS_URI, NULL, NULL);
	_tmp2_ = _tmp1_;
	_tmp3_ = xmpp_stanza_node_add_self_xmlns (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = xmpp_stanza_node_put_node (_tmp0_, _tmp4_);
	_tmp6_ = _tmp5_;
	_xmpp_stanza_entry_unref0 (_tmp6_);
	_xmpp_stanza_entry_unref0 (_tmp4_);
	_xmpp_stanza_entry_unref0 (_tmp2_);
}

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

