// gcc -Wall -o test-gvfs `pkg-config --libs --cflags glib-2.0 gio-2.0 gthread-2.0` test-gvfs.c

#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>

#define REPEATS 100

static void
load_contents_cb (GObject *source_object, GAsyncResult *result, gpointer user_data)
{
	gchar *contents;
	gsize length;
	GError *error = NULL;

	if (g_file_load_contents_finish (G_FILE (source_object), result, &contents, &length, NULL, &error) == FALSE) {
		/* Error! */
		g_message ("Error in request %u: %s", GPOINTER_TO_UINT (user_data), error->message);
		g_error_free (error);
		return;
	}

	g_free (contents);
	g_message ("Request %u successful (length returned: %" G_GSIZE_FORMAT ").", GPOINTER_TO_UINT (user_data), length);
}

int main (int argc, char **argv)
{
	guint i;
	GMainLoop *main_loop;

	g_type_init ();
	g_thread_init (NULL);

	main_loop = g_main_loop_new (NULL, TRUE);

	for (i = 0; i < REPEATS; i++) {
		gchar *_uri;
		GFile *uri;

		_uri = g_strdup_printf ("http://tecnocode.co.uk/%u", i);
		uri = g_file_new_for_uri (_uri);
		g_free (_uri);

		g_message ("Sending request %u of %u", i, REPEATS);
		g_file_load_contents_async (uri, NULL, (GAsyncReadyCallback) load_contents_cb, GUINT_TO_POINTER (i));
		g_object_unref (uri);
	}

	g_main_loop_run (main_loop);

	return 0;
}
