/* remove-contact.c generated by valac 0.12.0, the Vala compiler
 * generated from remove-contact.vala, do not modify */

/*
 * Copyright (C) 2011 Collabora Ltd.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Raul Gutierrez Segales <raul.gutierrez.segales@collabora.co.uk>
 *
 */

#include <glib.h>
#include <glib-object.h>
#include <folks-test.h>
#include <tracker-test.h>
#include <folks/folks.h>
#include <stdlib.h>
#include <string.h>
#include <gee.h>
#include <folks/folks-tracker.h>
#include <gio/gio.h>


#define TYPE_REMOVE_CONTACT_TESTS (remove_contact_tests_get_type ())
#define REMOVE_CONTACT_TESTS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_REMOVE_CONTACT_TESTS, RemoveContactTests))
#define REMOVE_CONTACT_TESTS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_REMOVE_CONTACT_TESTS, RemoveContactTestsClass))
#define IS_REMOVE_CONTACT_TESTS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_REMOVE_CONTACT_TESTS))
#define IS_REMOVE_CONTACT_TESTS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_REMOVE_CONTACT_TESTS))
#define REMOVE_CONTACT_TESTS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_REMOVE_CONTACT_TESTS, RemoveContactTestsClass))

typedef struct _RemoveContactTests RemoveContactTests;
typedef struct _RemoveContactTestsClass RemoveContactTestsClass;
typedef struct _RemoveContactTestsPrivate RemoveContactTestsPrivate;
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _tracker_test_backend_unref0(var) ((var == NULL) ? NULL : (var = (tracker_test_backend_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _RemoveContactTestsTestRemoveContactAsyncData RemoveContactTestsTestRemoveContactAsyncData;

struct _RemoveContactTests {
	FolksTestCase parent_instance;
	RemoveContactTestsPrivate * priv;
};

struct _RemoveContactTestsClass {
	FolksTestCaseClass parent_class;
};

struct _RemoveContactTestsPrivate {
	GMainLoop* _main_loop;
	TrackerTestBackend* _tracker_backend;
	FolksIndividualAggregator* _aggregator;
	gboolean _contact_added;
	gboolean _contact_removed;
	gchar* _individual_id;
	gchar* _persona_fullname;
};

struct _RemoveContactTestsTestRemoveContactAsyncData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	RemoveContactTests* self;
	FolksBackendStore* _tmp0_;
	FolksBackendStore* store;
	FolksIndividualAggregator* _tmp1_;
	GError * e;
	GError * _inner_error_;
};


static gpointer remove_contact_tests_parent_class = NULL;

GType remove_contact_tests_get_type (void) G_GNUC_CONST;
#define REMOVE_CONTACT_TESTS_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_REMOVE_CONTACT_TESTS, RemoveContactTestsPrivate))
enum  {
	REMOVE_CONTACT_TESTS_DUMMY_PROPERTY
};
RemoveContactTests* remove_contact_tests_new (void);
RemoveContactTests* remove_contact_tests_construct (GType object_type);
void remove_contact_tests_test_remove_contact (RemoveContactTests* self);
static void _remove_contact_tests_test_remove_contact_folks_test_case_test_method (gpointer self);
static void remove_contact_tests_real_set_up (FolksTestCase* base);
static void remove_contact_tests_real_tear_down (FolksTestCase* base);
static void _remove_contact_tests_test_remove_contact_async (RemoveContactTests* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
static void _remove_contact_tests_test_remove_contact_finish (RemoveContactTests* self, GAsyncResult* _res_);
static gboolean _lambda0_ (RemoveContactTests* self);
static gboolean __lambda0__gsource_func (gpointer self);
static void _remove_contact_tests_test_remove_contact_async_data_free (gpointer _data);
static gboolean _remove_contact_tests_test_remove_contact_async_co (RemoveContactTestsTestRemoveContactAsyncData* data);
static void _remove_contact_tests_test_remove_contact_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static void _remove_contact_tests_individuals_changed_cb (RemoveContactTests* self, GeeSet* added, GeeSet* removed, const gchar* message, FolksPersona* actor, FolksGroupDetailsChangeReason reason);
static void __remove_contact_tests_individuals_changed_cb_folks_individual_aggregator_individuals_changed (FolksIndividualAggregator* _sender, GeeSet* added, GeeSet* removed, const gchar* message, FolksPersona* actor, FolksGroupDetailsChangeReason reason, gpointer self);
static void remove_contact_tests_finalize (GObject* obj);
gint _vala_main (gchar** args, int args_length1);
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);
static gint _vala_array_length (gpointer array);


static void _remove_contact_tests_test_remove_contact_folks_test_case_test_method (gpointer self) {
	remove_contact_tests_test_remove_contact (self);
}


RemoveContactTests* remove_contact_tests_construct (GType object_type) {
	RemoveContactTests * self = NULL;
	TrackerTestBackend* _tmp0_ = NULL;
	self = (RemoveContactTests*) folks_test_case_construct (object_type, "RemoveContactTests");
	_tmp0_ = tracker_test_backend_new ();
	_tracker_test_backend_unref0 (self->priv->_tracker_backend);
	self->priv->_tracker_backend = _tmp0_;
	folks_test_case_add_test ((FolksTestCase*) self, "test removing contacts ", _remove_contact_tests_test_remove_contact_folks_test_case_test_method, self);
	return self;
}


RemoveContactTests* remove_contact_tests_new (void) {
	return remove_contact_tests_construct (TYPE_REMOVE_CONTACT_TESTS);
}


static void remove_contact_tests_real_set_up (FolksTestCase* base) {
	RemoveContactTests * self;
	self = (RemoveContactTests*) base;
}


static void remove_contact_tests_real_tear_down (FolksTestCase* base) {
	RemoveContactTests * self;
	self = (RemoveContactTests*) base;
}


static gboolean _lambda0_ (RemoveContactTests* self) {
	gboolean result = FALSE;
	g_main_loop_quit (self->priv->_main_loop);
	g_assert_not_reached ();
	return result;
}


static gboolean __lambda0__gsource_func (gpointer self) {
	gboolean result;
	result = _lambda0_ (self);
	return result;
}


void remove_contact_tests_test_remove_contact (RemoveContactTests* self) {
	GMainLoop* _tmp0_ = NULL;
	gchar* _tmp1_;
	GeeHashMap* _tmp2_ = NULL;
	GeeHashMap* c1;
	gchar* _tmp3_;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_main_loop_new (NULL, FALSE);
	_g_main_loop_unref0 (self->priv->_main_loop);
	self->priv->_main_loop = _tmp0_;
	_tmp1_ = g_strdup ("persona #1");
	_g_free0 (self->priv->_persona_fullname);
	self->priv->_persona_fullname = _tmp1_;
	_tmp2_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, NULL, NULL, NULL);
	c1 = _tmp2_;
	gee_abstract_map_set ((GeeAbstractMap*) c1, TRF_ONTOLOGY_DEFS_NCO_FULLNAME, self->priv->_persona_fullname);
	tracker_test_backend_add_contact (self->priv->_tracker_backend, c1);
	tracker_test_backend_set_up (self->priv->_tracker_backend);
	self->priv->_contact_added = FALSE;
	self->priv->_contact_removed = FALSE;
	_tmp3_ = g_strdup ("");
	_g_free0 (self->priv->_individual_id);
	self->priv->_individual_id = _tmp3_;
	_remove_contact_tests_test_remove_contact_async (self, NULL, NULL);
	g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, (guint) 5, __lambda0__gsource_func, g_object_ref (self), g_object_unref);
	g_main_loop_run (self->priv->_main_loop);
	g_assert (self->priv->_contact_added == TRUE);
	g_assert (self->priv->_contact_removed == TRUE);
	tracker_test_backend_tear_down (self->priv->_tracker_backend);
	_g_object_unref0 (c1);
}


