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

#include <glib-object.h>
#include "libdino.h"
#include <gtk/gtk.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gdk/gdk.h>
#include "xmpp-vala.h"
#include <gobject/gvaluecollector.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR (dino_ui_occupants_tab_completor_get_type ())
#define DINO_UI_OCCUPANTS_TAB_COMPLETOR(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR, DinoUiOccupantsTabCompletor))
#define DINO_UI_OCCUPANTS_TAB_COMPLETOR_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR, DinoUiOccupantsTabCompletorClass))
#define DINO_UI_IS_OCCUPANTS_TAB_COMPLETOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR))
#define DINO_UI_IS_OCCUPANTS_TAB_COMPLETOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR))
#define DINO_UI_OCCUPANTS_TAB_COMPLETOR_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR, DinoUiOccupantsTabCompletorClass))

typedef struct _DinoUiOccupantsTabCompletor DinoUiOccupantsTabCompletor;
typedef struct _DinoUiOccupantsTabCompletorClass DinoUiOccupantsTabCompletorClass;
typedef struct _DinoUiOccupantsTabCompletorPrivate DinoUiOccupantsTabCompletorPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _xmpp_jid_unref0(var) ((var == NULL) ? NULL : (var = (xmpp_jid_unref (var), NULL)))
typedef struct _DinoUiParamSpecOccupantsTabCompletor DinoUiParamSpecOccupantsTabCompletor;

struct _DinoUiOccupantsTabCompletor {
	GTypeInstance parent_instance;
	volatile int ref_count;
	DinoUiOccupantsTabCompletorPrivate * priv;
};

struct _DinoUiOccupantsTabCompletorClass {
	GTypeClass parent_class;
	void (*finalize) (DinoUiOccupantsTabCompletor *self);
};

struct _DinoUiOccupantsTabCompletorPrivate {
	DinoStreamInteractor* stream_interactor;
	DinoEntitiesConversation* conversation;
	GtkTextView* text_input;
	GeeList* completions;
	gboolean active;
	gint index;
};

struct _DinoUiParamSpecOccupantsTabCompletor {
	GParamSpec parent_instance;
};

static gint DinoUiOccupantsTabCompletor_private_offset;
static gpointer dino_ui_occupants_tab_completor_parent_class = NULL;

VALA_EXTERN gpointer dino_ui_occupants_tab_completor_ref (gpointer instance);
VALA_EXTERN void dino_ui_occupants_tab_completor_unref (gpointer instance);
VALA_EXTERN GParamSpec* dino_ui_param_spec_occupants_tab_completor (const gchar* name,
                                                        const gchar* nick,
                                                        const gchar* blurb,
                                                        GType object_type,
                                                        GParamFlags flags);
VALA_EXTERN void dino_ui_value_set_occupants_tab_completor (GValue* value,
                                                gpointer v_object);
VALA_EXTERN void dino_ui_value_take_occupants_tab_completor (GValue* value,
                                                 gpointer v_object);
VALA_EXTERN gpointer dino_ui_value_get_occupants_tab_completor (const GValue* value);
VALA_EXTERN GType dino_ui_occupants_tab_completor_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (DinoUiOccupantsTabCompletor, dino_ui_occupants_tab_completor_unref)
VALA_EXTERN DinoUiOccupantsTabCompletor* dino_ui_occupants_tab_completor_new (DinoStreamInteractor* stream_interactor,
                                                                  GtkTextView* text_input);
VALA_EXTERN DinoUiOccupantsTabCompletor* dino_ui_occupants_tab_completor_construct (GType object_type,
                                                                        DinoStreamInteractor* stream_interactor,
                                                                        GtkTextView* text_input);
VALA_EXTERN gboolean dino_ui_occupants_tab_completor_on_text_input_key_press (DinoUiOccupantsTabCompletor* self,
                                                                  guint keyval,
                                                                  guint keycode,
                                                                  GdkModifierType state);
static gboolean _dino_ui_occupants_tab_completor_on_text_input_key_press_gtk_event_controller_key_key_pressed (GtkEventControllerKey* _sender,
                                                                                                        guint keyval,
                                                                                                        guint keycode,
                                                                                                        GdkModifierType state,
                                                                                                        gpointer self);
VALA_EXTERN void dino_ui_occupants_tab_completor_initialize_for_conversation (DinoUiOccupantsTabCompletor* self,
                                                                  DinoEntitiesConversation* conversation);
static GeeList* dino_ui_occupants_tab_completor_generate_completions_from_messages (DinoUiOccupantsTabCompletor* self,
                                                                             const gchar* prefix);
static GeeList* dino_ui_occupants_tab_completor_generate_completions_from_occupants (DinoUiOccupantsTabCompletor* self,
                                                                              const gchar* prefix);
static gchar* dino_ui_occupants_tab_completor_next_completion (DinoUiOccupantsTabCompletor* self,
                                                        gboolean backwards);
static void dino_ui_occupants_tab_completor_finalize (DinoUiOccupantsTabCompletor * obj);
static GType dino_ui_occupants_tab_completor_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
dino_ui_occupants_tab_completor_get_instance_private (DinoUiOccupantsTabCompletor* self)
{
	return G_STRUCT_MEMBER_P (self, DinoUiOccupantsTabCompletor_private_offset);
}

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

