Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes
ImportAddressImpl Class Reference
Inheritance diagram for ImportAddressImpl:
Inheritance graph
[legend]
Collaboration diagram for ImportAddressImpl:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ImportAddressImpl ()
virtual ~ImportAddressImpl ()
NS_DECL_ISUPPORTS NS_IMETHOD GetSupportsMultiple (PRBool *_retval)
NS_IMETHOD GetAutoFind (PRUnichar **description, PRBool *_retval)
NS_IMETHOD GetNeedsFieldMap (nsIFileSpec *location, PRBool *_retval)
NS_IMETHOD GetDefaultLocation (nsIFileSpec **location, PRBool *found, PRBool *userVerify)
NS_IMETHOD FindAddressBooks (nsIFileSpec *location, nsISupportsArray **_retval)
NS_IMETHOD InitFieldMap (nsIFileSpec *location, nsIImportFieldMap *fieldMap)
NS_IMETHOD ImportAddressBook (nsIImportABDescriptor *source, nsIAddrDatabase *destination, nsIImportFieldMap *fieldMap, PRBool isAddrLocHome, PRUnichar **errorLog, PRUnichar **successLog, PRBool *fatalError)
NS_IMETHOD GetImportProgress (PRUint32 *_retval)
NS_IMETHOD GetSampleData (PRInt32 index, PRBool *pFound, PRUnichar **pStr)
NS_IMETHOD SetSampleLocation (nsIFileSpec *)
PRBool GetSupportsMultiple ()
PRBool GetAutoFind (out wstring description)
PRBool GetNeedsFieldMap (in nsIFileSpec location)
void GetDefaultLocation (out nsIFileSpec location, out boolean found, out boolean userVerify)
nsISupportsArray FindAddressBooks (in nsIFileSpec location)
void InitFieldMap (in nsIFileSpec location, in nsIImportFieldMap fieldMap)
void ImportAddressBook (in nsIImportABDescriptor source, in nsIAddrDatabase destination, in nsIImportFieldMap fieldMap, in boolean isAddrLocHome, out wstring errorLog, out wstring successLog, out boolean fatalError)
unsigned long GetImportProgress ()
void SetSampleLocation (in nsIFileSpec location)
wstring GetSampleData (in long recordNumber, out boolean recordExists)

Static Public Member Functions

static nsresult Create (nsIImportAddressBooks **aImport)

Private Member Functions

void ClearSampleFile (void)
void SaveFieldMap (nsIImportFieldMap *pMap)

Static Private Member Functions

static void ReportSuccess (nsString &name, nsString *pStream)
static void SetLogs (nsString &success, nsString &error, PRUnichar **pError, PRUnichar **pSuccess)
static void ReportError (PRInt32 errorNum, nsString &name, nsString *pStream)
static void SanitizeSampleData (nsCString &val)

Private Attributes

nsTextAddress m_text
PRBool m_haveDelim
nsIFileSpecm_fileLoc
char m_delim
PRUint32 m_bytesImported

Detailed Description

Definition at line 81 of file nsTextImport.cpp.


Constructor & Destructor Documentation

Definition at line 270 of file nsTextImport.cpp.

Here is the caller graph for this function:

Definition at line 277 of file nsTextImport.cpp.

{
       if (m_fileLoc) {
              PRBool open = PR_FALSE;
              m_fileLoc->IsStreamOpen( &open);
              if (open)
                     m_fileLoc->CloseStream();
              NS_RELEASE( m_fileLoc);
       }
}

Member Function Documentation

Definition at line 710 of file nsTextImport.cpp.

{
       if (m_fileLoc) {
              PRBool        open = PR_FALSE;
              m_fileLoc->IsStreamOpen( &open);
              if (open)
                     m_fileLoc->CloseStream();
              NS_RELEASE( m_fileLoc);
              m_fileLoc = nsnull;
              m_haveDelim = PR_FALSE;
       }
}

Here is the caller graph for this function:

Definition at line 255 of file nsTextImport.cpp.

