mozdev.org

Mozile (xhtml editing in your browser)    

resources:
Overview

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


/* ***** BEGIN LICENSE BLOCK *****
 * Licensed under Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * Full Terms at http://mozile.mozdev.org/license2.html
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is James A. Overton's code (james@overton.ca).
 *
 * The Initial Developer of the Original Code is James A. Overton.
 * Portions created by the Initial Developer are Copyright (C) 2005-2006
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *	James A. Overton <james@overton.ca>
 *
 * ***** END LICENSE BLOCK ***** */

/** 
 * @fileoverview A module used for saving files to a local file system. Requires privileges in order to function.
 * @link http://mozile.mozdev.org 
 * @author James A. Overton <james@overton.ca>
 * @version 0.7
 */

/** 
 * Initialize the module.
 * @type Void
 */
mozile.getModule("LocalFile").init = function() {
		// Copy this module to the mozile save list
	mozile.addSaveModule(mozile.getModule("LocalFile"));
	
	mozile.getModule("LocalFile").setOption("label", "Save to File");
	
		// If the configuration of this modules does not give a filepath, then set it to ""
	if(!mozile.getModule("LocalFile").getOption("file")) {
		mozile.getModule("LocalFile").setOption("file", "");
	}
		// If the configuration of this modules says it's the default, then set it as the default.
	if(mozile.getModule("LocalFile").getOption("default")) {
		mozile.setDefaultSaveModule(mozile.getSaveModule("LocalFile"));
	}
}


/** 
 * Using the "current" save options, saves the document to a file on the local system.
 * @type Boolean
 * @return True if successful, false otherwise.
 */
mozile.getModule("LocalFile").save = function() {
	var f = new Array();
	f["File"] = "LocalFile/LocalFile.js";
	f["Function"] = "mozileSaveToFile()";
	
	var file;
	
	//Get privileges
	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 this is a local file, and no file is set, set it to this file's path.
		if(document.location.protocol == "file:" && mozile.getSaveOption("current", "content") == "document" && !mozile.getSaveOption("current", "file") ) 
			mozile.setSaveOption("current", "file", document.location.pathname);
			
		// Save to a stored path.
		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"));
		}
		
		// Display a File Picker to choose the save path.
		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);
			// This might be causing a problem under Windows XP
			//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.defaultString = document.location.href;
			filePicker.appendFilters(filePicker.filterText | filePicker.filterHTML |filePicker.filterXML | filePicker.filterAll);
			filePicker.defaultExtension = "txt";
			//filePicker.defaultExtension = "mozile.txt";
				
			var result = filePicker.show();
			if(result == filePicker.returnOK || result == filePicker.returnReplace) {
				file = filePicker.file;
				mozile.setSaveOption("current", "file", file.path);
			}
			else {
				mozile.debug(f,1,"File Picker cancelled.");
				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 Wed Nov 1 15:11:15 2006

The mozile project can be contacted through the mailing list or the member list.
Copyright © 2000-2019. All rights reserved. Terms of Use & Privacy Policy.