static void
cursor_data_source_added (ESourceRegistry *registry,
                          ESource *source,
                          gpointer data)
{
	GError    *error = NULL;
	GMainLoop *loop = (GMainLoop *) data;

	if (g_strcmp0 (e_source_get_uid (source), CURSOR_DATA_SOURCE_ID) != 0)
		return;

	/* Open the address book */
	address_book = (EBookClient *) e_book_client_connect_sync (source, 30, NULL, &error);
	if (!address_book)
		g_error ("Unable to create the test book: %s", error->message);

	address_book_source = g_object_ref (source);

	if (loop)
		g_main_loop_quit (loop);
}
static void
import_contacts (void)
{
	EShell *shell;
	ESourceRegistry *registry;
	EClient *client = NULL;
	GList *list;
	gchar *name;
	GString *line;
	FILE *fp;
	gsize offset;
	const gchar *extension_name;
	GError *error = NULL;

	printf ("importing pine addressbook\n");

	shell = e_shell_get_default ();
	registry = e_shell_get_registry (shell);
	extension_name = E_SOURCE_EXTENSION_ADDRESS_BOOK;

	name = g_build_filename (g_get_home_dir (), ".addressbook", NULL);
	fp = fopen (name, "r");
	g_free (name);
	if (fp == NULL)
		return;

	list = e_source_registry_list_sources (registry, extension_name);

	if (list != NULL) {
		ESource *source;

		source = E_SOURCE (list->data);
		client = e_book_client_connect_sync (source, 30, NULL, &error);
	} else {
		/* No address books exist. */
		g_warning ("%s: No address books exist.", G_STRFUNC);
		fclose (fp);
		return;
	}

	g_list_free_full (list, (GDestroyNotify) g_object_unref);

	if (error != NULL) {
		g_warning (
			"%s: Failed to open book client: %s",
			G_STRFUNC, error ? error->message : "Unknown error");
		g_clear_error (&error);
		fclose (fp);
		return;
	}

	line = g_string_new ("");
	g_string_set_size (line, 256);
	offset = 0;
	while (fgets (line->str + offset, 256, fp)) {
		gsize len;

		len = strlen (line->str + offset) + offset;
		if (line->str[len - 1] == '\n')
			g_string_truncate (line, len - 1);
		else if (!feof (fp)) {
			offset = len;
			g_string_set_size (line, len + 256);
			continue;
		} else {
			g_string_truncate (line, len);
		}

		import_contact (E_BOOK_CLIENT (client), line->str);
		offset = 0;
	}

	g_string_free (line, TRUE);
	fclose (fp);
	g_object_unref (client);
}