Ticket #604 (closed enhancement: wontfix)
add cross-platform "ask for filename" function to core
| Reported by: | EricShulman | Owned by: | EricShulman |
|---|---|---|---|
| Priority: | minor | Milestone: | 2.7.1 |
| Component: | core | Version: | |
| Severity: | low | Keywords: | |
| Cc: |
Description (last modified by EricShulman) (diff)
The following cross-platform "ask for filename" functionality is very useful for many plugins (and potentially for future core features). Adding it to the core could greatly reduce the overhead due to having this function defined separately in each plugin:
function askForFilename(msg,path,file,mustExist) {
if(window.Components)
var r=mozAskForFilename(msg,path,file,mustExist);
else if(config.browser.isIE)
var r=ieAskForFilename(msg,path,file,mustExist);
else
var r=prompt(msg,path+file)||""; // fallback
return r||"";
}
function mozAskForFilename(msg,path,file,mustExist) {
try {
netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
var nsIFilePicker = window.Components.interfaces.nsIFilePicker;
var picker = Components.classes['@mozilla.org/filepicker;1'].createInstance(nsIFilePicker);
picker.init(window, msg, ,mustExist?nsiFilePicker.modeOpen:nsIFilePicker.modeSave);
var thispath = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
thispath.initWithPath(path);
picker.displayDirectory=thispath;
picker.defaultExtension='html';
picker.defaultString=file;
picker.appendFilters(nsIFilePicker.filterAll|nsIFilePicker.filterText|nsIFilePicker.filterHTML);
if (picker.show()!=nsIFilePicker.returnCancel)
var result=picker.file.persistentDescriptor;
}
catch(e) {
alert('error during local file access: '+e.toString()) }
}
return result;
}
function ieAskForFilename(msg,path,file,mustExist) {
try {
var s = new ActiveXObject('UserAccounts.CommonDialog');
s.Filter='All files|*.*|Text files|*.txt|HTML files|*.htm;*.html|';
s.FilterIndex=3; // default to HTML files;
s.InitialDir=path;
s.FileName=file;
if (s.showOpen())
var result=s.FileName;
}
catch(e) {
var result=prompt(msg,path+file); } // fallback for pre-XP/IE6
}
return result;
}
To support Safari, one or more wrapper functions could be added to TiddlySaver?.jar Java code to support javascript invokation of java file I/O for 'file open' (mustExist=true) and 'file save' (mustExist=false) versions of askForFilename() dialog box.
Change History
Note: See
TracTickets for help on using
tickets.
