From d5b4611a814a23ab17f1df3ad0a265f068a24aea Mon Sep 17 00:00:00 2001 From: "cbiesinger%web.de" Date: Wed, 23 Mar 2005 18:05:49 +0000 Subject: [PATCH] Bug 132180 eliminate nsFileSpec in address book. r=dmose sr=bienvenu patch by Mark Banner --- .../addrbook/public/nsIAddrBookSession.idl | 6 ++- mailnews/addrbook/public/nsIAddrDatabase.idl | 6 +-- .../addrbook/src/nsAbLDAPChangeLogData.cpp | 24 ++++++--- .../addrbook/src/nsAbLDAPReplicationData.cpp | 31 ++++------- mailnews/addrbook/src/nsAbMDBCardProperty.cpp | 20 +++---- mailnews/addrbook/src/nsAbMDBDirFactory.cpp | 14 ++--- mailnews/addrbook/src/nsAddrBookSession.cpp | 7 +-- mailnews/addrbook/src/nsAddrDatabase.cpp | 38 ++++++-------- mailnews/addrbook/src/nsAddrDatabase.h | 3 +- mailnews/addrbook/src/nsAddressBook.cpp | 46 ++++++++-------- mailnews/addrbook/src/nsDirPrefs.cpp | 48 ++++++++++------- .../extensions/palmsync/src/nsAbPalmSync.cpp | 25 ++------- mailnews/import/src/nsImportAddressBooks.cpp | 52 ++++++++++++------- 13 files changed, 159 insertions(+), 161 deletions(-) diff --git a/mailnews/addrbook/public/nsIAddrBookSession.idl b/mailnews/addrbook/public/nsIAddrBookSession.idl index bc12ca59e6cf..581e3c2b8320 100644 --- a/mailnews/addrbook/public/nsIAddrBookSession.idl +++ b/mailnews/addrbook/public/nsIAddrBookSession.idl @@ -51,7 +51,9 @@ #include "nsIAbDirectory.idl" #include "nsIAbCard.idl" -[scriptable, uuid(C5339441-303F-11d3-9E13-00A0C92B5F0D)] +interface nsILocalFile; + +[scriptable, uuid(957abaa8-5cef-49d0-84a5-ce43da384990)] interface nsIAddrBookSession : nsISupports { void addAddressBookListener(in nsIAbListener listener, in abListenerNotifyFlagValue notifyFlags); void removeAddressBookListener(in nsIAbListener listener); @@ -71,7 +73,7 @@ interface nsIAddrBookSession : nsISupports { */ void notifyDirectoryDeleted(in nsIAbDirectory directory, in nsISupports item); - [noscript] readonly attribute nsFileSpec userProfileDirectory; + readonly attribute nsILocalFile userProfileDirectory; /** * generate the name from the card, using the firstName and lastName and the displayName diff --git a/mailnews/addrbook/public/nsIAddrDatabase.idl b/mailnews/addrbook/public/nsIAddrDatabase.idl index 61e3d5d30bf6..c9537b75ac34 100644 --- a/mailnews/addrbook/public/nsIAddrDatabase.idl +++ b/mailnews/addrbook/public/nsIAddrDatabase.idl @@ -137,13 +137,11 @@ interface nsAddrDBCommitType const long kCompressCommit = 3; }; -[scriptable, uuid(A4186D8B-1DD0-11d3-A303-001083003D0C)] +[scriptable, uuid(8175a7ce-4414-489e-ba29-b22d8c4a1cae)] interface nsIAddrDatabase : nsIAddrDBAnnouncer { [noscript] attribute nsFileSpec dbPath; - [noscript] void open(in nsFileSpec folderName, in boolean create, - out nsIAddrDatabase pCardDB, in boolean upgrading); - nsIAddrDatabase openWithIFile(in nsIFile dbFile, in boolean create, in boolean upgrading); + nsIAddrDatabase open(in nsIFile dbFile, in boolean create, in boolean upgrading); void close(in boolean forceCommit); [noscript] void openMDB(in nsFileSpec dbName, in boolean create); diff --git a/mailnews/addrbook/src/nsAbLDAPChangeLogData.cpp b/mailnews/addrbook/src/nsAbLDAPChangeLogData.cpp index fe5cb8eccc1f..bdaee6132fe0 100644 --- a/mailnews/addrbook/src/nsAbLDAPChangeLogData.cpp +++ b/mailnews/addrbook/src/nsAbLDAPChangeLogData.cpp @@ -44,7 +44,6 @@ #include "nsAbUtils.h" #include "nsAbMDBCard.h" #include "nsAbLDAPCard.h" -#include "nsFileSpec.h" #include "nsAbLDAPProperties.h" #include "nsProxiedService.h" #include "nsAutoLock.h" @@ -191,14 +190,27 @@ nsresult nsAbLDAPProcessChangeLogData::OnLDAPSearchResult(nsILDAPMessage *aMessa nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); if(NS_FAILED(rv)) break; - nsFileSpec* dbPath; - rv = abSession->GetUserProfileDirectory(&dbPath); + nsCOMPtr dbPath; + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); if(NS_FAILED(rv)) break; - (*dbPath) += mDirServerInfo->replInfo->fileName; - if (!dbPath->Exists() || !dbPath->GetFileSize()) + + rv = dbPath->AppendNative(nsDependentCString(mDirServerInfo->replInfo->fileName)); + if(NS_FAILED(rv)) + break; + + PRBool fileExists; + rv = dbPath->Exists(&fileExists); + if(NS_FAILED(rv)) + break; + + PRInt64 fileSize; + rv = dbPath->GetFileSize(&fileSize); + if(NS_FAILED(rv)) + break; + + if (!fileExists || !fileSize) mUseChangeLog = PR_FALSE; - delete dbPath; // open / create the AB here since it calls Done, // just return from here. diff --git a/mailnews/addrbook/src/nsAbLDAPReplicationData.cpp b/mailnews/addrbook/src/nsAbLDAPReplicationData.cpp index 72f88899655a..7996ae378607 100644 --- a/mailnews/addrbook/src/nsAbLDAPReplicationData.cpp +++ b/mailnews/addrbook/src/nsAbLDAPReplicationData.cpp @@ -45,7 +45,6 @@ #include "nsAbUtils.h" #include "nsAbMDBCard.h" #include "nsAbLDAPCard.h" -#include "nsFileSpec.h" #include "nsAbLDAPProperties.h" #include "nsAbLDAPReplicationQuery.h" #include "nsProxiedService.h" @@ -496,25 +495,23 @@ nsresult nsAbLDAPProcessReplicationData::OpenABForReplicatedDir(PRBool aCreate) return NS_ERROR_FAILURE; } - nsFileSpec* dbPath; - rv = abSession->GetUserProfileDirectory(&dbPath); + rv = abSession->GetUserProfileDirectory(getter_AddRefs(mReplicationFile)); if(NS_FAILED(rv)) { Done(PR_FALSE); return rv; } - (*dbPath) += mDirServerInfo->replInfo->fileName; + rv = mReplicationFile->AppendNative(nsDependentCString(mDirServerInfo->replInfo->fileName)); + if(NS_FAILED(rv)) { + Done(PR_FALSE); + return rv; + } // if the AB DB already exists backup existing one, // in case if the user cancels or Abort put back the backed up file - if(dbPath->Exists()) { - // get nsIFile for nsFileSpec from abSession, why use a obsolete class if not required! - rv = NS_FileSpecToIFile(dbPath, getter_AddRefs(mReplicationFile)); - if(NS_FAILED(rv)) { - delete dbPath; - Done(PR_FALSE); - return rv; - } + PRBool fileExists; + rv = mReplicationFile->Exists(&fileExists); + if(NS_SUCCEEDED(rv) && fileExists) { // create the backup file object same as the Replication file object. // we create a backup file here since we need to cleanup the existing file // for create and then commit so instead of deleting existing cards we just @@ -524,33 +521,28 @@ nsresult nsAbLDAPProcessReplicationData::OpenABForReplicatedDir(PRBool aCreate) nsCOMPtr clone; rv = mReplicationFile->Clone(getter_AddRefs(clone)); if(NS_FAILED(rv)) { - delete dbPath; Done(PR_FALSE); return rv; } mBackupReplicationFile = do_QueryInterface(clone, &rv); if(NS_FAILED(rv)) { - delete dbPath; Done(PR_FALSE); return rv; } rv = mBackupReplicationFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0777); if(NS_FAILED(rv)) { - delete dbPath; Done(PR_FALSE); return rv; } nsAutoString backupFileLeafName; rv = mBackupReplicationFile->GetLeafName(backupFileLeafName); if(NS_FAILED(rv)) { - delete dbPath; Done(PR_FALSE); return rv; } // remove the newly created unique backup file so that move and copy succeeds. rv = mBackupReplicationFile->Remove(PR_FALSE); if(NS_FAILED(rv)) { - delete dbPath; Done(PR_FALSE); return rv; } @@ -578,7 +570,6 @@ nsresult nsAbLDAPProcessReplicationData::OpenABForReplicatedDir(PRBool aCreate) mBackupReplicationFile->SetLeafName(backupFileLeafName); } if(NS_FAILED(rv)) { - delete dbPath; Done(PR_FALSE); return rv; } @@ -587,15 +578,13 @@ nsresult nsAbLDAPProcessReplicationData::OpenABForReplicatedDir(PRBool aCreate) nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); if(NS_FAILED(rv)) { - delete dbPath; if (mBackupReplicationFile) mBackupReplicationFile->Remove(PR_FALSE); Done(PR_FALSE); return rv; } - rv = addrDBFactory->Open(dbPath, aCreate, getter_AddRefs(mReplicationDB), PR_TRUE); - delete dbPath; + rv = addrDBFactory->Open(mReplicationFile, aCreate, PR_TRUE, getter_AddRefs(mReplicationDB)); if(NS_FAILED(rv)) { Done(PR_FALSE); if (mBackupReplicationFile) diff --git a/mailnews/addrbook/src/nsAbMDBCardProperty.cpp b/mailnews/addrbook/src/nsAbMDBCardProperty.cpp index 9055de599930..bc356098031c 100644 --- a/mailnews/addrbook/src/nsAbMDBCardProperty.cpp +++ b/mailnews/addrbook/src/nsAbMDBCardProperty.cpp @@ -282,30 +282,32 @@ nsresult nsAbMDBCardProperty::GetCardDatabase(const char *uri) do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); if (NS_SUCCEEDED(rv)) { - nsFileSpec* dbPath; - abSession->GetUserProfileDirectory(&dbPath); + nsCOMPtr dbPath; + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); + NS_ENSURE_SUCCESS(rv, rv); - const char* file = nsnull; - file = &(uri[kMDBDirectoryRootLen]); - (*dbPath) += file; + + rv = dbPath->AppendNative(nsDependentCString(&(uri[kMDBDirectoryRootLen]))); + NS_ENSURE_SUCCESS(rv, rv); - if (dbPath->Exists()) + PRBool fileExists; + rv = dbPath->Exists(&fileExists); + if (NS_SUCCEEDED(rv) && fileExists) { nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); if (NS_SUCCEEDED(rv) && addrDBFactory) - rv = addrDBFactory->Open(dbPath, PR_TRUE, getter_AddRefs(mCardDatabase), PR_TRUE); + rv = addrDBFactory->Open(dbPath, PR_TRUE, PR_TRUE, getter_AddRefs(mCardDatabase)); } else rv = NS_ERROR_FAILURE; - delete dbPath; } return rv; } NS_IMETHODIMP nsAbMDBCardProperty::Equals(nsIAbCard *card, PRBool *result) - { +{ nsresult rv; if (this == card) { diff --git a/mailnews/addrbook/src/nsAbMDBDirFactory.cpp b/mailnews/addrbook/src/nsAbMDBDirFactory.cpp index 58b13d798f41..63b161e91285 100644 --- a/mailnews/addrbook/src/nsAbMDBDirFactory.cpp +++ b/mailnews/addrbook/src/nsAbMDBDirFactory.cpp @@ -138,11 +138,11 @@ NS_IMETHODIMP nsAbMDBDirFactory::CreateDirectory(nsIAbDirectoryProperties *aProp nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); - nsFileSpec* dbPath; - rv = abSession->GetUserProfileDirectory(&dbPath); + nsCOMPtr dbPath; + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); - nsCOMPtr listDatabase; - if (dbPath) + nsCOMPtr listDatabase; + if (NS_SUCCEEDED(rv)) { nsCAutoString fileName; nsDependentCString uriStr(uri); @@ -150,13 +150,13 @@ NS_IMETHODIMP nsAbMDBDirFactory::CreateDirectory(nsIAbDirectoryProperties *aProp if (StringBeginsWith(uriStr, NS_LITERAL_CSTRING(kMDBDirectoryRoot))) fileName = Substring(uriStr, kMDBDirectoryRootLen, uriStr.Length() - kMDBDirectoryRootLen); - (*dbPath) += fileName.get(); + rv = dbPath->AppendNative(fileName); + NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); - rv = addrDBFactory->Open(dbPath, PR_TRUE, getter_AddRefs(listDatabase), PR_TRUE); - delete dbPath; + rv = addrDBFactory->Open(dbPath, PR_TRUE, PR_TRUE, getter_AddRefs(listDatabase)); } NS_ENSURE_SUCCESS(rv, rv); diff --git a/mailnews/addrbook/src/nsAddrBookSession.cpp b/mailnews/addrbook/src/nsAddrBookSession.cpp index a28b2e0c79b2..2ebf39baae31 100644 --- a/mailnews/addrbook/src/nsAddrBookSession.cpp +++ b/mailnews/addrbook/src/nsAddrBookSession.cpp @@ -177,7 +177,7 @@ NS_IMETHODIMP nsAddrBookSession::NotifyDirectoryDeleted(nsIAbDirectory *director return NS_OK; } -NS_IMETHODIMP nsAddrBookSession::GetUserProfileDirectory(nsFileSpec * *userDir) +NS_IMETHODIMP nsAddrBookSession::GetUserProfileDirectory(nsILocalFile * *userDir) { NS_ENSURE_ARG_POINTER(userDir); *userDir = nsnull; @@ -192,10 +192,7 @@ NS_IMETHODIMP nsAddrBookSession::GetUserProfileDirectory(nsFileSpec * *userDir) rv = profileDir->GetNativePath(pathBuf); NS_ENSURE_SUCCESS(rv, rv); - *userDir = new nsFileSpec(pathBuf.get()); - NS_ENSURE_TRUE(*userDir, NS_ERROR_OUT_OF_MEMORY); - - return rv; + return NS_NewNativeLocalFile(pathBuf, PR_TRUE, userDir); } #define kDisplayName 0 diff --git a/mailnews/addrbook/src/nsAddrDatabase.cpp b/mailnews/addrbook/src/nsAddrDatabase.cpp index 2da9926a6e3e..c70c7596cf71 100644 --- a/mailnews/addrbook/src/nsAddrDatabase.cpp +++ b/mailnews/addrbook/src/nsAddrDatabase.cpp @@ -40,7 +40,6 @@ #include "nsAddrDatabase.h" #include "nsIEnumerator.h" -#include "nsFileStream.h" #include "nsString.h" #include "nsReadableUtils.h" #include "nsRDFCID.h" @@ -548,33 +547,28 @@ NS_IMETHODIMP nsAddrDatabase::SetDbPath(nsFileSpec * aDbPath) return NS_OK; } -NS_IMETHODIMP nsAddrDatabase::OpenWithIFile(nsIFile *aFile, PRBool aCreate, PRBool aUpgrading, nsIAddrDatabase **aDB) -{ - NS_ENSURE_ARG_POINTER(aDB); - nsCOMPtr dbSpec; - nsFileSpec addrDBFileSpec; - // Convert the nsILocalFile into an nsIFileSpec - // TODO: convert users of nsIFileSpec to nsILocalFile - // and avoid this step. - nsresult rv = NS_NewFileSpecFromIFile(aFile, getter_AddRefs(dbSpec)); - NS_ENSURE_SUCCESS(rv, rv); - rv = dbSpec->GetFileSpec(&addrDBFileSpec); - NS_ENSURE_SUCCESS(rv, rv); - return Open(&addrDBFileSpec, aCreate, aDB, aUpgrading); -} - NS_IMETHODIMP nsAddrDatabase::Open -(nsFileSpec *aMabFile, PRBool aCreate, nsIAddrDatabase** pAddrDB, PRBool upgrading /* unused */) +(nsIFile *aMabFile, PRBool aCreate, PRBool upgrading /* unused */, nsIAddrDatabase** pAddrDB) { *pAddrDB = nsnull; - nsAddrDatabase *pAddressBookDB = (nsAddrDatabase *) FindInCache(aMabFile); + nsCOMPtr mabIFileSpec; + nsFileSpec mabFileSpec; + // Convert the nsILocalFile into an nsIFileSpec + // TODO: convert users of nsIFileSpec to nsILocalFile + // and avoid this step. + nsresult rv = NS_NewFileSpecFromIFile(aMabFile, getter_AddRefs(mabIFileSpec)); + NS_ENSURE_SUCCESS(rv, rv); + rv = mabIFileSpec->GetFileSpec(&mabFileSpec); + NS_ENSURE_SUCCESS(rv, rv); + + nsAddrDatabase *pAddressBookDB = (nsAddrDatabase *) FindInCache(&mabFileSpec); if (pAddressBookDB) { *pAddrDB = pAddressBookDB; return NS_OK; } - nsresult rv = OpenInternal(aMabFile, aCreate, pAddrDB); + rv = OpenInternal(&mabFileSpec, aCreate, pAddrDB); if (NS_SUCCEEDED(rv)) return NS_OK; @@ -583,13 +577,13 @@ NS_IMETHODIMP nsAddrDatabase::Open // and prompt the user if (aCreate) { - nsFileSpec *newMabFile = new nsFileSpec(*aMabFile); + nsFileSpec *newMabFile = new nsFileSpec(mabFileSpec); if (!newMabFile) return NS_ERROR_OUT_OF_MEMORY; // save off the name of the corrupt mab file, example abook.mab nsXPIDLCString originalMabFileName; - originalMabFileName.Adopt(aMabFile->GetLeafName()); + originalMabFileName.Adopt(mabFileSpec.GetLeafName()); // the suggest new name for the backup will be abook.mab.bak nsCAutoString backupMabFileName(originalMabFileName); @@ -602,7 +596,7 @@ NS_IMETHODIMP nsAddrDatabase::Open backupMabFileName.Adopt(newMabFile->GetLeafName()); // rename abook.mab to abook.mab.bak - rv = aMabFile->Rename(backupMabFileName.get()); + rv = mabFileSpec.Rename(backupMabFileName.get()); NS_ASSERTION(NS_SUCCEEDED(rv), "failed to rename corrupt mab file"); if (NS_SUCCEEDED(rv)) { diff --git a/mailnews/addrbook/src/nsAddrDatabase.h b/mailnews/addrbook/src/nsAddrDatabase.h index 054cf931c48f..50122a394b38 100644 --- a/mailnews/addrbook/src/nsAddrDatabase.h +++ b/mailnews/addrbook/src/nsAddrDatabase.h @@ -76,8 +76,7 @@ public: NS_IMETHOD GetDbPath(nsFileSpec * *aDbPath); NS_IMETHOD SetDbPath(nsFileSpec * aDbPath); - NS_IMETHOD Open(nsFileSpec *aMabFile, PRBool aCreate, nsIAddrDatabase **pCardDB, PRBool upgrading); - NS_IMETHOD OpenWithIFile(nsIFile *dbFile, PRBool create, PRBool upgrading, nsIAddrDatabase **_retval); + NS_IMETHOD Open(nsIFile *aMabFile, PRBool aCreate, PRBool upgrading, nsIAddrDatabase **pCardDB); NS_IMETHOD Close(PRBool forceCommit); NS_IMETHOD OpenMDB(nsFileSpec *dbName, PRBool create); NS_IMETHOD CloseMDB(PRBool commit); diff --git a/mailnews/addrbook/src/nsAddressBook.cpp b/mailnews/addrbook/src/nsAddressBook.cpp index c1cb5dea547a..46a6b0db917f 100644 --- a/mailnews/addrbook/src/nsAddressBook.cpp +++ b/mailnews/addrbook/src/nsAddressBook.cpp @@ -67,7 +67,6 @@ #include "nsReadableUtils.h" #include "nsICategoryManager.h" #include "nsIAbUpgrader.h" -#include "nsSpecialSystemDirectory.h" #include "nsIFilePicker.h" #include "nsIPrefService.h" #include "nsIPrefBranch.h" @@ -282,8 +281,8 @@ NS_IMETHODIMP nsAddressBook::GetAbDatabaseFromURI(const char *aURI, nsIAddrDatab do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv,rv); - nsFileSpec* dbPath; - rv = abSession->GetUserProfileDirectory(&dbPath); + nsCOMPtr dbPath; + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); NS_ENSURE_SUCCESS(rv,rv); /* directory URIs are of the form @@ -300,7 +299,8 @@ NS_IMETHODIMP nsAddressBook::GetAbDatabaseFromURI(const char *aURI, nsIAddrDatab PRInt32 pos = file.Find("/"); if (pos != kNotFound) file.Truncate(pos); - (*dbPath) += file.get(); + rv = dbPath->AppendNative(file); + NS_ENSURE_SUCCESS(rv,rv); nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); @@ -308,8 +308,7 @@ NS_IMETHODIMP nsAddressBook::GetAbDatabaseFromURI(const char *aURI, nsIAddrDatab /* Don't create otherwise we end up re-opening a deleted address book */ /* bug 66410 */ - rv = addrDBFactory->Open(dbPath, PR_FALSE /* no create */, aDB, PR_TRUE); - delete dbPath; + rv = addrDBFactory->Open(dbPath, PR_FALSE /* no create */, PR_TRUE, aDB); return rv; } @@ -320,22 +319,24 @@ nsresult nsAddressBook::GetAbDatabaseFromFile(char* pDbFile, nsIAddrDatabase **d nsCOMPtr database; if (pDbFile) { - nsFileSpec* dbPath = nsnull; + nsCOMPtr dbPath; nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); if(NS_SUCCEEDED(rv)) - abSession->GetUserProfileDirectory(&dbPath); + { + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); + NS_ENSURE_SUCCESS(rv, rv); + } nsCAutoString file(pDbFile); - (*dbPath) += file.get(); + rv = dbPath->AppendNative(file); + NS_ENSURE_SUCCESS(rv,rv); nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); if (NS_SUCCEEDED(rv) && addrDBFactory) - rv = addrDBFactory->Open(dbPath, PR_TRUE, getter_AddRefs(database), PR_TRUE); - - delete dbPath; + rv = addrDBFactory->Open(dbPath, PR_TRUE, PR_TRUE, getter_AddRefs(database)); if (NS_SUCCEEDED(rv) && database) { @@ -476,27 +477,28 @@ nsresult AddressBookParser::ParseFile() // to do: we should use only one "return rv;" at the very end, instead of this // multi return structure nsresult rv = NS_OK; - nsFileSpec* dbPath = nsnull; - char* fileName = PR_smprintf("%s.mab", leafName); + nsCOMPtr dbPath; + nsCAutoString fileName(leafName); + fileName.Append(NS_LITERAL_CSTRING(".mab")); nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); if(NS_SUCCEEDED(rv)) - abSession->GetUserProfileDirectory(&dbPath); + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); /* create address book database */ - if (dbPath) + if(NS_SUCCEEDED(rv)) { - (*dbPath) += fileName; + dbPath->AppendNative(fileName); + NS_ENSURE_SUCCESS(rv, rv); + nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); if (NS_SUCCEEDED(rv) && addrDBFactory) - rv = addrDBFactory->Open(dbPath, PR_TRUE, getter_AddRefs(mDatabase), PR_TRUE); + rv = addrDBFactory->Open(dbPath, PR_TRUE, PR_TRUE, getter_AddRefs(mDatabase)); } NS_ENSURE_SUCCESS(rv, rv); - delete dbPath; - nsCOMPtr rdfService = do_GetService (NS_RDF_CONTRACTID "/rdf-service;1", &rv); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr parentResource; @@ -514,7 +516,7 @@ nsresult AddressBookParser::ParseFile() nsXPIDLString dirName; nsCOMPtr locString; nsCAutoString prefName; - if (strcmp(fileName, kPersonalAddressbook) == 0) + if (strcmp(fileName.get(), kPersonalAddressbook) == 0) prefName.AssignLiteral("ldap_2.servers.pab.description"); else prefName = NS_LITERAL_CSTRING("ldap_2.servers.") + nsDependentCString(leafName) + NS_LITERAL_CSTRING(".description"); @@ -533,8 +535,6 @@ nsresult AddressBookParser::ParseFile() if (leafName) nsCRT::free(leafName); - if (fileName) - PR_smprintf_free(fileName); return rv; } diff --git a/mailnews/addrbook/src/nsDirPrefs.cpp b/mailnews/addrbook/src/nsDirPrefs.cpp index 4719958aed61..e77ef490f8fa 100644 --- a/mailnews/addrbook/src/nsDirPrefs.cpp +++ b/mailnews/addrbook/src/nsDirPrefs.cpp @@ -1725,14 +1725,14 @@ nsresult DIR_DeleteServerFromList(DIR_Server *server) return NS_ERROR_NULL_POINTER; nsresult rv = NS_OK; - nsFileSpec* dbPath = nsnull; + nsCOMPtr dbPath; nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); if(NS_SUCCEEDED(rv)) - abSession->GetUserProfileDirectory(&dbPath); + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); - if (dbPath) + if (NS_SUCCEEDED(rv)) { // close the database, as long as it isn't the special ones // (personal addressbook and collected addressbook) @@ -1743,21 +1743,21 @@ nsresult DIR_DeleteServerFromList(DIR_Server *server) strcmp(server->fileName, kCollectedAddressbook)) { nsCOMPtr database; - (*dbPath) += server->fileName; + rv = dbPath->AppendNative(nsDependentCString(server->fileName)); + NS_ENSURE_SUCCESS(rv, rv); // close file before delete it nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); if (NS_SUCCEEDED(rv) && addrDBFactory) - rv = addrDBFactory->Open(dbPath, PR_FALSE, getter_AddRefs(database), PR_TRUE); + rv = addrDBFactory->Open(dbPath, PR_FALSE, PR_TRUE, getter_AddRefs(database)); if (database) /* database exists */ { database->ForceClosed(); - dbPath->Delete(PR_FALSE); + rv = dbPath->Remove(PR_FALSE); + NS_ENSURE_SUCCESS(rv, rv); } - - delete dbPath; } nsVoidArray *dirList = DIR_GetDirectories(); @@ -2562,27 +2562,35 @@ static void DIR_ConvertServerFileName(DIR_Server* pServer) if (leafName) PR_Free(leafName); } -/* This will generate a correct filename and then remove the path */ +/* This will generate a correct filename and then remove the path. + * Note: we are assuming that the default name is in the native + * filesystem charset. The filename will be returned as a UTF8 + * string. + */ void DIR_SetFileName(char** fileName, const char* defaultName) { nsresult rv = NS_OK; - nsFileSpec* dbPath = nsnull; + nsCOMPtr dbPath; + + *fileName = nsnull; nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); if(NS_SUCCEEDED(rv)) - abSession->GetUserProfileDirectory(&dbPath); - if (dbPath) + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); + if (NS_SUCCEEDED(rv)) { - (*dbPath) += defaultName; - dbPath->MakeUnique(defaultName); - char* file = nsnull; - file = dbPath->GetLeafName(); - *fileName = nsCRT::strdup(file); - if (file) - nsCRT::free(file); + rv = dbPath->AppendNative(nsDependentCString(defaultName)); + if (NS_SUCCEEDED(rv)) + { + rv = dbPath->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0664); - delete dbPath; + nsAutoString realFileName; + rv = dbPath->GetLeafName(realFileName); + + if (NS_SUCCEEDED(rv)) + *fileName = ToNewUTF8String(realFileName); + } } } diff --git a/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp b/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp index 4327a6f46504..fc1bf845477c 100644 --- a/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp +++ b/mailnews/extensions/palmsync/src/nsAbPalmSync.cpp @@ -731,31 +731,20 @@ nsresult nsAbPalmHotSync::OpenABDBForHotSync(PRBool aCreate) if(NS_FAILED(rv)) return rv; - nsFileSpec* dbPath; - rv = abSession->GetUserProfileDirectory(&dbPath); + rv = abSession->GetUserProfileDirectory(getter_AddRefs(mABFile)); if(NS_FAILED(rv)) return rv; - (*dbPath) += mFileName.get(); - - // get nsIFile for nsFileSpec from abSession, why use a obsolete class if not required! - rv = NS_FileSpecToIFile(dbPath, getter_AddRefs(mABFile)); - if(NS_FAILED(rv)) - { - delete dbPath; - return rv; - } + mABFile->AppendNative(mFileName); nsCOMPtr addrDBFactory = do_GetService(NS_ADDRDATABASE_CONTRACTID, &rv); if(NS_FAILED(rv)) { - delete dbPath; return rv; } - rv = addrDBFactory->Open(dbPath, aCreate, getter_AddRefs(mABDB), PR_TRUE); - delete dbPath; + rv = addrDBFactory->Open(mABFile, aCreate, PR_TRUE, getter_AddRefs(mABDB)); NS_ENSURE_SUCCESS(rv, rv); mDBOpen = PR_TRUE; // Moz AB DB is now Open @@ -777,14 +766,10 @@ nsresult nsAbPalmHotSync::KeepCurrentStateAsPrevious() nsCOMPtr abSession = do_GetService(NS_ADDRBOOKSESSION_CONTRACTID, &rv); if(NS_FAILED(rv)) return rv; - nsFileSpec* dbPath; - rv = abSession->GetUserProfileDirectory(&dbPath); // this still uses nsFileSpec!!! + rv = abSession->GetUserProfileDirectory(getter_AddRefs(mPreviousABFile)); if(NS_SUCCEEDED(rv)) { - (*dbPath) += previousLeafName.get(); - // get nsIFile for nsFileSpec from abSession, why use a obsolete class if not required! - rv = NS_FileSpecToIFile(dbPath, getter_AddRefs(mPreviousABFile)); - delete dbPath; + mPreviousABFile->AppendNative(previousLeafName); if(NS_FAILED(rv)) return rv; } diff --git a/mailnews/import/src/nsImportAddressBooks.cpp b/mailnews/import/src/nsImportAddressBooks.cpp index a4331fc4ea9c..56f045330ed9 100644 --- a/mailnews/import/src/nsImportAddressBooks.cpp +++ b/mailnews/import/src/nsImportAddressBooks.cpp @@ -777,29 +777,31 @@ nsIAddrDatabase *GetAddressBook( const PRUnichar *name, PRBool makeNew) nsIAddrDatabase * pDatabase = nsnull; /* Get the profile directory */ - // Note to Candice: This should return an nsIFileSpec, not a nsFileSpec - nsFileSpec * dbPath = nsnull; + nsCOMPtr dbPath; NS_WITH_PROXIED_SERVICE(nsIAddrBookSession, abSession, NS_ADDRBOOKSESSION_CONTRACTID, NS_UI_THREAD_EVENTQ, &rv); if (NS_SUCCEEDED(rv)) - abSession->GetUserProfileDirectory(&dbPath); - if (dbPath) { + rv = abSession->GetUserProfileDirectory(getter_AddRefs(dbPath)); + if (NS_SUCCEEDED(rv)) { // Create a new address book file - we don't care what the file // name is, as long as it's unique - (*dbPath) += "impab.mab"; - (*dbPath).MakeUnique(); + rv = dbPath->Append(NS_LITERAL_STRING("impab.mab")); + if (NS_SUCCEEDED(rv)) { + rv = dbPath->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600); - IMPORT_LOG0( "Getting the address database factory\n"); + if (NS_SUCCEEDED(rv)) { + IMPORT_LOG0( "Getting the address database factory\n"); - NS_WITH_PROXIED_SERVICE(nsIAddrDatabase, addrDBFactory, NS_ADDRDATABASE_CONTRACTID, NS_UI_THREAD_EVENTQ, &rv); - if (NS_SUCCEEDED(rv) && addrDBFactory) { - - IMPORT_LOG0( "Opening the new address book\n"); - rv = addrDBFactory->Open( dbPath, PR_TRUE, &pDatabase, PR_TRUE); - } + NS_WITH_PROXIED_SERVICE(nsIAddrDatabase, addrDBFactory, NS_ADDRDATABASE_CONTRACTID, NS_UI_THREAD_EVENTQ, &rv); + if (NS_SUCCEEDED(rv) && addrDBFactory) { + IMPORT_LOG0( "Opening the new address book\n"); + rv = addrDBFactory->Open( dbPath, PR_TRUE, PR_TRUE, &pDatabase); + } + } + } } - else { + if (NS_FAILED(rv)) { IMPORT_LOG0( "Failed to get the user profile directory from the address book session\n"); } @@ -829,15 +831,25 @@ nsIAddrDatabase *GetAddressBook( const PRUnichar *name, PRBool makeNew) rv = proxyMgr->GetProxyForObject( NS_UI_THREAD_EVENTQ, NS_GET_IID( nsIAbDirectory), parentResource, PROXY_SYNC | PROXY_ALWAYS, getter_AddRefs( parentDir)); if (parentDir) - { + { nsCAutoString URI("moz-abmdbdirectory://"); - URI.Append((*dbPath).GetLeafName()); - parentDir->CreateDirectoryByURI(name, URI.get (), PR_FALSE); - - delete dbPath; + nsCAutoString leafName; + rv = dbPath->GetNativeLeafName(leafName); + if (NS_FAILED(rv)) { + IMPORT_LOG0( "*** Error: Unable to get name of database file\n"); + } + else { + URI.Append(leafName); + rv = parentDir->CreateDirectoryByURI(name, URI.get (), PR_FALSE); + if (NS_FAILED(rv)) + IMPORT_LOG0( "*** Error: Unable to create address book directory\n"); + } } - IMPORT_LOG0( "Added new address book to the UI\n"); + if (NS_SUCCEEDED(rv)) + IMPORT_LOG0( "Added new address book to the UI\n"); + else + IMPORT_LOG0( "*** Error: An error occurred while adding the address book to the UI\n"); } }