toolkit/profile/content/createProfileWizard.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/profile/content/createProfileWizard.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,229 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +const C = Components.classes;
     1.9 +const I = Components.interfaces;
    1.10 +
    1.11 +const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
    1.12 +
    1.13 +var gProfileService;
    1.14 +var gProfileManagerBundle;
    1.15 +
    1.16 +var gDefaultProfileParent;
    1.17 +var gOldProfileName;
    1.18 +
    1.19 +// The directory where the profile will be created.
    1.20 +var gProfileRoot;
    1.21 +
    1.22 +// Text node to display the location and name of the profile to create.
    1.23 +var gProfileDisplay;
    1.24 +
    1.25 +// Called once when the wizard is opened.
    1.26 +function initWizard()
    1.27 +{ 
    1.28 +  try {
    1.29 +    gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
    1.30 +    gProfileManagerBundle = document.getElementById("bundle_profileManager");
    1.31 +
    1.32 +    var dirService = C["@mozilla.org/file/directory_service;1"].getService(I.nsIProperties);
    1.33 +    gDefaultProfileParent = dirService.get("DefProfRt", I.nsIFile);
    1.34 +
    1.35 +    gOldProfileName = document.getElementById("profileName").value;
    1.36 +
    1.37 +    // Initialize the profile location display.
    1.38 +    gProfileDisplay = document.getElementById("profileDisplay").firstChild;
    1.39 +    setDisplayToDefaultFolder();
    1.40 +  }
    1.41 +  catch(e) {
    1.42 +    window.close();
    1.43 +    throw (e);
    1.44 +  }
    1.45 +}
    1.46 +
    1.47 +// Called every time the second wizard page is displayed.
    1.48 +function initSecondWizardPage() 
    1.49 +{
    1.50 +  var profileName = document.getElementById("profileName");
    1.51 +  profileName.select();
    1.52 +  profileName.focus();
    1.53 +
    1.54 +  // Initialize profile name validation.
    1.55 +  checkCurrentInput(profileName.value);
    1.56 +}
    1.57 +
    1.58 +const kSaltTable = [
    1.59 +  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    1.60 +  'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    1.61 +  '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ];
    1.62 +
    1.63 +var kSaltString = "";
    1.64 +for (var i = 0; i < 8; ++i) {
    1.65 +  kSaltString += kSaltTable[Math.floor(Math.random() * kSaltTable.length)];
    1.66 +}
    1.67 +
    1.68 +
    1.69 +function saltName(aName)
    1.70 +{
    1.71 +  return kSaltString + "." + aName;
    1.72 +}
    1.73 +
    1.74 +function setDisplayToDefaultFolder()
    1.75 +{
    1.76 +  var defaultProfileDir = gDefaultProfileParent.clone();
    1.77 +  defaultProfileDir.append(saltName(document.getElementById("profileName").value));
    1.78 +  gProfileRoot = defaultProfileDir;
    1.79 +  document.getElementById("useDefault").disabled = true;
    1.80 +}
    1.81 +
    1.82 +function updateProfileDisplay()
    1.83 +{
    1.84 +  gProfileDisplay.data = gProfileRoot.path;
    1.85 +}
    1.86 +
    1.87 +// Invoke a folder selection dialog for choosing the directory of profile storage.
    1.88 +function chooseProfileFolder()
    1.89 +{
    1.90 +  var newProfileRoot;
    1.91 +  
    1.92 +  var dirChooser = C["@mozilla.org/filepicker;1"].createInstance(I.nsIFilePicker);
    1.93 +  dirChooser.init(window, gProfileManagerBundle.getString("chooseFolder"),
    1.94 +                  I.nsIFilePicker.modeGetFolder);
    1.95 +  dirChooser.appendFilters(I.nsIFilePicker.filterAll);
    1.96 +
    1.97 +  // default to the Profiles folder
    1.98 +  dirChooser.displayDirectory = gDefaultProfileParent;
    1.99 +
   1.100 +  dirChooser.show();
   1.101 +  newProfileRoot = dirChooser.file;
   1.102 +
   1.103 +  // Disable the "Default Folder..." button when the default profile folder
   1.104 +  // was selected manually in the File Picker.
   1.105 +  document.getElementById("useDefault").disabled =
   1.106 +    (newProfileRoot.parent.equals(gDefaultProfileParent));
   1.107 +
   1.108 +  gProfileRoot = newProfileRoot;
   1.109 +  updateProfileDisplay();
   1.110 +}
   1.111 +
   1.112 +// Checks the current user input for validity and triggers an error message accordingly.
   1.113 +function checkCurrentInput(currentInput)
   1.114 +{
   1.115 +  var finishButton = document.documentElement.getButton("finish");
   1.116 +  var finishText = document.getElementById("finishText");
   1.117 +  var canAdvance;
   1.118 +
   1.119 +  var errorMessage = checkProfileName(currentInput);
   1.120 +
   1.121 +  if (!errorMessage) {
   1.122 +    finishText.className = "";
   1.123 +#ifndef XP_MACOSX
   1.124 +    finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishText");
   1.125 +#else
   1.126 +    finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishTextMac");
   1.127 +#endif
   1.128 +    canAdvance = true;
   1.129 +  }
   1.130 +  else {
   1.131 +    finishText.className = "error";
   1.132 +    finishText.firstChild.data = errorMessage;
   1.133 +    canAdvance = false;
   1.134 +  }
   1.135 +
   1.136 +  document.documentElement.canAdvance = canAdvance;
   1.137 +  finishButton.disabled = !canAdvance;
   1.138 +
   1.139 +  updateProfileDisplay();
   1.140 +}
   1.141 +
   1.142 +function updateProfileName(aNewName) {
   1.143 +  checkCurrentInput(aNewName);
   1.144 +
   1.145 +  var re = new RegExp("^[a-z0-9]{8}\\." +
   1.146 +                      gOldProfileName.replace(/[|^$()\[\]{}\\+?.*]/g, "\\$&")
   1.147 +                      + '$');
   1.148 +
   1.149 +  if (re.test(gProfileRoot.leafName)) {
   1.150 +    gProfileRoot.leafName = saltName(aNewName);
   1.151 +    updateProfileDisplay();
   1.152 +  }
   1.153 +  gOldProfileName = aNewName;
   1.154 +}
   1.155 +
   1.156 +// Checks whether the given string is a valid profile name.
   1.157 +// Returns an error message describing the error in the name or "" when it's valid.
   1.158 +function checkProfileName(profileNameToCheck)
   1.159 +{
   1.160 +  // Check for emtpy profile name.
   1.161 +  if (!/\S/.test(profileNameToCheck))
   1.162 +    return gProfileManagerBundle.getString("profileNameEmpty");
   1.163 +
   1.164 +  // Check whether all characters in the profile name are allowed.
   1.165 +  if (/([\\*:?<>|\/\"])/.test(profileNameToCheck))
   1.166 +    return gProfileManagerBundle.getFormattedString("invalidChar", [RegExp.$1]);
   1.167 +
   1.168 +  // Check whether a profile with the same name already exists.
   1.169 +  if (profileExists(profileNameToCheck))
   1.170 +    return gProfileManagerBundle.getString("profileExists");
   1.171 +
   1.172 +  // profileNameToCheck is valid.
   1.173 +  return "";
   1.174 +}
   1.175 +
   1.176 +function profileExists(aName)
   1.177 +{
   1.178 +  var profiles = gProfileService.profiles;
   1.179 +  while (profiles.hasMoreElements()) {
   1.180 +    var profile = profiles.getNext().QueryInterface(I.nsIToolkitProfile);
   1.181 +    if (profile.name.toLowerCase() == aName.toLowerCase())
   1.182 +      return true;
   1.183 +  }
   1.184 +
   1.185 +  return false;
   1.186 +}
   1.187 +
   1.188 +// Called when the first wizard page is shown.
   1.189 +function enableNextButton()
   1.190 +{
   1.191 +  document.documentElement.canAdvance = true;
   1.192 +}
   1.193 +
   1.194 +function onFinish() 
   1.195 +{
   1.196 +  var profileName = document.getElementById("profileName").value;
   1.197 +  var profile;
   1.198 +
   1.199 +  // Create profile named profileName in profileRoot.
   1.200 +  try {
   1.201 +    profile = gProfileService.createProfile(gProfileRoot, profileName);
   1.202 +  }
   1.203 +  catch (e) {
   1.204 +    var profileCreationFailed =
   1.205 +      gProfileManagerBundle.getString("profileCreationFailed");
   1.206 +    var profileCreationFailedTitle =
   1.207 +      gProfileManagerBundle.getString("profileCreationFailedTitle");
   1.208 +    var promptService = C["@mozilla.org/embedcomp/prompt-service;1"].
   1.209 +      getService(I.nsIPromptService);
   1.210 +    promptService.alert(window, profileCreationFailedTitle,
   1.211 +                        profileCreationFailed + "\n" + e);
   1.212 +
   1.213 +    return false;
   1.214 +  }
   1.215 +
   1.216 +  // window.opener is false if the Create Profile Wizard was opened from the
   1.217 +  // command line.
   1.218 +  if (window.opener) {
   1.219 +    // Add new profile to the list in the Profile Manager.
   1.220 +    window.opener.CreateProfile(profile);
   1.221 +  }
   1.222 +  else {
   1.223 +    // Use the newly created Profile.
   1.224 +    var profileLock = profile.lock(null);
   1.225 +
   1.226 +    var dialogParams = window.arguments[0].QueryInterface(I.nsIDialogParamBlock);
   1.227 +    dialogParams.objects.insertElementAt(profileLock, 0, false);
   1.228 +  }
   1.229 +
   1.230 +  // Exit the wizard.
   1.231 +  return true;
   1.232 +}

mercurial