static gboolean
_dino_ui_occupants_tab_completor_on_text_input_key_press_gtk_event_controller_key_key_pressed (GtkEventControllerKey* _sender,
                                                                                               guint keyval,
                                                                                               guint keycode,
                                                                                               GdkModifierType state,
                                                                                               gpointer self)
{
	gboolean result;
	result = dino_ui_occupants_tab_completor_on_text_input_key_press ((DinoUiOccupantsTabCompletor*) self, keyval, keycode, state);
	return result;
}

DinoUiOccupantsTabCompletor*
dino_ui_occupants_tab_completor_construct (GType object_type,
                                           DinoStreamInteractor* stream_interactor,
                                           GtkTextView* text_input)
{
	DinoUiOccupantsTabCompletor* self = NULL;
	DinoStreamInteractor* _tmp0_;
	GtkTextView* _tmp1_;
	GtkEventControllerKey* text_input_key_events = NULL;
	GtkEventControllerKey* _tmp2_;
	GtkEventController* _tmp3_;
	g_return_val_if_fail (stream_interactor != NULL, NULL);
	g_return_val_if_fail (text_input != NULL, NULL);
	self = (DinoUiOccupantsTabCompletor*) g_type_create_instance (object_type);
	_tmp0_ = _g_object_ref0 (stream_interactor);
	_g_object_unref0 (self->priv->stream_interactor);
	self->priv->stream_interactor = _tmp0_;
	_tmp1_ = _g_object_ref0 (text_input);
	_g_object_unref0 (self->priv->text_input);
	self->priv->text_input = _tmp1_;
	_tmp2_ = (GtkEventControllerKey*) gtk_event_controller_key_new ();
	text_input_key_events = _tmp2_;
	g_signal_connect (text_input_key_events, "key-pressed", (GCallback) _dino_ui_occupants_tab_completor_on_text_input_key_press_gtk_event_controller_key_key_pressed, self);
	_tmp3_ = _g_object_ref0 ((GtkEventController*) text_input_key_events);
	gtk_widget_add_controller ((GtkWidget*) text_input, _tmp3_);
	_g_object_unref0 (text_input_key_events);
	return self;
}

DinoUiOccupantsTabCompletor*
dino_ui_occupants_tab_completor_new (DinoStreamInteractor* stream_interactor,
                                     GtkTextView* text_input)
{
	return dino_ui_occupants_tab_completor_construct (DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR, stream_interactor, text_input);
}

void
dino_ui_occupants_tab_completor_initialize_for_conversation (DinoUiOccupantsTabCompletor* self,
                                                             DinoEntitiesConversation* conversation)
{
	DinoEntitiesConversation* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (conversation != NULL);
	_tmp0_ = _g_object_ref0 (conversation);
	_g_object_unref0 (self->priv->conversation);
	self->priv->conversation = _tmp0_;
}

static gint
string_last_index_of (const gchar* self,
                      const gchar* needle,
                      gint start_index)
{
	gchar* _result_ = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	g_return_val_if_fail (needle != NULL, -1);
	_tmp0_ = g_strrstr (((gchar*) self) + start_index, (gchar*) needle);
	_result_ = _tmp0_;
	_tmp1_ = _result_;
	if (_tmp1_ != NULL) {
		gchar* _tmp2_;
		_tmp2_ = _result_;
		result = (gint) (_tmp2_ - ((gchar*) self));
		return result;
	} else {
		result = -1;
		return result;
	}
}

static glong
string_strnlen (gchar* str,
                glong maxlen)
{
	gchar* end = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	glong result;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	_tmp1_ = end;
	if (_tmp1_ == NULL) {
		result = maxlen;
		return result;
	} else {
		gchar* _tmp2_;
		_tmp2_ = end;
		result = (glong) (_tmp2_ - str);
		return result;
	}
}

static gchar*
string_substring (const gchar* self,
                  glong offset,
                  glong len)
{
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= ((glong) 0)) {
		_tmp0_ = len >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		string_length = string_strnlen ((gchar*) self, offset + len);
	} else {
		gint _tmp1_;
		gint _tmp2_;
		_tmp1_ = strlen (self);
		_tmp2_ = _tmp1_;
		string_length = (glong) _tmp2_;
	}
	if (offset < ((glong) 0)) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= ((glong) 0), NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < ((glong) 0)) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}

