]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/file.js
another great renaming, and an un-naming
[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 ) {
26                 try {
27                         if (!fname) { fname = this.name; } else { this.name = fname; }
28                         if (!fname) throw('Must specify a filename.');
29
30                         this._file = this.dirService.get( "AChrom",  Components.interfaces.nsIFile );
31                         this._file.append(myPackageDir); 
32                         this._file.append("content"); 
33                         this._file.append("conf"); 
34                         this._file.append(fname);
35         
36                         this.error.sdump('D_FILE',this._file.path);
37
38                         return this._file;
39
40                 } catch(E) {
41                         this.error.sdump('D_ERROR','util.file.get(): ' + E);
42                         throw(E);
43                 }
44         },
45
46         'close' : function() {
47                 try {
48                         if (!this._file) throw('Must .get() a file first.');
49                         if (this._input_stream) { this._input_stream.close(); this._input_stream = null; }
50                         if (this._output_stream) { this._output_stream.close(); this._output_stream = null; }
51
52                 } catch(E) {
53                         this.error.sdump('D_ERROR','util.file.close(): ' + E);
54                         throw(E);
55                 }
56         },
57
58         'set_object' : function(obj) {
59                 try {
60                         if (!this._file) throw('Must .get() a file first.');
61                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
62                         if (!obj) throw('Must specify an object.');
63
64                         var obj_json; 
65                         try { obj_json = js2JSON( obj ); } catch(E) { throw('Could not JSONify the object: ' + E); }
66
67                         this._create_output_stream();
68                         this._output_stream.write( obj_json, String( obj_json ).length );
69                         this.close();
70
71                 } catch(E) {
72                         this.error.sdump('D_ERROR','util.file.set_object(): ' + E);
73                         throw(E);
74                 }
75         },
76
77         'get_object' : function() {
78                 try {
79                         if (!this._file) throw('Must .get() a file first.');
80                         if (!this._file.exists()) throw('File does not exist.');
81                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
82                         
83                         this._create_input_stream();
84                         var data = this._input_stream.read(-1);
85                         this.close();
86         
87                         var obj; try { obj = JSON2js( data ); } catch(E) { throw('Could not js-ify the JSON: '+E); }
88         
89                         return obj;
90
91                 } catch(E) {
92                         this.error.sdump('D_ERROR','util.file.get_object(): ' + E);
93                         throw(E);
94                 }
95         },
96
97         '_create_input_stream' : function() {
98                 try {
99                         if (!this._file) throw('Must .get() a file first.');
100                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
101                         if (!this._file.exists()) throw('File does not exist.');
102         
103                         var f = Components.classes["@mozilla.org/network/file-input-stream;1"]
104                                 .createInstance(Components.interfaces.nsIFileInputStream);
105                         f.init(this._file, 0x01, 0, 0);
106                         this._input_stream = Components.classes["@mozilla.org/scriptableinputstream;1"]
107                                 .createInstance(Components.interfaces.nsIScriptableInputStream);
108                         if (f) {
109                                 this._input_stream.init(f);
110                                 return this._input_stream;
111                         } else {
112                                 throw('Could not instantiate input stream.');
113                         }
114
115                 } catch(E) {
116                         this.error.sdump('D_ERROR','util.file._create_input_stream(): ' + E);
117                         throw(E);
118                 }
119         },
120
121         '_create_output_stream' : function() {
122                 try {
123                         if (!this._file) throw('Must .get() a file first.');
124                         if (this._input_stream || this._output_stream) throw('Must .close() first.');
125
126                         if (! this._file.exists()) this._file.create( 0, 0640 );
127
128                         this._output_stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
129                                 .createInstance(Components.interfaces.nsIFileOutputStream);
130                         this._output_stream.init(this._file, 0x02 | 0x08 | 0x20, 0644, 0);
131
132                         return this._output_stream;
133
134                 } catch(E) {
135                         this.error.sdump('D_ERROR','util.file._create_output_stream(): ' + E);
136                         throw(E);
137                 }
138         }
139
140 }
141
142 dump('exiting util/file.js\n');