When initializing the compose app core, use the app cores service manager to register our app core. When deleting the app core, remove ourselves from the appcores manager. More memory leak fixes from Bruce Mitchener <bruce@cybersight.com>.

This commit is contained in:
mscott%netscape.com 1999-03-30 01:12:58 +00:00
Родитель fcf9ca4c61
Коммит 7486dfc494
1 изменённых файлов: 131 добавлений и 72 удалений

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

@ -19,7 +19,9 @@
#include "nsIDOMComposeAppCore.h" #include "nsIDOMComposeAppCore.h"
#include "nsComposeAppCore.h" #include "nsComposeAppCore.h"
#include "nsIScriptObjectOwner.h" #include "nsIScriptObjectOwner.h"
#include "nsAppCoresCIDs.h"
#include "nsIDOMBaseAppCore.h" #include "nsIDOMBaseAppCore.h"
#include "nsIDOMAppCoresManager.h"
#include "nsJSComposeAppCore.h" #include "nsJSComposeAppCore.h"
#include "nsIDOMDocument.h" #include "nsIDOMDocument.h"
#include "nsIDOMNodeList.h" #include "nsIDOMNodeList.h"
@ -59,6 +61,8 @@
#include "nsFileSpec.h" #include "nsFileSpec.h"
#include "nsFileStream.h" #include "nsFileStream.h"
static NS_DEFINE_IID(kIDOMAppCoresManagerIID, NS_IDOMAPPCORESMANAGER_IID);
static NS_DEFINE_IID(kAppCoresManagerCID, NS_APPCORESMANAGER_CID);
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID); static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
static NS_DEFINE_IID(kIDocumentIID, nsIDocument::GetIID()); static NS_DEFINE_IID(kIDocumentIID, nsIDocument::GetIID());
@ -74,12 +78,6 @@ static NS_DEFINE_CID(kMsgSendCID, NS_MSGSEND_CID);
static NS_DEFINE_CID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID); static NS_DEFINE_CID(kAppShellServiceCID, NS_APPSHELL_SERVICE_CID);
static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID); static NS_DEFINE_IID(kIRDFResourceIID, NS_IRDFRESOURCE_IID);
NS_BEGIN_EXTERN_C
//nsresult NS_MailNewsLoadUrl(const nsString& urlString, nsISupports * aConsumer);
NS_END_EXTERN_C
// defined in msgCompGlue.cpp // defined in msgCompGlue.cpp
extern char * INTL_GetDefaultMailCharset(void); extern char * INTL_GetDefaultMailCharset(void);
extern nsresult ConvertFromUnicode(const nsString aCharset, extern nsresult ConvertFromUnicode(const nsString aCharset,
@ -158,6 +156,7 @@ protected:
// //
nsComposeAppCore::nsComposeAppCore() nsComposeAppCore::nsComposeAppCore()
{ {
NS_INIT_REFCNT();
mScriptObject = nsnull; mScriptObject = nsnull;
mWebShell = nsnull; mWebShell = nsnull;
mScriptContext = nsnull; mScriptContext = nsnull;
@ -165,18 +164,29 @@ nsComposeAppCore::nsComposeAppCore()
mEditor = nsnull; mEditor = nsnull;
mMsgCompFields = nsnull; mMsgCompFields = nsnull;
mMsgSend = nsnull; mMsgSend = nsnull;
NS_INIT_REFCNT();
} }
nsComposeAppCore::~nsComposeAppCore() nsComposeAppCore::~nsComposeAppCore()
{ {
NS_IF_RELEASE(mWebShell); // remove ourselves from the app cores manager...
NS_IF_RELEASE(mScriptContext); // if we were able to inherit directly from nsBaseAppCore then it would do this for
NS_IF_RELEASE(mWindow); // us automatically
NS_IF_RELEASE(mEditor);
NS_IF_RELEASE(mMsgSend); nsIDOMAppCoresManager * appCoreManager;
NS_IF_RELEASE(mMsgCompFields); nsresult rv = nsServiceManager::GetService(kAppCoresManagerCID, kIDOMAppCoresManagerIID,
(nsISupports**)&appCoreManager);
if (NS_SUCCEEDED(rv) && appCoreManager)
{
appCoreManager->Remove((nsIDOMBaseAppCore *) this);
nsServiceManager::ReleaseService(kAppCoresManagerCID, appCoreManager);
}
NS_IF_RELEASE(mWebShell);
NS_IF_RELEASE(mScriptContext);
NS_IF_RELEASE(mWindow);
NS_IF_RELEASE(mEditor);
NS_IF_RELEASE(mMsgSend);
NS_IF_RELEASE(mMsgCompFields);
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -310,7 +320,12 @@ nsComposeAppCore::HackToGetBody(PRInt32 what)
msgBody += "> "; msgBody += "> ";
msgBody += buffer; msgBody += buffer;
} }
mMsgCompFields->SetBody(msgBody.ToNewCString(), NULL);
// mMsgCompFields->SetBody(msgBody.ToNewCString(), NULL);
// SetBody() strdup()'s cmsgBody.
char* cmsgBody = msgBody.ToNewCString();
mMsgCompFields->SetBody(cmsgBody, NULL);
delete[] cmsgBody;
PR_Free(buffer); PR_Free(buffer);
} }
} }
@ -446,9 +461,21 @@ nsComposeAppCore::SetScriptObject(void* aScriptObject)
nsresult nsresult
nsComposeAppCore::Init(const nsString& aId) nsComposeAppCore::Init(const nsString& aId)
{ {
printf("Init\n");
mId = aId;
nsresult res; nsresult res;
mId = aId;
// add ourselves to the app cores manager...
// if we were able to inherit directly from nsBaseAppCore then it would do this for
// us automatically
nsIDOMAppCoresManager * appCoreManager;
nsresult rv = nsServiceManager::GetService(kAppCoresManagerCID, kIDOMAppCoresManagerIID,
(nsISupports**)&appCoreManager);
if (NS_SUCCEEDED(rv) && appCoreManager)
{
appCoreManager->Add((nsIDOMBaseAppCore *) this);
nsServiceManager::ReleaseService(kAppCoresManagerCID, appCoreManager);
}
if (!mMsgSend) if (!mMsgSend)
{ {
@ -476,7 +503,6 @@ nsComposeAppCore::Init(const nsString& aId)
nsresult nsresult
nsComposeAppCore::GetId(nsString& aId) nsComposeAppCore::GetId(nsString& aId)
{ {
printf("GetID\n");
aId = mId; aId = mId;
return NS_OK; return NS_OK;
} }
@ -593,9 +619,15 @@ nsComposeAppCore::NewMessage(nsAutoString& aUrl,
{ {
bString += "Re: "; bString += "Re: ";
bString += aString; bString += aString;
mMsgCompFields->SetSubject(bString.ToNewCString(), NULL); char* cSubject = bString.ToNewCString();
message->GetAuthor(aString); mMsgCompFields->SetSubject(cSubject, NULL);
mMsgCompFields->SetTo(aString.ToNewCString(), NULL); delete[] cSubject;
message->GetAuthor(aString);
char * cTo = aString.ToNewCString();
mMsgCompFields->SetTo(cTo, NULL);
delete[] cTo;
if (messageType == 1) if (messageType == 1)
{ {
nsString cString, dString; nsString cString, dString;
@ -604,7 +636,9 @@ nsComposeAppCore::NewMessage(nsAutoString& aUrl,
if (cString.Length() > 0 && dString.Length() > 0) if (cString.Length() > 0 && dString.Length() > 0)
cString = cString + ", "; cString = cString + ", ";
cString = cString + dString; cString = cString + dString;
mMsgCompFields->SetCc(cString.ToNewCString(), NULL); char* cCc = cString.ToNewCString();
mMsgCompFields->SetCc(cCc, NULL);
delete[] cCc;
} }
HackToGetBody(1); HackToGetBody(1);
break; break;
@ -616,7 +650,11 @@ nsComposeAppCore::NewMessage(nsAutoString& aUrl,
bString += "[Fwd: "; bString += "[Fwd: ";
bString += aString; bString += aString;
bString += "]"; bString += "]";
mMsgCompFields->SetSubject(bString.ToNewCString(), NULL);
char* cSubject = bString.ToNewCString();
mMsgCompFields->SetSubject(cSubject, NULL);
delete[] cSubject;
/* We need to get more information out from the message. */ /* We need to get more information out from the message. */
nsCOMPtr<nsIRDFResource> rdfResource; nsCOMPtr<nsIRDFResource> rdfResource;
rv = object->QueryInterface(kIRDFResourceIID, rv = object->QueryInterface(kIRDFResourceIID,
@ -670,65 +708,86 @@ NS_IMETHODIMP nsComposeAppCore::SendMessage(nsAutoString& aAddrTo,
#endif //DEBUG #endif //DEBUG
// nsIMsgCompose *pMsgCompose; // nsIMsgCompose *pMsgCompose;
if (mMsgCompFields) { if (mMsgCompFields)
// Get the default charset from pref, use this as a mail charset for now. {
// TODO: For reply/forward, original charset need to be used instead. // Get the default charset from pref, use this as a mail charset for now.
// TODO: Also need to update charset for the charset menu. // TODO: For reply/forward, original charset need to be used instead.
mMsgCompFields->SetCharacterSet(INTL_GetDefaultMailCharset(), NULL); // TODO: Also need to update charset for the charset menu.
mMsgCompFields->SetCharacterSet(INTL_GetDefaultMailCharset(), NULL);
nsString aString; nsString aString;
nsString aCharset(msgCompHeaderInternalCharset()); nsString aCharset(msgCompHeaderInternalCharset());
char *outCString; char *outCString;
// Pref values are supposed to be stored as UTF-8, so no conversion // Pref values are supposed to be stored as UTF-8, so no conversion
mMsgCompFields->SetFrom((char *)pCompPrefs.GetUserEmail(), NULL); mMsgCompFields->SetFrom((char *)pCompPrefs.GetUserEmail(), NULL);
mMsgCompFields->SetReplyTo((char *)pCompPrefs.GetReplyTo(), NULL); mMsgCompFields->SetReplyTo((char *)pCompPrefs.GetReplyTo(), NULL);
mMsgCompFields->SetOrganization((char *)pCompPrefs.GetOrganization(), NULL); mMsgCompFields->SetOrganization((char *)pCompPrefs.GetOrganization(), NULL);
// Convert fields to UTF-8 // Convert fields to UTF-8
if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aAddrTo, &outCString))) { if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aAddrTo, &outCString)))
mMsgCompFields->SetTo(outCString, NULL); {
PR_Free(outCString); mMsgCompFields->SetTo(outCString, NULL);
} PR_Free(outCString);
else { }
mMsgCompFields->SetTo(aAddrTo.ToNewCString(), NULL); else
} {
char* cAddrTo = aAddrTo.ToNewCString();
mMsgCompFields->SetTo(cAddrTo, NULL);
delete[] cAddrTo;
}
if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aAddrCc, &outCString))) { if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aAddrCc, &outCString)))
mMsgCompFields->SetCc(outCString, NULL); {
PR_Free(outCString); mMsgCompFields->SetCc(outCString, NULL);
} PR_Free(outCString);
else { }
mMsgCompFields->SetCc(aAddrCc.ToNewCString(), NULL); else
} {
char* cAddrCc = aAddrCc.ToNewCString();
mMsgCompFields->SetCc(cAddrCc, NULL);
delete[] cAddrCc;
}
if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aAddrBcc, &outCString))) { if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aAddrBcc, &outCString)))
mMsgCompFields->SetBcc(outCString, NULL); {
PR_Free(outCString); mMsgCompFields->SetBcc(outCString, NULL);
} PR_Free(outCString);
else { }
mMsgCompFields->SetBcc(aAddrBcc.ToNewCString(), NULL); else
} {
char* cAddrBcc = aAddrBcc.ToNewCString();
mMsgCompFields->SetBcc(cAddrBcc, NULL);
delete[] cAddrBcc;
}
if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aSubject, &outCString))) { if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aSubject, &outCString)))
mMsgCompFields->SetSubject(outCString, NULL); {
PR_Free(outCString); mMsgCompFields->SetSubject(outCString, NULL);
} PR_Free(outCString);
else { }
mMsgCompFields->SetSubject(aSubject.ToNewCString(), NULL); else
} {
char* cSubject = aSubject.ToNewCString();
mMsgCompFields->SetSubject(cSubject, NULL);
delete[] cSubject;
}
// Convert body to mail charset not to utf-8 (because we don't manipulate body text) // Convert body to mail charset not to utf-8 (because we don't manipulate body text)
char *mail_charset; char *mail_charset;
mMsgCompFields->GetCharacterSet(&mail_charset); mMsgCompFields->GetCharacterSet(&mail_charset);
aCharset.SetString(mail_charset); aCharset.SetString(mail_charset);
if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aMsg, &outCString))) { if (NS_SUCCEEDED(ConvertFromUnicode(aCharset, aMsg, &outCString)))
mMsgCompFields->SetBody(outCString, NULL); {
PR_Free(outCString); mMsgCompFields->SetBody(outCString, NULL);
} PR_Free(outCString);
else { }
mMsgCompFields->SetBody(aMsg.ToNewCString(), NULL); else
} {
char* cMsg = aMsg.ToNewCString();
mMsgCompFields->SetBody(cMsg, NULL);
delete[] cMsg;
}
if (mMsgSend) if (mMsgSend)
mMsgSend->SendMessage(mMsgCompFields, NULL); mMsgSend->SendMessage(mMsgCompFields, NULL);