]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/file.js
Merge branch 'opac-tt-poc' of git+ssh://yeti.esilibrary.com/home/evergreen/evergreen...
[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     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                 case 'skin' :
48                     this._file = this.dirService.get( "AChrom",  Components.interfaces.nsIFile );
49                     this._file.append("skin");
50                 break;
51                 default:
52                 case 'chrome' : 
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"); 
57                 break;
58             }
59             this._file.append(fname);
60     
61             dump('file: ' + this._file.path + '\n');
62             this.error.sdump('D_FILE',this._file.path);
63
64             return this._file;
65
66         } catch(E) {
67             this.error.standard_unexpected_error_alert('error in util.file.get('+fname+','+path+')',E);
68             throw(E);
69         }
70     },
71
72     'close' : function() {
73         try {
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; }
79
80         } catch(E) {
81             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.close(): ' + E);
82             throw(E);
83         }
84     },
85
86     'append_object' : function(obj) {
87         try {
88             this.write_object('append',obj);
89         } catch(E) {
90             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.append_object(): ' + E);
91             throw(E);
92         }
93     },
94
95     'set_object' : function(obj) {
96         try {
97             this.write_object('truncate',obj);
98             this.close();
99         } catch(E) {
100             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.set_object(): ' + E);
101             throw(E);
102         }
103     },
104
105     'write_object' : function(write_type,obj) {
106         try {
107             if (!this._file) throw('Must .get() a file first.');
108             if (!obj) throw('Must specify an object.');
109
110             var obj_json; 
111             try { obj_json = js2JSON( obj ) + '\n'; } catch(E) { throw('Could not JSONify the object: ' + E); }
112
113             this.write_content(write_type,obj_json);
114
115         } catch(E) {
116             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.write_object(): ' + E);
117             throw(E);
118         }
119     },
120
121     'write_content' : function(write_type,content) {
122         try {
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 );
126         } catch(E) {
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');
130             throw(E);
131         }
132     },
133
134     'get_object' : function() {
135         try {
136             var data = this.get_content();
137             var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
138             return obj;
139         } catch(E) {
140             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_object(): ' + E);
141             throw(E);
142         }
143     },
144
145     'get_content' : function() {
146         try {
147             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
148
149             if (!this._file) throw('Must .get() a file first.');
150             if (!this._file.exists()) throw('File does not exist.');
151             
152             if (!this._input_stream) this._create_input_stream();
153             var data = this._input_stream.read(-1);
154             //var data = {}; this._istream.readLine(data);
155             return data;
156         } catch(E) {
157             this.error.sdump('D_ERROR',this._file.path + '\nutil.file.get_content(): ' + E);
158             throw(E);
159         }
160     },
161
162     '_create_input_stream' : function() {
163         try {
164             //dump('_create_input_stream()\n');
165             
166             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
167
168             if (!this._file) throw('Must .get() a file first.');
169             if (!this._file.exists()) throw('File does not exist.');
170
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);
174             /*
175             this._f.QueryInterface(Components.interfaces.nsILineInputStream);
176             this._istream = this._f;
177             */
178             this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
179                 .createInstance(Components.interfaces.nsIScriptableInputStream);
180             if (this._f) {
181                 this._input_stream.init(this._f);
182             } else {
183                 throw('Could not instantiate input stream.');
184             }
185             return this._input_stream;
186
187         } catch(E) {
188             this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_input_stream(): ' + E);
189             throw(E);
190         }
191     },
192
193     '_create_output_stream' : function(param) {
194         try {
195             //dump('_create_output_stream('+param+') for '+this._file.path+'\n');
196             
197             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead");
198
199             if (!this._file) throw('Must .get() a file first.');
200
201             if (! this._file.exists()) {
202                 if (param == 'truncate+exec') {
203                     this._file.create( 0, 0777 );
204                 } else {
205                     this._file.create( 0, PERMS_FILE );
206                 }
207             }
208             this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
209                 .createInstance(Components.interfaces.nsIFileOutputStream);
210             switch(param){
211                 case 'append' :
212                     this._output_stream.init(this._file, MODE_WRONLY | MODE_APPEND, PERMS_FILE, 0);
213                 break;
214                 case 'truncate+exec' :
215                     this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
216                 break;
217                 case 'truncate' :
218                 default:
219                     this._output_stream.init(this._file, MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE, PERMS_FILE, 0);
220                 break;
221             }
222
223             return this._output_stream;
224
225         } catch(E) {
226             this.error.sdump('D_ERROR',this._file.path + '\nutil.file._create_output_stream(): ' + E);
227             throw(E);
228         }
229     },
230
231     'pick_file' : function(params) {
232         try {
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 );
238             fp.init( 
239                 window, 
240                 typeof params.title == 'undefined' ? params.mode : params.title,
241                 params.mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
242             );
243             if (params.defaultFileName) {
244                 fp.defaultString = params.defaultFileName;
245             }
246             fp.appendFilters( nsIFilePicker.filterAll );
247             var fp_result = fp.show();
248             if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) {
249                 return fp.file;
250             } else {
251                 return null;
252             }
253         } catch(E) {
254             this.error.standard_unexpected_error_alert('error picking file',E);
255         }
256     },
257
258     'export_file' : function(params) {
259         try {
260             var obj = this;
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 );
266             if (f) {
267                 obj._file = f;
268                 var temp = params.data;
269                 if (typeof params.not_json == 'undefined') {
270                     temp = js2JSON( temp );
271                 }
272                 obj.write_content( 'truncate', temp );
273                 obj.close();
274                 alert('Exported ' + f.leafName);
275                 return obj._file;
276             } else {
277                 alert('File not chosen for export.');
278                 return null;
279             }
280
281         } catch(E) {
282             this.error.standard_unexpected_error_alert('Error exporting file',E);
283                 return null;
284         }
285     },
286
287     'import_file' : function(params) {
288         try {
289             var obj = this;
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()) {
295                 obj._file = f;
296                 var temp = obj.get_content();
297                 obj.close();
298                 if (typeof params.not_json == 'undefined') {
299                     temp = JSON2js( obj.get_content() );
300                 }
301                 return temp;
302             } else {
303                 alert('File not chosen for import.');
304                 return null;
305             }
306         } catch(E) {
307             this.error.standard_unexpected_error_alert('Error importing file',E);
308             return null;
309         }
310     }
311
312 }
313
314 dump('exiting util/file.js\n');