Back to index

lightning-sunbird  0.9+nobinonly
nsOutlookImport.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 "nsIComponentManager.h"
00057 #include "nsOutlookImport.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 "nsIImportFieldMap.h"
00066 #include "nsXPCOM.h"
00067 #include "nsISupportsPrimitives.h"
00068 #include "nsIOutputStream.h"
00069 #include "nsIAddrDatabase.h"
00070 #include "nsOutlookSettings.h"
00071 #include "nsTextFormatter.h"
00072 #include "nsOutlookStringBundle.h"
00073 #include "nsIStringBundle.h"
00074 #include "OutlookDebugLog.h"
00075 #include "nsUnicharUtils.h"
00076 
00077 #include "nsOutlookMail.h"
00078 
00079 #include "MapiApi.h"
00080 
00081 static NS_DEFINE_IID(kISupportsIID,                     NS_ISUPPORTS_IID);
00082 PRLogModuleInfo *OUTLOOKLOGMODULE = nsnull;
00083 
00084 class ImportOutlookMailImpl : public nsIImportMail
00085 {
00086 public:
00087     ImportOutlookMailImpl();
00088     virtual ~ImportOutlookMailImpl();
00089 
00090        static nsresult Create(nsIImportMail** aImport);
00091 
00092     // nsISupports interface
00093     NS_DECL_ISUPPORTS
00094 
00095     // nsIImportmail interface
00096   
00097        /* void GetDefaultLocation (out nsIFileSpec location, out boolean found, out boolean userVerify); */
00098        NS_IMETHOD GetDefaultLocation(nsIFileSpec **location, PRBool *found, PRBool *userVerify);
00099        
00100        /* nsISupportsArray FindMailboxes (in nsIFileSpec location); */
00101        NS_IMETHOD FindMailboxes(nsIFileSpec *location, nsISupportsArray **_retval);
00102        
00103        /* void ImportMailbox (in nsIImportMailboxDescriptor source, in nsIFileSpec destination, out boolean fatalError); */
00104        NS_IMETHOD ImportMailbox(nsIImportMailboxDescriptor *source, nsIFileSpec *destination, 
00105                                                         PRUnichar **pErrorLog, PRUnichar **pSuccessLog, PRBool *fatalError);
00106        
00107        /* unsigned long GetImportProgress (); */
00108        NS_IMETHOD GetImportProgress(PRUint32 *_retval);
00109        
00110     NS_IMETHOD TranslateFolderName(const nsAString & aFolderName, nsAString & _retval);
00111 
00112 public:
00113        static void   ReportSuccess( nsString& name, PRInt32 count, nsString *pStream);
00114        static void ReportError( PRInt32 errorNum, nsString& name, nsString *pStream);
00115        static void   AddLinebreak( nsString *pStream);
00116        static void   SetLogs( nsString& success, nsString& error, PRUnichar **pError, PRUnichar **pSuccess);
00117 
00118 private:
00119        nsOutlookMail m_mail;
00120        PRUint32             m_bytesDone;
00121 };
00122 
00123 
00124 class ImportOutlookAddressImpl : public nsIImportAddressBooks
00125 {
00126 public:
00127     ImportOutlookAddressImpl();
00128     virtual ~ImportOutlookAddressImpl();
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_TRUE; 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 *location, 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 GetFieldMap (in nsIImportABDescriptor source); */
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        void   ReportSuccess( nsString& name, nsString *pStream);
00176 
00177 private:
00178        PRUint32             m_msgCount;
00179        PRUint32             m_msgTotal;
00180        nsOutlookMail m_address;
00181 };
00183 
00184 
00186 
00187 
00188 nsOutlookImport::nsOutlookImport()
00189 {
00190   // Init logging module.
00191   if (!OUTLOOKLOGMODULE)
00192     OUTLOOKLOGMODULE = PR_NewLogModule("IMPORT");
00193 
00194        IMPORT_LOG0( "nsOutlookImport Module Created\n");
00195 
00196        nsOutlookStringBundle::GetStringBundle();
00197 }
00198 
00199 
00200 nsOutlookImport::~nsOutlookImport()
00201 {
00202 
00203        IMPORT_LOG0( "nsOutlookImport Module Deleted\n");
00204 
00205 }
00206 
00207 
00208 
00209 NS_IMPL_ISUPPORTS1(nsOutlookImport, nsIImportModule)
00210 
00211 
00212 NS_IMETHODIMP nsOutlookImport::GetName( PRUnichar **name)
00213 {
00214     NS_PRECONDITION(name != nsnull, "null ptr");
00215     if (! name)
00216         return NS_ERROR_NULL_POINTER;
00217 
00218        // nsString   title = "Outlook Express";
00219        // *name = ToNewUnicode(title);
00220        *name = nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_NAME);
00221               
00222     return NS_OK;
00223 }
00224 
00225 NS_IMETHODIMP nsOutlookImport::GetDescription( PRUnichar **name)
00226 {
00227     NS_PRECONDITION(name != nsnull, "null ptr");
00228     if (! name)
00229         return NS_ERROR_NULL_POINTER;
00230 
00231        // nsString   desc = "Outlook Express mail and address books";
00232        // *name = ToNewUnicode(desc);
00233        *name = nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_DESCRIPTION);
00234               
00235     return NS_OK;
00236 }
00237 
00238 NS_IMETHODIMP nsOutlookImport::GetSupports( char **supports)
00239 {
00240     NS_PRECONDITION(supports != nsnull, "null ptr");
00241     if (! supports)
00242         return NS_ERROR_NULL_POINTER;
00243        
00244        *supports = nsCRT::strdup( kOutlookSupportsString);
00245        return( NS_OK);
00246 }
00247 
00248 NS_IMETHODIMP nsOutlookImport::GetSupportsUpgrade( PRBool *pUpgrade)
00249 {
00250     NS_PRECONDITION(pUpgrade != nsnull, "null ptr");
00251     if (! pUpgrade)
00252         return NS_ERROR_NULL_POINTER;
00253        
00254        *pUpgrade = PR_TRUE;
00255        return( NS_OK);
00256 }
00257 
00258 NS_IMETHODIMP nsOutlookImport::GetImportInterface( const char *pImportType, nsISupports **ppInterface)
00259 {
00260     NS_PRECONDITION(pImportType != nsnull, "null ptr");
00261     if (! pImportType)
00262         return NS_ERROR_NULL_POINTER;
00263     NS_PRECONDITION(ppInterface != nsnull, "null ptr");
00264     if (! ppInterface)
00265         return NS_ERROR_NULL_POINTER;
00266 
00267        *ppInterface = nsnull;
00268        nsresult      rv;
00269        if (!nsCRT::strcmp( pImportType, "mail")) {
00270               // create the nsIImportMail interface and return it!
00271               nsIImportMail *      pMail = nsnull;
00272               nsIImportGeneric *pGeneric = nsnull;
00273               rv = ImportOutlookMailImpl::Create( &pMail);
00274               if (NS_SUCCEEDED( rv)) {
00275                      nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
00276                      if (NS_SUCCEEDED( rv)) {
00277                             rv = impSvc->CreateNewGenericMail( &pGeneric);
00278                             if (NS_SUCCEEDED( rv)) {
00279                                    pGeneric->SetData( "mailInterface", pMail);
00280                                    nsString name;
00281                                    nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_NAME, name);
00282                                    nsCOMPtr<nsISupportsString> nameString (do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv));
00283                                    if (NS_SUCCEEDED(rv)) {
00284                                           nameString->SetData(name);
00285                                           pGeneric->SetData( "name", nameString);
00286                                           rv = pGeneric->QueryInterface( kISupportsIID, (void **)ppInterface);
00287                                    }
00288                             }
00289                      }
00290               }
00291               NS_IF_RELEASE( pMail);
00292               NS_IF_RELEASE( pGeneric);
00293               return( rv);
00294        }
00295        
00296        if (!nsCRT::strcmp( pImportType, "addressbook")) {
00297               // create the nsIImportMail interface and return it!
00298               nsIImportAddressBooks *     pAddress = nsnull;
00299               nsIImportGeneric *          pGeneric = nsnull;
00300               rv = ImportOutlookAddressImpl::Create( &pAddress);
00301               if (NS_SUCCEEDED( rv)) {
00302                      nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
00303                      if (NS_SUCCEEDED( rv)) {
00304                             rv = impSvc->CreateNewGenericAddressBooks( &pGeneric);
00305                             if (NS_SUCCEEDED( rv)) {
00306                                    pGeneric->SetData( "addressInterface", pAddress);
00307                                    rv = pGeneric->QueryInterface( kISupportsIID, (void **)ppInterface);
00308                             }
00309                      }
00310               }
00311               NS_IF_RELEASE( pAddress);
00312               NS_IF_RELEASE( pGeneric);
00313               return( rv);
00314        }
00315        
00316        if (!nsCRT::strcmp( pImportType, "settings")) {
00317               nsIImportSettings *pSettings = nsnull;
00318               rv = nsOutlookSettings::Create( &pSettings);
00319               if (NS_SUCCEEDED( rv)) {
00320                      pSettings->QueryInterface( kISupportsIID, (void **)ppInterface);
00321               }
00322               NS_IF_RELEASE( pSettings);
00323               return( rv);
00324        }
00325               
00326        return( NS_ERROR_NOT_AVAILABLE);
00327 }
00328 
00330 nsresult ImportOutlookMailImpl::Create(nsIImportMail** aImport)
00331 {
00332     NS_PRECONDITION(aImport != nsnull, "null ptr");
00333     if (! aImport)
00334         return NS_ERROR_NULL_POINTER;
00335 
00336     *aImport = new ImportOutlookMailImpl();
00337     if (! *aImport)
00338         return NS_ERROR_OUT_OF_MEMORY;
00339 
00340     NS_ADDREF(*aImport);
00341     return NS_OK;
00342 }
00343 
00344 ImportOutlookMailImpl::ImportOutlookMailImpl()
00345 {
00346 }
00347 
00348 
00349 ImportOutlookMailImpl::~ImportOutlookMailImpl()
00350 {
00351 }
00352 
00353 
00354 
00355 NS_IMPL_THREADSAFE_ISUPPORTS1(ImportOutlookMailImpl, nsIImportMail)
00356 
00357 NS_IMETHODIMP ImportOutlookMailImpl::GetDefaultLocation( nsIFileSpec **ppLoc, PRBool *found, PRBool *userVerify)
00358 {
00359     NS_PRECONDITION(ppLoc != nsnull, "null ptr");
00360     NS_PRECONDITION(found != nsnull, "null ptr");
00361     NS_PRECONDITION(userVerify != nsnull, "null ptr");
00362     if (!ppLoc || !found || !userVerify)
00363         return NS_ERROR_NULL_POINTER;
00364        
00365 
00366        *found = PR_FALSE;
00367        *ppLoc = nsnull;
00368        *userVerify = PR_FALSE;
00369        // We need to verify here that we can get the mail, if true then
00370        // return a dummy location, otherwise return no location
00371        CMapiApi      mapi;
00372        if (!mapi.Initialize())
00373               return( NS_OK);
00374        if (!mapi.LogOn())
00375               return( NS_OK);
00376 
00377        CMapiFolderList      store;
00378        if (!mapi.IterateStores( store))
00379               return( NS_OK);
00380 
00381        if (store.GetSize() == 0)
00382               return( NS_OK);
00383 
00384 
00385        nsresult      rv;
00386        nsIFileSpec * spec;
00387        if (NS_FAILED( rv = NS_NewFileSpec( &spec)))
00388               return( rv);
00389        
00390        *found = PR_TRUE;
00391        *ppLoc = spec;
00392        *userVerify = PR_FALSE;
00393 
00394        return( NS_OK);
00395 }
00396 
00397 
00398 NS_IMETHODIMP ImportOutlookMailImpl::FindMailboxes( nsIFileSpec *pLoc, nsISupportsArray **ppArray)
00399 {
00400     NS_PRECONDITION(pLoc != nsnull, "null ptr");
00401     NS_PRECONDITION(ppArray != nsnull, "null ptr");
00402     if (!pLoc || !ppArray)
00403         return NS_ERROR_NULL_POINTER;
00404        
00405        return( m_mail.GetMailFolders( ppArray));
00406 }
00407 
00408 void ImportOutlookMailImpl::AddLinebreak( nsString *pStream)
00409 {
00410        if (pStream)
00411               pStream->Append( PRUnichar(nsCRT::LF));
00412 }
00413 
00414 void ImportOutlookMailImpl::ReportSuccess( nsString& name, PRInt32 count, nsString *pStream)
00415 {
00416        if (!pStream)
00417               return;
00418        // load the success string
00419        nsIStringBundle *pBundle = nsOutlookStringBundle::GetStringBundleProxy();
00420        PRUnichar *pFmt = nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_MAILBOX_SUCCESS, pBundle);
00421        PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get(), count);
00422        pStream->Append( pText);
00423        nsTextFormatter::smprintf_free( pText);
00424        nsOutlookStringBundle::FreeString( pFmt);
00425        AddLinebreak( pStream);
00426        NS_IF_RELEASE( pBundle);
00427 }
00428 
00429 void ImportOutlookMailImpl::ReportError( PRInt32 errorNum, nsString& name, nsString *pStream)
00430 {
00431        if (!pStream)
00432               return;
00433        // load the error string
00434        nsIStringBundle *pBundle = nsOutlookStringBundle::GetStringBundleProxy();
00435        PRUnichar *pFmt = nsOutlookStringBundle::GetStringByID( errorNum);
00436        PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get());
00437        pStream->Append( pText);
00438        nsTextFormatter::smprintf_free( pText);
00439        nsOutlookStringBundle::FreeString( pFmt);
00440        AddLinebreak( pStream);
00441        NS_IF_RELEASE( pBundle);
00442 }
00443 
00444 
00445 void ImportOutlookMailImpl::SetLogs( nsString& success, nsString& error, PRUnichar **pError, PRUnichar **pSuccess)
00446 {
00447        if (pError)
00448               *pError = ToNewUnicode(error);
00449        if (pSuccess)
00450               *pSuccess = ToNewUnicode(success);
00451 }
00452 
00453 NS_IMETHODIMP ImportOutlookMailImpl::ImportMailbox(     nsIImportMailboxDescriptor *pSource, 
00454                                                                                     nsIFileSpec *pDestination, 
00455                                                                                     PRUnichar **pErrorLog,
00456                                                                                     PRUnichar **pSuccessLog,
00457                                                                                     PRBool *fatalError)
00458 {
00459     NS_PRECONDITION(pSource != nsnull, "null ptr");
00460     NS_PRECONDITION(pDestination != nsnull, "null ptr");
00461     NS_PRECONDITION(fatalError != nsnull, "null ptr");
00462        
00463        nsCOMPtr<nsIStringBundle>   bundle( dont_AddRef( nsOutlookStringBundle::GetStringBundleProxy()));
00464 
00465        nsString      success;
00466        nsString      error;
00467     if (!pSource || !pDestination || !fatalError) {
00468               nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_MAILBOX_BADPARAM, error, bundle);
00469               if (fatalError)
00470                      *fatalError = PR_TRUE;
00471               SetLogs( success, error, pErrorLog, pSuccessLog);
00472            return NS_ERROR_NULL_POINTER;
00473        }
00474       
00475     PRBool           abort = PR_FALSE;
00476     nsString  name;
00477     PRUnichar *      pName;
00478     if (NS_SUCCEEDED( pSource->GetDisplayName( &pName))) {
00479        name = pName;
00480        nsCRT::free( pName);
00481     }
00482     
00483        PRUint32 mailSize = 0;
00484        pSource->GetSize( &mailSize);
00485        if (mailSize == 0) {
00486               ReportSuccess( name, 0, &success);
00487               SetLogs( success, error, pErrorLog, pSuccessLog);
00488               return( NS_OK);
00489        }
00490        
00491        PRUint32 index = 0;
00492        pSource->GetIdentifier( &index);
00493        PRInt32       msgCount = 0;
00494     nsresult rv = NS_OK;
00495 
00496        m_bytesDone = 0;
00497 
00498        rv = m_mail.ImportMailbox( &m_bytesDone, &abort, (PRInt32)index, name.get(), pDestination, &msgCount);
00499     
00500        if (NS_SUCCEEDED( rv)) {
00501               ReportSuccess( name, msgCount, &success);
00502        }
00503        else {
00504               ReportError( OUTLOOKIMPORT_MAILBOX_CONVERTERROR, name, &error);
00505        }
00506 
00507        SetLogs( success, error, pErrorLog, pSuccessLog);
00508 
00509     return( rv);
00510 }
00511 
00512 
00513 NS_IMETHODIMP ImportOutlookMailImpl::GetImportProgress( PRUint32 *pDoneSoFar) 
00514 { 
00515     NS_PRECONDITION(pDoneSoFar != nsnull, "null ptr");
00516     if (! pDoneSoFar)
00517         return NS_ERROR_NULL_POINTER;
00518        
00519        *pDoneSoFar = m_bytesDone;
00520        return( NS_OK);
00521 }
00522 
00523 NS_IMETHODIMP ImportOutlookMailImpl::TranslateFolderName(const nsAString & aFolderName, nsAString & _retval)
00524 {
00525     if (aFolderName.Equals(NS_LITERAL_STRING("Deleted Items"), nsCaseInsensitiveStringComparator()))
00526         _retval = NS_LITERAL_STRING(kDestTrashFolderName);
00527     else if (aFolderName.Equals(NS_LITERAL_STRING("Sent Items"), nsCaseInsensitiveStringComparator()))
00528         _retval = NS_LITERAL_STRING(kDestSentFolderName);
00529     else if (aFolderName.Equals(NS_LITERAL_STRING("Outbox"), nsCaseInsensitiveStringComparator()))
00530         _retval = NS_LITERAL_STRING(kDestUnsentMessagesFolderName);
00531     else
00532   _retval = aFolderName; 
00533   return NS_OK;
00534 }
00535 
00536 
00537 nsresult ImportOutlookAddressImpl::Create(nsIImportAddressBooks** aImport)
00538 {
00539     NS_PRECONDITION(aImport != nsnull, "null ptr");
00540     if (! aImport)
00541         return NS_ERROR_NULL_POINTER;
00542 
00543     *aImport = new ImportOutlookAddressImpl();
00544     if (! *aImport)
00545         return NS_ERROR_OUT_OF_MEMORY;
00546 
00547     NS_ADDREF(*aImport);
00548     return NS_OK;
00549 }
00550 
00551 ImportOutlookAddressImpl::ImportOutlookAddressImpl()
00552 {
00553        m_msgCount = 0;
00554        m_msgTotal = 0;
00555 }
00556 
00557 
00558 ImportOutlookAddressImpl::~ImportOutlookAddressImpl()
00559 {
00560 }
00561 
00562 
00563 
00564 NS_IMPL_THREADSAFE_ISUPPORTS1(ImportOutlookAddressImpl, nsIImportAddressBooks)
00565 
00566        
00567 NS_IMETHODIMP ImportOutlookAddressImpl::GetAutoFind(PRUnichar **description, PRBool *_retval)
00568 {
00569     NS_PRECONDITION(description != nsnull, "null ptr");
00570     NS_PRECONDITION(_retval != nsnull, "null ptr");
00571     if (! description || !_retval)
00572         return NS_ERROR_NULL_POINTER;
00573     
00574     *_retval = PR_TRUE;
00575     nsString str;
00576        nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_ADDRNAME, str);
00577        *description = ToNewUnicode(str);
00578     
00579     return( NS_OK);
00580 }
00581 
00582        
00583        
00584 NS_IMETHODIMP ImportOutlookAddressImpl::FindAddressBooks(nsIFileSpec *location, nsISupportsArray **_retval)
00585 {
00586     NS_PRECONDITION(_retval != nsnull, "null ptr");
00587     if (!_retval)
00588         return NS_ERROR_NULL_POINTER;
00589        
00590        return( m_address.GetAddressBooks( _retval));
00591 }
00592 
00593        
00594        
00595 NS_IMETHODIMP ImportOutlookAddressImpl::ImportAddressBook(     nsIImportABDescriptor *source, 
00596                                                                                            nsIAddrDatabase *    destination, 
00597                                                                                            nsIImportFieldMap *  fieldMap, 
00598                                                                                            PRBool isAddrLocHome, 
00599                                                                                            PRUnichar **         pErrorLog,
00600                                                                                            PRUnichar **         pSuccessLog,
00601                                                                                            PRBool *                    fatalError)
00602 {
00603        m_msgCount = 0;
00604        m_msgTotal = 0;
00605     NS_PRECONDITION(source != nsnull, "null ptr");
00606     NS_PRECONDITION(destination != nsnull, "null ptr");
00607        NS_PRECONDITION(fatalError != nsnull, "null ptr");
00608        
00609        nsCOMPtr<nsIStringBundle> bundle( dont_AddRef( nsOutlookStringBundle::GetStringBundleProxy()));
00610 
00611        nsString      success;
00612        nsString      error;
00613     if (!source || !destination || !fatalError) {
00614               IMPORT_LOG0( "*** Bad param passed to outlook address import\n");
00615               nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_ADDRESS_BADPARAM, error, bundle);
00616               if (fatalError)
00617                      *fatalError = PR_TRUE;
00618               ImportOutlookMailImpl::SetLogs( success, error, pErrorLog, pSuccessLog);
00619            return NS_ERROR_NULL_POINTER;
00620        }
00621       
00622     nsString  name;
00623     PRUnichar *      pName;
00624     if (NS_SUCCEEDED( source->GetPreferredName( &pName))) {
00625        name = pName;
00626        nsCRT::free( pName);
00627     }
00628     
00629     
00630        PRUint32      id;
00631     if (NS_FAILED( source->GetIdentifier( &id))) {
00632               ImportOutlookMailImpl::ReportError( OUTLOOKIMPORT_ADDRESS_BADSOURCEFILE, name, &error);
00633               ImportOutlookMailImpl::SetLogs( success, error, pErrorLog, pSuccessLog);            
00634        return( NS_ERROR_FAILURE);
00635     }
00636        
00637            
00638     nsresult rv = NS_OK;
00639        
00640        rv = m_address.ImportAddresses( &m_msgCount, &m_msgTotal, name.get(), id, destination, error);
00641     
00642        if (NS_SUCCEEDED( rv) && error.IsEmpty()) {
00643               ReportSuccess( name, &success);
00644        }
00645        else {
00646               ImportOutlookMailImpl::ReportError( OUTLOOKIMPORT_ADDRESS_CONVERTERROR, name, &error);
00647        }
00648 
00649        ImportOutlookMailImpl::SetLogs( success, error, pErrorLog, pSuccessLog);
00650 
00651        IMPORT_LOG0( "*** Returning from outlook address import\n");
00652 
00653   rv = destination->Commit(nsAddrDBCommitType::kLargeCommit);
00654        return rv;
00655 }
00656 
00657        
00658 NS_IMETHODIMP ImportOutlookAddressImpl::GetImportProgress(PRUint32 *_retval)
00659 {
00660     NS_PRECONDITION(_retval != nsnull, "null ptr");
00661     if (!_retval)
00662         return NS_ERROR_NULL_POINTER;
00663        
00664        PRUint32 result = m_msgCount;
00665        if (m_msgTotal) {
00666               result *= 100;
00667               result /= m_msgTotal;
00668        }
00669        else
00670               result = 0;
00671        
00672        if (result > 100)
00673               result = 100;
00674 
00675        *_retval = result;
00676        
00677        return( NS_OK);
00678 }
00679 
00680 void ImportOutlookAddressImpl::ReportSuccess( nsString& name, nsString *pStream)
00681 {
00682        if (!pStream)
00683               return;
00684        // load the success string
00685        nsIStringBundle *pBundle = nsOutlookStringBundle::GetStringBundleProxy();
00686        PRUnichar *pFmt = nsOutlookStringBundle::GetStringByID( OUTLOOKIMPORT_ADDRESS_SUCCESS, pBundle);
00687        PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get());
00688        pStream->Append( pText);
00689        nsTextFormatter::smprintf_free( pText);
00690        nsOutlookStringBundle::FreeString( pFmt);
00691        ImportOutlookMailImpl::AddLinebreak( pStream);
00692        NS_IF_RELEASE( pBundle);
00693 }