{
    NS_PRECONDITION(aImport != nsnull, "null ptr");
    if (! aImport)
        return NS_ERROR_NULL_POINTER;

    *aImport = new ImportAddressImpl();
    if (! *aImport)
        return NS_ERROR_OUT_OF_MEMORY;

    NS_ADDREF(*aImport);

    return NS_OK;
}

Here is the call graph for this function:

Definition at line 326 of file nsTextImport.cpp.

{
    NS_PRECONDITION(pLoc != nsnull, "null ptr");
    NS_PRECONDITION(ppArray != nsnull, "null ptr");
    if (!pLoc || !ppArray)
        return NS_ERROR_NULL_POINTER;
       
       ClearSampleFile();

       *ppArray = nsnull;
       PRBool exists = PR_FALSE;
       nsresult rv = pLoc->Exists( &exists);
       if (NS_FAILED( rv) || !exists)
              return( NS_ERROR_FAILURE);
       
       PRBool isFile = PR_FALSE;
       rv = pLoc->IsFile( &isFile);
       if (NS_FAILED( rv) || !isFile)
              return( NS_ERROR_FAILURE);

       rv = m_text.DetermineDelim( pLoc);
       
       if (NS_FAILED( rv)) {
              IMPORT_LOG0( "*** Error determining delimitter\n");
              return( rv);
       }
       m_haveDelim = PR_TRUE;
       m_delim = m_text.GetDelim();
       
       m_fileLoc = pLoc;
       NS_ADDREF( m_fileLoc);

       /* Build an address book descriptor based on the file passed in! */
       nsCOMPtr<nsISupportsArray>  array;
       rv = NS_NewISupportsArray( getter_AddRefs( array));
       if (NS_FAILED( rv)) {
              IMPORT_LOG0( "FAILED to allocate the nsISupportsArray\n");
              return( rv);
       }
              
       nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));
       if (NS_FAILED( rv)) {
              IMPORT_LOG0( "*** Failed to obtain the import service\n");
              return( rv);
       }
       
       nsXPIDLCString pName;
       rv = pLoc->GetLeafName(getter_Copies(pName));
       if (NS_FAILED( rv)) {
              IMPORT_LOG0( "*** Failed getting leaf name of file\n");
              return( rv);
       }

       // for get unicode leafname.  If it uses nsILocalFile interface,
       // these codes do not need due to nsILocalFile->GetUnicodeLeafName()
       nsString      name;
       rv = impSvc->SystemStringToUnicode((const char*) pName, name);
       if (NS_FAILED(rv))
              name.AssignWithConversion((const char*) pName);

       PRInt32              idx = name.RFindChar( '.');
       if ((idx != -1) && (idx > 0) && ((name.Length() - idx - 1) < 5)) {
              nsString t;
              name.Left( t, idx);
              name = t;
       }

       nsCOMPtr<nsIImportABDescriptor>    desc;
       nsISupports *                             pInterface;

       rv = impSvc->CreateNewABDescriptor( getter_AddRefs( desc));
       if (NS_SUCCEEDED( rv)) {
              PRUint32 sz = 0;
              pLoc->GetFileSize( &sz);    
              desc->SetPreferredName( name.get());
              desc->SetSize( sz);
              nsIFileSpec *pSpec = nsnull;
              desc->GetFileSpec( &pSpec);
              if (pSpec) {
                     pSpec->FromFileSpec( pLoc);
                     NS_RELEASE( pSpec);
              }
              rv = desc->QueryInterface( kISupportsIID, (void **) &pInterface);
              array->AppendElement( pInterface);
              pInterface->Release();
       }
       if (NS_FAILED( rv)) {
              IMPORT_LOG0( "*** Error creating address book descriptor for text import\n");
       }             
       else {
              rv = array->QueryInterface( NS_GET_IID(nsISupportsArray), (void **) ppArray);
       }

       return( rv);
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP ImportAddressImpl::GetAutoFind ( PRUnichar **  description,
PRBool _retval 
)

Definition at line 293 of file nsTextImport.cpp.

