Back to index

lightning-sunbird  0.9+nobinonly
nsAbLDAPDirectory.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Paul Sandoz <paul.sandoz@sun.com> Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Seth Spitzer <sspitzer@netscape.com>
00024  *   Dan Mosedale <dmose@netscape.com>
00025  *   Paul Sandoz <paul.sandoz@sun.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either of the GNU General Public License Version 2 or later (the "GPL"),
00029  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #include "nsAbLDAPDirectory.h"
00042 
00043 #include "nsAbQueryStringToExpression.h"
00044 
00045 #include "nsAbBaseCID.h"
00046 #include "nsIAddrBookSession.h"
00047 #include "nsIRDFService.h"
00048 
00049 #include "nsString.h"
00050 #include "nsXPIDLString.h"
00051 #include "nsAutoLock.h"
00052 #include "nsNetCID.h"
00053 #include "nsIIOService.h"
00054 #include "nsXPCOM.h"
00055 #include "nsISupportsPrimitives.h"
00056 #include "nsIPrefService.h"
00057 #include "nsIPrefBranch.h"
00058 #include "nsCOMArray.h"
00059 #include "nsArrayEnumerator.h"
00060 #include "nsLDAP.h"
00061 #include "nsIAbLDAPAttributeMap.h"
00062 
00063 nsAbLDAPDirectory::nsAbLDAPDirectory() :
00064     nsAbDirectoryRDFResource(),
00065     nsAbLDAPDirectoryQuery (),
00066     mInitialized(PR_FALSE),
00067     mInitializedConnection(PR_FALSE),
00068     mPerformingQuery(PR_FALSE),
00069     mContext(0),
00070     mLock(0)
00071 {
00072 }
00073 
00074 nsAbLDAPDirectory::~nsAbLDAPDirectory()
00075 {
00076     if (mLock)
00077         PR_DestroyLock (mLock);
00078 }
00079 
00080 NS_IMPL_ISUPPORTS_INHERITED4(nsAbLDAPDirectory, nsAbDirectoryRDFResource, nsIAbDirectory, nsIAbDirectoryQuery, nsIAbDirectorySearch, nsIAbLDAPDirectory)
00081 
00082 NS_IMETHODIMP nsAbLDAPDirectory::Init(const char* aURI)
00083 {
00084   mInitialized = PR_FALSE;
00085   return nsAbDirectoryRDFResource::Init(aURI);
00086 }
00087 
00088 nsresult nsAbLDAPDirectory::Initiate ()
00089 {
00090     if (!mIsQueryURI)
00091         return NS_ERROR_FAILURE;
00092 
00093     if (mInitialized)
00094         return NS_OK;
00095 
00096     nsresult rv;
00097 
00098     mLock = PR_NewLock ();
00099     if(!mLock)
00100     {
00101         return NS_ERROR_OUT_OF_MEMORY;
00102     }
00103 
00104     rv = nsAbQueryStringToExpression::Convert (mQueryString.get (),
00105         getter_AddRefs(mExpression));
00106     NS_ENSURE_SUCCESS(rv, rv);
00107 
00108     rv = InitiateConnection ();
00109 
00110     mInitialized = PR_TRUE;
00111     return rv;
00112 }
00113 
00114 nsresult nsAbLDAPDirectory::InitiateConnection ()
00115 {
00116     if (mInitializedConnection)
00117         return NS_OK;
00118 
00119     nsresult rv;
00120 
00121     mURL = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
00122     NS_ENSURE_SUCCESS(rv, rv);
00123 
00124     nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00125     NS_ENSURE_SUCCESS(rv, rv);
00126 
00127     // use mURINoQuery to get a prefName
00128     nsCAutoString prefName;
00129     prefName = nsDependentCString(mURINoQuery.get() + kLDAPDirectoryRootLen) + NS_LITERAL_CSTRING(".uri");
00130 
00131     // turn moz-abldapdirectory://ldap_2.servers.nscpphonebook into -> "ldap_2.servers.nscpphonebook.uri"
00132     nsXPIDLCString URI;
00133     rv = prefs->GetCharPref(prefName.get(), getter_Copies(URI));
00134     if (NS_FAILED(rv))
00135     {
00136         /*
00137          * A recent change in Mozilla now means that the LDAP Address Book
00138          * RDF Resource URI is based on the unique preference name value i.e.  
00139          * [moz-abldapdirectory://prefName]
00140          * Prior to this valid change it was based on the actual uri i.e. 
00141          * [moz-abldapdirectory://host:port/basedn]
00142          * Basing the resource on the prefName allows these attributes to 
00143          * change. 
00144          *
00145          * But the uri value was also the means by which third-party
00146          * products could integrate with Mozilla's LDAP Address Books
00147          * without necessarily having an entry in the preferences file
00148          * or more importantly needing to be able to change the
00149          * preferences entries. Thus to set the URI Spec now, it is
00150          * only necessary to read the uri pref entry, while in the
00151          * case where it is not a preference, we need to replace the
00152          * "moz-abldapdirectory".
00153          */
00154         nsCAutoString tempLDAPURL(mURINoQuery);
00155         tempLDAPURL.ReplaceSubstring("moz-abldapdirectory:", "ldap:");
00156         rv = mURL->SetSpec(tempLDAPURL);
00157     }
00158     else
00159     {
00160         rv = mURL->SetSpec(URI);
00161     }
00162     NS_ENSURE_SUCCESS(rv,rv);
00163 
00164     // get the login information, if there is any 
00165     //
00166     rv = prefs->GetCharPref(
00167         PromiseFlatCString(
00168             Substring(mURINoQuery, kLDAPDirectoryRootLen,
00169                       mURINoQuery.Length() - kLDAPDirectoryRootLen)
00170             + NS_LITERAL_CSTRING(".auth.dn")).get(),
00171         getter_Copies(mLogin));
00172     if (NS_FAILED(rv)) {
00173         mLogin.Truncate();  // zero out mLogin
00174     }
00175 
00176     // get the protocol version, if there is any.  using a string pref
00177     // here instead of an int, as protocol versions sometimes have names like
00178     // "4bis".
00179     //
00180     nsXPIDLCString protocolVersion;
00181     rv = prefs->GetCharPref(
00182         PromiseFlatCString(
00183             Substring(mURINoQuery, kLDAPDirectoryRootLen,
00184                       mURINoQuery.Length() - kLDAPDirectoryRootLen)
00185             + NS_LITERAL_CSTRING(".protocolVersion")).get(),
00186         getter_Copies(protocolVersion));
00187 
00188     if (NS_SUCCEEDED(rv) && protocolVersion.Equals("2")) {
00189         mProtocolVersion = nsILDAPConnection::VERSION2;
00190     }
00191     // otherwise we leave mProtocolVersion as the default (see the initializers
00192     // for the nsAbLDAPDirectoryQuery class).
00193 
00194     mConnection = do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID, &rv);
00195     NS_ENSURE_SUCCESS(rv, rv);
00196 
00197     mInitializedConnection = PR_TRUE;
00198     return rv;
00199 }
00200 
00201 
00202 /* 
00203  *
00204  * nsIAbDirectory methods
00205  *
00206  */
00207 
00208 NS_IMETHODIMP nsAbLDAPDirectory::GetOperations(PRInt32 *aOperations)
00209 {
00210     *aOperations = nsIAbDirectory::opSearch;
00211     return NS_OK;
00212 }
00213 
00214 NS_IMETHODIMP nsAbLDAPDirectory::GetChildNodes(nsISimpleEnumerator* *aResult)
00215 {
00216     nsCOMArray<nsIAbDirectory> children;
00217     return NS_NewArrayEnumerator(aResult, children);
00218 }
00219 
00220 NS_IMETHODIMP nsAbLDAPDirectory::GetChildCards(nsIEnumerator** result)
00221 {
00222     nsresult rv;
00223     
00224     // when offline, we need to get the child cards for the local, replicated mdb directory 
00225     PRBool offline;
00226     nsCOMPtr <nsIIOService> ioService = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
00227     NS_ENSURE_SUCCESS(rv,rv);
00228     rv = ioService->GetOffline(&offline);
00229     NS_ENSURE_SUCCESS(rv,rv);
00230     
00231     if (offline) {
00232       nsCOMPtr <nsIRDFService> rdfService = do_GetService("@mozilla.org/rdf/rdf-service;1",&rv);
00233       NS_ENSURE_SUCCESS(rv, rv);
00234 
00235       nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00236       NS_ENSURE_SUCCESS(rv, rv);
00237 
00238       // use mURINoQuery to get a prefName
00239       nsCAutoString prefName;
00240       prefName = nsDependentCString(mURINoQuery.get() + kLDAPDirectoryRootLen) + NS_LITERAL_CSTRING(".filename");
00241 
00242       nsXPIDLCString fileName;
00243       rv = prefs->GetCharPref(prefName.get(), getter_Copies(fileName));
00244       NS_ENSURE_SUCCESS(rv,rv);
00245       
00246       // if there is no fileName, bail out now.
00247       if (fileName.IsEmpty())
00248         return NS_OK;
00249 
00250       // perform the same query, but on the local directory
00251       nsCAutoString localDirectoryURI;
00252       localDirectoryURI = NS_LITERAL_CSTRING("moz-abmdbdirectory://") + fileName;
00253       if (mIsQueryURI)
00254         localDirectoryURI = localDirectoryURI + NS_LITERAL_CSTRING("?") + mQueryString;
00255       
00256       nsCOMPtr <nsIRDFResource> resource;
00257       rv = rdfService->GetResource(localDirectoryURI, getter_AddRefs(resource));
00258       NS_ENSURE_SUCCESS(rv, rv);
00259       
00260       nsCOMPtr <nsIAbDirectory> directory = do_QueryInterface(resource, &rv);
00261       NS_ENSURE_SUCCESS(rv, rv);
00262 
00263       rv = directory->GetChildCards(result);
00264     }
00265     else {
00266       // Start the search
00267       rv = StartSearch();
00268       NS_ENSURE_SUCCESS(rv, rv);
00269       
00270       nsCOMPtr<nsISupportsArray> array;
00271       NS_NewISupportsArray(getter_AddRefs(array));
00272       if (!array)
00273         return NS_ERROR_OUT_OF_MEMORY;
00274       
00275       rv = array->Enumerate(result);
00276     }
00277 
00278     NS_ENSURE_SUCCESS(rv,rv);
00279     return rv;
00280 }
00281 
00282 NS_IMETHODIMP nsAbLDAPDirectory::HasCard(nsIAbCard* card, PRBool* hasCard)
00283 {
00284     nsresult rv;
00285 
00286     rv = Initiate ();
00287     NS_ENSURE_SUCCESS(rv, rv);
00288 
00289     nsVoidKey key (NS_STATIC_CAST(void* ,card));
00290 
00291     // Enter lock
00292     nsAutoLock lock (mLock);
00293 
00294     *hasCard = mCache.Exists (&key);
00295     if (!*hasCard && mPerformingQuery)
00296             return NS_ERROR_NOT_AVAILABLE;
00297 
00298     return NS_OK;
00299 }
00300 
00301 /* 
00302  *
00303  * nsAbLDAPDirectoryQuery methods
00304  *
00305  */
00306 
00307 nsresult nsAbLDAPDirectory::GetLDAPConnection (nsILDAPConnection** connection)
00308 {
00309     nsresult rv;
00310 
00311     rv = InitiateConnection ();
00312     NS_ENSURE_SUCCESS(rv, rv);
00313 
00314     NS_IF_ADDREF(*connection = mConnection);
00315     return rv;
00316 }
00317 
00318 nsresult nsAbLDAPDirectory::GetLDAPURL (nsILDAPURL** url)
00319 {
00320     nsresult rv;
00321 
00322     rv = InitiateConnection ();
00323     NS_ENSURE_SUCCESS(rv, rv);
00324 
00325     NS_IF_ADDREF(*url = mURL);
00326     return rv;
00327 }
00328 
00329 nsresult nsAbLDAPDirectory::CreateCard (nsILDAPURL* uri, const char* dn, nsIAbCard** result)
00330 {
00331     nsresult rv;
00332 
00333     nsCOMPtr <nsIAbCard> card = do_CreateInstance(NS_ABLDAPCARD_CONTRACTID, &rv);
00334     NS_ENSURE_SUCCESS(rv, rv);
00335 
00336     NS_IF_ADDREF(*result = card);
00337     return NS_OK;
00338 }
00339 
00340 /* 
00341  *
00342  * nsIAbDirectorySearch methods
00343  *
00344  */
00345 
00346 NS_IMETHODIMP nsAbLDAPDirectory::StartSearch ()
00347 {
00348     nsresult rv;
00349     
00350     if (!mIsQueryURI || mQueryString.IsEmpty())
00351         return NS_OK;
00352 
00353     rv = Initiate ();
00354     NS_ENSURE_SUCCESS(rv, rv);
00355 
00356     rv = StopSearch ();
00357     NS_ENSURE_SUCCESS(rv, rv);
00358 
00359     nsCOMPtr<nsIAbDirectoryQueryArguments> arguments = do_CreateInstance(NS_ABDIRECTORYQUERYARGUMENTS_CONTRACTID,&rv);
00360     NS_ENSURE_SUCCESS(rv, rv);
00361 
00362     rv = arguments->SetExpression (mExpression);
00363     NS_ENSURE_SUCCESS(rv, rv);
00364 
00365     // Set the return properties to
00366     // return nsIAbCard interfaces
00367     const char *arr = "card:nsIAbCard";
00368     rv = arguments->SetReturnProperties (1, &arr);
00369     NS_ENSURE_SUCCESS(rv, rv);
00370 
00371     rv = arguments->SetQuerySubDirectories (PR_TRUE);
00372     NS_ENSURE_SUCCESS(rv, rv);
00373 
00374     // Set the the query listener
00375     nsCOMPtr<nsIAbDirectoryQueryResultListener> queryListener;
00376     nsAbDirSearchListener* _queryListener =
00377         new nsAbDirSearchListener (this);
00378     queryListener = _queryListener;
00379 
00380     nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00381     NS_ENSURE_SUCCESS(rv, rv);
00382 
00383     // use mURINoQuery to get a prefName
00384     nsCAutoString prefName;
00385     prefName = nsDependentCString(mURINoQuery.get() + kLDAPDirectoryRootLen) + NS_LITERAL_CSTRING(".maxHits");
00386 
00387     // turn moz-abldapdirectory://ldap_2.servers.nscpphonebook into -> "ldap_2.servers.nscpphonebook.maxHits"
00388     PRInt32 maxHits;
00389     rv = prefs->GetIntPref(prefName.get(), &maxHits);
00390     if (NS_FAILED(rv))
00391       maxHits = 100;
00392 
00393     // get the appropriate ldap attribute map, and pass it in via the
00394     // TypeSpecificArgument
00395     nsCOMPtr<nsIAbLDAPAttributeMapService> mapSvc = 
00396         do_GetService("@mozilla.org/addressbook/ldap-attribute-map-service;1", &rv);
00397     NS_ENSURE_SUCCESS(rv, rv);
00398 
00399     nsCOMPtr<nsIAbLDAPAttributeMap> attrMap;
00400     rv = mapSvc->GetMapForPrefBranch(m_DirPrefId, getter_AddRefs(attrMap));
00401     NS_ENSURE_SUCCESS(rv, rv);
00402 
00403     nsCOMPtr<nsISupports> typeSpecificArg = do_QueryInterface(attrMap, &rv);
00404     NS_ENSURE_SUCCESS(rv, rv);
00405 
00406     rv = arguments->SetTypeSpecificArg(attrMap);
00407     NS_ENSURE_SUCCESS(rv, rv);
00408 
00409 
00410     // Perform the query
00411     rv = DoQuery(arguments, queryListener, maxHits, 0, &mContext);
00412     NS_ENSURE_SUCCESS(rv, rv);
00413 
00414     // Enter lock
00415     nsAutoLock lock (mLock);
00416     mPerformingQuery = PR_TRUE;
00417     mCache.Reset ();
00418 
00419     return rv;
00420 }  
00421 
00422 NS_IMETHODIMP nsAbLDAPDirectory::StopSearch ()
00423 {
00424     nsresult rv;
00425 
00426     rv = Initiate ();
00427     NS_ENSURE_SUCCESS(rv, rv);
00428 
00429     // Enter lock
00430     {
00431         nsAutoLock lockGuard (mLock);
00432         if (!mPerformingQuery)
00433             return NS_OK;
00434         mPerformingQuery = PR_FALSE;
00435     }
00436     // Exit lock
00437 
00438     rv = StopQuery (mContext);
00439 
00440     return rv;
00441 }
00442 
00443 
00444 /* 
00445  *
00446  * nsAbDirSearchListenerContext methods
00447  *
00448  */
00449 nsresult nsAbLDAPDirectory::OnSearchFinished (PRInt32 result)
00450 {
00451     nsresult rv;
00452 
00453     rv = Initiate ();
00454     NS_ENSURE_SUCCESS(rv, rv);
00455 
00456     nsAutoLock lock (mLock);
00457     mPerformingQuery = PR_FALSE;
00458 
00459     return NS_OK;
00460 }
00461 
00462 nsresult nsAbLDAPDirectory::OnSearchFoundCard (nsIAbCard* card)
00463 {
00464     nsresult rv;
00465 
00466     rv = Initiate ();
00467     NS_ENSURE_SUCCESS(rv, rv);
00468 
00469     nsVoidKey key (NS_STATIC_CAST(void* ,card));
00470     // Enter lock
00471     {
00472         nsAutoLock lock (mLock);
00473         mCache.Put (&key, card);
00474     }
00475     // Exit lock
00476 
00477     nsCOMPtr<nsIAddrBookSession> abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv);
00478     if(NS_SUCCEEDED(rv))
00479         abSession->NotifyDirectoryItemAdded(this, card);
00480 
00481     return NS_OK;
00482 }
00483 
00484 NS_IMETHODIMP nsAbLDAPDirectory::GetSupportsMailingLists(PRBool *aSupportsMailingsLists)
00485 {
00486   NS_ENSURE_ARG_POINTER(aSupportsMailingsLists);
00487   *aSupportsMailingsLists = PR_FALSE;
00488   return NS_OK;
00489 }
00490 
00491 NS_IMETHODIMP nsAbLDAPDirectory::GetIsRemote(PRBool *aIsRemote)
00492 {
00493   NS_ENSURE_ARG_POINTER(aIsRemote);
00494   *aIsRemote = PR_TRUE;
00495   return NS_OK;
00496 }
00497 
00498 NS_IMETHODIMP nsAbLDAPDirectory::GetIsSecure(PRBool *aIsSecure)
00499 {
00500   NS_ENSURE_ARG_POINTER(aIsSecure);
00501 
00502   nsresult rv;
00503   nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
00504   NS_ENSURE_SUCCESS(rv, rv);
00505   
00506   // use mURINoQuery to get a prefName
00507   nsCAutoString prefName;
00508   prefName = nsDependentCString(mURINoQuery.get() + kLDAPDirectoryRootLen) + NS_LITERAL_CSTRING(".uri");
00509   
00510   nsXPIDLCString URI;
00511   rv = prefBranch->GetCharPref(prefName.get(), getter_Copies(URI));
00512   NS_ENSURE_SUCCESS(rv,rv);
00513   
00514   // to determine if this is a secure directory, check if the uri is ldaps:// or not
00515   *aIsSecure = (strncmp(URI.get(), "ldaps:", 6) == 0);
00516   return NS_OK;
00517 }
00518 
00519 NS_IMETHODIMP nsAbLDAPDirectory::GetSearchDuringLocalAutocomplete(PRBool *aSearchDuringLocalAutocomplete)
00520 {
00521   NS_ENSURE_ARG_POINTER(aSearchDuringLocalAutocomplete);
00522 
00523   // always skip LDAP directories when doing local autocomplete.
00524   // we do the LDAP autocompleting
00525   // in nsLDAPAutoCompleteSession
00526   *aSearchDuringLocalAutocomplete = PR_FALSE;
00527   return NS_OK;
00528 }
00529 
00530 NS_IMETHODIMP
00531 nsAbLDAPDirectory::GetSearchClientControls(nsIMutableArray **aControls)
00532 {
00533     NS_IF_ADDREF(*aControls = mSearchClientControls);
00534     return NS_OK;
00535 }
00536 
00537 NS_IMETHODIMP
00538 nsAbLDAPDirectory::SetSearchClientControls(nsIMutableArray *aControls)
00539 {
00540     mSearchClientControls = aControls;
00541     return NS_OK;
00542 }
00543 
00544 NS_IMETHODIMP
00545 nsAbLDAPDirectory::GetSearchServerControls(nsIMutableArray **aControls)
00546 {
00547     NS_IF_ADDREF(*aControls = mSearchServerControls);
00548     return NS_OK;
00549 }
00550 
00551 NS_IMETHODIMP
00552 nsAbLDAPDirectory::SetSearchServerControls(nsIMutableArray *aControls)
00553 {
00554     mSearchServerControls = aControls;
00555     return NS_OK;
00556 }