/* contacts-query-filter.c generated by valac 0.56.3, the Vala compiler
 * generated from contacts-query-filter.vala, do not modify */

/*
 * Copyright (C) 2021 Niels De Graef <nielsdegraef@gmail.com>
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "contacts.h"
#include <folks/folks.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <glib-object.h>

enum  {
	CONTACTS_QUERY_FILTER_0_PROPERTY,
	CONTACTS_QUERY_FILTER_QUERY_PROPERTY,
	CONTACTS_QUERY_FILTER_MIN_STRENGTH_PROPERTY,
	CONTACTS_QUERY_FILTER_NUM_PROPERTIES
};
static GParamSpec* contacts_query_filter_properties[CONTACTS_QUERY_FILTER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _ContactsQueryFilterPrivate {
	FolksQuery* _query;
	gchar* query_string;
	guint _min_strength;
};

static gint ContactsQueryFilter_private_offset;
static gpointer contacts_query_filter_parent_class = NULL;

static void contacts_query_filter_on_query_notify (ContactsQueryFilter* self,
                                            GObject* object,
                                            GParamSpec* pspec);
static gboolean contacts_query_filter_real_match (GtkFilter* base,
                                           GObject* item);
static GtkFilterMatch contacts_query_filter_real_get_strictness (GtkFilter* base);
static GObject * contacts_query_filter_constructor (GType type,
                                             guint n_construct_properties,
                                             GObjectConstructParam * construct_properties);
static void _contacts_query_filter_on_query_notify_g_object_notify (GObject* _sender,
                                                             GParamSpec* pspec,
                                                             gpointer self);
static void contacts_query_filter_finalize (GObject * obj);
static GType contacts_query_filter_get_type_once (void);
static void _vala_contacts_query_filter_get_property (GObject * object,
                                               guint property_id,
                                               GValue * value,
                                               GParamSpec * pspec);
static void _vala_contacts_query_filter_set_property (GObject * object,
                                               guint property_id,
                                               const GValue * value,
                                               GParamSpec * pspec);

static inline gpointer
contacts_query_filter_get_instance_private (ContactsQueryFilter* self)
{
	return G_STRUCT_MEMBER_P (self, ContactsQueryFilter_private_offset);
}

ContactsQueryFilter*
contacts_query_filter_construct (GType object_type,
                                 FolksQuery* query)
{
	ContactsQueryFilter * self = NULL;
	g_return_val_if_fail (query != NULL, NULL);
	self = (ContactsQueryFilter*) g_object_new (object_type, "query", query, NULL);
	return self;
}

ContactsQueryFilter*
contacts_query_filter_new (FolksQuery* query)
{
	return contacts_query_filter_construct (CONTACTS_TYPE_QUERY_FILTER, query);
}

static gint
string_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, 0);
	g_return_val_if_fail (needle != NULL, 0);
	_tmp0_ = strstr (((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 void
contacts_query_filter_on_query_notify (ContactsQueryFilter* self,
                                       GObject* object,
                                       GParamSpec* pspec)
{
	FolksQuery* query = NULL;
	FolksQuery* _tmp0_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (object != NULL);
	g_return_if_fail (pspec != NULL);
	query = G_TYPE_CHECK_INSTANCE_CAST (object, FOLKS_TYPE_QUERY, FolksQuery);
	_tmp0_ = query;
	if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp0_, FOLKS_TYPE_SIMPLE_QUERY)) {
		const gchar* _tmp1_;
		const gchar* _tmp2_;
		_tmp1_ = g_param_spec_get_name (pspec);
		if (g_strcmp0 (_tmp1_, "query-locale") == 0) {
			return;
		}
		_tmp2_ = g_param_spec_get_name (pspec);
		if (g_strcmp0 (_tmp2_, "query-string") == 0) {
			gchar* old_query_str = NULL;
			const gchar* _tmp3_;
			gchar* _tmp4_;
			FolksQuery* _tmp5_;
			const gchar* _tmp6_;
			const gchar* _tmp7_;
			gchar* _tmp8_;
			const gchar* _tmp9_;
			const gchar* _tmp10_;
			gboolean _tmp11_ = FALSE;
			const gchar* _tmp12_;
			gint _tmp13_;
			gint _tmp14_;
			const gchar* _tmp15_;
			gint _tmp16_;
			gint _tmp17_;
			gboolean _tmp20_ = FALSE;
			const gchar* _tmp21_;
			gint _tmp22_;
			gint _tmp23_;
			const gchar* _tmp24_;
			gint _tmp25_;
			gint _tmp26_;
			_tmp3_ = self->priv->query_string;
			_tmp4_ = g_strdup (_tmp3_);
			old_query_str = _tmp4_;
			_tmp5_ = query;
			_tmp6_ = folks_simple_query_get_query_string (G_TYPE_CHECK_INSTANCE_CAST (_tmp5_, FOLKS_TYPE_SIMPLE_QUERY, FolksSimpleQuery));
			_tmp7_ = _tmp6_;
			_tmp8_ = g_strdup (_tmp7_);
			_g_free0 (self->priv->query_string);
			self->priv->query_string = _tmp8_;
			_tmp9_ = self->priv->query_string;
			_tmp10_ = old_query_str;
			if (g_strcmp0 (_tmp9_, _tmp10_) == 0) {
				_g_free0 (old_query_str);
				return;
			}
			_tmp12_ = self->priv->query_string;
			_tmp13_ = strlen (_tmp12_);
			_tmp14_ = _tmp13_;
			_tmp15_ = old_query_str;
			_tmp16_ = strlen (_tmp15_);
			_tmp17_ = _tmp16_;
			if (_tmp14_ > _tmp17_) {
				const gchar* _tmp18_;
				const gchar* _tmp19_;
				_tmp18_ = self->priv->query_string;
				_tmp19_ = old_query_str;
				_tmp11_ = string_index_of (_tmp18_, _tmp19_, 0) != -1;
			} else {
				_tmp11_ = FALSE;
			}
			if (_tmp11_) {
				gtk_filter_changed ((GtkFilter*) self, GTK_FILTER_CHANGE_MORE_STRICT);
				_g_free0 (old_query_str);
				return;
			}
			_tmp21_ = self->priv->query_string;
			_tmp22_ = strlen (_tmp21_);
			_tmp23_ = _tmp22_;
			_tmp24_ = old_query_str;
			_tmp25_ = strlen (_tmp24_);
			_tmp26_ = _tmp25_;
			if (_tmp23_ < _tmp26_) {
				const gchar* _tmp27_;
				const gchar* _tmp28_;
				_tmp27_ = old_query_str;
				_tmp28_ = self->priv->query_string;
				_tmp20_ = string_index_of (_tmp27_, _tmp28_, 0) != -1;
			} else {
				_tmp20_ = FALSE;
			}
			if (_tmp20_) {
				gtk_filter_changed ((GtkFilter*) self, GTK_FILTER_CHANGE_LESS_STRICT);
				_g_free0 (old_query_str);
				return;
			}
			_g_free0 (old_query_str);
		}
	}
	gtk_filter_changed ((GtkFilter*) self, GTK_FILTER_CHANGE_DIFFERENT);
}

static gboolean
contacts_query_filter_real_match (GtkFilter* base,
                                  GObject* item)
{
	ContactsQueryFilter * self;
	FolksIndividual* individual = NULL;
	FolksQuery* _tmp0_;
	guint _tmp1_;
	guint _tmp2_;
	gboolean result;
	self = (ContactsQueryFilter*) base;
	individual = G_TYPE_CHECK_INSTANCE_TYPE (item, FOLKS_TYPE_INDIVIDUAL) ? ((FolksIndividual*) item) : NULL;
	g_return_val_if_fail (individual != NULL, FALSE);
	_tmp0_ = self->priv->_query;
	_tmp1_ = contacts_query_filter_get_min_strength (self);
	_tmp2_ = _tmp1_;
	result = folks_query_is_match (_tmp0_, individual) > _tmp2_;
	return result;
}

static GtkFilterMatch
contacts_query_filter_real_get_strictness (GtkFilter* base)
{
	ContactsQueryFilter * self;
	gboolean _tmp0_ = FALSE;
	FolksQuery* _tmp1_;
	GtkFilterMatch result;
	self = (ContactsQueryFilter*) base;
	_tmp1_ = self->priv->_query;
	if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp1_, FOLKS_TYPE_SIMPLE_QUERY)) {
		FolksQuery* _tmp2_;
		const gchar* _tmp3_;
		const gchar* _tmp4_;
		_tmp2_ = self->priv->_query;
		_tmp3_ = folks_simple_query_get_query_string (G_TYPE_CHECK_INSTANCE_CAST (_tmp2_, FOLKS_TYPE_SIMPLE_QUERY, FolksSimpleQuery));
		_tmp4_ = _tmp3_;
		_tmp0_ = g_strcmp0 (_tmp4_, "") == 0;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		result = GTK_FILTER_MATCH_ALL;
		return result;
	}
	result = GTK_FILTER_MATCH_SOME;
	return result;
}

FolksQuery*
contacts_query_filter_get_query (ContactsQueryFilter* self)
{
	FolksQuery* result;
	FolksQuery* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_query;
	result = _tmp0_;
	return result;
}

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

static void
contacts_query_filter_set_query (ContactsQueryFilter* self,
                                 FolksQuery* value)
{
	FolksQuery* old_value;
	g_return_if_fail (self != NULL);
	old_value = contacts_query_filter_get_query (self);
	if (old_value != value) {
		FolksQuery* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_query);
		self->priv->_query = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, contacts_query_filter_properties[CONTACTS_QUERY_FILTER_QUERY_PROPERTY]);
	}
}

guint
contacts_query_filter_get_min_strength (ContactsQueryFilter* self)
{
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	result = self->priv->_min_strength;
	return result;
}

void
contacts_query_filter_set_min_strength (ContactsQueryFilter* self,
                                        guint value)
{
	g_return_if_fail (self != NULL);
	if (value == self->priv->_min_strength) {
		return;
	}
	self->priv->_min_strength = value;
	gtk_filter_changed ((GtkFilter*) self, GTK_FILTER_CHANGE_DIFFERENT);
	g_object_notify_by_pspec ((GObject *) self, contacts_query_filter_properties[CONTACTS_QUERY_FILTER_MIN_STRENGTH_PROPERTY]);
}

static void
_contacts_query_filter_on_query_notify_g_object_notify (GObject* _sender,
                                                        GParamSpec* pspec,
                                                        gpointer self)
{
	contacts_query_filter_on_query_notify ((ContactsQueryFilter*) self, _sender, pspec);
}

static GObject *
contacts_query_filter_constructor (GType type,
                                   guint n_construct_properties,
                                   GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	ContactsQueryFilter * self;
	FolksQuery* _tmp0_;
	FolksQuery* _tmp5_;
	parent_class = G_OBJECT_CLASS (contacts_query_filter_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_QUERY_FILTER, ContactsQueryFilter);
	_tmp0_ = self->priv->_query;
	if (G_TYPE_CHECK_INSTANCE_TYPE (_tmp0_, FOLKS_TYPE_SIMPLE_QUERY)) {
		FolksQuery* _tmp1_;
		const gchar* _tmp2_;
		const gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp1_ = self->priv->_query;
		_tmp2_ = folks_simple_query_get_query_string (G_TYPE_CHECK_INSTANCE_CAST (_tmp1_, FOLKS_TYPE_SIMPLE_QUERY, FolksSimpleQuery));
		_tmp3_ = _tmp2_;
		_tmp4_ = g_strdup (_tmp3_);
		_g_free0 (self->priv->query_string);
		self->priv->query_string = _tmp4_;
	}
	_tmp5_ = self->priv->_query;
	g_signal_connect_object ((GObject*) _tmp5_, "notify", (GCallback) _contacts_query_filter_on_query_notify_g_object_notify, self, 0);
	return obj;
}

static void
contacts_query_filter_class_init (ContactsQueryFilterClass * klass,
                                  gpointer klass_data)
{
	contacts_query_filter_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ContactsQueryFilter_private_offset);
	((GtkFilterClass *) klass)->match = (gboolean (*) (GtkFilter*, GObject*)) contacts_query_filter_real_match;
	((GtkFilterClass *) klass)->get_strictness = (GtkFilterMatch (*) (GtkFilter*)) contacts_query_filter_real_get_strictness;
	G_OBJECT_CLASS (klass)->get_property = _vala_contacts_query_filter_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_contacts_query_filter_set_property;
	G_OBJECT_CLASS (klass)->constructor = contacts_query_filter_constructor;
	G_OBJECT_CLASS (klass)->finalize = contacts_query_filter_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTACTS_QUERY_FILTER_QUERY_PROPERTY, contacts_query_filter_properties[CONTACTS_QUERY_FILTER_QUERY_PROPERTY] = g_param_spec_object ("query", "query", "query", FOLKS_TYPE_QUERY, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), CONTACTS_QUERY_FILTER_MIN_STRENGTH_PROPERTY, contacts_query_filter_properties[CONTACTS_QUERY_FILTER_MIN_STRENGTH_PROPERTY] = g_param_spec_uint ("min-strength", "min-strength", "min-strength", 0, G_MAXUINT, 0U, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
contacts_query_filter_instance_init (ContactsQueryFilter * self,
                                     gpointer klass)
{
	gchar* _tmp0_;
	self->priv = contacts_query_filter_get_instance_private (self);
	_tmp0_ = g_strdup ("");
	self->priv->query_string = _tmp0_;
	self->priv->_min_strength = (guint) 0;
}

static void
contacts_query_filter_finalize (GObject * obj)
{
	ContactsQueryFilter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, CONTACTS_TYPE_QUERY_FILTER, ContactsQueryFilter);
	_g_object_unref0 (self->priv->_query);
	_g_free0 (self->priv->query_string);
	G_OBJECT_CLASS (contacts_query_filter_parent_class)->finalize (obj);
}

/**
 * A subclass of {@link Gtk.Filter} which applies a {@link Folks.Query} as a
 * filter on a list of individuals.
 *
 * Since {@link Folks.Query.is_match} returns a "match strength" number, you
 * can specify also an (exclusive) lower bound before the filter returns true.
 */
 G_GNUC_NO_INLINE static GType