{
    NS_PRECONDITION(addrDescription != nsnull, "null ptr");
    NS_PRECONDITION(_retval != nsnull, "null ptr");
    if (! addrDescription || !_retval)
        return NS_ERROR_NULL_POINTER;
    
    nsString  str;
    *_retval = PR_FALSE;
    nsTextStringBundle::GetStringByID( TEXTIMPORT_ADDRESS_NAME, str);
    *addrDescription = ToNewUnicode(str);
    
    return( NS_OK);
}

Here is the call graph for this function:

PRBool nsIImportAddressBooks::GetAutoFind ( out wstring  description) [inherited]

Here is the caller graph for this function:

NS_IMETHODIMP ImportAddressImpl::GetDefaultLocation ( nsIFileSpec **  location,
PRBool found,
PRBool userVerify 
)

Definition at line 309 of file nsTextImport.cpp.

{
    NS_PRECONDITION(found != nsnull, "null ptr");
    NS_PRECONDITION(ppLoc != nsnull, "null ptr");
    NS_PRECONDITION(userVerify != nsnull, "null ptr");
    if (! found || !userVerify || !ppLoc)
        return NS_ERROR_NULL_POINTER;
    
       *ppLoc = nsnull;
       *found = PR_FALSE;
       *userVerify = PR_TRUE;
       
       return( NS_OK);    
}
void nsIImportAddressBooks::GetDefaultLocation ( out nsIFileSpec  location,
out boolean  found,
out boolean  userVerify 
) [inherited]

Here is the caller graph for this function:

Definition at line 567 of file nsTextImport.cpp.

{
  NS_ENSURE_ARG_POINTER(_retval);
       
       *_retval = m_bytesImported;
       return NS_OK;
}

Here is the caller graph for this function:

Definition at line 576 of file nsTextImport.cpp.

{
  NS_ENSURE_ARG_POINTER(_retval);
  NS_ENSURE_ARG_POINTER(location);

       *_retval = PR_TRUE;
       PRBool exists = PR_FALSE;
       PRBool isFile = PR_FALSE;

       nsresult rv = location->Exists( &exists);
       rv = location->IsFile( &isFile);
       
       if (!exists || !isFile)
              return( NS_ERROR_FAILURE);

       PRBool isLDIF = PR_FALSE;
    nsCOMPtr<nsIAbLDIFService> ldifService = do_GetService(NS_ABLDIFSERVICE_CONTRACTID, &rv);

    if (NS_SUCCEEDED(rv))
      rv = ldifService->IsLDIFFile(location, &isLDIF);

       if (NS_FAILED( rv)) {
              IMPORT_LOG0( "*** Error determining if file is of type LDIF\n");
              return( rv);
       }
       
       if (isLDIF)
              *_retval = PR_FALSE;
       
       return( NS_OK);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 616 of file nsTextImport.cpp.

{
    NS_PRECONDITION(pFound != nsnull, "null ptr");
    NS_PRECONDITION(pStr != nsnull, "null ptr");
       if (!pFound || !pStr)
              return( NS_ERROR_NULL_POINTER);
       
       if (!m_fileLoc) {
              IMPORT_LOG0( "*** Error, called GetSampleData before SetSampleLocation\n");
              return( NS_ERROR_FAILURE);
       }

       nsresult      rv;
       *pStr = nsnull;
       PRBool        open = PR_FALSE;
       PRUnichar     term = 0;

       if (!m_haveDelim) {
              rv = m_fileLoc->IsStreamOpen( &open);
              if (open) {
                     m_fileLoc->CloseStream();
                     open = PR_FALSE;
              }
              rv = m_text.DetermineDelim( m_fileLoc);
              if (NS_FAILED( rv))
                     return( rv);
              m_haveDelim = PR_TRUE;
              m_delim = m_text.GetDelim();
       }
       else {
              rv = m_fileLoc->IsStreamOpen( &open);                   
       }
       
       if (!open) {
              rv = m_fileLoc->OpenStreamForReading();
              if (NS_FAILED( rv)) {
                     *pFound = PR_FALSE;
                     *pStr = nsCRT::strdup( &term);
                     return( NS_OK);
              }
       }
       
       PRInt32       lineLen;
       PRInt32       bufSz = 10240;
       char   *pLine = new char[bufSz];
       
       nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv));

       rv = nsTextAddress::ReadRecordNumber( m_fileLoc, pLine, bufSz, m_delim, &lineLen, index);
       if (NS_SUCCEEDED( rv)) {
              nsString      str;
              nsCString     field;
              nsString      uField;
              PRInt32              fNum = 0;
              while (nsTextAddress::GetField( pLine, lineLen, fNum, field, m_delim)) {
                     if (fNum)
                            str.AppendLiteral("\n");
                     SanitizeSampleData( field);
                     if (impSvc)
                            impSvc->SystemStringToUnicode( field.get(), uField);
                     else
                            uField.AssignWithConversion( field.get());

                     str.Append( uField);
                     fNum++;
                     field.Truncate();
              }

              *pStr = nsCRT::strdup( str.get());
              *pFound = PR_TRUE;

              /* IMPORT_LOG1( "Sample data: %S\n", str.get()); */
       }
       else {
              *pFound = PR_FALSE;
              *pStr = nsCRT::strdup( &term);
       }

       delete [] pLine;

       return( NS_OK);
}

