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

/*  This file is part of corebird, a Gtk+ linux Twitter client.
 *  Copyright (C) 2013 Timm Bäder
 *
 *  corebird is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  corebird is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with corebird.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>


#define TYPE_USER_COMPLETION (user_completion_get_type ())
#define USER_COMPLETION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_USER_COMPLETION, UserCompletion))
#define USER_COMPLETION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_USER_COMPLETION, UserCompletionClass))
#define IS_USER_COMPLETION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_USER_COMPLETION))
#define IS_USER_COMPLETION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_USER_COMPLETION))
#define USER_COMPLETION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_USER_COMPLETION, UserCompletionClass))

typedef struct _UserCompletion UserCompletion;
typedef struct _UserCompletionClass UserCompletionClass;
typedef struct _UserCompletionPrivate UserCompletionPrivate;

#define TYPE_ACCOUNT (account_get_type ())
#define ACCOUNT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_ACCOUNT, Account))
#define ACCOUNT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_ACCOUNT, AccountClass))
#define IS_ACCOUNT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_ACCOUNT))
#define IS_ACCOUNT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_ACCOUNT))
#define ACCOUNT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_ACCOUNT, AccountClass))

typedef struct _Account Account;
typedef struct _AccountClass AccountClass;
#define _g_free0(var) (var = (g_free (var), NULL))

#define TYPE_USER_INFO (user_info_get_type ())
#define USER_INFO(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_USER_INFO, UserInfo))
#define USER_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_USER_INFO, UserInfoClass))
#define IS_USER_INFO(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_USER_INFO))
#define IS_USER_INFO_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_USER_INFO))
#define USER_INFO_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_USER_INFO, UserInfoClass))

typedef struct _UserInfo UserInfo;
typedef struct _UserInfoClass UserInfoClass;

#define TYPE_USER_COUNTER (user_counter_get_type ())
#define USER_COUNTER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_USER_COUNTER, UserCounter))
#define USER_COUNTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_USER_COUNTER, UserCounterClass))
#define IS_USER_COUNTER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_USER_COUNTER))
#define IS_USER_COUNTER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_USER_COUNTER))
#define USER_COUNTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_USER_COUNTER, UserCounterClass))

typedef struct _UserCounter UserCounter;
typedef struct _UserCounterClass UserCounterClass;
typedef struct _UserInfoPrivate UserInfoPrivate;

struct _UserCompletion {
	GObject parent_instance;
	UserCompletionPrivate * priv;
};

struct _UserCompletionClass {
	GObjectClass parent_class;
};

struct _UserCompletionPrivate {
	GObject* obj;
	Account* account;
	gchar* name_property_name;
	gint num_results;
};

struct _UserInfo {
	GObject parent_instance;
	UserInfoPrivate * priv;
	gint64 id;
	gchar* screen_name;
	gchar* name;
	gint score;
	gboolean changed;
};

struct _UserInfoClass {
	GObjectClass parent_class;
};


static gpointer user_completion_parent_class = NULL;

GType user_completion_get_type (void) G_GNUC_CONST;
GType account_get_type (void) G_GNUC_CONST;
#define USER_COMPLETION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_USER_COMPLETION, UserCompletionPrivate))
enum  {
	USER_COMPLETION_DUMMY_PROPERTY
};
UserCompletion* user_completion_new (Account* account, gint num_results);
UserCompletion* user_completion_construct (GType object_type, Account* account, gint num_results);
void user_completion_connect_to (UserCompletion* self, GObject* obj, const gchar* name_property_name);
static void user_completion_prop_changed (UserCompletion* self);
static void _user_completion_prop_changed_g_object_notify (GObject* _sender, GParamSpec* pspec, gpointer self);
GType user_info_get_type (void) G_GNUC_CONST;
GType user_counter_get_type (void) G_GNUC_CONST;
UserCounter* account_get_user_counter (Account* self);
UserInfo** user_counter_query_by_prefix (UserCounter* self, const gchar* prefix, gint max_results, gint* num_results, int* result_length1);
static void g_cclosure_user_marshal_VOID__STRING_STRING (GClosure * closure, GValue * return_value, guint n_param_values, const GValue * param_values, gpointer invocation_hint, gpointer marshal_data);
static void user_completion_finalize (GObject* obj);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);


UserCompletion* user_completion_construct (GType object_type, Account* account, gint num_results) {
	UserCompletion * self = NULL;
	Account* _tmp0_ = NULL;
	gint _tmp1_ = 0;
	g_return_val_if_fail (IS_ACCOUNT (account), NULL);
	self = (UserCompletion*) g_object_new (object_type, NULL);
	_tmp0_ = account;
	self->priv->account = _tmp0_;
	_tmp1_ = num_results;
	self->priv->num_results = _tmp1_;
	return self;
}


UserCompletion* user_completion_new (Account* account, gint num_results) {
	return user_completion_construct (TYPE_USER_COMPLETION, account, num_results);
}


static void _user_completion_prop_changed_g_object_notify (GObject* _sender, GParamSpec* pspec, gpointer self) {
	user_completion_prop_changed ((UserCompletion*) self);
}


void user_completion_connect_to (UserCompletion* self, GObject* obj, const gchar* name_property_name) {
	GObject* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	gchar* _tmp2_ = NULL;
	GObject* _tmp3_ = NULL;
	const gchar* _tmp4_ = NULL;
	gchar* _tmp5_ = NULL;
	g_return_if_fail (IS_USER_COMPLETION (self));
	g_return_if_fail (G_IS_OBJECT (obj));
	g_return_if_fail (name_property_name != NULL);
	_tmp0_ = obj;
	self->priv->obj = _tmp0_;
	_tmp1_ = name_property_name;
	_tmp2_ = g_strdup (_tmp1_);
	_g_free0 (self->priv->name_property_name);
	self->priv->name_property_name = _tmp2_;
	_tmp3_ = obj;
	_tmp4_ = name_property_name;
	_tmp5_ = g_strconcat ("notify::", _tmp4_, NULL);
	g_signal_connect_object (_tmp3_, _tmp5_, (GCallback) _user_completion_prop_changed_g_object_notify, self, 0);
	_g_free0 (_tmp5_);
}


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


static gchar* string_substring (const gchar* self, glong offset, glong len) {
	gchar* result = NULL;
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	glong _tmp1_ = 0L;
	glong _tmp8_ = 0L;
	glong _tmp14_ = 0L;
	glong _tmp17_ = 0L;
	glong _tmp18_ = 0L;
	glong _tmp19_ = 0L;
	glong _tmp20_ = 0L;
	glong _tmp21_ = 0L;
	gchar* _tmp22_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp1_ = offset;
	if (_tmp1_ >= ((glong) 0)) {
		glong _tmp2_ = 0L;
		_tmp2_ = len;
		_tmp0_ = _tmp2_ >= ((glong) 0);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		glong _tmp3_ = 0L;
		glong _tmp4_ = 0L;
		glong _tmp5_ = 0L;
		_tmp3_ = offset;
		_tmp4_ = len;
		_tmp5_ = string_strnlen ((gchar*) self, _tmp3_ + _tmp4_);
		string_length = _tmp5_;
	} else {
		gint _tmp6_ = 0;
		gint _tmp7_ = 0;
		_tmp6_ = strlen (self);
		_tmp7_ = _tmp6_;
		string_length = (glong) _tmp7_;
	}
	_tmp8_ = offset;
	if (_tmp8_ < ((glong) 0)) {
		glong _tmp9_ = 0L;
		glong _tmp10_ = 0L;
		glong _tmp11_ = 0L;
		_tmp9_ = string_length;
		_tmp10_ = offset;
		offset = _tmp9_ + _tmp10_;
		_tmp11_ = offset;
		g_return_val_if_fail (_tmp11_ >= ((glong) 0), NULL);
	} else {
		glong _tmp12_ = 0L;
		glong _tmp13_ = 0L;
		_tmp12_ = offset;
		_tmp13_ = string_length;
		g_return_val_if_fail (_tmp12_ <= _tmp13_, NULL);
	}
	_tmp14_ = len;
	if (_tmp14_ < ((glong) 0)) {
		glong _tmp15_ = 0L;
		glong _tmp16_ = 0L;
		_tmp15_ = string_length;
		_tmp16_ = offset;
		len = _tmp15_ - _tmp16_;
	}
	_tmp17_ = offset;
	_tmp18_ = len;
	_tmp19_ = string_length;
	g_return_val_if_fail ((_tmp17_ + _tmp18_) <= _tmp19_, NULL);
	_tmp20_ = offset;
	_tmp21_ = len;
	_tmp22_ = g_strndup (((gchar*) self) + _tmp20_, (gsize) _tmp21_);
	result = _tmp22_;
	return result;
}


static void user_completion_prop_changed (UserCompletion* self) {
	gchar* name = NULL;
	GObject* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	gboolean _tmp3_ = FALSE;
	gint n_results = 0;
	UserInfo** names = NULL;
	Account* _tmp6_ = NULL;
	UserCounter* _tmp7_ = NULL;
	UserCounter* _tmp8_ = NULL;
	const gchar* _tmp9_ = NULL;
	gint _tmp10_ = 0;
	gint _tmp11_ = 0;
	UserInfo** _tmp12_ = NULL;
	gint names_length1 = 0;
	gint _names_size_ = 0;
	g_return_if_fail (IS_USER_COMPLETION (self));
	_tmp0_ = self->priv->obj;
	_tmp1_ = self->priv->name_property_name;
	g_object_get (_tmp0_, _tmp1_, &name, NULL);
	_tmp2_ = name;
	_tmp3_ = g_str_has_prefix (_tmp2_, "@");
	if (_tmp3_) {
		const gchar* _tmp4_ = NULL;
		gchar* _tmp5_ = NULL;
		_tmp4_ = name;
		_tmp5_ = string_substring (_tmp4_, (glong) 1, (glong) (-1));
		_g_free0 (name);
		name = _tmp5_;
	}
	g_signal_emit_by_name (self, "start-completion");
	_tmp6_ = self->priv->account;
	_tmp7_ = account_get_user_counter (_tmp6_);
	_tmp8_ = _tmp7_;
	_tmp9_ = name;
	_tmp12_ = user_counter_query_by_prefix (_tmp8_, _tmp9_, 10, &_tmp10_, &_tmp11_);
	n_results = _tmp10_;
	names = _tmp12_;
	names_length1 = _tmp11_;
	_names_size_ = names_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp13_ = FALSE;
			_tmp13_ = TRUE;
			while (TRUE) {
				gint _tmp15_ = 0;
				gint _tmp16_ = 0;
				UserInfo** _tmp17_ = NULL;
				gint _tmp17__length1 = 0;
				gint _tmp18_ = 0;
				UserInfo* _tmp19_ = NULL;
				const gchar* _tmp20_ = NULL;
				UserInfo** _tmp21_ = NULL;
				gint _tmp21__length1 = 0;
				gint _tmp22_ = 0;
				UserInfo* _tmp23_ = NULL;
				const gchar* _tmp24_ = NULL;
				if (!_tmp13_) {
					gint _tmp14_ = 0;
					_tmp14_ = i;
					i = _tmp14_ + 1;
				}
				_tmp13_ = FALSE;
				_tmp15_ = i;
				_tmp16_ = n_results;
				if (!(_tmp15_ < _tmp16_)) {
					break;
				}
				_tmp17_ = names;
				_tmp17__length1 = names_length1;
				_tmp18_ = i;
				_tmp19_ = _tmp17_[_tmp18_];
				_tmp20_ = _tmp19_->screen_name;
				_tmp21_ = names;
				_tmp21__length1 = names_length1;
				_tmp22_ = i;
				_tmp23_ = _tmp21_[_tmp22_];
				_tmp24_ = _tmp23_->name;
				g_signal_emit_by_name (self, "populate-completion", _tmp20_, _tmp24_);
			}
		}
	}
	names = (_vala_array_free (names, names_length1, (GDestroyNotify) g_object_unref), NULL);
	_g_free0 (name);
}


static void g_cclosure_user_marshal_VOID__STRING_STRING (GClosure * closure, GValue * return_value, guint n_param_values, const GValue * param_values, gpointer invocation_hint, gpointer marshal_data) {
	typedef void (*GMarshalFunc_VOID__STRING_STRING) (gpointer data1, const char* arg_1, const char* arg_2, gpointer data2);
	register GMarshalFunc_VOID__STRING_STRING callback;
	register GCClosure * cc;
	register gpointer data1;
	register gpointer data2;
	cc = (GCClosure *) closure;
	g_return_if_fail (n_param_values == 3);
	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__STRING_STRING) (marshal_data ? marshal_data : cc->callback);
	callback (data1, g_value_get_string (param_values + 1), g_value_get_string (param_values + 2), data2);
}


static void user_completion_class_init (UserCompletionClass * klass) {
	user_completion_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (UserCompletionPrivate));
	G_OBJECT_CLASS (klass)->finalize = user_completion_finalize;
	g_signal_new ("start_completion", TYPE_USER_COMPLETION, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	g_signal_new ("populate_completion", TYPE_USER_COMPLETION, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_user_marshal_VOID__STRING_STRING, G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
}


static void user_completion_instance_init (UserCompletion * self) {
	self->priv = USER_COMPLETION_GET_PRIVATE (self);
}


static void user_completion_finalize (GObject* obj) {
	UserCompletion * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_USER_COMPLETION, UserCompletion);
	_g_free0 (self->priv->name_property_name);
	G_OBJECT_CLASS (user_completion_parent_class)->finalize (obj);
}


GType user_completion_get_type (void) {
	static volatile gsize user_completion_type_id__volatile = 0;
	if (g_once_init_enter (&user_completion_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (UserCompletionClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) user_completion_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (UserCompletion), 0, (GInstanceInitFunc) user_completion_instance_init, NULL };
		GType user_completion_type_id;
		user_completion_type_id = g_type_register_static (G_TYPE_OBJECT, "UserCompletion", &g_define_type_info, 0);
		g_once_init_leave (&user_completion_type_id__volatile, user_completion_type_id);
	}
	return user_completion_type_id__volatile;
}


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int 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, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}



