browser/components/preferences/main.js

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 7 XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
michael@0 8 "resource:///modules/DownloadsCommon.jsm");
michael@0 9
michael@0 10 var gMainPane = {
michael@0 11 _pane: null,
michael@0 12
michael@0 13 /**
michael@0 14 * Initialization of this.
michael@0 15 */
michael@0 16 init: function ()
michael@0 17 {
michael@0 18 this._pane = document.getElementById("paneMain");
michael@0 19
michael@0 20 // set up the "use current page" label-changing listener
michael@0 21 this._updateUseCurrentButton();
michael@0 22 window.addEventListener("focus", this._updateUseCurrentButton.bind(this), false);
michael@0 23
michael@0 24 this.updateBrowserStartupLastSession();
michael@0 25
michael@0 26 // Notify observers that the UI is now ready
michael@0 27 Components.classes["@mozilla.org/observer-service;1"]
michael@0 28 .getService(Components.interfaces.nsIObserverService)
michael@0 29 .notifyObservers(window, "main-pane-loaded", null);
michael@0 30 },
michael@0 31
michael@0 32 // HOME PAGE
michael@0 33
michael@0 34 /*
michael@0 35 * Preferences:
michael@0 36 *
michael@0 37 * browser.startup.homepage
michael@0 38 * - the user's home page, as a string; if the home page is a set of tabs,
michael@0 39 * this will be those URLs separated by the pipe character "|"
michael@0 40 * browser.startup.page
michael@0 41 * - what page(s) to show when the user starts the application, as an integer:
michael@0 42 *
michael@0 43 * 0: a blank page
michael@0 44 * 1: the home page (as set by the browser.startup.homepage pref)
michael@0 45 * 2: the last page the user visited (DEPRECATED)
michael@0 46 * 3: windows and tabs from the last session (a.k.a. session restore)
michael@0 47 *
michael@0 48 * The deprecated option is not exposed in UI; however, if the user has it
michael@0 49 * selected and doesn't change the UI for this preference, the deprecated
michael@0 50 * option is preserved.
michael@0 51 */
michael@0 52
michael@0 53 syncFromHomePref: function ()
michael@0 54 {
michael@0 55 let homePref = document.getElementById("browser.startup.homepage");
michael@0 56
michael@0 57 // If the pref is set to about:home, set the value to "" to show the
michael@0 58 // placeholder text (about:home title).
michael@0 59 if (homePref.value.toLowerCase() == "about:home")
michael@0 60 return "";
michael@0 61
michael@0 62 // If the pref is actually "", show about:blank. The actual home page
michael@0 63 // loading code treats them the same, and we don't want the placeholder text
michael@0 64 // to be shown.
michael@0 65 if (homePref.value == "")
michael@0 66 return "about:blank";
michael@0 67
michael@0 68 // Otherwise, show the actual pref value.
michael@0 69 return undefined;
michael@0 70 },
michael@0 71
michael@0 72 syncToHomePref: function (value)
michael@0 73 {
michael@0 74 // If the value is "", use about:home.
michael@0 75 if (value == "")
michael@0 76 return "about:home";
michael@0 77
michael@0 78 // Otherwise, use the actual textbox value.
michael@0 79 return undefined;
michael@0 80 },
michael@0 81
michael@0 82 /**
michael@0 83 * Sets the home page to the current displayed page (or frontmost tab, if the
michael@0 84 * most recent browser window contains multiple tabs), updating preference
michael@0 85 * window UI to reflect this.
michael@0 86 */
michael@0 87 setHomePageToCurrent: function ()
michael@0 88 {
michael@0 89 let homePage = document.getElementById("browser.startup.homepage");
michael@0 90 let tabs = this._getTabsForHomePage();
michael@0 91 function getTabURI(t) t.linkedBrowser.currentURI.spec;
michael@0 92
michael@0 93 // FIXME Bug 244192: using dangerous "|" joiner!
michael@0 94 if (tabs.length)
michael@0 95 homePage.value = tabs.map(getTabURI).join("|");
michael@0 96 },
michael@0 97
michael@0 98 /**
michael@0 99 * Displays a dialog in which the user can select a bookmark to use as home
michael@0 100 * page. If the user selects a bookmark, that bookmark's name is displayed in
michael@0 101 * UI and the bookmark's address is stored to the home page preference.
michael@0 102 */
michael@0 103 setHomePageToBookmark: function ()
michael@0 104 {
michael@0 105 var rv = { urls: null, names: null };
michael@0 106 document.documentElement.openSubDialog("chrome://browser/content/preferences/selectBookmark.xul",
michael@0 107 "resizable", rv);
michael@0 108 if (rv.urls && rv.names) {
michael@0 109 var homePage = document.getElementById("browser.startup.homepage");
michael@0 110
michael@0 111 // XXX still using dangerous "|" joiner!
michael@0 112 homePage.value = rv.urls.join("|");
michael@0 113 }
michael@0 114 },
michael@0 115
michael@0 116 /**
michael@0 117 * Switches the "Use Current Page" button between its singular and plural
michael@0 118 * forms.
michael@0 119 */
michael@0 120 _updateUseCurrentButton: function () {
michael@0 121 let useCurrent = document.getElementById("useCurrent");
michael@0 122
michael@0 123 let tabs = this._getTabsForHomePage();
michael@0 124 if (tabs.length > 1)
michael@0 125 useCurrent.label = useCurrent.getAttribute("label2");
michael@0 126 else
michael@0 127 useCurrent.label = useCurrent.getAttribute("label1");
michael@0 128
michael@0 129 // In this case, the button's disabled state is set by preferences.xml.
michael@0 130 if (document.getElementById
michael@0 131 ("pref.browser.homepage.disable_button.current_page").locked)
michael@0 132 return;
michael@0 133
michael@0 134 useCurrent.disabled = !tabs.length
michael@0 135 },
michael@0 136
michael@0 137 _getTabsForHomePage: function ()
michael@0 138 {
michael@0 139 var win;
michael@0 140 var tabs = [];
michael@0 141 if (document.documentElement.instantApply) {
michael@0 142 const Cc = Components.classes, Ci = Components.interfaces;
michael@0 143 // If we're in instant-apply mode, use the most recent browser window
michael@0 144 var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
michael@0 145 .getService(Ci.nsIWindowMediator);
michael@0 146 win = wm.getMostRecentWindow("navigator:browser");
michael@0 147 }
michael@0 148 else {
michael@0 149 win = window.opener;
michael@0 150 }
michael@0 151
michael@0 152 if (win && win.document.documentElement
michael@0 153 .getAttribute("windowtype") == "navigator:browser") {
michael@0 154 // We should only include visible & non-pinned tabs
michael@0 155 tabs = win.gBrowser.visibleTabs.slice(win.gBrowser._numPinnedTabs);
michael@0 156 }
michael@0 157
michael@0 158 return tabs;
michael@0 159 },
michael@0 160
michael@0 161 /**
michael@0 162 * Restores the default home page as the user's home page.
michael@0 163 */
michael@0 164 restoreDefaultHomePage: function ()
michael@0 165 {
michael@0 166 var homePage = document.getElementById("browser.startup.homepage");
michael@0 167 homePage.value = homePage.defaultValue;
michael@0 168 },
michael@0 169
michael@0 170 // DOWNLOADS
michael@0 171
michael@0 172 /*
michael@0 173 * Preferences:
michael@0 174 *
michael@0 175 * browser.download.useDownloadDir - bool
michael@0 176 * True - Save files directly to the folder configured via the
michael@0 177 * browser.download.folderList preference.
michael@0 178 * False - Always ask the user where to save a file and default to
michael@0 179 * browser.download.lastDir when displaying a folder picker dialog.
michael@0 180 * browser.download.dir - local file handle
michael@0 181 * A local folder the user may have selected for downloaded files to be
michael@0 182 * saved. Migration of other browser settings may also set this path.
michael@0 183 * This folder is enabled when folderList equals 2.
michael@0 184 * browser.download.lastDir - local file handle
michael@0 185 * May contain the last folder path accessed when the user browsed
michael@0 186 * via the file save-as dialog. (see contentAreaUtils.js)
michael@0 187 * browser.download.folderList - int
michael@0 188 * Indicates the location users wish to save downloaded files too.
michael@0 189 * It is also used to display special file labels when the default
michael@0 190 * download location is either the Desktop or the Downloads folder.
michael@0 191 * Values:
michael@0 192 * 0 - The desktop is the default download location.
michael@0 193 * 1 - The system's downloads folder is the default download location.
michael@0 194 * 2 - The default download location is elsewhere as specified in
michael@0 195 * browser.download.dir.
michael@0 196 * browser.download.downloadDir
michael@0 197 * deprecated.
michael@0 198 * browser.download.defaultFolder
michael@0 199 * deprecated.
michael@0 200 */
michael@0 201
michael@0 202 /**
michael@0 203 * Enables/disables the folder field and Browse button based on whether a
michael@0 204 * default download directory is being used.
michael@0 205 */
michael@0 206 readUseDownloadDir: function ()
michael@0 207 {
michael@0 208 var downloadFolder = document.getElementById("downloadFolder");
michael@0 209 var chooseFolder = document.getElementById("chooseFolder");
michael@0 210 var preference = document.getElementById("browser.download.useDownloadDir");
michael@0 211 downloadFolder.disabled = !preference.value;
michael@0 212 chooseFolder.disabled = !preference.value;
michael@0 213
michael@0 214 // don't override the preference's value in UI
michael@0 215 return undefined;
michael@0 216 },
michael@0 217
michael@0 218 /**
michael@0 219 * Displays a file picker in which the user can choose the location where
michael@0 220 * downloads are automatically saved, updating preferences and UI in
michael@0 221 * response to the choice, if one is made.
michael@0 222 */
michael@0 223 chooseFolder: function ()
michael@0 224 {
michael@0 225 const nsIFilePicker = Components.interfaces.nsIFilePicker;
michael@0 226 const nsILocalFile = Components.interfaces.nsILocalFile;
michael@0 227
michael@0 228 let bundlePreferences = document.getElementById("bundlePreferences");
michael@0 229 let title = bundlePreferences.getString("chooseDownloadFolderTitle");
michael@0 230 let folderListPref = document.getElementById("browser.download.folderList");
michael@0 231 let currentDirPref = this._indexToFolder(folderListPref.value); // file
michael@0 232 let defDownloads = this._indexToFolder(1); // file
michael@0 233 let fp = Components.classes["@mozilla.org/filepicker;1"].
michael@0 234 createInstance(nsIFilePicker);
michael@0 235 let fpCallback = function fpCallback_done(aResult) {
michael@0 236 if (aResult == nsIFilePicker.returnOK) {
michael@0 237 let file = fp.file.QueryInterface(nsILocalFile);
michael@0 238 let downloadDirPref = document.getElementById("browser.download.dir");
michael@0 239
michael@0 240 downloadDirPref.value = file;
michael@0 241 folderListPref.value = this._folderToIndex(file);
michael@0 242 // Note, the real prefs will not be updated yet, so dnld manager's
michael@0 243 // userDownloadsDirectory may not return the right folder after
michael@0 244 // this code executes. displayDownloadDirPref will be called on
michael@0 245 // the assignment above to update the UI.
michael@0 246 }
michael@0 247 }.bind(this);
michael@0 248
michael@0 249 fp.init(window, title, nsIFilePicker.modeGetFolder);
michael@0 250 fp.appendFilters(nsIFilePicker.filterAll);
michael@0 251 // First try to open what's currently configured
michael@0 252 if (currentDirPref && currentDirPref.exists()) {
michael@0 253 fp.displayDirectory = currentDirPref;
michael@0 254 } // Try the system's download dir
michael@0 255 else if (defDownloads && defDownloads.exists()) {
michael@0 256 fp.displayDirectory = defDownloads;
michael@0 257 } // Fall back to Desktop
michael@0 258 else {
michael@0 259 fp.displayDirectory = this._indexToFolder(0);
michael@0 260 }
michael@0 261 fp.open(fpCallback);
michael@0 262 },
michael@0 263
michael@0 264 /**
michael@0 265 * Initializes the download folder display settings based on the user's
michael@0 266 * preferences.
michael@0 267 */
michael@0 268 displayDownloadDirPref: function ()
michael@0 269 {
michael@0 270 var folderListPref = document.getElementById("browser.download.folderList");
michael@0 271 var bundlePreferences = document.getElementById("bundlePreferences");
michael@0 272 var downloadFolder = document.getElementById("downloadFolder");
michael@0 273 var currentDirPref = document.getElementById("browser.download.dir");
michael@0 274
michael@0 275 // Used in defining the correct path to the folder icon.
michael@0 276 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 277 .getService(Components.interfaces.nsIIOService);
michael@0 278 var fph = ios.getProtocolHandler("file")
michael@0 279 .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
michael@0 280 var iconUrlSpec;
michael@0 281
michael@0 282 // Display a 'pretty' label or the path in the UI.
michael@0 283 if (folderListPref.value == 2) {
michael@0 284 // Custom path selected and is configured
michael@0 285 downloadFolder.label = this._getDisplayNameOfFile(currentDirPref.value);
michael@0 286 iconUrlSpec = fph.getURLSpecFromFile(currentDirPref.value);
michael@0 287 } else if (folderListPref.value == 1) {
michael@0 288 // 'Downloads'
michael@0 289 // In 1.5, this pointed to a folder we created called 'My Downloads'
michael@0 290 // and was available as an option in the 1.5 drop down. On XP this
michael@0 291 // was in My Documents, on OSX it was in User Docs. In 2.0, we did
michael@0 292 // away with the drop down option, although the special label was
michael@0 293 // still supported for the folder if it existed. Because it was
michael@0 294 // not exposed it was rarely used.
michael@0 295 // With 3.0, a new desktop folder - 'Downloads' was introduced for
michael@0 296 // platforms and versions that don't support a default system downloads
michael@0 297 // folder. See nsDownloadManager for details.
michael@0 298 downloadFolder.label = bundlePreferences.getString("downloadsFolderName");
michael@0 299 iconUrlSpec = fph.getURLSpecFromFile(this._indexToFolder(1));
michael@0 300 } else {
michael@0 301 // 'Desktop'
michael@0 302 downloadFolder.label = bundlePreferences.getString("desktopFolderName");
michael@0 303 iconUrlSpec = fph.getURLSpecFromFile(this._getDownloadsFolder("Desktop"));
michael@0 304 }
michael@0 305 downloadFolder.image = "moz-icon://" + iconUrlSpec + "?size=16";
michael@0 306
michael@0 307 // don't override the preference's value in UI
michael@0 308 return undefined;
michael@0 309 },
michael@0 310
michael@0 311 /**
michael@0 312 * Returns the textual path of a folder in readable form.
michael@0 313 */
michael@0 314 _getDisplayNameOfFile: function (aFolder)
michael@0 315 {
michael@0 316 // TODO: would like to add support for 'Downloads on Macintosh HD'
michael@0 317 // for OS X users.
michael@0 318 return aFolder ? aFolder.path : "";
michael@0 319 },
michael@0 320
michael@0 321 /**
michael@0 322 * Returns the Downloads folder. If aFolder is "Desktop", then the Downloads
michael@0 323 * folder returned is the desktop folder; otherwise, it is a folder whose name
michael@0 324 * indicates that it is a download folder and whose path is as determined by
michael@0 325 * the XPCOM directory service via the download manager's attribute
michael@0 326 * defaultDownloadsDirectory.
michael@0 327 *
michael@0 328 * @throws if aFolder is not "Desktop" or "Downloads"
michael@0 329 */
michael@0 330 _getDownloadsFolder: function (aFolder)
michael@0 331 {
michael@0 332 switch (aFolder) {
michael@0 333 case "Desktop":
michael@0 334 var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
michael@0 335 .getService(Components.interfaces.nsIProperties);
michael@0 336 return fileLoc.get("Desk", Components.interfaces.nsILocalFile);
michael@0 337 break;
michael@0 338 case "Downloads":
michael@0 339 var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
michael@0 340 .getService(Components.interfaces.nsIDownloadManager);
michael@0 341 return dnldMgr.defaultDownloadsDirectory;
michael@0 342 break;
michael@0 343 }
michael@0 344 throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
michael@0 345 },
michael@0 346
michael@0 347 /**
michael@0 348 * Determines the type of the given folder.
michael@0 349 *
michael@0 350 * @param aFolder
michael@0 351 * the folder whose type is to be determined
michael@0 352 * @returns integer
michael@0 353 * 0 if aFolder is the Desktop or is unspecified,
michael@0 354 * 1 if aFolder is the Downloads folder,
michael@0 355 * 2 otherwise
michael@0 356 */
michael@0 357 _folderToIndex: function (aFolder)
michael@0 358 {
michael@0 359 if (!aFolder || aFolder.equals(this._getDownloadsFolder("Desktop")))
michael@0 360 return 0;
michael@0 361 else if (aFolder.equals(this._getDownloadsFolder("Downloads")))
michael@0 362 return 1;
michael@0 363 return 2;
michael@0 364 },
michael@0 365
michael@0 366 /**
michael@0 367 * Converts an integer into the corresponding folder.
michael@0 368 *
michael@0 369 * @param aIndex
michael@0 370 * an integer
michael@0 371 * @returns the Desktop folder if aIndex == 0,
michael@0 372 * the Downloads folder if aIndex == 1,
michael@0 373 * the folder stored in browser.download.dir
michael@0 374 */
michael@0 375 _indexToFolder: function (aIndex)
michael@0 376 {
michael@0 377 switch (aIndex) {
michael@0 378 case 0:
michael@0 379 return this._getDownloadsFolder("Desktop");
michael@0 380 case 1:
michael@0 381 return this._getDownloadsFolder("Downloads");
michael@0 382 }
michael@0 383 var currentDirPref = document.getElementById("browser.download.dir");
michael@0 384 return currentDirPref.value;
michael@0 385 },
michael@0 386
michael@0 387 /**
michael@0 388 * Returns the value for the browser.download.folderList preference.
michael@0 389 */
michael@0 390 getFolderListPref: function ()
michael@0 391 {
michael@0 392 var folderListPref = document.getElementById("browser.download.folderList");
michael@0 393 switch (folderListPref.value) {
michael@0 394 case 0: // Desktop
michael@0 395 case 1: // Downloads
michael@0 396 return folderListPref.value;
michael@0 397 break;
michael@0 398 case 2: // Custom
michael@0 399 var currentDirPref = document.getElementById("browser.download.dir");
michael@0 400 if (currentDirPref.value) {
michael@0 401 // Resolve to a known location if possible. We are writing out
michael@0 402 // to prefs on this call, so now would be a good time to do it.
michael@0 403 return this._folderToIndex(currentDirPref.value);
michael@0 404 }
michael@0 405 return 0;
michael@0 406 break;
michael@0 407 }
michael@0 408 },
michael@0 409
michael@0 410 /**
michael@0 411 * Hide/show the "Show my windows and tabs from last time" option based
michael@0 412 * on the value of the browser.privatebrowsing.autostart pref.
michael@0 413 */
michael@0 414 updateBrowserStartupLastSession: function()
michael@0 415 {
michael@0 416 let pbAutoStartPref = document.getElementById("browser.privatebrowsing.autostart");
michael@0 417 let startupPref = document.getElementById("browser.startup.page");
michael@0 418 let menu = document.getElementById("browserStartupPage");
michael@0 419 let option = document.getElementById("browserStartupLastSession");
michael@0 420 if (pbAutoStartPref.value) {
michael@0 421 option.setAttribute("disabled", "true");
michael@0 422 if (option.selected) {
michael@0 423 menu.selectedItem = document.getElementById("browserStartupHomePage");
michael@0 424 }
michael@0 425 } else {
michael@0 426 option.removeAttribute("disabled");
michael@0 427 startupPref.updateElements(); // select the correct index in the startup menulist
michael@0 428 }
michael@0 429 }
michael@0 430 };

mercurial