Here is the call graph for this function:

wstring nsIImportAddressBooks::GetSampleData ( in long  recordNumber,
out boolean  recordExists 
) [inherited]

Here is the caller graph for this function:

Definition at line 95 of file nsTextImport.cpp.

{ *_retval = PR_FALSE; return( NS_OK);}
NS_IMETHODIMP ImportAddressImpl::ImportAddressBook ( nsIImportABDescriptor source,
nsIAddrDatabase destination,
nsIImportFieldMap fieldMap,
PRBool  isAddrLocHome,
PRUnichar **  errorLog,
PRUnichar **  successLog,
PRBool fatalError 
)

Definition at line 463 of file nsTextImport.cpp.

{
    NS_PRECONDITION(pSource != nsnull, "null ptr");
    NS_PRECONDITION(pDestination != nsnull, "null ptr");
    NS_PRECONDITION(fatalError != nsnull, "null ptr");
       
       nsCOMPtr<nsIStringBundle>   bundle( dont_AddRef( nsTextStringBundle::GetStringBundleProxy()));
       m_bytesImported = 0;

       nsString      success;
       nsString      error;
    if (!pSource || !pDestination || !fatalError) {
              IMPORT_LOG0( "*** Bad param passed to text address import\n");
              nsTextStringBundle::GetStringByID( TEXTIMPORT_ADDRESS_BADPARAM, error, bundle);
              if (fatalError)
                     *fatalError = PR_TRUE;
              SetLogs( success, error, pErrorLog, pSuccessLog);
           return( NS_ERROR_NULL_POINTER);
       }
   
       ClearSampleFile();

    PRBool           addrAbort = PR_FALSE;
    nsString  name;
    PRUnichar *      pName;
    if (NS_SUCCEEDED( pSource->GetPreferredName( &pName))) {
       name = pName;
       nsCRT::free( pName);
    }
    
       PRUint32 addressSize = 0;
       pSource->GetSize( &addressSize);
       if (addressSize == 0) {
              IMPORT_LOG0( "Address book size is 0, skipping import.\n");
              ReportSuccess( name, &success);
              SetLogs( success, error, pErrorLog, pSuccessLog);
              return( NS_OK);
       }

    
       nsIFileSpec   *      inFile;
    if (NS_FAILED( pSource->GetFileSpec( &inFile))) {
              ReportError( TEXTIMPORT_ADDRESS_BADSOURCEFILE, name, &error);
              SetLogs( success, error, pErrorLog, pSuccessLog);              
       return( NS_ERROR_FAILURE);
    }

  nsXPIDLCString pPath; 
  inFile->GetNativePath(getter_Copies(pPath));
       IMPORT_LOG1( "Importing address book: %s\n", pPath.get());
       
    nsresult rv = NS_OK;
       PRBool isLDIF = PR_FALSE;

    nsCOMPtr<nsIAbLDIFService> ldifService = do_GetService(NS_ABLDIFSERVICE_CONTRACTID, &rv);

    if (NS_SUCCEEDED(rv)) {
      rv = ldifService->IsLDIFFile(inFile, &isLDIF);
      if (NS_FAILED(rv)) {
        IMPORT_LOG0( "*** Error reading address file\n");
      }
    }

       if (NS_FAILED( rv)) {
              inFile->Release();
              ReportError( TEXTIMPORT_ADDRESS_CONVERTERROR, name, &error);
              SetLogs( success, error, pErrorLog, pSuccessLog);
              return( rv);
       }
       
    if (isLDIF) {
        if (ldifService)
          rv = ldifService->ImportLDIFFile(pDestination, inFile, PR_FALSE, &m_bytesImported);
        else
          return NS_ERROR_FAILURE;
       }
       else { 
              rv = m_text.ImportAddresses( &addrAbort, name.get(), inFile, pDestination, fieldMap, error, &m_bytesImported);
              SaveFieldMap( fieldMap);
       }

    inFile->Release();
           
    
       if (NS_SUCCEEDED( rv) && error.IsEmpty()) {
              ReportSuccess( name, &success);
       }
       else {
              ReportError( TEXTIMPORT_ADDRESS_CONVERTERROR, name, &error);
       }

       SetLogs( success, error, pErrorLog, pSuccessLog);

       IMPORT_LOG0( "*** Text address import done\n");
  return rv;
}

