1 dump('entering util/file.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.file = function (fname) {
6 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
8 JSAN.use('util.error'); this.error = new util.error();
10 this.dirService = Components.classes["@mozilla.org/file/directory_service;1"].
11 getService( Components.interfaces.nsIProperties );
13 if (fname) this.get(fname);
18 util.file.prototype = {
20 'myPackageDir' : 'open_ils_staff_client',
24 '_input_stream' : null,
25 '_output_stream' : null,
27 'get' : function( fname, path ) {
29 if (!fname) { fname = this.name; } else { this.name = fname; }
30 if (!fname) throw('Must specify a filename.');
33 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
34 var pref = Components.classes["@mozilla.org/preferences-service;1"]
35 .getService(Components.interfaces.nsIPrefBranch);
36 if (!path && pref.getBoolPref("open-ils.write_in_user_chrome_directory")) path = 'uchrome';
38 // getBoolPref throws an exception if "open-ils.write_in_user_chrome_directory" is not defined at all
39 // in defaults/preferences/prefs.js
44 this._file = this.dirService.get( "UChrm", Components.interfaces.nsIFile );
45 //this._file = this.dirService.get( "ProfD", Components.interfaces.nsIFile );
49 this._file = this.dirService.get( "AChrom", Components.interfaces.nsIFile );
50 this._file.append(myPackageDir);
51 this._file.append("content");
52 this._file.append("conf");
55 this._file.append(fname);
57 dump('file: ' + this._file.path + '\n');
58 this.error.sdump('D_FILE',this._file.path);
63 this.error.standard_unexpected_error_alert('error in util.file.get('+fname+','+path+')',E);
68 'close' : function() {
70 if (!this._file) throw('Must .get() a file first.');
71 if (this._input_stream) { this._input_stream.close(); this._input_stream = null; }
72 if (this._output_stream) { this._output_stream.close(); this._output_stream = null; }
73 if (this._istream) { this._istream.close(); this._istream = null; }
74 if (this._f) { this._f = null; }
77 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.close(): ' + E);
82 'append_object' : function(obj) {
84 this.write_object('append',obj);
86 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.append_object(): ' + E);
91 'set_object' : function(obj) {
93 this.write_object('truncate',obj);
96 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.set_object(): ' + E);
101 'write_object' : function(write_type,obj) {
103 if (!this._file) throw('Must .get() a file first.');
104 if (!obj) throw('Must specify an object.');
107 try { obj_json = js2JSON( obj ) + '\n'; } catch(E) { throw('Could not JSONify the object: ' + E); }
109 this.write_content(write_type,obj_json);
112 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_object(): ' + E);
117 'write_content' : function(write_type,content) {
119 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
120 if (!this._output_stream) this._create_output_stream(write_type);
121 this._output_stream.write( content, String( content ).length );
123 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_content(): ' + E);
124 //dump('write_type = ' + write_type + '\n');
125 //dump('content = ' + content + '\n');
130 'get_object' : function() {
132 var data = this.get_content();
133 var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
136 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_object(): ' + E);
141 'get_content' : function() {
143 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
145 if (!this._file) throw('Must .get() a file first.');
146 if (!this._file.exists()) throw('File does not exist.');
148 if (!this._input_stream) this._create_input_stream();
149 var data = this._input_stream.read(-1);
150 //var data = {}; this._istream.readLine(data);
153 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_content(): ' + E);
158 '_create_input_stream' : function() {
160 //dump('_create_input_stream()\n');
162 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
164 if (!this._file) throw('Must .get() a file first.');
165 if (!this._file.exists()) throw('File does not exist.');
167 this._f = Components.classes["@mozilla.org/network/file-input-stream;1"]
168 .createInstance(Components.interfaces.nsIFileInputStream);
169 this._f.init(this._file, MODE_RDONLY, 0, 0);
171 this._f.QueryInterface(Components.interfaces.nsILineInputStream);
172 this._istream = this._f;
174 this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
175 .createInstance(Components.interfaces.nsIScriptableInputStream);
177 this._input_stream.init(this._f);
179 throw('Could not instantiate input stream.');
181 return this._input_stream;
184 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_input_stream(): ' + E);
189 '_create_output_stream' : function(param) {
191 //dump('_create_output_stream('+param+') for '+this._file.path+'\n');
193 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
195 if (!this._file) throw('Must .get() a file first.');
197 if (! this._file.exists()) {
198 if (param == 'truncate+exec') {
199 this._file.create( 0, 0777 );
201 this._file.create( 0, PERMS_FILE );
204 this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
205 .createInstance(Components.interfaces.nsIFileOutputStream);
208 this._output_stream.init(this._file, MODE_WRONLY | MODE_APPEND, PERMS_FILE, 0);
210 case 'truncate+exec' :
211 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
215 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
219 return this._output_stream;
222 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_output_stream(): ' + E);
227 'pick_file' : function(params) {
229 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
230 if (typeof params == 'undefined') params = {};
231 if (typeof params.mode == 'undefined') params.mode = 'open';
232 var nsIFilePicker = Components.interfaces.nsIFilePicker;
233 var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker );
236 typeof params.title == 'undefined' ? params.mode : params.title,
237 params.mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
239 if (params.defaultFileName) {
240 fp.defaultString = params.defaultFileName;
242 fp.appendFilters( nsIFilePicker.filterAll );
243 var fp_result = fp.show();
244 if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) {
250 this.error.standard_unexpected_error_alert('error picking file',E);
254 'export_file' : function(params) {
257 if (typeof params == 'undefined') params = {};
258 params.mode = 'save';
259 if (typeof params.data == 'undefined') throw('Need a .data field to export');
260 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
261 var f = obj.pick_file( params );
264 var temp = params.data;
265 if (typeof params.not_json == 'undefined') {
266 temp = js2JSON( temp );
268 obj.write_content( 'truncate', temp );
270 alert('Exported ' + f.leafName);
273 alert('File not chosen for export.');
278 this.error.standard_unexpected_error_alert('Error exporting file',E);
283 'import_file' : function(params) {
286 if (typeof params == 'undefined') params = {};
287 params.mode = 'open';
288 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
289 var f = obj.pick_file(params);
290 if (f && f.exists()) {
292 var temp = obj.get_content();
294 if (typeof params.not_json == 'undefined') {
295 temp = JSON2js( obj.get_content() );
299 alert('File not chosen for import.');
303 this.error.standard_unexpected_error_alert('Error importing file',E);
310 dump('exiting util/file.js\n');