static void _remove_contact_tests_test_remove_contact_async_data_free (gpointer _data) {
	RemoveContactTestsTestRemoveContactAsyncData* data;
	data = _data;
	_g_object_unref0 (data->self);
	g_slice_free (RemoveContactTestsTestRemoveContactAsyncData, data);
}


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


static void _remove_contact_tests_test_remove_contact_async (RemoveContactTests* self, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	RemoveContactTestsTestRemoveContactAsyncData* _data_;
	_data_ = g_slice_new0 (RemoveContactTestsTestRemoveContactAsyncData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, _remove_contact_tests_test_remove_contact_async);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, _remove_contact_tests_test_remove_contact_async_data_free);
	_data_->self = _g_object_ref0 (self);
	_remove_contact_tests_test_remove_contact_async_co (_data_);
}


static void _remove_contact_tests_test_remove_contact_finish (RemoveContactTests* self, GAsyncResult* _res_) {
	RemoveContactTestsTestRemoveContactAsyncData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static void _remove_contact_tests_test_remove_contact_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	RemoveContactTestsTestRemoveContactAsyncData* data;
	data = _user_data_;
	data->_source_object_ = source_object;
	data->_res_ = _res_;
	_remove_contact_tests_test_remove_contact_async_co (data);
}


static void __remove_contact_tests_individuals_changed_cb_folks_individual_aggregator_individuals_changed (FolksIndividualAggregator* _sender, GeeSet* added, GeeSet* removed, const gchar* message, FolksPersona* actor, FolksGroupDetailsChangeReason reason, gpointer self) {
	_remove_contact_tests_individuals_changed_cb (self, added, removed, message, actor, reason);
}


