Fix Bug 124553 and Bug 297549 - Changes to various LDAP parameters in Address Book window not reflected by Quick search until restart. r=Neil,sr=bienvenu

git-svn-id: svn://10.0.0.236/trunk@228246 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bugzilla%standard8.demon.co.uk
2007-06-18 18:17:59 +00:00
parent 8dfca5b8c7
commit 435f56d246
5 changed files with 415 additions and 443 deletions

View File

@@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
@@ -42,13 +42,13 @@
#include "nsReadableUtils.h"
#include "netCore.h"
#include "plstr.h"
#include "nsCOMPtr.h"
// The two schemes we support, LDAP and LDAPS
//
static const char kLDAPScheme[] = "ldap";
static const char kLDAPSSLScheme[] = "ldaps";
// Constructor and destructor
//
NS_IMPL_THREADSAFE_ISUPPORTS2(nsLDAPURL, nsILDAPURL, nsIURI)
@@ -363,10 +363,33 @@ NS_IMETHODIMP nsLDAPURL::GetOriginCharset(nsACString &result)
}
// boolean equals (in nsIURI other)
//
// (based on nsSimpleURI::Equals)
NS_IMETHODIMP nsLDAPURL::Equals(nsIURI *other, PRBool *_retval)
{
return NS_ERROR_NOT_IMPLEMENTED;
*_retval = PR_FALSE;
if (other)
{
nsresult rv;
nsCOMPtr<nsILDAPURL> otherURL(do_QueryInterface(other, &rv));
if (NS_SUCCEEDED(rv))
{
nsCAutoString thisSpec, otherSpec;
PRUint32 otherOptions;
rv = GetSpec(thisSpec);
NS_ENSURE_SUCCESS(rv, rv);
rv = otherURL->GetSpec(otherSpec);
NS_ENSURE_SUCCESS(rv, rv);
rv = otherURL->GetOptions(&otherOptions);
NS_ENSURE_SUCCESS(rv, rv);
if (thisSpec == otherSpec && mOptions == otherOptions)
*_retval = PR_TRUE;
}
}
return NS_OK;
}
// boolean schemeIs(in const char * scheme);
@@ -513,11 +536,7 @@ NS_IMETHODIMP nsLDAPURL::HasAttribute(const char *aAttribute, PRBool *_retval)
}
str = nsDependentCString(aAttribute);
if (mAttributes->IndexOfIgnoreCase(str) >= 0) {
*_retval = PR_TRUE;
} else {
*_retval = PR_FALSE;
}
*_retval = mAttributes->IndexOfIgnoreCase(str) >= 0;
return NS_OK;
}

View File

