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