]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/main/main.js
Bring more i18n love to chrome/main
[working/Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / main / main.js
1 dump('entering main/main.js\n');
2 // vim:noet:sw=4:ts=4:
3
4 function grant_perms(url) {
5         var perms = "UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead";
6         dump('Granting ' + perms + ' to ' + url + '\n');
7         var pref = Components.classes["@mozilla.org/preferences-service;1"]
8                 .getService(Components.interfaces.nsIPrefBranch);
9         if (pref) {
10                 pref.setCharPref("capability.principal.codebase.p0.granted", perms);
11                 pref.setCharPref("capability.principal.codebase.p0.id", url);
12                 pref.setBoolPref("dom.disable_open_during_load",false);
13                 pref.setBoolPref("browser.popups.showPopupBlocker",false);
14         }
15
16 }
17
18 function clear_the_cache() {
19         try {
20                 var cacheClass          = Components.classes["@mozilla.org/network/cache-service;1"];
21                 var cacheService        = cacheClass.getService(Components.interfaces.nsICacheService);
22                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
23                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
24         } catch(E) {
25                 dump(E+'\n');alert(E);
26         }
27 }
28
29 function main_init() {
30         dump('entering main_init()\n');
31         try {
32                 clear_the_cache();
33
34                 if (typeof JSAN == 'undefined') {
35                         throw(
36                                 "The JSAN library object is missing."
37                         );
38                 }
39                 /////////////////////////////////////////////////////////////////////////////
40
41                 JSAN.errorLevel = "die"; // none, warn, or die
42                 JSAN.addRepository('..');
43
44                 //JSAN.use('test.test'); test.test.hello_world();
45
46                 var mw = self;
47                 G =  {};
48
49                 JSAN.use('util.error');
50                 G.error = new util.error();
51                 G.error.sdump('D_ERROR','Testing');
52
53                 JSAN.use('util.window');
54                 G.window = new util.window();
55
56                 JSAN.use('auth.controller');
57                 G.auth = new auth.controller( { 'window' : mw } );
58
59                 JSAN.use('OpenILS.data');
60                 G.data = new OpenILS.data()
61                 G.data.on_error = G.auth.logoff;
62
63                 JSAN.use('util.file');
64                 G.file = new util.file();
65                 try {
66                         G.file.get('ws_info');
67                         G.ws_info = G.file.get_object(); G.file.close();
68                 } catch(E) {
69                         G.ws_info = {};
70                 }
71                 G.data.ws_info = G.ws_info; G.data.stash('ws_info');
72
73                 G.auth.on_login = function() {
74
75                         var url = G.auth.controller.view.server_prompt.value || urls.remote;
76
77                         G.data.server_unadorned = url; G.data.stash('server_unadorned'); G.data.stash_retrieve();
78
79                         if (! url.match( '^http://' ) ) url = 'http://' + url;
80
81                         G.data.server = url; G.data.stash('server'); 
82                         G.data.session = { 'key' : G.auth.session.key, 'auth' : G.auth.session.authtime }; G.data.stash('session');
83                         G.data.stash_retrieve();
84
85                         grant_perms(url);
86
87                         var xulG = {
88                                 'auth' : G.auth,
89                                 'url' : url,
90                                 'window' : G.window,
91                         }
92
93                         if (G.data.ws_info && G.data.ws_info[G.auth.controller.view.server_prompt.value]) {
94                                 JSAN.use('util.widgets');
95                                 var deck = document.getElementById('progress_space');
96                                 util.widgets.remove_children( deck );
97                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
98                                 iframe.setAttribute( 'src', url + '/xul/server/main/data.xul' );
99                                 iframe.contentWindow.xulG = xulG;
100                         } else {
101                                 xulG.file = G.file;
102                                 var deck = G.auth.controller.view.ws_deck;
103                                 JSAN.use('util.widgets'); util.widgets.remove_children('ws_deck');
104                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
105                                 iframe.setAttribute( 'src', url + '/xul/server/main/ws_info.xul' );
106                                 iframe.contentWindow.xulG = xulG;
107                                 deck.selectedIndex = deck.childNodes.length - 1;
108                         }
109                 }
110
111                 G.auth.on_standalone = function() {
112                         try {
113                                 G.window.open(urls.XUL_STANDALONE,'Offline','chrome,resizable');
114                         } catch(E) {
115                                 alert(E);
116                         }
117                 }
118
119                 G.auth.on_standalone_export = function() {
120                         try {
121                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
122                                 if (file._file.exists()) {
123                     var file2 = new util.file('');
124                                         var f = file2.pick_file( { 'mode' : 'save', 'title' : 'Save Transaction File As' } );
125                                         if (f) {
126                                                 if (f.exists()) {
127                                                         var r = G.error.yns_alert(
128                                                                 'Would you like to overwrite the existing file ' + f.leafName + '?',
129                                                                 'Transaction Export Warning',
130                                                                 'Yes',
131                                                                 'No',
132                                                                 null,
133                                                                 'Check here to confirm this message'
134                                                         );
135                                                         if (r != 0) { file.close(); return; }
136                                                 }
137                                                 var e_file = new util.file(''); e_file._file = f;
138                                                 e_file.write_content( 'truncate', file.get_content() );
139                                                 e_file.close();
140                                                 var r = G.error.yns_alert(
141                                                         '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?',
142                                                         'Transaction Export Successful',
143                                                         'Yes',
144                                                         'No',
145                                                         null,
146                                                         'Check here to confirm this message'
147                                                 );
148                                                 if (r == 0) {
149                                                         var count = 0;
150                                                         var filename = 'pending_xacts_exported_' + new Date().getTime();
151                                                         var t_file = new util.file(filename);
152                                                         while (t_file._file.exists()) {
153                                                                 filename = 'pending_xacts_' + new Date().getTime() + '.exported';
154                                                                 t_file = new util.file(filename);
155                                                                 if (count++>100) throw('Error purging transactions:  Taking too long to find a unique filename for archival.');
156                                                         }
157                                                         file._file.moveTo(null,filename);
158                                                 } else {
159                                                         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.');
160                                                 }
161                                         } else {
162                                                 alert('No filename chosen.  Or a bug where you tried to overwrite an existing file.');
163                                         }
164                                 } else {
165                                         alert('There are no outstanding transactions to export.');
166                                 }
167                                 file.close();
168                         } catch(E) {
169                                 alert(E);
170                         }
171                 }
172
173                 G.auth.on_standalone_import = function() {
174                         try {
175                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
176                                 if (file._file.exists()) {
177                                         alert('There are already outstanding transactions on this staff client.  Upload these first.');
178                                 } else {
179                     var file2 = new util.file('');
180                                         var f = file2.pick_file( { 'mode' : 'open', 'title' : 'Import Transaction File'} );
181                                         if (f && f.exists()) {
182                                                 var i_file = new util.file(''); i_file._file = f;
183                                                 file.write_content( 'truncate', i_file.get_content() );
184                                                 i_file.close();
185                                                 var r = G.error.yns_alert(
186                                                         '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 + '?',
187                                                         'Transaction Import Successful',
188                                                         'Yes',
189                                                         'No',
190                                                         null,
191                                                         'Check here to confirm this message'
192                                                 );
193                                                 if (r == 0) {
194                                                         f.remove(false);
195                                                 }
196                                         }
197                                 }
198                                 file.close();
199                         } catch(E) {
200                                 alert(E);
201                         }
202                 }
203
204                 G.auth.on_debug = function(action) {
205                         switch(action) {
206                                 case 'js_console' :
207                                         G.window.open(urls.XUL_DEBUG_CONSOLE,'testconsole','chrome,resizable');
208                                 break;
209                                 case 'clear_cache' :
210                                         clear_the_cache();
211                                         alert('cache cleared');
212                                 break;
213                                 default:
214                                         alert('debug the debug :D');
215                                 break;
216                         }
217                 }
218
219                 G.auth.init();
220                 // XML_HTTP_SERVER will get reset to G.auth.controller.view.server_prompt.value
221
222                 /////////////////////////////////////////////////////////////////////////////
223
224                 var version = '/xul/server/'.split(/\//)[2];
225                 if (version == 'server') {
226                         version = 'versionless debug build';
227                         document.getElementById('debug_gb').hidden = false;
228                 }
229                 //var x = document.getElementById('version_label');
230                 //x.setAttribute('value','Build ID: ' + version);
231                 var x = document.getElementById('about_btn');
232                 x.setAttribute('label','About this client...');
233                 x.addEventListener(
234                         'command',
235                         function() {
236                                 try { 
237                                         G.window.open('about.html','about','chrome,resizable,width=800,height=600');
238                                 } catch(E) { alert(E); }
239                         }, 
240                         false
241                 );
242                 if ( found_ws_info_in_Achrome() ) {
243                         //var hbox = x.parentNode; var b = document.createElement('button'); 
244                         //b.setAttribute('label','Migrate legacy settings'); hbox.appendChild(b);
245                         //b.addEventListener(
246                         //      'command',
247                         //      function() {
248                         //              try {
249                         //                      handle_migration();
250                         //              } catch(E) { alert(E); }
251                         //      },
252                         //      false
253                         //);
254                         if (window.confirm('This version of the staff client stores local settings in a different location than your previous installation.  Should we attempt to migrate these settings?')) {
255                                 setTimeout( function() { handle_migration(); }, 0 );
256                         }
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 function found_ws_info_in_Achrome() {
269         JSAN.use('util.file');
270         var f = new util.file();
271         var f_in_chrome = f.get('ws_info','chrome');
272         var path = f_in_chrome.exists() ? f_in_chrome.path : false;
273         f.close();
274         return path;
275 }
276
277 function found_ws_info_in_Uchrome() {
278         JSAN.use('util.file');
279         var f = new util.file();
280         var f_in_uchrome = f.get('ws_info','uchrome');
281         var path = f_in_uchrome.exists() ? f_in_uchrome.path : false;
282         f.close();
283         return path;
284 }
285
286 function handle_migration() {
287         if ( found_ws_info_in_Uchrome() ) {
288                 alert('WARNING: Unable to migrate legacy settings.  The settings and configuration files appear to exist in multiple locations.\n'
289                         + 'To resolve manually, please consider:\n\t' + found_ws_info_in_Uchrome() + '\n'
290                         + 'which is in the directory where we want to store settings for the current OS user, and:\n\t'
291                         + found_ws_info_in_Achrome() + '\nwhich is where we used to store such information.\n'
292                 );
293         } else {
294                 var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties );
295                 var f_new = dirService.get( "UChrm", Components.interfaces.nsIFile );
296                 var f_old = dirService.get( "AChrom", Components.interfaces.nsIFile );
297                 f_old.append(myPackageDir); f_old.append("content"); f_old.append("conf"); 
298                 if (window.confirm("Move the settings and configuration files from\n" + f_old.path + "\nto\n" + f_new.path + "?")) {
299                         var files = f_old.directoryEntries;
300                         while (files.hasMoreElements()) {
301                                 var file = files.getNext();
302                                 var file2 = file.QueryInterface( Components.interfaces.nsILocalFile );
303                                 try {
304                                         file2.moveTo( f_new, '' );
305                                 } catch(E) {
306                                         alert('Error trying to move ' + file2.path + ' to directory ' + f_new.path + '\n');
307                                 }
308                         }
309                         location.href = location.href;
310                 }
311         }
312 }
313
314 dump('exiting main/main.js\n');