]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/file.js
internal: an alternative to default_focus
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / file.js
1 dump('entering util/file.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.file = function (fname) {
5
6     JSAN.use('util.error'); this.error = new util.error();
7
8     this.dirService = Components.classes["@mozilla.org/file/directory_service;1"].
9         getService( Components.interfaces.nsIProperties );
10
11     if (fname) this.get(fname);
12
13     return this;
14 };
15
16 util.file.prototype = {
17
18     'myPackageDir' : 'open_ils_staff_client',
19
20     'name' : '',
21     '_file' : null,
22     '_input_stream' : null,
23     '_output_stream' : null,
24
25     'get' : function( fname, path ) {
26         try {
27             if (!fname) { fname = this.name; } else { this.name = fname; }
28             if (!fname) throw('Must specify a filename.');
29
30             try {
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';
34             } catch(E) {
35                 // getBoolPref throws an exception if "open-ils.write_in_user_chrome_directory" is not defined at all
36                 // in defaults/preferences/prefs.js
37             }
38
39             switch(path) {
40                 case 'uchrome' :
41                     this._file = this.dirService.get( "UChrm",  Components.interfaces.nsIFile );
42                     //this._file = this.dirService.get( "ProfD",  Components.interfaces.nsIFile );
43                 break;
44                 default:
45                 case 'chrome' : 
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"); 
50                 break;
51             }
52             this._file.append(fname);
53     
54             dump('file: ' + this._file.path + '\n');
55             this.error.sdump('D_FILE',this._file.path);
56
57             return this._file;
58
59         } catch(E) {
60             this.error.standard_unexpected_error_alert('error in util.file.get('+fname+','+path+')',E);
61             throw(E);
62         }
63     },
64
65     'close' : function() {
66         try {
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; }
72
73         } catch(E) {
74             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.close(): ' + E);
75             throw(E);
76         }
77     },
78
79     'append_object' : function(obj) {
80         try {
81             this.write_object('append',obj);
82         } catch(E) {
83             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.append_object(): ' + E);
84             throw(E);
85         }
86     },
87
88     'set_object' : function(obj) {
89         try {
90             this.write_object('truncate',obj);
91             this.close();
92         } catch(E) {
93             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.set_object(): ' + E);
94             throw(E);
95         }
96     },
97
98     'write_object' : function(write_type,obj) {
99         try {
100             if (!this._file) throw('Must .get() a file first.');
101             if (!obj) throw('Must specify an object.');
102
103             var obj_json; 
104             try { obj_json = js2JSON( obj ) + '\n'; } catch(E) { throw('Could not JSONify the object: ' + E); }
105
106             this.write_content(write_type,obj_json);
107
108         } catch(E) {
109             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_object(): ' + E);
110             throw(E);
111         }
112     },
113
114     'write_content' : function(write_type,content) {
115         try {
116             if (!this._output_stream) this._create_output_stream(write_type);
117             this._output_stream.write( content, String( content ).length );
118         } catch(E) {
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');
122             throw(E);
123         }
124     },
125
126     'get_object' : function() {
127         try {
128             var data = this.get_content();
129             var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
130             return obj;
131         } catch(E) {
132             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_object(): ' + E);
133             throw(E);
134         }
135     },
136
137     'get_content' : function() {
138         try {
139             if (!this._file) throw('Must .get() a file first.');
140             if (!this._file.exists()) throw('File does not exist.');
141             
142             if (!this._input_stream) this._create_input_stream();
143             var data = this._input_stream.read(-1);
144             //var data = {}; this._istream.readLine(data);
145             return data;
146         } catch(E) {
147             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_content(): ' + E);
148             throw(E);
149         }
150     },
151
152     '_create_input_stream' : function() {
153         try {
154             //dump('_create_input_stream()\n');
155             
156             if (!this._file) throw('Must .get() a file first.');
157             if (!this._file.exists()) throw('File does not exist.');
158
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);
162             /*
163             this._f.QueryInterface(Components.interfaces.nsILineInputStream);
164             this._istream = this._f;
165             */
166             this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
167                 .createInstance(Components.interfaces.nsIScriptableInputStream);
168             if (this._f) {
169                 this._input_stream.init(this._f);
170             } else {
171                 throw('Could not instantiate input stream.');
172             }
173             return this._input_stream;
174
175         } catch(E) {
176             this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_input_stream(): ' + E);
177             throw(E);
178         }
179     },
180
181     '_create_output_stream' : function(param) {
182         try {
183             //dump('_create_output_stream('+param+') for '+this._file.path+'\n');
184             
185             if (!this._file) throw('Must .get() a file first.');
186
187             if (! this._file.exists()) {
188                 if (param == 'truncate+exec') {
189                     this._file.create( 0, 0777 );
190                 } else {
191                     this._file.create( 0, PERMS_FILE );
192                 }
193             }
194             this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
195                 .createInstance(Components.interfaces.nsIFileOutputStream);
196             switch(param){
197                 case 'append' :
198                     this._output_stream.init(this._file, MODE_WRONLY | MODE_APPEND, PERMS_FILE, 0);
199                 break;
200                 case 'truncate+exec' :
201                     this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
202                 break;
203                 case 'truncate' :
204                 default:
205                     this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
206                 break;
207             }
208
209             return this._output_stream;
210
211         } catch(E) {
212             this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_output_stream(): ' + E);
213             throw(E);
214         }
215     },
216
217     'pick_file' : function(params) {
218         try {
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 );
223             fp.init( 
224                 window, 
225                 typeof params.title == 'undefined' ? params.mode : params.title,
226                 params.mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
227             );
228             if (params.defaultFileName) {
229                 fp.defaultString = params.defaultFileName;
230             }
231             fp.appendFilters( nsIFilePicker.filterAll );
232             var fp_result = fp.show();
233             if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) {
234                 return fp.file;
235             } else {
236                 return null;
237             }
238         } catch(E) {
239             this.error.standard_unexpected_error_alert('error picking file',E);
240         }
241     },
242
243     'export_file' : function(params) {
244         try {
245             var obj = this;
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 );
250             if (f) {
251                 obj._file = f;
252                 var temp = params.data;
253                 if (typeof params.not_json == 'undefined') {
254                     temp = js2JSON( temp );
255                 }
256                 obj.write_content( 'truncate', temp );
257                 obj.close();
258                 alert('Exported ' + f.leafName);
259                 return obj._file;
260             } else {
261                 alert('File not chosen for export.');
262                 return null;
263             }
264
265         } catch(E) {
266             this.error.standard_unexpected_error_alert('Error exporting file',E);
267                 return null;
268         }
269     },
270
271     'import_file' : function(params) {
272         try {
273             var obj = this;
274             if (typeof params == 'undefined') params = {};
275             params.mode = 'open';
276             var f = obj.pick_file(params);
277             if (f && f.exists()) {
278                 obj._file = f;
279                 var temp = obj.get_content();
280                 obj.close();
281                 if (typeof params.not_json == 'undefined') {
282                     temp = JSON2js( obj.get_content() );
283                 }
284                 return temp;
285             } else {
286                 alert('File not chosen for import.');
287                 return null;
288             }
289         } catch(E) {
290             this.error.standard_unexpected_error_alert('Error importing file',E);
291             return null;
292         }
293     }
294
295 }
296
297 dump('exiting util/file.js\n');