Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / browser / components / preferences / main.js
blobd0f92a8c96d25eee715716bf7fbaae2803feeed1
1 # -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 # ***** BEGIN LICENSE BLOCK *****
3 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 # The contents of this file are subject to the Mozilla Public License Version
6 # 1.1 (the "License"); you may not use this file except in compliance with
7 # the License. You may obtain a copy of the License at
8 # http://www.mozilla.org/MPL/
10 # Software distributed under the License is distributed on an "AS IS" basis,
11 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 # for the specific language governing rights and limitations under the
13 # License.
15 # The Original Code is the Firefox Preferences System.
17 # The Initial Developer of the Original Code is
18 # Jeff Walden <jwalden+code@mit.edu>.
19 # Portions created by the Initial Developer are Copyright (C) 2006
20 # the Initial Developer. All Rights Reserved.
22 # Contributor(s):
23 # Ben Goodger <ben@mozilla.org>
24 # Asaf Romano <mozilla.mano@sent.com>
25 # Ehsan Akhgari <ehsan.akhgari@gmail.com>
27 # Alternatively, the contents of this file may be used under the terms of
28 # either the GNU General Public License Version 2 or later (the "GPL"), or
29 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 # in which case the provisions of the GPL or the LGPL are applicable instead
31 # of those above. If you wish to allow use of your version of this file only
32 # under the terms of either the GPL or the LGPL, and not to allow others to
33 # use your version of this file under the terms of the MPL, indicate your
34 # decision by deleting the provisions above and replace them with the notice
35 # and other provisions required by the GPL or the LGPL. If you do not delete
36 # the provisions above, a recipient may use your version of this file under
37 # the terms of any one of the MPL, the GPL or the LGPL.
39 # ***** END LICENSE BLOCK *****
41 var gMainPane = {
42 _pane: null,
44 /**
45 * Initialization of this.
47 init: function ()
49 this._pane = document.getElementById("paneMain");
51 // set up the "use current page" label-changing listener
52 this._updateUseCurrentButton();
53 window.addEventListener("focus", this._updateUseCurrentButton, false);
56 // HOME PAGE
59 * Preferences:
61 * browser.startup.homepage
62 * - the user's home page, as a string; if the home page is a set of tabs,
63 * this will be those URLs separated by the pipe character "|"
64 * browser.startup.page
65 * - what page(s) to show when the user starts the application, as an integer:
67 * 0: a blank page
68 * 1: the home page (as set by the browser.startup.homepage pref)
69 * 2: the last page the user visited (DEPRECATED)
70 * 3: windows and tabs from the last session (a.k.a. session restore)
72 * The deprecated option is not exposed in UI; however, if the user has it
73 * selected and doesn't change the UI for this preference, the deprecated
74 * option is preserved.
77 /**
78 * Sets the home page to the current displayed page (or frontmost tab, if the
79 * most recent browser window contains multiple tabs), updating preference
80 * window UI to reflect this.
82 setHomePageToCurrent: function ()
84 var win;
85 if (document.documentElement.instantApply) {
86 // If we're in instant-apply mode, use the most recent browser window
87 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
88 .getService(Components.interfaces.nsIWindowMediator);
89 win = wm.getMostRecentWindow("navigator:browser");
91 else
92 win = window.opener;
94 if (win) {
95 var homePage = document.getElementById("browser.startup.homepage");
96 var browser = win.document.getElementById("content");
98 var newVal = browser.browsers[0].currentURI.spec;
99 if (browser.browsers.length > 1) {
100 // XXX using dangerous "|" joiner!
101 for (var i = 1; i < browser.browsers.length; i++)
102 newVal += "|" + browser.browsers[i].currentURI.spec;
105 homePage.value = newVal;
110 * Displays a dialog in which the user can select a bookmark to use as home
111 * page. If the user selects a bookmark, that bookmark's name is displayed in
112 * UI and the bookmark's address is stored to the home page preference.
114 setHomePageToBookmark: function ()
116 var rv = { urls: null, names: null };
117 document.documentElement.openSubDialog("chrome://browser/content/preferences/selectBookmark.xul",
118 "resizable", rv);
119 if (rv.urls && rv.names) {
120 var homePage = document.getElementById("browser.startup.homepage");
122 // XXX still using dangerous "|" joiner!
123 homePage.value = rv.urls.join("|");
128 * Switches the "Use Current Page" button between its singular and plural
129 * forms.
131 _updateUseCurrentButton: function () {
132 var useCurrent = document.getElementById("useCurrent");
134 var windowIsPresent;
135 var win;
136 if (document.documentElement.instantApply) {
137 const Cc = Components.classes, Ci = Components.interfaces;
138 // If we're in instant-apply mode, use the most recent browser window
139 var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
140 .getService(Ci.nsIWindowMediator);
141 win = wm.getMostRecentWindow("navigator:browser");
143 else
144 win = window.opener;
146 if (win && win.document.documentElement
147 .getAttribute("windowtype") == "navigator:browser") {
148 windowIsPresent = true;
150 var tabbrowser = win.document.getElementById("content");
151 if (tabbrowser.browsers.length > 1)
152 useCurrent.label = useCurrent.getAttribute("label2");
153 else
154 useCurrent.label = useCurrent.getAttribute("label1");
156 else {
157 windowIsPresent = false;
158 useCurrent.label = useCurrent.getAttribute("label1");
161 // In this case, the button's disabled state is set by preferences.xml.
162 if (document.getElementById
163 ("pref.browser.homepage.disable_button.current_page").locked)
164 return;
166 useCurrent.disabled = !windowIsPresent;
170 * Restores the default home page as the user's home page.
172 restoreDefaultHomePage: function ()
174 var homePage = document.getElementById("browser.startup.homepage");
175 homePage.value = homePage.defaultValue;
178 // DOWNLOADS
181 * Preferences:
183 * browser.download.showWhenStarting - bool
184 * True if the Download Manager should be opened when a download is
185 * started, false if it shouldn't be opened.
186 * browser.download.closeWhenDone - bool
187 * True if the Download Manager should be closed when all downloads
188 * complete, false if it should be left open.
189 * browser.download.useDownloadDir - bool
190 * True - Save files directly to the folder configured via the
191 * browser.download.folderList preference.
192 * False - Always ask the user where to save a file and default to
193 * browser.download.lastDir when displaying a folder picker dialog.
194 * browser.download.dir - local file handle
195 * A local folder the user may have selected for downloaded files to be
196 * saved. Migration of other browser settings may also set this path.
197 * This folder is enabled when folderList equals 2.
198 * browser.download.lastDir - local file handle
199 * May contain the last folder path accessed when the user browsed
200 * via the file save-as dialog. (see contentAreaUtils.js)
201 * browser.download.folderList - int
202 * Indicates the location users wish to save downloaded files too.
203 * It is also used to display special file labels when the default
204 * download location is either the Desktop or the Downloads folder.
205 * Values:
206 * 0 - The desktop is the default download location.
207 * 1 - The system's downloads folder is the default download location.
208 * 2 - The default download location is elsewhere as specified in
209 * browser.download.dir.
210 * browser.download.downloadDir
211 * deprecated.
212 * browser.download.defaultFolder
213 * deprecated.
217 * Updates preferences which depend upon the value of the preference which
218 * determines whether the Downloads manager is opened at the start of a
219 * download.
221 readShowDownloadsWhenStarting: function ()
223 this.showDownloadsWhenStartingPrefChanged();
225 // don't override the preference's value in UI
226 return undefined;
230 * Enables or disables the "close Downloads manager when downloads finished"
231 * preference element, consequently updating the associated UI.
233 showDownloadsWhenStartingPrefChanged: function ()
235 var showWhenStartingPref = document.getElementById("browser.download.manager.showWhenStarting");
236 var closeWhenDonePref = document.getElementById("browser.download.manager.closeWhenDone");
237 closeWhenDonePref.disabled = !showWhenStartingPref.value;
241 * Enables/disables the folder field and Browse button based on whether a
242 * default download directory is being used.
244 readUseDownloadDir: function ()
246 var downloadFolder = document.getElementById("downloadFolder");
247 var chooseFolder = document.getElementById("chooseFolder");
248 var preference = document.getElementById("browser.download.useDownloadDir");
249 downloadFolder.disabled = !preference.value;
250 chooseFolder.disabled = !preference.value;
252 // don't override the preference's value in UI
253 return undefined;
257 * Displays a file picker in which the user can choose the location where
258 * downloads are automatically saved, updating preferences and UI in
259 * response to the choice, if one is made.
261 chooseFolder: function ()
263 const nsIFilePicker = Components.interfaces.nsIFilePicker;
264 const nsILocalFile = Components.interfaces.nsILocalFile;
266 var fp = Components.classes["@mozilla.org/filepicker;1"]
267 .createInstance(nsIFilePicker);
268 var bundlePreferences = document.getElementById("bundlePreferences");
269 var title = bundlePreferences.getString("chooseDownloadFolderTitle");
270 fp.init(window, title, nsIFilePicker.modeGetFolder);
271 fp.appendFilters(nsIFilePicker.filterAll);
273 var folderListPref = document.getElementById("browser.download.folderList");
274 var currentDirPref = this._indexToFolder(folderListPref.value); // file
275 var defDownloads = this._indexToFolder(1); // file
277 // First try to open what's currently configured
278 if (currentDirPref && currentDirPref.exists()) {
279 fp.displayDirectory = currentDirPref;
280 } // Try the system's download dir
281 else if (defDownloads && defDownloads.exists()) {
282 fp.displayDirectory = defDownloads;
283 } // Fall back to Desktop
284 else {
285 fp.displayDirectory = this._indexToFolder(0);
288 if (fp.show() == nsIFilePicker.returnOK) {
289 var file = fp.file.QueryInterface(nsILocalFile);
290 var currentDirPref = document.getElementById("browser.download.dir");
291 currentDirPref.value = file;
292 var folderListPref = document.getElementById("browser.download.folderList");
293 folderListPref.value = this._folderToIndex(file);
294 // Note, the real prefs will not be updated yet, so dnld manager's
295 // userDownloadsDirectory may not return the right folder after
296 // this code executes. displayDownloadDirPref will be called on
297 // the assignment above to update the UI.
302 * Initializes the download folder display settings based on the user's
303 * preferences.
305 displayDownloadDirPref: function ()
307 var folderListPref = document.getElementById("browser.download.folderList");
308 var bundlePreferences = document.getElementById("bundlePreferences");
309 var downloadFolder = document.getElementById("downloadFolder");
310 var currentDirPref = document.getElementById("browser.download.dir");
312 // The user's download folder is based on the preferences listed above.
313 // However, if the system does not support a download folder, the
314 // actual path returned will be the system's desktop or home folder.
315 // If this is the case, skip off displaying the Download label and
316 // display Desktop, even though folderList might be 1.
317 var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
318 .getService(Components.interfaces.nsIProperties);
319 var desk = fileLocator.get("Desk", Components.interfaces.nsILocalFile);
320 var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
321 .getService(Components.interfaces.nsIDownloadManager);
322 var supportDownloadLabel = !dnldMgr.defaultDownloadsDirectory.equals(desk);
324 // Used in defining the correct path to the folder icon.
325 var ios = Components.classes["@mozilla.org/network/io-service;1"]
326 .getService(Components.interfaces.nsIIOService);
327 var fph = ios.getProtocolHandler("file")
328 .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
329 var iconUrlSpec;
331 // Display a 'pretty' label or the path in the UI.
332 if (folderListPref.value == 2) {
333 // Custom path selected and is configured
334 downloadFolder.label = this._getDisplayNameOfFile(currentDirPref.value);
335 iconUrlSpec = fph.getURLSpecFromFile(currentDirPref.value);
336 } else if (folderListPref.value == 1 && supportDownloadLabel) {
337 // 'Downloads'
338 // In 1.5, this pointed to a folder we created called 'My Downloads'
339 // and was available as an option in the 1.5 drop down. On XP this
340 // was in My Documents, on OSX it was in User Docs. In 2.0, we did
341 // away with the drop down option, although the special label was
342 // still supported for the folder if it existed. Because it was
343 // not exposed it was rarely used.
344 // With 3.0, a new desktop folder - 'Downloads' was introduced for
345 // platforms and versions that don't support a default system downloads
346 // folder. See nsDownloadManager for details.
347 downloadFolder.label = bundlePreferences.getString("downloadsFolderName");
348 iconUrlSpec = fph.getURLSpecFromFile(this._indexToFolder(1));
349 } else {
350 // 'Desktop'
351 downloadFolder.label = bundlePreferences.getString("desktopFolderName");
352 iconUrlSpec = fph.getURLSpecFromFile(desk);
354 downloadFolder.image = "moz-icon://" + iconUrlSpec + "?size=16";
356 // don't override the preference's value in UI
357 return undefined;
361 * Returns the textual path of a folder in readable form.
363 _getDisplayNameOfFile: function (aFolder)
365 // TODO: would like to add support for 'Downloads on Macintosh HD'
366 // for OS X users.
367 return aFolder ? aFolder.path : "";
371 * Returns the Downloads folder. If aFolder is "Desktop", then the Downloads
372 * folder returned is the desktop folder; otherwise, it is a folder whose name
373 * indicates that it is a download folder and whose path is as determined by
374 * the XPCOM directory service via the download manager's attribute
375 * defaultDownloadsDirectory.
377 * @throws if aFolder is not "Desktop" or "Downloads"
379 _getDownloadsFolder: function (aFolder)
381 switch(aFolder)
383 case "Desktop":
384 var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
385 .getService(Components.interfaces.nsIProperties);
386 return fileLoc.get("Desk", Components.interfaces.nsILocalFile);
387 break;
388 case "Downloads":
389 var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
390 .getService(Components.interfaces.nsIDownloadManager);
391 return dnldMgr.defaultDownloadsDirectory;
392 break;
394 throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
398 * Determines the type of the given folder.
400 * @param aFolder
401 * the folder whose type is to be determined
402 * @returns integer
403 * 0 if aFolder is the Desktop or is unspecified,
404 * 1 if aFolder is the Downloads folder,
405 * 2 otherwise
407 _folderToIndex: function (aFolder)
409 if (!aFolder || aFolder.equals(this._getDownloadsFolder("Desktop")))
410 return 0;
411 else if (aFolder.equals(this._getDownloadsFolder("Downloads")))
412 return 1;
413 return 2;
417 * Converts an integer into the corresponding folder.
419 * @param aIndex
420 * an integer
421 * @returns the Desktop folder if aIndex == 0,
422 * the Downloads folder if aIndex == 1,
423 * the folder stored in browser.download.dir
425 _indexToFolder: function (aIndex)
427 switch (aIndex) {
428 case 0:
429 return this._getDownloadsFolder("Desktop");
430 case 1:
431 return this._getDownloadsFolder("Downloads");
433 var currentDirPref = document.getElementById("browser.download.dir");
434 return currentDirPref.value;
438 * Returns the value for the browser.download.folderList preference.
440 getFolderListPref: function ()
442 var folderListPref = document.getElementById("browser.download.folderList");
443 switch(folderListPref.value) {
444 case 0: // Desktop
445 case 1: // Downloads
446 return folderListPref.value;
447 break;
448 case 2: // Custom
449 var currentDirPref = document.getElementById("browser.download.dir");
450 if (currentDirPref.value) {
451 // Resolve to a known location if possible. We are writing out
452 // to prefs on this call, so now would be a good time to do it.
453 return this._folderToIndex(currentDirPref.value);
455 return 0;
456 break;
461 * Displays the Add-ons Manager.
463 showAddonsMgr: function ()
465 const EMTYPE = "Extension:Manager";
466 var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
467 .getService(Components.interfaces.nsIWindowMediator);
468 var theEM = wm.getMostRecentWindow(EMTYPE);
469 if (theEM) {
470 theEM.focus();
471 theEM.showView("extensions");
472 return;
475 const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
476 const EMFEATURES = "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable";
477 window.openDialog(EMURL, "", EMFEATURES, "extensions");