@@ -59,28 +59,25 @@
#define kDefaultMaxHits 100
nsAbLDAPDirectory::nsAbLDAPDirectory() :
nsAbDirectoryRDFResource(),
nsAbLDAPDirectoryQuery (),
mInitialized(PR_FALSE),
mInitializedConnection(PR_FALSE),
mPerformingQuery(PR_FALSE),
mContext(0),
mLock(0)
nsAbDirectoryRDFResource(),
nsAbLDAPDirectoryQuery(),
mPerformingQuery(PR_FALSE),
mContext(0),
mLock(0)
{
}
nsAbLDAPDirectory::~nsAbLDAPDirectory()
{
if (mLock)
PR_DestroyLock (mLock);
if (mLock)
PR_DestroyLock (mLock);
}
NS_IMPL_ISUPPORTS_INHERITED4(nsAbLDAPDirectory, nsAbDirectoryRDFResource, nsIAbDirectory, nsIAbDirectoryQuery, nsIAbDirectorySearch, nsIAbLDAPDirectory)
NS_IMPL_ISUPPORTS_INHERITED4(nsAbLDAPDirectory, nsAbDirectoryRDFResource, nsIAbDirectory,
nsIAbDirectoryQuery, nsIAbDirectorySearch, nsIAbLDAPDirectory)
NS_IMETHODIMP nsAbLDAPDirectory::Init(const char* aURI)
{
mInitialized = PR_FALSE;
// We need to ensure that the m_DirPrefId is initialized properly
nsCAutoString uri(aURI);
@@ -98,100 +95,17 @@ NS_IMETHODIMP nsAbLDAPDirectory::Init(const char* aURI)
return nsAbDirectoryRDFResource::Init(aURI);
}
nsresult nsAbLDAPDirectory::Initiate ()
nsresult nsAbLDAPDirectory::Initiate()
{
if (!mIsQueryURI)
return NS_ERROR_FAILURE;
if (!mIsQueryURI)
return NS_ERROR_FAILURE;
if (mInitialized)
return NS_OK;
if (!mLock)
mLock = PR_NewLock();
nsresult rv;
mLock = PR_NewLock ();
if(!mLock)
{
return NS_ERROR_OUT_OF_MEMORY;
}
rv = nsAbQueryStringToExpression::Convert (mQueryString.get (),
getter_AddRefs(mExpression));
NS_ENSURE_SUCCESS(rv, rv);
rv = InitiateConnection ();
mInitialized = PR_TRUE;
return rv;
return mLock ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
nsresult nsAbLDAPDirectory::InitiateConnection ()
{
if (mInitializedConnection)
return NS_OK;
nsresult rv;
mURL = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Rather than using GetURI here we call GetStringValue directly so
// we can handle the case where the URI isn't specified (see comments
// below)
nsCAutoString URI;
rv = GetStringValue("uri", EmptyCString(), URI);
if (NS_FAILED(rv) || URI.IsEmpty())
{
/*
* A recent change in Mozilla now means that the LDAP Address Book
* RDF Resource URI is based on the unique preference name value i.e.
* [moz-abldapdirectory://prefName]
* Prior to this valid change it was based on the actual uri i.e.
* [moz-abldapdirectory://host:port/basedn]
* Basing the resource on the prefName allows these attributes to
* change.
*
* But the uri value was also the means by which third-party
* products could integrate with Mozilla's LDAP Address Books
* without necessarily having an entry in the preferences file
* or more importantly needing to be able to change the
* preferences entries. Thus to set the URI Spec now, it is
* only necessary to read the uri pref entry, while in the
* case where it is not a preference, we need to replace the
* "moz-abldapdirectory".
*/
nsCAutoString tempLDAPURL(mURINoQuery);
tempLDAPURL.ReplaceSubstring("moz-abldapdirectory:", "ldap:");
rv = mURL->SetSpec(tempLDAPURL);
}
else
{
rv = mURL->SetSpec(URI);
}
NS_ENSURE_SUCCESS(rv,rv);
// get the login information, if there is any
//
rv = GetAuthDn(mLogin);
if (NS_FAILED(rv)) {
mLogin.Truncate(); // zero out mLogin
}
// get the protocol version, if there is any. using a string pref
// here instead of an int, as protocol versions sometimes have names like
// "4bis".
//
GetProtocolVersion(&mProtocolVersion);
// otherwise we leave mProtocolVersion as the default (see the initializers
// for the nsAbLDAPDirectoryQuery class).
mConnection = do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
mInitializedConnection = PR_TRUE;
return rv;
}
/*
*
* nsIAbDirectory methods
@@ -214,14 +128,14 @@ NS_IMETHODIMP nsAbLDAPDirectory::GetURI(nsACString &aURI)
NS_IMETHODIMP nsAbLDAPDirectory::GetOperations(PRInt32 *aOperations)
{
*aOperations = nsIAbDirectory::opSearch;
return NS_OK;
*aOperations = nsIAbDirectory::opSearch;
return NS_OK;
}
NS_IMETHODIMP nsAbLDAPDirectory::GetChildNodes(nsISimpleEnumerator* *aResult)
{
nsCOMArray<nsIAbDirectory> children;
return NS_NewArrayEnumerator(aResult, children);
nsCOMArray<nsIAbDirectory> children;
return NS_NewArrayEnumerator(aResult, children);
}
NS_IMETHODIMP nsAbLDAPDirectory::GetChildCards(nsISimpleEnumerator** result)
@@ -279,60 +193,66 @@ NS_IMETHODIMP nsAbLDAPDirectory::GetChildCards(nsISimpleEnumerator** result)
NS_IMETHODIMP nsAbLDAPDirectory::HasCard(nsIAbCard* card, PRBool* hasCard)
{
nsresult rv;
nsresult rv = Initiate ();
NS_ENSURE_SUCCESS(rv, rv);
rv = Initiate ();
NS_ENSURE_SUCCESS(rv, rv);
nsVoidKey key (NS_STATIC_CAST(void* ,card));
nsVoidKey key (NS_STATIC_CAST(void* ,card));
// Enter lock
nsAutoLock lock (mLock);
// Enter lock
nsAutoLock lock (mLock);
*hasCard = mCache.Exists (&key);
if (!*hasCard && mPerformingQuery)
return NS_ERROR_NOT_AVAILABLE;
*hasCard = mCache.Exists (&key);
if (!*hasCard && mPerformingQuery)
return NS_ERROR_NOT_AVAILABLE;
return NS_OK;
return NS_OK;
}
/*
*
* nsAbLDAPDirectoryQuery methods
*
*/
nsresult nsAbLDAPDirectory::GetLDAPConnection (nsILDAPConnection** connection)
nsresult nsAbLDAPDirectory::GetLDAPURL(nsILDAPURL** url)
{
nsresult rv;
NS_ENSURE_ARG_POINTER(url);
rv = InitiateConnection ();
NS_ENSURE_SUCCESS(rv, rv);
nsresult rv;
nsCOMPtr<nsILDAPURL> result = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
NS_IF_ADDREF(*connection = mConnection);
return rv;
}
// Rather than using GetURI here we call GetStringValue directly so
// we can handle the case where the URI isn't specified (see comments
// below)
nsCAutoString URI;
rv = GetStringValue("uri", EmptyCString(), URI);
if (NS_FAILED(rv) || URI.IsEmpty())
{
/*
* A recent change in Mozilla now means that the LDAP Address Book
* RDF Resource URI is based on the unique preference name value i.e.
* [moz-abldapdirectory://prefName]
* Prior to this valid change it was based on the actual uri i.e.
* [moz-abldapdirectory://host:port/basedn]
* Basing the resource on the prefName allows these attributes to
* change.
*
* But the uri value was also the means by which third-party
* products could integrate with Mozilla's LDAP Address Books
* without necessarily having an entry in the preferences file
* or more importantly needing to be able to change the
* preferences entries. Thus to set the URI Spec now, it is
* only necessary to read the uri pref entry, while in the
* case where it is not a preference, we need to replace the
* "moz-abldapdirectory".
*/
nsCAutoString tempLDAPURL(mURINoQuery);
tempLDAPURL.ReplaceSubstring("moz-abldapdirectory:", "ldap:");
rv = result->SetSpec(tempLDAPURL);
}
else
{
rv = result->SetSpec(URI);
}
NS_ENSURE_SUCCESS(rv, rv);
nsresult nsAbLDAPDirectory::GetLDAPURL (nsILDAPURL** url)
{
nsresult rv;
rv = InitiateConnection ();
NS_ENSURE_SUCCESS(rv, rv);
NS_IF_ADDREF(*url = mURL);
return rv;
}
nsresult nsAbLDAPDirectory::CreateCard (nsILDAPURL* uri, const char* dn, nsIAbCard** result)
{
nsresult rv;
nsCOMPtr <nsIAbCard> card = do_CreateInstance(NS_ABLDAPCARD_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
NS_IF_ADDREF(*result = card);
return NS_OK;
result.swap(*url);
return rv;
}
/*
@@ -343,30 +263,33 @@ nsresult nsAbLDAPDirectory::CreateCard (nsILDAPURL* uri, const char* dn, nsIAbCa
NS_IMETHODIMP nsAbLDAPDirectory::StartSearch ()
{
nsresult rv;
if (!mIsQueryURI || mQueryString.IsEmpty())
return NS_OK;
rv = Initiate ();
nsresult rv = Initiate();
NS_ENSURE_SUCCESS(rv, rv);
rv = StopSearch ();
rv = StopSearch();
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbDirectoryQueryArguments> arguments = do_CreateInstance(NS_ABDIRECTORYQUERYARGUMENTS_CONTRACTID,&rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = arguments->SetExpression (mExpression);
nsCOMPtr<nsIAbBooleanExpression> expression;
rv = nsAbQueryStringToExpression::Convert(mQueryString.get(),
getter_AddRefs(expression));
NS_ENSURE_SUCCESS(rv, rv);
rv = arguments->SetExpression(expression);
NS_ENSURE_SUCCESS(rv, rv);
// Set the return properties to
// return nsIAbCard interfaces
const char *arr = "card:nsIAbCard";
rv = arguments->SetReturnProperties (1, &arr);
rv = arguments->SetReturnProperties(1, &arr);
NS_ENSURE_SUCCESS(rv, rv);
rv = arguments->SetQuerySubDirectories (PR_TRUE);
rv = arguments->SetQuerySubDirectories(PR_TRUE);
NS_ENSURE_SUCCESS(rv, rv);
// Set the the query listener
@@ -407,64 +330,55 @@ NS_IMETHODIMP nsAbLDAPDirectory::StartSearch ()
NS_IMETHODIMP nsAbLDAPDirectory::StopSearch ()
{
nsresult rv;
nsresult rv = Initiate();
NS_ENSURE_SUCCESS(rv, rv);
rv = Initiate ();
NS_ENSURE_SUCCESS(rv, rv);
// Enter lock
{
nsAutoLock lockGuard(mLock);
if (!mPerformingQuery)
return NS_OK;
mPerformingQuery = PR_FALSE;
}
// Exit lock
// Enter lock
{
nsAutoLock lockGuard (mLock);
if (!mPerformingQuery)
return NS_OK;
mPerformingQuery = PR_FALSE;
}
// Exit lock
rv = StopQuery (mContext);
return rv;
return StopQuery(mContext);
}
/*
*
* nsAbDirSearchListenerContext methods
*
*/
nsresult nsAbLDAPDirectory::OnSearchFinished (PRInt32 result)
nsresult nsAbLDAPDirectory::OnSearchFinished(PRInt32 result)
{
nsresult rv;
nsresult rv = Initiate();
NS_ENSURE_SUCCESS(rv, rv);
rv = Initiate ();
NS_ENSURE_SUCCESS(rv, rv);
nsAutoLock lock(mLock);
mPerformingQuery = PR_FALSE;
nsAutoLock lock (mLock);
mPerformingQuery = PR_FALSE;
return NS_OK;
return NS_OK;
}
nsresult nsAbLDAPDirectory::OnSearchFoundCard (nsIAbCard* card)
nsresult nsAbLDAPDirectory::OnSearchFoundCard(nsIAbCard* card)
{
nsresult rv;
nsresult rv = Initiate();
NS_ENSURE_SUCCESS(rv, rv);
rv = Initiate ();
NS_ENSURE_SUCCESS(rv, rv);
nsVoidKey key (NS_STATIC_CAST(void* ,card));
// Enter lock
{
nsAutoLock lock(mLock);
mCache.Put(&key, card);
}
// Exit lock
nsVoidKey key (NS_STATIC_CAST(void* ,card));
// Enter lock
{
nsAutoLock lock (mLock);
mCache.Put (&key, card);
}
// Exit lock
nsCOMPtr<nsIAddrBookSession> abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv);
if(NS_SUCCEEDED(rv))
abSession->NotifyDirectoryItemAdded(this, card);
nsCOMPtr<nsIAddrBookSession> abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv);
if(NS_SUCCEEDED(rv))
abSession->NotifyDirectoryItemAdded(this, card);
return NS_OK;
return NS_OK;
}
NS_IMETHODIMP nsAbLDAPDirectory::GetSupportsMailingLists(PRBool *aSupportsMailingsLists)
@@ -508,29 +422,29 @@ NS_IMETHODIMP nsAbLDAPDirectory::GetSearchDuringLocalAutocomplete(PRBool *aSearc
NS_IMETHODIMP
nsAbLDAPDirectory::GetSearchClientControls(nsIMutableArray **aControls)
{
NS_IF_ADDREF(*aControls = mSearchClientControls);
return NS_OK;
NS_IF_ADDREF(*aControls = mSearchClientControls);
return NS_OK;
}
NS_IMETHODIMP
nsAbLDAPDirectory::SetSearchClientControls(nsIMutableArray *aControls)
{
mSearchClientControls = aControls;
return NS_OK;
mSearchClientControls = aControls;
return NS_OK;
}
NS_IMETHODIMP
nsAbLDAPDirectory::GetSearchServerControls(nsIMutableArray **aControls)
{
NS_IF_ADDREF(*aControls = mSearchServerControls);
return NS_OK;
NS_IF_ADDREF(*aControls = mSearchServerControls);
return NS_OK;
}
NS_IMETHODIMP
nsAbLDAPDirectory::SetSearchServerControls(nsIMutableArray *aControls)
{
mSearchServerControls = aControls;
return NS_OK;
mSearchServerControls = aControls;
return NS_OK;
}
NS_IMETHODIMP nsAbLDAPDirectory::GetProtocolVersion(PRUint32 *aProtocolVersion)

View File

@@ -1,4 +1,4 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
@@ -49,64 +49,54 @@
#include "nsHashtable.h"
class nsAbLDAPDirectory :
public nsAbDirectoryRDFResource, // nsIRDFResource
public nsAbDirProperty, // nsIAbDirectory
public nsAbLDAPDirectoryQuery, // nsIAbDirectoryQuery
public nsIAbDirectorySearch,
public nsAbDirSearchListenerContext,
public nsIAbLDAPDirectory
public nsAbDirectoryRDFResource, // nsIRDFResource
public nsAbDirProperty, // nsIAbDirectory
public nsAbLDAPDirectoryQuery, // nsIAbDirectoryQuery
public nsIAbDirectorySearch,
public nsAbDirSearchListenerContext,
public nsIAbLDAPDirectory
{
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_ISUPPORTS_INHERITED
nsAbLDAPDirectory();
virtual ~nsAbLDAPDirectory();
nsAbLDAPDirectory();
virtual ~nsAbLDAPDirectory();
NS_IMETHOD Init(const char *aUri);
NS_IMETHOD Init(const char *aUri);
// nsIAbDirectory methods
NS_IMETHOD GetURI(nsACString &aURI);
NS_IMETHOD GetOperations(PRInt32 *aOperations);
NS_IMETHOD GetChildNodes(nsISimpleEnumerator* *result);
NS_IMETHOD GetChildCards(nsISimpleEnumerator* *result);
NS_IMETHOD HasCard(nsIAbCard *cards, PRBool *hasCard);
NS_IMETHOD GetSupportsMailingLists(PRBool *aSupportsMailingsLists);
NS_IMETHOD GetIsRemote(PRBool *aIsRemote);
NS_IMETHOD GetIsSecure(PRBool *aIsRemote);
NS_IMETHOD GetSearchDuringLocalAutocomplete(PRBool *aSearchDuringLocalAutocomplete);
NS_IMETHOD GetOperations(PRInt32 *aOperations);
NS_IMETHOD GetChildNodes(nsISimpleEnumerator* *result);
NS_IMETHOD GetChildCards(nsISimpleEnumerator* *result);
NS_IMETHOD HasCard(nsIAbCard *cards, PRBool *hasCard);
NS_IMETHOD GetSupportsMailingLists(PRBool *aSupportsMailingsLists);
NS_IMETHOD GetIsRemote(PRBool *aIsRemote);
NS_IMETHOD GetIsSecure(PRBool *aIsRemote);
NS_IMETHOD GetSearchDuringLocalAutocomplete(PRBool *aSearchDuringLocalAutocomplete);
// nsAbLDAPDirectoryQuery methods
nsresult GetLDAPConnection (nsILDAPConnection** connection);
nsresult CreateCard (nsILDAPURL* uri, const char* dn, nsIAbCard** card);
// nsIAbDirectorySearch methods
NS_DECL_NSIABDIRECTORYSEARCH
// nsIAbDirectorySearch methods
NS_DECL_NSIABDIRECTORYSEARCH
// nsAbDirSearchListenerContext methods
nsresult OnSearchFinished(PRInt32 result);
nsresult OnSearchFoundCard(nsIAbCard* card);
// nsAbDirSearchListenerContext methods
nsresult OnSearchFinished (PRInt32 result);
nsresult OnSearchFoundCard (nsIAbCard* card);
NS_DECL_NSIABLDAPDIRECTORY
NS_DECL_NSIABLDAPDIRECTORY
protected:
nsresult Initiate();
nsresult InitiateConnection();
nsresult Initiate();
PRPackedBool mInitialized;
PRPackedBool mInitializedConnection;
PRPackedBool mPerformingQuery;
PRInt32 mContext;
PRInt32 mMaxHits;
nsCOMPtr<nsILDAPURL> mURL;
nsCOMPtr<nsILDAPConnection> mConnection;
PRPackedBool mPerformingQuery;
PRInt32 mContext;
PRInt32 mMaxHits;
nsCOMPtr<nsIAbBooleanExpression> mExpression;
nsSupportsHashtable mCache;
nsSupportsHashtable mCache;
PRLock* mLock;
PRLock* mLock;
nsCOMPtr<nsIMutableArray> mSearchServerControls;
nsCOMPtr<nsIMutableArray> mSearchClientControls;
nsCOMPtr<nsIMutableArray> mSearchServerControls;
nsCOMPtr<nsIMutableArray> mSearchClientControls;
};
#endif

View File

@@ -46,7 +46,7 @@
#include "nsILDAPOperation.h"
#include "nsIAbLDAPAttributeMap.h"
#include "nsAbUtils.h"
#include "nsAbBaseCID.h"
#include "nsString.h"
#include "nsAutoLock.h"
#include "nsIProxyObjectManager.h"
@@ -325,9 +325,7 @@ nsresult nsAbQueryLDAPMessageListener::OnLDAPMessageSearchEntry (nsILDAPMessage
rv = aMessage->GetDn (dn);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbCard> card;
rv = mDirectoryQuery->CreateCard(mSearchUrl, dn.get(),
getter_AddRefs(card));
nsCOMPtr<nsIAbCard> card = do_CreateInstance(NS_ABLDAPCARD_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = map->SetCardPropertiesFromLDAPMessage(aMessage, card);
@@ -414,7 +412,6 @@ nsresult nsAbQueryLDAPMessageListener::QueryResultStatus(nsISupportsArray* prope
NS_IMPL_THREADSAFE_ISUPPORTS1(nsAbLDAPDirectoryQuery, nsIAbDirectoryQuery)
nsAbLDAPDirectoryQuery::nsAbLDAPDirectoryQuery() :
mProtocolVersion (nsILDAPConnection::VERSION3),
mInitialized(PR_FALSE)
{
}
@@ -437,181 +434,238 @@ NS_IMETHODIMP nsAbLDAPDirectoryQuery::DoQuery(nsIAbDirectoryQueryArguments* argu
PRInt32 timeOut,
PRInt32* _retval)
{
PRBool alreadyInitialized = mInitialized;
mInitialized = PR_TRUE;
// Ensure existing query is stopped. Context id doesn't matter here
nsresult rv = StopQuery(0);
NS_ENSURE_SUCCESS(rv, rv);
mInitialized = PR_TRUE;
// Get the current directory as LDAP specific - we'll need this later
nsCOMPtr<nsIAbLDAPDirectory> directory(do_QueryInterface(this, &rv));
NS_ENSURE_SUCCESS(rv, rv);
// We also need the current URL to check as well...
nsCOMPtr<nsILDAPURL> currentUrl;
rv = directory->GetLDAPURL(getter_AddRefs(currentUrl));
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString login;
rv = directory->GetAuthDn(login);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 protocolVersion;
rv = directory->GetProtocolVersion(&protocolVersion);
NS_ENSURE_SUCCESS(rv, rv);
// To do:
// Ensure query is stopped
// If connection params have changed re-create connection
// else reuse existing connection
PRBool redoConnection = PR_FALSE;
if (!mConnection || !mDirectoryUrl)
{
mDirectoryUrl = currentUrl;
mCurrentLogin = login;
mCurrentProtocolVersion = protocolVersion;
redoConnection = PR_TRUE;
}
else
{
PRBool equal;
rv = mDirectoryUrl->Equals(currentUrl, &equal);
NS_ENSURE_SUCCESS(rv, rv);
nsXPIDLCString spec;
mDirectoryUrl->GetSpec(spec);
currentUrl->GetSpec(spec);
// Get the scope
nsCAutoString scope;
PRBool doSubDirectories;
nsresult rv = arguments->GetQuerySubDirectories (&doSubDirectories);
NS_ENSURE_SUCCESS(rv, rv);
scope = (doSubDirectories) ? "sub" : "one";
// Get the return attributes
nsCAutoString returnAttributes;
rv = getLdapReturnAttributes (arguments, returnAttributes);
NS_ENSURE_SUCCESS(rv, rv);
// Get the filter
nsCOMPtr<nsISupports> supportsExpression;
rv = arguments->GetExpression (getter_AddRefs (supportsExpression));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbBooleanExpression> expression (do_QueryInterface (supportsExpression, &rv));
nsCAutoString filter;
// figure out how we map attribute names to addressbook fields for this
// query
nsCOMPtr<nsISupports> iSupportsMap;
rv = arguments->GetTypeSpecificArg(getter_AddRefs(iSupportsMap));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbLDAPAttributeMap> map = do_QueryInterface(iSupportsMap, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = nsAbBoolExprToLDAPFilter::Convert (map, expression, filter);
NS_ENSURE_SUCCESS(rv, rv);
/*
* Mozilla itself cannot arrive here with a blank filter
* as the nsAbLDAPDirectory::StartSearch() disallows it.
* But 3rd party LDAP query integration with Mozilla begins
* in this method.
*
* Default the filter string if blank, otherwise it gets
* set to (objectclass=*) which returns everything. Set
* the default to (objectclass=inetorgperson) as this
* is the most appropriate default objectclass which is
* central to the makeup of the mozilla ldap address book
* entries.
*/
if(filter.IsEmpty())
if (!equal)
{
filter.AssignLiteral("(objectclass=inetorgperson)");
mDirectoryUrl = currentUrl;
mCurrentLogin = login;
mCurrentProtocolVersion = protocolVersion;
redoConnection = PR_TRUE;
}
// Set up the search ldap url
nsCOMPtr<nsIAbLDAPDirectory> directory(do_QueryInterface(this, &rv));
NS_ENSURE_SUCCESS(rv, rv);
rv = directory->GetLDAPURL(getter_AddRefs(mDirectoryUrl));
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString host;
rv = mDirectoryUrl->GetAsciiHost(host);
NS_ENSURE_SUCCESS(rv, rv);
PRInt32 port;
rv = mDirectoryUrl->GetPort(&port);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString dn;
rv = mDirectoryUrl->GetDn(dn);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 options;
rv = mDirectoryUrl->GetOptions(&options);
NS_ENSURE_SUCCESS(rv, rv);
// get the directoryFilter from the directory url and merge it with the user's
// search filter
nsCAutoString urlFilter;
rv = mDirectoryUrl->GetFilter(urlFilter);
// if urlFilter is unset (or set to the default "objectclass=*"), there's
// no need to AND in an empty search term, so leave prefix and suffix empty
nsCAutoString searchFilter;
if (urlFilter.Length() && !urlFilter.Equals(NS_LITERAL_CSTRING("(objectclass=*)")))
{
// if urlFilter isn't parenthesized, we need to add in parens so that
// the filter works as a term to &
//
if (urlFilter[0] != '(')
{
searchFilter = NS_LITERAL_CSTRING("(&(");
searchFilter += urlFilter;
searchFilter += NS_LITERAL_CSTRING(")");
}
else
{
searchFilter = NS_LITERAL_CSTRING("(&");
searchFilter += urlFilter;
}
searchFilter += filter;
searchFilter += ')';
}
else
searchFilter = filter;
nsCString ldapSearchUrlString;
char* _ldapSearchUrlString =
PR_smprintf ("ldap%s://%s:%d/%s?%s?%s?%s",
(options & nsILDAPURL::OPT_SECURE) ? "s" : "",
host.get (),
port,
dn.get (),
returnAttributes.get (),
scope.get (),
searchFilter.get ());
if (!_ldapSearchUrlString)
return NS_ERROR_OUT_OF_MEMORY;
ldapSearchUrlString = _ldapSearchUrlString;
PR_smprintf_free (_ldapSearchUrlString);
nsCOMPtr<nsILDAPURL> url;
url = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = url->SetSpec(ldapSearchUrlString);
NS_ENSURE_SUCCESS(rv, rv);
// Get the ldap connection
nsCOMPtr<nsILDAPConnection> ldapConnection;
rv = GetLDAPConnection (getter_AddRefs (ldapConnection));
NS_ENSURE_SUCCESS(rv, rv);
// too soon? Do we need a new listener?
if (alreadyInitialized)
{
nsAbQueryLDAPMessageListener *msgListener =
NS_STATIC_CAST(nsAbQueryLDAPMessageListener *,
NS_STATIC_CAST(nsILDAPMessageListener *, mListener.get()));
if (msgListener)
// Has login or version changed?
if (login != mCurrentLogin || protocolVersion != mCurrentProtocolVersion)
{
msgListener->mDirectoryUrl = mDirectoryUrl;
msgListener->mSearchUrl = url;
return msgListener->DoTask();
redoConnection = PR_TRUE;
mCurrentLogin = login;
mCurrentProtocolVersion = protocolVersion;
}
}
}
// Now formulate the search string
// Get the scope
nsCAutoString scope;
PRBool doSubDirectories;
rv = arguments->GetQuerySubDirectories (&doSubDirectories);
NS_ENSURE_SUCCESS(rv, rv);
scope = (doSubDirectories) ? "sub" : "one";
// Get the return attributes
nsCAutoString returnAttributes;
rv = getLdapReturnAttributes (arguments, returnAttributes);
NS_ENSURE_SUCCESS(rv, rv);
// Get the filter
nsCOMPtr<nsISupports> supportsExpression;
rv = arguments->GetExpression (getter_AddRefs (supportsExpression));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIAbBooleanExpression> expression (do_QueryInterface (supportsExpression, &rv));
nsCAutoString filter;
// figure out how we map attribute names to addressbook fields for this
// query
nsCOMPtr<nsISupports> iSupportsMap;
rv = arguments->GetTypeSpecificArg(getter_AddRefs(iSupportsMap));
NS_ENSURE_SUCCESS(rv, rv);
// Initiate LDAP message listener
nsAbQueryLDAPMessageListener* _messageListener =
new nsAbQueryLDAPMessageListener (
this,
mDirectoryUrl,
url,
ldapConnection,
arguments,
listener,
mLogin,
resultLimit,
timeOut);
if (_messageListener == NULL)
return NS_ERROR_OUT_OF_MEMORY;
mListener = _messageListener;
*_retval = 1;
nsCOMPtr<nsIAbLDAPAttributeMap> map = do_QueryInterface(iSupportsMap, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Now lets initialize the LDAP connection properly. We'll kick
// off the bind operation in the callback function, |OnLDAPInit()|.
rv = ldapConnection->Init(host.get(), port, options, mLogin,
mListener, nsnull, mProtocolVersion);
NS_ENSURE_SUCCESS(rv, rv);
rv = nsAbBoolExprToLDAPFilter::Convert(map, expression, filter);
NS_ENSURE_SUCCESS(rv, rv);
return rv;
/*
* Mozilla itself cannot arrive here with a blank filter
* as the nsAbLDAPDirectory::StartSearch() disallows it.
* But 3rd party LDAP query integration with Mozilla begins
* in this method.
*
* Default the filter string if blank, otherwise it gets
* set to (objectclass=*) which returns everything. Set
* the default to (objectclass=inetorgperson) as this
* is the most appropriate default objectclass which is
* central to the makeup of the mozilla ldap address book
* entries.
*/
if(filter.IsEmpty())
{
filter.AssignLiteral("(objectclass=inetorgperson)");
}
nsCAutoString host;
rv = mDirectoryUrl->GetAsciiHost(host);
NS_ENSURE_SUCCESS(rv, rv);
PRInt32 port;
rv = mDirectoryUrl->GetPort(&port);
NS_ENSURE_SUCCESS(rv, rv);
nsCAutoString dn;
rv = mDirectoryUrl->GetDn(dn);
NS_ENSURE_SUCCESS(rv, rv);
PRUint32 options;
rv = mDirectoryUrl->GetOptions(&options);
NS_ENSURE_SUCCESS(rv, rv);
// get the directoryFilter from the directory url and merge it with the user's
// search filter
nsCAutoString urlFilter;
rv = mDirectoryUrl->GetFilter(urlFilter);
// if urlFilter is unset (or set to the default "objectclass=*"), there's
// no need to AND in an empty search term, so leave prefix and suffix empty
nsCAutoString searchFilter;
if (urlFilter.Length() && !urlFilter.Equals(NS_LITERAL_CSTRING("(objectclass=*)")))
{
// if urlFilter isn't parenthesized, we need to add in parens so that
// the filter works as a term to &
//
if (urlFilter[0] != '(')
{
searchFilter = NS_LITERAL_CSTRING("(&(");
searchFilter.Append(urlFilter);
searchFilter.AppendLiteral(")");
}
else
{
searchFilter = NS_LITERAL_CSTRING("(&");
searchFilter.Append(urlFilter);
}
searchFilter += filter;
searchFilter += ')';
}
else
searchFilter = filter;
nsCString ldapSearchUrlString;
char* _ldapSearchUrlString =
PR_smprintf("ldap%s://%s:%d/%s?%s?%s?%s",
(options & nsILDAPURL::OPT_SECURE) ? "s" : "",
host.get(),
port,
dn.get(),
returnAttributes.get(),
scope.get(),
searchFilter.get());
if (!_ldapSearchUrlString)
return NS_ERROR_OUT_OF_MEMORY;
ldapSearchUrlString = _ldapSearchUrlString;
PR_smprintf_free(_ldapSearchUrlString);
nsCOMPtr<nsILDAPURL> url;
url = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
rv = url->SetSpec(ldapSearchUrlString);
NS_ENSURE_SUCCESS(rv, rv);
// too soon? Do we need a new listener?
// If we already have a connection, and don't need to re-do it, give it the
// new search details and go for it...
if (!redoConnection)
{
nsAbQueryLDAPMessageListener *msgListener =
NS_STATIC_CAST(nsAbQueryLDAPMessageListener *,
NS_STATIC_CAST(nsILDAPMessageListener *, mListener.get()));
if (msgListener)
{
// Ensure the urls are correct
msgListener->mDirectoryUrl = mDirectoryUrl;
msgListener->mSearchUrl = url;
// Also ensure we set the correct result limit
msgListener->mResultLimit = resultLimit;
return msgListener->DoTask();
}
}
// Create the new connection (which cause the old one to be dropped if necessary)
mConnection = do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
// Initiate LDAP message listener
nsAbQueryLDAPMessageListener* _messageListener =
new nsAbQueryLDAPMessageListener(this, mDirectoryUrl, url,
mConnection, arguments,
listener, mCurrentLogin,
resultLimit, timeOut);
if (_messageListener == NULL)
return NS_ERROR_OUT_OF_MEMORY;
mListener = _messageListener;
*_retval = 1;
// Now lets initialize the LDAP connection properly. We'll kick
// off the bind operation in the callback function, |OnLDAPInit()|.
rv = mConnection->Init(host.get(), port, options, mCurrentLogin,
mListener, nsnull, mCurrentProtocolVersion);
NS_ENSURE_SUCCESS(rv, rv);
return rv;
}
/* void stopQuery (in long contextID); */

View File

@@ -51,29 +51,24 @@
class nsAbLDAPDirectoryQuery : public nsIAbDirectoryQuery
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIABDIRECTORYQUERY
NS_DECL_ISUPPORTS
NS_DECL_NSIABDIRECTORYQUERY
nsAbLDAPDirectoryQuery();
virtual ~nsAbLDAPDirectoryQuery();
void setLdapUrl (const char* aldapUrl);
virtual nsresult GetLDAPConnection (nsILDAPConnection** connection) = 0;
virtual nsresult CreateCard (nsILDAPURL* url, const char* dn, nsIAbCard** card) = 0;
nsAbLDAPDirectoryQuery();
virtual ~nsAbLDAPDirectoryQuery();
protected:
nsresult getLdapReturnAttributes (
nsIAbDirectoryQueryArguments* arguments,
nsCString& returnAttributes);
nsCString mLogin; // authenticate to the LDAP server as...
nsCOMPtr<nsILDAPURL> mDirectoryUrl; // the URL for the server
PRUint32 mProtocolVersion; // version of LDAP (see nsILDAPConnection.idl)
nsCOMPtr <nsILDAPMessageListener> mListener;
nsresult getLdapReturnAttributes(nsIAbDirectoryQueryArguments* arguments,
nsCString& returnAttributes);
nsCOMPtr<nsILDAPMessageListener> mListener;
private:
PRBool mInitialized;
nsCOMPtr<nsILDAPConnection> mConnection;
nsCOMPtr<nsILDAPURL> mDirectoryUrl;
nsCString mCurrentLogin;
PRUint32 mCurrentProtocolVersion;
PRBool mInitialized;
};
#endif // nsAbLDAPDirectoryQuery_h__