/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla.org Code. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 2002 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Ben Goodger * Alec Flett * Stephen Walker * Christopher A. Aillon * Ian Neal * Karsten Düsterloh * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ // The contents of this file will be loaded into the scope of the object // ! // platform integration const PFINT_NOT_DEFAULT = 0; const PFINT_DEFAULT = 1; const PFINT_PENDING = 2; // put "global" definitions here for easy reference var gDefaultHomePage = ""; var gHomePagePrefPeak = 0; var gPreferences = null; // access helper methods function GetHomePagePrefCount() { return document.getElementById("browser.startup.homepage.count").value; } function SetHomePagePrefCount(aCount) { document.getElementById("browser.startup.homepage.count").value = aCount; } function GetHomePagePrefName(aIndex) { var prefname = "browser.startup.homepage"; if (aIndex > 0) prefname += "." + aIndex; return prefname; } function GetHomePagePref(aIndex) { // return the at aIndex return document.getElementById(GetHomePagePrefName(aIndex)); } function AddHomePagePref(aIndex) { // create new for aIndex var pref = document.createElement("preference"); var prefname = GetHomePagePrefName(aIndex); pref.setAttribute("id", prefname); pref.setAttribute("name", prefname); pref.setAttribute("type", "wstring"); gPreferences.appendChild(pref); return pref; } // homepage group textbox helper methods function GetHomePageGroup() { return document.getElementById("browserStartupHomepage").value; } function SetHomePageValue(aValue) { document.getElementById("browserStartupHomepage").value = aValue; } // helper methods for reading current page URIs function GetMostRecentBrowser() { var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"] .getService(Components.interfaces.nsIWindowMediator); var browserWindow = windowManager.getMostRecentWindow("navigator:browser"); return browserWindow && browserWindow.getBrowser(); } function GetCurrentPage() { var tabbrowser = GetMostRecentBrowser(); return tabbrowser && tabbrowser.currentURI.spec || ""; // ensure string } function GetCurrentGroup() { var uris = []; var tabbrowser = GetMostRecentBrowser(); if (tabbrowser) { var browsers = tabbrowser.browsers; var browsersLen = browsers.length; for (var i = 0; i < browsersLen; ++i) uris[i] = browsers[i].currentURI.spec; } return uris.join("\n"); } // synchronize button states with current input function CanonifyURLList(aList) { return (aList + "\n").replace(/\n+/g, "\n"); } function UpdateHomePageButtons() { var homePageGroup = CanonifyURLList(GetHomePageGroup()); var currentPage = CanonifyURLList(GetCurrentPage()); var currentGroup = CanonifyURLList(GetCurrentGroup()); // disable "current page" button if current page is already the homepage var currentPageButton = document.getElementById("browserUseCurrent"); currentPageButton.disabled = (homePageGroup == currentPage) || (currentPage == "\n"); // disable "current group" button if current group already set or no group var currentGroupButton = document.getElementById("browserUseCurrentGroup"); currentGroupButton.disabled = (homePageGroup == currentGroup) || (currentGroup == currentPage); // disable "restore" button if homepage hasn't changed var restoreButton = document.getElementById("browserUseDefault"); restoreButton.disabled = (homePageGroup == gDefaultHomePage); } function UpdateHomePagePrefs() { // update the list of s to the current settings var newCount = 0; // current number of homepages var homePageGroup = CanonifyURLList(GetHomePageGroup()).split("\n"); if (homePageGroup[0]) { // we have at least one homepage // (the last index is always empty due to canonification) newCount = homePageGroup.length - 1 for (var i = 0; i < newCount; ++i) { var pref = GetHomePagePref(i) || AddHomePagePref(i); pref.value = homePageGroup[i]; } } // work around bug 410562: // reset unneeded preferences on dialogaccept only // update pref count watermark before setting new number of homepages var alreadyRequested = (gHomePagePrefPeak > 0); var oldCount = GetHomePagePrefCount(); if (gHomePagePrefPeak < oldCount) gHomePagePrefPeak = oldCount; SetHomePagePrefCount(newCount); var needCleanup = (newCount < gHomePagePrefPeak); if (document.documentElement.instantApply) { // throw away unneeded preferences now if (needCleanup) HomePagePrefCleanup(); } else if (needCleanup != alreadyRequested) { // cleanup necessity changed if (needCleanup) { // register OK handler for the capturing phase window.addEventListener("dialogaccept", this.HomePagePrefCleanup, true); } else { // no cleanup necessary, remove OK handler window.removeEventListener("dialogaccept", this.HomePagePrefCleanup, true); } } } function HomePagePrefCleanup() { // remove the old user prefs values that we didn't overwrite var count = GetHomePagePrefCount(); for (var j = count; j < gHomePagePrefPeak; ++j) { // clear var pref = GetHomePagePref(j); pref.reset(); pref.parentNode.removeChild(pref); } gHomePagePrefPeak = 0; // cleanup done } function UpdateHomePageListFromInput() { UpdateHomePagePrefs(); UpdateHomePageButtons(); } function UpdateHomePageList(aSingleURL) { // write single URL into input box and set it as the list of homepages SetHomePageValue(aSingleURL); UpdateHomePageListFromInput(); } function SelectFile() { const nsIFilePicker = Components.interfaces.nsIFilePicker; var fp = Components.classes["@mozilla.org/filepicker;1"] .createInstance(nsIFilePicker); var prefutilitiesBundle = document.getElementById("bundle_prefutilities"); var title = prefutilitiesBundle.getString("choosehomepage"); fp.init(window, title, nsIFilePicker.modeOpen); fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText | nsIFilePicker.filterXML | nsIFilePicker.filterHTML | nsIFilePicker.filterImages); if (fp.show() == nsIFilePicker.returnOK) UpdateHomePageList(fp.fileURL.spec); } function SetHomePageToCurrentPage() { UpdateHomePageList(GetCurrentPage()); } function SetHomePageToCurrentGroup() { UpdateHomePageList(GetCurrentGroup()); } function SetHomePageToDefaultPage() { UpdateHomePageList(gDefaultHomePage); } function Startup() { // homepage groups can have an arbitrary number of s, // thus we create them manually here gPreferences = document.getElementById("navigator_preferences"); var homePageGroup = ""; var count = GetHomePagePrefCount(); for (var i = 0; i < count; ++i) { var pref = AddHomePagePref(i); homePageGroup += pref.value + "\n"; } gDefaultHomePage = CanonifyURLList(GetHomePagePref(0).defaultValue); SetHomePageValue(homePageGroup); UpdateHomePageButtons(); // platform integration InitPlatformIntegration(); } function SwitchPage(aIndex) { document.getElementById("behaviourDeck").selectedIndex = aIndex; } // platform integration function ApplySetAsDefaultBrowser() { // In future, we will use the Shell Service here, // for now, just use WinHooks. window.gWinHooks.winhooks.settings = window.gWinHooks.prefs; } function UpdateDefaultBrowserGroup() { // set description and button state according to integration setting var state = window.gWinHooks.state; var desc = document.getElementById("defaultBrowserDesc"); desc.textContent = desc.getAttribute("desc" + state); document.getElementById("defaultBrowserButton").disabled = (state != PFINT_NOT_DEFAULT); } function InitPlatformIntegration() { // In future, we will ask the Shell Service about platform integration here, // for now, just check WinHooks. var showDefaultBrowserGroup = /Win/.test(navigator.platform); document.getElementById("defaultBrowserGroup").hidden = !showDefaultBrowserGroup; if (!showDefaultBrowserGroup) return; // Determine if we have been selected as the default browser already, and // enable/disable the "Set As Default" button accordingly. // We store our state info in the same place as the code in pref-winhooks.js // uses so that this panel and the Advanced/System panel are kept in sync. if (!("gWinHooks" in window)) { // Neither the Advanced/System panel nor this panel has appeared. // Initialize the state information. window.gWinHooks = {}; // Get winhooks service. window.gWinHooks.winhooks = Components.classes["@mozilla.org/winhooks;1"] .getService(Components.interfaces.nsIWindowsHooks); // Extract current settings (these are what the user has checked on // the Advanced/System panel). window.gWinHooks.prefs = window.gWinHooks.winhooks.settings; } var winHooks = window.gWinHooks; winHooks.state = PFINT_NOT_DEFAULT; // Start by checking http/https/ftp and html/xhtml/xml. var prefs = winHooks.prefs; if (prefs.isHandlingHTTP && prefs.isHandlingHTTPS && prefs.isHandlingFTP && prefs.isHandlingHTML && prefs.isHandlingXHTML && prefs.isHandlingXML) { // The user *wants* us to be the default, so look if the registry matches. // We test the registry settings using a scratch copy of the settings // because we don't care about some of them, but we don't want to mess up // the user's choices from the Advanced/System panel. var testSettings = winHooks.winhooks.settings; // Test that these are set. testSettings.isHandlingHTTP = true; testSettings.isHandlingHTTPS = true; testSettings.isHandlingFTP = true; testSettings.isHandlingHTML = true; testSettings.isHandlingXHTML = true; testSettings.isHandlingXML = true; // Ignore the rest. testSettings.isHandlingCHROME = false; testSettings.isHandlingGOPHER = false; testSettings.isHandlingJPEG = false; testSettings.isHandlingGIF = false; testSettings.isHandlingPNG = false; testSettings.isHandlingBMP = false; testSettings.isHandlingICO = false; testSettings.isHandlingXUL = false; // Now test whether the registry matches that. if (testSettings.registryMatches) winHooks.state = PFINT_DEFAULT; } UpdateDefaultBrowserGroup(); } function SetAsDefaultBrowser() { // In future, we will use the Shell Service here, // for now, just process WinHooks. // Extract current settings (these are what the // user has checked on the Advanced/System panel). var settings = window.gWinHooks.prefs; // Turn on all "default browser" settings. settings.isHandlingHTTP = true; settings.isHandlingHTTPS = true; settings.isHandlingFTP = true; settings.isHandlingHTML = true; settings.isHandlingXHTML = true; settings.isHandlingXML = true; if (document.documentElement.instantApply) { window.gWinHooks.state = PFINT_DEFAULT; ApplySetAsDefaultBrowser(); } else { // register OK handler for the capturing phase window.gWinHooks.state = PFINT_PENDING; window.addEventListener("dialogaccept", this.ApplySetAsDefaultBrowser, true); } UpdateDefaultBrowserGroup(); }