gboolean
dino_ui_occupants_tab_completor_on_text_input_key_press (DinoUiOccupantsTabCompletor* self,
                                                         guint keyval,
                                                         guint keycode,
                                                         GdkModifierType state)
{
	DinoEntitiesConversation* _tmp0_;
	DinoEntitiesConversationType _tmp1_;
	DinoEntitiesConversationType _tmp2_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->priv->conversation;
	_tmp1_ = dino_entities_conversation_get_type_ (_tmp0_);
	_tmp2_ = _tmp1_;
	if (_tmp2_ == DINO_ENTITIES_CONVERSATION_TYPE_GROUPCHAT) {
		gboolean _tmp3_ = FALSE;
		if (keyval == GDK_KEY_Tab) {
			_tmp3_ = TRUE;
		} else {
			_tmp3_ = keyval == GDK_KEY_ISO_Left_Tab;
		}
		if (_tmp3_) {
			gchar* text = NULL;
			GtkTextView* _tmp4_;
			GtkTextBuffer* _tmp5_;
			GtkTextBuffer* _tmp6_;
			gchar* _tmp7_;
			gchar* _tmp8_;
			gint start_index = 0;
			const gchar* _tmp9_;
			const gchar* _tmp10_;
			gchar* word = NULL;
			const gchar* _tmp11_;
			gchar* _tmp12_;
			gboolean _tmp20_ = FALSE;
			_tmp4_ = self->priv->text_input;
			_tmp5_ = gtk_text_view_get_buffer (_tmp4_);
			_tmp6_ = _tmp5_;
			g_object_get (_tmp6_, "text", &_tmp7_, NULL);
			_tmp8_ = _tmp7_;
			text = _tmp8_;
			_tmp9_ = text;
			_tmp10_ = text;
			start_index = MAX (string_last_index_of (_tmp9_, " ", 0), string_last_index_of (_tmp10_, "\n", 0)) + 1;
			_tmp11_ = text;
			_tmp12_ = string_substring (_tmp11_, (glong) start_index, (glong) -1);
			word = _tmp12_;
			if (!self->priv->active) {
				const gchar* _tmp13_;
				GeeList* _tmp17_;
				gint _tmp18_;
				gint _tmp19_;
				_tmp13_ = word;
				if (g_strcmp0 (_tmp13_, "") == 0) {
					GeeList* _tmp14_;
					_tmp14_ = dino_ui_occupants_tab_completor_generate_completions_from_messages (self, NULL);
					_g_object_unref0 (self->priv->completions);
					self->priv->completions = _tmp14_;
				} else {
					const gchar* _tmp15_;
					GeeList* _tmp16_;
					_tmp15_ = word;
					_tmp16_ = dino_ui_occupants_tab_completor_generate_completions_from_occupants (self, _tmp15_);
					_g_object_unref0 (self->priv->completions);
					self->priv->completions = _tmp16_;
				}
				_tmp17_ = self->priv->completions;
				_tmp18_ = gee_collection_get_size ((GeeCollection*) _tmp17_);
				_tmp19_ = _tmp18_;
				if (_tmp19_ > 0) {
					self->priv->active = TRUE;
					self->priv->index = -1;
				}
			}
			if (keyval != GDK_KEY_ISO_Group_Shift) {
				_tmp20_ = self->priv->active;
			} else {
				_tmp20_ = FALSE;
			}
			if (_tmp20_) {
				GtkTextView* _tmp21_;
				GtkTextBuffer* _tmp22_;
				GtkTextBuffer* _tmp23_;
				gchar* _tmp24_;
				gchar* _tmp25_;
				_tmp21_ = self->priv->text_input;
				_tmp22_ = gtk_text_view_get_buffer (_tmp21_);
				_tmp23_ = _tmp22_;
				_tmp24_ = dino_ui_occupants_tab_completor_next_completion (self, keyval == GDK_KEY_ISO_Left_Tab);
				_tmp25_ = _tmp24_;
				g_object_set (_tmp23_, "text", _tmp25_, NULL);
				_g_free0 (_tmp25_);
				result = TRUE;
				_g_free0 (word);
				_g_free0 (text);
				return result;
			}
			_g_free0 (word);
			_g_free0 (text);
		} else {
			gboolean _tmp26_ = FALSE;
			if (keyval != GDK_KEY_Shift_L) {
				_tmp26_ = self->priv->active;
			} else {
				_tmp26_ = FALSE;
			}
			if (_tmp26_) {
				self->priv->active = FALSE;
			}
		}
	}
	result = FALSE;
	return result;
}

