browser/components/preferences/in-content/main.js

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

mercurial