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 this.write_content(write_type,obj_json);
109 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_object(): ' + E);
114 'write_content' : function(write_type,content) {
116 if (!this._output_stream) this._create_output_stream(write_type);
117 this._output_stream.write( content, String( content ).length );
119 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_content(): ' + E);
120 //dump('write_type = ' + write_type + '\n');
121 //dump('content = ' + content + '\n');
126 'get_object' : function() {
128 var data = this.get_content();
129 var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
132 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_object(): ' + E);
137 'get_content' : function() {
139 if (!this._file) throw('Must .get() a file first.');
140 if (!this._file.exists()) throw('File does not exist.');
142 if (!this._input_stream) this._create_input_stream();
143 var data = this._input_stream.read(-1);
144 //var data = {}; this._istream.readLine(data);
147 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_content(): ' + E);
152 '_create_input_stream' : function() {
154 //dump('_create_input_stream()\n');
156 if (!this._file) throw('Must .get() a file first.');
157 if (!this._file.exists()) throw('File does not exist.');
159 this._f = Components.classes["@mozilla.org/network/file-input-stream;1"]
160 .createInstance(Components.interfaces.nsIFileInputStream);
161 this._f.init(this._file, MODE_RDONLY, 0, 0);
163 this._f.QueryInterface(Components.interfaces.nsILineInputStream);
164 this._istream = this._f;
166 this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
167 .createInstance(Components.interfaces.nsIScriptableInputStream);
169 this._input_stream.init(this._f);
171 throw('Could not instantiate input stream.');
173 return this._input_stream;
176 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_input_stream(): ' + E);
181 '_create_output_stream' : function(param) {
183 //dump('_create_output_stream('+param+') for '+this._file.path+'\n');
185 if (!this._file) throw('Must .get() a file first.');
187 if (! this._file.exists()) {
188 if (param == 'truncate+exec') {
189 this._file.create( 0, 0777 );
191 this._file.create( 0, PERMS_FILE );
194 this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
195 .createInstance(Components.interfaces.nsIFileOutputStream);
198 this._output_stream.init(this._file, MODE_WRONLY | MODE_APPEND, PERMS_FILE, 0);
200 case 'truncate+exec' :
201 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
205 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
209 return this._output_stream;
212 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_output_stream(): ' + E);
217 'pick_file' : function(params) {
219 if (typeof params == 'undefined') params = {};
220 if (typeof params.mode == 'undefined') params.mode = 'open';
221 var nsIFilePicker = Components.interfaces.nsIFilePicker;
222 var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker );
225 typeof params.title == 'undefined' ? params.mode : params.title,
226 params.mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
228 if (params.defaultFileName) {
229 fp.defaultString = params.defaultFileName;
231 fp.appendFilters( nsIFilePicker.filterAll );
232 var fp_result = fp.show();
233 if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) {
239 this.error.standard_unexpected_error_alert('error picking file',E);
243 'export_file' : function(params) {
246 if (typeof params == 'undefined') params = {};
247 params.mode = 'save';
248 if (typeof params.data == 'undefined') throw('Need a .data field to export');
249 var f = obj.pick_file( params );
252 var temp = params.data;
253 if (typeof params.not_json == 'undefined') {
254 temp = js2JSON( temp );
256 obj.write_content( 'truncate', temp );
258 alert('Exported ' + f.leafName);
261 alert('File not chosen for export.');
266 this.error.standard_unexpected_error_alert('Error exporting file',E);
271 'import_file' : function(params) {
274 if (typeof params == 'undefined') params = {};
275 params.mode = 'open';
276 var f = obj.pick_file(params);
277 if (f && f.exists()) {
279 var temp = obj.get_content();
281 if (typeof params.not_json == 'undefined') {
282 temp = JSON2js( obj.get_content() );
286 alert('File not chosen for import.');
290 this.error.standard_unexpected_error_alert('Error importing file',E);
297 dump('exiting util/file.js\n');