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