Back to index

lightning-sunbird  0.9+nobinonly
nsAbLDAPReplicationService.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Rajiv Dayal <rdayal@netscape.com>
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 
00038 #include "nsCOMPtr.h"
00039 #include "nsAbLDAPReplicationService.h"
00040 #include "nsAbLDAPReplicationQuery.h"
00041 #include "nsAbBaseCID.h"
00042 #include "nsIWebProgressListener.h"
00043 
00044 #include "nsAbLDAPChangeLogQuery.h"
00045 #include "nsIAbLDAPReplicationData.h"
00046 
00047 /*** implementation of the service ******/
00048 
00049 NS_IMPL_ISUPPORTS1(nsAbLDAPReplicationService, nsIAbLDAPReplicationService)
00050 
00051 nsAbLDAPReplicationService::nsAbLDAPReplicationService() 
00052     : mReplicating(PR_FALSE)
00053 {
00054 }
00055 
00056 nsAbLDAPReplicationService::~nsAbLDAPReplicationService()
00057 {
00058 }
00059 
00060 /* void startReplication(in string aURI, in nsIWebProgressListener progressListener); */
00061 NS_IMETHODIMP nsAbLDAPReplicationService::StartReplication(const nsACString & aPrefName, nsIWebProgressListener *progressListener)
00062 {
00063     if(aPrefName.IsEmpty())
00064         return NS_ERROR_UNEXPECTED;
00065 
00066 #ifdef DEBUG_rdayal
00067     printf("Start Replication called");
00068 #endif
00069 
00070     // makes sure to allow only one replication at a time
00071     if(mReplicating)
00072         return NS_ERROR_FAILURE;
00073 
00074     mPrefName = aPrefName;
00075 
00076     nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
00077 
00078     switch(DecideProtocol())
00079     {
00080         case nsIAbLDAPProcessReplicationData::kDefaultDownloadAll :
00081             mQuery = do_CreateInstance(NS_ABLDAP_REPLICATIONQUERY_CONTRACTID, &rv);
00082             break ;
00083         case nsIAbLDAPProcessReplicationData::kChangeLogProtocol :
00084             mQuery = do_CreateInstance (NS_ABLDAP_CHANGELOGQUERY_CONTRACTID, &rv);
00085             break ;
00086         default :
00087             break;
00088     }
00089 
00090             if(NS_SUCCEEDED(rv) && mQuery)
00091             {
00092                rv = mQuery->Init(aPrefName, progressListener);
00093                if(NS_SUCCEEDED(rv))
00094                {
00095                    rv = mQuery->DoReplicationQuery();
00096                    if(NS_SUCCEEDED(rv))
00097                    {
00098                        mReplicating = PR_TRUE;
00099                        return rv;
00100                    }
00101                }
00102             }
00103 
00104     if(progressListener && NS_FAILED(rv))
00105         progressListener->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, PR_FALSE);
00106        
00107     return rv;
00108 }
00109 
00110 /* void cancelReplication(in string aURI); */
00111 NS_IMETHODIMP nsAbLDAPReplicationService::CancelReplication(const nsACString & aPrefName)
00112 {
00113     if(aPrefName.IsEmpty())
00114         return NS_ERROR_UNEXPECTED;
00115 
00116     nsresult rv = NS_ERROR_FAILURE;
00117 
00118     if(aPrefName == mPrefName)
00119     {
00120         if(mQuery && mReplicating)
00121             rv = mQuery->CancelQuery();  
00122     }
00123 
00124     // if query has been cancelled successfully
00125     if(NS_SUCCEEDED(rv))
00126         Done(PR_FALSE);
00127 
00128     return rv;
00129 }
00130 
00131 NS_IMETHODIMP nsAbLDAPReplicationService::Done(PRBool aSuccess)
00132 {
00133     mReplicating = PR_FALSE;
00134     if(mQuery)
00135         mQuery = nsnull;  // release query obj
00136 
00137     return NS_OK;
00138 }
00139 
00140 
00141 // XXX: This method should query the RootDSE for the changeLog attribute, 
00142 // if it exists ChangeLog protocol is supported.
00143 PRInt32 nsAbLDAPReplicationService::DecideProtocol()
00144 {
00145   // do the changeLog, it will decide if there is a need to replicate all
00146   // entries or only update existing DB and will do the approprite thing.
00147   //
00148   // XXX: Bug 231965 changed this from kChangeLogProtocol to
00149   // kDefaultDownloadAll because of a problem with ldap replication not
00150   // working correctly. We need to change this back at some stage (bug 311632).
00151   return nsIAbLDAPProcessReplicationData::kDefaultDownloadAll;
00152 }
00153 
00154