main.js
Go to the documentation of this file.
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
4 #
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/
9 #
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.
14 #
15 # The Original Code is the Firefox Preferences System.
16 #
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.
21 #
22 # Contributor(s):
23 # Ben Goodger <ben@mozilla.org>
24 # Asaf Romano <mozilla.mano@sent.com>
25 # Ehsan Akhgari <ehsan.akhgari@gmail.com>
26 #
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.
38 #
39 # ***** END LICENSE BLOCK *****
40 
41 var gMainPane = {
42  _pane: null,
43 
47  init: function ()
48  {
49  this._pane = document.getElementById("paneMain");
50 
51  // set up the "use current page" label-changing listener
52  this._updateUseCurrentButton();
53  window.addEventListener("focus", this._updateUseCurrentButton, false);
54  },
55 
56  // HOME PAGE
57 
58  /*
59  * Preferences:
60  *
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:
66  *
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)
71  *
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.
75  */
76 
82  setHomePageToCurrent: function ()
83  {
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("Songbird:Main");
90  }
91  else
92  win = window.opener;
93 
94  if (win) {
95  var homePage = document.getElementById("browser.startup.homepage");
96  var browser = win.document.getElementById("content");
97 
98  /* XXX SONGBIRD: we only use the current tab
99  var newVal = browser.browsers[0].currentURI.spec;
100  if (browser.browsers.length > 1) {
101  // XXX using dangerous "|" joiner!
102  for (var i = 1; i < browser.browsers.length; i++)
103  newVal += "|" + browser.browsers[i].currentURI.spec;
104  }
105  */
106  var newVal = browser.currentURI.spec;
107 
108  homePage.value = newVal;
109  }
110  },
111 
117  setHomePageToBookmark: function ()
118  {
119  var rv = { urls: null, names: null };
120  document.documentElement.openSubDialog("chrome://browser/content/preferences/selectBookmark.xul",
121  "resizable", rv);
122  if (rv.urls && rv.names) {
123  var homePage = document.getElementById("browser.startup.homepage");
124 
125  // XXX still using dangerous "|" joiner!
126  homePage.value = rv.urls.join("|");
127  }
128  },
129 
134  _updateUseCurrentButton: function () {
135  var useCurrent = document.getElementById("useCurrent");
136 
137  var windowIsPresent;
138  var win;
139  if (document.documentElement.instantApply) {
140  const Cc = Components.classes, Ci = Components.interfaces;
141  // If we're in instant-apply mode, use the most recent browser window
142  var wm = Cc["@mozilla.org/appshell/window-mediator;1"]
143  .getService(Ci.nsIWindowMediator);
144  win = wm.getMostRecentWindow("Songbird:Main");
145  }
146  else
147  win = window.opener;
148 
149  if (win && win.document.documentElement
150  .getAttribute("windowtype") == "Songbird:Main") {
151  windowIsPresent = true;
152 
153  var tabbrowser = win.document.getElementById("content");
154  /* SONGBIRD: we only use the current tab
155  if (tabbrowser.browsers.length > 1)
156  useCurrent.label = useCurrent.getAttribute("label2");
157  else
158  */
159  useCurrent.label = useCurrent.getAttribute("label1");
160  }
161  else {
162  windowIsPresent = false;
163  useCurrent.label = useCurrent.getAttribute("label1");
164  }
165 
166  // In this case, the button's disabled state is set by preferences.xml.
167  if (document.getElementById
168  ("pref.browser.homepage.disable_button.current_page").locked)
169  return;
170 
171  useCurrent.disabled = !windowIsPresent;
172  },
173 
177  restoreDefaultHomePage: function ()
178  {
179  var homePage = document.getElementById("browser.startup.homepage");
180  homePage.value = homePage.defaultValue;
181  },
182 
183  // DOWNLOADS
184 
185  /*
186  * Preferences:
187  *
188  * browser.download.showWhenStarting - bool
189  * True if the Download Manager should be opened when a download is
190  * started, false if it shouldn't be opened.
191  * browser.download.closeWhenDone - bool
192  * True if the Download Manager should be closed when all downloads
193  * complete, false if it should be left open.
194  * browser.download.useDownloadDir - bool
195  * True - Save files directly to the folder configured via the
196  * browser.download.folderList preference.
197  * False - Always ask the user where to save a file and default to
198  * browser.download.lastDir when displaying a folder picker dialog.
199  * browser.download.dir - local file handle
200  * A local folder the user may have selected for downloaded files to be
201  * saved. Migration of other browser settings may also set this path.
202  * This folder is enabled when folderList equals 2.
203  * browser.download.lastDir - local file handle
204  * May contain the last folder path accessed when the user browsed
205  * via the file save-as dialog. (see contentAreaUtils.js)
206  * browser.download.folderList - int
207  * Indicates the location users wish to save downloaded files too.
208  * It is also used to display special file labels when the default
209  * download location is either the Desktop or the Downloads folder.
210  * Values:
211  * 0 - The desktop is the default download location.
212  * 1 - The system's downloads folder is the default download location.
213  * 2 - The default download location is elsewhere as specified in
214  * browser.download.dir.
215  * browser.download.downloadDir
216  * deprecated.
217  * browser.download.defaultFolder
218  * deprecated.
219  */
220 
226  readShowDownloadsWhenStarting: function ()
227  {
228  this.showDownloadsWhenStartingPrefChanged();
229 
230  // don't override the preference's value in UI
231  return undefined;
232  },
233 
238  showDownloadsWhenStartingPrefChanged: function ()
239  {
240  var showWhenStartingPref = document.getElementById("browser.download.manager.showWhenStarting");
241  var closeWhenDonePref = document.getElementById("browser.download.manager.closeWhenDone");
242  closeWhenDonePref.disabled = !showWhenStartingPref.value;
243  },
244 
249  readUseDownloadDir: function ()
250  {
251  var downloadFolder = document.getElementById("downloadFolder");
252  var chooseFolder = document.getElementById("chooseFolder");
253  var preference = document.getElementById("browser.download.useDownloadDir");
254  downloadFolder.disabled = !preference.value;
255  chooseFolder.disabled = !preference.value;
256 
257  // don't override the preference's value in UI
258  return undefined;
259  },
260 
266  chooseFolder: function ()
267  {
268  const nsIFilePicker = Components.interfaces.nsIFilePicker;
269  const nsILocalFile = Components.interfaces.nsILocalFile;
270 
271  var fp = Components.classes["@mozilla.org/filepicker;1"]
272  .createInstance(nsIFilePicker);
273  var bundlePreferences = document.getElementById("bundlePreferences");
274  var title = bundlePreferences.getString("chooseDownloadFolderTitle");
275  fp.init(window, title, nsIFilePicker.modeGetFolder);
276  fp.appendFilters(nsIFilePicker.filterAll);
277 
278  var folderListPref = document.getElementById("browser.download.folderList");
279  var currentDirPref = this._indexToFolder(folderListPref.value); // file
280  var defDownloads = this._indexToFolder(1); // file
281 
282  // First try to open what's currently configured
283  if (currentDirPref && currentDirPref.exists()) {
284  fp.displayDirectory = currentDirPref;
285  } // Try the system's download dir
286  else if (defDownloads && defDownloads.exists()) {
287  fp.displayDirectory = defDownloads;
288  } // Fall back to Desktop
289  else {
290  fp.displayDirectory = this._indexToFolder(0);
291  }
292 
293  if (fp.show() == nsIFilePicker.returnOK) {
294  var file = fp.file.QueryInterface(nsILocalFile);
295  var currentDirPref = document.getElementById("browser.download.dir");
296  currentDirPref.value = file;
297  var folderListPref = document.getElementById("browser.download.folderList");
298  folderListPref.value = this._folderToIndex(file);
299  // Note, the real prefs will not be updated yet, so dnld manager's
300  // userDownloadsDirectory may not return the right folder after
301  // this code executes. displayDownloadDirPref will be called on
302  // the assignment above to update the UI.
303  }
304  },
305 
310  displayDownloadDirPref: function ()
311  {
312  var folderListPref = document.getElementById("browser.download.folderList");
313  var bundlePreferences = document.getElementById("bundlePreferences");
314  var downloadFolder = document.getElementById("downloadFolder");
315  var currentDirPref = document.getElementById("browser.download.dir");
316 
317  // The user's download folder is based on the preferences listed above.
318  // However, if the system does not support a download folder, the
319  // actual path returned will be the system's desktop or home folder.
320  // If this is the case, skip off displaying the Download label and
321  // display Desktop, even though folderList might be 1.
322  var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
323  .getService(Components.interfaces.nsIProperties);
324  var desk = fileLocator.get("Desk", Components.interfaces.nsILocalFile);
325  var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
326  .getService(Components.interfaces.nsIDownloadManager);
327  var supportDownloadLabel = !dnldMgr.defaultDownloadsDirectory.equals(desk);
328 
329  // Used in defining the correct path to the folder icon.
330  var ios = Components.classes["@mozilla.org/network/io-service;1"]
331  .getService(Components.interfaces.nsIIOService);
332  var fph = ios.getProtocolHandler("file")
333  .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
334  var iconUrlSpec;
335 
336  // Display a 'pretty' label or the path in the UI.
337  if (folderListPref.value == 2) {
338  // Custom path selected and is configured
339  downloadFolder.label = this._getDisplayNameOfFile(currentDirPref.value);
340  iconUrlSpec = fph.getURLSpecFromFile(currentDirPref.value);
341  } else if (folderListPref.value == 1 && supportDownloadLabel) {
342  // 'Downloads'
343  // In 1.5, this pointed to a folder we created called 'My Downloads'
344  // and was available as an option in the 1.5 drop down. On XP this
345  // was in My Documents, on OSX it was in User Docs. In 2.0, we did
346  // away with the drop down option, although the special label was
347  // still supported for the folder if it existed. Because it was
348  // not exposed it was rarely used.
349  // With 3.0, a new desktop folder - 'Downloads' was introduced for
350  // platforms and versions that don't support a default system downloads
351  // folder. See nsDownloadManager for details.
352  downloadFolder.label = bundlePreferences.getString("downloadsFolderName");
353  iconUrlSpec = fph.getURLSpecFromFile(this._indexToFolder(1));
354  } else {
355  // 'Desktop'
356  downloadFolder.label = bundlePreferences.getString("desktopFolderName");
357  iconUrlSpec = fph.getURLSpecFromFile(desk);
358  }
359  downloadFolder.image = "moz-icon://" + iconUrlSpec + "?size=16";
360 
361  // don't override the preference's value in UI
362  return undefined;
363  },
364 
368  _getDisplayNameOfFile: function (aFolder)
369  {
370  // TODO: would like to add support for 'Downloads on Macintosh HD'
371  // for OS X users.
372  return aFolder ? aFolder.path : "";
373  },
374 
384  _getDownloadsFolder: function (aFolder)
385  {
386  switch(aFolder)
387  {
388  case "Desktop":
389  var fileLoc = Components.classes["@mozilla.org/file/directory_service;1"]
390  .getService(Components.interfaces.nsIProperties);
391  return fileLoc.get("Desk", Components.interfaces.nsILocalFile);
392  break;
393  case "Downloads":
394  var dnldMgr = Components.classes["@mozilla.org/download-manager;1"]
395  .getService(Components.interfaces.nsIDownloadManager);
396  return dnldMgr.defaultDownloadsDirectory;
397  break;
398  }
399  throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
400  },
401 
412  _folderToIndex: function (aFolder)
413  {
414  if (!aFolder || aFolder.equals(this._getDownloadsFolder("Desktop")))
415  return 0;
416  else if (aFolder.equals(this._getDownloadsFolder("Downloads")))
417  return 1;
418  return 2;
419  },
420 
430  _indexToFolder: function (aIndex)
431  {
432  switch (aIndex) {
433  case 0:
434  return this._getDownloadsFolder("Desktop");
435  case 1:
436  return this._getDownloadsFolder("Downloads");
437  }
438  var currentDirPref = document.getElementById("browser.download.dir");
439  return currentDirPref.value;
440  },
441 
445  getFolderListPref: function ()
446  {
447  var folderListPref = document.getElementById("browser.download.folderList");
448  switch(folderListPref.value) {
449  case 0: // Desktop
450  case 1: // Downloads
451  return folderListPref.value;
452  break;
453  case 2: // Custom
454  var currentDirPref = document.getElementById("browser.download.dir");
455  if (currentDirPref.value) {
456  // Resolve to a known location if possible. We are writing out
457  // to prefs on this call, so now would be a good time to do it.
458  return this._folderToIndex(currentDirPref.value);
459  }
460  return 0;
461  break;
462  }
463  },
464 
468  showAddonsMgr: function ()
469  {
470  const EMTYPE = "Extension:Manager";
471  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
472  .getService(Components.interfaces.nsIWindowMediator);
473  var theEM = wm.getMostRecentWindow(EMTYPE);
474  if (theEM) {
475  theEM.focus();
476  theEM.showView("extensions");
477  return;
478  }
479 
480  const EMURL = "chrome://mozapps/content/extensions/extensions.xul";
481  const EMFEATURES = "chrome,menubar,extra-chrome,toolbar,dialog=no,resizable";
482  window.openDialog(EMURL, "", EMFEATURES, "extensions");
483  }
484 };
const Cc
let window
_window init
Definition: FeedWriter.js:1144
var gMainPane
Definition: main.js:41
var tabbrowser
const nsIFilePicker
return null
Definition: FeedWriter.js:1143
const Ci
var ios
Definition: head_feeds.js:5
var browser
Definition: openLocation.js:42
var file