Back to index

lightning-sunbird  0.9+nobinonly
nsAbLDAPChangeLogQuery.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2002
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Rajiv Dayal <rdayal@netscape.com>
00025  *   David Bienvenu <bienvenu@nventure.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 
00042 #include "nsCOMPtr.h"
00043 #include "nsAbLDAPChangeLogQuery.h"
00044 #include "nsAbLDAPReplicationService.h"
00045 #include "nsAbLDAPChangeLogData.h"
00046 #include "nsAutoLock.h"
00047 #include "nsAbUtils.h"
00048 #include "prprf.h"
00049 #include "nsDirPrefs.h"
00050 #include "nsAbBaseCID.h"
00051 #include "nsPrintfCString.h"
00052 
00053 
00054 // The tables below were originally in nsAbLDAPProperties.cpp, which has since
00055 // gone away.
00056 static const char * sChangeLogRootDSEAttribs[] =
00057 { 
00058   "changelog", 
00059   "firstChangeNumber", 
00060   "lastChangeNumber",
00061   "dataVersion"
00062 };
00063 static const char * sChangeLogEntryAttribs[] =
00064 { 
00065   "targetdn", 
00066   "changetype"
00067 };
00068 
00069 
00070 NS_IMPL_ISUPPORTS_INHERITED1(nsAbLDAPChangeLogQuery, nsAbLDAPReplicationQuery, nsIAbLDAPChangeLogQuery)
00071 
00072 nsAbLDAPChangeLogQuery::nsAbLDAPChangeLogQuery()
00073 {
00074 }
00075 
00076 nsAbLDAPChangeLogQuery::~nsAbLDAPChangeLogQuery()
00077 {
00078 
00079 }
00080 
00081 // this is to be defined only till this is not hooked to SSL to get authDN and authPswd
00082 #define USE_AUTHDLG
00083 
00084 NS_IMETHODIMP nsAbLDAPChangeLogQuery::Init(const nsACString & aPrefName, nsIWebProgressListener *aProgressListener)
00085 {
00086     if(aPrefName.IsEmpty()) 
00087         return NS_ERROR_UNEXPECTED;
00088 
00089     mDirPrefName = aPrefName;
00090 
00091     nsresult rv = InitLDAPData();
00092     if(NS_FAILED(rv)) 
00093         return rv;
00094 
00095     // create the ChangeLog Data Processor
00096     mDataProcessor =  do_CreateInstance(NS_ABLDAP_PROCESSCHANGELOGDATA_CONTRACTID, &rv);
00097     if(NS_FAILED(rv)) 
00098         return rv;
00099 
00100     // 'this' initialized
00101     mInitialized = PR_TRUE;
00102 
00103     return mDataProcessor->Init(this, aProgressListener);
00104 }
00105 
00106 NS_IMETHODIMP nsAbLDAPChangeLogQuery::DoReplicationQuery()
00107 {
00108     if(!mInitialized) 
00109         return NS_ERROR_NOT_INITIALIZED;
00110 
00111 #ifdef USE_AUTHDLG
00112     return ConnectToLDAPServer(mURL, EmptyCString());
00113 #else
00114     mDataProcessor->PopulateAuthData();
00115     return ConnectToLDAPServer(mURL, mAuthDN);
00116 #endif
00117 }
00118 
00119 NS_IMETHODIMP nsAbLDAPChangeLogQuery::QueryAuthDN(const nsACString & aValueUsedToFindDn)
00120 {
00121     if(!mInitialized) 
00122         return NS_ERROR_NOT_INITIALIZED;
00123 
00124     nsresult rv = NS_OK; 
00125 
00126     CharPtrArrayGuard attributes;
00127     *attributes.GetSizeAddr() = 2;
00128     *attributes.GetArrayAddr() = NS_STATIC_CAST(char **, nsMemory::Alloc((*attributes.GetSizeAddr()) * sizeof(char *)));
00129     attributes.GetArray()[0] = ToNewCString(nsDependentCString(DIR_GetFirstAttributeString(mDirServer, cn)));
00130     attributes.GetArray()[1] = nsnull;
00131     
00132     nsCAutoString filter(DIR_GetFirstAttributeString(mDirServer, auth));
00133     filter += '=';
00134     filter += aValueUsedToFindDn;
00135 
00136     nsCAutoString dn;
00137     rv = mURL->GetDn(dn);
00138     if(NS_FAILED(rv)) 
00139         return rv;
00140 
00141     rv = CreateNewLDAPOperation();
00142     NS_ENSURE_SUCCESS(rv, rv);
00143 
00144     return mOperation->SearchExt(dn, nsILDAPURL::SCOPE_SUBTREE, filter, 
00145                                attributes.GetSize(), attributes.GetArray(),
00146                                0, 0);
00147 }
00148 
00149 NS_IMETHODIMP nsAbLDAPChangeLogQuery::QueryRootDSE()
00150 {
00151     if(!mInitialized) 
00152         return NS_ERROR_NOT_INITIALIZED;
00153 
00154     nsresult rv = CreateNewLDAPOperation();
00155     NS_ENSURE_SUCCESS(rv, rv);
00156     return mOperation->SearchExt(EmptyCString(), nsILDAPURL::SCOPE_BASE, 
00157                                  NS_LITERAL_CSTRING("objectclass=*"), 
00158                                  sizeof(sChangeLogRootDSEAttribs),
00159                                  sChangeLogRootDSEAttribs, 0, 0);
00160 }
00161 
00162 NS_IMETHODIMP nsAbLDAPChangeLogQuery::QueryChangeLog(const nsACString & aChangeLogDN, PRInt32 aLastChangeNo)
00163 {
00164     if(!mInitialized) 
00165         return NS_ERROR_NOT_INITIALIZED;
00166     if(aChangeLogDN.IsEmpty()) 
00167         return NS_ERROR_UNEXPECTED;
00168 
00169     // make sure that the filter here just have one condition 
00170     // and should not be enclosed in enclosing brackets.
00171     // also condition '>' doesnot work, it should be '>='/
00172     nsCAutoString filter (NS_LITERAL_CSTRING("changenumber>="));
00173     filter.AppendInt(mDirServer->replInfo->lastChangeNumber+1);
00174 
00175     nsresult rv = CreateNewLDAPOperation();
00176     NS_ENSURE_SUCCESS(rv, rv);
00177 
00178     return mOperation->SearchExt(aChangeLogDN, nsILDAPURL::SCOPE_ONELEVEL, filter, 
00179                                  sizeof(sChangeLogEntryAttribs),
00180                                  sChangeLogEntryAttribs, 0, 0);
00181 }
00182 
00183 NS_IMETHODIMP nsAbLDAPChangeLogQuery::QueryChangedEntries(const nsACString & aChangedEntryDN)
00184 {
00185     if(!mInitialized) 
00186         return NS_ERROR_NOT_INITIALIZED;
00187     if(aChangedEntryDN.IsEmpty()) 
00188         return NS_ERROR_UNEXPECTED;
00189 
00190     nsCAutoString urlFilter;
00191     nsresult rv = mURL->GetFilter(urlFilter);
00192     if(NS_FAILED(rv)) 
00193         return rv;
00194 
00195     PRInt32 scope;
00196     rv = mURL->GetScope(&scope);
00197     if(NS_FAILED(rv)) 
00198         return rv;
00199 
00200     CharPtrArrayGuard attributes;
00201     rv = mURL->GetAttributes(attributes.GetSizeAddr(), attributes.GetArrayAddr());
00202     if(NS_FAILED(rv)) 
00203         return rv;
00204 
00205     rv = CreateNewLDAPOperation();
00206     NS_ENSURE_SUCCESS(rv, rv);
00207     return mOperation->SearchExt(aChangedEntryDN, scope, urlFilter, 
00208                                attributes.GetSize(), attributes.GetArray(),
00209                                0, 0);
00210 }
00211