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