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::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::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::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); }