diff --git a/mailnews/import/oexpress/makefile.win b/mailnews/import/oexpress/makefile.win index 481fa467655..c044d94f2f2 100644 --- a/mailnews/import/oexpress/makefile.win +++ b/mailnews/import/oexpress/makefile.win @@ -32,6 +32,7 @@ CPP_OBJS=\ .\$(OBJDIR)\WabObject.obj \ .\$(OBJDIR)\nsOERegUtil.obj \ .\$(OBJDIR)\nsOE5File.obj \ + .\$(OBJDIR)\nsOESettings.obj \ $(NULL) LLIBS=\ diff --git a/mailnews/import/oexpress/nsOEAddressIterator.cpp b/mailnews/import/oexpress/nsOEAddressIterator.cpp index f40ab78a45c..c340d1399e1 100644 --- a/mailnews/import/oexpress/nsOEAddressIterator.cpp +++ b/mailnews/import/oexpress/nsOEAddressIterator.cpp @@ -71,6 +71,7 @@ PRBool nsOEAddressIterator::EnumUser( LPCTSTR pName, LPENTRYID pEid, ULONG cbEid if (newRow) { if (BuildCard( pName, newRow, pUser)) { m_database->AddCardRowToDB( newRow); + IMPORT_LOG0( "* Added entry to address book database\n"); } } m_pWab->ReleaseUser( pUser); diff --git a/mailnews/import/oexpress/nsOEFactory.cpp b/mailnews/import/oexpress/nsOEFactory.cpp index f62833cb6aa..bf57426b0d0 100644 --- a/mailnews/import/oexpress/nsOEFactory.cpp +++ b/mailnews/import/oexpress/nsOEFactory.cpp @@ -18,9 +18,7 @@ /* - A sample of XPConnect. This file contains the XPCOM factory the - creates for SampleImpl objects. - + Outlook Express (Win32) import module */ #include "nsCOMPtr.h" diff --git a/mailnews/import/oexpress/nsOEImport.cpp b/mailnews/import/oexpress/nsOEImport.cpp index 91ef4eb0ac1..98b776a8737 100644 --- a/mailnews/import/oexpress/nsOEImport.cpp +++ b/mailnews/import/oexpress/nsOEImport.cpp @@ -19,8 +19,7 @@ /* - A sample of XPConnect. This file contains an implementation of - nsISample. + Outlook Express (Win32) import mail and addressbook interfaces */ #include "nscore.h" @@ -43,6 +42,7 @@ #include "nsIOutputStream.h" #include "nsOE5File.h" #include "nsIAddrDatabase.h" +#include "nsOESettings.h" #include "OEDebugLog.h" @@ -257,6 +257,16 @@ NS_IMETHODIMP nsOEImport::GetImportInterface( const char *pImportType, nsISuppor return( rv); } + if (!nsCRT::strcmp( pImportType, "settings")) { + nsIImportSettings *pSettings = nsnull; + rv = nsOESettings::Create( &pSettings); + if (NS_SUCCEEDED( rv)) { + pSettings->QueryInterface( kISupportsIID, (void **)ppInterface); + } + NS_IF_RELEASE( pSettings); + return( rv); + } + return( NS_ERROR_NOT_AVAILABLE); } diff --git a/mailnews/import/oexpress/nsOESettings.cpp b/mailnews/import/oexpress/nsOESettings.cpp new file mode 100644 index 00000000000..1a5e9de6501 --- /dev/null +++ b/mailnews/import/oexpress/nsOESettings.cpp @@ -0,0 +1,538 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +/* + + Outlook Express (Win32) settings + +*/ + +#include "nsCOMPtr.h" +#include "nscore.h" +#include "nsOEImport.h" +#include "nsIComponentManager.h" +#include "nsIServiceManager.h" +#include "nsIImportService.h" +#include "nsOERegUtil.h" +#include "nsIMsgMailSession.h" +#include "nsIMsgAccountManager.h" +#include "nsIMsgAccount.h" +#include "nsIImportSettings.h" +#include "nsOESettings.h" +#include "nsMsgBaseCID.h" + +#include "OEDebugLog.h" + +static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID); +static NS_DEFINE_CID(kComponentManagerCID, NS_COMPONENTMANAGER_CID); +static NS_DEFINE_CID(kMsgMailSessionCID, NS_MSGMAILSESSION_CID); + + +class OESettings { +public: + static HKEY Find50Key( void); + static HKEY Find40Key( void); + static HKEY FindAccountsKey( void); + + static PRBool DoImport( nsIMsgAccount **ppAccount); + + static PRBool DoIMAPServer( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount); + static PRBool DoPOP3Server( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount); + + static void SetIdentities( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, HKEY hKey); + static PRBool IdentityMatches( nsIMsgIdentity *pIdent, const char *pName, const char *pServer, const char *pEmail, const char *pReply, const char *pUserName); + +}; + + +//////////////////////////////////////////////////////////////////////// +nsresult nsOESettings::Create(nsIImportSettings** aImport) +{ + NS_PRECONDITION(aImport != nsnull, "null ptr"); + if (! aImport) + return NS_ERROR_NULL_POINTER; + + *aImport = new nsOESettings(); + if (! *aImport) + return NS_ERROR_OUT_OF_MEMORY; + + NS_ADDREF(*aImport); + return NS_OK; +} + +nsOESettings::nsOESettings() +{ + NS_INIT_REFCNT(); +} + +nsOESettings::~nsOESettings() +{ + NS_ASSERTION(mRefCnt == 0, "non-zero refcnt at destruction"); +} + +NS_IMPL_ISUPPORTS(nsOESettings, nsIImportSettings::GetIID()); + +NS_IMETHODIMP nsOESettings::AutoLocate(PRUnichar **description, nsIFileSpec **location, PRBool *_retval) +{ + NS_PRECONDITION(description != nsnull, "null ptr"); + NS_PRECONDITION(_retval != nsnull, "null ptr"); + if (!description || !_retval) + return( NS_ERROR_NULL_POINTER); + + nsString desc = "Outlook Express"; + *description = nsCRT::strdup( desc.GetUnicode()); + *_retval = PR_FALSE; + + if (location) + *location = nsnull; + HKEY key; + key = OESettings::Find50Key(); + if (key != nsnull) { + *_retval = PR_TRUE; + ::RegCloseKey( key); + } + else { + key = OESettings::Find40Key(); + if (key != nsnull) { + *_retval = PR_TRUE; + ::RegCloseKey( key); + } + } + if (*_retval) { + key = OESettings::FindAccountsKey(); + if (key == nsnull) { + *_retval = PR_FALSE; + } + else { + ::RegCloseKey( key); + } + } + + return( NS_OK); +} + +NS_IMETHODIMP nsOESettings::SetLocation(nsIFileSpec *location) +{ + return( NS_OK); +} + +NS_IMETHODIMP nsOESettings::Import(nsIMsgAccount **localMailAccount, PRBool *_retval) +{ + NS_PRECONDITION( _retval != nsnull, "null ptr"); + + if (OESettings::DoImport( localMailAccount)) { + *_retval = PR_TRUE; + IMPORT_LOG0( "Settings import appears successful\n"); + } + else { + *_retval = PR_FALSE; + IMPORT_LOG0( "Settings import returned FALSE\n"); + } + + return( NS_OK); +} + + +HKEY OESettings::FindAccountsKey( void) +{ + HKEY sKey; + if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Internet Account Manager\\Accounts", 0, KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS, &sKey) == ERROR_SUCCESS) { + return( sKey); + } + + return( nsnull); +} + +HKEY OESettings::Find50Key( void) +{ + PRBool success = PR_FALSE; + HKEY sKey; + + if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Identities", 0, KEY_QUERY_VALUE, &sKey) == ERROR_SUCCESS) { + BYTE * pBytes = nsOERegUtil::GetValueBytes( sKey, "Default User ID"); + ::RegCloseKey( sKey); + if (pBytes) { + nsCString key( "Identities\\"); + key += (const char *)pBytes; + nsOERegUtil::FreeValueBytes( pBytes); + key += "\\Software\\Microsoft\\Outlook Express\\5.0"; + if (::RegOpenKeyEx( HKEY_CURRENT_USER, key, 0, KEY_QUERY_VALUE, &sKey) == ERROR_SUCCESS) { + return( sKey); + } + } + } + + return( nsnull); +} + +HKEY OESettings::Find40Key( void) +{ + HKEY sKey; + if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Outlook Express", 0, KEY_QUERY_VALUE, &sKey) == ERROR_SUCCESS) { + return( sKey); + } + + return( nsnull); +} + + +PRBool OESettings::DoImport( nsIMsgAccount **ppAccount) +{ + HKEY hKey = FindAccountsKey(); + if (hKey == nsnull) { + IMPORT_LOG0( "*** Error finding Outlook Express registry account keys\n"); + return( PR_FALSE); + } + + nsresult rv; + + NS_WITH_SERVICE(nsIMsgMailSession, mailSession, kMsgMailSessionCID, &rv); + if (NS_FAILED(rv)) { + IMPORT_LOG0( "*** Failed to create a mail session!\n"); + return( PR_FALSE); + } + nsCOMPtr accMgr; + rv = mailSession->GetAccountManager( getter_AddRefs( accMgr)); + if (NS_FAILED( rv)) { + IMPORT_LOG0( "*** Failed to get account manager\n"); + return( PR_FALSE); + } + + + HKEY subKey; + nsCString defMailName; + + // First let's get the default mail account key name + if (::RegOpenKeyEx( HKEY_CURRENT_USER, "Software\\Microsoft\\Outlook Express", 0, KEY_QUERY_VALUE, &subKey) == ERROR_SUCCESS) { + BYTE * pBytes = nsOERegUtil::GetValueBytes( subKey, "Default Mail Account"); + ::RegCloseKey( subKey); + if (pBytes) { + defMailName = (const char *)pBytes; + nsOERegUtil::FreeValueBytes( pBytes); + } + } + + // Iterate the accounts looking for POP3 & IMAP accounts... + // Ignore LDAP & NNTP for now! + DWORD index = 0; + DWORD numChars; + TCHAR keyName[256]; + FILETIME modTime; + LONG result = ERROR_SUCCESS; + BYTE * pBytes; + int popCount = 0; + int accounts = 0; + nsCString keyComp; + + while (result == ERROR_SUCCESS) { + numChars = 256; + result = ::RegEnumKeyEx( hKey, index, keyName, &numChars, NULL, NULL, NULL, &modTime); + index++; + if (result == ERROR_SUCCESS) { + if (::RegOpenKeyEx( hKey, keyName, 0, KEY_QUERY_VALUE, &subKey) == ERROR_SUCCESS) { + // Get the values for this account. + IMPORT_LOG1( "Opened Outlook Express account: %s\n", (char *)keyName); + + nsIMsgAccount *anAccount = nsnull; + pBytes = nsOERegUtil::GetValueBytes( subKey, "IMAP Server"); + if (pBytes) { + if (DoIMAPServer( accMgr, subKey, (char *)pBytes, &anAccount)) + accounts++; + nsOERegUtil::FreeValueBytes( pBytes); + } + + pBytes = nsOERegUtil::GetValueBytes( subKey, "POP3 Server"); + if (pBytes) { + if (popCount == 0) { + if (DoPOP3Server( accMgr, subKey, (char *)pBytes, &anAccount)) { + popCount++; + accounts++; + if (ppAccount) { + *ppAccount = anAccount; + NS_ADDREF( anAccount); + } + } + } + else { + if (DoPOP3Server( accMgr, subKey, (char *)pBytes, &anAccount)) { + popCount++; + accounts++; + // If we created a mail account, get rid of it since + // we have 2 POP accounts! + if (ppAccount && *ppAccount) { + NS_RELEASE( *ppAccount); + *ppAccount = nsnull; + } + } + } + nsOERegUtil::FreeValueBytes( pBytes); + } + + if (anAccount) { + // Is this the default account? + keyComp = keyName; + if (keyComp.Equals( defMailName)) { + accMgr->SetDefaultAccount( anAccount); + } + NS_RELEASE( anAccount); + } + + ::RegCloseKey( subKey); + } + } + } + + return( accounts != 0); +} + + + +PRBool OESettings::DoIMAPServer( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount) +{ + if (ppAccount) + *ppAccount = nsnull; + + BYTE *pBytes; + pBytes = nsOERegUtil::GetValueBytes( hKey, "IMAP User Name"); + if (!pBytes) + return( PR_FALSE); + + PRBool result = PR_FALSE; + + // I now have a user name/server name pair, find out if it already exists? + nsCOMPtr in; + nsresult rv = pMgr->FindServer( (const char *)pBytes, pServerName, "imap", getter_AddRefs( in)); + if (NS_FAILED( rv) || (in == nsnull)) { + // Create the incoming server and an account for it? + rv = pMgr->CreateIncomingServer( "imap", getter_AddRefs( in)); + if (NS_SUCCEEDED( rv) && in) { + rv = in->SetHostName( pServerName); + rv = in->SetUsername( (char *)pBytes); + BYTE *pAccName = nsOERegUtil::GetValueBytes( hKey, "Account Name"); + nsString prettyName; + if (pAccName) { + prettyName = (const char *)pAccName; + nsOERegUtil::FreeValueBytes( pAccName); + } + else + prettyName = (const char *)pServerName; + + PRUnichar *pretty = prettyName.ToNewUnicode(); + rv = in->SetPrettyName( pretty); + nsCRT::free( pretty); + + // We have a server, create an account. + nsCOMPtr account; + rv = pMgr->CreateAccount( getter_AddRefs( account)); + if (NS_SUCCEEDED( rv) && account) { + rv = account->SetIncomingServer( in); + // Fiddle with the identities + SetIdentities( pMgr, account, hKey); + result = PR_TRUE; + if (ppAccount) + account->QueryInterface( nsIMsgAccount::GetIID(), (void **)ppAccount); + } + } + } + else + result = PR_TRUE; + + nsOERegUtil::FreeValueBytes( pBytes); + + return( result); +} + +PRBool OESettings::DoPOP3Server( nsIMsgAccountManager *pMgr, HKEY hKey, char *pServerName, nsIMsgAccount **ppAccount) +{ + if (ppAccount) + *ppAccount = nsnull; + + BYTE *pBytes; + pBytes = nsOERegUtil::GetValueBytes( hKey, "POP3 User Name"); + if (!pBytes) + return( PR_FALSE); + + PRBool result = PR_FALSE; + + // I now have a user name/server name pair, find out if it already exists? + nsCOMPtr in; + nsresult rv = pMgr->FindServer( (const char *)pBytes, pServerName, "pop3", getter_AddRefs( in)); + if (NS_FAILED( rv) || (in == nsnull)) { + // Create the incoming server and an account for it? + rv = pMgr->CreateIncomingServer( "pop3", getter_AddRefs( in)); + if (NS_SUCCEEDED( rv) && in) { + rv = in->SetHostName( pServerName); + rv = in->SetUsername( (char *)pBytes); + BYTE *pAccName = nsOERegUtil::GetValueBytes( hKey, "Account Name"); + nsString prettyName; + if (pAccName) { + prettyName = (const char *)pAccName; + nsOERegUtil::FreeValueBytes( pAccName); + } + else + prettyName = (const char *)pServerName; + + PRUnichar *pretty = prettyName.ToNewUnicode(); + rv = in->SetPrettyName( pretty); + nsCRT::free( pretty); + + // We have a server, create an account. + nsCOMPtr account; + rv = pMgr->CreateAccount( getter_AddRefs( account)); + if (NS_SUCCEEDED( rv) && account) { + rv = account->SetIncomingServer( in); + // Fiddle with the identities + SetIdentities( pMgr, account, hKey); + result = PR_TRUE; + if (ppAccount) + account->QueryInterface( nsIMsgAccount::GetIID(), (void **)ppAccount); + } + } + } + else + result = PR_TRUE; + + nsOERegUtil::FreeValueBytes( pBytes); + + return( result); +} + + +PRBool OESettings::IdentityMatches( nsIMsgIdentity *pIdent, const char *pName, const char *pServer, const char *pEmail, const char *pReply, const char *pUserName) +{ + if (!pIdent) + return( PR_FALSE); + + char * pIName = nsnull; + char * pIEmail = nsnull; + char * pIServer = nsnull; + char * pIReply = nsnull; + + PRBool result = PR_TRUE; + + // The test here is: + // If the smtp host is the same + // and the email address is the same (if it is supplied) + // and the reply to address is the same (if it is supplied) + // then we match regardless of the full name. + + nsresult rv = pIdent->GetFullName( &pIName); + rv = pIdent->GetEmail( &pIEmail); + rv = pIdent->GetSmtpHostname( &pIServer); + rv = pIdent->GetReplyTo( &pIReply); + + // for now, if it's the same server and reply to and email then it matches + if (pServer && pIServer && !nsCRT::strcasecmp( pServer, pIServer)) { + if (pReply) { + if (!pIReply || nsCRT::strcasecmp( pReply, pIReply)) + result = PR_FALSE; + } + if (pEmail) { + if (!pIEmail || nsCRT::strcasecmp( pEmail, pIEmail)) + result = PR_FALSE; + } + } + else + result = PR_FALSE; + + nsCRT::free( pIName); + nsCRT::free( pIEmail); + nsCRT::free( pIServer); + nsCRT::free( pIReply); + + return( result); +} + +void OESettings::SetIdentities( nsIMsgAccountManager *pMgr, nsIMsgAccount *pAcc, HKEY hKey) +{ + // Get the relevant information for an identity + char *pName = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Display Name"); + char *pServer = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Server"); + char *pEmail = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Email Address"); + char *pReply = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP Reply To Email Address"); + char *pUserName = (char *)nsOERegUtil::GetValueBytes( hKey, "SMTP User Name"); + + nsresult rv; + + // Go ahead and add the default identity to the server if there is one? + nsCOMPtr acc; + rv = pMgr->GetDefaultAccount( getter_AddRefs( acc)); + nsCOMPtr defIdent; + if (NS_SUCCEEDED( rv) && acc) { + rv = acc->GetDefaultIdentity( getter_AddRefs( defIdent)); + if (NS_SUCCEEDED( rv) && defIdent) { + rv = pAcc->AddIdentity( defIdent); + rv = pAcc->SetDefaultIdentity( defIdent); + } + } + + if (pEmail && pName && pServer) { + if (!IdentityMatches( defIdent, pName, pServer, pEmail, pReply, pUserName)) { + // Create a new identity + // First, find an existing identity + nsCOMPtr idents; + rv = pMgr->GetAllIdentities( getter_AddRefs( idents)); + PRUint32 cnt = 0; + if (idents) + idents->Count( &cnt); + nsISupports * pSupports; + PRBool match = PR_FALSE; + for (PRUint32 i = 0; (i < cnt) && !match; i++) { + pSupports = idents->ElementAt( i); + if (pSupports) { + nsCOMPtr ident( do_QueryInterface( pSupports)); + NS_RELEASE( pSupports); + if (ident) { + if (IdentityMatches( ident, pName, pServer, pEmail, pReply, pUserName)) { + match = PR_TRUE; + rv = pAcc->AddIdentity( ident); + rv = pAcc->SetDefaultIdentity( ident); + } + } + } + } + if (!match) { + // The default identity, nor any other identities matched, + // create a new one and add it to the account. + nsCOMPtr id; + rv = pMgr->CreateIdentity( getter_AddRefs( id)); + if (id) { + id->SetFullName( pName); + id->SetIdentityName( pName); + id->SetEmail( pEmail); + if (pReply) + id->SetReplyTo( pReply); + id->SetSmtpHostname( pServer); + if (pUserName) + id->SetSmtpUsername( pUserName); + pAcc->AddIdentity( id); + pAcc->SetDefaultIdentity( id); + } + } + } + } + + + nsOERegUtil::FreeValueBytes( (BYTE *)pName); + nsOERegUtil::FreeValueBytes( (BYTE *)pServer); + nsOERegUtil::FreeValueBytes( (BYTE *)pEmail); + nsOERegUtil::FreeValueBytes( (BYTE *)pReply); + nsOERegUtil::FreeValueBytes( (BYTE *)pUserName); +} + + diff --git a/mailnews/import/oexpress/nsOESettings.h b/mailnews/import/oexpress/nsOESettings.h new file mode 100644 index 00000000000..87cd7823707 --- /dev/null +++ b/mailnews/import/oexpress/nsOESettings.h @@ -0,0 +1,42 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#ifndef nsOESettings_h___ +#define nsOESettings_h___ + +#include "nsIImportSettings.h" + + +class nsOESettings : public nsIImportSettings { +public: + nsOESettings(); + virtual ~nsOESettings(); + + static nsresult Create(nsIImportSettings** aImport); + + // nsISupports interface + NS_DECL_ISUPPORTS + + // nsIImportSettings interface + NS_DECL_NSIIMPORTSETTINGS + +private: + +}; + +#endif /* nsOESettings_h___ */ diff --git a/mailnews/import/public/makefile.in b/mailnews/import/public/makefile.in index f77bfd04ad4..e69de29bb2d 100644 --- a/mailnews/import/public/makefile.in +++ b/mailnews/import/public/makefile.in @@ -1,42 +0,0 @@ -#!gmake -# -# The contents of this file are subject to the Netscape Public License -# Version 1.0 (the "NPL"); you may not use this file except in -# compliance with the NPL. You may obtain a copy of the NPL at -# http://www.mozilla.org/NPL/ -# -# Software distributed under the NPL is distributed on an "AS IS" basis, -# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL -# for the specific language governing rights and limitations under the -# NPL. -# -# The Initial Developer of this code under the NPL is Netscape -# Communications Corporation. Portions created by Netscape are -# Copyright (C) 1998 Netscape Communications Corporation. All Rights -# Reserved. - -DEPTH = ../../.. -topsrcdir = @top_srcdir@ -VPATH = @srcdir@ -srcdir = @srcdir@ -MODULE = import - -include $(DEPTH)/config/autoconf.mk - -XPIDLSRCS = \ - nsIImportService.idl \ - nsIImportModule.idl \ - nsIImportMail.idl \ - nsIImportMailboxDescriptor.idl \ - nsIImportGeneric.idl \ - nsIImportAddressBooks.idl \ - nsIImportABDescriptor.idl \ - $(NULL) - -EXPORTS = \ - $(NULL) - -include $(topsrcdir)/config/config.mk - -include $(topsrcdir)/config/rules.mk - diff --git a/mailnews/import/public/makefile.win b/mailnews/import/public/makefile.win index 7d6877d0055..469c494eacb 100644 --- a/mailnews/import/public/makefile.win +++ b/mailnews/import/public/makefile.win @@ -25,6 +25,7 @@ XPIDLSRCS= .\nsIImportService.idl \ .\nsIImportGeneric.idl \ .\nsIImportAddressBooks.idl \ .\nsIImportABDescriptor.idl \ + .\nsIImportSettings.idl \ $(NULL) MODULE= import diff --git a/mailnews/import/public/nsIImportSettings.idl b/mailnews/import/public/nsIImportSettings.idl new file mode 100644 index 00000000000..e2c562e6ea4 --- /dev/null +++ b/mailnews/import/public/nsIImportSettings.idl @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +/* + Interface for importing settings. Settings can be auto-located or + specified by a specific file. Depends upon the app that the settings + are coming from. + +*/ + +#include "nsISupports.idl" + +interface nsIMsgAccount; +interface nsIFileSpec; + +[scriptable, uuid(a9411860-66ac-11d3-a206-00a0cc26da63)] +interface nsIImportSettings : nsISupports +{ + boolean AutoLocate( out wstring description, out nsIFileSpec location); + + void SetLocation( in nsIFileSpec location); + + /* + Create all of the accounts, identities, and servers. Return an + account where any local mail from this app should be imported. + The returned account can be null which indicates that no suitable + account for local mail was created and a new account specifically for + the imported mail should be created. + */ + boolean Import( out nsIMsgAccount localMailAccount); +}; + + + +%{ C++ + +%} diff --git a/mailnews/import/resources/content/import-test.html b/mailnews/import/resources/content/import-test.html index 59ef1357662..8b1eaac0d48 100644 --- a/mailnews/import/resources/content/import-test.html +++ b/mailnews/import/resources/content/import-test.html @@ -100,6 +100,26 @@ function test2() { function test3() { dump( "test3\n"); + dump( "Loading outlook express component\n"); + var module = Components.classes["component://netscape/import/import-oe"].createInstance(); + module = module.QueryInterface(Components.interfaces.nsIImportModule); + + dump( "Loaded outlook express import module\n"); + dump( "Getting the settings interface\n"); + + var setInterface = module.GetImportInterface( "settings"); + setInterface = setInterface.QueryInterface( Components.interfaces.nsIImportSettings); + + dump( "Got the settings interface\n"); + + var location = new Object(); + if (setInterface.Import( location)) { + dump( "Settings import returned success\n"); + dump( "Location: " + location + "\n"); + } + else { + dump( "Settings import returned failure\n"); + } } @@ -108,7 +128,7 @@ function test3() {


- + diff --git a/mailnews/import/src/nsImportMail.cpp b/mailnews/import/src/nsImportMail.cpp index 0e1e0435e19..a26a80b6728 100644 --- a/mailnews/import/src/nsImportMail.cpp +++ b/mailnews/import/src/nsImportMail.cpp @@ -113,15 +113,17 @@ public: NS_IMETHOD CancelImport(void); private: - PRBool GetAccount( nsIFileSpec **ppSpec); + PRBool GetAccount( nsIMsgFolder **ppFolder); PRBool FindAccount( nsIMsgFolder **ppFolder); void GetDefaultMailboxes( void); void GetDefaultLocation( void); void GetDefaultDestination( void); private: + PRUnichar * m_pName; // module name that created this interface nsIMsgFolder * m_pDestFolder; PRBool m_deleteDestFolder; + PRBool m_createdAccount; nsIFileSpec * m_pSrcLocation; PRBool m_gotLocation; PRBool m_found; @@ -145,6 +147,7 @@ public: PRUint32 currentSize; nsIMsgFolder * destRoot; PRBool ownsDestRoot; + PRBool ownsAccount; nsISupportsArray * boxes; nsIImportMail * mailImport; nsIOutputStream * successLog; @@ -193,6 +196,8 @@ nsImportGenericMail::nsImportGenericMail() m_pDestFolder = nsnull; m_deleteDestFolder = PR_FALSE; + m_createdAccount = PR_FALSE; + m_pName = nsnull; } @@ -203,6 +208,9 @@ nsImportGenericMail::~nsImportGenericMail() m_pThreadData = nsnull; } + if (m_pName) + nsCRT::free( m_pName); + NS_IF_RELEASE( m_pDestFolder); NS_IF_RELEASE( m_pSrcLocation); NS_IF_RELEASE( m_pInterface); @@ -307,6 +315,17 @@ NS_IMETHODIMP nsImportGenericMail::SetData( const char *dataId, nsISupports *ite m_deleteDestFolder = PR_FALSE; } + if (!nsCRT::strcasecmp( dataId, "name")) { + // BIG CHEAT, is this OK to do? + PRUnichar *pName = (PRUnichar *)item; + if (m_pName) + nsCRT::free( m_pName); + if (pName) + m_pName = nsCRT::strdup( pName); + else + m_pName = nsnull; + } + return( NS_OK); } @@ -364,8 +383,19 @@ void nsImportGenericMail::GetDefaultDestination( void) { if (m_pDestFolder) return; + if (!m_pInterface) + return; + nsIMsgFolder * rootFolder; m_deleteDestFolder = PR_FALSE; + m_createdAccount = PR_FALSE; + if (GetAccount( &rootFolder)) { + m_pDestFolder = rootFolder; + m_deleteDestFolder = PR_TRUE; + m_createdAccount = PR_TRUE; + return; + } + if (FindAccount( &rootFolder)) { // create the sub folder for our import. char *pName = nsnull; @@ -582,6 +612,7 @@ ImportThreadData::ImportThreadData() currentSize = 0; destRoot = nsnull; ownsDestRoot = PR_FALSE; + ownsAccount = PR_FALSE; boxes = nsnull; mailImport = nsnull; successLog = nsnull; @@ -716,11 +747,12 @@ ImportMailThread( void *stuff) rv = curFolder->CreateSubfolder( pStr); rv = curFolder->GetChildNamed( pStr, getter_AddRefs( subFolder)); newFolder = do_QueryInterface( subFolder); - newFolder->GetPath( getter_AddRefs( outBox)); + if (newFolder) + newFolder->GetPath( getter_AddRefs( outBox)); nsCRT::free( pStr); - if (size && import) { + if (size && import && newFolder && outBox) { PRBool fatalError = PR_FALSE; pData->currentSize = size; rv = pData->mailImport->ImportMailbox( box, outBox, pData->errorLog, pData->successLog, &fatalError); @@ -748,11 +780,11 @@ ImportMailThread( void *stuff) } -PRBool nsImportGenericMail::GetAccount( nsIFileSpec **ppSpec) +PRBool nsImportGenericMail::GetAccount( nsIMsgFolder **ppFolder) { nsresult rv; - *ppSpec = nsnull; + *ppFolder = nsnull; NS_WITH_SERVICE(nsIMsgMailSession, mailSession, kMsgMailSessionCID, &rv); if (NS_FAILED(rv)) { @@ -778,97 +810,63 @@ PRBool nsImportGenericMail::GetAccount( nsIFileSpec **ppSpec) return( PR_FALSE); } - // Create a new account for the import? + // Create a new account for the import nsCOMPtr server; - rv = nsComponentManager::CreateInstance("component://netscape/messenger/server&type=pop3", - nsnull, - nsCOMTypeInfo::GetIID(), - getter_AddRefs(server)); + rv = accMgr->CreateIncomingServer( "none", getter_AddRefs( server)); - server->SetKey( "importedPOP1"); - server->SetType( "pop3"); + nsString prettyName; + if (m_pName) + prettyName = m_pName; + else + prettyName = "Imported Mail"; - nsString prettyName = "Imported Mail"; + char *pName = prettyName.ToNewCString(); server->SetPrettyName( (PRUnichar *) prettyName.GetUnicode()); - server->SetHostName( "imported.mail.noserver"); - server->SetUsername( "ImportTest"); + server->SetHostName( "imported.mail"); + server->SetUsername( "nobody"); - nsFileSpec profileDir; - - NS_WITH_SERVICE(nsIProfile, profile, kProfileCID, &rv); - if (NS_FAILED(rv)) { - IMPORT_LOG0( "*** Unable to get profile service.\n"); - return( PR_FALSE); - } - - rv = profile->GetCurrentProfileDir(&profileDir); - if (NS_FAILED(rv)) { - IMPORT_LOG0( "*** Unable to get profile directory\n"); - return( PR_FALSE); - } - - nsCOMPtr mailDir; - nsFileSpec dir(profileDir); - PRBool dirExists; - - // turn profileDir into the mail dir. - dir += "Mail"; - if (!dir.Exists()) { - dir.CreateDir(); - } - dir += "imported.mail"; - - rv = NS_NewFileSpecWithSpec(dir, getter_AddRefs( mailDir)); - if (NS_FAILED(rv)) { - IMPORT_LOG0( "*** Unable to create mail dir file spec\n"); - return( PR_FALSE); - } - - rv = mailDir->Exists(&dirExists); - if (NS_FAILED(rv)) { - IMPORT_LOG0( "*** Failed to see if mail dir exists\n"); - return( PR_FALSE); - } - - if (!dirExists) { - mailDir->CreateDir(); - } - - char *str = nsnull; - mailDir->GetNativePath( &str); - - if (str && *str) { - server->SetLocalPath( str); - IMPORT_LOG1( "New account mail dir: %s\n", str); - nsCRT::free( str); - } + nsCRT::free( pName); // create a new account with the server and identity. nsCOMPtr account; - rv = accMgr->GetAccount( "ImportedMailAccount", getter_AddRefs( account)); - if (NS_FAILED( rv)) { - rv = accMgr->CreateAccount( getter_AddRefs( account)); - if (NS_FAILED( rv)) { - IMPORT_LOG0( "*** Error creating new account\n"); - return( PR_FALSE); - } - - rv = account->SetKey( "ImportedMailAccount"); - rv = account->SetIncomingServer( server); - } - - - rv = mailDir->QueryInterface( nsIFileSpec::GetIID(), (void **) ppSpec); - + rv = accMgr->CreateAccount( getter_AddRefs( account)); if (NS_FAILED( rv)) { - IMPORT_LOG0( "*** Unable to get nsIFileSpec interface from nsIFileSpec!\n"); + IMPORT_LOG0( "*** Error creating new account\n"); return( PR_FALSE); } + + rv = account->SetIncomingServer( server); + if (NS_FAILED( rv)) { + IMPORT_LOG0( "*** Error setting incoming server on account\n"); + } + if (identity) { + rv = account->AddIdentity( identity); + if (NS_FAILED( rv)) { + IMPORT_LOG0( "*** Error adding identity to account\n"); + } + rv = account->SetDefaultIdentity( identity); + if (NS_FAILED( rv)) { + IMPORT_LOG0( "*** Error setting default identity for account\n"); + } + } - return( PR_TRUE); + + nsCOMPtr rootFolder; + rv = server->GetRootFolder( getter_AddRefs( rootFolder)); + if (NS_SUCCEEDED( rv) && (rootFolder != nsnull)) { + rv = rootFolder->QueryInterface( nsIMsgFolder::GetIID(), (void **)ppFolder); + if (NS_SUCCEEDED( rv)) { + IMPORT_LOG0( "****** CREATED NEW ACCOUNT FOR IMPORT\n"); + return( PR_TRUE); + } + } + + IMPORT_LOG0( "****** FAILED TO CREATE NEW ACCOUNT FOR IMPORT\n"); + + return( PR_FALSE); } PRBool nsImportGenericMail::FindAccount( nsIMsgFolder **ppFolder)