fix for #168269. autocollect improvements. autocollect screennames and displaynames.

r/sr=bienvenu
This commit is contained in:
sspitzer%netscape.com 2002-09-13 14:51:17 +00:00
Родитель 6be681ed96
Коммит 46ff07f829
6 изменённых файлов: 123 добавлений и 67 удалений

Просмотреть файл

@ -20,6 +20,7 @@
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Seth Spitzer <sspitzer@netscape.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
@ -37,17 +38,16 @@
#include "nsISupports.idl"
[scriptable, uuid(fe04c8e6-501e-11d3-a527-0060b0fc04b7)]
interface nsIAbAddressCollecter : nsISupports {
[noscript] void collectAddress(in string address, in boolean aCreateCard);
void collectAddress(in string address);
/* CollectUnicodeAddress is the most I18N friendly
of the two methods to call. If you just take a char * address
as used by CollectAddress then you don't know what char set
the address is in (UTF-8??).
as used by CollectAddress then you don't know what char set
the address is in (UTF-8??).
*/
void collectUnicodeAddress(in wstring address);
void collectUnicodeAddress(in wstring address, in boolean aCreateCard);
};

Просмотреть файл

@ -72,30 +72,18 @@ nsAbAddressCollecter::~nsAbAddressCollecter()
}
}
NS_IMETHODIMP nsAbAddressCollecter::CollectUnicodeAddress(const PRUnichar * aAddress)
NS_IMETHODIMP nsAbAddressCollecter::CollectUnicodeAddress(const PRUnichar * aAddress, PRBool aCreateCard)
{
NS_ENSURE_ARG_POINTER(aAddress);
nsresult rv = NS_OK;
// convert the unicode string to UTF-8...
nsAutoString unicodeString (aAddress);
char * utf8Version = ToNewUTF8String(unicodeString);
if (utf8Version) {
rv = CollectAddress(utf8Version);
Recycle(utf8Version);
}
nsresult rv = CollectAddress(NS_ConvertUCS2toUTF8(aAddress).get(), aCreateCard);
NS_ENSURE_SUCCESS(rv,rv);
return rv;
}
NS_IMETHODIMP nsAbAddressCollecter::CollectAddress(const char *address)
NS_IMETHODIMP nsAbAddressCollecter::CollectAddress(const char *address, PRBool aCreateCard)
{
nsresult rv;
nsCOMPtr<nsIPref> pPref(do_GetService(NS_PREF_CONTRACTID, &rv));
NS_ENSURE_SUCCESS(rv, rv);
rv = OpenDatabase();
nsresult rv = OpenDatabase();
NS_ENSURE_SUCCESS(rv, rv);
// note that we're now setting the whole recipient list,
@ -125,34 +113,39 @@ NS_IMETHODIMP nsAbAddressCollecter::CollectAddress(const char *address)
// PR_TRUE (ie, case insensitive) without reading bugs #128535 and #121478.
rv = m_database->GetCardFromAttribute(m_directory, kPriEmailColumn, curAddress, PR_FALSE /* retain case */, getter_AddRefs(existingCard));
if (!existingCard)
if (!existingCard && aCreateCard)
{
nsCOMPtr<nsIAbCard> senderCard = do_CreateInstance(NS_ABCARDPROPERTY_CONTRACTID, &rv);
if (NS_SUCCEEDED(rv) && senderCard)
{
if (curName && strlen(curName) > 0)
SetNamesForCard(senderCard, curName);
else
{
nsAutoString senderFromEmail(NS_ConvertASCIItoUCS2(curAddress).get());
PRInt32 atSignIndex = senderFromEmail.FindChar('@');
if (atSignIndex > 0)
{
senderFromEmail.Truncate(atSignIndex);
senderCard->SetDisplayName(senderFromEmail.get());
}
PRBool modifiedCard;
if (curName && strlen(curName) > 0) {
rv = SetNamesForCard(senderCard, curName, &modifiedCard);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to set names");
}
senderCard->SetPrimaryEmail(NS_ConvertASCIItoUCS2(curAddress).get());
rv = AutoCollectScreenName(senderCard, curAddress, &modifiedCard);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to set screenname");
rv = senderCard->SetPrimaryEmail(NS_ConvertASCIItoUCS2(curAddress).get());
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to set email");
rv = AddCardToAddressBook(senderCard);
NS_ENSURE_SUCCESS(rv,rv);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to add card");
}
}
else // address is already in the AB
{
SetNamesForCard(existingCard, curName);
existingCard->EditCardToDatabase(m_abURI.get());
else if (existingCard) {
// address is already in the AB, so update the names
PRBool setNames;
rv = SetNamesForCard(existingCard, curName, &setNames);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to set names");
PRBool setScreenName;
rv = AutoCollectScreenName(existingCard, curAddress, &setScreenName);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to set screen name");
if (setScreenName || setNames)
existingCard->EditCardToDatabase(m_abURI.get());
}
curName += strlen(curName) + 1;
@ -165,6 +158,47 @@ NS_IMETHODIMP nsAbAddressCollecter::CollectAddress(const char *address)
return NS_OK;
}
nsresult nsAbAddressCollecter::AutoCollectScreenName(nsIAbCard *aCard, const char *aEmail, PRBool *aModifiedCard)
{
NS_ENSURE_ARG_POINTER(aCard);
NS_ENSURE_ARG_POINTER(aEmail);
NS_ENSURE_ARG_POINTER(aModifiedCard);
*aModifiedCard = PR_FALSE;
nsXPIDLString screenName;
nsresult rv = aCard->GetAimScreenName(getter_Copies(screenName));
NS_ENSURE_SUCCESS(rv,rv);
// don't override existing screennames
if (!screenName.IsEmpty())
return NS_OK;
const char *atPos = strchr(aEmail, '@');
if (!atPos)
return NS_OK;
const char *domain = atPos + 1;
if (!domain)
return NS_OK;
// username in username@aol.com and username@netscape.net are also
// AIM screennames.
if (strcmp(domain,"aol.com") && strcmp(domain,"netscape.net"))
return NS_OK;
nsAutoString userName(NS_ConvertASCIItoUCS2(aEmail).get());
userName.SetLength(atPos - aEmail);
rv = aCard->SetAimScreenName(userName.get());
NS_ENSURE_SUCCESS(rv,rv);
*aModifiedCard = PR_TRUE;
return rv;
}
nsresult nsAbAddressCollecter::OpenDatabase()
{
// check if already open
@ -195,14 +229,24 @@ nsresult nsAbAddressCollecter::OpenDatabase()
}
nsresult
nsAbAddressCollecter::SetNamesForCard(nsIAbCard *senderCard, const char *fullName)
nsAbAddressCollecter::SetNamesForCard(nsIAbCard *senderCard, const char *fullName, PRBool *aModifiedCard)
{
char *firstName = nsnull;
char *lastName = nsnull;
*aModifiedCard = PR_FALSE;
nsXPIDLString displayName;
nsresult rv = senderCard->GetDisplayName(getter_Copies(displayName));
NS_ENSURE_SUCCESS(rv,rv);
// we already have a display name, so don't do anything
if (!displayName.IsEmpty())
return NS_OK;
senderCard->SetDisplayName(NS_ConvertUTF8toUCS2(fullName).get());
*aModifiedCard = PR_TRUE;
nsresult rv = SplitFullName(fullName, &firstName, &lastName);
rv = SplitFullName(fullName, &firstName, &lastName);
if (NS_SUCCEEDED(rv))
{
senderCard->SetFirstName(NS_ConvertUTF8toUCS2(firstName).get());
@ -253,7 +297,7 @@ nsAbAddressCollecter::collectAddressBookPrefChanged(const char *aNewpref, void *
{
nsresult rv;
nsAbAddressCollecter *adCol = (nsAbAddressCollecter *) aData;
nsCOMPtr<nsIPref> pPref(do_GetService(NS_PREF_CONTRACTID, &rv));
nsCOMPtr<nsIPref> pPref = do_GetService(NS_PREF_CONTRACTID, &rv);
NS_ASSERTION(NS_SUCCEEDED(rv), "failed to get prefs");
nsXPIDLCString prefVal;
@ -266,7 +310,7 @@ nsAbAddressCollecter::collectAddressBookPrefChanged(const char *aNewpref, void *
nsresult nsAbAddressCollecter::Init(void)
{
nsresult rv;
nsCOMPtr<nsIPref> pPref(do_GetService(NS_PREF_CONTRACTID, &rv));
nsCOMPtr<nsIPref> pPref = do_GetService(NS_PREF_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv,rv);
rv = pPref->RegisterCallback(PREF_MAIL_COLLECT_ADDRESSBOOK, collectAddressBookPrefChanged, this);

Просмотреть файл

@ -41,13 +41,10 @@
#include "nsIAbAddressCollecter.h"
#include "nsCOMPtr.h"
#include "nsIAbAddressCollecter.h"
#include "nsIAddrDatabase.h"
#include "nsAddrDatabase.h"
#include "nsIAbDirectory.h"
#include "nsIAbCard.h"
class nsIPref;
class nsAbAddressCollecter : public nsIAbAddressCollecter
{
public:
@ -59,15 +56,14 @@ public:
nsresult SetAbURI(const char *aURI);
nsresult OpenDatabase();
nsresult SetNamesForCard(nsIAbCard *senderCard, const char *fullName);
nsresult SplitFullName (const char *fullName, char **firstName, char **lastName);
nsresult Init();
private:
static int PR_CALLBACK collectAddressBookPrefChanged(const char *newpref, void *data);
nsresult AddCardToAddressBook(nsIAbCard *card);
protected:
nsresult AutoCollectScreenName(nsIAbCard *aCard, const char *aEmail, PRBool *aModifiedCard);
nsresult SetNamesForCard(nsIAbCard *senderCard, const char *fullName, PRBool *aModifiedCard);
nsresult SplitFullName (const char *fullName, char **firstName, char **lastName);
nsCOMPtr <nsIAddrDatabase> m_database;
nsCOMPtr <nsIAbDirectory> m_directory;
nsCString m_abURI;

Просмотреть файл

@ -102,7 +102,8 @@ NS_IMETHODIMP nsAddrBookSession::NotifyItemPropertyChanged(nsISupports *item, co
for(i = 0; i < count; i++)
{
if (mListenerNotifyFlags[i] & nsIAbListener::changed) {
nsCOMPtr<nsIAbListener> listener = getter_AddRefs((nsIAbListener*)mListeners->ElementAt(i));
nsCOMPtr<nsIAbListener> listener;
mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
NS_ASSERTION(listener, "listener is null");
if (listener)
listener->OnItemPropertyChanged(item, property, oldValue, newValue);
@ -122,7 +123,8 @@ NS_IMETHODIMP nsAddrBookSession::NotifyDirectoryItemAdded(nsIAbDirectory *direct
for(i = 0; i < count; i++)
{
if (mListenerNotifyFlags[i] & nsIAbListener::added) {
nsCOMPtr<nsIAbListener> listener = getter_AddRefs((nsIAbListener*)mListeners->ElementAt(i));
nsCOMPtr<nsIAbListener> listener;
mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
NS_ASSERTION(listener, "listener is null");
if (listener)
listener->OnItemAdded(directory, item);
@ -144,7 +146,8 @@ NS_IMETHODIMP nsAddrBookSession::NotifyDirectoryItemDeleted(nsIAbDirectory *dire
for(i = 0; i < count; i++)
{
if (mListenerNotifyFlags[i] & nsIAbListener::directoryItemRemoved) {
nsCOMPtr<nsIAbListener> listener = getter_AddRefs((nsIAbListener*)mListeners->ElementAt(i));
nsCOMPtr<nsIAbListener> listener;
mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
NS_ASSERTION(listener, "listener is null");
if (listener)
listener->OnItemRemoved(directory, item);
@ -165,7 +168,8 @@ NS_IMETHODIMP nsAddrBookSession::NotifyDirectoryDeleted(nsIAbDirectory *director
for(i = 0; i < count; i++)
{
if (mListenerNotifyFlags[i] & nsIAbListener::directoryRemoved) {
nsCOMPtr<nsIAbListener> listener = getter_AddRefs(NS_STATIC_CAST(nsIAbListener *,mListeners->ElementAt(i)));
nsCOMPtr<nsIAbListener> listener;
mListeners->QueryElementAt(i, NS_GET_IID(nsIAbListener), (void **) getter_AddRefs(listener));
NS_ASSERTION(listener, "listener is null");
if (listener)
listener->OnItemRemoved(directory, item);

Просмотреть файл

@ -42,10 +42,11 @@ var gViewAllHeaders = false;
var gNumAddressesToShow = 3;
var gShowUserAgent = false;
var gCollectIncoming = false;
var gCollectOutgoing = false;
var gCollectNewsgroup = false;
var gCollapsedHeaderViewMode = false;
var gCollectAddressTimer = null;
var gCollectAddess = null;
var gCollectAddress = null;
var gBuildAttachmentsForCurrentMsg = false;
var gBuildAttachmentPopupForCurrentMsg = true;
var gBuiltExpandedView = false;
@ -203,6 +204,7 @@ function OnLoadMsgHeaderPane()
gNumAddressesToShow = pref.getIntPref("mailnews.max_header_display_length");
gCollectIncoming = pref.getBoolPref("mail.collect_email_address_incoming");
gCollectNewsgroup = pref.getBoolPref("mail.collect_email_address_newsgroup");
gCollectOutgoing = pref.getBoolPref("mail.collect_email_address_outgoing");
gShowUserAgent = pref.getBoolPref("mailnews.headers.showUserAgent");
initializeHeaderViewTables();
@ -237,7 +239,7 @@ var messageHeaderSink = {
// clear out any pending collected address timers...
if (gCollectAddressTimer)
{
gCollectAddess = "";
gCollectAddress = "";
clearTimeout(gCollectAddressTimer);
gCollectAddressTimer = null;
}
@ -316,11 +318,21 @@ var messageHeaderSink = {
if (lowerCaseHeaderName == "from")
{
if (foo.headerValue && abAddressCollector && ((gCollectIncoming && !dontCollectAddress) || (gCollectNewsgroup && dontCollectAddress)))
{
gCollectAddess = foo.headerValue;
gCollectAddressTimer = setTimeout('abAddressCollector.collectUnicodeAddress(gCollectAddess);', 2000);
}
if (foo.headerValue && abAddressCollector) {
if ((gCollectIncoming && !dontCollectAddress) ||
(gCollectNewsgroup && dontCollectAddress))
{
gCollectAddress = foo.headerValue;
// collect, and add card if doesn't exist
gCollectAddressTimer = setTimeout('abAddressCollector.collectUnicodeAddress(gCollectAddress, true);', 2000);
}
else if (gCollectOutgoing)
{
// collect, but only update existing cards
gCollectAddress = foo.headerValue;
gCollectAddressTimer = setTimeout('abAddressCollector.collectUnicodeAddress(gCollectAddress, false);', 2000);
}
}
}
index++;

Просмотреть файл

@ -3248,19 +3248,19 @@ nsMsgComposeAndSend::DeliverFileAsMail()
{
PL_strcat (buf2, mCompFields->GetTo());
if (collectAddresses)
addressCollecter->CollectAddress(mCompFields->GetTo());
addressCollecter->CollectAddress(mCompFields->GetTo(), PR_TRUE);
}
if (mCompFields->GetCc() && *mCompFields->GetCc()) {
if (*buf2) PL_strcat (buf2, ",");
PL_strcat (buf2, mCompFields->GetCc());
if (collectAddresses)
addressCollecter->CollectAddress(mCompFields->GetCc());
addressCollecter->CollectAddress(mCompFields->GetCc(), PR_TRUE);
}
if (mCompFields->GetBcc() && *mCompFields->GetBcc()) {
if (mCompFields->GetBcc() && *mCompFields->GetBcc(), PR_TRUE) {
if (*buf2) PL_strcat (buf2, ",");
PL_strcat (buf2, mCompFields->GetBcc());
if (collectAddresses)
addressCollecter->CollectAddress(mCompFields->GetBcc());
addressCollecter->CollectAddress(mCompFields->GetBcc(), PR_TRUE);
}
// Ok, now MIME II encode this to prevent 8bit problems...