/* gcc `pkg-config --cflags --libs libsoup-2.4 glib-2.0 libecal-1.2` test-cal.c -g -O0 -o test-cal && ./test-cal */

#include <glib.h>
#include <stdio.h>
#include <string.h>
#include <libecal/libecal.h>

static void
test_func (ESource *source)
{
	EClient *client;
	GError *error = NULL;

	client = e_cal_client_connect_sync (source, E_CAL_CLIENT_SOURCE_TYPE_EVENTS, NULL, &error);
	if (client) {
		printf ("%s: got a client %p%s%s%s\n", __FUNCTION__, client, error ? " (" : "", error ? error->message : "", error ? ")" : "");
		g_object_unref (client);
	} else {
		printf ("%s: Failed to get client: %s\n", __FUNCTION__, error ? error->message : "Unknown error");
	}

	g_clear_error (&error);
}

static gpointer
test_thread (gpointer user_data)
{
	GMainLoop *loop = user_data;
	ESource *source;
	ESourceRegistry *registry;

	registry = e_source_registry_new_sync (NULL, NULL);
	g_return_val_if_fail (registry != NULL, NULL);

	source = e_source_registry_ref_source (registry, "1368717315.31246.0@zyxPad");
	g_return_val_if_fail (source != NULL, NULL);

	test_func (source);

	g_object_unref (source);
	g_object_unref (registry);

	g_main_loop_quit (loop);

	return NULL;
}

static gboolean
start_thread (gpointer user_data)
{
	GThread *thread;

	thread = g_thread_new (NULL, test_thread, user_data);
	g_thread_unref (thread);

	return FALSE;
}

int main (int argc, char *argv[])
{
	GMainLoop *loop;

	g_type_init ();

	loop = g_main_loop_new (NULL, FALSE);
	g_idle_add (start_thread, loop);
	g_main_loop_run (loop);
	g_main_loop_unref (loop);

	return 0;
}