static gchar*
dino_ui_occupants_tab_completor_next_completion (DinoUiOccupantsTabCompletor* self,
                                                 gboolean backwards)
{
	gchar* text = NULL;
	GtkTextView* _tmp0_;
	GtkTextBuffer* _tmp1_;
	GtkTextBuffer* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gint start_index = 0;
	const gchar* _tmp5_;
	const gchar* _tmp6_;
	gchar* prev_completion = NULL;
	const gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->text_input;
	_tmp1_ = gtk_text_view_get_buffer (_tmp0_);
	_tmp2_ = _tmp1_;
	g_object_get (_tmp2_, "text", &_tmp3_, NULL);
	_tmp4_ = _tmp3_;
	text = _tmp4_;
	_tmp5_ = text;
	_tmp6_ = text;
	start_index = MAX (string_last_index_of (_tmp5_, " ", 0), string_last_index_of (_tmp6_, "\n", 0)) + 1;
	_tmp7_ = text;
	_tmp8_ = string_substring (_tmp7_, (glong) start_index, (glong) -1);
	prev_completion = _tmp8_;
	if (self->priv->index > -1) {
		const gchar* _tmp9_;
		GeeList* _tmp10_;
		gpointer _tmp11_;
		gchar* _tmp12_;
		const gchar* _tmp13_;
		const gchar* _tmp14_;
		gint _tmp15_;
		gint _tmp16_;
		gchar* _tmp17_;
		gchar* _tmp18_;
		const gchar* _tmp19_;
		gchar* _tmp20_;
		_tmp9_ = text;
		_tmp10_ = self->priv->completions;
		_tmp11_ = gee_list_get (_tmp10_, self->priv->index);
		_tmp12_ = (gchar*) _tmp11_;
		_tmp13_ = text;
		_tmp14_ = text;
		_tmp15_ = strlen (_tmp14_);
		_tmp16_ = _tmp15_;
		_tmp17_ = string_substring (_tmp13_, (glong) 0, (glong) (_tmp16_ - 1));
		_tmp18_ = _tmp17_;
		start_index = MAX (string_last_index_of (_tmp9_, _tmp12_, 0), string_last_index_of (_tmp18_, "\n", 0));
		_g_free0 (_tmp18_);
		_g_free0 (_tmp12_);
		_tmp19_ = text;
		_tmp20_ = string_substring (_tmp19_, (glong) start_index, (glong) -1);
		_g_free0 (prev_completion);
		prev_completion = _tmp20_;
	}
	if (backwards) {
		self->priv->index = MAX (self->priv->index, 0) - 1;
		if (self->priv->index < 0) {
			GeeList* _tmp21_;
			gint _tmp22_;
			gint _tmp23_;
			_tmp21_ = self->priv->completions;
			_tmp22_ = gee_collection_get_size ((GeeCollection*) _tmp21_);
			_tmp23_ = _tmp22_;
			self->priv->index = _tmp23_ - 1;
		}
	} else {
		GeeList* _tmp24_;
		gint _tmp25_;
		gint _tmp26_;
		_tmp24_ = self->priv->completions;
		_tmp25_ = gee_collection_get_size ((GeeCollection*) _tmp24_);
		_tmp26_ = _tmp25_;
		self->priv->index = (self->priv->index + 1) % _tmp26_;
	}
	if (start_index == 0) {
		GeeList* _tmp27_;
		gpointer _tmp28_;
		gchar* _tmp29_;
		gchar* _tmp30_;
		gchar* _tmp31_;
		_tmp27_ = self->priv->completions;
		_tmp28_ = gee_list_get (_tmp27_, self->priv->index);
		_tmp29_ = (gchar*) _tmp28_;
		_tmp30_ = g_strconcat (_tmp29_, ", ", NULL);
		_tmp31_ = _tmp30_;
		_g_free0 (_tmp29_);
		result = _tmp31_;
		_g_free0 (prev_completion);
		_g_free0 (text);
		return result;
	} else {
		const gchar* _tmp32_;
		const gchar* _tmp33_;
		gint _tmp34_;
		gint _tmp35_;
		const gchar* _tmp36_;
		gint _tmp37_;
		gint _tmp38_;
		gchar* _tmp39_;
		gchar* _tmp40_;
		GeeList* _tmp41_;
		gpointer _tmp42_;
		gchar* _tmp43_;
		gchar* _tmp44_;
		gchar* _tmp45_;
		gchar* _tmp46_;
		gchar* _tmp47_;
		_tmp32_ = text;
		_tmp33_ = text;
		_tmp34_ = strlen (_tmp33_);
		_tmp35_ = _tmp34_;
		_tmp36_ = prev_completion;
		_tmp37_ = strlen (_tmp36_);
		_tmp38_ = _tmp37_;
		_tmp39_ = string_substring (_tmp32_, (glong) 0, (glong) (_tmp35_ - _tmp38_));
		_tmp40_ = _tmp39_;
		_tmp41_ = self->priv->completions;
		_tmp42_ = gee_list_get (_tmp41_, self->priv->index);
		_tmp43_ = (gchar*) _tmp42_;
		_tmp44_ = g_strconcat (_tmp40_, _tmp43_, NULL);
		_tmp45_ = _tmp44_;
		_tmp46_ = g_strconcat (_tmp45_, " ", NULL);
		_tmp47_ = _tmp46_;
		_g_free0 (_tmp45_);
		_g_free0 (_tmp43_);
		_g_free0 (_tmp40_);
		result = _tmp47_;
		_g_free0 (prev_completion);
		_g_free0 (text);
		return result;
	}
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

static GeeList*
dino_ui_occupants_tab_completor_generate_completions_from_messages (DinoUiOccupantsTabCompletor* self,
                                                                    const gchar* prefix)
{
	GeeList* ret = NULL;
	GeeArrayList* _tmp0_;
	GeeList* content_items = NULL;
	DinoStreamInteractor* _tmp1_;
	DinoModuleIdentity* _tmp2_;
	gpointer _tmp3_;
	DinoContentItemStore* _tmp4_;
	DinoEntitiesConversation* _tmp5_;
	GeeList* _tmp6_;
	GeeList* _tmp7_;
	GeeList* result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	ret = (GeeList*) _tmp0_;
	_tmp1_ = self->priv->stream_interactor;
	_tmp2_ = dino_content_item_store_IDENTITY;
	_tmp3_ = dino_stream_interactor_get_module (_tmp1_, DINO_TYPE_CONTENT_ITEM_STORE, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp2_);
	_tmp4_ = (DinoContentItemStore*) _tmp3_;
	_tmp5_ = self->priv->conversation;
	_tmp6_ = dino_content_item_store_get_n_latest (_tmp4_, _tmp5_, 10);
	_tmp7_ = _tmp6_;
	_g_object_unref0 (_tmp4_);
	content_items = _tmp7_;
	{
		gint i = 0;
		GeeList* _tmp8_;
		gint _tmp9_;
		gint _tmp10_;
		_tmp8_ = content_items;
		_tmp9_ = gee_collection_get_size ((GeeCollection*) _tmp8_);
		_tmp10_ = _tmp9_;
		i = _tmp10_ - 1;
		{
			gboolean _tmp11_ = FALSE;
			_tmp11_ = TRUE;
			while (TRUE) {
				gchar* resourcepart = NULL;
				GeeList* _tmp13_;
				gpointer _tmp14_;
				DinoContentItem* _tmp15_;
				XmppJid* _tmp16_;
				XmppJid* _tmp17_;
				const gchar* _tmp18_;
				gchar* _tmp19_;
				gchar* _tmp20_;
				XmppJid* own_nick = NULL;
				DinoStreamInteractor* _tmp21_;
				DinoModuleIdentity* _tmp22_;
				gpointer _tmp23_;
				DinoMucManager* _tmp24_;
				DinoEntitiesConversation* _tmp25_;
				XmppJid* _tmp26_;
				XmppJid* _tmp27_;
				DinoEntitiesConversation* _tmp28_;
				DinoEntitiesAccount* _tmp29_;
				DinoEntitiesAccount* _tmp30_;
				XmppJid* _tmp31_;
				XmppJid* _tmp32_;
				gboolean _tmp33_ = FALSE;
				gboolean _tmp34_ = FALSE;
				gboolean _tmp35_ = FALSE;
				const gchar* _tmp36_;
				if (!_tmp11_) {
					gint _tmp12_;
					_tmp12_ = i;
					i = _tmp12_ - 1;
				}
				_tmp11_ = FALSE;
				if (!(i > 0)) {
					break;
				}
				_tmp13_ = content_items;
				_tmp14_ = gee_list_get (_tmp13_, i);
				_tmp15_ = (DinoContentItem*) _tmp14_;
				_tmp16_ = dino_content_item_get_jid (_tmp15_);
				_tmp17_ = _tmp16_;
				_tmp18_ = _tmp17_->resourcepart;
				_tmp19_ = g_strdup (_tmp18_);
				_tmp20_ = _tmp19_;
				_g_object_unref0 (_tmp15_);
				resourcepart = _tmp20_;
				_tmp21_ = self->priv->stream_interactor;
				_tmp22_ = dino_muc_manager_IDENTITY;
				_tmp23_ = dino_stream_interactor_get_module (_tmp21_, DINO_TYPE_MUC_MANAGER, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp22_);
				_tmp24_ = (DinoMucManager*) _tmp23_;
				_tmp25_ = self->priv->conversation;
				_tmp26_ = dino_entities_conversation_get_counterpart (_tmp25_);
				_tmp27_ = _tmp26_;
				_tmp28_ = self->priv->conversation;
				_tmp29_ = dino_entities_conversation_get_account (_tmp28_);
				_tmp30_ = _tmp29_;
				_tmp31_ = dino_muc_manager_get_own_jid (_tmp24_, _tmp27_, _tmp30_);
				_tmp32_ = _tmp31_;
				_g_object_unref0 (_tmp24_);
				own_nick = _tmp32_;
				_tmp36_ = resourcepart;
				if (_tmp36_ != NULL) {
					const gchar* _tmp37_;
					_tmp37_ = resourcepart;
					_tmp35_ = g_strcmp0 (_tmp37_, "") != 0;
				} else {
					_tmp35_ = FALSE;
				}
				if (_tmp35_) {
					gboolean _tmp38_ = FALSE;
					XmppJid* _tmp39_;
					_tmp39_ = own_nick;
					if (_tmp39_ == NULL) {
						_tmp38_ = TRUE;
					} else {
						const gchar* _tmp40_;
						XmppJid* _tmp41_;
						const gchar* _tmp42_;
						_tmp40_ = resourcepart;
						_tmp41_ = own_nick;
						_tmp42_ = _tmp41_->resourcepart;
						_tmp38_ = g_strcmp0 (_tmp40_, _tmp42_) != 0;
					}
					_tmp34_ = _tmp38_;
				} else {
					_tmp34_ = FALSE;
				}
				if (_tmp34_) {
					GeeList* _tmp43_;
					const gchar* _tmp44_;
					_tmp43_ = ret;
					_tmp44_ = resourcepart;
					_tmp33_ = !gee_collection_contains ((GeeCollection*) _tmp43_, _tmp44_);
				} else {
					_tmp33_ = FALSE;
				}
				if (_tmp33_) {
					gboolean _tmp45_ = FALSE;
					GeeList* _tmp52_;
					const gchar* _tmp53_;
					if (prefix != NULL) {
						const gchar* _tmp46_;
						const gchar* _tmp47_;
						gchar* _tmp48_;
						gchar* _tmp49_;
						gchar* _tmp50_;
						gchar* _tmp51_;
						_tmp46_ = resourcepart;
						_tmp47_ = string_to_string (_tmp46_);
						_tmp48_ = g_utf8_strdown (_tmp47_, (gssize) -1);
						_tmp49_ = _tmp48_;
						_tmp50_ = g_utf8_strdown (prefix, (gssize) -1);
						_tmp51_ = _tmp50_;
						_tmp45_ = !g_str_has_prefix (_tmp49_, _tmp51_);
						_g_free0 (_tmp51_);
						_g_free0 (_tmp49_);
					} else {
						_tmp45_ = FALSE;
					}
					if (_tmp45_) {
						_xmpp_jid_unref0 (own_nick);
						_g_free0 (resourcepart);
						continue;
					}
					_tmp52_ = ret;
					_tmp53_ = resourcepart;
					gee_collection_add ((GeeCollection*) _tmp52_, _tmp53_);
				}
				_xmpp_jid_unref0 (own_nick);
				_g_free0 (resourcepart);
			}
		}
	}
	result = ret;
	_g_object_unref0 (content_items);
	return result;
}

static GeeList*
dino_ui_occupants_tab_completor_generate_completions_from_occupants (DinoUiOccupantsTabCompletor* self,
                                                                     const gchar* prefix)
{
	GeeList* ret = NULL;
	GeeList* _tmp0_;
	GeeList* occupants = NULL;
	DinoStreamInteractor* _tmp1_;
	DinoModuleIdentity* _tmp2_;
	gpointer _tmp3_;
	DinoMucManager* _tmp4_;
	DinoEntitiesConversation* _tmp5_;
	XmppJid* _tmp6_;
	XmppJid* _tmp7_;
	DinoEntitiesConversation* _tmp8_;
	DinoEntitiesAccount* _tmp9_;
	DinoEntitiesAccount* _tmp10_;
	GeeList* _tmp11_;
	GeeList* _tmp12_;
	GeeList* filtered_occupants = NULL;
	GeeArrayList* _tmp13_;
	GeeList* _tmp14_;
	GeeList* _tmp38_;
	GeeList* _tmp39_;
	GeeList* _tmp40_;
	GeeList* result;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (prefix != NULL, NULL);
	_tmp0_ = dino_ui_occupants_tab_completor_generate_completions_from_messages (self, prefix);
	ret = _tmp0_;
	_tmp1_ = self->priv->stream_interactor;
	_tmp2_ = dino_muc_manager_IDENTITY;
	_tmp3_ = dino_stream_interactor_get_module (_tmp1_, DINO_TYPE_MUC_MANAGER, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp2_);
	_tmp4_ = (DinoMucManager*) _tmp3_;
	_tmp5_ = self->priv->conversation;
	_tmp6_ = dino_entities_conversation_get_counterpart (_tmp5_);
	_tmp7_ = _tmp6_;
	_tmp8_ = self->priv->conversation;
	_tmp9_ = dino_entities_conversation_get_account (_tmp8_);
	_tmp10_ = _tmp9_;
	_tmp11_ = dino_muc_manager_get_other_occupants (_tmp4_, _tmp7_, _tmp10_);
	_tmp12_ = _tmp11_;
	_g_object_unref0 (_tmp4_);
	occupants = _tmp12_;
	_tmp13_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	filtered_occupants = (GeeList*) _tmp13_;
	_tmp14_ = occupants;
	if (_tmp14_ != NULL) {
		{
			GeeList* _jid_list = NULL;
			GeeList* _tmp15_;
			gint _jid_size = 0;
			GeeList* _tmp16_;
			gint _tmp17_;
			gint _tmp18_;
			gint _jid_index = 0;
			_tmp15_ = occupants;
			_jid_list = _tmp15_;
			_tmp16_ = _jid_list;
			_tmp17_ = gee_collection_get_size ((GeeCollection*) _tmp16_);
			_tmp18_ = _tmp17_;
			_jid_size = _tmp18_;
			_jid_index = -1;
			while (TRUE) {
				gint _tmp19_;
				gint _tmp20_;
				XmppJid* jid = NULL;
				GeeList* _tmp21_;
				gpointer _tmp22_;
				gchar* resourcepart = NULL;
				XmppJid* _tmp23_;
				const gchar* _tmp24_;
				const gchar* _tmp25_;
				gchar* _tmp26_;
				gboolean _tmp27_ = FALSE;
				const gchar* _tmp28_;
				gchar* _tmp29_;
				gchar* _tmp30_;
				gchar* _tmp31_;
				gchar* _tmp32_;
				gboolean _tmp33_;
				_jid_index = _jid_index + 1;
				_tmp19_ = _jid_index;
				_tmp20_ = _jid_size;
				if (!(_tmp19_ < _tmp20_)) {
					break;
				}
				_tmp21_ = _jid_list;
				_tmp22_ = gee_list_get (_tmp21_, _jid_index);
				jid = (XmppJid*) _tmp22_;
				_tmp23_ = jid;
				_tmp24_ = _tmp23_->resourcepart;
				_tmp25_ = string_to_string (_tmp24_);
				_tmp26_ = g_strdup (_tmp25_);
				resourcepart = _tmp26_;
				_tmp28_ = resourcepart;
				_tmp29_ = g_utf8_strdown (_tmp28_, (gssize) -1);
				_tmp30_ = _tmp29_;
				_tmp31_ = g_utf8_strdown (prefix, (gssize) -1);
				_tmp32_ = _tmp31_;
				_tmp33_ = g_str_has_prefix (_tmp30_, _tmp32_);
				_g_free0 (_tmp32_);
				_g_free0 (_tmp30_);
				if (_tmp33_) {
					GeeList* _tmp34_;
					const gchar* _tmp35_;
					_tmp34_ = ret;
					_tmp35_ = resourcepart;
					_tmp27_ = !gee_collection_contains ((GeeCollection*) _tmp34_, _tmp35_);
				} else {
					_tmp27_ = FALSE;
				}
				if (_tmp27_) {
					GeeList* _tmp36_;
					const gchar* _tmp37_;
					_tmp36_ = filtered_occupants;
					_tmp37_ = resourcepart;
					gee_collection_add ((GeeCollection*) _tmp36_, _tmp37_);
				}
				_g_free0 (resourcepart);
				_xmpp_jid_unref0 (jid);
			}
		}
	}
	_tmp38_ = filtered_occupants;
	gee_list_sort (_tmp38_, NULL, NULL, NULL);
	_tmp39_ = ret;
	_tmp40_ = filtered_occupants;
	gee_collection_add_all ((GeeCollection*) _tmp39_, (GeeCollection*) _tmp40_);
	result = ret;
	_g_object_unref0 (filtered_occupants);
	_g_object_unref0 (occupants);
	return result;
}

static void
dino_ui_value_occupants_tab_completor_init (GValue* value)
{
	value->data[0].v_pointer = NULL;
}

static void
dino_ui_value_occupants_tab_completor_free_value (GValue* value)
{
	if (value->data[0].v_pointer) {
		dino_ui_occupants_tab_completor_unref (value->data[0].v_pointer);
	}
}

static void
dino_ui_value_occupants_tab_completor_copy_value (const GValue* src_value,
                                                  GValue* dest_value)
{
	if (src_value->data[0].v_pointer) {
		dest_value->data[0].v_pointer = dino_ui_occupants_tab_completor_ref (src_value->data[0].v_pointer);
	} else {
		dest_value->data[0].v_pointer = NULL;
	}
}

static gpointer
dino_ui_value_occupants_tab_completor_peek_pointer (const GValue* value)
{
	return value->data[0].v_pointer;
}

static gchar*
dino_ui_value_occupants_tab_completor_collect_value (GValue* value,
                                                     guint n_collect_values,
                                                     GTypeCValue* collect_values,
                                                     guint collect_flags)
{
	if (collect_values[0].v_pointer) {
		DinoUiOccupantsTabCompletor * object;
		object = collect_values[0].v_pointer;
		if (object->parent_instance.g_class == NULL) {
			return g_strconcat ("invalid unclassed object pointer for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		} else if (!g_value_type_compatible (G_TYPE_FROM_INSTANCE (object), G_VALUE_TYPE (value))) {
			return g_strconcat ("invalid object type `", g_type_name (G_TYPE_FROM_INSTANCE (object)), "' for value type `", G_VALUE_TYPE_NAME (value), "'", NULL);
		}
		value->data[0].v_pointer = dino_ui_occupants_tab_completor_ref (object);
	} else {
		value->data[0].v_pointer = NULL;
	}
	return NULL;
}

static gchar*
dino_ui_value_occupants_tab_completor_lcopy_value (const GValue* value,
                                                   guint n_collect_values,
                                                   GTypeCValue* collect_values,
                                                   guint collect_flags)
{
	DinoUiOccupantsTabCompletor ** object_p;
	object_p = collect_values[0].v_pointer;
	if (!object_p) {
		return g_strdup_printf ("value location for `%s' passed as NULL", G_VALUE_TYPE_NAME (value));
	}
	if (!value->data[0].v_pointer) {
		*object_p = NULL;
	} else if (collect_flags & G_VALUE_NOCOPY_CONTENTS) {
		*object_p = value->data[0].v_pointer;
	} else {
		*object_p = dino_ui_occupants_tab_completor_ref (value->data[0].v_pointer);
	}
	return NULL;
}

GParamSpec*
dino_ui_param_spec_occupants_tab_completor (const gchar* name,
                                            const gchar* nick,
                                            const gchar* blurb,
                                            GType object_type,
                                            GParamFlags flags)
{
	DinoUiParamSpecOccupantsTabCompletor* spec;
	g_return_val_if_fail (g_type_is_a (object_type, DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR), NULL);
	spec = g_param_spec_internal (G_TYPE_PARAM_OBJECT, name, nick, blurb, flags);
	G_PARAM_SPEC (spec)->value_type = object_type;
	return G_PARAM_SPEC (spec);
}

gpointer
dino_ui_value_get_occupants_tab_completor (const GValue* value)
{
	g_return_val_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR), NULL);
	return value->data[0].v_pointer;
}

