1999-09-06 22:24:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-09-06 22:24:10 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-09-06 22:24:10 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-09-06 22:24:10 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-09-06 22:24:10 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
A sample of XPConnect. This file contains an implementation of
|
|
|
|
nsISample.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2000-01-29 02:51:39 +03:00
|
|
|
#include "nsIImportService.h"
|
|
|
|
#include "nsIImportFieldMap.h"
|
1999-09-06 22:24:10 +04:00
|
|
|
#include "nsABBaseCID.h"
|
|
|
|
#include "nsIAbCard.h"
|
|
|
|
|
|
|
|
#include "nsOEAddressIterator.h"
|
|
|
|
|
|
|
|
#include "OEDebugLog.h"
|
|
|
|
|
1999-10-24 20:09:21 +04:00
|
|
|
static NS_DEFINE_CID(kAbCardCID, NS_ABCARD_CID);
|
1999-09-06 22:24:10 +04:00
|
|
|
static NS_DEFINE_CID(kAbCardPropertyCID, NS_ABCARDPROPERTY_CID);
|
2000-01-29 02:51:39 +03:00
|
|
|
static NS_DEFINE_CID(kImportServiceCID, NS_IMPORTSERVICE_CID);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PRInt32 mozField;
|
|
|
|
PRInt32 multiLine;
|
|
|
|
ULONG mapiTag;
|
|
|
|
} MAPIFields;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Fields in MAPI, not in Mozilla
|
|
|
|
PR_OFFICE_LOCATION
|
|
|
|
FIX - PR_BIRTHDAY - stored as PT_SYSTIME - FIX to extract for moz address book birthday
|
|
|
|
PR_DISPLAY_NAME_PREFIX - Mr., Mrs. Dr., etc.
|
|
|
|
PR_SPOUSE_NAME
|
|
|
|
PR_GENDER - integer, not text
|
|
|
|
FIX - PR_CONTACT_EMAIL_ADDRESSES - multiuline strings for email addresses, needs
|
|
|
|
parsing to get secondary email address for mozilla
|
|
|
|
*/
|
1999-09-06 22:24:10 +04:00
|
|
|
|
2000-01-29 02:51:39 +03:00
|
|
|
#define kIsMultiLine -2
|
|
|
|
#define kNoMultiLine -1
|
|
|
|
|
|
|
|
MAPIFields gMapiFields[] = {
|
|
|
|
{ 35, kIsMultiLine, PR_COMMENT},
|
|
|
|
{ 6, kNoMultiLine, PR_BUSINESS_TELEPHONE_NUMBER},
|
|
|
|
{ 7, kNoMultiLine, PR_HOME_TELEPHONE_NUMBER},
|
|
|
|
{ 25, kNoMultiLine, PR_COMPANY_NAME},
|
|
|
|
{ 23, kNoMultiLine, PR_TITLE},
|
|
|
|
{ 10, kNoMultiLine, PR_CELLULAR_TELEPHONE_NUMBER},
|
|
|
|
{ 9, kNoMultiLine, PR_PAGER_TELEPHONE_NUMBER},
|
|
|
|
{ 8, kNoMultiLine, PR_BUSINESS_FAX_NUMBER},
|
|
|
|
{ 8, kNoMultiLine, PR_HOME_FAX_NUMBER},
|
|
|
|
{ 22, kNoMultiLine, PR_COUNTRY},
|
|
|
|
{ 19, kNoMultiLine, PR_LOCALITY},
|
|
|
|
{ 20, kNoMultiLine, PR_STATE_OR_PROVINCE},
|
|
|
|
{ 17, 18, PR_STREET_ADDRESS},
|
|
|
|
{ 21, kNoMultiLine, PR_POSTAL_CODE},
|
|
|
|
{ 26, kNoMultiLine, PR_PERSONAL_HOME_PAGE},
|
|
|
|
{ 27, kNoMultiLine, PR_BUSINESS_HOME_PAGE},
|
|
|
|
{ 13, kNoMultiLine, PR_HOME_ADDRESS_CITY},
|
|
|
|
{ 16, kNoMultiLine, PR_HOME_ADDRESS_COUNTRY},
|
|
|
|
{ 15, kNoMultiLine, PR_HOME_ADDRESS_POSTAL_CODE},
|
|
|
|
{ 14, kNoMultiLine, PR_HOME_ADDRESS_STATE_OR_PROVINCE},
|
|
|
|
{ 11, 12, PR_HOME_ADDRESS_STREET},
|
|
|
|
{ 24, kNoMultiLine, PR_DEPARTMENT_NAME}
|
|
|
|
};
|
1999-09-06 22:24:10 +04:00
|
|
|
|
|
|
|
nsOEAddressIterator::nsOEAddressIterator( CWAB *pWab, nsIAddrDatabase *database)
|
|
|
|
{
|
|
|
|
m_pWab = pWab;
|
|
|
|
m_database = database;
|
|
|
|
NS_IF_ADDREF( m_database);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsOEAddressIterator::~nsOEAddressIterator()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE( m_database);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsOEAddressIterator::EnumUser( LPCTSTR pName, LPENTRYID pEid, ULONG cbEid)
|
|
|
|
{
|
|
|
|
IMPORT_LOG1( "User: %s\n", pName);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (m_database) {
|
|
|
|
LPMAILUSER pUser = m_pWab->GetUser( cbEid, pEid);
|
|
|
|
if (pUser) {
|
|
|
|
// Get a new row from the database!
|
|
|
|
nsIMdbRow* newRow = nsnull;
|
|
|
|
m_database->GetNewRow( &newRow);
|
|
|
|
// FIXME: Check with Candice about releasing the newRow if it
|
|
|
|
// isn't added to the database. Candice's code in nsAddressBook
|
|
|
|
// never releases it but that doesn't seem right to me!
|
|
|
|
if (newRow) {
|
|
|
|
if (BuildCard( pName, newRow, pUser)) {
|
|
|
|
m_database->AddCardRowToDB( newRow);
|
1999-09-10 07:29:46 +04:00
|
|
|
IMPORT_LOG0( "* Added entry to address book database\n");
|
1999-09-06 22:24:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_pWab->ReleaseUser( pUser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsOEAddressIterator::EnumList( LPCTSTR pName, LPENTRYID pEid, ULONG cbEid)
|
|
|
|
{
|
|
|
|
IMPORT_LOG1( "List: %s\n", pName);
|
|
|
|
return( PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsOEAddressIterator::SanitizeValue( nsString& val)
|
|
|
|
{
|
2000-01-29 02:51:39 +03:00
|
|
|
val.ReplaceSubstring( "\x0D\x0A", ", ");
|
|
|
|
val.ReplaceChar( 13, ',');
|
|
|
|
val.ReplaceChar( 10, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsOEAddressIterator::SplitString( nsString& val1, nsString& val2)
|
|
|
|
{
|
|
|
|
nsString temp;
|
|
|
|
|
|
|
|
// Find the last line if there is more than one!
|
|
|
|
PRInt32 idx = val1.RFind( "\x0D\x0A");
|
|
|
|
PRInt32 cnt = 2;
|
|
|
|
if (idx == -1) {
|
|
|
|
cnt = 1;
|
|
|
|
idx = val1.RFindChar( 13);
|
|
|
|
}
|
|
|
|
if (idx == -1)
|
|
|
|
idx= val1.RFindChar( 10);
|
|
|
|
if (idx != -1) {
|
|
|
|
val1.Right( val2, val1.Length() - idx - cnt);
|
|
|
|
val1.Left( temp, idx);
|
|
|
|
val1 = temp;
|
|
|
|
SanitizeValue( val1);
|
|
|
|
}
|
1999-09-06 22:24:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsOEAddressIterator::BuildCard( LPCTSTR pName, nsIMdbRow *newRow, LPMAILUSER pUser)
|
|
|
|
{
|
|
|
|
|
|
|
|
nsString lastName;
|
|
|
|
nsString firstName;
|
|
|
|
nsString eMail;
|
|
|
|
nsString nickName;
|
|
|
|
nsString middleName;
|
|
|
|
|
|
|
|
LPSPropValue pProp = m_pWab->GetUserProperty( pUser, PR_EMAIL_ADDRESS);
|
|
|
|
if (pProp) {
|
|
|
|
m_pWab->GetValueString( pProp, eMail);
|
|
|
|
SanitizeValue( eMail);
|
|
|
|
m_pWab->FreeProperty( pProp);
|
|
|
|
}
|
|
|
|
pProp = m_pWab->GetUserProperty( pUser, PR_GIVEN_NAME);
|
|
|
|
if (pProp) {
|
|
|
|
m_pWab->GetValueString( pProp, firstName);
|
|
|
|
SanitizeValue( firstName);
|
|
|
|
m_pWab->FreeProperty( pProp);
|
|
|
|
}
|
|
|
|
pProp = m_pWab->GetUserProperty( pUser, PR_SURNAME);
|
|
|
|
if (pProp) {
|
|
|
|
m_pWab->GetValueString( pProp, lastName);
|
|
|
|
SanitizeValue( lastName);
|
|
|
|
m_pWab->FreeProperty( pProp);
|
|
|
|
}
|
|
|
|
pProp = m_pWab->GetUserProperty( pUser, PR_MIDDLE_NAME);
|
|
|
|
if (pProp) {
|
|
|
|
m_pWab->GetValueString( pProp, middleName);
|
|
|
|
SanitizeValue( middleName);
|
|
|
|
m_pWab->FreeProperty( pProp);
|
|
|
|
}
|
|
|
|
pProp = m_pWab->GetUserProperty( pUser, PR_NICKNAME);
|
|
|
|
if (pProp) {
|
|
|
|
m_pWab->GetValueString( pProp, nickName);
|
|
|
|
SanitizeValue( nickName);
|
|
|
|
m_pWab->FreeProperty( pProp);
|
|
|
|
}
|
|
|
|
if (nickName.IsEmpty())
|
|
|
|
nickName = pName;
|
|
|
|
if (firstName.IsEmpty()) {
|
|
|
|
firstName = nickName;
|
|
|
|
middleName.Truncate();
|
|
|
|
lastName.Truncate();
|
|
|
|
}
|
|
|
|
if (lastName.IsEmpty())
|
|
|
|
middleName.Truncate();
|
|
|
|
|
|
|
|
if (eMail.IsEmpty())
|
|
|
|
eMail = nickName;
|
|
|
|
|
|
|
|
nsString displayName;
|
2000-01-29 02:51:39 +03:00
|
|
|
pProp = m_pWab->GetUserProperty( pUser, PR_DISPLAY_NAME);
|
|
|
|
if (pProp) {
|
|
|
|
m_pWab->GetValueString( pProp, displayName);
|
|
|
|
SanitizeValue( displayName);
|
|
|
|
m_pWab->FreeProperty( pProp);
|
|
|
|
}
|
|
|
|
if (displayName.IsEmpty()) {
|
|
|
|
if (firstName.IsEmpty())
|
|
|
|
displayName = nickName;
|
|
|
|
else {
|
|
|
|
displayName = firstName;
|
|
|
|
if (!middleName.IsEmpty()) {
|
|
|
|
displayName.Append( ' ');
|
|
|
|
displayName.Append( middleName);
|
|
|
|
}
|
|
|
|
if (!lastName.IsEmpty()) {
|
|
|
|
displayName.Append( ' ');
|
|
|
|
displayName.Append( lastName);
|
|
|
|
}
|
1999-09-06 22:24:10 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *pCStr;
|
|
|
|
// We now have the required fields
|
|
|
|
// write them out followed by any optional fields!
|
2000-01-29 02:51:39 +03:00
|
|
|
if (!displayName.IsEmpty()) {
|
2000-03-03 04:15:00 +03:00
|
|
|
m_database->AddDisplayName( newRow, pCStr = displayName.ToNewUTF8String());
|
2000-01-29 02:51:39 +03:00
|
|
|
nsCRT::free( pCStr);
|
|
|
|
}
|
1999-09-06 22:24:10 +04:00
|
|
|
if (!firstName.IsEmpty()) {
|
2000-03-03 04:15:00 +03:00
|
|
|
m_database->AddFirstName( newRow, pCStr = firstName.ToNewUTF8String());
|
1999-09-06 22:24:10 +04:00
|
|
|
nsCRT::free( pCStr);
|
|
|
|
}
|
|
|
|
if (!lastName.IsEmpty()) {
|
2000-03-03 04:15:00 +03:00
|
|
|
m_database->AddLastName( newRow, pCStr = lastName.ToNewUTF8String());
|
1999-09-06 22:24:10 +04:00
|
|
|
nsCRT::free( pCStr);
|
|
|
|
}
|
2000-01-29 02:51:39 +03:00
|
|
|
if (!nickName.IsEmpty()) {
|
2000-03-03 04:15:00 +03:00
|
|
|
m_database->AddNickName( newRow, pCStr = nickName.ToNewUTF8String());
|
2000-01-29 02:51:39 +03:00
|
|
|
nsCRT::free( pCStr);
|
|
|
|
}
|
|
|
|
if (!eMail.IsEmpty()) {
|
2000-03-03 04:15:00 +03:00
|
|
|
m_database->AddPrimaryEmail( newRow, pCStr = eMail.ToNewUTF8String());
|
2000-01-29 02:51:39 +03:00
|
|
|
nsCRT::free( pCStr);
|
|
|
|
}
|
1999-09-06 22:24:10 +04:00
|
|
|
|
2000-01-29 02:51:39 +03:00
|
|
|
// Do all of the extra fields!
|
1999-09-06 22:24:10 +04:00
|
|
|
|
2000-01-29 02:51:39 +03:00
|
|
|
nsString value;
|
|
|
|
nsString line2;
|
|
|
|
nsresult rv;
|
|
|
|
// Create a field map
|
1999-09-06 22:24:10 +04:00
|
|
|
|
2000-01-29 02:51:39 +03:00
|
|
|
NS_WITH_SERVICE( nsIImportService, impSvc, kImportServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED( rv)) {
|
|
|
|
nsIImportFieldMap * pFieldMap = nsnull;
|
|
|
|
rv = impSvc->CreateNewFieldMap( &pFieldMap);
|
|
|
|
if (NS_SUCCEEDED( rv) && pFieldMap) {
|
|
|
|
int max = sizeof( gMapiFields) / sizeof( MAPIFields);
|
|
|
|
for (int i = 0; i < max; i++) {
|
|
|
|
pProp = m_pWab->GetUserProperty( pUser, gMapiFields[i].mapiTag);
|
|
|
|
if (pProp) {
|
|
|
|
m_pWab->GetValueString( pProp, value);
|
|
|
|
m_pWab->FreeProperty( pProp);
|
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
if (gMapiFields[i].multiLine == kNoMultiLine) {
|
|
|
|
SanitizeValue( value);
|
|
|
|
pFieldMap->SetFieldValue( m_database, newRow, gMapiFields[i].mozField, value.GetUnicode());
|
|
|
|
}
|
|
|
|
else if (gMapiFields[i].multiLine == kIsMultiLine) {
|
|
|
|
pFieldMap->SetFieldValue( m_database, newRow, gMapiFields[i].mozField, value.GetUnicode());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
line2.Truncate();
|
|
|
|
SplitString( value, line2);
|
|
|
|
if (!value.IsEmpty())
|
|
|
|
pFieldMap->SetFieldValue( m_database, newRow, gMapiFields[i].mozField, value.GetUnicode());
|
|
|
|
if (!line2.IsEmpty())
|
|
|
|
pFieldMap->SetFieldValue( m_database, newRow, gMapiFields[i].multiLine, line2.GetUnicode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-09-06 22:24:10 +04:00
|
|
|
}
|
2000-01-29 02:51:39 +03:00
|
|
|
// call fieldMap SetFieldValue based on the table of fields
|
|
|
|
|
|
|
|
NS_RELEASE( pFieldMap);
|
1999-09-06 22:24:10 +04:00
|
|
|
}
|
|
|
|
}
|
2000-01-29 02:51:39 +03:00
|
|
|
|
1999-09-06 22:24:10 +04:00
|
|
|
|
|
|
|
return( PR_TRUE);
|
|
|
|
}
|