webglade

JavaScript library to dynamically create XUL GUI from Glade XML files
git clone https://logand.com/git/webglade.git/
Log | Files | Refs | README | LICENSE

FileManager.js (5297B)


      1 /**
      2  *
      3  * @filename FileManager.js
      4  * @$LastChangedDate: 2004-06-26 13:51:10 +0200 (Sat, 26 Jun 2004) $
      5  * @author Fabio Serra AKA Faser - faser@faser.net
      6  * @copyright Fabio Serra (The Initial Developer of the Original Code)
      7  * @license Mozilla Public License Version 1.1
      8  *
      9 */
     10 
     11 /**
     12  * Construct a new FileManager object
     13  * @class This class is used To manage file from disk.
     14  * It can be used only with installed application.Remote MAB have to read and write from memory
     15  * @constructor
     16  * @return A new FileManager object
     17  *
     18 */
     19 function FileManager() {
     20 	/**
     21 	 *@type string
     22 	*/
     23 	this.path = "";
     24 
     25 	/**
     26 	 *@type string
     27 	*/
     28 	this.leafName = "";
     29 }
     30 
     31 /**
     32  * Read a .mab file
     33  * @throw exception reading file
     34  * @return the file content
     35  * @type string
     36 */
     37 FileManager.prototype.readFile = function() {
     38 	try {
     39 		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
     40 	} catch (e) {
     41 		alert("Permission to read file was denied.");
     42 	}
     43 
     44 	try {
     45 		var nsIFilePicker = Components.interfaces.nsIFilePicker;
     46 		var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
     47 	}catch(e) {
     48 		alert(e);
     49 	}
     50 
     51 	fp.init(window, "Select a MAB File", nsIFilePicker.modeOpen);
     52 	fp.appendFilter("MAB Files","*.mab;");
     53 
     54 	var res=fp.show();
     55 	if (res==nsIFilePicker.returnOK){
     56   		var is = Components.classes["@mozilla.org/network/file-input-stream;1"]
     57 		.createInstance( Components.interfaces.nsIFileInputStream );
     58 
     59 		is.init(fp.file,0x01, 00004, null);
     60 		var sis = Components.classes["@mozilla.org/scriptableinputstream;1"]
     61 		.createInstance( Components.interfaces.nsIScriptableInputStream );
     62 
     63 		sis.init(is);
     64 		var tmp, output;
     65 		try {
     66 			tmp = sis.read(sis.available());
     67 		}catch(e){
     68 			throw(e);
     69 		}
     70 
     71 		try {
     72 			//File should be in UTF-8
     73 			output = this.convertToUnicode("UTF-8",tmp);
     74 		}catch(e) {
     75 			output = tmp;
     76 		}
     77 		
     78 		sis.close();
     79 
     80 		//Set some properties that have to be saved in the XMDManager that contain
     81 		//the current document
     82 		this.path = fp.file.path;
     83 		this.leafName = fp.file.leafName;
     84   		return output;
     85 	}else{
     86 		return false;
     87 	}
     88 
     89 }
     90 
     91 /**
     92  * Save on file the document in UTF-8
     93  * @param {XML Document} XMLDoc the XML document
     94  * @param {string} fullPath the absolute path where to save the file
     95  * @throw alert if it fails
     96 */
     97 FileManager.prototype.saveFile = function(XMLDoc,fullPath) {
     98 	if(!fullPath) {return false;}
     99 	try {
    100 		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    101 	} catch (e) {
    102 		alert("Permission to save file was denied.");
    103 	}
    104 
    105 	try{
    106 		var f = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
    107 		f.initWithPath(fullPath);
    108 			if(f.exists == false) {
    109 				f.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    110 
    111 			}
    112 	} catch (e) {
    113 		alert("Component: \n" + e + '\n fullpath:' + fullPath );
    114 	}
    115 
    116 	try {
    117 		var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    118 			.createInstance( Components.interfaces.nsIFileOutputStream );
    119 	}catch(e) {
    120 		alert("OutputStrem:" + e);
    121 	}
    122 
    123 	try {
    124 		outputStream.init(f, 0x04 | 0x08 | 0x20, 420, 0 );
    125 		var xmlSerializer = new XMLSerializer();
    126 		//Force to save file in UTF-8
    127 		xmlSerializer.serializeToStream(XMLDoc, outputStream, 'UTF-8');
    128 		outputStream.close();
    129 	} catch (e) {
    130 		alert("Unable to write to file: " + e);
    131 	} finally {
    132 		outputStream.close();
    133 	}
    134 
    135 	return true;
    136 }
    137 
    138 /**
    139  *@param {string} aCharset
    140  *@param {string} aSrc
    141  *@type string
    142 */
    143 FileManager.prototype.convertToUnicode = function (aCharset, aSrc ) {
    144 	// http://lxr.mozilla.org/mozilla/source/intl/uconv/idl/nsIScriptableUConv.idl
    145    	var unicodeConverter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
    146 	unicodeConverter.charset = aCharset;
    147 	return unicodeConverter.ConvertToUnicode(aSrc);
    148 }
    149 
    150 /**
    151  * @return the full path where to save the file
    152  * @type string
    153 */
    154 FileManager.prototype.saveAs = function() {
    155 	try {
    156 		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    157 	} catch (e) {
    158 		alert("Permission to write file was denied.");
    159 	}
    160 
    161 	try {
    162 		var nsIFilePicker = Components.interfaces.nsIFilePicker;
    163 		var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
    164 	}catch(e) {
    165 		alert(e);
    166 	}
    167 
    168 	fp.init(window, "Select As", nsIFilePicker.modeSave);
    169 	fp.appendFilter("MAB Files","*.mab;");
    170 
    171 	var res=fp.show();
    172 		if (res==nsIFilePicker.returnOK || nsIFilePicker.returnReplace){
    173 			if(res == nsIFilePicker.returnCancel) {return false;}
    174 			this.path = fp.file.path;
    175 			this.leafName = fp.file.leafName;
    176 
    177 			var extension = ".mab";
    178 			var exProvided = "";
    179 
    180 			var extSeparator = fp.file.leafName.lastIndexOf(".");
    181 			if (extSeparator == -1) {
    182 				this.leafName = fp.file.leafName + extension;
    183 				this.path = fp.file.path + extension;
    184 			}else{
    185 				extProvided = fp.file.leafName.substr(extSeparator);
    186 				if(extProvided != extension) {
    187 					this.leafName = fp.file.leafName + extension;
    188 					this.path = fp.file.leafName + extension;
    189 				}
    190 			}
    191 
    192 			return this.path;
    193 		}else{
    194 			return false;
    195 		}
    196 }