mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-15 21:36:20 +00:00
Bug 343707: Add Restore Default Search Engines button to search manager, patch by Michael Wu <michael.wu@mozilla.com>, r=me
This commit is contained in:
parent
cefc0731a0
commit
599cc60aa0
@ -106,6 +106,16 @@ var gEngineManagerDialog = {
|
|||||||
os.removeObserver(this, "browser-search-engine-modified");
|
os.removeObserver(this, "browser-search-engine-modified");
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onRestoreDefaults: function engineManager_onRestoreDefaults() {
|
||||||
|
var num = gEngineView._engineStore.restoreDefaultEngines();
|
||||||
|
gEngineView.rowCountChanged(0, num);
|
||||||
|
gEngineView.invalidate();
|
||||||
|
},
|
||||||
|
|
||||||
|
showRestoreDefaults: function engineManager_showRestoreDefaults(val) {
|
||||||
|
document.documentElement.getButton("extra2").disabled = !val;
|
||||||
|
},
|
||||||
|
|
||||||
loadAddEngines: function engineManager_loadAddEngines() {
|
loadAddEngines: function engineManager_loadAddEngines() {
|
||||||
this.onOK();
|
this.onOK();
|
||||||
window.opener.BrowserSearch.loadAddEngines();
|
window.opener.BrowserSearch.loadAddEngines();
|
||||||
@ -205,15 +215,38 @@ EngineRemoveOp.prototype = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function EngineUnhideOp(aEngineClone, aNewIndex) {
|
||||||
|
if (!aEngineClone)
|
||||||
|
throw new Error("bad args to new EngineUnhideOp!");
|
||||||
|
this._engine = aEngineClone.originalEngine;
|
||||||
|
this._newIndex = aNewIndex;
|
||||||
|
}
|
||||||
|
EngineUnhideOp.prototype = {
|
||||||
|
_engine: null,
|
||||||
|
_newIndex: null,
|
||||||
|
commit: function EUO_commit() {
|
||||||
|
this._engine.hidden = false;
|
||||||
|
var searchService = Cc["@mozilla.org/browser/search-service;1"].
|
||||||
|
getService(Ci.nsIBrowserSearchService);
|
||||||
|
searchService.moveEngine(this._engine, this._newIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function EngineStore() {
|
function EngineStore() {
|
||||||
var searchService = Cc["@mozilla.org/browser/search-service;1"].
|
var searchService = Cc["@mozilla.org/browser/search-service;1"].
|
||||||
getService(Ci.nsIBrowserSearchService);
|
getService(Ci.nsIBrowserSearchService);
|
||||||
this._engines = searchService.getVisibleEngines({}).map(this._cloneEngine);
|
this._engines = searchService.getVisibleEngines({}).map(this._cloneEngine);
|
||||||
|
this._defaultEngines = searchService.getDefaultEngines({}).map(this._cloneEngine);
|
||||||
|
|
||||||
this._ops = [];
|
this._ops = [];
|
||||||
|
|
||||||
|
// check if we need to disable the restore defaults button
|
||||||
|
var someHidden = this._defaultEngines.some(function (e) {return e.hidden;});
|
||||||
|
gEngineManagerDialog.showRestoreDefaults(someHidden);
|
||||||
}
|
}
|
||||||
EngineStore.prototype = {
|
EngineStore.prototype = {
|
||||||
_engines: null,
|
_engines: null,
|
||||||
|
_defaultEngines: null,
|
||||||
_ops: null,
|
_ops: null,
|
||||||
|
|
||||||
get engines() {
|
get engines() {
|
||||||
@ -221,6 +254,7 @@ EngineStore.prototype = {
|
|||||||
},
|
},
|
||||||
set engines(val) {
|
set engines(val) {
|
||||||
this._engines = val;
|
this._engines = val;
|
||||||
|
return val;
|
||||||
},
|
},
|
||||||
|
|
||||||
_getIndexForEngine: function ES_getIndexForEngine(aEngine) {
|
_getIndexForEngine: function ES_getIndexForEngine(aEngine) {
|
||||||
@ -235,9 +269,23 @@ EngineStore.prototype = {
|
|||||||
return newO;
|
return newO;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Callback for Array's some(). A thisObj must be passed to some()
|
||||||
|
_isSameEngine: function ES_isSameEngine(aEngineClone) {
|
||||||
|
return aEngineClone.originalEngine == this.originalEngine;
|
||||||
|
},
|
||||||
|
|
||||||
commit: function ES_commit() {
|
commit: function ES_commit() {
|
||||||
|
var searchService = Cc["@mozilla.org/browser/search-service;1"].
|
||||||
|
getService(Ci.nsIBrowserSearchService);
|
||||||
|
var currentEngine = this._cloneEngine(searchService.currentEngine);
|
||||||
for (var i = 0; i < this._ops.length; i++)
|
for (var i = 0; i < this._ops.length; i++)
|
||||||
this._ops[i].commit();
|
this._ops[i].commit();
|
||||||
|
|
||||||
|
// Restore currentEngine if it is a default engine that is still visible.
|
||||||
|
// Needed if the user deletes currentEngine and then restores it.
|
||||||
|
if (this._defaultEngines.some(this._isSameEngine, currentEngine) &&
|
||||||
|
!currentEngine.originalEngine.hidden)
|
||||||
|
searchService.currentEngine = currentEngine.originalEngine;
|
||||||
},
|
},
|
||||||
|
|
||||||
addEngine: function ES_addEngine(aEngine) {
|
addEngine: function ES_addEngine(aEngine) {
|
||||||
@ -265,6 +313,23 @@ EngineStore.prototype = {
|
|||||||
|
|
||||||
this._engines.splice(index, 1);
|
this._engines.splice(index, 1);
|
||||||
this._ops.push(new EngineRemoveOp(aEngine));
|
this._ops.push(new EngineRemoveOp(aEngine));
|
||||||
|
if (this._defaultEngines.some(this._isSameEngine, aEngine))
|
||||||
|
gEngineManagerDialog.showRestoreDefaults(true);
|
||||||
|
},
|
||||||
|
|
||||||
|
restoreDefaultEngines: function ES_restoreDefaultEngines() {
|
||||||
|
var i = 0;
|
||||||
|
for each (var e in this._defaultEngines) {
|
||||||
|
// skip adding engine if the engine is already in the list
|
||||||
|
if (this._engines.some(this._isSameEngine, e))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
this._engines.splice(i, 0, e);
|
||||||
|
this._ops.push(new EngineUnhideOp(e, i));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
gEngineManagerDialog.showRestoreDefaults(false);
|
||||||
|
return i;
|
||||||
},
|
},
|
||||||
|
|
||||||
reloadIcons: function ES_reloadIcons() {
|
reloadIcons: function ES_reloadIcons() {
|
||||||
|
@ -44,10 +44,13 @@
|
|||||||
|
|
||||||
<dialog id="engineManager"
|
<dialog id="engineManager"
|
||||||
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
|
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
|
||||||
buttons="accept,cancel"
|
buttons="accept,cancel,extra2"
|
||||||
|
buttonlabelextra2="&restoreDefaults.label;"
|
||||||
|
buttonaccesskeyextra2="&restoreDefaults.accesskey;"
|
||||||
onload="gEngineManagerDialog.init();"
|
onload="gEngineManagerDialog.init();"
|
||||||
ondialogaccept="gEngineManagerDialog.onOK();"
|
ondialogaccept="gEngineManagerDialog.onOK();"
|
||||||
ondialogcancel="gEngineManagerDialog.onCancel();"
|
ondialogcancel="gEngineManagerDialog.onCancel();"
|
||||||
|
ondialogextra2="gEngineManagerDialog.onRestoreDefaults();"
|
||||||
title="&engineManager.title;"
|
title="&engineManager.title;"
|
||||||
style="&engineManager.style;"
|
style="&engineManager.style;"
|
||||||
persist="screenX screenY"
|
persist="screenX screenY"
|
||||||
|
@ -162,7 +162,7 @@ interface nsISearchEngine : nsISupports
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
[scriptable, uuid(0c6164fe-580d-497f-87d3-1bf36006cb7c)]
|
[scriptable, uuid(8307b8f2-08ea-45b8-96bf-b1dc7688fe3b)]
|
||||||
interface nsIBrowserSearchService : nsISupports
|
interface nsIBrowserSearchService : nsISupports
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -228,7 +228,8 @@ interface nsIBrowserSearchService : nsISupports
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Un-hides all engines installed in the directory corresponding to
|
* Un-hides all engines installed in the directory corresponding to
|
||||||
* the directory service's NS_APP_SEARCH_DIR key.
|
* the directory service's NS_APP_SEARCH_DIR key. (i.e. the set of
|
||||||
|
* engines returned by getDefaultEngines)
|
||||||
*/
|
*/
|
||||||
void restoreDefaultEngines();
|
void restoreDefaultEngines();
|
||||||
|
|
||||||
@ -271,6 +272,15 @@ interface nsIBrowserSearchService : nsISupports
|
|||||||
out unsigned long engineCount,
|
out unsigned long engineCount,
|
||||||
[retval, array, size_is(engineCount)] out nsISearchEngine engines);
|
[retval, array, size_is(engineCount)] out nsISearchEngine engines);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of all default search engines.
|
||||||
|
*
|
||||||
|
* @returns an array of nsISearchEngine objects.
|
||||||
|
*/
|
||||||
|
void getDefaultEngines(
|
||||||
|
out unsigned long engineCount,
|
||||||
|
[retval, array, size_is(engineCount)] out nsISearchEngine engines);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Moves a search engine.
|
* Moves a search engine.
|
||||||
*
|
*
|
||||||
|
@ -2537,6 +2537,43 @@ SearchService.prototype = {
|
|||||||
return engines;
|
return engines;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getDefaultEngines: function SRCH_SVC_getDefault(aCount) {
|
||||||
|
function isDefault (engine) {
|
||||||
|
return engine._isInAppDir;
|
||||||
|
};
|
||||||
|
var engines = this._sortedEngines.filter(isDefault);
|
||||||
|
var prefB = Cc["@mozilla.org/preferences-service;1"].
|
||||||
|
getService(Ci.nsIPrefService).
|
||||||
|
getBranch(BROWSER_SEARCH_PREF + "order.");
|
||||||
|
var engineOrder = {};
|
||||||
|
var i = 1;
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
engineOrder[prefB.getCharPref(i)] = i++;
|
||||||
|
} catch (ex) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function compareEngines (a, b) {
|
||||||
|
var aIdx = engineOrder[a.name];
|
||||||
|
var bIdx = engineOrder[b.name];
|
||||||
|
|
||||||
|
if (aIdx && bIdx)
|
||||||
|
return aIdx - bIdx;
|
||||||
|
if (aIdx)
|
||||||
|
return -1;
|
||||||
|
if (bIdx)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return a.name < b.name ? -1 : 1;
|
||||||
|
}
|
||||||
|
engines.sort(compareEngines);
|
||||||
|
|
||||||
|
aCount.value = engines.length;
|
||||||
|
return engines;
|
||||||
|
},
|
||||||
|
|
||||||
getEngineByName: function SRCH_SVC_getEngineByName(aEngineName) {
|
getEngineByName: function SRCH_SVC_getEngineByName(aEngineName) {
|
||||||
return this._engines[aEngineName] || null;
|
return this._engines[aEngineName] || null;
|
||||||
},
|
},
|
||||||
|
@ -15,3 +15,6 @@
|
|||||||
|
|
||||||
<!ENTITY enableSuggest.label "Show search suggestions">
|
<!ENTITY enableSuggest.label "Show search suggestions">
|
||||||
<!ENTITY enableSuggest.accesskey "S">
|
<!ENTITY enableSuggest.accesskey "S">
|
||||||
|
|
||||||
|
<!ENTITY restoreDefaults.label "Restore Defaults">
|
||||||
|
<!ENTITY restoreDefaults.accesskey "e">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user