home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / toolkit.jar / content / mozapps / profile / createProfileWizard.js next >
Encoding:
Text File  |  2006-04-03  |  8.4 KB  |  265 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code, released
  15.  * March 31, 1998.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998-1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Ben Goodger (30/09/99)
  24.  *   Brant Gurganus (23/03/03)
  25.  *   Stefan Borggraefe (17/10/03)
  26.  *   Benjamin Smedberg <bsmedberg@covad.net> - 8-Apr-2004
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the MPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the MPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41.  
  42. const C = Components.classes;
  43. const I = Components.interfaces;
  44.  
  45. const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
  46.  
  47. var gProfileService;
  48. var gProfileManagerBundle;
  49.  
  50. var gDefaultProfileParent;
  51. var gOldProfileName;
  52.  
  53. // The directory where the profile will be created.
  54. var gProfileRoot;
  55.  
  56. // Text node to display the location and name of the profile to create.
  57. var gProfileDisplay;
  58.  
  59. // Called once when the wizard is opened.
  60. function initWizard()
  61.   try {
  62.     gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
  63.     gProfileManagerBundle = document.getElementById("bundle_profileManager");
  64.  
  65.     var dirService = C["@mozilla.org/file/directory_service;1"].getService(I.nsIProperties);
  66.     gDefaultProfileParent = dirService.get("DefProfRt", I.nsIFile);
  67.  
  68.     gOldProfileName = document.getElementById("profileName").value;
  69.  
  70.     // Initialize the profile location display.
  71.     gProfileDisplay = document.getElementById("profileDisplay").firstChild;
  72.     setDisplayToDefaultFolder();
  73.   }
  74.   catch(e) {
  75.     window.close();
  76.     throw (e);
  77.   }
  78. }
  79.  
  80. // Called every time the second wizard page is displayed.
  81. function initSecondWizardPage() 
  82. {
  83.   var profileName = document.getElementById("profileName");
  84.   profileName.select();
  85.   profileName.focus();
  86.  
  87.   // Initialize profile name validation.
  88.   checkCurrentInput(profileName.value);
  89. }
  90.  
  91. const kSaltTable = [
  92.   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  93.   'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  94.   '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ];
  95.  
  96. var kSaltString = "";
  97. for (var i = 0; i < 8; ++i) {
  98.   kSaltString += kSaltTable[Math.floor(Math.random() * kSaltTable.length)];
  99. }
  100.  
  101.  
  102. function saltName(aName)
  103. {
  104.   return kSaltString + "." + aName;
  105. }
  106.  
  107. function setDisplayToDefaultFolder()
  108. {
  109.   var defaultProfileDir = gDefaultProfileParent.clone();
  110.   defaultProfileDir.append(saltName(document.getElementById("profileName").value));
  111.   gProfileRoot = defaultProfileDir;
  112.   document.getElementById("useDefault").disabled = true;
  113. }
  114.  
  115. function updateProfileDisplay()
  116. {
  117.   gProfileDisplay.data = gProfileRoot.path;
  118. }
  119.  
  120. // Invoke a folder selection dialog for choosing the directory of profile storage.
  121. function chooseProfileFolder()
  122. {
  123.   var newProfileRoot;
  124.   
  125.   var dirChooser = C["@mozilla.org/filepicker;1"].createInstance(I.nsIFilePicker);
  126.   dirChooser.init(window, gProfileManagerBundle.getString("chooseFolder"),
  127.                   I.nsIFilePicker.modeGetFolder);
  128.   dirChooser.appendFilters(I.nsIFilePicker.filterAll);
  129.  
  130.   // default to the Profiles folder
  131.   dirChooser.displayDirectory = gDefaultProfileParent;
  132.  
  133.   dirChooser.show();
  134.   newProfileRoot = dirChooser.file;
  135.  
  136.   // Disable the "Default Folder..." button when the default profile folder
  137.   // was selected manually in the File Picker.
  138.   document.getElementById("useDefault").disabled =
  139.     (newProfileRoot.parent.equals(gDefaultProfileParent));
  140.  
  141.   gProfileRoot = newProfileRoot;
  142.   updateProfileDisplay();
  143. }
  144.  
  145. // Checks the current user input for validity and triggers an error message accordingly.
  146. function checkCurrentInput(currentInput)
  147. {
  148.   var finishButton = document.documentElement.getButton("finish");
  149.   var finishText = document.getElementById("finishText");
  150.   var canAdvance;
  151.  
  152.   var errorMessage = checkProfileName(currentInput);
  153.  
  154.   if (!errorMessage) {
  155.     finishText.className = "";
  156. //@line 158 "/build/buildd/xulrunner-1.9-1.9.0.14+build2+nobinonly/mozilla/toolkit/profile/content/createProfileWizard.js"
  157.     finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishText");
  158. //@line 162 "/build/buildd/xulrunner-1.9-1.9.0.14+build2+nobinonly/mozilla/toolkit/profile/content/createProfileWizard.js"
  159.     canAdvance = true;
  160.   }
  161.   else {
  162.     finishText.className = "error";
  163.     finishText.firstChild.data = errorMessage;
  164.     canAdvance = false;
  165.   }
  166.  
  167.   document.documentElement.canAdvance = canAdvance;
  168.   finishButton.disabled = !canAdvance;
  169.  
  170.   updateProfileDisplay();
  171. }
  172.  
  173. function updateProfileName(aNewName) {
  174.   checkCurrentInput(aNewName);
  175.  
  176.   var re = new RegExp("^[a-z0-9]{8}\\." +
  177.                       gOldProfileName.replace(/[|^$()\[\]{}\\+?.*]/g, "\\$&")
  178.                       + '$');
  179.  
  180.   if (re.test(gProfileRoot.leafName)) {
  181.     gProfileRoot.leafName = saltName(aNewName);
  182.     updateProfileDisplay();
  183.   }
  184.   gOldProfileName = aNewName;
  185. }
  186.  
  187. // Checks whether the given string is a valid profile name.
  188. // Returns an error message describing the error in the name or "" when it's valid.
  189. function checkProfileName(profileNameToCheck)
  190. {
  191.   // Check for emtpy profile name.
  192.   if (!/\S/.test(profileNameToCheck))
  193.     return gProfileManagerBundle.getString("profileNameEmpty");
  194.  
  195.   // Check whether all characters in the profile name are allowed.
  196.   if (/([\\*:?<>|\/\"])/.test(profileNameToCheck))
  197.     return gProfileManagerBundle.getFormattedString("invalidChar", [RegExp.$1]);
  198.  
  199.   // Check whether a profile with the same name already exists.
  200.   if (profileExists(profileNameToCheck))
  201.     return gProfileManagerBundle.getString("profileExists");
  202.  
  203.   // profileNameToCheck is valid.
  204.   return "";
  205. }
  206.  
  207. function profileExists(aName)
  208. {
  209.   var profiles = gProfileService.profiles;
  210.   while (profiles.hasMoreElements()) {
  211.     var profile = profiles.getNext().QueryInterface(I.nsIToolkitProfile);
  212.     if (profile.name.toLowerCase() == aName.toLowerCase())
  213.       return true;
  214.   }
  215.  
  216.   return false;
  217. }
  218.  
  219. // Called when the first wizard page is shown.
  220. function enableNextButton()
  221. {
  222.   document.documentElement.canAdvance = true;
  223. }
  224.  
  225. function onFinish() 
  226. {
  227.   var profileName = document.getElementById("profileName").value;
  228.   var profile;
  229.  
  230.   // Create profile named profileName in profileRoot.
  231.   try {
  232.     profile = gProfileService.createProfile(gProfileRoot, null, profileName);
  233.   }
  234.   catch (e) {
  235.     var profileCreationFailed =
  236.       gProfileManagerBundle.getString("profileCreationFailed");
  237.     var profileCreationFailedTitle =
  238.       gProfileManagerBundle.getString("profileCreationFailedTitle");
  239.     var promptService = C["@mozilla.org/embedcomp/prompt-service;1"].
  240.       getService(I.nsIPromptService);
  241.     promptService.alert(window, profileCreationFailedTitle,
  242.                         profileCreationFailed + "\n" + e);
  243.  
  244.     return false;
  245.   }
  246.  
  247.   // window.opener is false if the Create Profile Wizard was opened from the
  248.   // command line.
  249.   if (window.opener) {
  250.     // Add new profile to the list in the Profile Manager.
  251.     window.opener.CreateProfile(profile);
  252.   }
  253.   else {
  254.     // Use the newly created Profile.
  255.     var profileLock = profile.lock(null);
  256.  
  257.     var dialogParams = window.arguments[0].QueryInterface(I.nsIDialogParamBlock);
  258.     dialogParams.objects.insertElementAt(profileLock, 0, false);
  259.   }
  260.  
  261.   // Exit the wizard.
  262.   return true;
  263. }
  264.