Back to index

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