fix memory leaks in GetString functions; written by sfraser; built by jst and brade

This commit is contained in:
brade%netscape.com 2000-03-20 15:44:32 +00:00
Родитель 9a1181dc2e
Коммит cd0bcbca28
4 изменённых файлов: 178 добавлений и 116 удалений

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

@ -1258,7 +1258,6 @@ nsEditorShell::CheckAndSaveDocument(const PRUnichar *reasonToSave, PRBool *_retv
{ {
*_retval = PR_FALSE; *_retval = PR_FALSE;
nsAutoString ReasonToSave(reasonToSave);
nsCOMPtr<nsIDOMDocument> theDoc; nsCOMPtr<nsIDOMDocument> theDoc;
nsresult rv = GetEditorDocument(getter_AddRefs(theDoc)); nsresult rv = GetEditorDocument(getter_AddRefs(theDoc));
if (NS_SUCCEEDED(rv) && theDoc) if (NS_SUCCEEDED(rv) && theDoc)
@ -1275,18 +1274,22 @@ nsEditorShell::CheckAndSaveDocument(const PRUnichar *reasonToSave, PRBool *_retv
if (modCount > 0) if (modCount > 0)
{ {
// Ask user if they want to save current changes // Ask user if they want to save current changes
nsAutoString tmp1 = GetString("Save"); nsAutoString reasonToSaveStr(reasonToSave);
nsAutoString tmp2 = GetString("DontSave"); nsAutoString tmp1, tmp2, title;
nsAutoString title; GetBundleString("Save", tmp1);
GetBundleString("DontSave", tmp2);
GetDocumentTitleString(title); GetDocumentTitleString(title);
// If title is empty, use "untitled" // If title is empty, use "untitled"
if (title.Length() == 0) if (title.Length() == 0)
title = GetString("untitled"); GetBundleString("untitled", title);
nsAutoString saveMsg = ((GetString("SaveFilePrompt")).ReplaceSubstring("%title%",title)).ReplaceSubstring("%reason%",ReasonToSave); nsAutoString saveMsg;
GetBundleString("SaveFilePrompt", saveMsg);
saveMsg.ReplaceSubstring("%title%", title).ReplaceSubstring("%reason%", reasonToSaveStr);
EConfirmResult result = ConfirmWithCancel(GetString("SaveDocument"), saveMsg, nsAutoString saveDocString;
&tmp1, &tmp2); GetBundleString("SaveDocument", saveDocString);
EConfirmResult result = ConfirmWithCancel(saveDocString, saveMsg, &tmp1, &tmp2);
if (result == eCancel) if (result == eCancel)
{ {
*_retval = PR_FALSE; *_retval = PR_FALSE;
@ -1351,10 +1354,13 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
if (NS_SUCCEEDED(res)) if (NS_SUCCEEDED(res))
{ {
PRUnichar *titleUnicode; PRUnichar *titleUnicode;
nsAutoString caption = GetString("DocumentTitle"); nsAutoString captionStr, msgStr;
nsAutoString msg = GetString("NeedDocTitle");
GetBundleString("DocumentTitle", captionStr);
GetBundleString("NeedDocTitle", msgStr);
PRBool retVal = PR_FALSE; PRBool retVal = PR_FALSE;
res = dialog->Prompt(mContentWindow, caption.GetUnicode(), msg.GetUnicode(), res = dialog->Prompt(mContentWindow, captionStr.GetUnicode(), msgStr.GetUnicode(),
title.GetUnicode(), &titleUnicode, &retVal); title.GetUnicode(), &titleUnicode, &retVal);
if( retVal == PR_FALSE) if( retVal == PR_FALSE)
@ -1375,7 +1381,8 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
res = nsComponentManager::CreateInstance(kCFileWidgetCID, nsnull, NS_GET_IID(nsIFileWidget), getter_AddRefs(fileWidget)); res = nsComponentManager::CreateInstance(kCFileWidgetCID, nsnull, NS_GET_IID(nsIFileWidget), getter_AddRefs(fileWidget));
if (NS_SUCCEEDED(res) && fileWidget) if (NS_SUCCEEDED(res) && fileWidget)
{ {
nsAutoString promptString = GetString("SaveDocumentAs"); nsAutoString promptString;
GetBundleString("SaveDocumentAs", promptString);
nsString* titles = nsnull; nsString* titles = nsnull;
nsString* filters = nsnull; nsString* filters = nsnull;
@ -1388,31 +1395,32 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
titles = new nsString[3]; titles = new nsString[3];
if (!titles) if (!titles)
{ return NS_ERROR_OUT_OF_MEMORY;
res = NS_ERROR_OUT_OF_MEMORY;
goto SkipFilters;
}
filters = new nsString[3]; filters = new nsString[3];
if (!filters) if (!filters)
{ {
res = NS_ERROR_OUT_OF_MEMORY; delete [] titles;
goto SkipFilters; return NS_ERROR_OUT_OF_MEMORY;
} }
nextTitle = titles; nextTitle = titles;
nextFilter = filters; nextFilter = filters;
// The names of the file types are localizable // The names of the file types are localizable
HTMLFiles = GetString("HTMLFiles"); GetBundleString("HTMLFiles", HTMLFiles);
TextFiles = GetString("TextFiles"); GetBundleString("TextFiles", TextFiles);
if (HTMLFiles.Length() == 0 || TextFiles.Length() == 0) if (! (HTMLFiles.Length() == 0 || TextFiles.Length() == 0))
goto SkipFilters; {
nsAutoString allFilesStr;
GetBundleString("AllFiles", allFilesStr);
*nextTitle++ = HTMLFiles; *nextTitle++ = HTMLFiles;
*nextFilter++ = "*.htm; *.html; *.shtml"; *nextFilter++ = "*.htm; *.html; *.shtml";
*nextTitle++ = TextFiles; *nextTitle++ = TextFiles;
*nextFilter++ = "*.txt"; *nextFilter++ = "*.txt";
*nextTitle++ = GetString("AllFiles"); *nextTitle++ = allFilesStr;
*nextFilter++ = "*.*"; *nextFilter++ = "*.*";
fileWidget->SetFilterList(3, titles, filters); fileWidget->SetFilterList(3, titles, filters);
}
if (noFileSpec) if (noFileSpec)
{ {
@ -1458,7 +1466,9 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
title = title.ReplaceChar(colon, underscore); title = title.ReplaceChar(colon, underscore);
fileName = title + nsString(".html"); fileName = title + nsString(".html");
} }
} else { }
else
{
char *leafName = docFileSpec.GetLeafName(); char *leafName = docFileSpec.GetLeafName();
if (leafName) if (leafName)
{ {
@ -1469,10 +1479,10 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
// TODO: CHANGE TO THE DIRECTORY OF THE PARENT PATH? // TODO: CHANGE TO THE DIRECTORY OF THE PARENT PATH?
} }
if (fileName.Length() > 0) if (fileName.Length() > 0)
fileWidget->SetDefaultString(fileName); fileWidget->SetDefaultString(fileName);
// Why is SkipFilters here? Shouldn't it be before if (noFileSpec)???
SkipFilters:
nsFileDlgResults dialogResult; nsFileDlgResults dialogResult;
// 1ST PARAM SHOULD BE nsIDOMWindow*, not nsIWidget* // 1ST PARAM SHOULD BE nsIDOMWindow*, not nsIWidget*
dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec); dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec);
@ -1516,7 +1526,10 @@ SkipFilters:
res = editor->SaveFile(&docFileSpec, replacing, saveCopy, nsIDiskDocument::eSaveFileHTML); res = editor->SaveFile(&docFileSpec, replacing, saveCopy, nsIDiskDocument::eSaveFileHTML);
if (NS_FAILED(res)) if (NS_FAILED(res))
{ {
Alert(GetString("SaveDocument"), GetString("SaveFileFailed")); nsAutoString saveDocStr, failedStr;
GetBundleString("SaveDocument", saveDocStr);
GetBundleString("SaveFileFailed", failedStr);
Alert(saveDocStr, failedStr);
} else { } else {
// File was saved successfully // File was saved successfully
*_retval = PR_TRUE; *_retval = PR_TRUE;
@ -1533,9 +1546,10 @@ SkipFilters:
NS_IMETHODIMP NS_IMETHODIMP
nsEditorShell::CloseWindow( PRBool *_retval ) nsEditorShell::CloseWindow( PRBool *_retval )
{ {
nsresult rv = NS_OK; nsAutoString beforeClosingStr;
GetBundleString("BeforeClosing", beforeClosingStr);
rv = CheckAndSaveDocument(GetString("BeforeClosing").GetUnicode(),_retval); nsresult rv = CheckAndSaveDocument(beforeClosingStr.GetUnicode(), _retval);
// Don't close the window if there was an error saving file or // Don't close the window if there was an error saving file or
// user canceled an action along the way // user canceled an action along the way
@ -1579,7 +1593,8 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
nsCOMPtr<nsIFileWidget> fileWidget; nsCOMPtr<nsIFileWidget> fileWidget;
nsAutoString HTMLTitle = GetString("OpenHTMLFile"); nsAutoString HTMLTitle;
GetBundleString("OpenHTMLFile", HTMLTitle);
// An empty string should just result in "Open" for the dialog // An empty string should just result in "Open" for the dialog
nsAutoString title; nsAutoString title;
@ -1587,7 +1602,8 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
{ {
title = HTMLTitle; title = HTMLTitle;
} else { } else {
nsAutoString ImageTitle = GetString("SelectImageFile"); nsAutoString ImageTitle;
GetBundleString("SelectImageFile", ImageTitle);
if (ImageTitle.Length() > 0 && imgFilter) if (ImageTitle.Length() > 0 && imgFilter)
title = ImageTitle; title = ImageTitle;
@ -1609,16 +1625,21 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
nsString* nextTitle; nsString* nextTitle;
nsString* nextFilter; nsString* nextFilter;
nsAutoString tempStr;
if (htmlFilter) if (htmlFilter)
{ {
titles = new nsString[3]; titles = new nsString[3];
filters = new nsString[3]; filters = new nsString[3];
if (!titles || ! filters) if (!titles || ! filters)
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
nextTitle = titles; nextTitle = titles;
nextFilter = filters; nextFilter = filters;
*nextTitle++ = GetString("HTMLFiles"); GetBundleString("HTMLFiles", tempStr);
*nextTitle++ = GetString("TextFiles"); *nextTitle++ = tempStr;
GetBundleString("TextFiles", tempStr);
*nextTitle++ = tempStr;
*nextFilter++ = "*.htm; *.html; *.shtml"; *nextFilter++ = "*.htm; *.html; *.shtml";
*nextFilter++ = "*.txt"; *nextFilter++ = "*.txt";
fileWidget->SetFilterList(3, titles, filters); fileWidget->SetFilterList(3, titles, filters);
@ -1629,11 +1650,13 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
nextTitle = titles; nextTitle = titles;
nextFilter = filters; nextFilter = filters;
*nextTitle++ = GetString("IMGFiles"); GetBundleString("IMGFiles", tempStr);
*nextTitle++ = tempStr;
*nextFilter++ = "*.gif; *.jpg; *.jpeg; *.png", "*.*"; *nextFilter++ = "*.gif; *.jpg; *.jpeg; *.png", "*.*";
fileWidget->SetFilterList(2, titles, filters); fileWidget->SetFilterList(2, titles, filters);
} }
*nextTitle++ = GetString("AllFiles"); GetBundleString("AllFiles", tempStr);
*nextTitle++ = tempStr;
*nextFilter++ = "*.*"; *nextFilter++ = "*.*";
// First param should be Parent window, but type is nsIWidget* // First param should be Parent window, but type is nsIWidget*
// Bug is filed to change this to a more suitable window type // Bug is filed to change this to a more suitable window type
@ -1676,7 +1699,7 @@ nsEditorShell::UpdateWindowTitle()
res = GetDocumentTitleString(windowCaption); res = GetDocumentTitleString(windowCaption);
// If title is empty, use "untitled" // If title is empty, use "untitled"
if (windowCaption.Length() == 0) if (windowCaption.Length() == 0)
windowCaption = GetString("untitled"); GetBundleString("untitled", windowCaption);
// Append just the 'leaf' filename to the Doc. Title for the window caption // Append just the 'leaf' filename to the Doc. Title for the window caption
if (NS_SUCCEEDED(res)) if (NS_SUCCEEDED(res))
@ -2371,27 +2394,24 @@ nsEditorShell::GetString(const PRUnichar *name, PRUnichar **_retval)
} }
} }
static nsAutoString *ptmpString = 0;
// Use this version within the shell: // Use this version within the shell:
nsString void nsEditorShell::GetBundleString(const nsString& name, nsString &outString)
nsEditorShell::GetString(const nsString& name)
{ {
// Initialize upon first use to avoid static constructor if (mStringBundle && name.Length() > 0)
if (!ptmpString)
ptmpString = new nsAutoString();
// Don't fail, just return an empty string
*ptmpString = "";
if (mStringBundle && (name != ""))
{ {
const PRUnichar *ptrtmp = name.GetUnicode();
PRUnichar *ptrv = nsnull; PRUnichar *ptrv = nsnull;
nsresult res = mStringBundle->GetStringFromName(ptrtmp, &ptrv); if (NS_SUCCEEDED(mStringBundle->GetStringFromName(name.GetUnicode(), &ptrv)))
if (NS_SUCCEEDED(res)) outString = ptrv;
*ptmpString = ptrv; else
outString = "";
nsAllocator::Free(ptrv);
}
else
{
outString = "";
} }
return *ptmpString;
} }
// Utility to bring up a Yes/No/Cancel dialog. // Utility to bring up a Yes/No/Cancel dialog.
@ -2413,14 +2433,25 @@ nsEditorShell::ConfirmWithCancel(const nsString& aTitle, const nsString& aQuesti
nsAutoString url( "chrome://global/skin/question-icon.gif" ); nsAutoString url( "chrome://global/skin/question-icon.gif" );
block->SetString( nsICommonDialogs::eIconURL, url.GetUnicode()); block->SetString( nsICommonDialogs::eIconURL, url.GetUnicode());
nsAutoString yes = aYesString ? *aYesString : GetString("Yes"); nsAutoString yesStr, noStr;
nsAutoString no = aNoString ? *aNoString : GetString("No"); if (aYesString)
nsAutoString cancel = GetString("Cancel"); yesStr = *aYesString;
else
GetBundleString("Yes", yesStr);
if (aNoString)
noStr = *aNoString;
else
GetBundleString("No", noStr);
nsAutoString cancelStr;
GetBundleString("Cancel", cancelStr);
block->SetString( nsICommonDialogs::eDialogTitle, aTitle.GetUnicode() ); block->SetString( nsICommonDialogs::eDialogTitle, aTitle.GetUnicode() );
//Note: "button0" is always Ok or Yes action, "button1" is Cancel //Note: "button0" is always Ok or Yes action, "button1" is Cancel
block->SetString( nsICommonDialogs::eButton0Text, yes.GetUnicode() ); block->SetString( nsICommonDialogs::eButton0Text, yesStr.GetUnicode() );
block->SetString( nsICommonDialogs::eButton1Text, cancel.GetUnicode() ); block->SetString( nsICommonDialogs::eButton1Text, cancelStr.GetUnicode() );
block->SetString( nsICommonDialogs::eButton2Text, no.GetUnicode() ); block->SetString( nsICommonDialogs::eButton2Text, noStr.GetUnicode() );
NS_WITH_SERVICE(nsICommonDialogs, dialog, kCommonDialogsCID, &rv); NS_WITH_SERVICE(nsICommonDialogs, dialog, kCommonDialogsCID, &rv);
if ( NS_SUCCEEDED( rv ) ) if ( NS_SUCCEEDED( rv ) )

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

@ -154,7 +154,7 @@ class nsEditorShell : public nsIEditorShell,
nsCOMPtr<nsISupportsArray> mDocStateListeners; // contents are nsISupports nsCOMPtr<nsISupportsArray> mDocStateListeners; // contents are nsISupports
// Get a string from the string bundle file // Get a string from the string bundle file
nsString GetString(const nsString& name); void GetBundleString(const nsString& name, nsString &outString);
// Get the text of the <title> tag // Get the text of the <title> tag
NS_IMETHOD GetDocumentTitleString(nsString& title); NS_IMETHOD GetDocumentTitleString(nsString& title);

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

@ -1258,7 +1258,6 @@ nsEditorShell::CheckAndSaveDocument(const PRUnichar *reasonToSave, PRBool *_retv
{ {
*_retval = PR_FALSE; *_retval = PR_FALSE;
nsAutoString ReasonToSave(reasonToSave);
nsCOMPtr<nsIDOMDocument> theDoc; nsCOMPtr<nsIDOMDocument> theDoc;
nsresult rv = GetEditorDocument(getter_AddRefs(theDoc)); nsresult rv = GetEditorDocument(getter_AddRefs(theDoc));
if (NS_SUCCEEDED(rv) && theDoc) if (NS_SUCCEEDED(rv) && theDoc)
@ -1275,18 +1274,22 @@ nsEditorShell::CheckAndSaveDocument(const PRUnichar *reasonToSave, PRBool *_retv
if (modCount > 0) if (modCount > 0)
{ {
// Ask user if they want to save current changes // Ask user if they want to save current changes
nsAutoString tmp1 = GetString("Save"); nsAutoString reasonToSaveStr(reasonToSave);
nsAutoString tmp2 = GetString("DontSave"); nsAutoString tmp1, tmp2, title;
nsAutoString title; GetBundleString("Save", tmp1);
GetBundleString("DontSave", tmp2);
GetDocumentTitleString(title); GetDocumentTitleString(title);
// If title is empty, use "untitled" // If title is empty, use "untitled"
if (title.Length() == 0) if (title.Length() == 0)
title = GetString("untitled"); GetBundleString("untitled", title);
nsAutoString saveMsg = ((GetString("SaveFilePrompt")).ReplaceSubstring("%title%",title)).ReplaceSubstring("%reason%",ReasonToSave); nsAutoString saveMsg;
GetBundleString("SaveFilePrompt", saveMsg);
saveMsg.ReplaceSubstring("%title%", title).ReplaceSubstring("%reason%", reasonToSaveStr);
EConfirmResult result = ConfirmWithCancel(GetString("SaveDocument"), saveMsg, nsAutoString saveDocString;
&tmp1, &tmp2); GetBundleString("SaveDocument", saveDocString);
EConfirmResult result = ConfirmWithCancel(saveDocString, saveMsg, &tmp1, &tmp2);
if (result == eCancel) if (result == eCancel)
{ {
*_retval = PR_FALSE; *_retval = PR_FALSE;
@ -1351,10 +1354,13 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
if (NS_SUCCEEDED(res)) if (NS_SUCCEEDED(res))
{ {
PRUnichar *titleUnicode; PRUnichar *titleUnicode;
nsAutoString caption = GetString("DocumentTitle"); nsAutoString captionStr, msgStr;
nsAutoString msg = GetString("NeedDocTitle");
GetBundleString("DocumentTitle", captionStr);
GetBundleString("NeedDocTitle", msgStr);
PRBool retVal = PR_FALSE; PRBool retVal = PR_FALSE;
res = dialog->Prompt(mContentWindow, caption.GetUnicode(), msg.GetUnicode(), res = dialog->Prompt(mContentWindow, captionStr.GetUnicode(), msgStr.GetUnicode(),
title.GetUnicode(), &titleUnicode, &retVal); title.GetUnicode(), &titleUnicode, &retVal);
if( retVal == PR_FALSE) if( retVal == PR_FALSE)
@ -1375,7 +1381,8 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
res = nsComponentManager::CreateInstance(kCFileWidgetCID, nsnull, NS_GET_IID(nsIFileWidget), getter_AddRefs(fileWidget)); res = nsComponentManager::CreateInstance(kCFileWidgetCID, nsnull, NS_GET_IID(nsIFileWidget), getter_AddRefs(fileWidget));
if (NS_SUCCEEDED(res) && fileWidget) if (NS_SUCCEEDED(res) && fileWidget)
{ {
nsAutoString promptString = GetString("SaveDocumentAs"); nsAutoString promptString;
GetBundleString("SaveDocumentAs", promptString);
nsString* titles = nsnull; nsString* titles = nsnull;
nsString* filters = nsnull; nsString* filters = nsnull;
@ -1388,31 +1395,32 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
titles = new nsString[3]; titles = new nsString[3];
if (!titles) if (!titles)
{ return NS_ERROR_OUT_OF_MEMORY;
res = NS_ERROR_OUT_OF_MEMORY;
goto SkipFilters;
}
filters = new nsString[3]; filters = new nsString[3];
if (!filters) if (!filters)
{ {
res = NS_ERROR_OUT_OF_MEMORY; delete [] titles;
goto SkipFilters; return NS_ERROR_OUT_OF_MEMORY;
} }
nextTitle = titles; nextTitle = titles;
nextFilter = filters; nextFilter = filters;
// The names of the file types are localizable // The names of the file types are localizable
HTMLFiles = GetString("HTMLFiles"); GetBundleString("HTMLFiles", HTMLFiles);
TextFiles = GetString("TextFiles"); GetBundleString("TextFiles", TextFiles);
if (HTMLFiles.Length() == 0 || TextFiles.Length() == 0) if (! (HTMLFiles.Length() == 0 || TextFiles.Length() == 0))
goto SkipFilters; {
nsAutoString allFilesStr;
GetBundleString("AllFiles", allFilesStr);
*nextTitle++ = HTMLFiles; *nextTitle++ = HTMLFiles;
*nextFilter++ = "*.htm; *.html; *.shtml"; *nextFilter++ = "*.htm; *.html; *.shtml";
*nextTitle++ = TextFiles; *nextTitle++ = TextFiles;
*nextFilter++ = "*.txt"; *nextFilter++ = "*.txt";
*nextTitle++ = GetString("AllFiles"); *nextTitle++ = allFilesStr;
*nextFilter++ = "*.*"; *nextFilter++ = "*.*";
fileWidget->SetFilterList(3, titles, filters); fileWidget->SetFilterList(3, titles, filters);
}
if (noFileSpec) if (noFileSpec)
{ {
@ -1458,7 +1466,9 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
title = title.ReplaceChar(colon, underscore); title = title.ReplaceChar(colon, underscore);
fileName = title + nsString(".html"); fileName = title + nsString(".html");
} }
} else { }
else
{
char *leafName = docFileSpec.GetLeafName(); char *leafName = docFileSpec.GetLeafName();
if (leafName) if (leafName)
{ {
@ -1469,10 +1479,10 @@ nsEditorShell::SaveDocument(PRBool saveAs, PRBool saveCopy, PRBool *_retval)
// TODO: CHANGE TO THE DIRECTORY OF THE PARENT PATH? // TODO: CHANGE TO THE DIRECTORY OF THE PARENT PATH?
} }
if (fileName.Length() > 0) if (fileName.Length() > 0)
fileWidget->SetDefaultString(fileName); fileWidget->SetDefaultString(fileName);
// Why is SkipFilters here? Shouldn't it be before if (noFileSpec)???
SkipFilters:
nsFileDlgResults dialogResult; nsFileDlgResults dialogResult;
// 1ST PARAM SHOULD BE nsIDOMWindow*, not nsIWidget* // 1ST PARAM SHOULD BE nsIDOMWindow*, not nsIWidget*
dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec); dialogResult = fileWidget->PutFile(nsnull, promptString, docFileSpec);
@ -1516,7 +1526,10 @@ SkipFilters:
res = editor->SaveFile(&docFileSpec, replacing, saveCopy, nsIDiskDocument::eSaveFileHTML); res = editor->SaveFile(&docFileSpec, replacing, saveCopy, nsIDiskDocument::eSaveFileHTML);
if (NS_FAILED(res)) if (NS_FAILED(res))
{ {
Alert(GetString("SaveDocument"), GetString("SaveFileFailed")); nsAutoString saveDocStr, failedStr;
GetBundleString("SaveDocument", saveDocStr);
GetBundleString("SaveFileFailed", failedStr);
Alert(saveDocStr, failedStr);
} else { } else {
// File was saved successfully // File was saved successfully
*_retval = PR_TRUE; *_retval = PR_TRUE;
@ -1533,9 +1546,10 @@ SkipFilters:
NS_IMETHODIMP NS_IMETHODIMP
nsEditorShell::CloseWindow( PRBool *_retval ) nsEditorShell::CloseWindow( PRBool *_retval )
{ {
nsresult rv = NS_OK; nsAutoString beforeClosingStr;
GetBundleString("BeforeClosing", beforeClosingStr);
rv = CheckAndSaveDocument(GetString("BeforeClosing").GetUnicode(),_retval); nsresult rv = CheckAndSaveDocument(beforeClosingStr.GetUnicode(), _retval);
// Don't close the window if there was an error saving file or // Don't close the window if there was an error saving file or
// user canceled an action along the way // user canceled an action along the way
@ -1579,7 +1593,8 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
nsCOMPtr<nsIFileWidget> fileWidget; nsCOMPtr<nsIFileWidget> fileWidget;
nsAutoString HTMLTitle = GetString("OpenHTMLFile"); nsAutoString HTMLTitle;
GetBundleString("OpenHTMLFile", HTMLTitle);
// An empty string should just result in "Open" for the dialog // An empty string should just result in "Open" for the dialog
nsAutoString title; nsAutoString title;
@ -1587,7 +1602,8 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
{ {
title = HTMLTitle; title = HTMLTitle;
} else { } else {
nsAutoString ImageTitle = GetString("SelectImageFile"); nsAutoString ImageTitle;
GetBundleString("SelectImageFile", ImageTitle);
if (ImageTitle.Length() > 0 && imgFilter) if (ImageTitle.Length() > 0 && imgFilter)
title = ImageTitle; title = ImageTitle;
@ -1609,16 +1625,21 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
nsString* nextTitle; nsString* nextTitle;
nsString* nextFilter; nsString* nextFilter;
nsAutoString tempStr;
if (htmlFilter) if (htmlFilter)
{ {
titles = new nsString[3]; titles = new nsString[3];
filters = new nsString[3]; filters = new nsString[3];
if (!titles || ! filters) if (!titles || ! filters)
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
nextTitle = titles; nextTitle = titles;
nextFilter = filters; nextFilter = filters;
*nextTitle++ = GetString("HTMLFiles"); GetBundleString("HTMLFiles", tempStr);
*nextTitle++ = GetString("TextFiles"); *nextTitle++ = tempStr;
GetBundleString("TextFiles", tempStr);
*nextTitle++ = tempStr;
*nextFilter++ = "*.htm; *.html; *.shtml"; *nextFilter++ = "*.htm; *.html; *.shtml";
*nextFilter++ = "*.txt"; *nextFilter++ = "*.txt";
fileWidget->SetFilterList(3, titles, filters); fileWidget->SetFilterList(3, titles, filters);
@ -1629,11 +1650,13 @@ nsEditorShell::GetLocalFileURL(nsIDOMWindow *parent, const PRUnichar *filterType
return NS_ERROR_OUT_OF_MEMORY; return NS_ERROR_OUT_OF_MEMORY;
nextTitle = titles; nextTitle = titles;
nextFilter = filters; nextFilter = filters;
*nextTitle++ = GetString("IMGFiles"); GetBundleString("IMGFiles", tempStr);
*nextTitle++ = tempStr;
*nextFilter++ = "*.gif; *.jpg; *.jpeg; *.png", "*.*"; *nextFilter++ = "*.gif; *.jpg; *.jpeg; *.png", "*.*";
fileWidget->SetFilterList(2, titles, filters); fileWidget->SetFilterList(2, titles, filters);
} }
*nextTitle++ = GetString("AllFiles"); GetBundleString("AllFiles", tempStr);
*nextTitle++ = tempStr;
*nextFilter++ = "*.*"; *nextFilter++ = "*.*";
// First param should be Parent window, but type is nsIWidget* // First param should be Parent window, but type is nsIWidget*
// Bug is filed to change this to a more suitable window type // Bug is filed to change this to a more suitable window type
@ -1676,7 +1699,7 @@ nsEditorShell::UpdateWindowTitle()
res = GetDocumentTitleString(windowCaption); res = GetDocumentTitleString(windowCaption);
// If title is empty, use "untitled" // If title is empty, use "untitled"
if (windowCaption.Length() == 0) if (windowCaption.Length() == 0)
windowCaption = GetString("untitled"); GetBundleString("untitled", windowCaption);
// Append just the 'leaf' filename to the Doc. Title for the window caption // Append just the 'leaf' filename to the Doc. Title for the window caption
if (NS_SUCCEEDED(res)) if (NS_SUCCEEDED(res))
@ -2371,27 +2394,24 @@ nsEditorShell::GetString(const PRUnichar *name, PRUnichar **_retval)
} }
} }
static nsAutoString *ptmpString = 0;
// Use this version within the shell: // Use this version within the shell:
nsString void nsEditorShell::GetBundleString(const nsString& name, nsString &outString)
nsEditorShell::GetString(const nsString& name)
{ {
// Initialize upon first use to avoid static constructor if (mStringBundle && name.Length() > 0)
if (!ptmpString)
ptmpString = new nsAutoString();
// Don't fail, just return an empty string
*ptmpString = "";
if (mStringBundle && (name != ""))
{ {
const PRUnichar *ptrtmp = name.GetUnicode();
PRUnichar *ptrv = nsnull; PRUnichar *ptrv = nsnull;
nsresult res = mStringBundle->GetStringFromName(ptrtmp, &ptrv); if (NS_SUCCEEDED(mStringBundle->GetStringFromName(name.GetUnicode(), &ptrv)))
if (NS_SUCCEEDED(res)) outString = ptrv;
*ptmpString = ptrv; else
outString = "";
nsAllocator::Free(ptrv);
}
else
{
outString = "";
} }
return *ptmpString;
} }
// Utility to bring up a Yes/No/Cancel dialog. // Utility to bring up a Yes/No/Cancel dialog.
@ -2413,14 +2433,25 @@ nsEditorShell::ConfirmWithCancel(const nsString& aTitle, const nsString& aQuesti
nsAutoString url( "chrome://global/skin/question-icon.gif" ); nsAutoString url( "chrome://global/skin/question-icon.gif" );
block->SetString( nsICommonDialogs::eIconURL, url.GetUnicode()); block->SetString( nsICommonDialogs::eIconURL, url.GetUnicode());
nsAutoString yes = aYesString ? *aYesString : GetString("Yes"); nsAutoString yesStr, noStr;
nsAutoString no = aNoString ? *aNoString : GetString("No"); if (aYesString)
nsAutoString cancel = GetString("Cancel"); yesStr = *aYesString;
else
GetBundleString("Yes", yesStr);
if (aNoString)
noStr = *aNoString;
else
GetBundleString("No", noStr);
nsAutoString cancelStr;
GetBundleString("Cancel", cancelStr);
block->SetString( nsICommonDialogs::eDialogTitle, aTitle.GetUnicode() ); block->SetString( nsICommonDialogs::eDialogTitle, aTitle.GetUnicode() );
//Note: "button0" is always Ok or Yes action, "button1" is Cancel //Note: "button0" is always Ok or Yes action, "button1" is Cancel
block->SetString( nsICommonDialogs::eButton0Text, yes.GetUnicode() ); block->SetString( nsICommonDialogs::eButton0Text, yesStr.GetUnicode() );
block->SetString( nsICommonDialogs::eButton1Text, cancel.GetUnicode() ); block->SetString( nsICommonDialogs::eButton1Text, cancelStr.GetUnicode() );
block->SetString( nsICommonDialogs::eButton2Text, no.GetUnicode() ); block->SetString( nsICommonDialogs::eButton2Text, noStr.GetUnicode() );
NS_WITH_SERVICE(nsICommonDialogs, dialog, kCommonDialogsCID, &rv); NS_WITH_SERVICE(nsICommonDialogs, dialog, kCommonDialogsCID, &rv);
if ( NS_SUCCEEDED( rv ) ) if ( NS_SUCCEEDED( rv ) )

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

@ -154,7 +154,7 @@ class nsEditorShell : public nsIEditorShell,
nsCOMPtr<nsISupportsArray> mDocStateListeners; // contents are nsISupports nsCOMPtr<nsISupportsArray> mDocStateListeners; // contents are nsISupports
// Get a string from the string bundle file // Get a string from the string bundle file
nsString GetString(const nsString& name); void GetBundleString(const nsString& name, nsString &outString);
// Get the text of the <title> tag // Get the text of the <title> tag
NS_IMETHOD GetDocumentTitleString(nsString& title); NS_IMETHOD GetDocumentTitleString(nsString& title);