static gboolean
fill_book_client (EBookClient *book_client,
                  GSList **uids)
{
	EContact *contact;

	g_return_val_if_fail (book_client != NULL, FALSE);
	g_return_val_if_fail (uids != NULL, FALSE);

	*uids = NULL;

	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact))
		return FALSE;

	*uids = g_slist_append (*uids, e_contact_get (contact, E_CONTACT_UID));
	g_object_unref (contact);

	if (!add_contact_from_test_case_verify (book_client, "simple-2", &contact))
		return FALSE;

	*uids = g_slist_append (*uids, e_contact_get (contact, E_CONTACT_UID));
	g_object_unref (contact);

	return TRUE;
}
static void
setup_book (EBookClient *book_client)
{
	if (!add_contact_from_test_case_verify (book_client, "simple-1", NULL) ||
	    !add_contact_from_test_case_verify (book_client, "simple-2", NULL))
		g_error ("Failed to add contacts");
}
static void
get_revision_compare_cycle (EBookClient *client)
{
	gchar    *revision_before = NULL, *revision_after = NULL;
	EContact *contact = NULL;
	GError   *error = NULL;

	if (!e_client_get_backend_property_sync (E_CLIENT (client), CLIENT_BACKEND_PROPERTY_REVISION, &revision_before, NULL, &error))
		g_error ("Error getting book revision: %s", error->message);

	if (!add_contact_from_test_case_verify (client, "simple-1", &contact)) {
		g_object_unref (client);
		exit (1);
	}

	if (!e_book_client_remove_contact_sync (client, contact, NULL, &error))
		g_error ("Unable to remove contact: %s", error->message);

	g_object_unref (contact);

	if (!e_client_get_backend_property_sync (E_CLIENT (client), CLIENT_BACKEND_PROPERTY_REVISION, &revision_after, NULL, &error))
		g_error ("Error getting book revision: %s", error->message);

	g_assert (revision_before);
	g_assert (revision_after);
	g_assert (strcmp (revision_before, revision_after) != 0);

	g_message (
		"Passed cycle, revision before '%s' revision after '%s'",
		revision_before, revision_after);

	g_free (revision_before);
	g_free (revision_after);
}
static void
test_remove_contact_async (ETestServerFixture *fixture,
                           gconstpointer user_data)
{
	EBookClient *book_client;
	EContact *contact = NULL;
	gchar *uid;
	RemoveData data;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact))
		g_error ("Failed to add contact");

	uid = e_contact_get (contact, E_CONTACT_UID);

	data.uid = uid;
	data.loop = fixture->loop;
	e_book_client_remove_contact (book_client, contact, NULL, remove_contact_cb, &data);

	g_object_unref (contact);

	g_main_loop_run (fixture->loop);
	g_free (uid);
}
static void
test_remove_contact_sync (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact = NULL;
	gchar *uid;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact))
		g_error ("Failed to add contact");

	uid = e_contact_get (contact, E_CONTACT_UID);

	if (!e_book_client_remove_contact_sync (book_client, contact, NULL, &error))
		g_error ("remove contact sync: %s", error->message);

	g_object_unref (contact);

	check_removed_contact (book_client, uid);

	g_free (uid);
}
static void
test_modify_contact_sync (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact, *book_contact;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	if (!add_contact_from_test_case_verify (book_client, "name-only", &contact))
		g_error ("Failed to add contact");

	verify_premodify_and_prepare_contact (contact);

	if (!e_book_client_modify_contact_sync (book_client, contact, NULL, &error))
		g_error ("modify contact sync: %s", error->message);

	if (!e_book_client_get_contact_sync (book_client, e_contact_get_const (contact, E_CONTACT_UID),
					     &book_contact, NULL, &error))
		g_error ("get contact sync: %s", error->message);

	verify_modify (book_contact);
	g_object_unref (book_contact);
	g_object_unref (contact);
}
static void
test_modify_contact_async (ETestServerFixture *fixture,
                          gconstpointer user_data)
{
	EBookClient *book_client;
	EContact *contact;
	ModifyData data;

	book_client = E_TEST_SERVER_UTILS_SERVICE (fixture, EBookClient);

	if (!add_contact_from_test_case_verify (book_client, "name-only", &contact))
		g_error ("Failed to add contact");

	verify_premodify_and_prepare_contact (contact);

	data.contact = contact;
	data.loop = fixture->loop;
	e_book_client_modify_contact (book_client, contact, NULL, contact_modified_cb, &data);

	g_main_loop_run (fixture->loop);

	g_object_unref (contact);
}
static void
add_contact (EBookClient *book_client)
{
	g_return_if_fail (add_contact_from_test_case_verify (book_client, "name-only", NULL));
}
gint
main (gint argc,
      gchar **argv)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact;
	gchar *vcard;

	main_initialize ();

	/*
	 * Setup
	 */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	/*
	 * Sync version
	 */
	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) {
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (contact);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

	/*
	 * Async version
	 */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	vcard = new_vcard_from_test_case ("simple-1");
	contact = e_contact_new_from_vcard (vcard);
	g_free (vcard);

	e_book_client_add_contact (book_client, contact, NULL, add_contact_cb, NULL);
	g_object_unref (contact);

	start_main_loop (NULL, NULL);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

	return get_main_loop_stop_result ();
}
gint
main (gint argc,
      gchar **argv)
{
	EBookClient *book_client;
	GError *error = NULL;
	EContact *contact;
	gchar *uid;

	main_initialize ();

        /*
         * Setup
         */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

        /*
         * Sync version
         */
	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) {
		g_object_unref (book_client);
		return 1;
	}

	uid = e_contact_get (contact, E_CONTACT_UID);
	g_object_unref (contact);

	if (!e_book_client_remove_contact_by_uid_sync (book_client, uid, NULL, &error)) {
		report_error ("remove contact sync", &error);
		g_object_unref (book_client);
		g_free (uid);
		return 1;
	}

	if (!e_book_client_get_contact_sync (book_client, uid, &contact, NULL, &error) &&
	    g_error_matches (error, E_BOOK_CLIENT_ERROR, E_BOOK_CLIENT_ERROR_CONTACT_NOT_FOUND)) {
		g_clear_error (&error);
	} else {
		report_error ("fail with get contact sync on removed contact", &error);
		g_object_unref (book_client);
		g_free (uid);
		return 1;
	}

	g_free (uid);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

        /*
         * Async version
         */
	book_client = new_temp_client (NULL);
	g_return_val_if_fail (book_client != NULL, 1);

	if (!e_client_open_sync (E_CLIENT (book_client), FALSE, NULL, &error)) {
		report_error ("client open sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	contact = NULL;

	if (!add_contact_from_test_case_verify (book_client, "simple-1", &contact)) {
		g_object_unref (book_client);
		return 1;
	}

	uid = e_contact_get (contact, E_CONTACT_UID);
	g_object_unref (contact);
	e_book_client_remove_contact_by_uid (book_client, uid, NULL, remove_contact_by_uid_cb, uid);

	start_main_loop (NULL, NULL);

	g_free (uid);

	if (!e_client_remove_sync (E_CLIENT (book_client), NULL, &error)) {
		report_error ("client remove sync", &error);
		g_object_unref (book_client);
		return 1;
	}

	g_object_unref (book_client);

	return 0;
}