]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/main/main.js
Fix so that replacing a file with the save file dialog works. Should probably move...
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / main / main.js
1 dump('entering main/main.js\n');
2
3 function grant_perms(url) {
4         var perms = "UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead";
5         dump('Granting ' + perms + ' to ' + url + '\n');
6         var pref = Components.classes["@mozilla.org/preferences-service;1"]
7                 .getService(Components.interfaces.nsIPrefBranch);
8         if (pref) {
9                 pref.setCharPref("capability.principal.codebase.p0.granted", perms);
10                 pref.setCharPref("capability.principal.codebase.p0.id", url);
11                 pref.setBoolPref("dom.disable_open_during_load",false);
12                 pref.setBoolPref("browser.popups.showPopupBlocker",false);
13         }
14
15 }
16
17 function clear_the_cache() {
18         try {
19                 var cacheClass          = Components.classes["@mozilla.org/network/cache-service;1"];
20                 var cacheService        = cacheClass.getService(Components.interfaces.nsICacheService);
21                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
22                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
23         } catch(E) {
24                 dump(E+'\n');alert(E);
25         }
26 }
27
28 function pick_file(mode) {
29         var nsIFilePicker = Components.interfaces.nsIFilePicker;
30         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( nsIFilePicker );
31         fp.init( 
32                 window, 
33                 mode == 'open' ? "Import Transaction File" : "Save Transaction File As", 
34                 mode == 'open' ? nsIFilePicker.modeOpen : nsIFilePicker.modeSave
35         );
36         fp.appendFilters( nsIFilePicker.filterAll );
37         var fp_result = fp.show();
38         if ( ( fp_result == nsIFilePicker.returnOK || fp_result == nsIFilePicker.returnReplace ) && fp.file ) {
39                 return fp.file;
40         } else {
41                 return null;
42         }
43 }
44
45 function main_init() {
46         dump('entering main_init()\n');
47         try {
48                 clear_the_cache();
49
50                 if (typeof JSAN == 'undefined') {
51                         throw(
52                                 "The JSAN library object is missing."
53                         );
54                 }
55                 /////////////////////////////////////////////////////////////////////////////
56
57                 JSAN.errorLevel = "die"; // none, warn, or die
58                 JSAN.addRepository('..');
59
60                 //JSAN.use('test.test'); test.test.hello_world();
61
62                 var mw = self;
63                 G =  {};
64
65                 JSAN.use('util.error');
66                 G.error = new util.error();
67                 G.error.sdump('D_ERROR','Testing');
68
69                 JSAN.use('util.window');
70                 G.window = new util.window();
71
72                 JSAN.use('auth.controller');
73                 G.auth = new auth.controller( { 'window' : mw } );
74
75                 JSAN.use('OpenILS.data');
76                 G.data = new OpenILS.data()
77                 G.data.on_error = G.auth.logoff;
78                 G.data.entities = entities;
79                 G.data.stash('entities');
80
81                 JSAN.use('util.file');
82                 G.file = new util.file();
83                 try {
84                         G.file.get('ws_info');
85                         G.ws_info = G.file.get_object(); G.file.close();
86                 } catch(E) {
87                         G.ws_info = {};
88                 }
89                 G.data.ws_info = G.ws_info; G.data.stash('ws_info');
90
91                 G.auth.on_login = function() {
92
93                         var url = G.auth.controller.view.server_prompt.value || urls.remote;
94
95                         G.data.server_unadorned = url; G.data.stash('server_unadorned'); G.data.stash_retrieve();
96
97                         if (! url.match( '^http://' ) ) url = 'http://' + url;
98
99                         G.data.server = url; G.data.stash('server'); 
100                         G.data.session = { 'key' : G.auth.session.key, 'auth' : G.auth.session.authtime }; G.data.stash('session');
101                         G.data.stash_retrieve();
102
103                         grant_perms(url);
104
105                         var xulG = {
106                                 'auth' : G.auth,
107                                 'url' : url,
108                                 'window' : G.window,
109                         }
110
111                         if (G.data.ws_info && G.data.ws_info[G.auth.controller.view.server_prompt.value]) {
112                                 JSAN.use('util.widgets');
113                                 var deck = document.getElementById('progress_space');
114                                 util.widgets.remove_children( deck );
115                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
116                                 iframe.setAttribute( 'src', url + '/xul/server/main/data.xul' );
117                                 iframe.contentWindow.xulG = xulG;
118                         } else {
119                                 xulG.file = G.file;
120                                 var deck = G.auth.controller.view.ws_deck;
121                                 JSAN.use('util.widgets'); util.widgets.remove_children('ws_deck');
122                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
123                                 iframe.setAttribute( 'src', url + '/xul/server/main/ws_info.xul' );
124                                 iframe.contentWindow.xulG = xulG;
125                                 deck.selectedIndex = deck.childNodes.length - 1;
126                         }
127                 }
128
129                 G.auth.on_standalone = function() {
130                         try {
131                                 G.window.open(urls.XUL_STANDALONE,'Offline','chrome,resizable');
132                         } catch(E) {
133                                 alert(E);
134                         }
135                 }
136
137                 G.auth.on_standalone_export = function() {
138                         try {
139                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
140                                 if (file._file.exists()) {
141                                         var f = pick_file('save');
142                                         if (f) {
143                                                 if (f.exists()) {
144                                                         var r = G.error.yns_alert(
145                                                                 'Would you like to overwrite the existing file ' + f.leafName + '?',
146                                                                 'Transaction Export Warning',
147                                                                 'Yes',
148                                                                 'No',
149                                                                 null,
150                                                                 'Check here to confirm this message'
151                                                         );
152                                                         if (r != 0) { file.close(); return; }
153                                                 }
154                                                 var e_file = new util.file(''); e_file._file = f;
155                                                 e_file.write_content( 'truncate', file.get_content() );
156                                                 e_file.close();
157                                                 var r = G.error.yns_alert(
158                                                         'Your transactions have been successfully exported to file ' + f.leafName + '.\n\nWe strongly recommend that you now purge the transactions from this staff client.  Would you like for us to do this?',
159                                                         'Transaction Export Successful',
160                                                         'Yes',
161                                                         'No',
162                                                         null,
163                                                         'Check here to confirm this message'
164                                                 );
165                                                 if (r == 0) {
166                                                         var count = 0;
167                                                         var filename = 'pending_xacts_exported_' + new Date().getTime();
168                                                         var t_file = new util.file(filename);
169                                                         while (t_file._file.exists()) {
170                                                                 filename = 'pending_xacts_' + new Date().getTime() + '.exported';
171                                                                 t_file = new util.file(filename);
172                                                                 if (count++>100) throw('Error purging transactions:  Taking too long to find a unique filename for archival.');
173                                                         }
174                                                         file._file.moveTo(null,filename);
175                                                 } else {
176                                                         alert('Please note that you now have two sets of identical transactions.  Unless the set you just exported is soley for archival purposes, we run the risk of duplicate transactions being processed on the server.');
177                                                 }
178                                         } else {
179                                                 alert('No filename chosen.  Or a bug where you tried to overwrite an existing file.');
180                                         }
181                                 } else {
182                                         alert('There are no outstanding transactions to export.');
183                                 }
184                                 file.close();
185                         } catch(E) {
186                                 alert(E);
187                         }
188                 }
189
190                 G.auth.on_standalone_import = function() {
191                         try {
192                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
193                                 if (file._file.exists()) {
194                                         alert('There are already outstanding transactions on this staff client.  Upload these first.');
195                                 } else {
196                                         var f = pick_file('open');
197                                         if (f && f.exists()) {
198                                                 var i_file = new util.file(''); i_file._file = f;
199                                                 file.write_content( 'truncate', i_file.get_content() );
200                                                 i_file.close();
201                                                 var r = G.error.yns_alert(
202                                                         'Your transactions have been successfully migrated to this staff client.\n\nWe recommend that you delete the external copy.  Would you like for us to delete ' + f.leafName + '?',
203                                                         'Transaction Import Successful',
204                                                         'Yes',
205                                                         'No',
206                                                         null,
207                                                         'Check here to confirm this message'
208                                                 );
209                                                 if (r == 0) {
210                                                         f.remove(false);
211                                                 }
212                                         }
213                                 }
214                                 file.close();
215                         } catch(E) {
216                                 alert(E);
217                         }
218                 }
219
220                 G.auth.on_debug = function(action) {
221                         switch(action) {
222                                 case 'js_console' :
223                                         G.window.open(urls.XUL_DEBUG_CONSOLE,'testconsole','chrome,resizable');
224                                 break;
225                                 case 'clear_cache' :
226                                         clear_the_cache();
227                                         alert('cache cleared');
228                                 break;
229                                 default:
230                                         alert('debug the debug :D');
231                                 break;
232                         }
233                 }
234
235                 G.auth.init();
236                 // XML_HTTP_SERVER will get reset to G.auth.controller.view.server_prompt.value
237
238                 /////////////////////////////////////////////////////////////////////////////
239
240                 var version = '/xul/server/'.split(/\//)[2];
241                 if (version == 'server') {
242                         version = 'versionless debug build';
243                         document.getElementById('debug_gb').hidden = false;
244                 }
245                 //var x = document.getElementById('version_label');
246                 //x.setAttribute('value','Build ID: ' + version);
247                 var x = document.getElementById('about_btn');
248                 x.setAttribute('label','About this client...');
249                 x.addEventListener(
250                         'click',
251                         function() {
252                                 try { 
253                                         G.window.open('about.html','about','chrome,resizable,width=800,height=600');
254                                 } catch(E) { alert(E); }
255                         }, 
256                         false
257                 );
258
259         } catch(E) {
260                 var error = "!! This software has encountered an error.  Please tell your friendly " +
261                         "system administrator or software developer the following:\n" + E + '\n';
262                 try { G.error.sdump('D_ERROR',error); } catch(E) { dump(error); }
263                 alert(error);
264         }
265         dump('exiting main_init()\n');
266 }
267
268 dump('exiting main/main.js\n');