Back to index

lightning-sunbird  0.9+nobinonly
nsComm4xMailImport.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  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Srilatha Moturi <srilatha@netscape.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 
00039 /*
00040 
00041   Comm4xMail import mail interface
00042 
00043 */
00044 #ifdef MOZ_LOGGING
00045 // sorry, this has to be before the pre-compiled header
00046 #define FORCE_PR_LOG /* Allow logging in the release build */
00047 #endif
00048 
00049 #include "nscore.h"
00050 #include "nsCRT.h"
00051 #include "nsString.h"
00052 #include "nsCOMPtr.h"
00053 #include "nsIServiceManager.h"
00054 #include "nsIImportService.h"
00055 #include "nsIComponentManager.h"
00056 #include "nsComm4xMailImport.h"
00057 #include "nsIMemory.h"
00058 #include "nsXPCOM.h"
00059 #include "nsISupportsPrimitives.h"
00060 #include "nsIImportService.h"
00061 #include "nsIImportMailboxDescriptor.h"
00062 #include "nsIImportGeneric.h"
00063 #include "nsIImportFieldMap.h"
00064 #include "nsIOutputStream.h"
00065 #include "nsXPIDLString.h"
00066 #include "nsTextFormatter.h"
00067 #include "nsComm4xMailStringBundle.h"
00068 #include "nsIStringBundle.h"
00069 #include "nsReadableUtils.h"
00070 #include "Comm4xMailDebugLog.h"
00071 
00072 #include "nsDirectoryServiceDefs.h"
00073 
00074 #include "nsIProxyObjectManager.h"
00075 #include "nsProxiedService.h"
00076 
00077 static NS_DEFINE_IID(kISupportsIID,         NS_ISUPPORTS_IID);
00078 
00079 #define COMM4XMAIL_MSGS_URL   "chrome://messenger/locale/comm4xMailImportMsgs.properties"
00080 
00081 PRLogModuleInfo *COMM4XLOGMODULE = nsnull;
00082 
00085 
00086 
00087 nsComm4xMailImport::nsComm4xMailImport()
00088 {
00089     // Init logging module.
00090     if (!COMM4XLOGMODULE)
00091       COMM4XLOGMODULE = PR_NewLogModule("IMPORT");
00092 
00093     IMPORT_LOG0("nsComm4xMailImport Module Created\n");
00094 
00095     nsCOMPtr <nsIStringBundleService> pBundleService;
00096     nsresult rv;
00097 
00098     m_pBundle = nsnull;
00099 
00100     pBundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
00101     if (NS_SUCCEEDED(rv) && (pBundleService)) 
00102            pBundleService->CreateBundle(COMM4XMAIL_MSGS_URL, getter_AddRefs(m_pBundle));
00103 }
00104 
00105 
00106 nsComm4xMailImport::~nsComm4xMailImport()
00107 {
00108 
00109     IMPORT_LOG0("nsComm4xMailImport Module Deleted\n");
00110 
00111 }
00112 
00113 
00114 
00115 NS_IMPL_THREADSAFE_ISUPPORTS1(nsComm4xMailImport, nsIImportModule)
00116 
00117 
00118 NS_IMETHODIMP nsComm4xMailImport::GetName(PRUnichar **name)
00119 {
00120     NS_ENSURE_ARG_POINTER (name);
00121     nsresult rv = NS_ERROR_FAILURE;
00122     if (m_pBundle)
00123         rv = m_pBundle->GetStringFromID(COMM4XMAILIMPORT_NAME, name);
00124         return rv;
00125 }
00126 
00127 NS_IMETHODIMP nsComm4xMailImport::GetDescription(PRUnichar **name)
00128 {
00129     NS_ENSURE_ARG_POINTER (name);
00130     nsresult rv = NS_ERROR_FAILURE;
00131     if (m_pBundle)
00132         rv = m_pBundle->GetStringFromID(COMM4XMAILIMPORT_DESCRIPTION, name);
00133     return rv;
00134 }
00135 
00136 NS_IMETHODIMP nsComm4xMailImport::GetSupports(char **supports)
00137 {
00138     NS_ENSURE_ARG_POINTER (supports); 
00139     *supports = nsCRT::strdup(kComm4xMailSupportsString);
00140     return NS_OK;
00141 }
00142 
00143 NS_IMETHODIMP nsComm4xMailImport::GetSupportsUpgrade(PRBool *pUpgrade)
00144 {
00145     NS_ENSURE_ARG_POINTER (pUpgrade); 
00146     *pUpgrade = PR_FALSE;
00147     return NS_OK;
00148 }
00149 
00150 
00151 NS_IMETHODIMP nsComm4xMailImport::GetImportInterface(const char *pImportType, nsISupports **ppInterface)
00152 {
00153     NS_ENSURE_ARG_POINTER (pImportType);
00154     NS_ENSURE_ARG_POINTER (ppInterface);
00155     *ppInterface = nsnull;
00156     nsresult    rv;
00157     
00158     if (!strcmp(pImportType, "mail")) {
00159         // create the nsIImportMail interface and return it!
00160         nsCOMPtr <nsIImportMail> pMail = do_CreateInstance(NS_COMM4XMAILIMPL_CONTRACTID, &rv);
00161         if (NS_SUCCEEDED(rv)) {
00162             nsCOMPtr <nsIImportGeneric> pGeneric;
00163             nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
00164             if (NS_SUCCEEDED(rv)) {
00165                 rv = impSvc->CreateNewGenericMail(getter_AddRefs(pGeneric));
00166                 if (NS_SUCCEEDED(rv)) {
00167                     pGeneric->SetData("mailInterface", pMail);
00168                     nsXPIDLString name;
00169                     rv = m_pBundle->GetStringFromID( COMM4XMAILIMPORT_NAME, getter_Copies(name));
00170 
00171                     nsCOMPtr<nsISupportsString> nameString (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv));
00172                     NS_ENSURE_SUCCESS(rv,rv);
00173                     nameString->SetData(name);
00174                     pGeneric->SetData("name", nameString);
00175                     rv = pGeneric->QueryInterface(kISupportsIID, (void **)ppInterface);
00176                 }
00177             }
00178         }
00179         return rv;
00180     }
00181             
00182     return NS_ERROR_NOT_AVAILABLE;
00183 }
00184 
00186 
00187 ImportComm4xMailImpl::ImportComm4xMailImpl()
00188 {
00189     m_pBundleProxy = nsnull;
00190 }
00191 
00192 nsresult ImportComm4xMailImpl::Initialize()
00193 {
00194     nsCOMPtr <nsIStringBundleService> pBundleService;
00195     nsresult rv;
00196     nsCOMPtr <nsIStringBundle>  pBundle;
00197 
00198     pBundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
00199     if (NS_SUCCEEDED(rv) && (pBundleService))
00200         pBundleService->CreateBundle(COMM4XMAIL_MSGS_URL, getter_AddRefs(pBundle));
00201 
00202     nsCOMPtr<nsIProxyObjectManager> proxyMgr(do_GetService(NS_XPCOMPROXY_CONTRACTID, &rv));
00203     if (NS_SUCCEEDED(rv)) {
00204         rv = proxyMgr->GetProxyForObject( NS_UI_THREAD_EVENTQ, NS_GET_IID(nsIStringBundle),
00205                                           pBundle, PROXY_SYNC | PROXY_ALWAYS, getter_AddRefs(m_pBundleProxy));
00206     }
00207     return rv;
00208 }
00209 
00210 ImportComm4xMailImpl::~ImportComm4xMailImpl()
00211 {
00212 }
00213 
00214 
00215 
00216 NS_IMPL_THREADSAFE_ISUPPORTS1(ImportComm4xMailImpl, nsIImportMail)
00217 
00218 NS_IMETHODIMP ImportComm4xMailImpl::GetDefaultLocation(nsIFileSpec **ppLoc, PRBool *found, PRBool *userVerify)
00219 {
00220     NS_ENSURE_ARG_POINTER(found);
00221     NS_ENSURE_ARG_POINTER(ppLoc);
00222     NS_ENSURE_ARG_POINTER(userVerify);
00223     
00224     *ppLoc = nsnull;
00225     *found = PR_FALSE;
00226     *userVerify = PR_TRUE; 
00227     return NS_OK;  
00228 }
00229 
00230 
00231 NS_IMETHODIMP ImportComm4xMailImpl::FindMailboxes(nsIFileSpec *pLoc, nsISupportsArray **ppArray)
00232 {
00233     NS_ENSURE_ARG_POINTER(pLoc);
00234     NS_ENSURE_ARG_POINTER(ppArray);
00235 
00236     PRBool exists = PR_FALSE;
00237     nsresult rv = pLoc->Exists(&exists);
00238     if (NS_FAILED(rv) || !exists)
00239         return NS_ERROR_FAILURE;
00240 
00241     // find mail boxes
00242     rv = m_mail.FindMailboxes(pLoc, ppArray);
00243 
00244     if (NS_FAILED(rv) && *ppArray) {
00245         NS_RELEASE(*ppArray);
00246         *ppArray = nsnull;
00247     }
00248 
00249     return rv;
00250 }
00251 
00252 void ImportComm4xMailImpl::ReportStatus( PRInt32 errorNum, nsString& name, nsString *pStream)
00253 {
00254     if (!pStream) return;
00255     nsXPIDLString statusStr;
00256     const PRUnichar * fmtStr = name.get();
00257     nsresult rv = m_pBundleProxy->FormatStringFromID(errorNum, &fmtStr, 1, getter_Copies(statusStr));
00258     if (NS_SUCCEEDED (rv)) {
00259         pStream->Append (statusStr.get());
00260         pStream->Append( PRUnichar(nsCRT::LF));
00261     }
00262     
00263 }
00264 
00265 void ImportComm4xMailImpl::SetLogs(nsString& success, nsString& error, PRUnichar **pError, PRUnichar **pSuccess)
00266 {
00267     if (pError)
00268         *pError = ToNewUnicode(error);
00269     if (pSuccess)
00270         *pSuccess = ToNewUnicode(success);
00271 }
00272 
00273 NS_IMETHODIMP ImportComm4xMailImpl::ImportMailbox(nsIImportMailboxDescriptor *pSource, 
00274                                                 nsIFileSpec *pDestination, 
00275                                                 PRUnichar **pErrorLog,
00276                                                 PRUnichar **pSuccessLog,
00277                                                 PRBool *fatalError)
00278 {
00279     NS_PRECONDITION(pSource != nsnull, "null ptr");
00280     NS_PRECONDITION(pDestination != nsnull, "null ptr");
00281     NS_PRECONDITION(fatalError != nsnull, "null ptr");
00282     
00283     nsString    success;
00284     nsString    error;
00285     if (!pSource || !pDestination || !fatalError) {
00286         nsXPIDLString errorString;
00287         m_pBundleProxy->GetStringFromID(COMM4XMAILIMPORT_MAILBOX_BADPARAM, getter_Copies(errorString));
00288         error = errorString;
00289         if (fatalError)
00290             *fatalError = PR_TRUE;
00291         SetLogs(success, error, pErrorLog, pSuccessLog);
00292         return NS_ERROR_NULL_POINTER;
00293     }
00294       
00295     nsString    name;
00296     PRUnichar *    pName;
00297     if (NS_SUCCEEDED(pSource->GetDisplayName(&pName))) {
00298         name.Adopt(pName);
00299     }
00300     
00301     PRUint32 mailSize = 0;
00302     pSource->GetSize(&mailSize);
00303     if (mailSize == 0) {
00304         ReportStatus(COMM4XMAILIMPORT_MAILBOX_SUCCESS, name, &success);
00305         SetLogs(success, error, pErrorLog, pSuccessLog);
00306         return NS_OK;
00307     }
00308     
00309     PRUint32 index = 0;
00310     pSource->GetIdentifier(&index);
00311     nsresult rv = NS_OK;
00312 
00313     m_bytesDone = 0;
00314 
00315     // copy files from 4.x to here.
00316     nsCOMPtr <nsIFileSpec> inFile;
00317     if (NS_FAILED(pSource->GetFileSpec(getter_AddRefs(inFile)))) {
00318         ReportStatus(COMM4XMAILIMPORT_MAILBOX_CONVERTERROR, name, &error);
00319         SetLogs(success, error, pErrorLog, pSuccessLog);
00320         return NS_ERROR_FAILURE;
00321     }
00322 
00323     nsXPIDLCString pSrcPath, pDestPath;;
00324     inFile->GetNativePath(getter_Copies(pSrcPath));
00325     pDestination ->GetNativePath(getter_Copies(pDestPath));
00326     IMPORT_LOG2("ImportComm4xMailImpl::ImportMailbox: Copying folder from '%s' to '%s'.", pSrcPath.get(), pDestPath.get());
00327 
00328     nsCOMPtr <nsIFileSpec> parent;
00329     if (NS_FAILED (pDestination->GetParent(getter_AddRefs(parent))))
00330     {
00331         ReportStatus( COMM4XMAILIMPORT_MAILBOX_CONVERTERROR, name, &error);
00332         SetLogs( success, error, pErrorLog, pSuccessLog);
00333         return( NS_ERROR_FAILURE);
00334     }
00335     PRBool exists = PR_FALSE;
00336     pDestination->Exists(&exists);
00337     if (exists)
00338         rv = pDestination->Delete(PR_FALSE);
00339     rv = inFile->CopyToDir(parent);
00340       
00341     if (NS_SUCCEEDED(rv)) {
00342         m_bytesDone = mailSize;
00343         ReportStatus(COMM4XMAILIMPORT_MAILBOX_SUCCESS, name, &success);
00344     }
00345     else {
00346         ReportStatus(COMM4XMAILIMPORT_MAILBOX_CONVERTERROR, name, &error);
00347     }
00348 
00349     SetLogs(success, error, pErrorLog, pSuccessLog);
00350 
00351     return rv;
00352 }
00353 
00354 
00355 NS_IMETHODIMP ImportComm4xMailImpl::GetImportProgress(PRUint32 *pDoneSoFar) 
00356 { 
00357     NS_ENSURE_ARG_POINTER(pDoneSoFar);
00358 
00359     *pDoneSoFar = m_bytesDone;
00360     return NS_OK;
00361 }
00362 
00363 NS_IMETHODIMP ImportComm4xMailImpl::TranslateFolderName(const nsAString & aFolderName, nsAString & _retval)
00364 {
00365   _retval = aFolderName; 
00366   return NS_OK;
00367 }