Bug 337491: Allow disabling search suggestions. r=gavin, ui-r=beltzner, sr=beng

This commit is contained in:
pamg.bugs%gmail.com 2006-07-20 22:54:19 +00:00
parent bf33bbb75c
commit bb15006068
7 changed files with 161 additions and 13 deletions

View File

@ -229,6 +229,9 @@ pref("browser.search.openintab", false);
// send ping to the server to update
pref("browser.search.update", true);
// enable search suggestions by default
pref("browser.search.suggest.enabled", true);
pref("browser.history.grouping", "day");
pref("browser.sessionhistory.max_entries", 50);

View File

@ -21,6 +21,7 @@
# Contributor(s):
# Ben Goodger <beng@google.com> (Original author)
# Gavin Sharp <gavin@gavinsharp.com>
# Pamela Greene <pamg.bugs@gmail.com>
#
# 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
@ -41,12 +42,19 @@ const Cc = Components.classes;
const ENGINE_FLAVOR = "text/x-moz-search-engine";
const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled";
var gEngineView = null;
var gEngineManagerDialog = {
init: function engineManager_init() {
gEngineView = new EngineView(new EngineStore());
var prefService = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
var suggestEnabled = prefService.getBoolPref(BROWSER_SUGGEST_PREF);
document.getElementById("enableSuggest").checked = suggestEnabled;
var tree = document.getElementById("engineList");
tree.view = gEngineView;
@ -81,6 +89,12 @@ var gEngineManagerDialog = {
getService(Ci.nsIObserverService);
os.removeObserver(this, "browser-search-engine-modified");
// Set the preference
var newSuggestEnabled = document.getElementById("enableSuggest").checked;
var prefService = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
prefService.setBoolPref(BROWSER_SUGGEST_PREF, newSuggestEnabled);
// Commit the changes
gEngineView._engineStore.commit();
},

View File

@ -105,6 +105,12 @@
command="cmd_remove"/>
</vbox>
</hbox>
<hbox>
<checkbox id="enableSuggest"
label="&enableSuggest.label;"
accesskey="&enableSuggest.accesskey;"/>
<spacer flex="1"/>
</hbox>
<hbox>
<label id="addEngines" class="text-link" value="&addEngine.label;"
onclick="gEngineManagerDialog.loadAddEngines();"/>

View File

@ -124,7 +124,7 @@
<destructor><![CDATA[
var os = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
.getService(Components.interfaces.nsIObserverService);
os.removeObserver(this, "browser-search-engine-modified");
// Make sure to break the cycle from _texbox to us. Otherwise we leak
@ -548,7 +548,7 @@
<binding id="searchbar-textbox"
extends="chrome://global/content/bindings/autocomplete.xml#autocomplete">
<implementation>
<implementation implements="nsIObserver">
<constructor><![CDATA[
if (this._getParentSearchbar().parentNode.parentNode.localName ==
"toolbarpaletteitem")
@ -557,6 +557,10 @@
]]></constructor>
<destructor><![CDATA[
var ps2 = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch2);
ps2.removeObserver("browser.search.suggest.enabled", this);
// Because XBL and the customize toolbar code interacts poorly,
// there may not be anything to remove here
try {
@ -567,9 +571,12 @@
<field name="_stringBundle"/>
<field name="_formHistSvc"/>
<field name="_prefBranch"/>
<field name="_suggestMenuItem"/>
<field name="_suggestEnabled"/>
<method name="initialize">
<body><![CDATA[
// Initialize fields
this._stringBundle = this._getParentSearchbar()._stringBundle;
this._formHistSvc =
Components.classes["@mozilla.org/satchel/form-history;1"]
@ -577,10 +584,13 @@
this._prefBranch =
Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch);
this._suggestEnabled =
this._prefBranch.getBoolPref("browser.search.suggest.enabled");
if (this._prefBranch.getBoolPref("browser.urlbar.clickSelectsAll"))
this.setAttribute("clickSelectsAll", true);
// Add items to context menu and attach controller to handle them
var textBox = document.getAnonymousElementByAttribute(this,
"anonid", "textbox-input-box");
var cxmenu = document.getAnonymousElementByAttribute(textBox,
@ -589,15 +599,47 @@
var element = document.createElementNS(XUL_NS, "menuseparator");
cxmenu.appendChild(element);
element = document.createElementNS(XUL_NS, "menuitem");
const label = this._stringBundle.getString("cmd_clearHistory");
const akey = this._stringBundle.getString("cmd_clearHistory_accesskey");
var label = this._stringBundle.getString("cmd_clearHistory");
var akey = this._stringBundle.getString("cmd_clearHistory_accesskey");
element.setAttribute("label", label);
element.setAttribute("accesskey", akey);
element.setAttribute("cmd", "cmd_clearhistory");
cxmenu.appendChild(element);
element = document.createElementNS(XUL_NS, "menuitem");
label = this._stringBundle.getString("cmd_showSuggestions");
akey = this._stringBundle.getString("cmd_showSuggestions_accesskey");
element.setAttribute("anonid", "toggle-suggest-item");
element.setAttribute("label", label);
element.setAttribute("accesskey", akey);
element.setAttribute("cmd", "cmd_togglesuggest");
element.setAttribute("type", "checkbox");
element.setAttribute("checked", this._suggestEnabled);
element.setAttribute("autocheck", "false");
this._suggestMenuItem = element;
cxmenu.appendChild(element);
this.controllers.appendController(this.searchbarController);
// Add observer for suggest preference
var ps2 = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch2);
ps2.addObserver("browser.search.suggest.enabled", this, false);
]]></body>
</method>
<method name="observe">
<parameter name="aSubject"/>
<parameter name="aTopic"/>
<parameter name="aData"/>
<body><![CDATA[
if (aTopic == "nsPref:changed") {
this._suggestEnabled =
this._prefBranch.getBoolPref("browser.search.suggest.enabled");
this._suggestMenuItem.setAttribute("checked", this._suggestEnabled);
}
]]></body>
</method>
@ -665,18 +707,34 @@
<field name="searchbarController" readonly="true"><![CDATA[({
_self: this,
supportsCommand: function(aCommand) {
return aCommand == "cmd_clearhistory";
return aCommand == "cmd_clearhistory" ||
aCommand == "cmd_togglesuggest";
},
isCommandEnabled: function(aCommand) {
return this._self._formHistSvc.nameExists(
this._self.getAttribute("autocompletesearchparam"));
if (aCommand == "cmd_clearhistory") {
var param = this._self.getAttribute("autocompletesearchparam");
return this._self._formHistSvc.nameExists(param);
}
return true;
},
doCommand: function (aCommand) {
this._self._formHistSvc.removeEntriesForName(
this._self.getAttribute("autocompletesearchparam"));
this._self.value = "";
switch (aCommand) {
case "cmd_clearhistory":
var param = this._self.getAttribute("autocompletesearchparam");
this._self._formHistSvc.removeEntriesForName(param);
this._self.value = "";
break;
case "cmd_togglesuggest":
// The pref observer will update _suggestEnabled and the menu
// checkmark.
this._self._prefBranch.setBoolPref("browser.search.suggest.enabled",
!this._self._suggestEnabled);
break;
default:
// do nothing with unrecognized command
}
}
})]]></field>

View File

@ -21,6 +21,7 @@
* Ben Goodger <beng@google.com>
* Mike Connor <mconnor@mozilla.com>
* Joe Hughes <joe@retrovirus.com>
* Pamela Greene <pamg.bugs@gmail.com>
*
* 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
@ -38,6 +39,10 @@
const SEARCH_RESPONSE_SUGGESTION_JSON = "application/x-suggestions+json";
const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled";
const XPCOM_SHUTDOWN_TOPIC = "xpcom-shutdown";
const NS_PREFBRANCH_PREFCHANGE_TOPIC_ID = "nsPref:changed";
/**
* Metadata describing the Web Search suggest mode
*/
@ -219,9 +224,16 @@ SuggestAutoCompleteResult.prototype = {
* the logic for two providers is identical.
* @constructor
*/
function SuggestAutoComplete() {}
function SuggestAutoComplete() {
this._init();
}
SuggestAutoComplete.prototype = {
_init: function() {
this._addObservers();
this._loadSuggestPref();
},
/**
* this._strings is the string bundle for message internationalization.
*/
@ -236,6 +248,16 @@ SuggestAutoComplete.prototype = {
},
__strings: null,
/**
* Search suggestions will be shown if this._suggestEnabled is true.
*/
_loadSuggestPref: function SAC_loadSuggestPref() {
var prefService = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
this._suggestEnabled = prefService.getBoolPref(BROWSER_SUGGEST_PREF);
},
_suggestEnabled: null,
/*************************************************************************
* Server request backoff implementation fields below
* These allow us to throttle requests if the server is getting hammered.
@ -607,11 +629,13 @@ SuggestAutoComplete.prototype = {
this._checkForEngineSwitch(engine);
if (!searchString ||
!this._suggestEnabled ||
!engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON) ||
!this._okToRequest()) {
// We have an empty search string (user pressed down arrow to see
// history), or the current engine has no suggest functionality,
// or we're in backoff mode; so just use local history.
// history), or search suggestions are disabled, or the current engine
// has no suggest functionality, or we're in backoff mode; so just use
// local history.
this._sentSuggestRequest = false;
this._startHistorySearch(searchString, searchParam, previousResult);
return;
@ -650,6 +674,40 @@ SuggestAutoComplete.prototype = {
}
},
/**
* nsIObserver
*/
observe: function SAC_observe(aSubject, aTopic, aData) {
switch (aTopic) {
case NS_PREFBRANCH_PREFCHANGE_TOPIC_ID:
this._loadSuggestPref();
break;
case XPCOM_SHUTDOWN_TOPIC:
this._removeObservers();
break;
}
},
_addObservers: function SAC_addObservers() {
var prefService2 = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch2);
prefService2.addObserver(BROWSER_SUGGEST_PREF, this, false);
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.addObserver(this, XPCOM_SHUTDOWN_TOPIC, false);
},
_removeObservers: function SAC_removeObservers() {
var prefService2 = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch2);
prefService2.removeObserver(BROWSER_SUGGEST_PREF, this);
var os = Cc["@mozilla.org/observer-service;1"].
getService(Ci.nsIObserverService);
os.removeObserver(this, XPCOM_SHUTDOWN_TOPIC);
},
/**
* Part of nsISupports implementation.
* @param iid requested interface identifier
@ -671,6 +729,9 @@ SuggestAutoComplete.prototype = {
* @constructor
*/
function SearchSuggestAutoComplete() {
// This calls _init() in the parent class (SuggestAutoComplete) via the
// prototype, below.
this._init();
}
SearchSuggestAutoComplete.prototype = {
__proto__: SuggestAutoComplete.prototype,

View File

@ -12,3 +12,6 @@
<!ENTITY addEngine.label "Get more search engines...">
<!ENTITY addEngine.accesskey "A">
<!ENTITY enableSuggest.label "Show search suggestions">
<!ENTITY enableSuggest.accesskey "S">

View File

@ -3,6 +3,9 @@ searchtip=Search using %S
cmd_clearHistory=Clear Search History
cmd_clearHistory_accesskey=C
cmd_showSuggestions=Show Suggestions
cmd_showSuggestions_accesskey=S
addEngineConfirmTitle=Add Search Engine
addEngineConfirmation=Add "%S" to the list of engines available in the search bar?\n\nFrom: %S
addEngineUseNowText=Start &using it right away