]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/main/main.js
61b8f4c7c8c430217b5c81800cc704bba8653b59
[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         try {
301             if (G.pref && G.pref.getBoolPref('open-ils.debug_options')) {
302                 document.getElementById('debug_gb').hidden = false;
303             }
304         } catch(E) {
305         }
306
307         window.title = authStrings.getFormattedString('staff.auth.titlebar.label', version);
308         var x = document.getElementById('about_btn');
309         x.addEventListener(
310             'command',
311             function() {
312                 try { 
313                     G.window.open('about.html','about','chrome,resizable,width=800,height=600');
314                 } catch(E) { alert(E); }
315             }, 
316             false
317         );
318
319         var y = document.getElementById('new_window_btn');
320         y.addEventListener(
321             'command',
322             function() {
323                 if (G.data.session) {
324                     try {
325                         //G.data_xul.g.open_menu();
326                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
327                         var mframe = xulG.window.open(( String(urls.XUL_MENU_FRAME).match(/^chrome:/) ? '' : G.data.server ) + urls.XUL_MENU_FRAME
328                             + '?server='+window.escape(G.data.server),
329                             'main'+xulG.window.window_name_increment(),'chrome,resizable'
330                         );
331                         mframe.xulG = xulG; 
332                     } catch(E) { alert(E); }
333                 } else {
334                     alert ( offlineStrings.getString('main.new_window_btn.login_first_warning') );
335                 }
336             },
337             false
338         );
339
340         JSAN.use('util.mozilla');
341         var z = document.getElementById('locale_menupopup');
342         if (z) {
343             while (z.lastChild) z.removeChild(z.lastChild);
344             var locales = util.mozilla.chromeRegistry().getLocalesForPackage( String( location.href ).split(/\//)[2] );
345             var current_locale = util.mozilla.prefs().getCharPref('general.useragent.locale');
346             while (locales.hasMore()) {
347                 var locale = locales.getNext();
348                 var parts = locale.split(/-/);
349                 var label;
350                 try {
351                     label = locale + ' : ' + util.mozilla.languages().GetStringFromName(parts[0]);
352                     if (parts.length > 1) {
353                         try {
354                             label += ' (' + util.mozilla.regions().GetStringFromName(parts[1].toLowerCase()) + ')';
355                         } catch(E) {
356                             label += ' (' + parts[1] + ')';
357                         }
358                     }
359                 } catch(E) {
360                     label = locale;
361                 }
362                 var mi = document.createElement('menuitem');
363                 mi.setAttribute('label',label);
364                 mi.setAttribute('value',locale);
365                 if (locale == current_locale) {
366                     if (z.parentNode.tagName == 'menulist') {
367                         mi.setAttribute('selected','true');
368                         z.parentNode.setAttribute('label',label);
369                         z.parentNode.setAttribute('value',locale);
370                     }
371                 }
372                 z.appendChild( mi );
373             }
374         }
375         var xx = document.getElementById('apply_locale_btn');
376         xx.addEventListener(
377             'command',
378             function() {
379                 util.mozilla.change_locale(z.parentNode.value);
380             },
381             false
382         );
383
384         if ( found_ws_info_in_Achrome() && G.pref && G.pref.getBoolPref("open-ils.write_in_user_chrome_directory") ) {
385             //var hbox = x.parentNode; var b = document.createElement('button'); 
386             //b.setAttribute('label','Migrate legacy settings'); hbox.appendChild(b);
387             //b.addEventListener(
388             //    'command',
389             //    function() {
390             //        try {
391             //            handle_migration();
392             //        } catch(E) { alert(E); }
393             //    },
394             //    false
395             //);
396             if (window.confirm(offlineStrings.getString('main.settings.migrate'))) {
397                 setTimeout( function() { handle_migration(); }, 0 );
398             }
399         }
400
401     } catch(E) {
402         var error = offlineStrings.getFormattedString('common.exception', [E, '']);
403         try { G.error.sdump('D_ERROR',error); } catch(E) { dump(error); }
404         alert(error);
405     }
406     dump('exiting main_init()\n');
407 }
408
409 function found_ws_info_in_Achrome() {
410     JSAN.use('util.file');
411     var f = new util.file();
412     var f_in_chrome = f.get('ws_info','chrome');
413     var path = f_in_chrome.exists() ? f_in_chrome.path : false;
414     f.close();
415     return path;
416 }
417
418 function found_ws_info_in_Uchrome() {
419     JSAN.use('util.file');
420     var f = new util.file();
421     var f_in_uchrome = f.get('ws_info','uchrome');
422     var path = f_in_uchrome.exists() ? f_in_uchrome.path : false;
423     f.close();
424     return path;
425 }
426
427 function handle_migration() {
428     if ( found_ws_info_in_Uchrome() ) {
429         alert(offlineStrings.getFormattedString('main.settings.migrate.failed', [found_ws_info_in_Uchrome(), found_ws_info_in_Achrome()])
430         );
431     } else {
432         var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties );
433         var f_new = dirService.get( "UChrm", Components.interfaces.nsIFile );
434         var f_old = dirService.get( "AChrom", Components.interfaces.nsIFile );
435         f_old.append(myPackageDir); f_old.append("content"); f_old.append("conf"); 
436         if (window.confirm(offlineStrings.getFormattedString("main.settings.migrate.confirm", [f_old.path, f_new.path]))) {
437             var files = f_old.directoryEntries;
438             while (files.hasMoreElements()) {
439                 var file = files.getNext();
440                 var file2 = file.QueryInterface( Components.interfaces.nsILocalFile );
441                 try {
442                     file2.moveTo( f_new, '' );
443                 } catch(E) {
444                     alert(offlineStrings.getFormattedString('main.settings.migrate.error', [file2.path, f_new.path]) + '\n');
445                 }
446             }
447             location.href = location.href; // huh?
448         }
449     }
450 }
451
452 dump('exiting main/main.js\n');