]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/main/main.js
Patch to better handle migration of settings on the filesystem. Thanks Jeff!
[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('version_label');
240                 //x.setAttribute('value','Build ID: ' + version);
241                 var x = document.getElementById('about_btn');
242                 x.setAttribute('label', offlineStrings.getString('main.about_btn.label'));
243                 x.addEventListener(
244                         'command',
245                         function() {
246                                 try { 
247                                         G.window.open('about.html','about','chrome,resizable,width=800,height=600');
248                                 } catch(E) { alert(E); }
249                         }, 
250                         false
251                 );
252
253                 var y = document.getElementById('new_window_btn');
254                 y.setAttribute('label', offlineStrings.getString('main.new_window_btn.label'));
255                 y.addEventListener(
256                         'command',
257                         function() {
258                                 if (G.data.session) {
259                                         try {
260                                                 G.window.open('chrome://open_ils_staff_client/content/main/menu_frame.xul?server=' +
261                                                         G.data.server,'main','chrome,resizable' );
262
263                                         } catch(E) { alert(E); }
264                                 } else {
265                                         alert ( offlineStrings.getString('main.new_window_btn.login_first_warning') );
266                                 }
267                         },
268                         false
269                 );
270
271         var pref = Components.classes["@mozilla.org/preferences-service;1"]
272                 .getService(Components.interfaces.nsIPrefBranch);
273
274                 if ( found_ws_info_in_Achrome() && pref.getBoolPref("open-ils.write_in_user_chrome_directory") ) {
275                         //var hbox = x.parentNode; var b = document.createElement('button'); 
276                         //b.setAttribute('label','Migrate legacy settings'); hbox.appendChild(b);
277                         //b.addEventListener(
278                         //      'command',
279                         //      function() {
280                         //              try {
281                         //                      handle_migration();
282                         //              } catch(E) { alert(E); }
283                         //      },
284                         //      false
285                         //);
286                         if (window.confirm(offlineStrings.getString('main.settings.migrate'))) {
287                                 setTimeout( function() { handle_migration(); }, 0 );
288                         }
289                 }
290
291         } catch(E) {
292                 var error = offlineStrings.getFormattedString('common.exception', [E, '']);
293                 try { G.error.sdump('D_ERROR',error); } catch(E) { dump(error); }
294                 alert(error);
295         }
296         dump('exiting main_init()\n');
297 }
298
299 function found_ws_info_in_Achrome() {
300         JSAN.use('util.file');
301         var f = new util.file();
302         var f_in_chrome = f.get('ws_info','chrome');
303         var path = f_in_chrome.exists() ? f_in_chrome.path : false;
304         f.close();
305         return path;
306 }
307
308 function found_ws_info_in_Uchrome() {
309         JSAN.use('util.file');
310         var f = new util.file();
311         var f_in_uchrome = f.get('ws_info','uchrome');
312         var path = f_in_uchrome.exists() ? f_in_uchrome.path : false;
313         f.close();
314         return path;
315 }
316
317 function handle_migration() {
318         if ( found_ws_info_in_Uchrome() ) {
319                 alert(offlineStrings.getFormattedString('main.settings.migrate.failed', [found_ws_info_in_Uchrome(), found_ws_info_in_Achrome()])
320                 );
321         } else {
322                 var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties );
323                 var f_new = dirService.get( "UChrm", Components.interfaces.nsIFile );
324                 var f_old = dirService.get( "AChrom", Components.interfaces.nsIFile );
325                 f_old.append(myPackageDir); f_old.append("content"); f_old.append("conf"); 
326                 if (window.confirm(offlineStrings.getFormattedString("main.settings.migrate.confirm", [f_old.path, f_new.path]))) {
327                         var files = f_old.directoryEntries;
328                         while (files.hasMoreElements()) {
329                                 var file = files.getNext();
330                                 var file2 = file.QueryInterface( Components.interfaces.nsILocalFile );
331                                 try {
332                                         file2.moveTo( f_new, '' );
333                                 } catch(E) {
334                                         alert(offlineStrings.getFormattedString('main.settings.migrate.error', [file2.path, f_new.path]) + '\n');
335                                 }
336                         }
337                         location.href = location.href; // huh?
338                 }
339         }
340 }
341
342 dump('exiting main/main.js\n');