toolkit/profile/content/profileSelection.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/profile/content/profileSelection.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,264 @@
     1.4 +/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.11 +
    1.12 +const C = Components.classes;
    1.13 +const I = Components.interfaces;
    1.14 +
    1.15 +const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
    1.16 +
    1.17 +var gDialogParams;
    1.18 +var gProfileManagerBundle;
    1.19 +var gBrandBundle;
    1.20 +var gProfileService;
    1.21 +
    1.22 +function startup()
    1.23 +{
    1.24 +  try {
    1.25 +    gDialogParams = window.arguments[0].
    1.26 +      QueryInterface(I.nsIDialogParamBlock);
    1.27 +
    1.28 +    gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
    1.29 +
    1.30 +    gProfileManagerBundle = document.getElementById("bundle_profileManager");
    1.31 +    gBrandBundle = document.getElementById("bundle_brand");
    1.32 +
    1.33 +    document.documentElement.centerWindowOnScreen();
    1.34 +
    1.35 +    var profilesElement = document.getElementById("profiles");
    1.36 +
    1.37 +    var profileList = gProfileService.profiles;
    1.38 +    while (profileList.hasMoreElements()) {
    1.39 +      var profile = profileList.getNext().QueryInterface(I.nsIToolkitProfile);
    1.40 +
    1.41 +      var listitem = profilesElement.appendItem(profile.name, "");
    1.42 +
    1.43 +      var tooltiptext =
    1.44 +        gProfileManagerBundle.getFormattedString("profileTooltip", [profile.name, profile.rootDir.path]);
    1.45 +      listitem.setAttribute("tooltiptext", tooltiptext);
    1.46 +      listitem.setAttribute("class", "listitem-iconic");
    1.47 +      listitem.profile = profile;
    1.48 +      try {
    1.49 +        if (profile === gProfileService.selectedProfile) {
    1.50 +          setTimeout(function(a) {
    1.51 +            profilesElement.ensureElementIsVisible(a);
    1.52 +            profilesElement.selectItem(a);
    1.53 +          }, 0, listitem);
    1.54 +        }
    1.55 +      }
    1.56 +      catch(e) { }
    1.57 +    }
    1.58 +
    1.59 +    var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
    1.60 +    autoSelectLastProfile.checked = gProfileService.startWithLastProfile;
    1.61 +    profilesElement.focus();
    1.62 +  }
    1.63 +  catch(e) {
    1.64 +    window.close();
    1.65 +    throw (e);
    1.66 +  }
    1.67 +}
    1.68 +
    1.69 +function acceptDialog()
    1.70 +{
    1.71 +  var appName = gBrandBundle.getString("brandShortName");
    1.72 +
    1.73 +  var profilesElement = document.getElementById("profiles");
    1.74 +  var selectedProfile = profilesElement.selectedItem;
    1.75 +  if (!selectedProfile) {
    1.76 +    var pleaseSelectTitle = gProfileManagerBundle.getString("pleaseSelectTitle");
    1.77 +    var pleaseSelect =
    1.78 +      gProfileManagerBundle.getFormattedString("pleaseSelect", [appName]);
    1.79 +    Services.prompt.alert(window, pleaseSelectTitle, pleaseSelect);
    1.80 +
    1.81 +    return false;
    1.82 +  }
    1.83 +
    1.84 +  var profileLock;
    1.85 +
    1.86 +  try {
    1.87 +    profileLock = selectedProfile.profile.lock({ value: null });
    1.88 +  }
    1.89 +  catch (e) {
    1.90 +    if (!selectedProfile.profile.rootDir.exists()) {
    1.91 +      var missingTitle = gProfileManagerBundle.getString("profileMissingTitle");
    1.92 +      var missing =
    1.93 +        gProfileManagerBundle.getFormattedString("profileMissing", [appName]);
    1.94 +      Services.prompt.alert(window, missingTitle, missing);
    1.95 +      return false;
    1.96 +    }
    1.97 +
    1.98 +    var lockedTitle = gProfileManagerBundle.getString("profileLockedTitle");
    1.99 +    var locked =
   1.100 +      gProfileManagerBundle.getFormattedString("profileLocked2", [appName, selectedProfile.profile.name, appName]);
   1.101 +    Services.prompt.alert(window, lockedTitle, locked);
   1.102 +
   1.103 +    return false;
   1.104 +  }
   1.105 +  gDialogParams.objects.insertElementAt(profileLock.nsIProfileLock, 0, false);
   1.106 +
   1.107 +  gProfileService.selectedProfile = selectedProfile.profile;
   1.108 +  updateStartupPrefs();
   1.109 +
   1.110 +  gDialogParams.SetInt(0, 1);
   1.111 +
   1.112 +  gDialogParams.SetString(0, selectedProfile.profile.name);
   1.113 +
   1.114 +  return true;
   1.115 +}
   1.116 +
   1.117 +function exitDialog()
   1.118 +{
   1.119 +  updateStartupPrefs();
   1.120 +  
   1.121 +  return true;
   1.122 +}
   1.123 +
   1.124 +function updateStartupPrefs()
   1.125 +{
   1.126 +  var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
   1.127 +  gProfileService.startWithLastProfile = autoSelectLastProfile.checked;
   1.128 +
   1.129 +  /* Bug 257777 */
   1.130 +  gProfileService.startOffline = document.getElementById("offlineState").checked;
   1.131 +}
   1.132 +
   1.133 +// handle key event on listboxes
   1.134 +function onProfilesKey(aEvent)
   1.135 +{
   1.136 +  switch( aEvent.keyCode ) 
   1.137 +  {
   1.138 +  case KeyEvent.DOM_VK_DELETE:
   1.139 +    ConfirmDelete();
   1.140 +    break;
   1.141 +  case KeyEvent.DOM_VK_F2:
   1.142 +    RenameProfile();
   1.143 +    break;
   1.144 +  }
   1.145 +}
   1.146 +
   1.147 +function onProfilesDblClick(aEvent)
   1.148 +{
   1.149 +  if(aEvent.target.localName == "listitem")
   1.150 +    document.documentElement.acceptDialog();
   1.151 +}
   1.152 +
   1.153 +// invoke the createProfile Wizard
   1.154 +function CreateProfileWizard()
   1.155 +{
   1.156 +  window.openDialog('chrome://mozapps/content/profile/createProfileWizard.xul',
   1.157 +                    '', 'centerscreen,chrome,modal,titlebar', gProfileService);
   1.158 +}
   1.159 +
   1.160 +/**
   1.161 + * Called from createProfileWizard to update the display.
   1.162 + */
   1.163 +function CreateProfile(aProfile)
   1.164 +{
   1.165 +  var profilesElement = document.getElementById("profiles");
   1.166 +
   1.167 +  var listitem = profilesElement.appendItem(aProfile.name, "");
   1.168 +
   1.169 +  var tooltiptext =
   1.170 +    gProfileManagerBundle.getFormattedString("profileTooltip", [aProfile.name, aProfile.rootDir.path]);
   1.171 +  listitem.setAttribute("tooltiptext", tooltiptext);
   1.172 +  listitem.setAttribute("class", "listitem-iconic");
   1.173 +  listitem.profile = aProfile;
   1.174 +
   1.175 +  profilesElement.ensureElementIsVisible(listitem);
   1.176 +  profilesElement.selectItem(listitem);
   1.177 +}
   1.178 +
   1.179 +// rename the selected profile
   1.180 +function RenameProfile()
   1.181 +{
   1.182 +  var profilesElement = document.getElementById("profiles");
   1.183 +  var selectedItem = profilesElement.selectedItem;
   1.184 +  if (!selectedItem) {
   1.185 +    return false;
   1.186 +  }
   1.187 +
   1.188 +  var selectedProfile = selectedItem.profile;
   1.189 +
   1.190 +  var oldName = selectedProfile.name;
   1.191 +  var newName = {value: oldName};
   1.192 +
   1.193 +  var dialogTitle = gProfileManagerBundle.getString("renameProfileTitle");
   1.194 +  var msg =
   1.195 +    gProfileManagerBundle.getFormattedString("renameProfilePrompt", [oldName]);
   1.196 +
   1.197 +  if (Services.prompt.prompt(window, dialogTitle, msg, newName, null, {value:0})) {
   1.198 +    newName = newName.value;
   1.199 +
   1.200 +    // User hasn't changed the profile name. Treat as if cancel was pressed.
   1.201 +    if (newName == oldName)
   1.202 +      return false;
   1.203 +
   1.204 +    try {
   1.205 +      selectedProfile.name = newName;
   1.206 +    }
   1.207 +    catch (e) {
   1.208 +      var alTitle = gProfileManagerBundle.getString("profileNameInvalidTitle");
   1.209 +      var alMsg = gProfileManagerBundle.getFormattedString("profileNameInvalid", [newName]);
   1.210 +      Services.prompt.alert(window, alTitle, alMsg);
   1.211 +      return false;
   1.212 +    }
   1.213 +
   1.214 +    selectedItem.label = newName;
   1.215 +    var tiptext = gProfileManagerBundle.
   1.216 +                  getFormattedString("profileTooltip",
   1.217 +                                     [newName, selectedProfile.rootDir.path]);
   1.218 +    selectedItem.setAttribute("tooltiptext", tiptext);
   1.219 +
   1.220 +    return true;
   1.221 +  }
   1.222 +
   1.223 +  return false;
   1.224 +}
   1.225 +
   1.226 +function ConfirmDelete()
   1.227 +{
   1.228 +  var deleteButton = document.getElementById("delbutton");
   1.229 +  var profileList = document.getElementById( "profiles" );
   1.230 +
   1.231 +  var selectedItem = profileList.selectedItem;
   1.232 +  if (!selectedItem) {
   1.233 +    return false;
   1.234 +  }
   1.235 +
   1.236 +  var selectedProfile = selectedItem.profile;
   1.237 +  var deleteFiles = false;
   1.238 +
   1.239 +  if (selectedProfile.rootDir.exists()) {
   1.240 +    var dialogTitle = gProfileManagerBundle.getString("deleteTitle");
   1.241 +    var dialogText =
   1.242 +      gProfileManagerBundle.getFormattedString("deleteProfileConfirm",
   1.243 +                                               [selectedProfile.rootDir.path]);
   1.244 +
   1.245 +    var buttonPressed = Services.prompt.confirmEx(window, dialogTitle, dialogText,
   1.246 +                          (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_0) +
   1.247 +                          (Services.prompt.BUTTON_TITLE_CANCEL * Services.prompt.BUTTON_POS_1) +
   1.248 +                          (Services.prompt.BUTTON_TITLE_IS_STRING * Services.prompt.BUTTON_POS_2),
   1.249 +                          gProfileManagerBundle.getString("dontDeleteFiles"),
   1.250 +                          null,
   1.251 +                          gProfileManagerBundle.getString("deleteFiles"),
   1.252 +                          null, {value:0});
   1.253 +    if (buttonPressed == 1)
   1.254 +      return false;
   1.255 +
   1.256 +    if (buttonPressed == 2)
   1.257 +      deleteFiles = true;
   1.258 +  }
   1.259 +  
   1.260 +  selectedProfile.remove(deleteFiles);
   1.261 +  profileList.removeChild(selectedItem);
   1.262 +  if (profileList.firstChild != undefined) {
   1.263 +    profileList.selectItem(profileList.firstChild);
   1.264 +  }
   1.265 +
   1.266 +  return true;
   1.267 +}

mercurial