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