contacts_query_filter_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ContactsQueryFilterClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) contacts_query_filter_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ContactsQueryFilter), 0, (GInstanceInitFunc) contacts_query_filter_instance_init, NULL };
	GType contacts_query_filter_type_id;
	contacts_query_filter_type_id = g_type_register_static (gtk_filter_get_type (), "ContactsQueryFilter", &g_define_type_info, 0);
	ContactsQueryFilter_private_offset = g_type_add_instance_private (contacts_query_filter_type_id, sizeof (ContactsQueryFilterPrivate));
	return contacts_query_filter_type_id;
}

GType
contacts_query_filter_get_type (void)
{
	static volatile gsize contacts_query_filter_type_id__once = 0;
	if (g_once_init_enter (&contacts_query_filter_type_id__once)) {
		GType contacts_query_filter_type_id;
		contacts_query_filter_type_id = contacts_query_filter_get_type_once ();
		g_once_init_leave (&contacts_query_filter_type_id__once, contacts_query_filter_type_id);
	}
	return contacts_query_filter_type_id__once;
}

static void
_vala_contacts_query_filter_get_property (GObject * object,
                                          guint property_id,
                                          GValue * value,
                                          GParamSpec * pspec)
{
	ContactsQueryFilter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, CONTACTS_TYPE_QUERY_FILTER, ContactsQueryFilter);
	switch (property_id) {
		case CONTACTS_QUERY_FILTER_QUERY_PROPERTY:
		g_value_set_object (value, contacts_query_filter_get_query (self));
		break;
		case CONTACTS_QUERY_FILTER_MIN_STRENGTH_PROPERTY:
		g_value_set_uint (value, contacts_query_filter_get_min_strength (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_contacts_query_filter_set_property (GObject * object,
                                          guint property_id,
                                          const GValue * value,
                                          GParamSpec * pspec)
{
	ContactsQueryFilter * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, CONTACTS_TYPE_QUERY_FILTER, ContactsQueryFilter);
	switch (property_id) {
		case CONTACTS_QUERY_FILTER_QUERY_PROPERTY:
		contacts_query_filter_set_query (self, g_value_get_object (value));
		break;
		case CONTACTS_QUERY_FILTER_MIN_STRENGTH_PROPERTY:
		contacts_query_filter_set_min_strength (self, g_value_get_uint (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

