NS_IMETHODIMP ImportAddressImpl::GetSampleData(PRInt32 index, bool *pFound, PRUnichar **pStr) { 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; PRUnichar term = 0; if (!m_haveDelim) { rv = m_text.DetermineDelim(m_fileLoc); NS_ENSURE_SUCCESS(rv, rv); m_haveDelim = true; m_delim = m_text.GetDelim(); } bool fileExists; rv = m_fileLoc->Exists(&fileExists); NS_ENSURE_SUCCESS(rv, rv); if (!fileExists) { *pFound = false; *pStr = NS_strdup(&term); return NS_OK; } nsCString line; rv = nsTextAddress::ReadRecordNumber(m_fileLoc, line, index); if (NS_SUCCEEDED(rv)) { nsString str; nsCString field; nsString uField; PRInt32 fNum = 0; while (nsTextAddress::GetField(line.get(), line.Length(), fNum, field, m_delim)) { if (fNum) str.Append(PRUnichar('\n')); SanitizeSampleData(field); rv = nsMsgI18NConvertToUnicode(nsMsgI18NFileSystemCharset(), field, uField); str.Append(uField); fNum++; field.Truncate(); } *pStr = ToNewUnicode(str); *pFound = true; /* IMPORT_LOG1("Sample data: %S\n", str.get()); */ } else { *pFound = false; *pStr = NS_strdup(&term); } return NS_OK; }
NS_IMETHODIMP ImportAddressImpl::ImportAddressBook(nsIImportABDescriptor *pSource, nsIAddrDatabase *pDestination, nsIImportFieldMap *fieldMap, nsISupports *aSupportService, PRUnichar ** pErrorLog, PRUnichar ** pSuccessLog, bool * fatalError) { NS_PRECONDITION(pSource != nsnull, "null ptr"); NS_PRECONDITION(pDestination != nsnull, "null ptr"); NS_PRECONDITION(fatalError != nsnull, "null ptr"); m_bytesImported = 0; nsString success, error; if (!pSource || !pDestination || !fatalError) { IMPORT_LOG0("*** Bad param passed to text address import\n"); nsImportStringBundle::GetStringByID(TEXTIMPORT_ADDRESS_BADPARAM, m_notProxyBundle, error); SetLogs(success, error, pErrorLog, pSuccessLog); if (fatalError) *fatalError = true; return NS_ERROR_NULL_POINTER; } ClearSampleFile(); bool addrAbort = false; nsString name; pSource->GetPreferredName(name); PRUint32 addressSize = 0; pSource->GetSize(&addressSize); if (addressSize == 0) { IMPORT_LOG0("Address book size is 0, skipping import.\n"); ReportSuccess(name, &success, m_notProxyBundle); SetLogs(success, error, pErrorLog, pSuccessLog); return NS_OK; } nsCOMPtr<nsIFile> inFile; if (NS_FAILED(pSource->GetAbFile(getter_AddRefs(inFile)))) { ReportError(TEXTIMPORT_ADDRESS_BADSOURCEFILE, name, &error, m_notProxyBundle); SetLogs(success, error, pErrorLog, pSuccessLog); return NS_ERROR_FAILURE; } if (!aSupportService) { IMPORT_LOG0("Missing support service to import call"); return NS_ERROR_FAILURE; } bool isLDIF = false; nsresult rv; nsCOMPtr<nsIAbLDIFService> ldifService(do_QueryInterface(aSupportService, &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)) { ReportError(TEXTIMPORT_ADDRESS_CONVERTERROR, name, &error, m_notProxyBundle); SetLogs(success, error, pErrorLog, pSuccessLog); return rv; } if (isLDIF) { if (ldifService) rv = ldifService->ImportLDIFFile(pDestination, inFile, false, &m_bytesImported); else return NS_ERROR_FAILURE; } else { rv = m_text.ImportAddresses(&addrAbort, name.get(), inFile, pDestination, fieldMap, error, &m_bytesImported); SaveFieldMap(fieldMap); } if (NS_SUCCEEDED(rv) && error.IsEmpty()) { ReportSuccess(name, &success, m_notProxyBundle); SetLogs(success, error, pErrorLog, pSuccessLog); } else { ReportError(TEXTIMPORT_ADDRESS_CONVERTERROR, name, &error, m_notProxyBundle); SetLogs(success, error, pErrorLog, pSuccessLog); } IMPORT_LOG0("*** Text address import done\n"); return rv; }
NS_IMETHODIMP ImportAddressImpl::FindAddressBooks(nsIFile *pLoc, nsISupportsArray **ppArray) { NS_PRECONDITION(pLoc != nsnull, "null ptr"); NS_PRECONDITION(ppArray != nsnull, "null ptr"); if (!pLoc || !ppArray) return NS_ERROR_NULL_POINTER; ClearSampleFile(); *ppArray = nsnull; bool exists = false; nsresult rv = pLoc->Exists(&exists); if (NS_FAILED(rv) || !exists) return NS_ERROR_FAILURE; bool isFile = 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 = true; m_delim = m_text.GetDelim(); m_fileLoc = do_QueryInterface(pLoc); /* 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; } nsString name; m_fileLoc->GetLeafName(name); if (NS_FAILED(rv)) { IMPORT_LOG0("*** Failed getting leaf name of file\n"); return rv; } PRInt32 idx = name.RFindChar('.'); if ((idx != -1) && (idx > 0) && ((name.Length() - idx - 1) < 5)) { name.SetLength(idx); } nsCOMPtr<nsIImportABDescriptor> desc; nsISupports * pInterface; nsCOMPtr<nsIImportService> impSvc(do_GetService(NS_IMPORTSERVICE_CONTRACTID, &rv)); if (NS_FAILED(rv)) { IMPORT_LOG0("*** Failed to obtain the import service\n"); return rv; } rv = impSvc->CreateNewABDescriptor(getter_AddRefs(desc)); if (NS_SUCCEEDED(rv)) { PRInt64 sz = 0; pLoc->GetFileSize(&sz); desc->SetPreferredName(name); desc->SetSize((PRUint32) sz); desc->SetAbFile(m_fileLoc); 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; }
NS_IMETHODIMP ImportAddressImpl::GetSampleData( PRInt32 index, PRBool *pFound, PRUnichar **pStr) { 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); }
NS_IMETHODIMP ImportAddressImpl::ImportAddressBook( nsIImportABDescriptor *pSource, nsIAddrDatabase * pDestination, nsIImportFieldMap * fieldMap, PRBool isAddrLocHome, PRUnichar ** pErrorLog, PRUnichar ** pSuccessLog, PRBool * fatalError) { 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; }
NS_IMETHODIMP ImportAddressImpl::FindAddressBooks(nsIFileSpec *pLoc, nsISupportsArray **ppArray) { 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); }