contact_list_uids_handler (LDAPOp *op,
                           LDAPMessage *res)
{
	LDAPGetContactListUIDsOp *contact_list_uids_op = (LDAPGetContactListUIDsOp *) op;
	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
	LDAPMessage *e;
	gint msg_type;
	GTimeVal start, end;
	gulong diff;

	if (enable_debug) {
		printf ("contact_list_uids_handler ...\n");
		g_get_current_time (&start);
	}

	g_rec_mutex_lock (&eds_ldap_handler_lock);
	if (!bl->priv->ldap) {
		g_rec_mutex_unlock (&eds_ldap_handler_lock);
		e_data_book_respond_get_contact_list_uids (op->book, op->opid, EDB_ERROR_NOT_CONNECTED (), NULL);
		ldap_op_finished (op);
		if (enable_debug)
			printf ("contact_list_uids_handler ... ldap handler is NULL \n");
		return;
	}
	g_rec_mutex_unlock (&eds_ldap_handler_lock);

	msg_type = ldap_msgtype (res);
	if (msg_type == LDAP_RES_SEARCH_ENTRY) {
		g_rec_mutex_lock (&eds_ldap_handler_lock);
		if (bl->priv->ldap)
			e = ldap_first_entry (bl->priv->ldap, res);
		else
			e = NULL;
		g_rec_mutex_unlock (&eds_ldap_handler_lock);

		while (NULL != e) {
			EContact *contact;
			gchar *uid = NULL;

			contact = build_contact_from_entry (bl, e, NULL, &uid);
			g_clear_object (&contact);

			if (enable_debug)
				printf ("uid = %s\n", uid ? uid : "(null)");

			if (uid)
				contact_list_uids_op->uids = g_slist_append (contact_list_uids_op->uids, uid);

			g_rec_mutex_lock (&eds_ldap_handler_lock);
			if (bl->priv->ldap)
				e = ldap_next_entry (bl->priv->ldap, e);
			else
				e = NULL;
			g_rec_mutex_unlock (&eds_ldap_handler_lock);
		}
	} else if (msg_type == LDAP_RES_SEARCH_REFERENCE) {
		/* ignore references */
	} else if (msg_type == LDAP_RES_SEARCH_RESULT) {
		gchar *ldap_error_msg = NULL;
		gint ldap_error;

		g_rec_mutex_lock (&eds_ldap_handler_lock);
		if (bl->priv->ldap) {
			ldap_parse_result (
				bl->priv->ldap, res, &ldap_error,
				NULL, &ldap_error_msg, NULL, NULL, 0);
		} else {
			ldap_error = LDAP_SERVER_DOWN;
		}
		g_rec_mutex_unlock (&eds_ldap_handler_lock);
		if (ldap_error != LDAP_SUCCESS) {
			g_warning (
				"contact_list_uids_handler: %02X (%s), additional info: %s",
				ldap_error,
				ldap_err2string (ldap_error), ldap_error_msg);
		}
		if (ldap_error_msg)
			ldap_memfree (ldap_error_msg);

		g_warning ("search returned %d\n", ldap_error);

		if (ldap_error == LDAP_TIMELIMIT_EXCEEDED)
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				EDB_ERROR (SEARCH_TIME_LIMIT_EXCEEDED),
				contact_list_uids_op->uids);
		else if (ldap_error == LDAP_SIZELIMIT_EXCEEDED)
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				EDB_ERROR (SEARCH_SIZE_LIMIT_EXCEEDED),
				contact_list_uids_op->uids);
		else if (ldap_error == LDAP_SUCCESS)
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				EDB_ERROR (SUCCESS),
				contact_list_uids_op->uids);
		else
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				ldap_error_to_response (ldap_error),
				contact_list_uids_op->uids);

		ldap_op_finished (op);
		if (enable_debug) {
			printf ("contact_list_uids_handler success ");
			g_get_current_time (&end);
			diff = end.tv_sec * 1000 + end.tv_usec / 1000;
			diff -= start.tv_sec * 1000 + start.tv_usec / 1000;
			printf ("and took %ld.%03ld seconds\n", diff / 1000, diff % 1000);
		}
	}
	else {
		g_warning ("unhandled search result type %d returned", msg_type);
		e_data_book_respond_get_contact_list_uids (
			op->book, op->opid,
			e_data_book_create_error_fmt (E_DATA_BOOK_STATUS_OTHER_ERROR,
			_("%s: Unhandled search result type %d returned"), G_STRFUNC, msg_type),
			NULL);
		ldap_op_finished (op);
	}
}
contact_list_uids_handler (LDAPOp *op,
                           LDAPMessage *res)
{
	LDAPGetContactListUIDsOp *contact_list_uids_op = (LDAPGetContactListUIDsOp *) op;
	EBookBackendLDAP *bl = E_BOOK_BACKEND_LDAP (op->backend);
	LDAPMessage *e;
	gint msg_type;
	GTimeVal start, end;
	gulong diff;

	if (enable_debug) {
		printf ("contact_list_uids_handler ...\n");
		g_get_current_time (&start);
	}

	g_rec_mutex_lock (&eds_ldap_handler_lock);
	if (!bl->priv->ldap) {
		g_rec_mutex_unlock (&eds_ldap_handler_lock);
		e_data_book_respond_get_contact_list_uids (op->book, op->opid, EDB_ERROR_NOT_CONNECTED (), NULL);
		ldap_op_finished (op);
		if (enable_debug)
			printf ("contact_list_uids_handler ... ldap handler is NULL \n");
		return;
	}
	g_rec_mutex_unlock (&eds_ldap_handler_lock);

	msg_type = ldap_msgtype (res);
	if (msg_type == LDAP_RES_SEARCH_ENTRY) {
		g_rec_mutex_lock (&eds_ldap_handler_lock);
		if (bl->priv->ldap)
			e = ldap_first_entry (bl->priv->ldap, res);
		else
			e = NULL;
		g_rec_mutex_unlock (&eds_ldap_handler_lock);

		while (NULL != e) {
			EContact *contact;
			gchar *uid = NULL;

			contact = build_contact_from_entry (bl, e, NULL, &uid);
			g_clear_object (&contact);

			if (enable_debug)
				printf ("uid = %s\n", uid ? uid : "(null)");

			if (uid)
				contact_list_uids_op->uids = g_slist_append (contact_list_uids_op->uids, uid);

			g_rec_mutex_lock (&eds_ldap_handler_lock);
			if (bl->priv->ldap)
				e = ldap_next_entry (bl->priv->ldap, e);
			else
				e = NULL;
			g_rec_mutex_unlock (&eds_ldap_handler_lock);
		}
	} else if (msg_type == LDAP_RES_SEARCH_REFERENCE) {
		 
	} else if (msg_type == LDAP_RES_SEARCH_RESULT) {
		gchar *ldap_error_msg = NULL;
		gint ldap_error;

		g_rec_mutex_lock (&eds_ldap_handler_lock);
		if (bl->priv->ldap) {
			ldap_parse_result (
				bl->priv->ldap, res, &ldap_error,
				NULL, &ldap_error_msg, NULL, NULL, 0);
		} else {
			ldap_error = LDAP_SERVER_DOWN;
		}
		g_rec_mutex_unlock (&eds_ldap_handler_lock);
		if (ldap_error != LDAP_SUCCESS) {
			g_warning (
				"contact_list_uids_handler: %02X (%s), additional info: %s",
				ldap_error,
				ldap_err2string (ldap_error), ldap_error_msg);
		}
		if (ldap_error_msg)
			ldap_memfree (ldap_error_msg);

		g_warning ("search returned %d\n", ldap_error);

		if (ldap_error == LDAP_TIMELIMIT_EXCEEDED)
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				EDB_ERROR (SEARCH_TIME_LIMIT_EXCEEDED),
				contact_list_uids_op->uids);
		else if (ldap_error == LDAP_SIZELIMIT_EXCEEDED)
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				EDB_ERROR (SEARCH_SIZE_LIMIT_EXCEEDED),
				contact_list_uids_op->uids);
		else if (ldap_error == LDAP_SUCCESS)
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				EDB_ERROR (SUCCESS),
				contact_list_uids_op->uids);
		else
			e_data_book_respond_get_contact_list_uids (
				op->book, op->opid,
				ldap_error_to_response (ldap_error),
				contact_list_uids_op->uids);

		ldap_op_finished (op);
		if (enable_debug) {
			printf ("contact_list_uids_handler success ");
			g_get_current_time (&end);
			diff = end.tv_sec * 1000 + end.tv_usec / 1000;
			diff -= start.tv_sec * 1000 + start.tv_usec / 1000;
			printf ("and took %ld.%03ld seconds\n", diff / 1000, diff % 1000);
		}
	}
	else {
		g_warning ("unhandled search result type %d returned", msg_type);
		e_data_book_respond_get_contact_list_uids (
			op->book, op->opid,
			e_data_book_create_error_fmt (E_DATA_BOOK_STATUS_OTHER_ERROR,
			_("%s: Unhandled search result type %d returned"), G_STRFUNC, msg_type),
			NULL);
		ldap_op_finished (op);
	}
}