f13cbfe6331b4ba7fd13c102a8b25ad37853bc4f
[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 var openTabs = new Array();
8 var tempWindow = null;
9 var tempFocusWindow = null;
10
11 function grant_perms(url) {
12     var perms = "UniversalXPConnect UniversalPreferencesWrite UniversalBrowserWrite UniversalPreferencesRead UniversalBrowserRead UniversalFileRead";
13     dump('Granting ' + perms + ' to ' + url + '\n');
14     if (G.pref) {
15         G.pref.setCharPref("capability.principal.codebase.p0.granted", perms);
16         G.pref.setCharPref("capability.principal.codebase.p0.id", url);
17         G.pref.setCharPref("capability.principal.codebase.p1.granted", perms);
18         G.pref.setCharPref("capability.principal.codebase.p1.id", url.replace('http:','https:'));
19         G.pref.setBoolPref("dom.disable_open_during_load",false);
20         G.pref.setBoolPref("browser.popups.showPopupBlocker",false);
21     }
22
23 }
24
25 function clear_the_cache() {
26     try {
27         var cacheClass         = Components.classes["@mozilla.org/network/cache-service;1"];
28         var cacheService    = cacheClass.getService(Components.interfaces.nsICacheService);
29         cacheService.evictEntries(Components.interfaces.nsICache.STORE_ON_DISK);
30         cacheService.evictEntries(Components.interfaces.nsICache.STORE_IN_MEMORY);
31     } catch(E) {
32         dump(E+'\n');alert(E);
33     }
34 }
35
36 function toOpenWindowByType(inType, uri) { /* for Venkman */
37     try {
38         var winopts = "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar";
39         window.open(uri, "_blank", winopts);
40     } catch(E) {
41         alert(E); throw(E);
42     }
43 }
44
45 function start_debugger() {
46     setTimeout(
47         function() {
48             try { start_venkman(); } catch(E) { alert(E); }
49         }, 0
50     );
51 };
52
53 function start_inspector() {
54     setTimeout(
55         function() {
56             try { inspectDOMDocument(); } catch(E) { alert(E); }
57         }, 0
58     );
59 };
60
61 function start_chrome_list() {
62     setTimeout(
63         function() {
64             try { startChromeList(); } catch(E) { alert(E); }
65         }, 0
66     );
67 };
68
69 function start_js_shell() {
70     setTimeout(
71         function() {
72             try { window.open('chrome://open_ils_staff_client/content/util/shell.html','shell','chrome,resizable,scrollbars'); } catch(E) { alert(E); }
73         }, 0
74     );
75 };
76
77 function new_tabs(aTabList, aContinue) {
78     if(aTabList != null) {
79         openTabs = openTabs.concat(aTabList);
80     }
81     if(G.data.session) { // Just add to the list of stuff to open unless we are logged in
82         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
83         var targetwindow = null;
84         var focuswindow = null;
85         var focustab = {'focus' : true};
86         if(aContinue == true && tempWindow.closed == false) {
87             if(tempWindow.g == undefined || tempWindow.g.menu == undefined) {
88                 setTimeout(
89                     function() {
90                         new_tabs(null, true);
91                     }, 300);
92                 return null;
93             }
94             targetwindow = tempWindow;
95             tempWindow = null;
96             focuswindow = tempFocusWindow;
97             tempFocusWindow = null;
98             focustab = {'nofocus' : true};
99         }
100         else if(tempWindow != null) { // In theory, we are waiting on a setTimeout
101             if(tempWindow.closed == true) // But someone closed our window?
102             {
103                 tempWindow = null;
104                 tempFocusWindow = null;
105             }
106             else
107             {
108                 return null;
109             }
110         }
111         var newTab;
112         var firstURL;
113         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
114             getService(Components.interfaces.nsIWindowMediator);
115             // This may look out of place, but this is so we can continue this loop from down below
116 opentabs:
117             while(openTabs.length > 0) {
118             newTab = openTabs.shift();
119             if(newTab == 'new' || newTab == 'init') {
120                 if(newTab != 'init' && openTabs.length > 0 && openTabs[0] != 'new') {
121                     firstURL = openTabs.shift();
122                     if(firstURL != 'tab') { // 'new' followed by 'tab' should be equal to 'init' in functionality, this should do that
123                         if(urls[firstURL]) {
124                             firstURL = urls[firstURL];
125                         }
126                         firstURL = '&firstURL=' + window.escape(firstURL);
127                     }
128                     else {
129                         firstURL = '';
130                     }
131                 }
132                 else {
133                     firstURL = '';
134                 }
135                 targetwindow = xulG.window.open(urls.XUL_MENU_FRAME
136                     + '?server='+window.escape(G.data.server) + firstURL,
137                     '_blank','chrome,resizable'
138                 );
139                 targetwindow.xulG = xulG;
140                 if (focuswindow == null) {
141                     focuswindow = targetwindow;
142                 }
143                 tempWindow = targetwindow;
144                 tempFocusWindow = focuswindow;
145                 setTimeout(
146                     function() {
147                         new_tabs(null, true);
148                     }, 300);
149                 return null;
150             }
151             else {
152                 if(newTab == 'tab') {
153                     newTab = null;
154                 }
155                 else if(urls[newTab]) {
156                     newTab = urls[newTab];
157                 }
158                 if(targetwindow != null) { // Already have a previous target window? Use it first.
159                     if(targetwindow.g.menu.new_tab(newTab,focustab,null)) {
160                         focustab = {'nofocus' : true};
161                         continue;
162                     }
163                 }
164                 var enumerator = wm.getEnumerator('eg_menu');
165                 while(enumerator.hasMoreElements()) {
166                     targetwindow = enumerator.getNext();
167                     if(targetwindow.g.menu.new_tab(newTab,focustab,null)) {
168                         focustab = {'nofocus' : true};
169                         if (focuswindow == null) {
170                             focuswindow = targetwindow;
171                         }
172                         continue opentabs;
173                     }
174                 }
175                 // No windows found to add the tab to? Make a new one.
176                 if(newTab == null) { // Were we making a "default" tab?
177                     openTabs.unshift('init'); // 'init' does that for us!
178                 }
179                 else {
180                     openTabs.unshift('new',newTab);
181                 }
182             }
183         }
184         if(focuswindow != null) {
185             focuswindow.focus();
186         }
187     }
188 }
189
190 // Returns false if we can't get an actual perm list
191 // Returns an array of perms with boolean has/hasn't flag
192 function get_menu_perms(indocument) {
193     // If we don't have our static perm list, and we have a remote window, go looking.
194     // We never need to look twice unless a dev is manually editing their files.
195     // Shame on them, they can restart the entire client ;)
196     if(typeof(get_menu_perms.perm_list) == 'undefined' && indocument != null)
197     {
198         get_menu_perms.perm_list = [ ];
199         var commands = indocument.getElementById('universal_cmds').getElementsByTagName('command');
200         for (var i = 0; i < commands.length; i++) { 
201             if (commands[i].hasAttribute('perm')) {
202                 get_menu_perms.perm_list = get_menu_perms.perm_list.concat(commands[i].getAttribute('perm').split(' '));
203             }           
204         }
205     }
206     // 
207     if(typeof(get_menu_perms.perm_list) == 'object') {
208         G.data.stash_retrieve();
209         if(!G.data.menu_perms) {
210             JSAN.use('util.network');
211             var network = new util.network();
212             var r = network.simple_request('BATCH_PERM_RETRIEVE_WORK_OU', [ G.data.session.key, get_menu_perms.perm_list ]);
213             for(p in r)
214                 r[p] = (typeof(r[p][0]) == 'number');
215             G.data.menu_perms = r;
216             G.data.stash('menu_perms');
217         }
218         return G.data.menu_perms;
219     }
220     return false;
221 }
222
223 // Returns a list (cached or from filesystem) of hotkey sets
224 function load_hotkey_sets() {
225     if(typeof(load_hotkey_sets.set_list) == 'undefined') {
226         load_hotkey_sets.set_list = [];
227         JSAN.use('util.file');
228         var file = new util.file();
229         var dirEntries = file.get('hotkeys','skin').directoryEntries;
230         while(dirEntries.hasMoreElements()) {
231             var entry = dirEntries.getNext();
232             entry.QueryInterface(Components.interfaces.nsIFile);
233             if(!entry.isFile()) continue;
234             if(!entry.leafName.match(/.+\.keyset$/)) continue;
235             var keysetname = entry.leafName.replace(/\.keyset$/,'');
236             load_hotkey_sets.set_list.push(keysetname);
237         }
238         file.close();
239     }
240     return load_hotkey_sets.set_list;
241 }
242
243 // Returns an array (cached or from filesystem) for a given hotkey set
244 function get_hotkey_array(keyset_name) {
245     if(typeof(get_hotkey_array.keyset_cache) == 'undefined') {
246         get_hotkey_array.keyset_cache = {};
247     }
248     if(get_hotkey_array.keyset_cache[keyset_name])
249         return get_hotkey_array.keyset_cache[keyset_name];
250     JSAN.use('util.file');
251     var file = new util.file();
252     var keyset_raw;
253     try {
254         var keyset_file = file.get('hotkeys','skin');
255         keyset_file.append(keyset_name + ".keyset");
256         keyset_raw = file.get_content();
257         file.close();
258         var tempArray = [];
259
260         var keyset_lines = keyset_raw.trim().split("\n");
261         for(var line = 0; line < keyset_lines.length; line++) {
262             // Grab line, strip comments, strip leading/trailing whitespace
263             var curline = keyset_lines[line].replace(/\s*#.*$/,'').trim();
264             if(curline == "") continue; // Skip empty lines
265             // Split into pieces
266             var split_line = curline.split(',');
267             // We need at least 3 elements. Command, modifiers, keycode.
268             if(split_line.length < 3) continue;
269             // Trim each segment
270             split_line[0] = split_line[0].trim();
271             split_line[1] = split_line[1].trim();
272             split_line[2] = split_line[2].trim();
273             if(split_line.length > 3)
274                 split_line[3] = split_line[3].trim();
275             // Skip empty commands
276             if(split_line[0] == "") continue;
277             // Push to array
278             tempArray.push(split_line);
279         }
280         get_hotkey_array.keyset_cache[keyset_name] = tempArray;
281         return tempArray;
282     } catch(E) { // Something went wrong.
283         return false;
284     }
285 }
286
287 function main_init() {
288     dump('entering main_init()\n');
289     try {
290         clear_the_cache();
291         if("arguments" in window && window.arguments.length > 0 && window.arguments[0].wrappedJSObject != undefined && window.arguments[0].wrappedJSObject.openTabs != undefined) {
292             openTabs = openTabs.concat(window.arguments[0].wrappedJSObject.openTabs);
293         }
294
295         // Now we can safely load the strings without the cache getting wiped
296         offlineStrings = document.getElementById('offlineStrings');
297         authStrings = document.getElementById('authStrings');
298
299         if (typeof JSAN == 'undefined') {
300             throw(
301                 offlineStrings.getString('common.jsan.missing')
302             );
303         }
304         /////////////////////////////////////////////////////////////////////////////
305
306         JSAN.errorLevel = "die"; // none, warn, or die
307         JSAN.addRepository('..');
308
309         //JSAN.use('test.test'); test.test.hello_world();
310
311         var mw = self;
312         G =  {};
313         
314         G.pref = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
315         if (! G.pref.prefHasUserValue('general.useragent.override')) {
316             G.pref.setCharPref('general.useragent.override',navigator.userAgent + ' oils_xulrunner /xul/server/');
317         }
318
319         JSAN.use('util.error');
320         G.error = new util.error();
321         G.error.sdump('D_ERROR', offlineStrings.getString('main.testing'));
322
323         JSAN.use('util.window');
324         G.window = new util.window();
325
326         JSAN.use('auth.controller');
327         G.auth = new auth.controller( { 'window' : mw } );
328
329         JSAN.use('OpenILS.data');
330         G.data = new OpenILS.data();
331         G.data.on_error = G.auth.logoff;
332
333         JSAN.use('util.file');
334         G.file = new util.file();
335         try {
336             G.file.get('ws_info');
337             G.ws_info = G.file.get_object(); G.file.close();
338         } catch(E) {
339             G.ws_info = {};
340         }
341         G.data.ws_info = G.ws_info; G.data.stash('ws_info');
342
343         G.auth.on_login = function() {
344
345             var url = G.auth.controller.view.server_prompt.value || urls.remote;
346
347             G.data.server_unadorned = url; G.data.stash('server_unadorned'); G.data.stash_retrieve();
348
349             if (! url.match( '^http://' ) ) { url = 'http://' + url; }
350
351             G.data.server = url; G.data.stash('server'); 
352             G.data.session = { 'key' : G.auth.session.key, 'auth' : G.auth.session.authtime }; G.data.stash('session');
353             G.data.stash_retrieve();
354             try {
355                 var ios = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
356                 var cookieUri = ios.newURI("http://" + G.data.server_unadorned, null, null);
357                 var cookieUriSSL = ios.newURI("https://" + G.data.server_unadorned, null, null);
358                 var cookieSvc = Components.classes["@mozilla.org/cookieService;1"].getService(Components.interfaces.nsICookieService);
359
360                 cookieSvc.setCookieString(cookieUri, null, "ses="+G.data.session.key, null);
361                 cookieSvc.setCookieString(cookieUriSSL, null, "ses="+G.data.session.key, null);
362                 cookieSvc.setCookieString(cookieUri, null, "xul=1", null);
363                 cookieSvc.setCookieString(cookieUriSSL, null, "xul=1", null);
364
365             } catch(E) {
366                 alert(offlineStrings.getFormattedString(main.session_cookie.error, [E]));
367             }
368
369             grant_perms(url);
370
371             xulG = {
372                 'auth' : G.auth,
373                 'url' : url,
374                 'window' : G.window,
375                 'data' : G.data,
376                 'pref' : G.pref
377             };
378
379             if (G.data.ws_info && G.data.ws_info[G.auth.controller.view.server_prompt.value]) {
380                 JSAN.use('util.widgets');
381                 var deck = document.getElementById('progress_space');
382                 util.widgets.remove_children( deck );
383                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
384                 iframe.setAttribute( 'src', url + urls.XUL_LOGIN_DATA );
385                 iframe.contentWindow.xulG = xulG;
386                 G.data_xul = iframe.contentWindow;
387             } else {
388                 xulG.file = G.file;
389                 var deck = G.auth.controller.view.ws_deck;
390                 JSAN.use('util.widgets'); util.widgets.remove_children('ws_deck');
391                 var iframe = document.createElement('iframe'); deck.appendChild(iframe);
392                 iframe.setAttribute( 'src', url + urls.XUL_WORKSTATION_INFO );
393                 iframe.contentWindow.xulG = xulG;
394                 deck.selectedIndex = deck.childNodes.length - 1;
395             }
396         };
397
398         G.auth.on_standalone = function() {
399             try {
400                 G.window.open(urls.XUL_STANDALONE,'Offline','chrome,resizable');
401             } catch(E) {
402                 alert(E);
403             }
404         };
405
406         G.auth.on_standalone_export = function() {
407             try {
408                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
409                 if (file._file.exists()) {
410                     var file2 = new util.file('');
411                     var f = file2.pick_file( { 'mode' : 'save', 'title' : offlineStrings.getString('main.transaction_export.title') } );
412                     if (f) {
413                         if (f.exists()) {
414                             var r = G.error.yns_alert(
415                                 offlineStrings.getFormattedString('main.transaction_export.prompt', [f.leafName]),
416                                 offlineStrings.getString('main.transaction_export.prompt.title'),
417                                 offlineStrings.getString('common.yes'),
418                                 offlineStrings.getString('common.no'),
419                                 null,
420                                 offlineStrings.getString('common.confirm')
421                             );
422                             if (r != 0) { file.close(); return; }
423                         }
424                         var e_file = new util.file(''); e_file._file = f;
425                         e_file.write_content( 'truncate', file.get_content() );
426                         e_file.close();
427                         var r = G.error.yns_alert(
428                             offlineStrings.getFormattedString('main.transaction_export.success.prompt', [f.leafName]),
429                             offlineStrings.getString('main.transaction_export.success.title'),
430                             offlineStrings.getString('common.yes'),
431                             offlineStrings.getString('common.no'),
432                             null,
433                             offlineStrings.getString('common.confirm')
434                         );
435                         if (r == 0) {
436                             var count = 0;
437                             var filename = 'pending_xacts_exported_' + new Date().getTime();
438                             var t_file = new util.file(filename);
439                             while (t_file._file.exists()) {
440                                 filename = 'pending_xacts_' + new Date().getTime() + '.exported';
441                                 t_file = new util.file(filename);
442                                 if (count++ > 100) {
443                                     throw(offlineStrings.getString('main.transaction_export.filename.error'));
444                                 }
445                             }
446                             file.close(); file = new util.file('pending_xacts'); // prevents a bug with .moveTo below
447                             file._file.moveTo(null,filename);
448                         } else {
449                             alert(offlineStrings.getString('main.transaction_export.duplicate.warning'));
450                         }
451                     } else {
452                         alert(offlineStrings.getString('main.transaction_export.no_filename.error'));
453                     }
454                 } else {
455                     alert(offlineStrings.getString('main.transaction_export.no_transactions.error'));
456                 }
457                 file.close();
458             } catch(E) {
459                 alert(E);
460             }
461         };
462
463         G.auth.on_standalone_import = function() {
464             try {
465                 JSAN.use('util.file'); var file = new util.file('pending_xacts');
466                 if (file._file.exists()) {
467                     alert(offlineStrings.getString('main.transaction_import.outstanding.error'));
468                 } else {
469                     var file2 = new util.file('');
470                     var f = file2.pick_file( { 'mode' : 'open', 'title' : offlineStrings.getString('main.transaction_import.title')} );
471                     if (f && f.exists()) {
472                         var i_file = new util.file(''); i_file._file = f;
473                         file.write_content( 'truncate', i_file.get_content() );
474                         i_file.close();
475                         var r = G.error.yns_alert(
476                             offlineStrings.getFormattedString('main.transaction_import.delete.prompt', [f.leafName]),
477                             offlineStrings.getString('main.transaction_import.success'),
478                             offlineStrings.getString('common.yes'),
479                             offlineStrings.getString('common.no'),
480                             null,
481                             offlineStrings.getString('common.confirm')
482                         );
483                         if (r == 0) {
484                             f.remove(false);
485                         }
486                     }
487                 }
488                 file.close();
489             } catch(E) {
490                 alert(E);
491             }
492         };
493
494         G.auth.on_debug = function(action) {
495             switch(action) {
496                 case 'js_console' :
497                     G.window.open(urls.XUL_DEBUG_CONSOLE,'testconsole','chrome,resizable');
498                 break;
499                 case 'clear_cache' :
500                     clear_the_cache();
501                     alert(offlineStrings.getString('main.on_debug.clear_cache'));
502                 break;
503                 default:
504                     alert(offlineStrings.getString('main.on_debug.debug'));
505                 break;
506             }
507         };
508
509         G.auth.init();
510         // XML_HTTP_SERVER will get reset to G.auth.controller.view.server_prompt.value
511
512         /////////////////////////////////////////////////////////////////////////////
513
514         var version = CLIENT_VERSION;
515         if (CLIENT_STAMP.length == 0) {
516             version = 'versionless debug build';
517             document.getElementById('debug_gb').hidden = false;
518         }
519
520         try {
521             if (G.pref && G.pref.getBoolPref('open-ils.debug_options')) {
522                 document.getElementById('debug_gb').hidden = false;
523             }
524         } catch(E) {
525         }
526
527         var appInfo = Components.classes["@mozilla.org/xre/app-info;1"] 
528             .getService(Components.interfaces.nsIXULAppInfo); 
529
530         if (appInfo.ID == "staff-client@open-ils.org")
531         {
532             try {
533                 if (G.pref && G.pref.getBoolPref('app.update.enabled')) {
534                     document.getElementById('check_upgrade_sep').hidden = false;
535                     var upgrademenu = document.getElementById('check_upgrade');
536                     upgrademenu.hidden = false;
537                     G.upgradeCheck = function () {
538                         var um = Components.classes["@mozilla.org/updates/update-manager;1"]
539                             .getService(Components.interfaces.nsIUpdateManager);
540                         var prompter = Components.classes["@mozilla.org/updates/update-prompt;1"]
541                             .createInstance(Components.interfaces.nsIUpdatePrompt);
542
543                         if (um.activeUpdate && um.activeUpdate.state == "pending")
544                             prompter.showUpdateDownloaded(um.activeUpdate);
545                         else
546                             prompter.checkForUpdates();
547                     }
548                     upgrademenu.addEventListener(
549                         'command',
550                         G.upgradeCheck,
551                         false
552                     );
553                 }
554             } catch(E) {
555             }
556         }
557
558         window.title = authStrings.getFormattedString('staff.auth.titlebar.label', version);
559         var x = document.getElementById('about_btn');
560         x.addEventListener(
561             'command',
562             function() {
563                 try { 
564                     G.window.open('about.html','about','chrome,resizable,width=800,height=600');
565                 } catch(E) { alert(E); }
566             }, 
567             false
568         );
569
570         var y = document.getElementById('new_window_btn');
571         y.addEventListener(
572             'command',
573             function() {
574                 if (G.data.session) {
575                     new_tabs(Array('new'), null, null);
576                 } else {
577                     alert ( offlineStrings.getString('main.new_window_btn.login_first_warning') );
578                 }
579             },
580             false
581         );
582
583         JSAN.use('util.mozilla');
584         var z = document.getElementById('locale_menupopup');
585         if (z) {
586             while (z.lastChild) z.removeChild(z.lastChild);
587             var locales = util.mozilla.chromeRegistry().getLocalesForPackage( String( location.href ).split(/\//)[2] );
588             var current_locale = util.mozilla.prefs().getCharPref('general.useragent.locale');
589             while (locales.hasMore()) {
590                 var locale = locales.getNext();
591                 var parts = locale.split(/-/);
592                 var label;
593                 try {
594                     label = locale + ' : ' + util.mozilla.languages().GetStringFromName(parts[0]);
595                     if (parts.length > 1) {
596                         try {
597                             label += ' (' + util.mozilla.regions().GetStringFromName(parts[1].toLowerCase()) + ')';
598                         } catch(E) {
599                             label += ' (' + parts[1] + ')';
600                         }
601                     }
602                 } catch(E) {
603                     label = locale;
604                 }
605                 var mi = document.createElement('menuitem');
606                 mi.setAttribute('label',label);
607                 mi.setAttribute('value',locale);
608                 if (locale == current_locale) {
609                     if (z.parentNode.tagName == 'menulist') {
610                         mi.setAttribute('selected','true');
611                         z.parentNode.setAttribute('label',label);
612                         z.parentNode.setAttribute('value',locale);
613                     }
614                 }
615                 z.appendChild( mi );
616             }
617         }
618         var xx = document.getElementById('apply_locale_btn');
619         xx.addEventListener(
620             'command',
621             function() {
622                 util.mozilla.change_locale(z.parentNode.value);
623             },
624             false
625         );
626
627         if ( found_ws_info_in_Achrome() && G.pref && G.pref.getBoolPref("open-ils.write_in_user_chrome_directory") ) {
628             //var hbox = x.parentNode; var b = document.createElement('button'); 
629             //b.setAttribute('label','Migrate legacy settings'); hbox.appendChild(b);
630             //b.addEventListener(
631             //    'command',
632             //    function() {
633             //        try {
634             //            handle_migration();
635             //        } catch(E) { alert(E); }
636             //    },
637             //    false
638             //);
639             if (window.confirm(offlineStrings.getString('main.settings.migrate'))) {
640                 setTimeout( function() { handle_migration(); }, 0 );
641             }
642         }
643
644         window.addEventListener(
645             'close',
646             function(ev) {
647
648                 G.data.stash_retrieve();
649                 if (typeof G.data.unsaved_data != 'undefined') {
650                     if (G.data.unsaved_data > 0) {
651                         var confirmation = window.confirm(offlineStrings.getString('menu.shutdown.unsaved_data_warning'));
652                         if (!confirmation) {
653                             ev.preventDefault();
654                             return false;
655                         }
656                     }
657                 }
658                 G.data.unsaved_data = 0;
659                 G.data.stash('unsaved_data');
660
661                 return true;
662
663             },
664             false
665         );
666
667
668     } catch(E) {
669         var error = offlineStrings.getFormattedString('common.exception', [E, '']);
670         try { G.error.sdump('D_ERROR',error); } catch(E) { dump(error); }
671         alert(error);
672     }
673     dump('exiting main_init()\n');
674 }
675
676 function found_ws_info_in_Achrome() {
677     JSAN.use('util.file');
678     var f = new util.file();
679     var f_in_chrome = f.get('ws_info','chrome');
680     var path = f_in_chrome.exists() ? f_in_chrome.path : false;
681     f.close();
682     return path;
683 }
684
685 function found_ws_info_in_Uchrome() {
686     JSAN.use('util.file');
687     var f = new util.file();
688     var f_in_uchrome = f.get('ws_info','uchrome');
689     var path = f_in_uchrome.exists() ? f_in_uchrome.path : false;
690     f.close();
691     return path;
692 }
693
694 function handle_migration() {
695     if ( found_ws_info_in_Uchrome() ) {
696         alert(offlineStrings.getFormattedString('main.settings.migrate.failed', [found_ws_info_in_Uchrome(), found_ws_info_in_Achrome()])
697         );
698     } else {
699         var dirService = Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties );
700         var f_new = dirService.get( "UChrm", Components.interfaces.nsIFile );
701         var f_old = dirService.get( "AChrom", Components.interfaces.nsIFile );
702         f_old.append(myPackageDir); f_old.append("content"); f_old.append("conf"); 
703         if (window.confirm(offlineStrings.getFormattedString("main.settings.migrate.confirm", [f_old.path, f_new.path]))) {
704             var files = f_old.directoryEntries;
705             while (files.hasMoreElements()) {
706                 var file = files.getNext();
707                 var file2 = file.QueryInterface( Components.interfaces.nsILocalFile );
708                 try {
709                     file2.moveTo( f_new, '' );
710                 } catch(E) {
711                     alert(offlineStrings.getFormattedString('main.settings.migrate.error', [file2.path, f_new.path]) + '\n');
712                 }
713             }
714             location.href = location.href; // huh?
715         }
716     }
717 }
718
719 dump('exiting main/main.js\n');