Here is the call graph for this function:

void nsIImportAddressBooks::ImportAddressBook ( in nsIImportABDescriptor  source,
in nsIAddrDatabase  destination,
in nsIImportFieldMap  fieldMap,
in boolean  isAddrLocHome,
out wstring  errorLog,
out wstring  successLog,
out boolean  fatalError 
) [inherited]

Definition at line 724 of file nsTextImport.cpp.

{
       // Let's remember the last one the user used!
       // This should be normal for someone importing multiple times, it's usually
       // from the same file format.
       
       nsresult rv;
       nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
       if (NS_SUCCEEDED( rv)) {
              nsXPIDLCString       prefStr;
              rv = prefs->GetCharPref( "mailnews.import.text.fieldmap", getter_Copies(prefStr));
              if (NS_SUCCEEDED( rv)) {
                     const char *pStr = (const char *)prefStr;
                     if (pStr) {
                            fieldMap->SetFieldMapSize( 0);
                            long          fNum;
                            PRBool        active;
                            long          fIndex = 0;
                            while (*pStr) {
                                   while (*pStr && (*pStr != '+') && (*pStr != '-'))
                                          pStr++;
                                   if (*pStr == '+')
                                          active = PR_TRUE;
                                   else if (*pStr == '-')
                                          active = PR_FALSE;
                                   else
                                          break;
                                   fNum = 0;
                                   while (*pStr && ((*pStr < '0') || (*pStr > '9')))
                                          pStr++;
                                   if (!(*pStr))
                                          break;
                                   while (*pStr && (*pStr >= '0') && (*pStr <= '9')) {
                                          fNum *= 10;
                                          fNum += (*pStr - '0');
                                          pStr++;
                                   }
                                   while (*pStr && (*pStr != ','))
                                          pStr++;
                                   if (*pStr == ',')
                                          pStr++;
                                   fieldMap->SetFieldMap( -1, fNum);
                                   fieldMap->SetFieldActive( fIndex, active);
                                   fIndex++;
                            }
                            if (!fIndex) {
                                   int num;
                                   fieldMap->GetNumMozFields( &num);
                                   fieldMap->DefaultFieldMap( num);
                            }
                     }
              }

        // Now also get the last used skip first record value.
        PRBool skipFirstRecord = PR_FALSE;
        rv = prefs->GetBoolPref("mailnews.import.text.skipfirstrecord", &skipFirstRecord);
        if (NS_SUCCEEDED(rv))
        {
          fieldMap->SetSkipFirstRecord(skipFirstRecord);
        }
       }

       return( NS_OK);
}