static gboolean _remove_contact_tests_test_remove_contact_async_co (RemoveContactTestsTestRemoveContactAsyncData* data) {
	switch (data->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	data->_tmp0_ = NULL;
	data->_tmp0_ = folks_backend_store_dup ();
	data->store = data->_tmp0_;
	data->_state_ = 1;
	folks_backend_store_prepare (data->store, _remove_contact_tests_test_remove_contact_async_ready, data);
	return FALSE;
	_state_1:
	folks_backend_store_prepare_finish (data->store, data->_res_);
	data->_tmp1_ = NULL;
	data->_tmp1_ = folks_individual_aggregator_new ();
	_g_object_unref0 (data->self->priv->_aggregator);
	data->self->priv->_aggregator = data->_tmp1_;
	g_signal_connect_object (data->self->priv->_aggregator, "individuals-changed", (GCallback) __remove_contact_tests_individuals_changed_cb_folks_individual_aggregator_individuals_changed, data->self, 0);
	data->_state_ = 2;
	folks_individual_aggregator_prepare (data->self->priv->_aggregator, _remove_contact_tests_test_remove_contact_async_ready, data);
	return FALSE;
	_state_2:
	folks_individual_aggregator_prepare_finish (data->self->priv->_aggregator, data->_res_, &data->_inner_error_);
	if (data->_inner_error_ != NULL) {
		goto __catch0_g_error;
	}
	goto __finally0;
	__catch0_g_error:
	{
		data->e = data->_inner_error_;
		data->_inner_error_ = NULL;
		g_warning ("remove-contact.vala:96: Error when calling prepare: %s\n", data->e->message);
		_g_error_free0 (data->e);
	}
	__finally0:
	if (data->_inner_error_ != NULL) {
		_g_object_unref0 (data->store);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, data->_inner_error_->message, g_quark_to_string (data->_inner_error_->domain), data->_inner_error_->code);
		g_clear_error (&data->_inner_error_);
		return FALSE;
	}
	_g_object_unref0 (data->store);
	if (data->_state_ == 0) {
		g_simple_async_result_complete_in_idle (data->_async_result);
	} else {
		g_simple_async_result_complete (data->_async_result);
	}
	g_object_unref (data->_async_result);
	return FALSE;
}


static void _remove_contact_tests_individuals_changed_cb (RemoveContactTests* self, GeeSet* added, GeeSet* removed, const gchar* message, FolksPersona* actor, FolksGroupDetailsChangeReason reason) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (added != NULL);
	g_return_if_fail (removed != NULL);
	{
		GeeIterator* _tmp0_ = NULL;
		GeeIterator* _i_it;
		_tmp0_ = gee_iterable_iterator ((GeeIterable*) added);
		_i_it = _tmp0_;
		while (TRUE) {
			gboolean _tmp1_;
			gpointer _tmp2_ = NULL;
			FolksIndividual* i;
			const gchar* _tmp3_ = NULL;
			gchar* _tmp4_;
			gchar* full_name;
			_tmp1_ = gee_iterator_next (_i_it);
			if (!_tmp1_) {
				break;
			}
			_tmp2_ = gee_iterator_get (_i_it);
			i = (FolksIndividual*) _tmp2_;
			_tmp3_ = folks_name_details_get_full_name ((FolksNameDetails*) i);
			_tmp4_ = g_strdup (_tmp3_);
			full_name = _tmp4_;
			if (g_strcmp0 (full_name, self->priv->_persona_fullname) == 0) {
				const gchar* _tmp5_ = NULL;
				gchar* _tmp6_;
				self->priv->_contact_added = TRUE;
				_tmp5_ = folks_individual_get_id (i);
				_tmp6_ = g_strdup (_tmp5_);
				_g_free0 (self->priv->_individual_id);
				self->priv->_individual_id = _tmp6_;
				{
					GeeSet* _tmp7_ = NULL;
					GeeIterator* _tmp8_ = NULL;
					GeeIterator* _persona_it;
					_tmp7_ = folks_individual_get_personas (i);
					_tmp8_ = gee_iterable_iterator ((GeeIterable*) _tmp7_);
					_persona_it = _tmp8_;
					while (TRUE) {
						gboolean _tmp9_;
						gpointer _tmp10_ = NULL;
						FolksPersona* persona;
						const gchar* _tmp11_ = NULL;
						gchar** _tmp12_;
						gchar** _tmp13_ = NULL;
						gchar** _tmp14_;
						gint _tmp14__length1;
						gchar* _tmp15_;
						gchar* _tmp16_;
						gchar* contact_id;
						_tmp9_ = gee_iterator_next (_persona_it);
						if (!_tmp9_) {
							break;
						}
						_tmp10_ = gee_iterator_get (_persona_it);
						persona = (FolksPersona*) _tmp10_;
						_tmp11_ = folks_persona_get_iid (persona);
						_tmp13_ = _tmp12_ = g_strsplit (_tmp11_, ":", 0);
						_tmp14_ = _tmp13_;
						_tmp14__length1 = _vala_array_length (_tmp12_);
						_tmp15_ = g_strdup (_tmp14_[1]);
						_tmp16_ = _tmp15_;
						_tmp14_ = (_vala_array_free (_tmp14_, _tmp14__length1, (GDestroyNotify) g_free), NULL);
						contact_id = _tmp16_;
						tracker_test_backend_remove_contact (self->priv->_tracker_backend, contact_id);
						_g_free0 (contact_id);
						_g_object_unref0 (persona);
					}
					_g_object_unref0 (_persona_it);
				}
			}
			_g_free0 (full_name);
			_g_object_unref0 (i);
		}
		_g_object_unref0 (_i_it);
	}
	{
		GeeIterator* _tmp17_ = NULL;
		GeeIterator* _i_it;
		_tmp17_ = gee_iterable_iterator ((GeeIterable*) added);
		_i_it = _tmp17_;
		while (TRUE) {
			gboolean _tmp18_;
			gpointer _tmp19_ = NULL;
			FolksIndividual* i;
			const gchar* _tmp20_ = NULL;
			_tmp18_ = gee_iterator_next (_i_it);
			if (!_tmp18_) {
				break;
			}
			_tmp19_ = gee_iterator_get (_i_it);
			i = (FolksIndividual*) _tmp19_;
			_tmp20_ = folks_individual_get_id (i);
			if (g_strcmp0 (_tmp20_, self->priv->_individual_id) == 0) {
				self->priv->_contact_removed = TRUE;
				g_main_loop_quit (self->priv->_main_loop);
			}
			_g_object_unref0 (i);
		}
		_g_object_unref0 (_i_it);
	}
}


