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

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

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

mercurial