home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / profile / profileSelection.js < prev    next >
Text File  |  2003-06-08  |  11KB  |  295 lines

  1. /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released
  14.  * March 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape
  17.  * Communications Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Ben Goodger (03/01/00)
  23.  *   Seth Spitzer (28/10/99)
  24.  *   Dan Veditz <dveditz@netscape.com>
  25.  */
  26.  
  27. var gDialogParams;
  28. var gStartupMode; // TRUE if we're being shown at app startup, FALSE if from menu.
  29. var gProfileManagerBundle;
  30. var gBrandBundle;
  31. var profile     = Components.classes["@mozilla.org/profile/manager;1"].getService();
  32. if (profile)
  33.   profile       = profile.QueryInterface(Components.interfaces.nsIProfileInternal);
  34.  
  35. var Registry;
  36.  
  37. function StartUp()
  38. {
  39.   gDialogParams = window.arguments[0].
  40.                     QueryInterface(Components.interfaces.nsIDialogParamBlock);
  41.   gStartupMode = (gDialogParams.GetString(0) == "startup");
  42.   
  43.   gProfileManagerBundle = document.getElementById("bundle_profileManager");
  44.   gBrandBundle = document.getElementById("bundle_brand");
  45.  
  46.   SetUpOKCancelButtons();
  47.   centerWindowOnScreen();
  48.   if(window.location && window.location.search && window.location.search == "?manage=true" )
  49.     SwitchProfileManagerMode();
  50.     
  51.   // Set up the intro text, depending on our context
  52.   var introTextItem = document.getElementById("intro");
  53.   var introText, insertText;
  54.   if (gStartupMode) {
  55.     insertText = gProfileManagerBundle.getFormattedString("startButton",
  56.                                     [gBrandBundle.getString("brandShortName")]);
  57.     introText = gProfileManagerBundle.getFormattedString("intro_start",
  58.                     [insertText]);
  59.   }
  60.   else {
  61.     insertText = gProfileManagerBundle.getString("selectButton");
  62.     introText = gProfileManagerBundle.getFormattedString("intro_switch",
  63.                     [insertText]);
  64.   }
  65.   introTextItem.childNodes[0].nodeValue = introText;
  66.  
  67.   var dirServ = Components.classes['@mozilla.org/file/directory_service;1'].createInstance();
  68.   dirServ = dirServ.QueryInterface(Components.interfaces.nsIProperties);
  69.  
  70.   // "AggRegF" stands for Application Registry File.
  71.   // Forgive the weird name directory service has adapted for
  72.   // application registry file....
  73.   var regFile = dirServ.get("AppRegF", Components.interfaces.nsIFile);
  74.  
  75.   Registry = Components.classes['@mozilla.org/registry;1'].createInstance();
  76.   Registry = Registry.QueryInterface(Components.interfaces.nsIRegistry);
  77.   Registry.open(regFile);
  78.  
  79.   // get new profile registry & users location and dump it to console
  80.   // to let users know about it.
  81.   var regFolder = dirServ.get("AppRegD", Components.interfaces.nsIFile);
  82.   dump("New location for profile registry and user profile directories is -> " + regFolder.path + "\n");
  83.  
  84.   loadElements();
  85.   highlightCurrentProfile();
  86.  
  87.   var offlineState = document.getElementById("offlineState");
  88.   if (gStartupMode) {
  89.     // set the checkbox to reflect the current state.
  90.     var ioService = Components.classes["@mozilla.org/network/io-service;1"].
  91.                       getService(Components.interfaces.nsIIOService);
  92.     offlineState.checked = ioService.offline;
  93.   }
  94.   else {
  95.     // hide the checkbox in this context
  96.     offlineState.hidden = true;
  97.   }
  98.  
  99.   var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
  100.   autoSelectLastProfile.checked = profile.startWithLastUsedProfile;
  101.  
  102.   var profileList = document.getElementById("profiles");
  103.   profileList.focus();
  104.  
  105.   DoEnabling();
  106. }
  107.  
  108. // select the last opened profile in the profile list
  109. function highlightCurrentProfile()
  110. {
  111.   try {
  112.     var currentProfile = profile.currentProfile;
  113.     if( !currentProfile )
  114.       return;
  115.     var currentProfileItem = document.getElementById( ( "profileName_" + currentProfile ) );
  116.     var profileList = document.getElementById( "profiles" );
  117.     if( currentProfileItem ) {
  118.       profileList.selectItem( currentProfileItem );
  119.       profileList.ensureElementIsVisible( currentProfileItem );
  120.     }
  121.   }
  122.   catch(e) {
  123.     dump("*** failed to select current profile in list\n");
  124.   }
  125. }
  126.  
  127. // function : <profileSelection.js>::AddItem();
  128. // purpose  : utility function for adding items to a listbox.
  129. function AddItem( aChildren, aProfileObject )
  130. {
  131.   var kids    = document.getElementById(aChildren);
  132.   var listitem = document.createElement("listitem");
  133.   listitem.setAttribute("label", aProfileObject.mName );
  134.   listitem.setAttribute("rowMigrate",  aProfileObject.mMigrated );
  135.   listitem.setAttribute("class", "listitem-iconic");
  136.   listitem.setAttribute("profile_name", aProfileObject.mName );
  137.   listitem.setAttribute("rowName", aProfileObject.mName );
  138.   listitem.setAttribute("id", ( "profileName_" + aProfileObject.mName ) );
  139.   // 23/10/99 - no roaming access yet!
  140.   //  var roaming = document.getElementById("roamingitem");
  141.   //  kids.insertBefore(item,roaming);
  142.   kids.appendChild(listitem);
  143.   return listitem;
  144. }
  145.  
  146. function Profile ( aName, aMigrated )
  147. {
  148.   this.mName       = aName ? aName : null;
  149.   this.mMigrated   = aMigrated ? aMigrated : null;
  150. }
  151.  
  152. // function : <profileSelection.js>::loadElements();
  153. // purpose  : load profiles into listbox
  154. function loadElements()
  155. {
  156.   try {
  157.     var profileRoot = Registry.getKey(Registry.Common, "Profiles");
  158.     var regEnum = Registry.enumerateSubtrees( profileRoot );
  159.  
  160.     // the registry class is old and uses sucky nsIEnumerator instead
  161.     // of nsISimpleEnumerator. We'll rely on a blank profile name to
  162.     // throw us out of the while loop for now.
  163.     regEnum.first();
  164.     while (true)
  165.     {
  166.       var node = regEnum.currentItem();
  167.       node = node.QueryInterface(Components.interfaces.nsIRegistryNode);
  168.  
  169.       if ( node.name == "" )
  170.         break;
  171.  
  172.       var migrated = Registry.getString( node.key, "migrated" );
  173.  
  174.       AddItem( "profiles", new Profile( node.name, migrated ) );
  175.  
  176.       regEnum.next();
  177.     }
  178.   }
  179.   catch (e) {}
  180. }
  181.  
  182.  
  183. // function : <profileSelection.js>::onStart();
  184. // purpose  : sets the current profile to the selected profile (user choice: "Start Mozilla")
  185. function onStart()
  186. {
  187.   var profileList = document.getElementById("profiles");
  188.   var selected = profileList.selectedItems[0];
  189.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].
  190.     getService(Components.interfaces.nsIPromptService);
  191.  
  192.   var profilename = selected.getAttribute("profile_name");
  193.   if( selected.getAttribute("rowMigrate") == "no" ) {
  194.     var lString = gProfileManagerBundle.getString("migratebeforestart");
  195.     lString = lString.replace(/\s*<html:br\/>/g,"\n");
  196.     lString = lString.replace(/%brandShortName%/gi,
  197.                               gBrandBundle.getString("brandShortName"));
  198.     var title = gProfileManagerBundle.getString("migratetitle");
  199.  
  200.     if (promptService.confirm(window, title, lString)) {
  201.       var profileDir = profile.getProfileDir(profilename);
  202.       if (profileDir) {
  203.         profileDir = profileDir.QueryInterface( Components.interfaces.nsIFile );
  204.         if (profileDir) {
  205.           if (!profileDir.exists()) {
  206.             var errorMessage = gProfileManagerBundle.getString("sourceProfileDirMissing");
  207.             var profileDirMissingTitle = gProfileManagerBundle.getString("sourceProfileDirMissingTitle");
  208.             promptService.alert(window, profileDirMissingTitle, errorMessage);
  209.               return false;          
  210.           }
  211.         }
  212.       }      
  213.       profile.migrateProfile( profilename );
  214.     }
  215.     else
  216.       return false;
  217.   }
  218.  
  219.   // start in online or offline mode
  220.   if (gStartupMode) {
  221.     var offlineState = document.getElementById("offlineState");
  222.     var ioService = Components.classes["@mozilla.org/network/io-service;1"].
  223.                       getService(Components.interfaces.nsIIOService);
  224.     if (offlineState.checked != ioService.offline)
  225.       ioService.offline = offlineState.checked;
  226.   }
  227.   
  228.   var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
  229.   profile.startWithLastUsedProfile = autoSelectLastProfile.checked;
  230.   
  231.   try {
  232.     profile.currentProfile = profilename;
  233.   }
  234.   catch (ex) {
  235.       var brandName = gBrandBundle.getString("brandShortName");    
  236.     var message;
  237.     switch (ex.result) {
  238.       case Components.results.NS_ERROR_FILE_ACCESS_DENIED:
  239.         message = gProfileManagerBundle.getFormattedString("profDirLocked", [brandName, profilename]);
  240.         message = message.replace(/\s*<html:br\/>/g,"\n");
  241.         break;
  242.       case Components.results.NS_ERROR_FILE_NOT_FOUND:
  243.         message = gProfileManagerBundle.getFormattedString("profDirMissing", [brandName, profilename]);
  244.         message = message.replace(/\s*<html:br\/>/g,"\n");
  245.         break;
  246.       default:
  247.         message = ex.message;
  248.         break;
  249.   }
  250.       promptService.alert(window, null, message);
  251.       return false;
  252.   }
  253.   
  254.   gDialogParams.SetInt(0, 1); // 1 == success
  255.     
  256.   return true;
  257. }
  258.  
  259. // function : <profileSelection.js>::onExit();
  260. // purpose  : quits startup process (User Choice: "Exit")
  261. function onExit()
  262. {
  263.   gDialogParams.SetInt(0, 0); // 0 == cancel
  264.   return true;
  265. }
  266.  
  267. function SetUpOKCancelButtons()
  268. {
  269.   doSetOKCancel( onStart, onExit, null, null );
  270.   var okButton = document.getElementById("ok");
  271.   var cancelButton = document.getElementById("cancel");
  272.  
  273.   var okButtonString;
  274.   var cancelButtonString;
  275.   
  276.   try {    
  277.     if (gStartupMode) {
  278.       okButtonString = gProfileManagerBundle.getFormattedString("startButton",
  279.                                     [gBrandBundle.getString("brandShortName")]);
  280.       cancelButtonString = gProfileManagerBundle.getString("exitButton");
  281.     }
  282.     else {
  283.       okButtonString = gProfileManagerBundle.getString("selectButton");
  284.       cancelButtonString = gProfileManagerBundle.getString("cancel");
  285.     }
  286.   } catch (e) {
  287.     okButtonString = "Start Yah";
  288.     cancelButtonString = "Exit Yah";
  289.   }
  290.  
  291.   okButton.setAttribute( "label", okButtonString );
  292.   okButton.setAttribute( "class", ( okButton.getAttribute("class") + " padded" ) );
  293.   cancelButton.setAttribute( "label", cancelButtonString );
  294. }
  295.