1 dump('entering util/file.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.file = function (fname) {
6 JSAN.use('util.error'); this.error = new util.error();
8 this.dirService = Components.classes["@mozilla.org/file/directory_service;1"].
9 getService( Components.interfaces.nsIProperties );
11 if (fname) this.get(fname);
16 util.file.prototype = {
18 'myPackageDir' : 'open_ils_staff_client',
22 '_input_stream' : null,
23 '_output_stream' : null,
25 'get' : function( fname, path ) {
27 if (!fname) { fname = this.name; } else { this.name = fname; }
28 if (!fname) throw('Must specify a filename.');
31 var pref = Components.classes["@mozilla.org/preferences-service;1"]
32 .getService(Components.interfaces.nsIPrefBranch);
33 if (!path && pref.getBoolPref("open-ils.write_in_user_chrome_directory")) path = 'uchrome';
35 // getBoolPref throws an exception if "open-ils.write_in_user_chrome_directory" is not defined at all
36 // in defaults/preferences/prefs.js
41 this._file = this.dirService.get( "UChrm", Components.interfaces.nsIFile );
42 //this._file = this.dirService.get( "ProfD", Components.interfaces.nsIFile );
46 this._file = this.dirService.get( "AChrom", Components.interfaces.nsIFile );
47 this._file.append(myPackageDir);
48 this._file.append("content");
49 this._file.append("conf");
52 this._file.append(fname);
54 dump('file: ' + this._file.path + '\n');
55 this.error.sdump('D_FILE',this._file.path);
60 this.error.standard_unexpected_error_alert('error in util.file.get('+fname+','+path+')',E);
65 'close' : function() {
67 if (!this._file) throw('Must .get() a file first.');
68 if (this._input_stream) { this._input_stream.close(); this._input_stream = null; }
69 if (this._output_stream) { this._output_stream.close(); this._output_stream = null; }
70 if (this._istream) { this._istream.close(); this._istream = null; }
71 if (this._f) { this._f = null; }
74 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.close(): ' + E);
79 'append_object' : function(obj) {
81 this.write_object('append',obj);
83 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.append_object(): ' + E);
88 'set_object' : function(obj) {
90 this.write_object('truncate',obj);
93 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.set_object(): ' + E);
98 'write_object' : function(write_type,obj) {
100 if (!this._file) throw('Must .get() a file first.');
101 if (!obj) throw('Must specify an object.');
104 try { obj_json = js2JSON( obj ) + '\n'; } catch(E) { throw('Could not JSONify the object: ' + E); }
106 var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
107 .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
108 converter.charset = "UTF-8";
109 var converted = converter.ConvertFromUnicode(obj_json);
111 this.write_content(write_type,converted);
114 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_object(): ' + E);
119 'write_content' : function(write_type,content) {
121 if (!this._output_stream) this._create_output_stream(write_type);
122 this._output_stream.write( content, String( content ).length );
124 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_content(): ' + E);
125 //dump('write_type = ' + write_type + '\n');
126 //dump('content = ' + content + '\n');
131 'get_object' : function() {
133 var data = this.get_content();
134 var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
135 .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
136 converter.charset = "UTF-8";
137 var converted = converter.ConvertToUnicode(data);
138 var obj; try { obj = JSON2js( converted ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
141 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_object(): ' + E);
146 'get_content' : function() {
148 if (!this._file) throw('Must .get() a file first.');
149 if (!this._file.exists()) throw('File does not exist.');
151 if (!this._input_stream) this._create_input_stream();
152 var data = this._input_stream.read(-1);
153 //var data = {}; this._istream.readLine(data);
156 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_content(): ' + E);
161 '_create_input_stream' : function() {
163 //dump('_create_input_stream()\n');
165 if (!this._file) throw('Must .get() a file first.');
166 if (!this._file.exists()) throw('File does not exist.');
168 this._f = Components.classes["@mozilla.org/network/file-input-stream;1"]
169 .createInstance(Components.interfaces.nsIFileInputStream);
170 this._f.init(this._file, MODE_RDONLY, 0, 0);
172 this._f.QueryInterface(Components.interfaces.nsILineInputStream);
173 this._istream = this._f;
175 this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
176 .createInstance(Components.interfaces.nsIScriptableInputStream);
178 this._input_stream.init(this._f);
180 throw('Could not instantiate input stream.');
182 return this._input_stream;
185 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_input_stream(): ' + E);
190 '_create_output_stream' : function(param) {
192 //dump('_create_output_stream('+param+') for '+this._file.path+'\n');
194 if (!this._file) throw('Must .get() a file first.');
196 if (! this._file.exists()) {
197 if (param == 'truncate+exec') {
198 this._file.create( 0, 0777 );
200 this._file.create( 0, PERMS_FILE );
203 this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
204 .createInstance(Components.interfaces.nsIFileOutputStream);
207 this._output_stream.init(this._file, MODE_WRONLY | MODE_APPEND, PERMS_FILE, 0);
209 case 'truncate+exec' :
210 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
214 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
218 return this._output_stream;
221 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_output_stream(): ' + E);
226 'pick_file' : function(params) {
228 if (typeof params == 'undefined') params = {};
229 if (typeof params.mode == 'undefined') params.mode = 'open';
230 var nsIFilePicker = Components.interfaces.nsIFilePicker;
231 var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker );
234 typeof params.title == 'undefined' ? params.mode : params.title,
235 params.mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
237 if (params.defaultFileName) {
238 fp.defaultString = params.defaultFileName;
240 fp.appendFilters( nsIFilePicker.filterAll );
241 var fp_result = fp.show();
242 if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) {
248 this.error.standard_unexpected_error_alert('error picking file',E);
252 'export_file' : function(params) {
255 if (typeof params == 'undefined') params = {};
256 params.mode = 'save';
257 if (typeof params.data == 'undefined') throw('Need a .data field to export');
258 var f = obj.pick_file( params );
261 var temp = params.data;
262 if (typeof params.not_json == 'undefined') {
263 temp = js2JSON( temp );
265 obj.write_content( 'truncate', temp );
267 alert('Exported ' + f.leafName);
270 alert('File not chosen for export.');
275 this.error.standard_unexpected_error_alert('Error exporting file',E);
280 'import_file' : function(params) {
283 if (typeof params == 'undefined') params = {};
284 params.mode = 'open';
285 var f = obj.pick_file(params);
286 if (f && f.exists()) {
288 var temp = obj.get_content();
290 if (typeof params.not_json == 'undefined') {
291 temp = JSON2js( obj.get_content() );
295 alert('File not chosen for import.');
299 this.error.standard_unexpected_error_alert('Error importing file',E);
306 dump('exiting util/file.js\n');