e_book_backend_ldap_search (EBookBackendLDAP *bl,
                            EDataBook *book,
                            EDataBookView *view)
{
	EBookBackendSExp *sexp;
	const gchar *query;
	gchar *ldap_query;
	GList *contacts;
	GList *l;
	GTimeVal start, end;
	gulong diff;

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

	sexp = e_data_book_view_get_sexp (view);
	query = e_book_backend_sexp_text (sexp);

	if (!e_backend_get_online (E_BACKEND (bl)) || (bl->priv->marked_for_offline && bl->priv->cache)) {
		if (!(bl->priv->marked_for_offline && bl->priv->cache)) {
			GError *edb_err = EDB_ERROR (REPOSITORY_OFFLINE);
			e_data_book_view_notify_complete (view, edb_err);
			g_error_free (edb_err);
			return;
		}

		contacts = e_book_backend_cache_get_contacts (bl->priv->cache, query);

		for (l = contacts; l; l = g_list_next (l)) {
			EContact *contact = l->data;
			e_data_book_view_notify_update (view, contact);
			g_object_unref (contact);
		}

		g_list_free (contacts);

		e_data_book_view_notify_complete (view, NULL /* Success */);
		return;
	}

	ldap_query = e_book_backend_ldap_build_query (bl, query);

	/* search for nonempty full names */
	if (!ldap_query && can_browse ((EBookBackend *) bl))
		ldap_query = g_strdup ("(cn=*)");

	g_rec_mutex_lock (&eds_ldap_handler_lock);
	if (ldap_query != NULL && bl->priv->ldap) {
		gint ldap_err;
		gint search_msgid;
		gint view_limit;

		g_rec_mutex_unlock (&eds_ldap_handler_lock);

		view_limit = bl->priv->ldap_limit;
		/* if (view_limit == -1 || view_limit > bl->priv->ldap_limit)
			view_limit = bl->priv->ldap_limit; */

		if (enable_debug)
			printf ("searching server using filter: %s (expecting max %d results)\n", ldap_query, view_limit);

		do {
			book_view_notify_status (bl, view, _("Searching..."));

			g_rec_mutex_lock (&eds_ldap_handler_lock);
			if (bl->priv->ldap) {
				ldap_err = ldap_search_ext (
					bl->priv->ldap, bl->priv->ldap_rootdn,
					bl->priv->ldap_scope,
					ldap_query,
					NULL, 0,
					NULL, /* XXX */
					NULL, /* XXX */
					NULL, /* XXX timeout */
					view_limit, &search_msgid);
			} else {
				ldap_err = LDAP_SERVER_DOWN;
			}
			g_rec_mutex_unlock (&eds_ldap_handler_lock);
		} while (e_book_backend_ldap_reconnect (bl, view, ldap_err));

		g_free (ldap_query);

		if (ldap_err != LDAP_SUCCESS) {
			book_view_notify_status (bl, view, ldap_err2string (ldap_err));
			return;
		} else if (search_msgid == -1) {
			book_view_notify_status (bl, view,
						 _("Error performing search"));
			return;
		} else {
			LDAPSearchOp *op = g_new0 (LDAPSearchOp, 1);

			d (printf ("adding search_op (%p, %d)\n", view, search_msgid));

			op->view = view;
			op->aborted = FALSE;
			g_object_ref (view);

			ldap_op_add (
				(LDAPOp *) op, E_BOOK_BACKEND (bl), book, view,
				0, search_msgid,
				ldap_search_handler, ldap_search_dtor);

			if (enable_debug) {
				printf ("e_book_backend_ldap_search invoked ldap_search_handler ");
				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);
			}

			g_mutex_lock (&bl->priv->view_mutex);
			g_object_set_data (G_OBJECT (view), LDAP_SEARCH_OP_IDENT, op);
			g_mutex_unlock (&bl->priv->view_mutex);
		}
		return;
	} else {
		g_rec_mutex_unlock (&eds_ldap_handler_lock);
		/* Ignore NULL query */
		e_data_book_view_notify_complete (view, NULL /* Success */);
		return;
	}
}
e_book_backend_ldap_search (EBookBackendLDAP *bl,
                            EDataBook *book,
                            EDataBookView *view)
{
	EBookBackendSExp *sexp;
	const gchar *query;
	gchar *ldap_query;
	GList *contacts;
	GList *l;
	GTimeVal start, end;
	gulong diff;

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

	sexp = e_data_book_view_get_sexp (view);
	query = e_book_backend_sexp_text (sexp);

	if (!e_backend_get_online (E_BACKEND (bl)) || (bl->priv->marked_for_offline && bl->priv->cache)) {
		if (!(bl->priv->marked_for_offline && bl->priv->cache)) {
			GError *edb_err = EDB_ERROR (REPOSITORY_OFFLINE);
			e_data_book_view_notify_complete (view, edb_err);
			g_error_free (edb_err);
			return;
		}

		contacts = e_book_backend_cache_get_contacts (bl->priv->cache, query);

		for (l = contacts; l; l = g_list_next (l)) {
			EContact *contact = l->data;
			e_data_book_view_notify_update (view, contact);
			g_object_unref (contact);
		}

		g_list_free (contacts);

		e_data_book_view_notify_complete (view, NULL  );
		return;
	}

	ldap_query = e_book_backend_ldap_build_query (bl, query);

	 
	if (!ldap_query && can_browse ((EBookBackend *) bl))
		ldap_query = g_strdup ("(cn=*)");

	g_rec_mutex_lock (&eds_ldap_handler_lock);
	if (ldap_query != NULL && bl->priv->ldap) {
		gint ldap_err;
		gint search_msgid;
		gint view_limit;

		g_rec_mutex_unlock (&eds_ldap_handler_lock);

		view_limit = bl->priv->ldap_limit;
		 

		if (enable_debug)
			printf ("searching server using filter: %s (expecting max %d results)\n", ldap_query, view_limit);

		do {
			book_view_notify_status (bl, view, _("Searching..."));

			g_rec_mutex_lock (&eds_ldap_handler_lock);
			if (bl->priv->ldap) {
				ldap_err = ldap_search_ext (
					bl->priv->ldap, bl->priv->ldap_rootdn,
					bl->priv->ldap_scope,
					ldap_query,
					NULL, 0,
					NULL,  
					NULL,  
					NULL,  
					view_limit, &search_msgid);
			} else {
				ldap_err = LDAP_SERVER_DOWN;
			}
			g_rec_mutex_unlock (&eds_ldap_handler_lock);
		} while (e_book_backend_ldap_reconnect (bl, view, ldap_err));

		g_free (ldap_query);

		if (ldap_err != LDAP_SUCCESS) {
			book_view_notify_status (bl, view, ldap_err2string (ldap_err));
			return;
		} else if (search_msgid == -1) {
			book_view_notify_status (bl, view,
						 _("Error performing search"));
			return;
		} else {
			LDAPSearchOp *op = g_new0 (LDAPSearchOp, 1);

			d (printf ("adding search_op (%p, %d)\n", view, search_msgid));

			op->view = view;
			op->aborted = FALSE;
			g_object_ref (view);

			ldap_op_add (
				(LDAPOp *) op, E_BOOK_BACKEND (bl), book, view,
				0, search_msgid,
				ldap_search_handler, ldap_search_dtor);

			if (enable_debug) {
				printf ("e_book_backend_ldap_search invoked ldap_search_handler ");
				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);
			}

			g_mutex_lock (&bl->priv->view_mutex);
			g_object_set_data (G_OBJECT (view), LDAP_SEARCH_OP_IDENT, op);
			g_mutex_unlock (&bl->priv->view_mutex);
		}
		return;
	} else {
		g_rec_mutex_unlock (&eds_ldap_handler_lock);
		 
		e_data_book_view_notify_complete (view, NULL  );
		return;
	}
}