static void remove_contact_tests_class_init (RemoveContactTestsClass * klass) {
	remove_contact_tests_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RemoveContactTestsPrivate));
	FOLKS_TEST_CASE_CLASS (klass)->set_up = remove_contact_tests_real_set_up;
	FOLKS_TEST_CASE_CLASS (klass)->tear_down = remove_contact_tests_real_tear_down;
	G_OBJECT_CLASS (klass)->finalize = remove_contact_tests_finalize;
}


static void remove_contact_tests_instance_init (RemoveContactTests * self) {
	self->priv = REMOVE_CONTACT_TESTS_GET_PRIVATE (self);
}


static void remove_contact_tests_finalize (GObject* obj) {
	RemoveContactTests * self;
	self = REMOVE_CONTACT_TESTS (obj);
	_g_main_loop_unref0 (self->priv->_main_loop);
	_tracker_test_backend_unref0 (self->priv->_tracker_backend);
	_g_object_unref0 (self->priv->_aggregator);
	_g_free0 (self->priv->_individual_id);
	_g_free0 (self->priv->_persona_fullname);
	G_OBJECT_CLASS (remove_contact_tests_parent_class)->finalize (obj);
}


GType remove_contact_tests_get_type (void) {
	static volatile gsize remove_contact_tests_type_id__volatile = 0;
	if (g_once_init_enter (&remove_contact_tests_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RemoveContactTestsClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) remove_contact_tests_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RemoveContactTests), 0, (GInstanceInitFunc) remove_contact_tests_instance_init, NULL };
		GType remove_contact_tests_type_id;
		remove_contact_tests_type_id = g_type_register_static (FOLKS_TYPE_TEST_CASE, "RemoveContactTests", &g_define_type_info, 0);
		g_once_init_leave (&remove_contact_tests_type_id__volatile, remove_contact_tests_type_id);
	}
	return remove_contact_tests_type_id__volatile;
}


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	GTestSuite* _tmp0_ = NULL;
	GTestSuite* root;
	RemoveContactTests* _tmp1_ = NULL;
	RemoveContactTests* _tmp2_;
	GTestSuite* _tmp3_ = NULL;
	g_test_init (&args_length1, &args, NULL);
	_tmp0_ = g_test_get_root ();
	root = _tmp0_;
	_tmp1_ = remove_contact_tests_new ();
	_tmp2_ = _tmp1_;
	_tmp3_ = folks_test_case_get_suite ((FolksTestCase*) _tmp2_);
	g_test_suite_add_suite (root, _tmp3_);
	_g_object_unref0 (_tmp2_);
	g_test_run ();
	result = 0;
	return result;
}


int main (int argc, char ** argv) {
	g_type_init ();
	return _vala_main (argv, argc);
}


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


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



