michael@0: /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * 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: Components.utils.import("resource://gre/modules/Services.jsm"); 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 gDialogParams; michael@0: var gProfileManagerBundle; michael@0: var gBrandBundle; michael@0: var gProfileService; michael@0: michael@0: function startup() michael@0: { michael@0: try { michael@0: gDialogParams = window.arguments[0]. michael@0: QueryInterface(I.nsIDialogParamBlock); michael@0: michael@0: gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService); michael@0: michael@0: gProfileManagerBundle = document.getElementById("bundle_profileManager"); michael@0: gBrandBundle = document.getElementById("bundle_brand"); michael@0: michael@0: document.documentElement.centerWindowOnScreen(); michael@0: michael@0: var profilesElement = document.getElementById("profiles"); michael@0: michael@0: var profileList = gProfileService.profiles; michael@0: while (profileList.hasMoreElements()) { michael@0: var profile = profileList.getNext().QueryInterface(I.nsIToolkitProfile); michael@0: michael@0: var listitem = profilesElement.appendItem(profile.name, ""); michael@0: michael@0: var tooltiptext = michael@0: gProfileManagerBundle.getFormattedString("profileTooltip", [profile.name, profile.rootDir.path]); michael@0: listitem.setAttribute("tooltiptext", tooltiptext); michael@0: listitem.setAttribute("class", "listitem-iconic"); michael@0: listitem.profile = profile; michael@0: try { michael@0: if (profile === gProfileService.selectedProfile) { michael@0: setTimeout(function(a) { michael@0: profilesElement.ensureElementIsVisible(a); michael@0: profilesElement.selectItem(a); michael@0: }, 0, listitem); michael@0: } michael@0: } michael@0: catch(e) { } michael@0: } michael@0: michael@0: var autoSelectLastProfile = document.getElementById("autoSelectLastProfile"); michael@0: autoSelectLastProfile.checked = gProfileService.startWithLastProfile; michael@0: profilesElement.focus(); michael@0: } michael@0: catch(e) { michael@0: window.close(); michael@0: throw (e); michael@0: } michael@0: } michael@0: michael@0: function acceptDialog() michael@0: { michael@0: var appName = gBrandBundle.getString("brandShortName"); michael@0: michael@0: var profilesElement = document.getElementById("profiles"); michael@0: var selectedProfile = profilesElement.selectedItem; michael@0: if (!selectedProfile) { michael@0: var pleaseSelectTitle = gProfileManagerBundle.getString("pleaseSelectTitle"); michael@0: var pleaseSelect = michael@0: gProfileManagerBundle.getFormattedString("pleaseSelect", [appName]); michael@0: Services.prompt.alert(window, pleaseSelectTitle, pleaseSelect); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: var profileLock; michael@0: michael@0: try { michael@0: profileLock = selectedProfile.profile.lock({ value: null }); michael@0: } michael@0: catch (e) { michael@0: if (!selectedProfile.profile.rootDir.exists()) { michael@0: var missingTitle = gProfileManagerBundle.getString("profileMissingTitle"); michael@0: var missing = michael@0: gProfileManagerBundle.getFormattedString("profileMissing", [appName]); michael@0: Services.prompt.alert(window, missingTitle, missing); michael@0: return false; michael@0: } michael@0: michael@0: var lockedTitle = gProfileManagerBundle.getString("profileLockedTitle"); michael@0: var locked = michael@0: gProfileManagerBundle.getFormattedString("profileLocked2", [appName, selectedProfile.profile.name, appName]); michael@0: Services.prompt.alert(window, lockedTitle, locked); michael@0: michael@0: return false; michael@0: } michael@0: gDialogParams.objects.insertElementAt(profileLock.nsIProfileLock, 0, false); michael@0: michael@0: gProfileService.selectedProfile = selectedProfile.profile; michael@0: updateStartupPrefs(); michael@0: michael@0: gDialogParams.SetInt(0, 1); michael@0: michael@0: gDialogParams.SetString(0, selectedProfile.profile.name); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: function exitDialog() michael@0: { michael@0: updateStartupPrefs(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: function updateStartupPrefs() michael@0: { michael@0: var autoSelectLastProfile = document.getElementById("autoSelectLastProfile"); michael@0: gProfileService.startWithLastProfile = autoSelectLastProfile.checked; michael@0: michael@0: /* Bug 257777 */ michael@0: gProfileService.startOffline = document.getElementById("offlineState").checked; michael@0: } michael@0: michael@0: // handle key event on listboxes michael@0: function onProfilesKey(aEvent) michael@0: { michael@0: switch( aEvent.keyCode ) michael@0: { michael@0: case KeyEvent.DOM_VK_DELETE: michael@0: ConfirmDelete(); michael@0: break; michael@0: case KeyEvent.DOM_VK_F2: michael@0: RenameProfile(); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: function onProfilesDblClick(aEvent) michael@0: { michael@0: if(aEvent.target.localName == "listitem") michael@0: document.documentElement.acceptDialog(); michael@0: } michael@0: michael@0: // invoke the createProfile Wizard michael@0: function CreateProfileWizard() michael@0: { michael@0: window.openDialog('chrome://mozapps/content/profile/createProfileWizard.xul', michael@0: '', 'centerscreen,chrome,modal,titlebar', gProfileService); michael@0: } michael@0: michael@0: /** michael@0: * Called from createProfileWizard to update the display. michael@0: */ michael@0: function CreateProfile(aProfile) michael@0: { michael@0: var profilesElement = document.getElementById("profiles"); michael@0: michael@0: var listitem = profilesElement.appendItem(aProfile.name, ""); michael@0: michael@0: var tooltiptext = michael@0: gProfileManagerBundle.getFormattedString("profileTooltip", [aProfile.name, aProfile.rootDir.path]); michael@0: listitem.setAttribute("tooltiptext", tooltiptext); michael@0: listitem.setAttribute("class", "listitem-iconic"); michael@0: listitem.profile = aProfile; michael@0: michael@0: profilesElement.ensureElementIsVisible(listitem); michael@0: profilesElement.selectItem(listitem); michael@0: } michael@0: michael@0: // rename the selected profile michael@0: function RenameProfile() michael@0: { michael@0: var profilesElement = document.getElementById("profiles"); michael@0: var selectedItem = profilesElement.selectedItem; michael@0: if (!selectedItem) { michael@0: return false; michael@0: } michael@0: michael@0: var selectedProfile = selectedItem.profile; michael@0: michael@0: var oldName = selectedProfile.name; michael@0: var newName = {value: oldName}; michael@0: michael@0: var dialogTitle = gProfileManagerBundle.getString("renameProfileTitle"); michael@0: var msg = michael@0: gProfileManagerBundle.getFormattedString("renameProfilePrompt", [oldName]); michael@0: michael@0: if (Services.prompt.prompt(window, dialogTitle, msg, newName, null, {value:0})) { michael@0: newName = newName.value; michael@0: michael@0: // User hasn't changed the profile name. Treat as if cancel was pressed. michael@0: if (newName == oldName) michael@0: return false; michael@0: michael@0: try { michael@0: selectedProfile.name = newName; michael@0: } michael@0: catch (e) { michael@0: var alTitle = gProfileManagerBundle.getString("profileNameInvalidTitle"); michael@0: var alMsg = gProfileManagerBundle.getFormattedString("profileNameInvalid", [newName]); michael@0: Services.prompt.alert(window, alTitle, alMsg); michael@0: return false; michael@0: } michael@0: michael@0: selectedItem.label = newName; michael@0: var tiptext = gProfileManagerBundle. michael@0: getFormattedString("profileTooltip", michael@0: [newName, selectedProfile.rootDir.path]); michael@0: selectedItem.setAttribute("tooltiptext", tiptext); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: function ConfirmDelete() michael@0: { michael@0: var deleteButton = document.getElementById("delbutton"); michael@0: var profileList = document.getElementById( "profiles" ); michael@0: michael@0: var selectedItem = profileList.selectedItem; michael@0: if (!selectedItem) { michael@0: return false; michael@0: } michael@0: michael@0: var selectedProfile = selectedItem.profile; michael@0: var deleteFiles = false; michael@0: michael@0: if (selectedProfile.rootDir.exists()) { michael@0: var dialogTitle = gProfileManagerBundle.getString("deleteTitle"); michael@0: var dialogText = michael@0: gProfileManagerBundle.getFormattedString("deleteProfileConfirm", michael@0: [selectedProfile.rootDir.path]); michael@0: michael@0: var buttonPressed = Services.prompt.confirmEx(window, dialogTitle, dialogText, michael@0: (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0) + michael@0: (Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1) + michael@0: (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_2), michael@0: gProfileManagerBundle.getString("dontDeleteFiles"), michael@0: null, michael@0: gProfileManagerBundle.getString("deleteFiles"), michael@0: null, {value:0}); michael@0: if (buttonPressed == 1) michael@0: return false; michael@0: michael@0: if (buttonPressed == 2) michael@0: deleteFiles = true; michael@0: } michael@0: michael@0: selectedProfile.remove(deleteFiles); michael@0: profileList.removeChild(selectedItem); michael@0: if (profileList.firstChild != undefined) { michael@0: profileList.selectItem(profileList.firstChild); michael@0: } michael@0: michael@0: return true; michael@0: }