Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
nsImportMail.cpp File Reference
#include "prthread.h"
#include "prprf.h"
#include "nscore.h"
#include "nsCOMPtr.h"
#include "nsISupportsArray.h"
#include "nsIImportMail.h"
#include "nsIImportGeneric.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsIImportMailboxDescriptor.h"
#include "nsCRT.h"
#include "nsString.h"
#include "nsUnicharUtils.h"
#include "nsIProxyObjectManager.h"
#include "nsXPIDLString.h"
#include "nsIFileSpec.h"
#include "nsIMsgAccountManager.h"
#include "nsIMessengerMigrator.h"
#include "nsIMsgMailSession.h"
#include "nsMsgBaseCID.h"
#include "nsIMsgFolder.h"
#include "nsImportStringBundle.h"
#include "nsIStringBundle.h"
#include "nsTextFormatter.h"
#include "nsIImportService.h"
#include "ImportDebug.h"

Go to the source code of this file.

Classes

class  nsImportGenericMail
class  ImportThreadData

Defines

#define IMPORT_MSGS_URL   "chrome://messenger/locale/importMsgs.properties"

Functions

static NS_DEFINE_CID (kProxyObjectManagerCID, NS_PROXYEVENT_MANAGER_CID)
static NS_DEFINE_CID (kSupportsWStringCID, NS_SUPPORTS_STRING_CID)
 PR_STATIC_CALLBACK (void) ImportMailThread(void *stuff)
nsresult NS_NewGenericMail (nsIImportGeneric **aImportGeneric)
 ImportMailThread (void *stuff)

Variables

static nsCOMPtr< nsIImportServicegService

Define Documentation

#define IMPORT_MSGS_URL   "chrome://messenger/locale/importMsgs.properties"

Definition at line 79 of file nsImportMail.cpp.


Function Documentation

ImportMailThread ( void stuff)

Definition at line 735 of file nsImportMail.cpp.

