Back to index

lightning-sunbird  0.9+nobinonly
nsEudoraImport.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) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Pierre Phaneuf <pp@ludusdesign.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 
00041 /*
00042 
00043   Eudora import mail and addressbook interfaces
00044 
00045 */
00046 #ifdef MOZ_LOGGING
00047 // sorry, this has to be before the pre-compiled header
00048 #define FORCE_PR_LOG /* Allow logging in the release build */
00049 #endif
00050 
00051 #include "nscore.h"
00052 #include "nsCRT.h"
00053 #include "nsString.h"
00054 #include "nsIServiceManager.h"
00055 #include "nsIImportService.h"
00056 #include "nsIComponentManager.h"
00057 #include "nsEudoraImport.h"
00058 #include "nsIMemory.h"
00059 #include "nsIImportService.h"
00060 #include "nsIImportMail.h"
00061 #include "nsIImportMailboxDescriptor.h"
00062 #include "nsIImportGeneric.h"
00063 #include "nsIImportAddressBooks.h"
00064 #include "nsIImportABDescriptor.h"
00065 #include "nsIImportSettings.h"
00066 #include "nsIImportFieldMap.h"
00067 #include "nsXPCOM.h"
00068 #include "nsISupportsPrimitives.h"
00069 #include "nsIOutputStream.h"
00070 #include "nsIAddrDatabase.h"
00071 #include "nsTextFormatter.h"
00072 #include "nsEudoraStringBundle.h"
00073 #include "nsIStringBundle.h"
00074 #include "nsEudoraSettings.h"
00075 #include "nsReadableUtils.h"
00076 #include "nsUnicharUtils.h"
00077 
00078 #if defined(XP_WIN) || defined(XP_OS2)
00079 #include "nsEudoraWin32.h"
00080 #endif
00081 #if defined(XP_MAC) || defined(XP_MACOSX)
00082 #include "nsEudoraMac.h"
00083 #endif
00084 
00085 #include "EudoraDebugLog.h"
00086 
00087 static NS_DEFINE_IID(kISupportsIID,                     NS_ISUPPORTS_IID);
00088 PRLogModuleInfo *EUDORALOGMODULE = nsnull;
00089 
00090 class ImportEudoraMailImpl : public nsIImportMail
00091 {
00092 public:
00093     ImportEudoraMailImpl();
00094     virtual ~ImportEudoraMailImpl();
00095 
00096        static nsresult Create(nsIImportMail** aImport);
00097 
00098     // nsISupports interface
00099     NS_DECL_ISUPPORTS
00100 
00101     // nsIImportmail interface
00102   
00103        /* void GetDefaultLocation (out nsIFileSpec location, out boolean found, out boolean userVerify); */
00104        NS_IMETHOD GetDefaultLocation(nsIFileSpec **location, PRBool *found, PRBool *userVerify);
00105        
00106        /* nsISupportsArray FindMailboxes (in nsIFileSpec location); */
00107        NS_IMETHOD FindMailboxes(nsIFileSpec *location, nsISupportsArray **_retval);
00108        
00109        /* void ImportMailbox (in nsIImportMailboxDescriptor source, in nsIFileSpec destination, out boolean fatalError); */
00110        NS_IMETHOD ImportMailbox(nsIImportMailboxDescriptor *source, nsIFileSpec *destination, 
00111                                                         PRUnichar **pErrorLog, PRUnichar **pSuccessLog, PRBool *fatalError);
00112        
00113        /* unsigned long GetImportProgress (); */
00114        NS_IMETHOD GetImportProgress(PRUint32 *_retval);
00115        
00116     NS_IMETHOD TranslateFolderName(const nsAString & aFolderName, nsAString & _retval);
00117        
00118 public:
00119        static void   AddLinebreak( nsString *pStream);
00120        static void   SetLogs( nsString& success, nsString& error, PRUnichar **pError, PRUnichar **pSuccess);
00121        static void ReportError( PRInt32 errorNum, nsString& name, nsString *pStream);
00122 
00123 
00124 private:
00125        static void   ReportSuccess( nsString& name, PRInt32 count, nsString *pStream);
00126 
00127 private:
00128 #if defined(XP_WIN) || defined(XP_OS2)
00129        nsEudoraWin32 m_eudora;
00130 #endif
00131 #if defined(XP_MAC) || defined(XP_MACOSX)
00132        nsEudoraMac          m_eudora;
00133 #endif
00134        PRUint32             m_bytes;
00135 };
00136 
00137 
00138 class ImportEudoraAddressImpl : public nsIImportAddressBooks
00139 {
00140 public:
00141     ImportEudoraAddressImpl();
00142     virtual ~ImportEudoraAddressImpl();
00143 
00144        static nsresult Create(nsIImportAddressBooks** aImport);
00145 
00146     // nsISupports interface
00147     NS_DECL_ISUPPORTS
00148 
00149     // nsIImportAddressBooks interface
00150     
00151        /* PRBool GetSupportsMultiple (); */
00152        NS_IMETHOD GetSupportsMultiple(PRBool *_retval) { *_retval = PR_TRUE; return( NS_OK);}
00153        
00154        /* PRBool GetAutoFind (out wstring description); */
00155        NS_IMETHOD GetAutoFind(PRUnichar **description, PRBool *_retval);
00156        
00157        /* PRBool GetNeedsFieldMap (nsIFileSpec location); */
00158        NS_IMETHOD GetNeedsFieldMap(nsIFileSpec *location, PRBool *_retval) { *_retval = PR_FALSE; return( NS_OK);}
00159        
00160        /* void GetDefaultLocation (out nsIFileSpec location, out boolean found, out boolean userVerify); */
00161        NS_IMETHOD GetDefaultLocation(nsIFileSpec **location, PRBool *found, PRBool *userVerify);
00162        
00163        /* nsISupportsArray FindAddressBooks (in nsIFileSpec location); */
00164        NS_IMETHOD FindAddressBooks(nsIFileSpec *location, nsISupportsArray **_retval);
00165        
00166        /* nsISupports GetFieldMap (in nsIImportABDescriptor source); */
00167        NS_IMETHOD InitFieldMap(nsIFileSpec *location, nsIImportFieldMap *fieldMap)
00168               { return( NS_ERROR_FAILURE); }
00169        
00170        /* void ImportAddressBook (in nsIImportABDescriptor source, in nsIAddrDatabase destination, in nsIImportFieldMap fieldMap, in boolean isAddrLocHome, out wstring errorLog, out wstring successLog, out boolean fatalError); */
00171        NS_IMETHOD ImportAddressBook(      nsIImportABDescriptor *source, 
00172                                                                nsIAddrDatabase *    destination, 
00173                                                                nsIImportFieldMap *  fieldMap, 
00174                                                                PRBool isAddrLocHome, 
00175                                                                PRUnichar **         errorLog,
00176                                                                PRUnichar **         successLog,
00177                                                                PRBool *                    fatalError);
00178        
00179        /* unsigned long GetImportProgress (); */
00180        NS_IMETHOD GetImportProgress(PRUint32 *_retval);
00181  
00182        NS_IMETHOD GetSampleData( PRInt32 index, PRBool *pFound, PRUnichar **pStr)
00183               { return( NS_ERROR_FAILURE);}
00184 
00185        NS_IMETHOD SetSampleLocation( nsIFileSpec *) { return( NS_OK); }
00186 
00187 private:
00188        static void   ReportSuccess( nsString& name, nsString *pStream);
00189 
00190 private:
00191 #if defined(XP_WIN) || defined(XP_OS2)
00192        nsEudoraWin32 m_eudora;
00193 #endif
00194 #if defined(XP_MAC) || defined(XP_MACOSX)
00195        nsEudoraMac          m_eudora;
00196 #endif
00197        PRUint32             m_bytes;
00198 };
00199 
00202 
00203 
00204 nsEudoraImport::nsEudoraImport()
00205 {
00206   // Init logging module.
00207   if (!EUDORALOGMODULE)
00208     EUDORALOGMODULE = PR_NewLogModule("IMPORT");
00209        IMPORT_LOG0( "nsEudoraImport Module Created\n");
00210 
00211        nsEudoraStringBundle::GetStringBundle();
00212 }
00213 
00214 
00215 nsEudoraImport::~nsEudoraImport()
00216 {
00217 
00218        IMPORT_LOG0( "nsEudoraImport Module Deleted\n");
00219 
00220 }
00221 
00222 
00223 
00224 NS_IMPL_ISUPPORTS1(nsEudoraImport, nsIImportModule)
00225 
00226 
00227 NS_IMETHODIMP nsEudoraImport::GetName( PRUnichar **name)
00228 {
00229     NS_PRECONDITION(name != nsnull, "null ptr");
00230     if (! name)
00231         return NS_ERROR_NULL_POINTER;
00232 
00233        // nsString   title = "Outlook Express";
00234        // *name = ToNewUnicode(title);
00235        *name = nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_NAME);
00236               
00237     return NS_OK;
00238 }
00239 
00240 NS_IMETHODIMP nsEudoraImport::GetDescription( PRUnichar **name)
00241 {
00242     NS_PRECONDITION(name != nsnull, "null ptr");
00243     if (! name)
00244         return NS_ERROR_NULL_POINTER;
00245 
00246        // nsString   desc = "Outlook Express mail and address books";
00247        // *name = ToNewUnicode(desc);
00248        *name = nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_DESCRIPTION);
00249               
00250     return NS_OK;
00251 }
00252 
00253 NS_IMETHODIMP nsEudoraImport::GetSupports( char **supports)
00254 {
00255     NS_PRECONDITION(supports != nsnull, "null ptr");
00256     if (! supports)
00257         return NS_ERROR_NULL_POINTER;
00258        
00259        *supports = nsCRT::strdup( kEudoraSupportsString);
00260        return( NS_OK);
00261 }
00262 
00263 NS_IMETHODIMP nsEudoraImport::GetSupportsUpgrade( PRBool *pUpgrade)
00264 {
00265     NS_PRECONDITION(pUpgrade != nsnull, "null ptr");
00266     if (! pUpgrade)
00267         return NS_ERROR_NULL_POINTER;
00268        
00269        *pUpgrade = PR_TRUE;
00270        return( NS_OK);
00271 }
00272 
00273 
00274 NS_IMETHODIMP nsEudoraImport::GetImportInterface( const char *pImportType, nsISupports **ppInterface)
00275 {
00276     NS_PRECONDITION(pImportType != nsnull, "null ptr");
00277     if (! pImportType)
00278         return NS_ERROR_NULL_POINTER;
00279     NS_PRECONDITION(ppInterface != nsnull, "null ptr");
00280     if (! ppInterface)
00281         return NS_ERROR_NULL_POINTER;
00282 
00283        *ppInterface = nsnull;
00284        nsresult      rv;
00285        if (!nsCRT::strcmp( pImportType, "mail")) {
00286               // create the nsIImportMail interface and return it!
00287               nsIImportMail *      pMail = nsnull;
00288               nsIImportGeneric *pGeneric = nsnull;
00289               rv = ImportEudoraMailImpl::Create( &pMail);
00290               if (NS_SUCCEEDED( rv)) {
00291                      nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
00292                      if (NS_SUCCEEDED( rv)) {
00293                             rv = impSvc->CreateNewGenericMail( &pGeneric);
00294                             if (NS_SUCCEEDED( rv)) {
00295                                    pGeneric->SetData( "mailInterface", pMail);
00296                                    nsString name;
00297                                    nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_NAME, name);
00298                                    nsCOMPtr<nsISupportsString> nameString (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv));
00299                                    if (NS_SUCCEEDED(rv)) {
00300                                           nameString->SetData(name);
00301                                           pGeneric->SetData( "name", nameString);
00302                                           rv = pGeneric->QueryInterface( kISupportsIID, (void **)ppInterface);
00303                                    }
00304                             }
00305                      }
00306               }
00307               NS_IF_RELEASE( pMail);
00308               NS_IF_RELEASE( pGeneric);
00309               return( rv);
00310        }
00311        
00312        if (!nsCRT::strcmp( pImportType, "addressbook")) {
00313               // create the nsIImportMail interface and return it!
00314               nsIImportAddressBooks *     pAddress = nsnull;
00315               nsIImportGeneric *          pGeneric = nsnull;
00316               rv = ImportEudoraAddressImpl::Create( &pAddress);
00317               if (NS_SUCCEEDED( rv)) {
00318                      nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
00319                      if (NS_SUCCEEDED( rv)) {
00320                             rv = impSvc->CreateNewGenericAddressBooks( &pGeneric);
00321                             if (NS_SUCCEEDED( rv)) {
00322                                    pGeneric->SetData( "addressInterface", pAddress);
00323                                    rv = pGeneric->QueryInterface( kISupportsIID, (void **)ppInterface);
00324                             }
00325                      }
00326               }
00327               NS_IF_RELEASE( pAddress);
00328               NS_IF_RELEASE( pGeneric);
00329               return( rv);
00330        }
00331        
00332        if (!nsCRT::strcmp( pImportType, "settings")) {
00333               nsIImportSettings *pSettings = nsnull;
00334               rv = nsEudoraSettings::Create( &pSettings);
00335               if (NS_SUCCEEDED( rv)) {
00336                      pSettings->QueryInterface( kISupportsIID, (void **)ppInterface);
00337               }
00338               NS_IF_RELEASE( pSettings);
00339               return( rv);
00340        }
00341               
00342        return( NS_ERROR_NOT_AVAILABLE);
00343 }
00344 
00346 nsresult ImportEudoraMailImpl::Create(nsIImportMail** aImport)
00347 {
00348     NS_PRECONDITION(aImport != nsnull, "null ptr");
00349     if (! aImport)
00350         return NS_ERROR_NULL_POINTER;
00351 
00352     *aImport = new ImportEudoraMailImpl();
00353     if (! *aImport)
00354         return NS_ERROR_OUT_OF_MEMORY;
00355 
00356     NS_ADDREF(*aImport);
00357     return NS_OK;
00358 }
00359 
00360 ImportEudoraMailImpl::ImportEudoraMailImpl()
00361 {
00362 }
00363 
00364 
00365 ImportEudoraMailImpl::~ImportEudoraMailImpl()
00366 {
00367 }
00368 
00369 
00370 
00371 NS_IMPL_THREADSAFE_ISUPPORTS1(ImportEudoraMailImpl, nsIImportMail)
00372 
00373 NS_IMETHODIMP ImportEudoraMailImpl::GetDefaultLocation( nsIFileSpec **ppLoc, PRBool *found, PRBool *userVerify)
00374 {
00375     NS_PRECONDITION(ppLoc != nsnull, "null ptr");
00376     NS_PRECONDITION(found != nsnull, "null ptr");
00377     NS_PRECONDITION(userVerify != nsnull, "null ptr");
00378     if (!ppLoc || !found || !userVerify)
00379         return NS_ERROR_NULL_POINTER;
00380        
00381        *ppLoc = nsnull;
00382 
00383        nsresult      rv;
00384        nsIFileSpec * spec;
00385        if (NS_FAILED( rv = NS_NewFileSpec( &spec)))
00386               return( rv);
00387        
00388        *found = m_eudora.FindMailFolder( spec);
00389        
00390        if (!*found) {
00391               NS_RELEASE( spec);
00392        }
00393        else {
00394               *ppLoc = spec;
00395        }
00396 
00397        *userVerify = PR_TRUE;
00398 
00399        return( NS_OK);
00400 }
00401 
00402 
00403 NS_IMETHODIMP ImportEudoraMailImpl::FindMailboxes( nsIFileSpec *pLoc, nsISupportsArray **ppArray)
00404 {
00405     NS_PRECONDITION(pLoc != nsnull, "null ptr");
00406     NS_PRECONDITION(ppArray != nsnull, "null ptr");
00407     if (!pLoc || !ppArray)
00408         return NS_ERROR_NULL_POINTER;
00409        
00410        PRBool exists = PR_FALSE;
00411        nsresult rv = pLoc->Exists( &exists);
00412        if (NS_FAILED( rv) || !exists)
00413               return( NS_ERROR_FAILURE);
00414 
00415        rv = m_eudora.FindMailboxes( pLoc, ppArray);
00416        if (NS_FAILED( rv) && *ppArray) {
00417               NS_RELEASE( *ppArray);
00418               *ppArray = nsnull;
00419        }
00420        
00421        return( rv);
00422 }
00423 
00424 void ImportEudoraMailImpl::AddLinebreak( nsString *pStream)
00425 {
00426        if (pStream)
00427               pStream->Append( PRUnichar(nsCRT::LF));
00428 }
00429 
00430 void ImportEudoraMailImpl::ReportSuccess( nsString& name, PRInt32 count, nsString *pStream)
00431 {
00432        if (!pStream)
00433               return;
00434        // load the success string
00435        nsIStringBundle *pBundle = nsEudoraStringBundle::GetStringBundleProxy();
00436        PRUnichar *pFmt = nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_MAILBOX_SUCCESS, pBundle);
00437        PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get(), count);
00438        pStream->Append( pText);
00439        nsTextFormatter::smprintf_free( pText);
00440        nsEudoraStringBundle::FreeString( pFmt);
00441        AddLinebreak( pStream);
00442        NS_IF_RELEASE( pBundle);
00443 }
00444 
00445 void ImportEudoraMailImpl::ReportError( PRInt32 errorNum, nsString& name, nsString *pStream)
00446 {
00447        if (!pStream)
00448               return;
00449        // load the error string
00450        nsIStringBundle *pBundle = nsEudoraStringBundle::GetStringBundleProxy();
00451        PRUnichar *pFmt = nsEudoraStringBundle::GetStringByID( errorNum);
00452        PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get());
00453        pStream->Append( pText);
00454        nsTextFormatter::smprintf_free( pText);
00455        nsEudoraStringBundle::FreeString( pFmt);
00456        AddLinebreak( pStream);
00457        NS_IF_RELEASE( pBundle);
00458 }
00459 
00460 
00461 void ImportEudoraMailImpl::SetLogs( nsString& success, nsString& error, PRUnichar **pError, PRUnichar **pSuccess)
00462 {
00463        if (pError)
00464               *pError = ToNewUnicode(error);
00465        if (pSuccess)
00466               *pSuccess = ToNewUnicode(success);
00467 }
00468 
00469 NS_IMETHODIMP ImportEudoraMailImpl::ImportMailbox(      nsIImportMailboxDescriptor *pSource, 
00470                                                                                     nsIFileSpec *pDestination, 
00471                                                                                     PRUnichar **pErrorLog,
00472                                                                                     PRUnichar **pSuccessLog,
00473                                                                                     PRBool *fatalError)
00474 {
00475     NS_PRECONDITION(pSource != nsnull, "null ptr");
00476     NS_PRECONDITION(pDestination != nsnull, "null ptr");
00477     NS_PRECONDITION(fatalError != nsnull, "null ptr");
00478 
00479 
00480        nsCOMPtr<nsIStringBundle> bundle( dont_AddRef( nsEudoraStringBundle::GetStringBundleProxy()));
00481        
00482        nsString      success;
00483        nsString      error;
00484     if (!pSource || !pDestination || !fatalError) {
00485               IMPORT_LOG0( "*** Bad param passed to eudora mailbox import\n");
00486               nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_MAILBOX_BADPARAM, error, bundle);
00487               if (fatalError)
00488                      *fatalError = PR_TRUE;
00489               SetLogs( success, error, pErrorLog, pSuccessLog);
00490            return NS_ERROR_NULL_POINTER;
00491        }
00492       
00493     PRBool           abort = PR_FALSE;
00494     nsString  name;
00495     PRUnichar *      pName;
00496     if (NS_SUCCEEDED( pSource->GetDisplayName( &pName))) {
00497        name = pName;
00498        nsCRT::free( pName);
00499     }
00500     
00501        PRUint32 mailSize = 0;
00502        pSource->GetSize( &mailSize);
00503        if (mailSize == 0) {
00504               IMPORT_LOG0( "Mailbox size is 0, skipping mailbox.\n");
00505               ReportSuccess( name, 0, &success);
00506               SetLogs( success, error, pErrorLog, pSuccessLog);
00507               return( NS_OK);
00508        }
00509 
00510     
00511        nsIFileSpec   *      inFile;
00512     if (NS_FAILED( pSource->GetFileSpec( &inFile))) {
00513               ReportError( EUDORAIMPORT_MAILBOX_BADSOURCEFILE, name, &error);
00514               SetLogs( success, error, pErrorLog, pSuccessLog);              
00515        return( NS_ERROR_FAILURE);
00516     }
00517 
00518 #ifdef IMPORT_DEBUG
00519        char *pPath;
00520        inFile->GetNativePath( &pPath);    
00521        IMPORT_LOG1( "Import mailbox: %s\n", pPath);
00522        nsCRT::free( pPath);
00523 #endif
00524        
00525            
00526        PRInt32       msgCount = 0;
00527     nsresult rv = NS_OK;
00528        
00529        m_bytes = 0;
00530        rv = m_eudora.ImportMailbox( &m_bytes, &abort, name.get(), inFile, pDestination, &msgCount);
00531 
00532     inFile->Release();
00533 
00534            
00535     
00536        if (NS_SUCCEEDED( rv)) {
00537               ReportSuccess( name, msgCount, &success);
00538        }
00539        else {
00540               ReportError( EUDORAIMPORT_MAILBOX_CONVERTERROR, name, &error);
00541        }
00542 
00543        SetLogs( success, error, pErrorLog, pSuccessLog);
00544 
00545        IMPORT_LOG0( "*** Returning from eudora mailbox import\n");
00546 
00547     return( rv);
00548 }
00549 
00550 
00551 NS_IMETHODIMP ImportEudoraMailImpl::GetImportProgress( PRUint32 *pDoneSoFar) 
00552 { 
00553     NS_PRECONDITION(pDoneSoFar != nsnull, "null ptr");
00554     if (! pDoneSoFar)
00555         return NS_ERROR_NULL_POINTER;
00556        
00557        *pDoneSoFar = m_bytes;
00558        return( NS_OK);
00559 }
00560 
00561 
00562 NS_IMETHODIMP ImportEudoraMailImpl::TranslateFolderName(const nsAString & aFolderName, nsAString & _retval)
00563 {
00564     if (aFolderName.Equals(NS_LITERAL_STRING("Out"), nsCaseInsensitiveStringComparator()))
00565         _retval = NS_LITERAL_STRING(kDestUnsentMessagesFolderName);
00566     else if (aFolderName.Equals(NS_LITERAL_STRING("In"), nsCaseInsensitiveStringComparator()))
00567         _retval = NS_LITERAL_STRING(kDestInboxFolderName);
00568     else
00569     _retval = aFolderName; 
00570     return NS_OK;
00571 }
00572 
00573 nsresult ImportEudoraAddressImpl::Create(nsIImportAddressBooks** aImport)
00574 {
00575     NS_PRECONDITION(aImport != nsnull, "null ptr");
00576     if (! aImport)
00577         return NS_ERROR_NULL_POINTER;
00578 
00579     *aImport = new ImportEudoraAddressImpl();
00580     if (! *aImport)
00581         return NS_ERROR_OUT_OF_MEMORY;
00582 
00583     NS_ADDREF(*aImport);
00584     return NS_OK;
00585 }
00586 
00587 ImportEudoraAddressImpl::ImportEudoraAddressImpl()
00588 {
00589 }
00590 
00591 
00592 ImportEudoraAddressImpl::~ImportEudoraAddressImpl()
00593 {
00594 }
00595 
00596 
00597 
00598 NS_IMPL_THREADSAFE_ISUPPORTS1(ImportEudoraAddressImpl, nsIImportAddressBooks)
00599 
00600        
00601 NS_IMETHODIMP ImportEudoraAddressImpl::GetAutoFind(PRUnichar **description, PRBool *_retval)
00602 {
00603     NS_PRECONDITION(description != nsnull, "null ptr");
00604     NS_PRECONDITION(_retval != nsnull, "null ptr");
00605     if (! description || !_retval)
00606         return NS_ERROR_NULL_POINTER;
00607     
00608     nsString  str;
00609     *_retval = PR_FALSE;
00610     nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_NICKNAMES_NAME, str);
00611     *description = ToNewUnicode(str);
00612     
00613     return( NS_OK);
00614 }
00615 
00616 
00617 NS_IMETHODIMP ImportEudoraAddressImpl::GetDefaultLocation(nsIFileSpec **ppLoc, PRBool *found, PRBool *userVerify)
00618 {
00619     NS_PRECONDITION(found != nsnull, "null ptr");
00620     NS_PRECONDITION(ppLoc != nsnull, "null ptr");
00621     NS_PRECONDITION(userVerify != nsnull, "null ptr");
00622     if (! found || !userVerify || !ppLoc)
00623         return NS_ERROR_NULL_POINTER;
00624     
00625        *ppLoc = nsnull;
00626        nsresult             rv;
00627        nsIFileSpec * spec;
00628        if (NS_FAILED( rv = NS_NewFileSpec( &spec)))
00629               return( rv);
00630        
00631        *found = m_eudora.FindAddressFolder( spec);
00632        if (!(*found)) {
00633               NS_IF_RELEASE( spec);
00634        }
00635        else {
00636               *ppLoc = spec;
00637        }
00638        *userVerify = PR_TRUE;
00639        
00640        return( NS_OK);    
00641 }
00642 
00643 
00644        
00645 NS_IMETHODIMP ImportEudoraAddressImpl::FindAddressBooks(nsIFileSpec *pLoc, nsISupportsArray **ppArray)
00646 {
00647     NS_PRECONDITION(pLoc != nsnull, "null ptr");
00648     NS_PRECONDITION(ppArray != nsnull, "null ptr");
00649     if (!pLoc || !ppArray)
00650         return NS_ERROR_NULL_POINTER;
00651        
00652        PRBool exists = PR_FALSE;
00653        nsresult rv = pLoc->Exists( &exists);
00654        if (NS_FAILED( rv) || !exists)
00655               return( NS_ERROR_FAILURE);
00656 
00657        rv = m_eudora.FindAddressBooks( pLoc, ppArray);
00658        if (NS_FAILED( rv) && *ppArray) {
00659               NS_RELEASE( *ppArray);
00660               *ppArray = nsnull;
00661        }
00662        
00663        return( rv);
00664 }
00665 
00666        
00667 
00668 void ImportEudoraAddressImpl::ReportSuccess( nsString& name, nsString *pStream)
00669 {
00670        if (!pStream)
00671               return;
00672        // load the success string
00673        nsIStringBundle *pBundle = nsEudoraStringBundle::GetStringBundleProxy();
00674        PRUnichar *pFmt = nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_ADDRESS_SUCCESS, pBundle);
00675        PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get());
00676        pStream->Append( pText);
00677        nsTextFormatter::smprintf_free( pText);
00678        nsEudoraStringBundle::FreeString( pFmt);
00679        ImportEudoraMailImpl::AddLinebreak( pStream);
00680        NS_IF_RELEASE( pBundle);
00681 }
00682 
00683 
00684 NS_IMETHODIMP ImportEudoraAddressImpl::ImportAddressBook(      nsIImportABDescriptor *pSource, 
00685                                                                                            nsIAddrDatabase *    pDestination, 
00686                                                                                            nsIImportFieldMap *  fieldMap, 
00687                                                         PRBool isAddrLocHome, 
00688                                                                                            PRUnichar **         pErrorLog,
00689                                                                                            PRUnichar **         pSuccessLog,
00690                                                                                            PRBool *                    fatalError)
00691 {
00692     NS_PRECONDITION(pSource != nsnull, "null ptr");
00693     NS_PRECONDITION(pDestination != nsnull, "null ptr");
00694     NS_PRECONDITION(fatalError != nsnull, "null ptr");
00695        
00696        nsCOMPtr<nsIStringBundle> bundle( dont_AddRef( nsEudoraStringBundle::GetStringBundleProxy()));
00697 
00698        nsString      success;
00699        nsString      error;
00700     if (!pSource || !pDestination || !fatalError) {
00701               IMPORT_LOG0( "*** Bad param passed to eudora address import\n");
00702               nsEudoraStringBundle::GetStringByID( EUDORAIMPORT_ADDRESS_BADPARAM, error, bundle);
00703               if (fatalError)
00704                      *fatalError = PR_TRUE;
00705               ImportEudoraMailImpl::SetLogs( success, error, pErrorLog, pSuccessLog);
00706            return NS_ERROR_NULL_POINTER;
00707        }
00708       
00709     PRBool           abort = PR_FALSE;
00710     nsString  name;
00711     PRUnichar *      pName;
00712     if (NS_SUCCEEDED( pSource->GetPreferredName( &pName))) {
00713        name = pName;
00714        nsCRT::free( pName);
00715     }
00716     
00717        PRUint32 addressSize = 0;
00718        pSource->GetSize( &addressSize);
00719        if (addressSize == 0) {
00720               IMPORT_LOG0( "Address book size is 0, skipping mailbox.\n");
00721               ReportSuccess( name, &success);
00722               ImportEudoraMailImpl::SetLogs( success, error, pErrorLog, pSuccessLog);
00723               return( NS_OK);
00724        }
00725 
00726     
00727        nsIFileSpec   *      inFile;
00728     if (NS_FAILED( pSource->GetFileSpec( &inFile))) {
00729               ImportEudoraMailImpl::ReportError( EUDORAIMPORT_ADDRESS_BADSOURCEFILE, name, &error);
00730               ImportEudoraMailImpl::SetLogs( success, error, pErrorLog, pSuccessLog);             
00731        return( NS_ERROR_FAILURE);
00732     }
00733 
00734 #ifdef IMPORT_DEBUG
00735        char *pPath;
00736        inFile->GetNativePath( &pPath);    
00737        IMPORT_LOG1( "Import address book: %s\n", pPath);
00738        nsCRT::free( pPath);
00739 #endif
00740        
00741            
00742     nsresult rv = NS_OK;
00743        
00744        m_bytes = 0;
00745        rv = m_eudora.ImportAddresses( &m_bytes, &abort, name.get(), inFile, pDestination, error);
00746 
00747     inFile->Release();
00748 
00749            
00750     
00751        if (NS_SUCCEEDED( rv) && error.IsEmpty()) {
00752               ReportSuccess( name, &success);
00753        }
00754        else {
00755               ImportEudoraMailImpl::ReportError( EUDORAIMPORT_ADDRESS_CONVERTERROR, name, &error);
00756        }
00757 
00758        ImportEudoraMailImpl::SetLogs( success, error, pErrorLog, pSuccessLog);
00759 
00760        IMPORT_LOG0( "*** Returning from eudora address import\n");
00761 
00762     return( rv);
00763 }
00764 
00765        
00766 NS_IMETHODIMP ImportEudoraAddressImpl::GetImportProgress(PRUint32 *_retval)
00767 {
00768     NS_PRECONDITION(_retval != nsnull, "null ptr");
00769        if (!_retval)
00770               return( NS_ERROR_NULL_POINTER);
00771        
00772        *_retval = m_bytes;
00773 
00774        return( NS_OK);
00775 }
00776 
00777 
00778