void
dino_ui_value_set_occupants_tab_completor (GValue* value,
                                           gpointer v_object)
{
	DinoUiOccupantsTabCompletor * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
		dino_ui_occupants_tab_completor_ref (value->data[0].v_pointer);
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		dino_ui_occupants_tab_completor_unref (old);
	}
}

void
dino_ui_value_take_occupants_tab_completor (GValue* value,
                                            gpointer v_object)
{
	DinoUiOccupantsTabCompletor * old;
	g_return_if_fail (G_TYPE_CHECK_VALUE_TYPE (value, DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR));
	old = value->data[0].v_pointer;
	if (v_object) {
		g_return_if_fail (G_TYPE_CHECK_INSTANCE_TYPE (v_object, DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR));
		g_return_if_fail (g_value_type_compatible (G_TYPE_FROM_INSTANCE (v_object), G_VALUE_TYPE (value)));
		value->data[0].v_pointer = v_object;
	} else {
		value->data[0].v_pointer = NULL;
	}
	if (old) {
		dino_ui_occupants_tab_completor_unref (old);
	}
}

static void
dino_ui_occupants_tab_completor_class_init (DinoUiOccupantsTabCompletorClass * klass,
                                            gpointer klass_data)
{
	dino_ui_occupants_tab_completor_parent_class = g_type_class_peek_parent (klass);
	((DinoUiOccupantsTabCompletorClass *) klass)->finalize = dino_ui_occupants_tab_completor_finalize;
	g_type_class_adjust_private_offset (klass, &DinoUiOccupantsTabCompletor_private_offset);
}