Here is the call graph for this function:

void nsIImportAddressBooks::InitFieldMap ( in nsIFileSpec  location,
in nsIImportFieldMap  fieldMap 
) [inherited]

Here is the caller graph for this function:

void ImportAddressImpl::ReportError ( PRInt32  errorNum,
nsString name,
nsString pStream 
) [static, private]

Definition at line 439 of file nsTextImport.cpp.

{
       if (!pStream)
              return;
       // load the error string
       nsIStringBundle *pBundle = nsTextStringBundle::GetStringBundleProxy();
       PRUnichar *pFmt = nsTextStringBundle::GetStringByID( errorNum, pBundle);
       PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get());
       pStream->Append( pText);
       nsTextFormatter::smprintf_free( pText);
       nsTextStringBundle::FreeString( pFmt);
       pStream->Append( PRUnichar(nsCRT::LF));
       NS_IF_RELEASE( pBundle);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ImportAddressImpl::ReportSuccess ( nsString name,
nsString pStream 
) [static, private]

Definition at line 424 of file nsTextImport.cpp.

{
       if (!pStream)
              return;
       // load the success string
       nsIStringBundle *pBundle = nsTextStringBundle::GetStringBundleProxy();
       PRUnichar *pFmt = nsTextStringBundle::GetStringByID( TEXTIMPORT_ADDRESS_SUCCESS, pBundle);
       PRUnichar *pText = nsTextFormatter::smprintf( pFmt, name.get());
       pStream->Append( pText);
       nsTextFormatter::smprintf_free( pText);
       nsTextStringBundle::FreeString( pFmt);
       pStream->Append( PRUnichar(nsCRT::LF));
       NS_IF_RELEASE( pBundle);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 608 of file nsTextImport.cpp.

{
       // remove any line-feeds...
       val.ReplaceSubstring( "\x0D\x0A", ", ");
       val.ReplaceChar( 13, ',');
       val.ReplaceChar( 10, ',');
}

Here is the caller graph for this function:

Definition at line 790 of file nsTextImport.cpp.

{
       if (!pMap)
              return;

       int                  size;
       int                  index;
       PRBool        active;
       nsCString     str;

       pMap->GetMapSize( &size);
       for (long i = 0; i < size; i++) {
              index = i;
              active = PR_FALSE;
              pMap->GetFieldMap( i, &index);
              pMap->GetFieldActive( i, &active);
              if (active)
                     str.Append( '+');
              else
                     str.Append( '-');

              str.AppendInt( index);
              str.Append( ',');
       }

       PRBool done = PR_FALSE;
       nsresult rv;

       nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));

       if (NS_SUCCEEDED( rv)) {
              nsXPIDLCString       prefStr;
              rv = prefs->GetCharPref( "mailnews.import.text.fieldmap", getter_Copies(prefStr));
              if (NS_SUCCEEDED( rv)) {
                     if (str.Equals(prefStr))
                            done = PR_TRUE;
              }
              if (!done) {
                     rv = prefs->SetCharPref( "mailnews.import.text.fieldmap", str.get());
              }
       }

    // Now also save last used skip first record value.
    PRBool skipFirstRecord = PR_FALSE;
    rv = pMap->GetSkipFirstRecord(&skipFirstRecord);
    if (NS_SUCCEEDED(rv))
    {
      prefs->SetBoolPref("mailnews.import.text.skipfirstrecord", skipFirstRecord);
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void ImportAddressImpl::SetLogs ( nsString success,
nsString error,
PRUnichar **  pError,
PRUnichar **  pSuccess 
) [static, private]

Definition at line 454 of file nsTextImport.cpp.

{
       if (pError)
              *pError = ToNewUnicode(error);
       if (pSuccess)
              *pSuccess = ToNewUnicode(success);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 142 of file nsTextImport.cpp.

Definition at line 141 of file nsTextImport.cpp.

Definition at line 140 of file nsTextImport.cpp.

Definition at line 139 of file nsTextImport.cpp.

Definition at line 138 of file nsTextImport.cpp.


The documentation for this class was generated from the following file: