Save default file dirs for HTML and Images, b=93487, r=syd, sr=hewitt

This commit is contained in:
cmanske%netscape.com 2002-01-05 20:25:53 +00:00
parent 9fc64d2378
commit 0336a6e359
4 changed files with 107 additions and 37 deletions

View File

@ -301,6 +301,8 @@ var nsOpenCommand =
fp.appendFilters(nsIFilePicker.filterText);
fp.appendFilters(nsIFilePicker.filterAll);
SetFilePickerDirectory(fp, "html");
/* doesn't handle *.shtml files */
try {
fp.show();
@ -312,9 +314,10 @@ var nsOpenCommand =
/* This checks for already open window and activates it...
* note that we have to test the native path length
* since fileURL.spec will be "file:///" if no filename picked (Cancel button used)
* since file.URL will be "file:///" if no filename picked (Cancel button used)
*/
if (fp.file && fp.file.path.length > 0) {
SaveFilePickerDirectory(fp, "html");
EditorOpenUrl(fp.fileURL.spec);
}
}
@ -441,7 +444,18 @@ function PromptForSaveLocation(aDoSaveAsText, aEditorType, aMIMEType, ahtmlDocum
fileLocation.URL = aDocumentURLString;
var parentLocation = fileLocation.parent;
if (parentLocation)
{
// Save current filepicker's default location
if ("gFilePickerDirectory" in window)
gFilePickerDirectory = fp.displayDirectory;
fp.displayDirectory = parentLocation;
}
else
{
// Initialize to the last-used directory for the particular type (saved in prefs)
SetFilePickerDirectory(fp, aEditorType);
}
}
catch(e) {}
@ -451,7 +465,10 @@ function PromptForSaveLocation(aDoSaveAsText, aEditorType, aMIMEType, ahtmlDocum
// reset urlstring to new save location
dialogResult.resultingURIString = fp.file.URL;
dialogResult.resultingLocalFile = fp.file;
SaveFilePickerDirectory(fp, aEditorType);
}
else if ("gFilePickerDirectory" in window && gFilePickerDirectory)
fp.displayDirectory = gFilePickerDirectory;
return dialogResult;
}

View File

@ -1424,8 +1424,8 @@ function BuildRecentMenu(savePrefs)
}
for (i = 0; i < historyCount; i++)
{
var title = getUnicharPref("editor.history_title_"+i);
var url = getUnicharPref("editor.history_url_"+i);
var title = GetUnicharPref("editor.history_title_"+i);
var url = GetUnicharPref("editor.history_url_"+i);
// Continue if URL pref is missing because
// a URL not found during loading may have been removed
@ -1458,8 +1458,8 @@ function BuildRecentMenu(savePrefs)
{
if (!urlArray[i])
break;
setUnicharPref("editor.history_title_"+i, titleArray[i]);
setUnicharPref("editor.history_url_"+i, urlArray[i]);
SetUnicharPref("editor.history_title_"+i, titleArray[i]);
SetUnicharPref("editor.history_url_"+i, urlArray[i]);
savePrefs = true;
}
}
@ -1511,34 +1511,6 @@ function AppendRecentMenuitem(menupopup, title, url, menuIndex)
}
}
function setUnicharPref(aPrefName, aPrefValue)
{
if (!gPrefs) return;
try
{
var str = Components.classes["@mozilla.org/supports-wstring;1"]
.createInstance(Components.interfaces.nsISupportsWString);
str.data = aPrefValue;
gPrefs.setComplexValue(aPrefName, Components.interfaces.nsISupportsWString, str);
}
catch(e)
{
}
}
function getUnicharPref(aPrefName, aDefVal)
{
if (!gPrefs) return "";
try
{
return gPrefs.getComplexValue(aPrefName, Components.interfaces.nsISupportsWString).data;
}
catch(e)
{
}
return "";
}
function EditorInitFormatMenu()
{
try {

View File

@ -51,6 +51,7 @@ const gOutputEncodeEntities = 256;
var gStringBundle;
var gIOService;
var gPrefsService;
var gFilePickerDirectory;
var gOS = "";
const gWin = "Win";
@ -101,7 +102,9 @@ function GetString(name)
{
if (editorShell)
{
return editorShell.GetString(name);
try {
return editorShell.GetString(name);
} catch (e) {}
}
else
{
@ -112,7 +115,9 @@ function GetString(name)
if (!gStringBundle)
return null;
}
return gStringBundle.GetStringFromName(name);
try {
return gStringBundle.GetStringFromName(name);
} catch (e) {}
}
return null;
}
@ -273,6 +278,73 @@ function GetPrefs()
return null;
}
function SetUnicharPref(aPrefName, aPrefValue)
{
var prefs = GetPrefs();
if (prefs)
{
try
{
var str = Components.classes["@mozilla.org/supports-wstring;1"]
.createInstance(Components.interfaces.nsISupportsWString);
str.data = aPrefValue;
prefs.setComplexValue(aPrefName, Components.interfaces.nsISupportsWString, str);
}
catch(e) {}
}
}
function GetUnicharPref(aPrefName, aDefVal)
{
var prefs = GetPrefs();
if (prefs)
{
try
{
return prefs.getComplexValue(aPrefName, Components.interfaces.nsISupportsWString).data;
}
catch(e) {}
}
return "";
}
// Set initial directory for a filepicker from URLs saved in prefs
function SetFilePickerDirectory(filePicker, fileType)
{
if (filePicker)
{
var location = GetUnicharPref("editor.lastFileLocation."+fileType);
if (location)
{
try {
var lastLocation = Components.classes["@mozilla.org/file/local;1"].createInstance().QueryInterface(Components.interfaces.nsIFile);
lastLocation.URL = location;
// Save current directory so we can reset it in SaveFilePickerDirectory
gFilePickerDirectory = filePicker.displayDirectory;
filePicker.displayDirectory = lastLocation;
}
catch(e) {}
}
}
}
// Save the directory of the selected file to prefs
function SaveFilePickerDirectory(filePicker, fileType)
{
if (filePicker && filePicker.file && filePicker.file.parent)
{
SetUnicharPref("editor.lastFileLocation."+fileType, filePicker.file.parent.URL);
}
// Restore the directory used before SetFilePickerDirectory was called;
// This reduces interference with Browser and other module directory defaults
if (gFilePickerDirectory)
filePicker.displayDirectory = gFilePickerDirectory;
gFilePickerDirectory = "";
}
function GetDefaultBrowserColors()
{
var prefs = GetPrefs();
@ -586,3 +658,4 @@ function GetOS()
return gOS;
}

View File

@ -664,11 +664,13 @@ const nsIFilePicker = Components.interfaces.nsIFilePicker;
function GetLocalFileURL(filterType)
{
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
var fileType = "html";
if (filterType == "img")
{
fp.init(window, GetString("SelectImageFile"), nsIFilePicker.modeOpen);
fp.appendFilters(nsIFilePicker.filterImages);
fileType = "image";
}
// Current usage of this is in Link dialog,
// where we always want HTML first
@ -689,6 +691,10 @@ function GetLocalFileURL(filterType)
// Default or last filter is "All Files"
fp.appendFilters(nsIFilePicker.filterAll);
// set the file picker's current directory to last-opened location saved in prefs
SetFilePickerDirectory(fp, fileType);
/* doesn't handle *.shtml files */
try {
var ret = fp.show();
@ -699,8 +705,10 @@ function GetLocalFileURL(filterType)
dump("filePicker.chooseInputFile threw an exception\n");
return null;
}
return fp.fileURL.spec;
SaveFilePickerDirectory(fp, fileType);
// Note: fp.file.URL = fp.fileURL.spec
return fp.file ? fp.file.URL : null;
}
function GetMetaElement(name)