]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/main/main.js
I18N version of a patch from Bill Ott for a New Window button on the login window...
[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 var offlineStrings;
5
6 function grant_perms(url) {
7         var perms = "UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead";
8         dump('Granting ' + perms + ' to ' + url + '\n');
9         var pref = Components.classes["@mozilla.org/preferences-service;1"]
10                 .getService(Components.interfaces.nsIPrefBranch);
11         if (pref) {
12                 pref.setCharPref("capability.principal.codebase.p0.granted", perms);
13                 pref.setCharPref("capability.principal.codebase.p0.id", url);
14                 pref.setBoolPref("dom.disable_open_during_load",false);
15                 pref.setBoolPref("browser.popups.showPopupBlocker",false);
16         }
17
18 }
19
20 function clear_the_cache() {
21         try {
22                 var cacheClass          = Components.classes["@mozilla.org/network/cache-service;1"];
23                 var cacheService        = cacheClass.getService(Components.interfaces.nsICacheService);
24                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
25                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
26         } catch(E) {
27                 dump(E+'\n');alert(E);
28         }
29 }
30
31 function main_init() {
32         dump('entering main_init()\n');
33         try {
34                 clear_the_cache();
35
36                 // Now we can safely load the strings without the cache getting wiped
37                 offlineStrings = document.getElementById('offlineStrings');
38
39                 if (typeof JSAN == 'undefined') {
40                         throw(
41                                 offlineStrings.getString('common.jsan.missing')
42                         );
43                 }
44                 /////////////////////////////////////////////////////////////////////////////
45
46                 JSAN.errorLevel = "die"; // none, warn, or die
47                 JSAN.addRepository('..');
48
49                 //JSAN.use('test.test'); test.test.hello_world();
50
51                 var mw = self;
52                 G =  {};
53
54                 JSAN.use('util.error');
55                 G.error = new util.error();
56                 G.error.sdump('D_ERROR', offlineStrings.getString('main.testing'));
57
58                 JSAN.use('util.window');
59                 G.window = new util.window();
60
61                 JSAN.use('auth.controller');
62                 G.auth = new auth.controller( { 'window' : mw } );
63
64                 JSAN.use('OpenILS.data');
65                 G.data = new OpenILS.data();
66                 G.data.on_error = G.auth.logoff;
67
68                 JSAN.use('util.file');
69                 G.file = new util.file();
70                 try {
71                         G.file.get('ws_info');
72                         G.ws_info = G.file.get_object(); G.file.close();
73                 } catch(E) {
74                         G.ws_info = {};
75                 }
76                 G.data.ws_info = G.ws_info; G.data.stash('ws_info');
77
78                 G.auth.on_login = function() {
79
80                         var url = G.auth.controller.view.server_prompt.value || urls.remote;
81
82                         G.data.server_unadorned = url; G.data.stash('server_unadorned'); G.data.stash_retrieve();
83
84                         if (! url.match( '^http://' ) ) { url = 'http://' + url; }
85
86                         G.data.server = url; G.data.stash('server'); 
87                         G.data.session = { 'key' : G.auth.session.key, 'auth' : G.auth.session.authtime }; G.data.stash('session');
88                         G.data.stash_retrieve();
89
90                         grant_perms(url);
91
92                         var xulG = {
93                                 'auth' : G.auth,
94                                 'url' : url,
95                                 'window' : G.window
96                         };
97
98                         if (G.data.ws_info && G.data.ws_info[G.auth.controller.view.server_prompt.value]) {
99                                 JSAN.use('util.widgets');
100                                 var deck = document.getElementById('progress_space');
101                                 util.widgets.remove_children( deck );
102                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
103                                 iframe.setAttribute( 'src', url + '/xul/server/main/data.xul' );
104                                 iframe.contentWindow.xulG = xulG;
105                         } else {
106                                 xulG.file = G.file;
107                                 var deck = G.auth.controller.view.ws_deck;
108                                 JSAN.use('util.widgets'); util.widgets.remove_children('ws_deck');
109                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
110                                 iframe.setAttribute( 'src', url + '/xul/server/main/ws_info.xul' );
111                                 iframe.contentWindow.xulG = xulG;
112                                 deck.selectedIndex = deck.childNodes.length - 1;
113                         }
114                 };
115
116                 G.auth.on_standalone = function() {
117                         try {
118                                 G.window.open(urls.XUL_STANDALONE,'Offline','chrome,resizable');
119                         } catch(E) {
120                                 alert(E);
121                         }
122                 };
123
124                 G.auth.on_standalone_export = function() {
125                         try {
126                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
127                                 if (file._file.exists()) {
128                     var file2 = new util.file('');
129                                         var f = file2.pick_file( { 'mode' : 'save', 'title' : offlineStrings.getString('main.transaction_export.title') } );
130                                         if (f) {
131                                                 if (f.exists()) {
132                                                         var r = G.error.yns_alert(
133                                                                 offlineStrings.getFormattedString('main.transaction_export.prompt', [f.leafName]),
134                                                                 offlineStrings.getString('main.transaction_export.prompt.title'),
135                                                                 offlineStrings.getString('common.yes'),
136                                                                 offlineStrings.getString('common.no'),
137                                                                 null,
138                                                                 offlineStrings.getString('common.confirm')
139                                                         );
140                                                         if (r != 0) { file.close(); return; }
141                                                 }
142                                                 var e_file = new util.file(''); e_file._file = f;
143                                                 e_file.write_content( 'truncate', file.get_content() );
144                                                 e_file.close();
145                                                 var r = G.error.yns_alert(
146                                                         offlineStrings.getFormattedString('main.transaction_export.success.prompt', [f.leafName]),
147                                                         offlineStrings.getString('main.transaction_export.success.title'),
148                                                         offlineStrings.getString('common.yes'),
149                                                         offlineStrings.getString('common.no'),
150                                                         null,
151                                                         offlineStrings.getString('common.confirm')
152                                                 );
153                                                 if (r == 0) {
154                                                         var count = 0;
155                                                         var filename = 'pending_xacts_exported_' + new Date().getTime();
156                                                         var t_file = new util.file(filename);
157                                                         while (t_file._file.exists()) {
158                                                                 filename = 'pending_xacts_' + new Date().getTime() + '.exported';
159                                                                 t_file = new util.file(filename);
160                                                                 if (count++ > 100) {
161                                                                         throw(offlineStrings.getString('main.transaction_export.filename.error'));
162                                                                 }
163                                                         }
164                                                         file._file.moveTo(null,filename);
165                                                 } else {
166                                                         alert(offlineStrings.getString('main.transaction_export.duplicate.warning'));
167                                                 }
168                                         } else {
169                                                 alert(offlineStrings.getString('main.transaction_export.no_filename.error'));
170                                         }
171                                 } else {
172                                         alert(offlineStrings.getString('main.transaction_export.no_transactions.error'));
173                                 }
174                                 file.close();
175                         } catch(E) {
176                                 alert(E);
177                         }
178                 };
179
180                 G.auth.on_standalone_import = function() {
181                         try {
182                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
183                                 if (file._file.exists()) {
184                                         alert(offlineStrings.getString('main.transaction_import.outstanding.error'));
185                                 } else {
186                     var file2 = new util.file('');
187                                         var f = file2.pick_file( { 'mode' : 'open', 'title' : offlineStrings.getString('main.transaction_import.title')} );
188                                         if (f && f.exists()) {
189                                                 var i_file = new util.file(''); i_file._file = f;
190                                                 file.write_content( 'truncate', i_file.get_content() );
191                                                 i_file.close();
192                                                 var r = G.error.yns_alert(
193                                                         offlineStrings.getFormattedString('main.transaction_import.delete.prompt', [f.leafName]),
194                                                         offlineStrings.getString('main.transaction_import.success'),
195                                                         offlineStrings.getString('common.yes'),
196                                                         offlineStrings.getString('common.no'),
197                                                         null,
198                                                         offlineStrings.getString('common.confirm')
199                                                 );
200                                                 if (r == 0) {
201                                                         f.remove(false);
202                                                 }
203                                         }
204                                 }
205                                 file.close();
206                         } catch(E) {
207                                 alert(E);
208                         }
209                 };
210
211                 G.auth.on_debug = function(action) {
212                         switch(action) {
213                                 case 'js_console' :
214                                         G.window.open(urls.XUL_DEBUG_CONSOLE,'testconsole','chrome,resizable');
215                                 break;
216                                 case 'clear_cache' :
217                                         clear_the_cache();
218                                         alert(offlineStrings.getString('main.on_debug.clear_cache'));
219                                 break;
220                                 default:
221                                         alert(offlineStrings.getString('main.on_debug.debug'));
222                                 break;
223                         }
224                 };
225
226                 G.auth.init();
227                 // XML_HTTP_SERVER will get reset to G.auth.controller.view.server_prompt.value
228
229                 /////////////////////////////////////////////////////////////////////////////
230
231                 var version = '/xul/server/'.split(/\//)[2];
232                 if (version == 'server') {
233                         version = 'versionless debug build';
234                         document.getElementById('debug_gb').hidden = false;
235                 }
236                 //var x = document.getElementById('version_label');
237                 //x.setAttribute('value','Build ID: ' + version);
238                 var x = document.getElementById('about_btn');
239                 x.setAttribute('label', offlineStrings.getString('main.about_btn.label'));
240                 x.addEventListener(
241                         'command',
242                         function() {
243                                 try { 
244                                         G.window.open('about.html','about','chrome,resizable,width=800,height=600');
245                                 } catch(E) { alert(E); }
246                         }, 
247                         false
248                 );
249
250                 var y = document.getElementById('new_window_btn');
251                 y.setAttribute('label', offlineStrings.getString('main.new_window_btn.label'));
252                 y.addEventListener(
253                         'command',
254                         function() {
255                                 if (G.data.session) {
256                                         try {
257                                                 G.window.open('chrome://open_ils_staff_client/content/main/menu_frame.xul?server=' +
258                                                         G.data.server,'main','chrome,resizable' );
259
260                                         } catch(E) { alert(E); }
261                                 } else {
262                                         alert ( offlineStrings.getString('main.new_window_btn.login_first_warning') );
263                                 }
264                         },
265                         false
266                 );
267
268                 if ( found_ws_info_in_Achrome() ) {
269                         //var hbox = x.parentNode; var b = document.createElement('button'); 
270                         //b.setAttribute('label','Migrate legacy settings'); hbox.appendChild(b);
271                         //b.addEventListener(
272                         //      'command',
273                         //      function() {
274                         //              try {
275                         //                      handle_migration();
276                         //              } catch(E) { alert(E); }
277                         //      },
278                         //      false
279                         //);
280                         if (window.confirm(offlineStrings.getString('main.settings.migrate'))) {
281                                 setTimeout( function() { handle_migration(); }, 0 );
282                         }
283                 }
284
285         } catch(E) {
286                 var error = offlineStrings.getFormattedString('common.exception', [E, '']);
287                 try { G.error.sdump('D_ERROR',error); } catch(E) { dump(error); }
288                 alert(error);
289         }
290         dump('exiting main_init()\n');
291 }
292
293 function found_ws_info_in_Achrome() {
294         JSAN.use('util.file');
295         var f = new util.file();
296         var f_in_chrome = f.get('ws_info','chrome');
297         var path = f_in_chrome.exists() ? f_in_chrome.path : false;
298         f.close();
299         return path;
300 }
301
302 function found_ws_info_in_Uchrome() {
303         JSAN.use('util.file');
304         var f = new util.file();
305         var f_in_uchrome = f.get('ws_info','uchrome');
306         var path = f_in_uchrome.exists() ? f_in_uchrome.path : false;
307         f.close();
308         return path;
309 }
310
311 function handle_migration() {
312         if ( found_ws_info_in_Uchrome() ) {
313                 alert(offlineStrings.getFormattedString('main.settings.migrate.failed', [found_ws_info_in_Uchrome(), found_ws_info_in_Achrome()])
314                 );
315         } else {
316                 var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties );
317                 var f_new = dirService.get( "UChrm", Components.interfaces.nsIFile );
318                 var f_old = dirService.get( "AChrom", Components.interfaces.nsIFile );
319                 f_old.append(myPackageDir); f_old.append("content"); f_old.append("conf"); 
320                 if (window.confirm(offlineStrings.getFormattedString("main.settings.migrate.confirm", [f_old.path, f_new.path]))) {
321                         var files = f_old.directoryEntries;
322                         while (files.hasMoreElements()) {
323                                 var file = files.getNext();
324                                 var file2 = file.QueryInterface( Components.interfaces.nsILocalFile );
325                                 try {
326                                         file2.moveTo( f_new, '' );
327                                 } catch(E) {
328                                         alert(offlineStrings.getFormattedString('main.settings.migrate.error', [file2.path, f_new.path]) + '\n');
329                                 }
330                         }
331                         location.href = location.href; // huh?
332                 }
333         }
334 }
335
336 dump('exiting main/main.js\n');