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 );
48 this._file = this.dirService.get( "AChrom", Components.interfaces.nsIFile );
49 this._file.append("skin");
53 this._file = this.dirService.get( "AChrom", Components.interfaces.nsIFile );
54 this._file.append(myPackageDir);
55 this._file.append("content");
56 this._file.append("conf");
59 this._file.append(fname);
61 dump('file: ' + this._file.path + '\n');
62 this.error.sdump('D_FILE',this._file.path);
67 this.error.standard_unexpected_error_alert('error in util.file.get('+fname+','+path+')',E);
72 'close' : function() {
74 if (!this._file) throw('Must .get() a file first.');
75 if (this._input_stream) { this._input_stream.close(); this._input_stream = null; }
76 if (this._output_stream) { this._output_stream.close(); this._output_stream = null; }
77 if (this._istream) { this._istream.close(); this._istream = null; }
78 if (this._f) { this._f = null; }
81 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.close(): ' + E);
86 'append_object' : function(obj) {
88 this.write_object('append',obj);
90 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.append_object(): ' + E);
95 'set_object' : function(obj) {
97 this.write_object('truncate',obj);
100 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.set_object(): ' + E);
105 'write_object' : function(write_type,obj) {
107 if (!this._file) throw('Must .get() a file first.');
108 if (!obj) throw('Must specify an object.');
111 try { obj_json = js2JSON( obj ) + '\n'; } catch(E) { throw('Could not JSONify the object: ' + E); }
113 this.write_content(write_type,obj_json);
116 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_object(): ' + E);
121 'write_content' : function(write_type,content) {
123 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
124 if (!this._output_stream) this._create_output_stream(write_type);
125 this._output_stream.write( content, String( content ).length );
127 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_content(): ' + E);
128 //dump('write_type = ' + write_type + '\n');
129 //dump('content = ' + content + '\n');
134 'get_object' : function() {
136 var data = this.get_content();
137 var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
140 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_object(): ' + E);
145 'get_content' : function() {
147 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
149 if (!this._file) throw('Must .get() a file first.');
150 if (!this._file.exists()) throw('File does not exist.');
152 if (!this._input_stream) this._create_input_stream();
153 var data = this._input_stream.read(-1);
154 //var data = {}; this._istream.readLine(data);
157 this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_content(): ' + E);
162 '_create_input_stream' : function() {
164 //dump('_create_input_stream()\n');
166 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
168 if (!this._file) throw('Must .get() a file first.');
169 if (!this._file.exists()) throw('File does not exist.');
171 this._f = Components.classes["@mozilla.org/network/file-input-stream;1"]
172 .createInstance(Components.interfaces.nsIFileInputStream);
173 this._f.init(this._file, MODE_RDONLY, 0, 0);
175 this._f.QueryInterface(Components.interfaces.nsILineInputStream);
176 this._istream = this._f;
178 this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
179 .createInstance(Components.interfaces.nsIScriptableInputStream);
181 this._input_stream.init(this._f);
183 throw('Could not instantiate input stream.');
185 return this._input_stream;
188 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_input_stream(): ' + E);
193 '_create_output_stream' : function(param) {
195 //dump('_create_output_stream('+param+') for '+this._file.path+'\n');
197 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
199 if (!this._file) throw('Must .get() a file first.');
201 if (! this._file.exists()) {
202 if (param == 'truncate+exec') {
203 this._file.create( 0, 0777 );
205 this._file.create( 0, PERMS_FILE );
208 this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
209 .createInstance(Components.interfaces.nsIFileOutputStream);
212 this._output_stream.init(this._file, MODE_WRONLY | MODE_APPEND, PERMS_FILE, 0);
214 case 'truncate+exec' :
215 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
219 this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
223 return this._output_stream;
226 this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_output_stream(): ' + E);
231 'pick_file' : function(params) {
233 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
234 if (typeof params == 'undefined') params = {};
235 if (typeof params.mode == 'undefined') params.mode = 'open';
236 var nsIFilePicker = Components.interfaces.nsIFilePicker;
237 var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker );
240 typeof params.title == 'undefined' ? params.mode : params.title,
241 params.mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
243 if (params.defaultFileName) {
244 fp.defaultString = params.defaultFileName;
246 fp.appendFilters( nsIFilePicker.filterAll );
247 var fp_result = fp.show();
248 if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) {
254 this.error.standard_unexpected_error_alert('error picking file',E);
258 'export_file' : function(params) {
261 if (typeof params == 'undefined') params = {};
262 params.mode = 'save';
263 if (typeof params.data == 'undefined') throw('Need a .data field to export');
264 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
265 var f = obj.pick_file( params );
268 var temp = params.data;
269 if (typeof params.not_json == 'undefined') {
270 temp = js2JSON( temp );
272 obj.write_content( 'truncate', temp );
274 alert('Exported ' + f.leafName);
277 alert('File not chosen for export.');
282 this.error.standard_unexpected_error_alert('Error exporting file',E);
287 'import_file' : function(params) {
290 if (typeof params == 'undefined') params = {};
291 params.mode = 'open';
292 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
293 var f = obj.pick_file(params);
294 if (f && f.exists()) {
296 var temp = obj.get_content();
298 if (typeof params.not_json == 'undefined') {
299 temp = JSON2js( obj.get_content() );
303 alert('File not chosen for import.');
307 this.error.standard_unexpected_error_alert('Error importing file',E);
314 dump('exiting util/file.js\n');