From 4f5ecccc0ac5f1ebbc19390efe82db4509c0d8bc Mon Sep 17 00:00:00 2001 From: "chuang%netscape.com" Date: Sat, 24 Jul 1999 19:06:08 +0000 Subject: [PATCH] Add "Edit new feature attributes" support --- mailnews/addrbook/public/nsIAbCard.idl | 1 + mailnews/addrbook/public/nsIAddrDatabase.idl | 3 + mailnews/addrbook/src/nsAbCardProperty.cpp | 11 ++ mailnews/addrbook/src/nsAbCardProperty.h | 1 + mailnews/addrbook/src/nsAddrDatabase.cpp | 132 ++++++++++++++----- mailnews/addrbook/src/nsAddrDatabase.h | 13 +- mailnews/addrbook/src/nsDirPrefs.cpp | 2 +- 7 files changed, 124 insertions(+), 39 deletions(-) diff --git a/mailnews/addrbook/public/nsIAbCard.idl b/mailnews/addrbook/public/nsIAbCard.idl index 904eb165e015..47c7e0e340ca 100644 --- a/mailnews/addrbook/public/nsIAbCard.idl +++ b/mailnews/addrbook/public/nsIAbCard.idl @@ -84,6 +84,7 @@ interface nsIAbCard : nsISupports { void SetAnonymousIntAttribute(in string attrname, in unsigned long value); void SetAnonymousBoolAttribute(in string attrname, in boolean value); void AddAnonymousAttributesToDB(); + void EditAnonymousAttributesToDB(); void GetCardURI(out string uri); void AddCardToDatabase(in string uri); diff --git a/mailnews/addrbook/public/nsIAddrDatabase.idl b/mailnews/addrbook/public/nsIAddrDatabase.idl index c32588823d93..9fee26c3f428 100644 --- a/mailnews/addrbook/public/nsIAddrDatabase.idl +++ b/mailnews/addrbook/public/nsIAddrDatabase.idl @@ -60,6 +60,9 @@ interface nsIAddrDatabase : nsIAddrDBAnnouncer { void GetAnonymousBoolAttribute(in string attrname, out boolean value); void AddAnonymousAttributesToDB(); void RemoveAnonymousAttributesFromDB(); + void EditAnonymousAttributesInDB(); void AddAnonymousAttributesFromCard(in nsIAbCard card); void RemoveAnonymousAttributesFromCard(in nsIAbCard card); + void EditAnonymousAttributesFromCard(in nsIAbCard card); + }; diff --git a/mailnews/addrbook/src/nsAbCardProperty.cpp b/mailnews/addrbook/src/nsAbCardProperty.cpp index 4e4646e6562a..6edf58d48d5e 100644 --- a/mailnews/addrbook/src/nsAbCardProperty.cpp +++ b/mailnews/addrbook/src/nsAbCardProperty.cpp @@ -618,6 +618,17 @@ NS_IMETHODIMP nsAbCardProperty::AddAnonymousAttributesToDB() return rv; } +NS_IMETHODIMP nsAbCardProperty::EditAnonymousAttributesToDB() +{ + nsresult rv = NS_OK; + if (mDatabase) + mDatabase = null_nsCOMPtr(); + rv = GetCardDatabase("abdirectory://abook.mab"); + if (NS_SUCCEEDED(rv) && mDatabase) + rv = mDatabase->EditAnonymousAttributesFromCard(this); + return rv; +} + /* caller need to PR_smprintf_free *uri */ NS_IMETHODIMP nsAbCardProperty::GetCardURI(char **uri) { diff --git a/mailnews/addrbook/src/nsAbCardProperty.h b/mailnews/addrbook/src/nsAbCardProperty.h index 138b28e1f11f..31102ba021d8 100644 --- a/mailnews/addrbook/src/nsAbCardProperty.h +++ b/mailnews/addrbook/src/nsAbCardProperty.h @@ -143,6 +143,7 @@ public: NS_IMETHOD SetAnonymousIntAttribute(const char *attrname, PRUint32 value); NS_IMETHOD SetAnonymousBoolAttribute(const char *attrname, PRBool value); NS_IMETHOD AddAnonymousAttributesToDB(); + NS_IMETHOD EditAnonymousAttributesToDB(); NS_IMETHOD GetCardURI(char **uri); NS_IMETHOD AddCardToDatabase(const char *uri); diff --git a/mailnews/addrbook/src/nsAddrDatabase.cpp b/mailnews/addrbook/src/nsAddrDatabase.cpp index a744aeee13b4..ae8f705badc2 100644 --- a/mailnews/addrbook/src/nsAddrDatabase.cpp +++ b/mailnews/addrbook/src/nsAddrDatabase.cpp @@ -49,7 +49,6 @@ extern const char *kCardDataSourceRoot; /* The definition is nsAddrDatabase.cpp */ extern const char *kMainPersonalAddressBook; - #define ID_PAB_TABLE 1 #define ID_ANONYMOUS_TABLE 2 @@ -1123,8 +1122,37 @@ NS_IMETHODIMP nsAddrDatabase::CreateNewCardAndAddToDB(nsIAbCard *newCard, PRBool return err; } +nsresult nsAddrDatabase::FindAttributeRow(nsIMdbTable* pTable, mdb_token columnToken, nsIMdbRow** row) +{ + nsIMdbTableRowCursor* rowCursor = nsnull; + nsIMdbRow* findRow = nsnull; + nsIMdbCell* valueCell = nsnull; + mdb_pos rowPos = 0; + nsresult err = NS_ERROR_FAILURE; + + err = pTable->GetTableRowCursor(GetEnv(), -1, &rowCursor); + + if (NS_FAILED(err) || !rowCursor) + return NS_ERROR_FAILURE; + do + { + err = rowCursor->NextRow(GetEnv(), &findRow, &rowPos); + if (NS_SUCCEEDED(err) && findRow) + { + err = findRow->GetCell(GetEnv(), columnToken, &valueCell); + if (NS_SUCCEEDED(err) && valueCell) + { + *row = findRow; + return NS_OK; + } + } + } while (findRow); + + return NS_ERROR_FAILURE; +} + nsresult nsAddrDatabase::DoStringAnonymousTransaction -(nsVoidArray* pAttributes, nsVoidArray* pValues, PRBool bAdd) +(nsVoidArray* pAttributes, nsVoidArray* pValues, AB_NOTIFY_CODE code) { nsresult err = NS_OK; @@ -1140,7 +1168,7 @@ nsresult nsAddrDatabase::DoStringAnonymousTransaction char* pValueStr = (char*)pValues->ElementAt(i); nsIMdbRow *anonymousRow = nsnull; - if (bAdd) + if (code == AB_NotifyInserted) { err = GetStore()->NewRow(GetEnv(), m_CardRowScopeToken, &anonymousRow); if (NS_SUCCEEDED(err) && anonymousRow) @@ -1149,7 +1177,7 @@ nsresult nsAddrDatabase::DoStringAnonymousTransaction err = m_mdbAnonymousTable->AddRow(GetEnv(), anonymousRow); } } - else + else if (code == AB_NotifyDeleted) { struct mdbYarn yarn; mdbOid rowOid; @@ -1160,13 +1188,24 @@ nsresult nsAddrDatabase::DoStringAnonymousTransaction if (NS_SUCCEEDED(err) && anonymousRow) err = m_mdbAnonymousTable->CutRow(GetEnv(), anonymousRow); } + else /* Edit */ + { + err = FindAttributeRow(m_mdbAnonymousTable, anonymousColumnToken, &anonymousRow); + if (NS_SUCCEEDED(err) && anonymousRow) + { + AddStringColumn(anonymousRow, anonymousColumnToken, pValueStr); + err = m_mdbAnonymousTable->AddRow(GetEnv(), anonymousRow); + return NS_OK; + } + err = NS_ERROR_FAILURE; + } } } return err; } nsresult nsAddrDatabase::DoIntAnonymousTransaction -(nsVoidArray* pAttributes, nsVoidArray* pValues, PRBool bAdd) +(nsVoidArray* pAttributes, nsVoidArray* pValues, AB_NOTIFY_CODE code) { nsresult err = NS_OK; if (pAttributes && pValues) @@ -1182,7 +1221,7 @@ nsresult nsAddrDatabase::DoIntAnonymousTransaction PRUint32 value = *pValue; nsIMdbRow *anonymousRow = nsnull; - if (bAdd) + if (code == AB_NotifyInserted) { err = GetStore()->NewRow(GetEnv(), m_CardRowScopeToken, &anonymousRow); if (NS_SUCCEEDED(err) && anonymousRow) @@ -1191,7 +1230,7 @@ nsresult nsAddrDatabase::DoIntAnonymousTransaction err = m_mdbAnonymousTable->AddRow(GetEnv(), anonymousRow); } } - else + else if (code == AB_NotifyDeleted) { struct mdbYarn yarn; mdbOid rowOid; @@ -1204,13 +1243,24 @@ nsresult nsAddrDatabase::DoIntAnonymousTransaction if (NS_SUCCEEDED(err) && anonymousRow) err = m_mdbAnonymousTable->CutRow(GetEnv(), anonymousRow); } + else + { + err = FindAttributeRow(m_mdbAnonymousTable, anonymousColumnToken, &anonymousRow); + if (NS_SUCCEEDED(err) && anonymousRow) + { + AddIntColumn(anonymousRow, anonymousColumnToken, value); + err = m_mdbAnonymousTable->AddRow(GetEnv(), anonymousRow); + return NS_OK; + } + err = NS_ERROR_FAILURE; + } } } return err; } nsresult nsAddrDatabase::DoBoolAnonymousTransaction -(nsVoidArray* pAttributes, nsVoidArray* pValues, PRBool bAdd) +(nsVoidArray* pAttributes, nsVoidArray* pValues, AB_NOTIFY_CODE code) { nsresult err = NS_OK; if (pAttributes && pValues) @@ -1231,7 +1281,7 @@ nsresult nsAddrDatabase::DoBoolAnonymousTransaction nBoolValue = 0; nsIMdbRow *anonymousRow = nsnull; - if (bAdd) + if (code == AB_NotifyInserted) { err = GetStore()->NewRow(GetEnv(), m_CardRowScopeToken, &anonymousRow); if (NS_SUCCEEDED(err) && anonymousRow) @@ -1240,7 +1290,7 @@ nsresult nsAddrDatabase::DoBoolAnonymousTransaction err = m_mdbAnonymousTable->AddRow(GetEnv(), anonymousRow); } } - else + else if (code == AB_NotifyDeleted) { struct mdbYarn yarn; mdbOid rowOid; @@ -1253,12 +1303,23 @@ nsresult nsAddrDatabase::DoBoolAnonymousTransaction if (NS_SUCCEEDED(err) && anonymousRow) err = m_mdbAnonymousTable->CutRow(GetEnv(), anonymousRow); } + else + { + err = FindAttributeRow(m_mdbAnonymousTable, anonymousColumnToken, &anonymousRow); + if (NS_SUCCEEDED(err) && anonymousRow) + { + AddIntColumn(anonymousRow, anonymousColumnToken, nBoolValue); + err = m_mdbAnonymousTable->AddRow(GetEnv(), anonymousRow); + return NS_OK; + } + err = NS_ERROR_FAILURE; + } } } return err; } -nsresult nsAddrDatabase::DoAnonymousAttributesTransaction(PRBool bAdd) +nsresult nsAddrDatabase::DoAnonymousAttributesTransaction(AB_NOTIFY_CODE code) { nsresult err = NS_OK; @@ -1268,59 +1329,62 @@ nsresult nsAddrDatabase::DoAnonymousAttributesTransaction(PRBool bAdd) if (NS_FAILED(err) || !m_mdbAnonymousTable) return NS_ERROR_FAILURE; - DoStringAnonymousTransaction(m_pAnonymousStrAttributes, m_pAnonymousStrValues, bAdd); - DoIntAnonymousTransaction(m_pAnonymousIntAttributes, m_pAnonymousIntValues, bAdd); - DoBoolAnonymousTransaction(m_pAnonymousBoolAttributes, m_pAnonymousBoolValues, bAdd); + DoStringAnonymousTransaction(m_pAnonymousStrAttributes, m_pAnonymousStrValues, code); + DoIntAnonymousTransaction(m_pAnonymousIntAttributes, m_pAnonymousIntValues, code); + DoBoolAnonymousTransaction(m_pAnonymousBoolAttributes, m_pAnonymousBoolValues, code); Commit(kSessionCommit); return err; } -NS_IMETHODIMP nsAddrDatabase::AddAnonymousAttributesFromCard(nsIAbCard* card) +void nsAddrDatabase::GetAnonymousAttributesFromCard(nsIAbCard* card) { + nsresult err = NS_OK; RemoveAnonymousList(m_pAnonymousStrAttributes); RemoveAnonymousList(m_pAnonymousStrValues); RemoveAnonymousList(m_pAnonymousIntAttributes); RemoveAnonymousList(m_pAnonymousIntValues); RemoveAnonymousList(m_pAnonymousBoolAttributes); RemoveAnonymousList(m_pAnonymousBoolValues); - nsresult err = card->GetAnonymousStrAttrubutesList(&m_pAnonymousStrAttributes); + err = card->GetAnonymousStrAttrubutesList(&m_pAnonymousStrAttributes); err = card->GetAnonymousStrValuesList(&m_pAnonymousStrValues); err = card->GetAnonymousIntAttrubutesList(&m_pAnonymousIntAttributes); err = card->GetAnonymousIntValuesList(&m_pAnonymousIntValues); err = card->GetAnonymousBoolAttrubutesList(&m_pAnonymousBoolAttributes); err = card->GetAnonymousBoolValuesList(&m_pAnonymousBoolValues); - err = DoAnonymousAttributesTransaction(PR_TRUE); +} - return NS_OK; +NS_IMETHODIMP nsAddrDatabase::AddAnonymousAttributesFromCard(nsIAbCard* card) +{ + GetAnonymousAttributesFromCard(card); + return DoAnonymousAttributesTransaction(AB_NotifyInserted); } NS_IMETHODIMP nsAddrDatabase::AddAnonymousAttributesToDB() { - return DoAnonymousAttributesTransaction(PR_TRUE); + return DoAnonymousAttributesTransaction(AB_NotifyInserted); } NS_IMETHODIMP nsAddrDatabase::RemoveAnonymousAttributesFromCard(nsIAbCard *card) { - RemoveAnonymousList(m_pAnonymousStrAttributes); - RemoveAnonymousList(m_pAnonymousStrValues); - RemoveAnonymousList(m_pAnonymousIntAttributes); - RemoveAnonymousList(m_pAnonymousIntValues); - RemoveAnonymousList(m_pAnonymousBoolAttributes); - RemoveAnonymousList(m_pAnonymousBoolValues); - nsresult err = card->GetAnonymousStrAttrubutesList(&m_pAnonymousStrAttributes); - err = card->GetAnonymousStrValuesList(&m_pAnonymousStrValues); - err = card->GetAnonymousIntAttrubutesList(&m_pAnonymousIntAttributes); - err = card->GetAnonymousIntValuesList(&m_pAnonymousIntValues); - err = card->GetAnonymousBoolAttrubutesList(&m_pAnonymousBoolAttributes); - err = card->GetAnonymousBoolValuesList(&m_pAnonymousBoolValues); - err = DoAnonymousAttributesTransaction(PR_FALSE); - return err; + GetAnonymousAttributesFromCard(card); + return DoAnonymousAttributesTransaction(AB_NotifyDeleted); } NS_IMETHODIMP nsAddrDatabase::RemoveAnonymousAttributesFromDB() { - return DoAnonymousAttributesTransaction(PR_FALSE); + return DoAnonymousAttributesTransaction(AB_NotifyDeleted); +} + +NS_IMETHODIMP nsAddrDatabase::EditAnonymousAttributesFromCard(nsIAbCard* card) +{ + GetAnonymousAttributesFromCard(card); + return DoAnonymousAttributesTransaction(AB_NotifyPropertyChanged); +} + +NS_IMETHODIMP nsAddrDatabase::EditAnonymousAttributesInDB() +{ + return DoAnonymousAttributesTransaction(AB_NotifyPropertyChanged); } NS_IMETHODIMP nsAddrDatabase::DeleteCard(nsIAbCard *card, PRBool notify) diff --git a/mailnews/addrbook/src/nsAddrDatabase.h b/mailnews/addrbook/src/nsAddrDatabase.h index 4957ef0284a3..2515e5026816 100644 --- a/mailnews/addrbook/src/nsAddrDatabase.h +++ b/mailnews/addrbook/src/nsAddrDatabase.h @@ -94,8 +94,11 @@ public: NS_IMETHOD GetAnonymousBoolAttribute(const char *attrname, PRBool* value); NS_IMETHOD AddAnonymousAttributesToDB(); NS_IMETHOD RemoveAnonymousAttributesFromDB(); + NS_IMETHOD EditAnonymousAttributesInDB(); + NS_IMETHOD AddAnonymousAttributesFromCard(nsIAbCard *card); NS_IMETHOD RemoveAnonymousAttributesFromCard(nsIAbCard *card); + NS_IMETHOD EditAnonymousAttributesFromCard(nsIAbCard *card); ////////////////////////////////////////////////////////////////////////////// // nsAddrDatabase methods: @@ -148,10 +151,12 @@ protected: nsresult RemoveAnonymousList(nsVoidArray* pArray); nsresult SetAnonymousAttribute(nsVoidArray** pAttrAray, nsVoidArray** pValueArray, void *attrname, void *value); - nsresult DoAnonymousAttributesTransaction(PRBool bAdd); - nsresult DoStringAnonymousTransaction(nsVoidArray* pAttributes, nsVoidArray* pValues, PRBool bAdd); - nsresult DoIntAnonymousTransaction(nsVoidArray* pAttributes, nsVoidArray* pValues, PRBool bAdd); - nsresult DoBoolAnonymousTransaction(nsVoidArray* pAttributes, nsVoidArray* pValues, PRBool bAdd); + nsresult DoAnonymousAttributesTransaction(AB_NOTIFY_CODE code); + nsresult DoStringAnonymousTransaction(nsVoidArray* pAttributes, nsVoidArray* pValues, AB_NOTIFY_CODE code); + nsresult DoIntAnonymousTransaction(nsVoidArray* pAttributes, nsVoidArray* pValues, AB_NOTIFY_CODE code); + nsresult DoBoolAnonymousTransaction(nsVoidArray* pAttributes, nsVoidArray* pValues, AB_NOTIFY_CODE code); + void GetAnonymousAttributesFromCard(nsIAbCard* card); + nsresult FindAttributeRow(nsIMdbTable* pTable, mdb_token columnToken, nsIMdbRow** row); static nsVoidArray/**/* GetDBCache(); static nsVoidArray/**/* m_dbCache; diff --git a/mailnews/addrbook/src/nsDirPrefs.cpp b/mailnews/addrbook/src/nsDirPrefs.cpp index 053c4c0ab441..03d10513a8d8 100644 --- a/mailnews/addrbook/src/nsDirPrefs.cpp +++ b/mailnews/addrbook/src/nsDirPrefs.cpp @@ -312,7 +312,7 @@ static nsresult dir_ConvertToMabFileName() { nsString name(server->fileName); PRInt32 pos = name.Find(ABFileName_kPreviousSuffix); - if (pos) + if (pos > 0) { //Move old abook.na2 to end of the list anf change the description DIR_Server * newServer = nsnull;