{
       ImportThreadData *pData = (ImportThreadData *)stuff;
       
  IMPORT_LOG0("ImportMailThread: Starting...");
              
       nsresult rv = NS_OK;

       nsCOMPtr<nsIMsgFolder>      destRoot( pData->destRoot);        
                            
       PRUint32      count = 0;
       rv = pData->boxes->Count( &count);
       
       PRUint32             i;
       PRBool               import;
       PRUint32             size;  
       PRUint32             depth = 1;
       PRUint32             newDepth;
       nsString             lastName;
       PRUnichar *          pName;
       
       nsCOMPtr<nsIMsgFolder>      curFolder( destRoot);
       nsCOMPtr<nsIMsgFolder>             curProxy;

       nsCOMPtr<nsIMsgFolder>             newFolder;
       nsCOMPtr<nsIFileSpec>              outBox;
       nsCOMPtr<nsISupports>              subFolder;
       nsCOMPtr<nsIEnumerator>     enumerator;

       PRBool                                    exists;
       
       nsString      success;
       nsString      error;

       nsCOMPtr<nsIStringBundle>   bundle( dont_AddRef( nsImportStringBundle::GetStringBundleProxy()));

       // Initialize the curFolder proxy object
       nsCOMPtr<nsIProxyObjectManager> proxyMgr = 
                do_GetService(kProxyObjectManagerCID, &rv);
       if (NS_SUCCEEDED(rv)) {
              rv = proxyMgr->GetProxyForObject( NS_UI_THREAD_EVENTQ, NS_GET_IID(nsIMsgFolder),
                                                                      curFolder, PROXY_SYNC | PROXY_ALWAYS, getter_AddRefs( curProxy));

              if (NS_SUCCEEDED(rv))
                     // GetSubfolders() will initialize folders if they are not already initialized.
                     curProxy->GetSubFolders(getter_AddRefs(enumerator));
    else
      IMPORT_LOG1( "*** ImportMailThread: Can't get the destination root folder proxy. rv=(0x%lx)", (long) rv);
       }
       else {
    IMPORT_LOG0("*** ImportMailThread: Unable to obtain proxy service to do the import.");
              nsImportStringBundle::GetStringByID( IMPORT_ERROR_MB_NOPROXY, error, bundle);
              pData->abort = PR_TRUE;
       }


  IMPORT_LOG1("ImportMailThread: Total number of folders to import = %d.", count);

  // Note that the front-end js script only displays one import result string so 
  // we combine both good and bad import status into one string (in var 'success').

       for (i = 0; (i < count) && !(pData->abort); i++) {
              nsCOMPtr<nsIImportMailboxDescriptor> box =
                     do_QueryElementAt(pData->boxes, i);
              if (box) {
                     pData->currentMailbox = i;

                     import = PR_FALSE;
                     size = 0;
                     rv = box->GetImport( &import);
                     if (import)
                            rv = box->GetSize( &size);
                     rv = box->GetDepth( &newDepth);
                     if (newDepth > depth) {
          // OK, we are going to add a subfolder under the last/previous folder we processed, so
          // find this folder (stored in 'lastName') who is going to be the new parent folder.
        IMPORT_LOG1("ImportMailThread: Processing child folder '%s'.", NS_ConvertUCS2toUTF8(lastName).get());
                            rv = curProxy->GetChildNamed( lastName.get(), getter_AddRefs( subFolder));
                            if (NS_FAILED( rv)) {
          IMPORT_LOG1("*** ImportMailThread: Failed to get the interface for child folder '%s'.", NS_ConvertUCS2toUTF8(lastName).get());
                                   nsImportGenericMail::ReportError( IMPORT_ERROR_MB_FINDCHILD, lastName.get(), &error);
                                   pData->fatalError = PR_TRUE;
                                   break;
                            }

                            rv = proxyMgr->GetProxyForObject( NS_UI_THREAD_EVENTQ, NS_GET_IID(nsIMsgFolder), 
                                                                                    subFolder, PROXY_SYNC | PROXY_ALWAYS, getter_AddRefs( curProxy));
                            if (NS_FAILED( rv)) {
          IMPORT_LOG1("*** ImportMailThread: Failed to get the proxy interface for child folder '%s'.", NS_ConvertUCS2toUTF8(lastName).get());
                                   nsImportStringBundle::GetStringByID( IMPORT_ERROR_MB_NOPROXY, error, bundle);
                                   pData->fatalError = PR_TRUE;
                                   break;
                            }

                            // Make sure this new parent folder obj has the correct subfolder list so far.
                            rv = curProxy->GetSubFolders(getter_AddRefs(enumerator));
                     }
                     else if (newDepth < depth) {
                            rv = NS_OK;
                            while ((newDepth < depth) && NS_SUCCEEDED( rv)) {
                                   nsCOMPtr<nsIMsgFolder> parFolder;
                                   rv = curProxy->GetParent( getter_AddRefs( parFolder));
          if (NS_FAILED( rv)) {
            IMPORT_LOG1("*** ImportMailThread: Failed to get the interface for parent folder '%s'.", lastName.get());
                                     nsImportGenericMail::ReportError( IMPORT_ERROR_MB_FINDCHILD, lastName.get(), &error);
                                     pData->fatalError = PR_TRUE;
                                     break;
                              }

                                   rv = proxyMgr->GetProxyForObject( NS_UI_THREAD_EVENTQ, NS_GET_IID(nsIMsgFolder),
                                                                                           parFolder, PROXY_SYNC | PROXY_ALWAYS, getter_AddRefs( curProxy));
                                   depth--;
                            }
                            if (NS_FAILED( rv)) {
          IMPORT_LOG1("*** ImportMailThread: Failed to get the proxy interface for parent folder '%s'.", lastName.get());
                                   nsImportStringBundle::GetStringByID( IMPORT_ERROR_MB_NOPROXY, error, bundle);
                                   pData->fatalError = PR_TRUE;
                                   break;
                            }
                     }
                     depth = newDepth;
                     pName = nsnull;
                     box->GetDisplayName( &pName);
                     if (pName) {
                            lastName = pName;
                            nsCRT::free( pName);
                     }
                     else
                            lastName.AssignLiteral("Unknown!");
                            
            // translate the folder name if we are doing migration
            if (pData->performingMigration)
                pData->mailImport->TranslateFolderName(lastName, lastName); 
                            
                     exists = PR_FALSE;
                     rv = curProxy->ContainsChildNamed( lastName.get(), &exists);
      
            // If we are performing profile migration (as opposed to importing) then we are starting
            // with empty local folders. In that case, always choose to over-write the existing local folder
            // with this name. Don't create a unique subfolder name. Otherwise you end up with "Inbox, Inbox0" 
            // or "Unsent Folders, UnsentFolders0"
            if (exists && !pData->performingMigration) {
                            nsXPIDLString subName;
                            curProxy->GenerateUniqueSubfolderName( lastName.get(), nsnull, getter_Copies(subName));
                            if (!subName.IsEmpty()) 
                                   lastName.Assign(subName);
                     }
                            
      IMPORT_LOG1("ImportMailThread: Creating new import folder '%s'.", NS_ConvertUCS2toUTF8(lastName).get());
            curProxy->CreateSubfolder( lastName.get(),nsnull); // this may fail if the folder already exists..that's ok

                            rv = curProxy->GetChildNamed( lastName.get(), getter_AddRefs( subFolder));
                            if (NS_SUCCEEDED( rv)) {
                                   newFolder = do_QueryInterface( subFolder);
                                   if (newFolder) {
                                          newFolder->GetPath( getter_AddRefs( outBox));
                                   }
                                   else {
            IMPORT_LOG1("*** ImportMailThread: Failed to locate subfolder interface '%s'.", lastName.get());
                                   }
                            }
        else
          IMPORT_LOG1("*** ImportMailThread: Failed to locate subfolder '%s' after it's been created.", lastName.get());
                            
                     if (NS_FAILED( rv)) {
                            nsImportGenericMail::ReportError( IMPORT_ERROR_MB_CREATE, lastName.get(), &error);
                     }

                     if (size && import && newFolder && outBox && NS_SUCCEEDED( rv)) {
                            PRBool fatalError = PR_FALSE;
                            pData->currentSize = size;
                            PRUnichar *pSuccess = nsnull;
                            PRUnichar *pError = nsnull;
                            rv = pData->mailImport->ImportMailbox( box, outBox, &pError, &pSuccess, &fatalError);
                            if (pError) {
                                   error.Append( pError);
                                   nsCRT::free( pError);
                            }
                            if (pSuccess) {
                                   success.Append( pSuccess);
                                   nsCRT::free( pSuccess);
                            }

                            pData->currentSize = 0;
                            pData->currentTotal += size;
                                   
                            outBox->CloseStream();

          // OK, we've copied the actual folder/file over if the folder size is not 0
          // (ie, the msg summary is no longer valid) so close the msg database so that
          // when the folder is reopened the folder db can be reconstructed (which
          // validates msg summary and forces folder to be reparsed).
                            newFolder->ForceDBClosed();

                            if (fatalError) {
                                   IMPORT_LOG1( "*** ImportMailThread: ImportMailbox returned fatalError, mailbox #%d\n", (int) i);
                                   pData->fatalError = PR_TRUE;
                                   break;
                            }
                     }
              }
       }

       // Now save the new acct info to pref file.
       nsCOMPtr <nsIMsgAccountManager> accMgr = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
        if (NS_SUCCEEDED(rv) && accMgr) {
         rv = accMgr->SaveAccountInfo();
         NS_ASSERTION(NS_SUCCEEDED(rv), "Can't save account info to pref file");
       }
       
       nsImportGenericMail::SetLogs( success, error, pData->successLog, pData->errorLog);

       if (pData->abort || pData->fatalError) {
              IMPORT_LOG0( "*** ImportMailThread: Abort or fatalError flag was set\n");
              if (pData->ownsDestRoot) {
                     IMPORT_LOG0( "Calling destRoot->RecursiveDelete\n");
                     destRoot->RecursiveDelete( PR_TRUE, nsnull);
              }
              else {
                     // FIXME: just delete the stuff we created?
              }
       }

       IMPORT_LOG1( "Import mailbox thread done: %d\n", (int) pData->currentTotal);

       pData->ThreadDelete();      

}

