/* tracker-entry.c generated by valac 0.12.0, the Vala compiler
 * generated from tracker-entry.gs, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include <gdk/gdk.h>


#define TYPE_TRACKER_SEARCH_ENTRY (tracker_search_entry_get_type ())
#define TRACKER_SEARCH_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TRACKER_SEARCH_ENTRY, TrackerSearchEntry))
#define TRACKER_SEARCH_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TRACKER_SEARCH_ENTRY, TrackerSearchEntryClass))
#define IS_TRACKER_SEARCH_ENTRY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TRACKER_SEARCH_ENTRY))
#define IS_TRACKER_SEARCH_ENTRY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TRACKER_SEARCH_ENTRY))
#define TRACKER_SEARCH_ENTRY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TRACKER_SEARCH_ENTRY, TrackerSearchEntryClass))

typedef struct _TrackerSearchEntry TrackerSearchEntry;
typedef struct _TrackerSearchEntryClass TrackerSearchEntryClass;
typedef struct _TrackerSearchEntryPrivate TrackerSearchEntryPrivate;

#define TYPE_TRACKER_QUERY (tracker_query_get_type ())
#define TRACKER_QUERY(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TRACKER_QUERY, TrackerQuery))
#define TRACKER_QUERY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TRACKER_QUERY, TrackerQueryClass))
#define IS_TRACKER_QUERY(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TRACKER_QUERY))
#define IS_TRACKER_QUERY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TRACKER_QUERY))
#define TRACKER_QUERY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TRACKER_QUERY, TrackerQueryClass))

typedef struct _TrackerQuery TrackerQuery;
typedef struct _TrackerQueryClass TrackerQueryClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _TrackerSearchEntry {
	GtkComboBoxEntry parent_instance;
	TrackerSearchEntryPrivate * priv;
	guint id_invoker;
	GtkEntry* entry;
	GeeArrayList* history;
};

struct _TrackerSearchEntryClass {
	GtkComboBoxEntryClass parent_class;
};

struct _TrackerSearchEntryPrivate {
	TrackerQuery* _Query;
};


static gpointer tracker_search_entry_parent_class = NULL;
static GtkActivatableIface* tracker_search_entry_gtk_activatable_parent_iface = NULL;

#define RUN_DELAY 500
GType tracker_search_entry_get_type (void) G_GNUC_CONST;
GType tracker_query_get_type (void) G_GNUC_CONST;
#define TRACKER_SEARCH_ENTRY_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_TRACKER_SEARCH_ENTRY, TrackerSearchEntryPrivate))
enum  {
	TRACKER_SEARCH_ENTRY_DUMMY_PROPERTY,
	TRACKER_SEARCH_ENTRY_QUERY
};
static void tracker_search_entry_entry_changed (TrackerSearchEntry* self);
TrackerQuery* tracker_search_entry_get_Query (TrackerSearchEntry* self);
void tracker_query_set_SearchTerms (TrackerQuery* self, const gchar* value);
static gboolean tracker_search_entry_run_query (TrackerSearchEntry* self);
static gboolean _tracker_search_entry_run_query_gsource_func (gpointer self);
static void tracker_search_entry_entry_activate (TrackerSearchEntry* self);
gchar* tracker_utils_EscapeSparql (const gchar* sparql, gboolean add_wildcard);
static void tracker_search_entry_real_sync_action_properties (GtkActivatable* base, GtkAction* action);
static void tracker_search_entry_real_update (GtkActivatable* base, GtkAction* action, const gchar* prop);
TrackerSearchEntry* tracker_search_entry_new (void);
TrackerSearchEntry* tracker_search_entry_construct (GType object_type);
void tracker_search_entry_set_Query (TrackerSearchEntry* self, TrackerQuery* value);
static GObject * tracker_search_entry_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void _tracker_search_entry_entry_activate_gtk_entry_activate (GtkEntry* _sender, gpointer self);
static void _tracker_search_entry_entry_changed_gtk_editable_changed (GtkEntry* _sender, gpointer self);
static void _lambda1_ (GtkEntryIconPosition p0, TrackerSearchEntry* self);
static void __lambda1__gtk_entry_icon_press (GtkEntry* _sender, GtkEntryIconPosition p0, GdkEvent* p1, gpointer self);
static void tracker_search_entry_finalize (GObject* obj);
static void _vala_tracker_search_entry_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_tracker_search_entry_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


static gboolean _tracker_search_entry_run_query_gsource_func (gpointer self) {
	gboolean result;
	result = tracker_search_entry_run_query (self);
	return result;
}


static void tracker_search_entry_entry_changed (TrackerSearchEntry* self) {
	const gchar* _tmp0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = gtk_entry_get_text (self->entry);
	if (_tmp0_ == NULL) {
		tracker_query_set_SearchTerms (self->priv->_Query, "");
		if (self->id_invoker != 0) {
			g_source_remove (self->id_invoker);
			self->id_invoker = (guint) 0;
		}
	} else {
		guint _tmp1_;
		if (self->id_invoker != 0) {
			g_source_remove (self->id_invoker);
		}
		_tmp1_ = g_timeout_add_full (G_PRIORITY_DEFAULT, (guint) RUN_DELAY, _tracker_search_entry_run_query_gsource_func, g_object_ref (self), g_object_unref);
		self->id_invoker = _tmp1_;
	}
}


static void tracker_search_entry_entry_activate (TrackerSearchEntry* self) {
	g_return_if_fail (self != NULL);
	gtk_widget_grab_focus ((GtkWidget*) self->entry);
}


static gchar* string_chug (const gchar* self) {
	gchar* result = NULL;
	gchar* _tmp0_ = NULL;
	gchar* _result_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_strdup (self);
	_result_ = _tmp0_;
	g_strchug (_result_);
	result = _result_;
	return result;
}


static gchar string_get (const gchar* self, glong index) {
	gchar result = '\0';
	g_return_val_if_fail (self != NULL, '\0');
	result = ((gchar*) self)[index];
	return result;
}


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


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


static gboolean tracker_search_entry_run_query (TrackerSearchEntry* self) {
	gboolean result = FALSE;
	const gchar* _tmp0_ = NULL;
	gchar* _tmp1_;
	gchar* txt;
	gboolean _tmp2_ = FALSE;
	gchar* _tmp3_ = NULL;
	glong _tmp4_;
	glong len;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = gtk_entry_get_text (self->entry);
	_tmp1_ = g_strdup (_tmp0_);
	txt = _tmp1_;
	if (txt == NULL) {
		_tmp2_ = TRUE;
	} else {
		_tmp2_ = g_strcmp0 (txt, "") == 0;
	}
	if (_tmp2_) {
		tracker_query_set_SearchTerms (self->priv->_Query, "");
		result = FALSE;
		_g_free0 (txt);
		return result;
	}
	_tmp3_ = string_chug (txt);
	_g_free0 (txt);
	txt = _tmp3_;
	_tmp4_ = strlen (txt);
	len = _tmp4_;
	if (len > 2) {
		gchar _tmp10_;
		gboolean _tmp11_;
		gchar* _tmp16_ = NULL;
		gchar* _tmp17_;
		while (TRUE) {
			gchar _tmp5_;
			gboolean _tmp6_;
			gchar _tmp7_;
			glong _tmp8_;
			gchar* _tmp9_ = NULL;
			_tmp5_ = string_get (txt, (glong) 0);
			_tmp6_ = g_ascii_isalnum (_tmp5_);
			if (!(!_tmp6_)) {
				break;
			}
			_tmp7_ = string_get (txt, (glong) 0);
			if (_tmp7_ == '_') {
				break;
			}
			_tmp8_ = len;
			len = _tmp8_ - 1;
			_tmp9_ = string_slice (txt, (glong) 1, _tmp8_);
			_g_free0 (txt);
			txt = _tmp9_;
			if (len < 3) {
				result = FALSE;
				_g_free0 (txt);
				return result;
			}
		}
		_tmp10_ = string_get (txt, len - 1);
		_tmp11_ = g_ascii_isalnum (_tmp10_);
		if (!_tmp11_) {
			while (TRUE) {
				gchar _tmp12_;
				gboolean _tmp13_;
				gchar _tmp14_;
				gchar* _tmp15_ = NULL;
				_tmp12_ = string_get (txt, len - 2);
				_tmp13_ = g_ascii_isalnum (_tmp12_);
				if (!(!_tmp13_)) {
					break;
				}
				_tmp14_ = string_get (txt, len - 2);
				if (_tmp14_ == '_') {
					break;
				}
				_tmp15_ = string_slice (txt, (glong) 0, len - 2);
				_g_free0 (txt);
				txt = _tmp15_;
				len--;
				if (len < 3) {
					result = FALSE;
					_g_free0 (txt);
					return result;
				}
			}
		}
		_tmp16_ = tracker_utils_EscapeSparql (txt, TRUE);
		_tmp17_ = _tmp16_;
		tracker_query_set_SearchTerms (self->priv->_Query, _tmp17_);
		_g_free0 (_tmp17_);
		{
			GeeArrayList* _tmp18_;
			GeeArrayList* _item_list;
			gint _tmp19_;
			gint _item_size;
			gint _item_index;
			_tmp18_ = _g_object_ref0 (self->history);
			_item_list = _tmp18_;
			_tmp19_ = gee_collection_get_size ((GeeCollection*) _item_list);
			_item_size = _tmp19_;
			_item_index = -1;
			while (TRUE) {
				gpointer _tmp20_ = NULL;
				gchar* item;
				_item_index = _item_index + 1;
				if (!(_item_index < _item_size)) {
					break;
				}
				_tmp20_ = gee_abstract_list_get ((GeeAbstractList*) _item_list, _item_index);
				item = (gchar*) _tmp20_;
				if (g_strcmp0 (txt, item) == 0) {
					result = FALSE;
					_g_free0 (item);
					_g_object_unref0 (_item_list);
					_g_free0 (txt);
					return result;
				}
				_g_free0 (item);
			}
			_g_object_unref0 (_item_list);
		}
		gee_abstract_collection_add ((GeeAbstractCollection*) self->history, txt);
		gtk_combo_box_prepend_text ((GtkComboBox*) self, txt);
	}
	result = FALSE;
	_g_free0 (txt);
	return result;
}


static void tracker_search_entry_real_sync_action_properties (GtkActivatable* base, GtkAction* action) {
	TrackerSearchEntry * self;
	self = (TrackerSearchEntry*) base;
	g_return_if_fail (action != NULL);
	return;
}


static void tracker_search_entry_real_update (GtkActivatable* base, GtkAction* action, const gchar* prop) {
	TrackerSearchEntry * self;
	self = (TrackerSearchEntry*) base;
	g_return_if_fail (action != NULL);
	g_return_if_fail (prop != NULL);
	return;
}


TrackerSearchEntry* tracker_search_entry_construct (GType object_type) {
	TrackerSearchEntry * self = NULL;
	self = (TrackerSearchEntry*) g_object_new (object_type, NULL);
	return self;
}


TrackerSearchEntry* tracker_search_entry_new (void) {
	return tracker_search_entry_construct (TYPE_TRACKER_SEARCH_ENTRY);
}


TrackerQuery* tracker_search_entry_get_Query (TrackerSearchEntry* self) {
	TrackerQuery* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self->priv->_Query;
	return result;
}


void tracker_search_entry_set_Query (TrackerSearchEntry* self, TrackerQuery* value) {
	g_return_if_fail (self != NULL);
	self->priv->_Query = value;
	g_object_notify ((GObject *) self, "Query");
}


static void _tracker_search_entry_entry_activate_gtk_entry_activate (GtkEntry* _sender, gpointer self) {
	tracker_search_entry_entry_activate (self);
}


static void _tracker_search_entry_entry_changed_gtk_editable_changed (GtkEntry* _sender, gpointer self) {
	tracker_search_entry_entry_changed (self);
}


static void _lambda1_ (GtkEntryIconPosition p0, TrackerSearchEntry* self) {
	if (p0 == GTK_ENTRY_ICON_SECONDARY) {
		gtk_entry_set_text (self->entry, "");
	}
}


static void __lambda1__gtk_entry_icon_press (GtkEntry* _sender, GtkEntryIconPosition p0, GdkEvent* p1, gpointer self) {
	_lambda1_ (p0, self);
}


static GObject * tracker_search_entry_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	TrackerSearchEntry * self;
	GtkWidget* _tmp0_ = NULL;
	GtkWidget* _tmp1_;
	GtkEntry* _tmp2_;
	const gchar* _tmp3_ = NULL;
	GtkListStore* _tmp4_ = NULL;
	GtkListStore* model;
	GtkEntryCompletion* _tmp5_ = NULL;
	GtkEntryCompletion* completion;
	GeeArrayList* _tmp6_ = NULL;
	parent_class = G_OBJECT_CLASS (tracker_search_entry_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = TRACKER_SEARCH_ENTRY (obj);
	_tmp0_ = gtk_bin_get_child ((GtkBin*) self);
	_tmp1_ = _tmp0_;
	_tmp2_ = _g_object_ref0 (GTK_IS_ENTRY (_tmp1_) ? ((GtkEntry*) _tmp1_) : NULL);
	_g_object_unref0 (self->entry);
	self->entry = _tmp2_;
	gtk_entry_set_icon_from_stock (self->entry, GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
	gtk_entry_set_icon_sensitive (self->entry, GTK_ENTRY_ICON_SECONDARY, TRUE);
	_tmp3_ = _ ("Clear the search text");
	gtk_entry_set_icon_tooltip_text (self->entry, GTK_ENTRY_ICON_SECONDARY, _tmp3_);
	_tmp4_ = gtk_list_store_new (1, G_TYPE_STRING);
	model = _tmp4_;
	gtk_combo_box_set_model ((GtkComboBox*) self, (GtkTreeModel*) model);
	gtk_combo_box_entry_set_text_column ((GtkComboBoxEntry*) self, 0);
	_tmp5_ = gtk_entry_completion_new ();
	completion = _tmp5_;
	gtk_entry_completion_set_model (completion, (GtkTreeModel*) model);
	gtk_entry_completion_set_text_column (completion, 0);
	gtk_entry_set_completion (self->entry, completion);
	g_signal_connect_object (self->entry, "activate", (GCallback) _tracker_search_entry_entry_activate_gtk_entry_activate, self, 0);
	g_signal_connect_object ((GtkEditable*) self->entry, "changed", (GCallback) _tracker_search_entry_entry_changed_gtk_editable_changed, self, 0);
	g_signal_connect_object (self->entry, "icon-press", (GCallback) __lambda1__gtk_entry_icon_press, self, 0);
	_tmp6_ = gee_array_list_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, NULL);
	_g_object_unref0 (self->history);
	self->history = _tmp6_;
	_g_object_unref0 (completion);
	_g_object_unref0 (model);
	return obj;
}


static void tracker_search_entry_class_init (TrackerSearchEntryClass * klass) {
	tracker_search_entry_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (TrackerSearchEntryPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_tracker_search_entry_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_tracker_search_entry_set_property;
	G_OBJECT_CLASS (klass)->constructor = tracker_search_entry_constructor;
	G_OBJECT_CLASS (klass)->finalize = tracker_search_entry_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), TRACKER_SEARCH_ENTRY_QUERY, g_param_spec_object ("Query", "Query", "Query", TYPE_TRACKER_QUERY, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void tracker_search_entry_gtk_activatable_interface_init (GtkActivatableIface * iface) {
	tracker_search_entry_gtk_activatable_parent_iface = g_type_interface_peek_parent (iface);
	iface->sync_action_properties = (void (*)(GtkActivatable* ,GtkAction*)) tracker_search_entry_real_sync_action_properties;
	iface->update = (void (*)(GtkActivatable* ,GtkAction* ,const gchar*)) tracker_search_entry_real_update;
}


static void tracker_search_entry_instance_init (TrackerSearchEntry * self) {
	self->priv = TRACKER_SEARCH_ENTRY_GET_PRIVATE (self);
	self->id_invoker = (guint) 0;
}


static void tracker_search_entry_finalize (GObject* obj) {
	TrackerSearchEntry * self;
	self = TRACKER_SEARCH_ENTRY (obj);
	_g_object_unref0 (self->entry);
	_g_object_unref0 (self->history);
	G_OBJECT_CLASS (tracker_search_entry_parent_class)->finalize (obj);
}


GType tracker_search_entry_get_type (void) {
	static volatile gsize tracker_search_entry_type_id__volatile = 0;
	if (g_once_init_enter (&tracker_search_entry_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (TrackerSearchEntryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) tracker_search_entry_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (TrackerSearchEntry), 0, (GInstanceInitFunc) tracker_search_entry_instance_init, NULL };
		static const GInterfaceInfo gtk_activatable_info = { (GInterfaceInitFunc) tracker_search_entry_gtk_activatable_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
		GType tracker_search_entry_type_id;
		tracker_search_entry_type_id = g_type_register_static (GTK_TYPE_COMBO_BOX_ENTRY, "TrackerSearchEntry", &g_define_type_info, 0);
		g_type_add_interface_static (tracker_search_entry_type_id, GTK_TYPE_ACTIVATABLE, &gtk_activatable_info);
		g_once_init_leave (&tracker_search_entry_type_id__volatile, tracker_search_entry_type_id);
	}
	return tracker_search_entry_type_id__volatile;
}


static void _vala_tracker_search_entry_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	TrackerSearchEntry * self;
	self = TRACKER_SEARCH_ENTRY (object);
	switch (property_id) {
		case TRACKER_SEARCH_ENTRY_QUERY:
		g_value_set_object (value, tracker_search_entry_get_Query (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_tracker_search_entry_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	TrackerSearchEntry * self;
	self = TRACKER_SEARCH_ENTRY (object);
	switch (property_id) {
		case TRACKER_SEARCH_ENTRY_QUERY:
		tracker_search_entry_set_Query (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



