/* parses ImapMessageURI */ nsresult nsParseImapMessageURI(const char* uri, nsCString& folderURI, PRUint32 *key, char **part) { if(!key) return NS_ERROR_NULL_POINTER; nsCAutoString uriStr(uri); PRInt32 folderEnd = -1; // imap-message uri's can have imap:// url strings tacked on the end, // e.g., when opening/saving attachments. We don't want to look for '#' // in that part of the uri, if the attachment name contains '#', // so check for that here. if (StringBeginsWith(uriStr, NS_LITERAL_CSTRING("imap-message"))) folderEnd = uriStr.Find("imap://"); PRInt32 keySeparator = MsgRFindChar(uriStr, '#', folderEnd); if(keySeparator != -1) { PRInt32 keyEndSeparator = MsgFindCharInSet(uriStr, "/?&", keySeparator); nsAutoString folderPath; folderURI = StringHead(uriStr, keySeparator); folderURI.Cut(4, 8); // cut out the _message part of imap-message: // folder uri's don't have fully escaped usernames. PRInt32 atPos = folderURI.FindChar('@'); if (atPos != -1) { nsCString unescapedName, escapedName; PRInt32 userNamePos = folderURI.Find("//") + 2; PRUint32 origUserNameLen = atPos - userNamePos; if (NS_SUCCEEDED(MsgUnescapeString(Substring(folderURI, userNamePos, origUserNameLen), 0, unescapedName))) { // Re-escape the username, matching the way we do it in uris, not the // way necko escapes urls. See nsMsgIncomingServer::GetServerURI. MsgEscapeString(unescapedName, nsINetUtil::ESCAPE_XALPHAS, escapedName); folderURI.Replace(userNamePos, origUserNameLen, escapedName); } } nsCAutoString keyStr; if (keyEndSeparator != -1) keyStr = Substring(uriStr, keySeparator + 1, keyEndSeparator - (keySeparator + 1)); else keyStr = Substring(uriStr, keySeparator + 1); *key = strtoul(keyStr.get(), nsnull, 10); if (part && keyEndSeparator != -1) { PRInt32 partPos = MsgFind(uriStr, "part=", PR_FALSE, keyEndSeparator); if (partPos != -1) { *part = ToNewCString(Substring(uriStr, keyEndSeparator)); } } } return NS_OK; }
void nsEudoraCompose::ExtractCharset(nsString& str) { int32_t idx = MsgFind(str, "charset=", true, 0); if (idx != -1) { str.Cut(0, idx + 8); idx = str.FindChar(';'); if (idx != -1) str.SetLength(idx); str.Trim(kWhitespace); if ((str.CharAt(0) == '"') && (str.Length() > 2)) { str.SetLength(str.Length() - 1); str.Cut(0, 1); str.Trim(kWhitespace); } } else str.Truncate(); }
NS_IMETHODIMP nsAbManager::ExportAddressBook(nsIDOMWindow *aParentWin, nsIAbDirectory *aDirectory) { NS_ENSURE_ARG_POINTER(aParentWin); nsresult rv; nsCOMPtr<nsIFilePicker> filePicker = do_CreateInstance("@mozilla.org/filepicker;1", &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIStringBundleService> bundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIStringBundle> bundle; rv = bundleService->CreateBundle("chrome://messenger/locale/addressbook/addressBook.properties", getter_AddRefs(bundle)); NS_ENSURE_SUCCESS(rv, rv); nsString title; rv = bundle->GetStringFromName(NS_LITERAL_STRING("ExportAddressBookTitle").get(), getter_Copies(title)); NS_ENSURE_SUCCESS(rv, rv); rv = filePicker->Init(aParentWin, title, nsIFilePicker::modeSave); NS_ENSURE_SUCCESS(rv, rv); nsString filterString; rv = bundle->GetStringFromName(NS_LITERAL_STRING("LDIFFiles").get(), getter_Copies(filterString)); NS_ENSURE_SUCCESS(rv, rv); rv = filePicker->AppendFilter(filterString, NS_LITERAL_STRING("*.ldi; *.ldif")); NS_ENSURE_SUCCESS(rv, rv); rv = bundle->GetStringFromName(NS_LITERAL_STRING("CSVFiles").get(), getter_Copies(filterString)); NS_ENSURE_SUCCESS(rv, rv); rv = filePicker->AppendFilter(filterString, NS_LITERAL_STRING("*.csv")); NS_ENSURE_SUCCESS(rv, rv); rv = bundle->GetStringFromName(NS_LITERAL_STRING("TABFiles").get(), getter_Copies(filterString)); NS_ENSURE_SUCCESS(rv, rv); rv = filePicker->AppendFilter(filterString, NS_LITERAL_STRING("*.tab; *.txt")); NS_ENSURE_SUCCESS(rv, rv); PRInt16 dialogResult; filePicker->Show(&dialogResult); if (dialogResult == nsIFilePicker::returnCancel) return rv; nsCOMPtr<nsILocalFile> localFile; rv = filePicker->GetFile(getter_AddRefs(localFile)); NS_ENSURE_SUCCESS(rv, rv); if (dialogResult == nsIFilePicker::returnReplace) { // be extra safe and only delete when the file is really a file bool isFile; rv = localFile->IsFile(&isFile); if (NS_SUCCEEDED(rv) && isFile) { rv = localFile->Remove(PR_FALSE /* recursive delete */); NS_ENSURE_SUCCESS(rv, rv); } } // The type of export is determined by the drop-down in // the file picker dialog. PRInt32 exportType; rv = filePicker->GetFilterIndex(&exportType); NS_ENSURE_SUCCESS(rv,rv); nsAutoString fileName; rv = localFile->GetLeafName(fileName); NS_ENSURE_SUCCESS(rv, rv); switch ( exportType ) { default: case LDIF_EXPORT_TYPE: // ldif // If filename does not have the correct ext, add one. if ((MsgFind(fileName, LDIF_FILE_EXTENSION, PR_TRUE, fileName.Length() - strlen(LDIF_FILE_EXTENSION)) == -1) && (MsgFind(fileName, LDIF_FILE_EXTENSION2, PR_TRUE, fileName.Length() - strlen(LDIF_FILE_EXTENSION2)) == -1)) { // Add the extension and build a new localFile. fileName.AppendLiteral(LDIF_FILE_EXTENSION2); localFile->SetLeafName(fileName); } rv = ExportDirectoryToLDIF(aDirectory, localFile); break; case CSV_EXPORT_TYPE: // csv // If filename does not have the correct ext, add one. if (MsgFind(fileName, CSV_FILE_EXTENSION, PR_TRUE, fileName.Length() - strlen(CSV_FILE_EXTENSION)) == -1) { // Add the extension and build a new localFile. fileName.AppendLiteral(CSV_FILE_EXTENSION); localFile->SetLeafName(fileName); } rv = ExportDirectoryToDelimitedText(aDirectory, CSV_DELIM, CSV_DELIM_LEN, localFile); break; case TAB_EXPORT_TYPE: // tab & text // If filename does not have the correct ext, add one. if ((MsgFind(fileName, TXT_FILE_EXTENSION, PR_TRUE, fileName.Length() - strlen(TXT_FILE_EXTENSION)) == -1) && (MsgFind(fileName, TAB_FILE_EXTENSION, PR_TRUE, fileName.Length() - strlen(TAB_FILE_EXTENSION)) == -1)) { // Add the extension and build a new localFile. fileName.AppendLiteral(TXT_FILE_EXTENSION); localFile->SetLeafName(fileName); } rv = ExportDirectoryToDelimitedText(aDirectory, TAB_DELIM, TAB_DELIM_LEN, localFile); break; }; return rv; }
bool nsTextAddress::GetField(const nsAString &aLine, int32_t index, nsString &field, char16_t delim) { bool result = false; int32_t pos = 0; int32_t maxLen = aLine.Length(); char16_t tab = char16_t('\t'); char16_t doubleQuote = char16_t('"'); field.Truncate(); if (delim == tab) tab = 0; while (index && (pos < maxLen)) { while (((aLine[pos] == char16_t(' ')) || (aLine[pos] == tab)) && (pos < maxLen)) { pos++; } if (pos >= maxLen) break; if (aLine[pos] == doubleQuote) { do { pos++; if (((pos + 1) < maxLen) && (aLine[pos] == doubleQuote) && (aLine[pos + 1] == doubleQuote)) { pos += 2; } } while ((pos < maxLen) && (aLine[pos] != doubleQuote)); if (pos < maxLen) pos++; } if (pos >= maxLen) break; while ((pos < maxLen) && (aLine[pos] != delim)) pos++; if (pos >= maxLen) break; index--; pos++; } if (pos >= maxLen) return result; result = true; while ((pos < maxLen) && ((aLine[pos] == ' ') || (aLine[pos] == tab))) pos++; int32_t fLen = 0; int32_t startPos = pos; bool quoted = false; if (aLine[pos] == '"') { startPos++; fLen = -1; do { pos++; fLen++; if (((pos + 1) < maxLen) && (aLine[pos] == doubleQuote) && (aLine[pos + 1] == doubleQuote)) { quoted = true; pos += 2; fLen += 2; } } while ((pos < maxLen) && (aLine[pos] != doubleQuote)); } else { while ((pos < maxLen) && (aLine[pos] != delim)) { pos++; fLen++; } } if (!fLen) { return result; } field.Append(nsDependentSubstring(aLine, startPos, fLen)); field.Trim(kWhitespace); if (quoted) { int32_t offset = field.Find("\"\""); while (offset != -1) { field.Cut(offset, 1); offset = MsgFind(field, "\"\"", false, offset + 1); } } return result; }