]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/main/main.js
instead of the old progress meter, show some textual progress information
[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         if ( fp.show( ) == nsIFilePicker.returnOK && fp.file ) {
38                 return fp.file;
39         } else {
40                 return null;
41         }
42 }
43
44 function main_init() {
45         dump('entering main_init()\n');
46         try {
47                 clear_the_cache();
48
49                 if (typeof JSAN == 'undefined') {
50                         throw(
51                                 "The JSAN library object is missing."
52                         );
53                 }
54                 /////////////////////////////////////////////////////////////////////////////
55
56                 JSAN.errorLevel = "die"; // none, warn, or die
57                 JSAN.addRepository('..');
58
59                 //JSAN.use('test.test'); test.test.hello_world();
60
61                 var mw = self;
62                 G =  {};
63
64                 JSAN.use('util.error');
65                 G.error = new util.error();
66                 G.error.sdump('D_ERROR','Testing');
67
68                 JSAN.use('util.window');
69                 G.window = new util.window();
70
71                 JSAN.use('auth.controller');
72                 G.auth = new auth.controller( { 'window' : mw } );
73
74                 JSAN.use('OpenILS.data');
75                 G.data = new OpenILS.data()
76                 G.data.on_error = G.auth.logoff;
77                 G.data.entities = entities;
78                 G.data.stash('entities');
79
80                 JSAN.use('util.file');
81                 G.file = new util.file();
82                 try {
83                         G.file.get('ws_info');
84                         G.ws_info = G.file.get_object(); G.file.close();
85                 } catch(E) {
86                         G.ws_info = {};
87                 }
88                 G.data.ws_info = G.ws_info; G.data.stash('ws_info');
89
90                 G.auth.on_login = function() {
91
92                         var url = G.auth.controller.view.server_prompt.value || urls.remote;
93
94                         G.data.server_unadorned = url; G.data.stash('server_unadorned'); G.data.stash_retrieve();
95
96                         if (! url.match( '^http://' ) ) url = 'http://' + url;
97
98                         G.data.server = url; G.data.stash('server'); 
99                         G.data.session = { 'key' : G.auth.session.key, 'auth' : G.auth.session.authtime }; G.data.stash('session');
100                         G.data.stash_retrieve();
101
102                         grant_perms(url);
103
104                         var xulG = {
105                                 'auth' : G.auth,
106                                 'url' : url,
107                                 'window' : G.window,
108                         }
109
110                         if (G.data.ws_info && G.data.ws_info[G.auth.controller.view.server_prompt.value]) {
111                                 JSAN.use('util.widgets');
112                                 var deck = document.getElementById('progress_space');
113                                 util.widgets.remove_children( deck );
114                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
115                                 iframe.setAttribute( 'src', url + '/xul/server/main/data.xul' );
116                                 iframe.contentWindow.xulG = xulG;
117                         } else {
118                                 xulG.file = G.file;
119                                 var deck = G.auth.controller.view.ws_deck;
120                                 JSAN.use('util.widgets'); util.widgets.remove_children('ws_deck');
121                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
122                                 iframe.setAttribute( 'src', url + '/xul/server/main/ws_info.xul' );
123                                 iframe.contentWindow.xulG = xulG;
124                                 deck.selectedIndex = deck.childNodes.length - 1;
125                         }
126                 }
127
128                 G.auth.on_standalone = function() {
129                         try {
130                                 G.window.open(urls.XUL_STANDALONE,'Offline','chrome,resizable');
131                         } catch(E) {
132                                 alert(E);
133                         }
134                 }
135
136                 G.auth.on_standalone_export = function() {
137                         try {
138                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
139                                 if (file._file.exists()) {
140                                         var f = pick_file('save');
141                                         if (f) {
142                                                 if (f.exists()) {
143                                                         var r = G.error.yns_alert(
144                                                                 'Would you like to overwrite the existing file ' + f.leafName + '?',
145                                                                 'Transaction Export Warning',
146                                                                 'Yes',
147                                                                 'No',
148                                                                 null,
149                                                                 'Check here to confirm this message'
150                                                         );
151                                                         if (r != 0) { file.close(); return; }
152                                                 }
153                                                 var e_file = new util.file(''); e_file._file = f;
154                                                 e_file.write_content( 'truncate', file.get_content() );
155                                                 e_file.close();
156                                                 var r = G.error.yns_alert(
157                                                         '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?',
158                                                         'Transaction Export Successful',
159                                                         'Yes',
160                                                         'No',
161                                                         null,
162                                                         'Check here to confirm this message'
163                                                 );
164                                                 if (r == 0) {
165                                                         var count = 0;
166                                                         var filename = 'pending_xacts_exported_' + new Date().getTime();
167                                                         var t_file = new util.file(filename);
168                                                         while (t_file._file.exists()) {
169                                                                 filename = 'pending_xacts_' + new Date().getTime() + '.exported';
170                                                                 t_file = new util.file(filename);
171                                                                 if (count++>100) throw('Error purging transactions:  Taking too long to find a unique filename for archival.');
172                                                         }
173                                                         file._file.moveTo(null,filename);
174                                                 } else {
175                                                         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.');
176                                                 }
177                                         } else {
178                                                 alert('No filename chosen.  Or a bug where you tried to overwrite an existing file.');
179                                         }
180                                 } else {
181                                         alert('There are no outstanding transactions to export.');
182                                 }
183                                 file.close();
184                         } catch(E) {
185                                 alert(E);
186                         }
187                 }
188
189                 G.auth.on_standalone_import = function() {
190                         try {
191                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
192                                 if (file._file.exists()) {
193                                         alert('There are already outstanding transactions on this staff client.  Upload these first.');
194                                 } else {
195                                         var f = pick_file('open');
196                                         if (f && f.exists()) {
197                                                 var i_file = new util.file(''); i_file._file = f;
198                                                 file.write_content( 'truncate', i_file.get_content() );
199                                                 i_file.close();
200                                                 var r = G.error.yns_alert(
201                                                         '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 + '?',
202                                                         'Transaction Import Successful',
203                                                         'Yes',
204                                                         'No',
205                                                         null,
206                                                         'Check here to confirm this message'
207                                                 );
208                                                 if (r == 0) {
209                                                         f.remove(false);
210                                                 }
211                                         }
212                                 }
213                                 file.close();
214                         } catch(E) {
215                                 alert(E);
216                         }
217                 }
218
219                 G.auth.on_debug = function(action) {
220                         switch(action) {
221                                 case 'js_console' :
222                                         G.window.open(urls.XUL_DEBUG_CONSOLE,'testconsole','chrome,resizable');
223                                 break;
224                                 case 'clear_cache' :
225                                         clear_the_cache();
226                                         alert('cache cleared');
227                                 break;
228                                 default:
229                                         alert('debug the debug :D');
230                                 break;
231                         }
232                 }
233
234                 G.auth.init();
235                 // XML_HTTP_SERVER will get reset to G.auth.controller.view.server_prompt.value
236
237                 /////////////////////////////////////////////////////////////////////////////
238
239                 var version = '/xul/server/'.split(/\//)[2];
240                 if (version == 'server') {
241                         version = 'versionless debug build';
242                         document.getElementById('debug_gb').hidden = false;
243                 }
244                 //var x = document.getElementById('version_label');
245                 //x.setAttribute('value','Build ID: ' + version);
246                 var x = document.getElementById('about_btn');
247                 x.setAttribute('label','About this client...');
248                 x.addEventListener(
249                         'click',
250                         function() {
251                                 try { 
252                                         G.window.open('about.html','about','chrome,resizable,width=800,height=600');
253                                 } catch(E) { alert(E); }
254                         }, 
255                         false
256                 );
257
258         } catch(E) {
259                 var error = "!! This software has encountered an error.  Please tell your friendly " +
260                         "system administrator or software developer the following:\n" + E + '\n';
261                 try { G.error.sdump('D_ERROR',error); } catch(E) { dump(error); }
262                 alert(error);
263         }
264         dump('exiting main_init()\n');
265 }
266
267 dump('exiting main/main.js\n');