From 4b5530493a9880fc729fc5083e6663df4b31793a Mon Sep 17 00:00:00 2001 From: "bienvenu%netscape.com" Date: Fri, 13 Aug 1999 00:52:04 +0000 Subject: [PATCH] more work on ab history, not part of build --- .../addrbook/src/nsAbAddressCollecter.cpp | 157 +++++++++++++++--- mailnews/addrbook/src/nsAbAddressCollecter.h | 4 + 2 files changed, 136 insertions(+), 25 deletions(-) diff --git a/mailnews/addrbook/src/nsAbAddressCollecter.cpp b/mailnews/addrbook/src/nsAbAddressCollecter.cpp index 1ce23289273..1d75970c95c 100644 --- a/mailnews/addrbook/src/nsAbAddressCollecter.cpp +++ b/mailnews/addrbook/src/nsAbAddressCollecter.cpp @@ -26,6 +26,7 @@ #include "nsIPref.h" #include "nsIAddrBookSession.h" #include "nsIMsgHeaderParser.h" +#include "nsXPIDLString.h" // For the new pref API's static NS_DEFINE_CID(kPrefCID, NS_PREF_CID); @@ -53,7 +54,7 @@ nsAbAddressCollecter::~nsAbAddressCollecter() NS_IMETHODIMP nsAbAddressCollecter::CollectAddress(const char *address) { nsresult rv; - PRBool collectAddresses; + PRBool collectAddresses = PR_TRUE; NS_WITH_SERVICE(nsIPref, pPref, kPrefCID, &rv); if (NS_FAILED(rv) || !pPref) @@ -87,31 +88,59 @@ NS_IMETHODIMP nsAbAddressCollecter::CollectAddress(const char *address) char *curName = names; char *curAddress = addresses; char *excludeDomainList = nsnull; - PRUint32 numAddresses; - rv = pPref->CopyCharPref("mail.address_collection_ignore_domain_list", &excludeDomainList); - if (NS_SUCCEEDED(rv)) + for (PRUint32 i = 0; i < numAddresses; i++) { - for (PRUint32 i = 0; i < numAddresses; i++) - { - nsCOMPtr senderCard; - rv = nsComponentManager::CreateInstance(kAbCardPropertyCID, nsnull, nsCOMTypeInfo::GetIID(), getter_AddRefs(senderCard)); - if (NS_SUCCEEDED(rv) && senderCard) - { - senderCard->SetDisplayName(curName); - senderCard->SetPrimaryEmail(curAddress); - senderCard->AddCardToDatabase("abdirectory://history.mab"); - } - curName += strlen(curName) + 1; - curAddress += strlen(curAddress) + 1; - } - PR_FREEIF(addresses); - PR_FREEIF(names); - PR_FREEIF(excludeDomainList); - } - } + PRBool exclude; -// pref("mail.address_collection_ignore_domain_list",""); + rv = IsDomainExcluded(curAddress, pPref, &exclude); + if (NS_SUCCEEDED(rv) && !exclude) + { + nsCOMPtr existingCard; + + rv = m_historyAB->GetCardForEmailAddress(curAddress, getter_AddRefs(existingCard)); + + if (!existingCard) + { + nsCOMPtr senderCard; + rv = nsComponentManager::CreateInstance(kAbCardPropertyCID, nsnull, nsCOMTypeInfo::GetIID(), getter_AddRefs(senderCard)); + if (NS_SUCCEEDED(rv) && senderCard) + { + if (curName && nsCRT::strlen(curName) > 0) + { + senderCard->SetDisplayName(curName); + } + else + { + nsCAutoString senderFromEmail(curAddress); + PRInt32 atSignIndex = senderFromEmail.FindChar('@'); + if (atSignIndex > 0) + { + senderFromEmail.Truncate(atSignIndex + 1); + senderCard->SetDisplayName(senderFromEmail); + } + } + senderCard->SetPrimaryEmail(curAddress); + senderCard->AddCardToDatabase("abdirectory://history.mab"); + } + } + else + { + char *displayName = nsnull; + + rv = existingCard->GetDisplayName(&displayName); + if (NS_SUCCEEDED(rv) && displayName) + { + } + } + } + curName += strlen(curName) + 1; + curAddress += strlen(curAddress) + 1; + } + PR_FREEIF(addresses); + PR_FREEIF(names); + PR_FREEIF(excludeDomainList); + } return NS_OK; } @@ -131,12 +160,90 @@ nsresult nsAbAddressCollecter::OpenHistoryAB(nsIAddrDatabase **aDatabase) if (dbPath) { - (*dbPath) += "Collected Addresses.mab"; + (*dbPath) += "history.mab"; NS_WITH_SERVICE(nsIAddrDatabase, addrDBFactory, kAddressBookDBCID, &rv); if (NS_SUCCEEDED(rv) && addrDBFactory) - rv = addrDBFactory->Open(dbPath, PR_FALSE, aDatabase, PR_TRUE); + rv = addrDBFactory->Open(dbPath, PR_TRUE, aDatabase, PR_TRUE); } return rv; } + +nsresult nsAbAddressCollecter::IsDomainExcluded(const char *address, nsIPref *pPref, PRBool *bExclude) +{ + if (!bExclude) + return NS_ERROR_NULL_POINTER; + + *bExclude = PR_FALSE; + + nsXPIDLCString excludedDomainList; + nsresult rv = pPref->CopyCharPref("mail.address_collection_ignore_domain_list", + getter_Copies(excludedDomainList)); + + if (NS_FAILED(rv) || !excludedDomainList || !excludedDomainList[0]) + return NS_OK; + + nsCAutoString incomingDomain(address); + PRInt32 atSignIndex = incomingDomain.RFindChar('@'); + if (atSignIndex > 0) + { + incomingDomain.Cut(0, atSignIndex + 1); + + char *token = nsnull; + char *rest = NS_CONST_CAST(char*,(const char*)excludedDomainList); + nsCAutoString str; + + token = nsCRT::strtok(rest, ",", &rest); + while (token && *token) + { + str = token; + str.StripWhitespace(); + + if (!str.IsEmpty()) + { + if (str.Equals(incomingDomain)) + { + *bExclude = PR_TRUE; + break; + } + } + str = ""; + token = nsCRT::strtok(rest, ",", &rest); + } + } + return rv; +} + +nsresult nsAbAddressCollecter::SplitFullName (const char *fullName, char **firstName, char **lastName) +{ + if (fullName) + { + *firstName = nsCRT::strdup(fullName); + if (NULL == *firstName) + return NS_ERROR_OUT_OF_MEMORY; + + char *plastSpace = *firstName; + char *walkName = *firstName; + char *plastName = nsnull; + + while (walkName && *walkName) + { + if (*walkName == ' ') + { + plastSpace = walkName; + plastName = ++plastSpace; + } + + walkName++; + } + + if (plastName) + { + *plastSpace = '\0'; + *lastName = nsCRT::strdup (plastName); + } + } + + return NS_OK; +} diff --git a/mailnews/addrbook/src/nsAbAddressCollecter.h b/mailnews/addrbook/src/nsAbAddressCollecter.h index e395d28dadb..0fc58138961 100644 --- a/mailnews/addrbook/src/nsAbAddressCollecter.h +++ b/mailnews/addrbook/src/nsAbAddressCollecter.h @@ -25,6 +25,8 @@ #include "nsIAddrDatabase.h" #include "nsAddrDatabase.h" +class nsIPref; + class nsAbAddressCollecter : public nsIAbAddressCollecter { public: @@ -35,6 +37,8 @@ public: NS_IMETHOD CollectAddress(const char *address); nsresult OpenHistoryAB(nsIAddrDatabase **aDatabase); + nsresult IsDomainExcluded(const char *address, nsIPref *pPref, PRBool *bExclude); + nsresult SplitFullName (const char *fullName, char **firstName, char **lastName); protected: nsCOMPtr m_historyAB;