static void
dino_ui_occupants_tab_completor_instance_init (DinoUiOccupantsTabCompletor * self,
                                               gpointer klass)
{
	GeeArrayList* _tmp0_;
	self->priv = dino_ui_occupants_tab_completor_get_instance_private (self);
	_tmp0_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, NULL, NULL, NULL);
	self->priv->completions = (GeeList*) _tmp0_;
	self->priv->active = FALSE;
	self->priv->index = -1;
	self->ref_count = 1;
}

static void
dino_ui_occupants_tab_completor_finalize (DinoUiOccupantsTabCompletor * obj)
{
	DinoUiOccupantsTabCompletor * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, DINO_UI_TYPE_OCCUPANTS_TAB_COMPLETOR, DinoUiOccupantsTabCompletor);
	g_signal_handlers_destroy (self);
	_g_object_unref0 (self->priv->stream_interactor);
	_g_object_unref0 (self->priv->conversation);
	_g_object_unref0 (self->priv->text_input);
	_g_object_unref0 (self->priv->completions);
}

/**
 * - With given prefix: Complete from occupant list (sorted lexicographically)
 * - W/o prefix: Complete from received messages (most recent first)
 * - At the start (with ",") and in the middle of a text
 * - Backwards tabbing
 */
static GType
dino_ui_occupants_tab_completor_get_type_once (void)
{
	static const GTypeValueTable g_define_type_value_table = { dino_ui_value_occupants_tab_completor_init, dino_ui_value_occupants_tab_completor_free_value, dino_ui_value_occupants_tab_completor_copy_value, dino_ui_value_occupants_tab_completor_peek_pointer, "p", dino_ui_value_occupants_tab_completor_collect_value, "p", dino_ui_value_occupants_tab_completor_lcopy_value };
	static const GTypeInfo g_define_type_info = { sizeof (DinoUiOccupantsTabCompletorClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dino_ui_occupants_tab_completor_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DinoUiOccupantsTabCompletor), 0, (GInstanceInitFunc) dino_ui_occupants_tab_completor_instance_init, &g_define_type_value_table };
	static const GTypeFundamentalInfo g_define_type_fundamental_info = { (G_TYPE_FLAG_CLASSED | G_TYPE_FLAG_INSTANTIATABLE | G_TYPE_FLAG_DERIVABLE | G_TYPE_FLAG_DEEP_DERIVABLE) };
	GType dino_ui_occupants_tab_completor_type_id;
	dino_ui_occupants_tab_completor_type_id = g_type_register_fundamental (g_type_fundamental_next (), "DinoUiOccupantsTabCompletor", &g_define_type_info, &g_define_type_fundamental_info, 0);
	DinoUiOccupantsTabCompletor_private_offset = g_type_add_instance_private (dino_ui_occupants_tab_completor_type_id, sizeof (DinoUiOccupantsTabCompletorPrivate));
	return dino_ui_occupants_tab_completor_type_id;
}

GType
dino_ui_occupants_tab_completor_get_type (void)
{
	static volatile gsize dino_ui_occupants_tab_completor_type_id__once = 0;
	if (g_once_init_enter (&dino_ui_occupants_tab_completor_type_id__once)) {
		GType dino_ui_occupants_tab_completor_type_id;
		dino_ui_occupants_tab_completor_type_id = dino_ui_occupants_tab_completor_get_type_once ();
		g_once_init_leave (&dino_ui_occupants_tab_completor_type_id__once, dino_ui_occupants_tab_completor_type_id);
	}
	return dino_ui_occupants_tab_completor_type_id__once;
}

gpointer
dino_ui_occupants_tab_completor_ref (gpointer instance)
{
	DinoUiOccupantsTabCompletor * self;
	self = instance;
	g_atomic_int_inc (&self->ref_count);
	return instance;
}

void
dino_ui_occupants_tab_completor_unref (gpointer instance)
{
	DinoUiOccupantsTabCompletor * self;
	self = instance;
	if (g_atomic_int_dec_and_test (&self->ref_count)) {
		DINO_UI_OCCUPANTS_TAB_COMPLETOR_GET_CLASS (self)->finalize (self);
		g_type_free_instance ((GTypeInstance *) self);
	}
}

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

