browser/components/preferences/main.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/preferences/main.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,430 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
    1.10 +XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
    1.11 +                                  "resource:///modules/DownloadsCommon.jsm");
    1.12 +
    1.13 +var gMainPane = {
    1.14 +  _pane: null,
    1.15 +
    1.16 +  /**
    1.17 +   * Initialization of this.
    1.18 +   */
    1.19 +  init: function ()
    1.20 +  {
    1.21 +    this._pane = document.getElementById("paneMain");
    1.22 +
    1.23 +    // set up the "use current page" label-changing listener
    1.24 +    this._updateUseCurrentButton();
    1.25 +    window.addEventListener("focus", this._updateUseCurrentButton.bind(this), false);
    1.26 +
    1.27 +    this.updateBrowserStartupLastSession();
    1.28 +
    1.29 +    // Notify observers that the UI is now ready
    1.30 +    Components.classes["@mozilla.org/observer-service;1"]
    1.31 +              .getService(Components.interfaces.nsIObserverService)
    1.32 +              .notifyObservers(window, "main-pane-loaded", null);
    1.33 +  },
    1.34 +
    1.35 +  // HOME PAGE
    1.36 +
    1.37 +  /*
    1.38 +   * Preferences:
    1.39 +   *
    1.40 +   * browser.startup.homepage
    1.41 +   * - the user's home page, as a string; if the home page is a set of tabs,
    1.42 +   *   this will be those URLs separated by the pipe character "|"
    1.43 +   * browser.startup.page
    1.44 +   * - what page(s) to show when the user starts the application, as an integer:
    1.45 +   *
    1.46 +   *     0: a blank page
    1.47 +   *     1: the home page (as set by the browser.startup.homepage pref)
    1.48 +   *     2: the last page the user visited (DEPRECATED)
    1.49 +   *     3: windows and tabs from the last session (a.k.a. session restore)
    1.50 +   *
    1.51 +   *   The deprecated option is not exposed in UI; however, if the user has it
    1.52 +   *   selected and doesn't change the UI for this preference, the deprecated
    1.53 +   *   option is preserved.
    1.54 +   */
    1.55 +
    1.56 +  syncFromHomePref: function ()
    1.57 +  {
    1.58 +    let homePref = document.getElementById("browser.startup.homepage");
    1.59 +
    1.60 +    // If the pref is set to about:home, set the value to "" to show the
    1.61 +    // placeholder text (about:home title).
    1.62 +    if (homePref.value.toLowerCase() == "about:home")
    1.63 +      return "";
    1.64 +
    1.65 +    // If the pref is actually "", show about:blank.  The actual home page
    1.66 +    // loading code treats them the same, and we don't want the placeholder text
    1.67 +    // to be shown.
    1.68 +    if (homePref.value == "")
    1.69 +      return "about:blank";
    1.70 +
    1.71 +    // Otherwise, show the actual pref value.
    1.72 +    return undefined;
    1.73 +  },
    1.74 +
    1.75 +  syncToHomePref: function (value)
    1.76 +  {
    1.77 +    // If the value is "", use about:home.
    1.78 +    if (value == "")
    1.79 +      return "about:home";
    1.80 +
    1.81 +    // Otherwise, use the actual textbox value.
    1.82 +    return undefined;
    1.83 +  },
    1.84 +
    1.85 +  /**
    1.86 +   * Sets the home page to the current displayed page (or frontmost tab, if the
    1.87 +   * most recent browser window contains multiple tabs), updating preference
    1.88 +   * window UI to reflect this.
    1.89 +   */
    1.90 +  setHomePageToCurrent: function ()
    1.91 +  {
    1.92 +    let homePage = document.getElementById("browser.startup.homepage");
    1.93 +    let tabs = this._getTabsForHomePage();
    1.94 +    function getTabURI(t) t.linkedBrowser.currentURI.spec;
    1.95 +
    1.96 +    // FIXME Bug 244192: using dangerous "|" joiner!
    1.97 +    if (tabs.length)
    1.98 +      homePage.value = tabs.map(getTabURI).join("|");
    1.99 +  },
   1.100 +
   1.101 +  /**
   1.102 +   * Displays a dialog in which the user can select a bookmark to use as home
   1.103 +   * page.  If the user selects a bookmark, that bookmark's name is displayed in
   1.104 +   * UI and the bookmark's address is stored to the home page preference.
   1.105 +   */
   1.106 +  setHomePageToBookmark: function ()
   1.107 +  {
   1.108 +    var rv = { urls: null, names: null };
   1.109 +    document.documentElement.openSubDialog("chrome://browser/content/preferences/selectBookmark.xul",
   1.110 +                                           "resizable", rv);  
   1.111 +    if (rv.urls && rv.names) {
   1.112 +      var homePage = document.getElementById("browser.startup.homepage");
   1.113 +
   1.114 +      // XXX still using dangerous "|" joiner!
   1.115 +      homePage.value = rv.urls.join("|");
   1.116 +    }
   1.117 +  },
   1.118 +
   1.119 +  /**
   1.120 +   * Switches the "Use Current Page" button between its singular and plural
   1.121 +   * forms.
   1.122 +   */
   1.123 +  _updateUseCurrentButton: function () {
   1.124 +    let useCurrent = document.getElementById("useCurrent");
   1.125 +
   1.126 +    let tabs = this._getTabsForHomePage();
   1.127 +    if (tabs.length > 1)
   1.128 +      useCurrent.label = useCurrent.getAttribute("label2");
   1.129 +    else
   1.130 +      useCurrent.label = useCurrent.getAttribute("label1");
   1.131 +
   1.132 +    // In this case, the button's disabled state is set by preferences.xml.
   1.133 +    if (document.getElementById
   1.134 +        ("pref.browser.homepage.disable_button.current_page").locked)
   1.135 +      return;
   1.136 +
   1.137 +    useCurrent.disabled = !tabs.length
   1.138 +  },
   1.139 +
   1.140 +  _getTabsForHomePage: function ()
   1.141 +  {
   1.142 +    var win;
   1.143 +    var tabs = [];
   1.144 +    if (document.documentElement.instantApply) {
   1.145 +      const Cc = Components.classes, Ci = Components.interfaces;
   1.146 +      // If we're in instant-apply mode, use the most recent browser window
   1.147 +      var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
   1.148 +                 .getService(Ci.nsIWindowMediator);
   1.149 +      win = wm.getMostRecentWindow("navigator:browser");
   1.150 +    }
   1.151 +    else {
   1.152 +      win = window.opener;
   1.153 +    }
   1.154 +
   1.155 +    if (win && win.document.documentElement
   1.156 +                  .getAttribute("windowtype") == "navigator:browser") {
   1.157 +      // We should only include visible & non-pinned tabs
   1.158 +      tabs = win.gBrowser.visibleTabs.slice(win.gBrowser._numPinnedTabs);
   1.159 +    }
   1.160 +
   1.161 +    return tabs;
   1.162 +  },
   1.163 +
   1.164 +  /**
   1.165 +   * Restores the default home page as the user's home page.
   1.166 +   */
   1.167 +  restoreDefaultHomePage: function ()
   1.168 +  {
   1.169 +    var homePage = document.getElementById("browser.startup.homepage");
   1.170 +    homePage.value = homePage.defaultValue;
   1.171 +  },
   1.172 +
   1.173 +  // DOWNLOADS
   1.174 +
   1.175 +  /*
   1.176 +   * Preferences:
   1.177 +   * 
   1.178 +   * browser.download.useDownloadDir - bool
   1.179 +   *   True - Save files directly to the folder configured via the
   1.180 +   *   browser.download.folderList preference.
   1.181 +   *   False - Always ask the user where to save a file and default to 
   1.182 +   *   browser.download.lastDir when displaying a folder picker dialog.
   1.183 +   * browser.download.dir - local file handle
   1.184 +   *   A local folder the user may have selected for downloaded files to be
   1.185 +   *   saved. Migration of other browser settings may also set this path.
   1.186 +   *   This folder is enabled when folderList equals 2.
   1.187 +   * browser.download.lastDir - local file handle
   1.188 +   *   May contain the last folder path accessed when the user browsed
   1.189 +   *   via the file save-as dialog. (see contentAreaUtils.js)
   1.190 +   * browser.download.folderList - int
   1.191 +   *   Indicates the location users wish to save downloaded files too.
   1.192 +   *   It is also used to display special file labels when the default
   1.193 +   *   download location is either the Desktop or the Downloads folder.
   1.194 +   *   Values:
   1.195 +   *     0 - The desktop is the default download location.
   1.196 +   *     1 - The system's downloads folder is the default download location.
   1.197 +   *     2 - The default download location is elsewhere as specified in
   1.198 +   *         browser.download.dir.
   1.199 +   * browser.download.downloadDir
   1.200 +   *   deprecated.
   1.201 +   * browser.download.defaultFolder
   1.202 +   *   deprecated.
   1.203 +   */
   1.204 +
   1.205 +  /**
   1.206 +   * Enables/disables the folder field and Browse button based on whether a
   1.207 +   * default download directory is being used.
   1.208 +   */
   1.209 +  readUseDownloadDir: function ()
   1.210 +  {
   1.211 +    var downloadFolder = document.getElementById("downloadFolder");
   1.212 +    var chooseFolder = document.getElementById("chooseFolder");
   1.213 +    var preference = document.getElementById("browser.download.useDownloadDir");
   1.214 +    downloadFolder.disabled = !preference.value;
   1.215 +    chooseFolder.disabled = !preference.value;
   1.216 +
   1.217 +    // don't override the preference's value in UI
   1.218 +    return undefined;
   1.219 +  },
   1.220 +  
   1.221 +  /**
   1.222 +   * Displays a file picker in which the user can choose the location where
   1.223 +   * downloads are automatically saved, updating preferences and UI in
   1.224 +   * response to the choice, if one is made.
   1.225 +   */
   1.226 +  chooseFolder: function ()
   1.227 +  {
   1.228 +    const nsIFilePicker = Components.interfaces.nsIFilePicker;
   1.229 +    const nsILocalFile = Components.interfaces.nsILocalFile;
   1.230 +
   1.231 +    let bundlePreferences = document.getElementById("bundlePreferences");
   1.232 +    let title = bundlePreferences.getString("chooseDownloadFolderTitle");
   1.233 +    let folderListPref = document.getElementById("browser.download.folderList");
   1.234 +    let currentDirPref = this._indexToFolder(folderListPref.value); // file
   1.235 +    let defDownloads = this._indexToFolder(1); // file
   1.236 +    let fp = Components.classes["@mozilla.org/filepicker;1"].
   1.237 +             createInstance(nsIFilePicker);
   1.238 +    let fpCallback = function fpCallback_done(aResult) {
   1.239 +      if (aResult == nsIFilePicker.returnOK) {
   1.240 +        let file = fp.file.QueryInterface(nsILocalFile);
   1.241 +        let downloadDirPref = document.getElementById("browser.download.dir");
   1.242 +
   1.243 +        downloadDirPref.value = file;
   1.244 +        folderListPref.value = this._folderToIndex(file);
   1.245 +        // Note, the real prefs will not be updated yet, so dnld manager's
   1.246 +        // userDownloadsDirectory may not return the right folder after
   1.247 +        // this code executes. displayDownloadDirPref will be called on
   1.248 +        // the assignment above to update the UI.
   1.249 +      }
   1.250 +    }.bind(this);
   1.251 +
   1.252 +    fp.init(window, title, nsIFilePicker.modeGetFolder);
   1.253 +    fp.appendFilters(nsIFilePicker.filterAll);
   1.254 +    // First try to open what's currently configured
   1.255 +    if (currentDirPref && currentDirPref.exists()) {
   1.256 +      fp.displayDirectory = currentDirPref;
   1.257 +    } // Try the system's download dir
   1.258 +    else if (defDownloads && defDownloads.exists()) {
   1.259 +      fp.displayDirectory = defDownloads;
   1.260 +    } // Fall back to Desktop
   1.261 +    else {
   1.262 +      fp.displayDirectory = this._indexToFolder(0);
   1.263 +    }
   1.264 +    fp.open(fpCallback);
   1.265 +  },
   1.266 +
   1.267 +  /**
   1.268 +   * Initializes the download folder display settings based on the user's 
   1.269 +   * preferences.
   1.270 +   */
   1.271 +  displayDownloadDirPref: function ()
   1.272 +  {
   1.273 +    var folderListPref = document.getElementById("browser.download.folderList");
   1.274 +    var bundlePreferences = document.getElementById("bundlePreferences");
   1.275 +    var downloadFolder = document.getElementById("downloadFolder");
   1.276 +    var currentDirPref = document.getElementById("browser.download.dir");
   1.277 +
   1.278 +    // Used in defining the correct path to the folder icon.
   1.279 +    var ios = Components.classes["@mozilla.org/network/io-service;1"]
   1.280 +                        .getService(Components.interfaces.nsIIOService);
   1.281 +    var fph = ios.getProtocolHandler("file")
   1.282 +                 .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
   1.283 +    var iconUrlSpec;
   1.284 +      
   1.285 +    // Display a 'pretty' label or the path in the UI.
   1.286 +    if (folderListPref.value == 2) {
   1.287 +      // Custom path selected and is configured
   1.288 +      downloadFolder.label = this._getDisplayNameOfFile(currentDirPref.value);
   1.289 +      iconUrlSpec = fph.getURLSpecFromFile(currentDirPref.value);
   1.290 +    } else if (folderListPref.value == 1) {
   1.291 +      // 'Downloads'
   1.292 +      // In 1.5, this pointed to a folder we created called 'My Downloads'
   1.293 +      // and was available as an option in the 1.5 drop down. On XP this
   1.294 +      // was in My Documents, on OSX it was in User Docs. In 2.0, we did
   1.295 +      // away with the drop down option, although the special label was
   1.296 +      // still supported for the folder if it existed. Because it was
   1.297 +      // not exposed it was rarely used.
   1.298 +      // With 3.0, a new desktop folder - 'Downloads' was introduced for
   1.299 +      // platforms and versions that don't support a default system downloads
   1.300 +      // folder. See nsDownloadManager for details. 
   1.301 +      downloadFolder.label = bundlePreferences.getString("downloadsFolderName");
   1.302 +      iconUrlSpec = fph.getURLSpecFromFile(this._indexToFolder(1));
   1.303 +    } else {
   1.304 +      // 'Desktop'
   1.305 +      downloadFolder.label = bundlePreferences.getString("desktopFolderName");
   1.306 +      iconUrlSpec = fph.getURLSpecFromFile(this._getDownloadsFolder("Desktop"));
   1.307 +    }
   1.308 +    downloadFolder.image = "moz-icon://" + iconUrlSpec + "?size=16";
   1.309 +    
   1.310 +    // don't override the preference's value in UI
   1.311 +    return undefined;
   1.312 +  },
   1.313 +
   1.314 +  /**
   1.315 +   * Returns the textual path of a folder in readable form.
   1.316 +   */
   1.317 +  _getDisplayNameOfFile: function (aFolder)
   1.318 +  {
   1.319 +    // TODO: would like to add support for 'Downloads on Macintosh HD'
   1.320 +    //       for OS X users.
   1.321 +    return aFolder ? aFolder.path : "";
   1.322 +  },
   1.323 +
   1.324 +  /**
   1.325 +   * Returns the Downloads folder.  If aFolder is "Desktop", then the Downloads
   1.326 +   * folder returned is the desktop folder; otherwise, it is a folder whose name
   1.327 +   * indicates that it is a download folder and whose path is as determined by
   1.328 +   * the XPCOM directory service via the download manager's attribute 
   1.329 +   * defaultDownloadsDirectory.
   1.330 +   *
   1.331 +   * @throws if aFolder is not "Desktop" or "Downloads"
   1.332 +   */
   1.333 +  _getDownloadsFolder: function (aFolder)
   1.334 +  {
   1.335 +    switch (aFolder) {
   1.336 +      case "Desktop":
   1.337 +        var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
   1.338 +                                    .getService(Components.interfaces.nsIProperties);
   1.339 +        return fileLoc.get("Desk", Components.interfaces.nsILocalFile);
   1.340 +      break;
   1.341 +      case "Downloads":
   1.342 +        var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
   1.343 +                                .getService(Components.interfaces.nsIDownloadManager);
   1.344 +        return dnldMgr.defaultDownloadsDirectory;
   1.345 +      break;
   1.346 +    }
   1.347 +    throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
   1.348 +  },
   1.349 +
   1.350 +  /**
   1.351 +   * Determines the type of the given folder.
   1.352 +   *
   1.353 +   * @param   aFolder
   1.354 +   *          the folder whose type is to be determined
   1.355 +   * @returns integer
   1.356 +   *          0 if aFolder is the Desktop or is unspecified,
   1.357 +   *          1 if aFolder is the Downloads folder,
   1.358 +   *          2 otherwise
   1.359 +   */
   1.360 +  _folderToIndex: function (aFolder)
   1.361 +  {
   1.362 +    if (!aFolder || aFolder.equals(this._getDownloadsFolder("Desktop")))
   1.363 +      return 0;
   1.364 +    else if (aFolder.equals(this._getDownloadsFolder("Downloads")))
   1.365 +      return 1;
   1.366 +    return 2;
   1.367 +  },
   1.368 +
   1.369 +  /**
   1.370 +   * Converts an integer into the corresponding folder.
   1.371 +   *
   1.372 +   * @param   aIndex
   1.373 +   *          an integer
   1.374 +   * @returns the Desktop folder if aIndex == 0,
   1.375 +   *          the Downloads folder if aIndex == 1,
   1.376 +   *          the folder stored in browser.download.dir
   1.377 +   */
   1.378 +  _indexToFolder: function (aIndex)
   1.379 +  {
   1.380 +    switch (aIndex) {
   1.381 +      case 0:
   1.382 +        return this._getDownloadsFolder("Desktop");
   1.383 +      case 1:
   1.384 +        return this._getDownloadsFolder("Downloads");
   1.385 +    }
   1.386 +    var currentDirPref = document.getElementById("browser.download.dir");
   1.387 +    return currentDirPref.value;
   1.388 +  },
   1.389 +
   1.390 +  /**
   1.391 +   * Returns the value for the browser.download.folderList preference.
   1.392 +   */
   1.393 +  getFolderListPref: function ()
   1.394 +  {
   1.395 +    var folderListPref = document.getElementById("browser.download.folderList");
   1.396 +    switch (folderListPref.value) {
   1.397 +      case 0: // Desktop
   1.398 +      case 1: // Downloads
   1.399 +        return folderListPref.value;
   1.400 +      break;
   1.401 +      case 2: // Custom
   1.402 +        var currentDirPref = document.getElementById("browser.download.dir");
   1.403 +        if (currentDirPref.value) {
   1.404 +          // Resolve to a known location if possible. We are writing out
   1.405 +          // to prefs on this call, so now would be a good time to do it.
   1.406 +          return this._folderToIndex(currentDirPref.value);
   1.407 +        }
   1.408 +        return 0;
   1.409 +      break;
   1.410 +    }
   1.411 +  },
   1.412 +
   1.413 +  /**
   1.414 +   * Hide/show the "Show my windows and tabs from last time" option based
   1.415 +   * on the value of the browser.privatebrowsing.autostart pref.
   1.416 +   */
   1.417 +  updateBrowserStartupLastSession: function()
   1.418 +  {
   1.419 +    let pbAutoStartPref = document.getElementById("browser.privatebrowsing.autostart");
   1.420 +    let startupPref = document.getElementById("browser.startup.page");
   1.421 +    let menu = document.getElementById("browserStartupPage");
   1.422 +    let option = document.getElementById("browserStartupLastSession");
   1.423 +    if (pbAutoStartPref.value) {
   1.424 +      option.setAttribute("disabled", "true");
   1.425 +      if (option.selected) {
   1.426 +        menu.selectedItem = document.getElementById("browserStartupHomePage");
   1.427 +      }
   1.428 +    } else {
   1.429 +      option.removeAttribute("disabled");
   1.430 +      startupPref.updateElements(); // select the correct index in the startup menulist
   1.431 +    }
   1.432 +  }
   1.433 +};

mercurial