michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const C = Components.classes; michael@0: const I = Components.interfaces; michael@0: michael@0: const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1"; michael@0: michael@0: var gProfileService; michael@0: var gProfileManagerBundle; michael@0: michael@0: var gDefaultProfileParent; michael@0: var gOldProfileName; michael@0: michael@0: // The directory where the profile will be created. michael@0: var gProfileRoot; michael@0: michael@0: // Text node to display the location and name of the profile to create. michael@0: var gProfileDisplay; michael@0: michael@0: // Called once when the wizard is opened. michael@0: function initWizard() michael@0: { michael@0: try { michael@0: gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService); michael@0: gProfileManagerBundle = document.getElementById("bundle_profileManager"); michael@0: michael@0: var dirService = C["@mozilla.org/file/directory_service;1"].getService(I.nsIProperties); michael@0: gDefaultProfileParent = dirService.get("DefProfRt", I.nsIFile); michael@0: michael@0: gOldProfileName = document.getElementById("profileName").value; michael@0: michael@0: // Initialize the profile location display. michael@0: gProfileDisplay = document.getElementById("profileDisplay").firstChild; michael@0: setDisplayToDefaultFolder(); michael@0: } michael@0: catch(e) { michael@0: window.close(); michael@0: throw (e); michael@0: } michael@0: } michael@0: michael@0: // Called every time the second wizard page is displayed. michael@0: function initSecondWizardPage() michael@0: { michael@0: var profileName = document.getElementById("profileName"); michael@0: profileName.select(); michael@0: profileName.focus(); michael@0: michael@0: // Initialize profile name validation. michael@0: checkCurrentInput(profileName.value); michael@0: } michael@0: michael@0: const kSaltTable = [ michael@0: 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', michael@0: 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', michael@0: '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ]; michael@0: michael@0: var kSaltString = ""; michael@0: for (var i = 0; i < 8; ++i) { michael@0: kSaltString += kSaltTable[Math.floor(Math.random() * kSaltTable.length)]; michael@0: } michael@0: michael@0: michael@0: function saltName(aName) michael@0: { michael@0: return kSaltString + "." + aName; michael@0: } michael@0: michael@0: function setDisplayToDefaultFolder() michael@0: { michael@0: var defaultProfileDir = gDefaultProfileParent.clone(); michael@0: defaultProfileDir.append(saltName(document.getElementById("profileName").value)); michael@0: gProfileRoot = defaultProfileDir; michael@0: document.getElementById("useDefault").disabled = true; michael@0: } michael@0: michael@0: function updateProfileDisplay() michael@0: { michael@0: gProfileDisplay.data = gProfileRoot.path; michael@0: } michael@0: michael@0: // Invoke a folder selection dialog for choosing the directory of profile storage. michael@0: function chooseProfileFolder() michael@0: { michael@0: var newProfileRoot; michael@0: michael@0: var dirChooser = C["@mozilla.org/filepicker;1"].createInstance(I.nsIFilePicker); michael@0: dirChooser.init(window, gProfileManagerBundle.getString("chooseFolder"), michael@0: I.nsIFilePicker.modeGetFolder); michael@0: dirChooser.appendFilters(I.nsIFilePicker.filterAll); michael@0: michael@0: // default to the Profiles folder michael@0: dirChooser.displayDirectory = gDefaultProfileParent; michael@0: michael@0: dirChooser.show(); michael@0: newProfileRoot = dirChooser.file; michael@0: michael@0: // Disable the "Default Folder..." button when the default profile folder michael@0: // was selected manually in the File Picker. michael@0: document.getElementById("useDefault").disabled = michael@0: (newProfileRoot.parent.equals(gDefaultProfileParent)); michael@0: michael@0: gProfileRoot = newProfileRoot; michael@0: updateProfileDisplay(); michael@0: } michael@0: michael@0: // Checks the current user input for validity and triggers an error message accordingly. michael@0: function checkCurrentInput(currentInput) michael@0: { michael@0: var finishButton = document.documentElement.getButton("finish"); michael@0: var finishText = document.getElementById("finishText"); michael@0: var canAdvance; michael@0: michael@0: var errorMessage = checkProfileName(currentInput); michael@0: michael@0: if (!errorMessage) { michael@0: finishText.className = ""; michael@0: #ifndef XP_MACOSX michael@0: finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishText"); michael@0: #else michael@0: finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishTextMac"); michael@0: #endif michael@0: canAdvance = true; michael@0: } michael@0: else { michael@0: finishText.className = "error"; michael@0: finishText.firstChild.data = errorMessage; michael@0: canAdvance = false; michael@0: } michael@0: michael@0: document.documentElement.canAdvance = canAdvance; michael@0: finishButton.disabled = !canAdvance; michael@0: michael@0: updateProfileDisplay(); michael@0: } michael@0: michael@0: function updateProfileName(aNewName) { michael@0: checkCurrentInput(aNewName); michael@0: michael@0: var re = new RegExp("^[a-z0-9]{8}\\." + michael@0: gOldProfileName.replace(/[|^$()\[\]{}\\+?.*]/g, "\\$&") michael@0: + '$'); michael@0: michael@0: if (re.test(gProfileRoot.leafName)) { michael@0: gProfileRoot.leafName = saltName(aNewName); michael@0: updateProfileDisplay(); michael@0: } michael@0: gOldProfileName = aNewName; michael@0: } michael@0: michael@0: // Checks whether the given string is a valid profile name. michael@0: // Returns an error message describing the error in the name or "" when it's valid. michael@0: function checkProfileName(profileNameToCheck) michael@0: { michael@0: // Check for emtpy profile name. michael@0: if (!/\S/.test(profileNameToCheck)) michael@0: return gProfileManagerBundle.getString("profileNameEmpty"); michael@0: michael@0: // Check whether all characters in the profile name are allowed. michael@0: if (/([\\*:?<>|\/\"])/.test(profileNameToCheck)) michael@0: return gProfileManagerBundle.getFormattedString("invalidChar", [RegExp.$1]); michael@0: michael@0: // Check whether a profile with the same name already exists. michael@0: if (profileExists(profileNameToCheck)) michael@0: return gProfileManagerBundle.getString("profileExists"); michael@0: michael@0: // profileNameToCheck is valid. michael@0: return ""; michael@0: } michael@0: michael@0: function profileExists(aName) michael@0: { michael@0: var profiles = gProfileService.profiles; michael@0: while (profiles.hasMoreElements()) { michael@0: var profile = profiles.getNext().QueryInterface(I.nsIToolkitProfile); michael@0: if (profile.name.toLowerCase() == aName.toLowerCase()) michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // Called when the first wizard page is shown. michael@0: function enableNextButton() michael@0: { michael@0: document.documentElement.canAdvance = true; michael@0: } michael@0: michael@0: function onFinish() michael@0: { michael@0: var profileName = document.getElementById("profileName").value; michael@0: var profile; michael@0: michael@0: // Create profile named profileName in profileRoot. michael@0: try { michael@0: profile = gProfileService.createProfile(gProfileRoot, profileName); michael@0: } michael@0: catch (e) { michael@0: var profileCreationFailed = michael@0: gProfileManagerBundle.getString("profileCreationFailed"); michael@0: var profileCreationFailedTitle = michael@0: gProfileManagerBundle.getString("profileCreationFailedTitle"); michael@0: var promptService = C["@mozilla.org/embedcomp/prompt-service;1"]. michael@0: getService(I.nsIPromptService); michael@0: promptService.alert(window, profileCreationFailedTitle, michael@0: profileCreationFailed + "\n" + e); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: // window.opener is false if the Create Profile Wizard was opened from the michael@0: // command line. michael@0: if (window.opener) { michael@0: // Add new profile to the list in the Profile Manager. michael@0: window.opener.CreateProfile(profile); michael@0: } michael@0: else { michael@0: // Use the newly created Profile. michael@0: var profileLock = profile.lock(null); michael@0: michael@0: var dialogParams = window.arguments[0].QueryInterface(I.nsIDialogParamBlock); michael@0: dialogParams.objects.insertElementAt(profileLock, 0, false); michael@0: } michael@0: michael@0: // Exit the wizard. michael@0: return true; michael@0: }