Here is the call graph for this function:

Here is the caller graph for this function:

static NS_DEFINE_CID ( kProxyObjectManagerCID  ,
NS_PROXYEVENT_MANAGER_CID   
) [static]
static NS_DEFINE_CID ( kSupportsWStringCID  ,
NS_SUPPORTS_STRING_CID   
) [static]

Definition at line 178 of file nsImportMail.cpp.

{
    NS_PRECONDITION(aImportGeneric != nsnull, "null ptr");
    if (! aImportGeneric)
        return NS_ERROR_NULL_POINTER;
       
       nsImportGenericMail *pGen = new nsImportGenericMail();

       if (pGen == nsnull)
              return NS_ERROR_OUT_OF_MEMORY;

       NS_ADDREF( pGen);
       nsresult rv = pGen->QueryInterface( NS_GET_IID(nsIImportGeneric), (void **)aImportGeneric);
       NS_RELEASE( pGen);
    
    return( rv);
}

Here is the call graph for this function:

Definition at line 102 of file TestIPCLocks.cpp.

{
  const char *lockName = (const char *) arg;

  LOG("entering TestThread [lock=%s]\n", lockName);

  nsresult rv;

  RandomSleep(1000, 1100);

  //LOG("done sleeping\n");

  rv = gLockService->AcquireLock(lockName, PR_TRUE);
  if (NS_SUCCEEDED(rv))
  {
    //LOG("acquired lock \"%s\"\n", lockName);
    RandomSleep(500, 1000);
    //LOG("releasing lock \"%s\"\n", lockName);
    rv = gLockService->ReleaseLock(lockName);
    if (NS_FAILED(rv))
    {
      LOG("failed to release lock [rv=%x]\n", rv);
      NS_ERROR("failed to release lock");
    }
  }
  else
  {
    LOG("failed to acquire lock [rv=%x]\n", rv);
    NS_NOTREACHED("failed to acquire lock");
  }

  LOG("exiting TestThread [lock=%s rv=%x]\n", lockName, rv);
}

Variable Documentation

Definition at line 88 of file nsImportMail.cpp.