]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/main/main.js
though the staff client graciously passes IAMXUL into the environment, it's not avail...
[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 xulG;
5 var offlineStrings;
6 var authStrings;
7
8 function grant_perms(url) {
9         var perms = "UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead";
10         dump('Granting ' + perms + ' to ' + url + '\n');
11         if (G.pref) {
12                 G.pref.setCharPref("capability.principal.codebase.p0.granted", perms);
13                 G.pref.setCharPref("capability.principal.codebase.p0.id", url);
14                 G.pref.setCharPref("capability.principal.codebase.p1.granted", perms);
15                 G.pref.setCharPref("capability.principal.codebase.p1.id", url.replace('http:','https:'));
16                 G.pref.setBoolPref("dom.disable_open_during_load",false);
17                 G.pref.setBoolPref("browser.popups.showPopupBlocker",false);
18         }
19
20 }
21
22 function clear_the_cache() {
23         try {
24                 var cacheClass          = Components.classes["@mozilla.org/network/cache-service;1"];
25                 var cacheService        = cacheClass.getService(Components.interfaces.nsICacheService);
26                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
27                 cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
28         } catch(E) {
29                 dump(E+'\n');alert(E);
30         }
31 }
32
33 function toOpenWindowByType(inType, uri) { /* for Venkman */
34     try {
35         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
36         window.open(uri, "_blank", winopts);
37     } catch(E) {
38         alert(E); throw(E);
39     }
40 }
41
42 function start_debugger() {
43     setTimeout(
44         function() {
45             try { start_venkman(); } catch(E) { alert(E); }
46         }, 0
47     );
48 };
49
50 function start_inspector() {
51     setTimeout(
52         function() {
53             try { inspectDOMDocument(); } catch(E) { alert(E); }
54         }, 0
55     );
56 };
57
58 function start_chrome_list() {
59     setTimeout(
60         function() {
61             try { startChromeList(); } catch(E) { alert(E); }
62         }, 0
63     );
64 };
65
66 function start_js_shell() {
67     setTimeout(
68         function() {
69             try { window.open('chrome://open_ils_staff_client/content/util/shell.html','shell','chrome,resizable,scrollbars'); } catch(E) { alert(E); }
70         }, 0
71     );
72 };
73
74 function main_init() {
75         dump('entering main_init()\n');
76         try {
77                 clear_the_cache();
78
79                 // Now we can safely load the strings without the cache getting wiped
80                 offlineStrings = document.getElementById('offlineStrings');
81                 authStrings = document.getElementById('authStrings');
82
83                 if (typeof JSAN == 'undefined') {
84                         throw(
85                                 offlineStrings.getString('common.jsan.missing')
86                         );
87                 }
88                 /////////////////////////////////////////////////////////////////////////////
89
90                 JSAN.errorLevel = "die"; // none, warn, or die
91                 JSAN.addRepository('..');
92
93                 //JSAN.use('test.test'); test.test.hello_world();
94
95                 var mw = self;
96                 G =  {};
97         
98                 G.pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
99
100                 JSAN.use('util.error');
101                 G.error = new util.error();
102                 G.error.sdump('D_ERROR', offlineStrings.getString('main.testing'));
103
104                 JSAN.use('util.window');
105                 G.window = new util.window();
106
107                 JSAN.use('auth.controller');
108                 G.auth = new auth.controller( { 'window' : mw } );
109
110                 JSAN.use('OpenILS.data');
111                 G.data = new OpenILS.data();
112                 G.data.on_error = G.auth.logoff;
113
114                 JSAN.use('util.file');
115                 G.file = new util.file();
116                 try {
117                         G.file.get('ws_info');
118                         G.ws_info = G.file.get_object(); G.file.close();
119                 } catch(E) {
120                         G.ws_info = {};
121                 }
122                 G.data.ws_info = G.ws_info; G.data.stash('ws_info');
123
124                 G.auth.on_login = function() {
125
126                         var url = G.auth.controller.view.server_prompt.value || urls.remote;
127
128                         G.data.server_unadorned = url; G.data.stash('server_unadorned'); G.data.stash_retrieve();
129
130                         if (! url.match( '^http://' ) ) { url = 'http://' + url; }
131
132                         G.data.server = url; G.data.stash('server'); 
133                         G.data.session = { 'key' : G.auth.session.key, 'auth' : G.auth.session.authtime }; G.data.stash('session');
134                         G.data.stash_retrieve();
135             try {
136                 var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
137                 var cookieUri = ios.newURI("http://" + G.data.server_unadorned, null, null);
138                 var cookieUriSSL = ios.newURI("https://" + G.data.server_unadorned, null, null);
139                 var cookieSvc = Components.classes["@mozilla.org/cookieService;1"].getService(Components.interfaces.nsICookieService);
140
141                 cookieSvc.setCookieString(cookieUri, null, "ses="+G.data.session.key, null);
142                 cookieSvc.setCookieString(cookieUriSSL, null, "ses="+G.data.session.key, null);
143                 cookieSvc.setCookieString(cookieUri, null, "xul=1", null);
144                 cookieSvc.setCookieString(cookieUriSSL, null, "xul=1", null);
145
146             } catch(E) {
147                 alert(offlineStrings.getFormattedString(main.session_cookie.error, [E]));
148             }
149
150                         grant_perms(url);
151
152                         xulG = {
153                                 'auth' : G.auth,
154                                 'url' : url,
155                                 'window' : G.window,
156                 'data' : G.data,
157                                 'pref' : G.pref
158                         };
159
160                         if (G.data.ws_info && G.data.ws_info[G.auth.controller.view.server_prompt.value]) {
161                                 JSAN.use('util.widgets');
162                                 var deck = document.getElementById('progress_space');
163                                 util.widgets.remove_children( deck );
164                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
165                                 iframe.setAttribute( 'src', url + '/xul/server/main/data.xul' );
166                                 iframe.contentWindow.xulG = xulG;
167                 G.data_xul = iframe.contentWindow;
168                         } else {
169                                 xulG.file = G.file;
170                                 var deck = G.auth.controller.view.ws_deck;
171                                 JSAN.use('util.widgets'); util.widgets.remove_children('ws_deck');
172                                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
173                                 iframe.setAttribute( 'src', url + '/xul/server/main/ws_info.xul' );
174                                 iframe.contentWindow.xulG = xulG;
175                                 deck.selectedIndex = deck.childNodes.length - 1;
176                         }
177                 };
178
179                 G.auth.on_standalone = function() {
180                         try {
181                                 G.window.open(urls.XUL_STANDALONE,'Offline','chrome,resizable');
182                         } catch(E) {
183                                 alert(E);
184                         }
185                 };
186
187                 G.auth.on_standalone_export = function() {
188                         try {
189                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
190                                 if (file._file.exists()) {
191                     var file2 = new util.file('');
192                                         var f = file2.pick_file( { 'mode' : 'save', 'title' : offlineStrings.getString('main.transaction_export.title') } );
193                                         if (f) {
194                                                 if (f.exists()) {
195                                                         var r = G.error.yns_alert(
196                                                                 offlineStrings.getFormattedString('main.transaction_export.prompt', [f.leafName]),
197                                                                 offlineStrings.getString('main.transaction_export.prompt.title'),
198                                                                 offlineStrings.getString('common.yes'),
199                                                                 offlineStrings.getString('common.no'),
200                                                                 null,
201                                                                 offlineStrings.getString('common.confirm')
202                                                         );
203                                                         if (r != 0) { file.close(); return; }
204                                                 }
205                                                 var e_file = new util.file(''); e_file._file = f;
206                                                 e_file.write_content( 'truncate', file.get_content() );
207                                                 e_file.close();
208                                                 var r = G.error.yns_alert(
209                                                         offlineStrings.getFormattedString('main.transaction_export.success.prompt', [f.leafName]),
210                                                         offlineStrings.getString('main.transaction_export.success.title'),
211                                                         offlineStrings.getString('common.yes'),
212                                                         offlineStrings.getString('common.no'),
213                                                         null,
214                                                         offlineStrings.getString('common.confirm')
215                                                 );
216                                                 if (r == 0) {
217                                                         var count = 0;
218                                                         var filename = 'pending_xacts_exported_' + new Date().getTime();
219                                                         var t_file = new util.file(filename);
220                                                         while (t_file._file.exists()) {
221                                                                 filename = 'pending_xacts_' + new Date().getTime() + '.exported';
222                                                                 t_file = new util.file(filename);
223                                                                 if (count++ > 100) {
224                                                                         throw(offlineStrings.getString('main.transaction_export.filename.error'));
225                                                                 }
226                                                         }
227                                                         file._file.moveTo(null,filename);
228                                                 } else {
229                                                         alert(offlineStrings.getString('main.transaction_export.duplicate.warning'));
230                                                 }
231                                         } else {
232                                                 alert(offlineStrings.getString('main.transaction_export.no_filename.error'));
233                                         }
234                                 } else {
235                                         alert(offlineStrings.getString('main.transaction_export.no_transactions.error'));
236                                 }
237                                 file.close();
238                         } catch(E) {
239                                 alert(E);
240                         }
241                 };
242
243                 G.auth.on_standalone_import = function() {
244                         try {
245                                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
246                                 if (file._file.exists()) {
247                                         alert(offlineStrings.getString('main.transaction_import.outstanding.error'));
248                                 } else {
249                     var file2 = new util.file('');
250                                         var f = file2.pick_file( { 'mode' : 'open', 'title' : offlineStrings.getString('main.transaction_import.title')} );
251                                         if (f && f.exists()) {
252                                                 var i_file = new util.file(''); i_file._file = f;
253                                                 file.write_content( 'truncate', i_file.get_content() );
254                                                 i_file.close();
255                                                 var r = G.error.yns_alert(
256                                                         offlineStrings.getFormattedString('main.transaction_import.delete.prompt', [f.leafName]),
257                                                         offlineStrings.getString('main.transaction_import.success'),
258                                                         offlineStrings.getString('common.yes'),
259                                                         offlineStrings.getString('common.no'),
260                                                         null,
261                                                         offlineStrings.getString('common.confirm')
262                                                 );
263                                                 if (r == 0) {
264                                                         f.remove(false);
265                                                 }
266                                         }
267                                 }
268                                 file.close();
269                         } catch(E) {
270                                 alert(E);
271                         }
272                 };
273
274                 G.auth.on_debug = function(action) {
275                         switch(action) {
276                                 case 'js_console' :
277                                         G.window.open(urls.XUL_DEBUG_CONSOLE,'testconsole','chrome,resizable');
278                                 break;
279                                 case 'clear_cache' :
280                                         clear_the_cache();
281                                         alert(offlineStrings.getString('main.on_debug.clear_cache'));
282                                 break;
283                                 default:
284                                         alert(offlineStrings.getString('main.on_debug.debug'));
285                                 break;
286                         }
287                 };
288
289                 G.auth.init();
290                 // XML_HTTP_SERVER will get reset to G.auth.controller.view.server_prompt.value
291
292                 /////////////////////////////////////////////////////////////////////////////
293
294                 var version = '/xul/server/'.split(/\//)[2];
295                 if (version == 'server') {
296                         version = 'versionless debug build';
297                         document.getElementById('debug_gb').hidden = false;
298                 }
299
300         if (G.pref && G.pref.getBoolPref('open-ils.debug_options')) {
301                         document.getElementById('debug_gb').hidden = false;
302         }
303
304         window.title = authStrings.getFormattedString('staff.auth.titlebar.label', version);
305                 var x = document.getElementById('about_btn');
306                 x.addEventListener(
307                         'command',
308                         function() {
309                                 try { 
310                                         G.window.open('about.html','about','chrome,resizable,width=800,height=600');
311                                 } catch(E) { alert(E); }
312                         }, 
313                         false
314                 );
315
316                 var y = document.getElementById('new_window_btn');
317                 y.addEventListener(
318                         'command',
319                         function() {
320                                 if (G.data.session) {
321                                         try {
322                                                 //G.data_xul.g.open_menu();
323                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
324                         var mframe = xulG.window.open(( String(urls.XUL_MENU_FRAME).match(/^chrome:/) ? '' : G.data.server ) + urls.XUL_MENU_FRAME
325                             + '?server='+window.escape(G.data.server),
326                             'main'+xulG.window.window_name_increment(),'chrome,resizable'
327                         );
328                         mframe.xulG = xulG; 
329                                         } catch(E) { alert(E); }
330                                 } else {
331                                         alert ( offlineStrings.getString('main.new_window_btn.login_first_warning') );
332                                 }
333                         },
334                         false
335                 );
336
337                 JSAN.use('util.mozilla');
338                 var z = document.getElementById('locale_menupopup');
339                 if (z) {
340                         while (z.lastChild) z.removeChild(z.lastChild);
341                         var locales = util.mozilla.chromeRegistry().getLocalesForPackage( String( location.href ).split(/\//)[2] );
342                         var current_locale = util.mozilla.prefs().getCharPref('general.useragent.locale');
343             while (locales.hasMore()) {
344                 var locale = locales.getNext();
345                 var parts = locale.split(/-/);
346                 var label;
347                 try {
348                     label = locale + ' : ' + util.mozilla.languages().GetStringFromName(parts[0]);
349                     if (parts.length > 1) {
350                         try {
351                             label += ' (' + util.mozilla.regions().GetStringFromName(parts[1].toLowerCase()) + ')';
352                         } catch(E) {
353                             label += ' (' + parts[1] + ')';
354                         }
355                     }
356                 } catch(E) {
357                     label = locale;
358                 }
359                                 var mi = document.createElement('menuitem');
360                                 mi.setAttribute('label',label);
361                                 mi.setAttribute('value',locale);
362                 if (locale == current_locale) {
363                     if (z.parentNode.tagName == 'menulist') {
364                         mi.setAttribute('selected','true');
365                         z.parentNode.setAttribute('label',label);
366                         z.parentNode.setAttribute('value',locale);
367                     }
368                 }
369                 z.appendChild( mi );
370             }
371                 }
372                 var xx = document.getElementById('apply_locale_btn');
373                 xx.addEventListener(
374                         'command',
375                         function() {
376                                 util.mozilla.change_locale(z.parentNode.value);
377                         },
378                         false
379                 );
380
381                 if ( found_ws_info_in_Achrome() && G.pref && G.pref.getBoolPref("open-ils.write_in_user_chrome_directory") ) {
382                         //var hbox = x.parentNode; var b = document.createElement('button'); 
383                         //b.setAttribute('label','Migrate legacy settings'); hbox.appendChild(b);
384                         //b.addEventListener(
385                         //      'command',
386                         //      function() {
387                         //              try {
388                         //                      handle_migration();
389                         //              } catch(E) { alert(E); }
390                         //      },
391                         //      false
392                         //);
393                         if (window.confirm(offlineStrings.getString('main.settings.migrate'))) {
394                                 setTimeout( function() { handle_migration(); }, 0 );
395                         }
396                 }
397
398         } catch(E) {
399                 var error = offlineStrings.getFormattedString('common.exception', [E, '']);
400                 try { G.error.sdump('D_ERROR',error); } catch(E) { dump(error); }
401                 alert(error);
402         }
403         dump('exiting main_init()\n');
404 }
405
406 function found_ws_info_in_Achrome() {
407         JSAN.use('util.file');
408         var f = new util.file();
409         var f_in_chrome = f.get('ws_info','chrome');
410         var path = f_in_chrome.exists() ? f_in_chrome.path : false;
411         f.close();
412         return path;
413 }
414
415 function found_ws_info_in_Uchrome() {
416         JSAN.use('util.file');
417         var f = new util.file();
418         var f_in_uchrome = f.get('ws_info','uchrome');
419         var path = f_in_uchrome.exists() ? f_in_uchrome.path : false;
420         f.close();
421         return path;
422 }
423
424 function handle_migration() {
425         if ( found_ws_info_in_Uchrome() ) {
426                 alert(offlineStrings.getFormattedString('main.settings.migrate.failed', [found_ws_info_in_Uchrome(), found_ws_info_in_Achrome()])
427                 );
428         } else {
429                 var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties );
430                 var f_new = dirService.get( "UChrm", Components.interfaces.nsIFile );
431                 var f_old = dirService.get( "AChrom", Components.interfaces.nsIFile );
432                 f_old.append(myPackageDir); f_old.append("content"); f_old.append("conf"); 
433                 if (window.confirm(offlineStrings.getFormattedString("main.settings.migrate.confirm", [f_old.path, f_new.path]))) {
434                         var files = f_old.directoryEntries;
435                         while (files.hasMoreElements()) {
436                                 var file = files.getNext();
437                                 var file2 = file.QueryInterface( Components.interfaces.nsILocalFile );
438                                 try {
439                                         file2.moveTo( f_new, '' );
440                                 } catch(E) {
441                                         alert(offlineStrings.getFormattedString('main.settings.migrate.error', [file2.path, f_new.path]) + '\n');
442                                 }
443                         }
444                         location.href = location.href; // huh?
445                 }
446         }
447 }
448
449 dump('exiting main/main.js\n');