Back to index

lightning-sunbird  0.9+nobinonly
nsAbLDAPReplicationQuery.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  * Rajiv Dayal <rdayal@netscape.com>
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Dan Mosedale <dmose@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 
00040 #include "nsCOMPtr.h"
00041 #include "nsAbLDAPReplicationQuery.h"
00042 #include "nsAbLDAPReplicationService.h"
00043 #include "nsAbLDAPReplicationData.h"
00044 #include "nsILDAPURL.h"
00045 #include "nsAbBaseCID.h"
00046 #include "nsProxiedService.h"
00047 #include "nsLDAP.h"
00048 #include "nsAbUtils.h"
00049 #include "nsDirPrefs.h"
00050 
00051 
00052 NS_IMPL_ISUPPORTS1(nsAbLDAPReplicationQuery, nsIAbLDAPReplicationQuery)
00053 
00054 nsAbLDAPReplicationQuery::nsAbLDAPReplicationQuery()
00055     :  mInitialized(PR_FALSE),
00056        mDirServer(nsnull)
00057 {
00058 }
00059 
00060 nsAbLDAPReplicationQuery::~nsAbLDAPReplicationQuery()
00061 {
00062     DIR_DeleteServer(mDirServer);
00063 }
00064 
00065 nsresult nsAbLDAPReplicationQuery::InitLDAPData()
00066 {
00067     mDirServer = (DIR_Server *) PR_Calloc(1, sizeof(DIR_Server));
00068     if (!mDirServer) return NS_ERROR_NULL_POINTER;
00069 
00070     DIR_InitServerWithType(mDirServer, LDAPDirectory);
00071     // since DeleteServer frees the prefName make a copy of prefName string
00072     mDirServer->prefName = nsCRT::strdup(mDirPrefName.get());
00073     DIR_GetPrefsForOneServer(mDirServer, PR_FALSE, PR_FALSE);
00074 
00075     // this is done here to take care of the problem related to bug # 99124.
00076     // earlier versions of Mozilla could have the fileName associated with the directory
00077     // to be abook.mab which is the profile's personal addressbook. If the pref points to
00078     // it, calls nsDirPrefs to generate a new server filename.
00079     if (!nsCRT::strcasecmp(mDirServer->fileName,kPersonalAddressbook) 
00080         || !mDirServer->fileName || !(*mDirServer->fileName)) {
00081         // initialize mDirServer->filename is null or else DIR_SetServerFileName doesnot work
00082         // and no point in passing the 2nd param (leafName) to it as it doesnot use that.
00083         PR_FREEIF(mDirServer->fileName);
00084         mDirServer->fileName=nsnull;
00085         DIR_SetServerFileName (mDirServer, nsnull);
00086     }
00087     // use the dir server filename for replication
00088     PR_FREEIF(mDirServer->replInfo->fileName);
00089     // since DeleteServer frees the replInfo->fileName make a copy of string
00090     // if we donot do this the DeleteServer functions crashes.
00091     mDirServer->replInfo->fileName = nsCRT::strdup(mDirServer->fileName);
00092 
00093     nsresult rv = NS_OK;
00094 
00095     mURL = do_CreateInstance(NS_LDAPURL_CONTRACTID, &rv);
00096     if (NS_FAILED(rv)) 
00097         return rv;
00098 
00099     rv = mURL->SetSpec(nsDependentCString(mDirServer->uri));
00100     if (NS_FAILED(rv)) 
00101         return rv;
00102 
00103     mConnection = do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID, &rv);
00104     if (NS_FAILED(rv)) 
00105         return rv;
00106 
00107     mOperation = do_CreateInstance(NS_LDAPOPERATION_CONTRACTID, &rv);
00108 
00109     return rv;
00110 }
00111 
00112 nsresult nsAbLDAPReplicationQuery::CreateNewLDAPOperation()
00113 {
00114   nsresult rv;
00115   nsCOMPtr <nsILDAPMessageListener> oldListener;
00116   mOperation->GetMessageListener(getter_AddRefs(oldListener));
00117   // release old and create a new instance
00118   mOperation = do_CreateInstance(NS_LDAPOPERATION_CONTRACTID, &rv);
00119   NS_ENSURE_SUCCESS(rv, rv);
00120   return mOperation->Init(mConnection, oldListener, nsnull);
00121 }
00122 
00123 
00124 NS_IMETHODIMP nsAbLDAPReplicationQuery::ConnectToLDAPServer(nsILDAPURL *aURL, const nsACString & aAuthDN)
00125 {
00126     NS_ENSURE_ARG_POINTER(aURL);
00127     if (!mInitialized) 
00128         return NS_ERROR_NOT_INITIALIZED;
00129 
00130     nsCAutoString host;
00131     nsresult rv = aURL->GetHost(host);
00132     if (NS_FAILED(rv)) 
00133         return rv;
00134     if (host.IsEmpty())
00135         return NS_ERROR_UNEXPECTED;
00136 
00137     PRInt32 port;
00138     rv = aURL->GetPort(&port);
00139     if (NS_FAILED(rv)) 
00140         return rv;
00141     if (!port)
00142         return NS_ERROR_UNEXPECTED;
00143 
00144     PRUint32 options;
00145     rv = aURL->GetOptions(&options);
00146     if (NS_FAILED(rv))
00147       return NS_ERROR_UNEXPECTED;
00148 
00149     // Initiate LDAP message listener to the current thread
00150     nsCOMPtr<nsILDAPMessageListener> listener;
00151     rv = NS_GetProxyForObject(NS_CURRENT_EVENTQ,
00152                   NS_GET_IID(nsILDAPMessageListener), 
00153                   NS_STATIC_CAST(nsILDAPMessageListener*, mDataProcessor),
00154                   PROXY_SYNC | PROXY_ALWAYS, 
00155                   getter_AddRefs(listener));
00156     if (!listener) 
00157         return NS_ERROR_FAILURE;
00158 
00159     // this could be a rebind call
00160     PRInt32 replicationState = nsIAbLDAPProcessReplicationData::kIdle;
00161     rv = mDataProcessor->GetReplicationState(&replicationState);
00162     if(NS_FAILED(rv)) 
00163         return rv;
00164     if((replicationState != nsIAbLDAPProcessReplicationData::kIdle))
00165     {
00166         // release old and create a new instance
00167         mConnection = do_CreateInstance(NS_LDAPCONNECTION_CONTRACTID, &rv);
00168         if(NS_FAILED(rv)) 
00169             return rv;
00170 
00171         // release old and create a new instance
00172         mOperation = do_CreateInstance(NS_LDAPOPERATION_CONTRACTID, &rv);
00173         if(NS_FAILED(rv)) 
00174             return rv;
00175     }
00176 
00177     PRUint32 protocolVersion;
00178     if (DIR_TestFlag(mDirServer, DIR_LDAP_VERSION3)) {
00179         protocolVersion = nsILDAPConnection::VERSION3;
00180     } else {
00181         protocolVersion = nsILDAPConnection::VERSION2;
00182     }
00183 
00184     // initialize the LDAP connection
00185     return mConnection->Init(host.get(), port, 
00186                              (options & nsILDAPURL::OPT_SECURE) ? PR_TRUE : PR_FALSE,
00187                              aAuthDN, listener, nsnull, protocolVersion);
00188 }
00189 
00190 NS_IMETHODIMP nsAbLDAPReplicationQuery::Init(const nsACString & aPrefName, nsIWebProgressListener *aProgressListener)
00191 {
00192     if (aPrefName.IsEmpty())
00193         return NS_ERROR_UNEXPECTED;
00194 
00195     mDirPrefName = aPrefName;
00196 
00197     nsresult rv = InitLDAPData();
00198     if (NS_FAILED(rv)) 
00199         return rv;
00200 
00201     mDataProcessor =  do_CreateInstance(NS_ABLDAP_PROCESSREPLICATIONDATA_CONTRACTID, &rv);
00202     if (NS_FAILED(rv)) 
00203         return rv;
00204 
00205     // 'this' initialized
00206     mInitialized = PR_TRUE;
00207 
00208     return mDataProcessor->Init(this, aProgressListener);
00209 }
00210 
00211 NS_IMETHODIMP nsAbLDAPReplicationQuery::DoReplicationQuery()
00212 {
00213     return ConnectToLDAPServer(mURL, EmptyCString());
00214 }
00215 
00216 NS_IMETHODIMP nsAbLDAPReplicationQuery::QueryAllEntries()
00217 {
00218     if (!mInitialized) 
00219         return NS_ERROR_NOT_INITIALIZED;
00220 
00221     // get the search filter associated with the directory server url; 
00222     //
00223     nsCAutoString urlFilter;
00224     nsresult rv = mURL->GetFilter(urlFilter);
00225     if (NS_FAILED(rv)) 
00226         return rv;
00227 
00228     nsCAutoString dn;
00229     rv = mURL->GetDn(dn);
00230     if (NS_FAILED(rv)) 
00231         return rv;
00232     if (dn.IsEmpty())
00233         return NS_ERROR_UNEXPECTED;
00234 
00235     PRInt32 scope;
00236     rv = mURL->GetScope(&scope);
00237     if (NS_FAILED(rv)) 
00238         return rv;
00239 
00240     CharPtrArrayGuard attributes;
00241     rv = mURL->GetAttributes(attributes.GetSizeAddr(), attributes.GetArrayAddr());
00242     if (NS_FAILED(rv)) 
00243         return rv;
00244 
00245     rv = CreateNewLDAPOperation();
00246     NS_ENSURE_SUCCESS(rv, rv);
00247     return mOperation->SearchExt(dn, scope, urlFilter, 
00248                                attributes.GetSize(), attributes.GetArray(),
00249                                0, 0);
00250 }
00251 
00252 NS_IMETHODIMP nsAbLDAPReplicationQuery::CancelQuery()
00253 {
00254     if (!mInitialized) 
00255         return NS_ERROR_NOT_INITIALIZED;
00256 
00257     return mDataProcessor->Abort();
00258 }
00259 
00260 NS_IMETHODIMP nsAbLDAPReplicationQuery::Done(PRBool aSuccess)
00261 {
00262    if (!mInitialized) 
00263        return NS_ERROR_NOT_INITIALIZED;
00264    
00265    nsresult rv = NS_OK;
00266    nsCOMPtr<nsIAbLDAPReplicationService> replicationService = 
00267                             do_GetService(NS_ABLDAP_REPLICATIONSERVICE_CONTRACTID, &rv);
00268    if (NS_SUCCEEDED(rv))
00269       replicationService->Done(aSuccess);
00270 
00271    if (aSuccess)
00272        DIR_SavePrefsForOneServer(mDirServer);
00273 
00274    return rv;
00275 }
00276 
00277 
00278 NS_IMETHODIMP nsAbLDAPReplicationQuery::GetOperation(nsILDAPOperation * *aOperation)
00279 {
00280    NS_ENSURE_ARG_POINTER(aOperation);
00281    if (!mInitialized) 
00282        return NS_ERROR_NOT_INITIALIZED;
00283    
00284    NS_IF_ADDREF(*aOperation = mOperation);
00285 
00286    return NS_OK;
00287 }
00288 
00289 NS_IMETHODIMP nsAbLDAPReplicationQuery::GetConnection(nsILDAPConnection * *aConnection)
00290 {
00291    NS_ENSURE_ARG_POINTER(aConnection);
00292    if (!mInitialized) 
00293        return NS_ERROR_NOT_INITIALIZED;
00294 
00295    NS_IF_ADDREF(*aConnection = mConnection); 
00296 
00297    return NS_OK;
00298 }
00299 
00300 NS_IMETHODIMP nsAbLDAPReplicationQuery::GetReplicationURL(nsILDAPURL * *aReplicationURL)
00301 {
00302    NS_ENSURE_ARG_POINTER(aReplicationURL);
00303    if (!mInitialized) 
00304        return NS_ERROR_NOT_INITIALIZED;
00305 
00306    NS_IF_ADDREF(*aReplicationURL = mURL); 
00307 
00308    return NS_OK;
00309 }
00310 
00311 NS_IMETHODIMP nsAbLDAPReplicationQuery::GetReplicationServerInfo(DIR_Server * *aReplicationServerInfo)
00312 {
00313    NS_ENSURE_ARG_POINTER(aReplicationServerInfo);
00314    if (!mInitialized) 
00315        return NS_ERROR_NOT_INITIALIZED;
00316 
00317    *aReplicationServerInfo = mDirServer;
00318 
00319    return NS_OK;
00320 }
00321