LocalFile.js
Summary
A module used for saving files to a local file system. Requires privileges in order to function.
Version: 0.7
Author: James A. Overton
mozile.addSaveModule(mozile.getModule("LocalFile"));
mozile.getModule("LocalFile").setOption("label", "Save to File");
if(!mozile.getModule("LocalFile").getOption("file")) {
mozile.getModule("LocalFile").setOption("file", "");
}
if(mozile.getModule("LocalFile").getOption("default")) {
mozile.setDefaultSaveModule(mozile.getSaveModule("LocalFile"));
}
mozile.getModule("LocalFile").save = function() {
var f = new Array();
f["File"] = "LocalFile/LocalFile.js";
f["Function"] = "mozileSaveToFile()";
var file;
try{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalBrowserWrite ");
} catch(e){
mozile.debug(f,3,"Universal Connect denied: "+e);
mozile.status(f,3,"Save failed! Insufficient privileges.");
return false;
}
try {
if(mozile.getDocument().location.protocol == "file:" && mozile.getSaveOption("current", "content") == "document" && !mozile.getSaveOption("current", "file") )
mozile.setSaveOption("current", "file", mozile.getDocument().location.pathname);
if(mozile.getSaveOption("current", "file") && !mozile.getSaveOption("current", "prompt")) {
mozile.debug(f,1,"Stored path: "+mozile.getSaveOption("current", "file"));
file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(mozile.getSaveOption("current", "file"));
}
else {
mozile.debug(f,1,"No stored path.");
var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
filePicker.init(window, "Save As", filePicker.modeSave);
if(mozile.getSaveOption("current", "file")) {
var dir = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
dir.initWithPath(mozile.getSaveOption("current", "file"))
filePicker.displayDirectory = dir;
}
filePicker.appendFilters(filePicker.filterText);
filePicker.appendFilters(filePicker.filterHTML);
filePicker.appendFilters(filePicker.filterXML);
filePicker.appendFilters(filePicker.filterAll);
filePicker.defaultExtension = "txt";
var result = filePicker.show();
if(result == filePicker.returnOK || result == filePicker.returnReplace) {
file = filePicker.file;
mozile.setSaveOption("current", "file", file.path);
}
else {
return false;
}
}
if(!file.exists()) {
file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 444);
}
if(!file.isWritable()) {
mozile.status(f,3,"Can't write to file!!");
}
var text = mozile._convertCharacterSet(mozile.content());
var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
outputStream.init(file, 0x04 | 0x08 | 0x20, 0444, null);
outputStream.write(text, text.length);
outputStream.close();
} catch(e) {
alert("Some sort of output error "+e);
}
mozile.status(f,1,"File saved.");
return true;
}
Documentation generated by
JSDoc on Thu Feb 16 20:20:37 2006