31 Components.utils.import(
"resource://app/jsmodules/DebugUtils.jsm");
32 const LOG = DebugUtils.generateLogFunction(
"searchHandler", 2);
53 SEARCHENGINE_NAME_SONGBIRD:
"Nightingale",
58 init:
function SearchHandler_init() {
62 if (typeof gBrowser ==
'undefined') {
67 gBrowser.addEventListener(
"DOMLinkAdded",
72 gBrowser.addEventListener(
'TabContentChange',
75 gBrowser.addEventListener(
'TabPropertyChange',
80 document.addEventListener(
"search",
85 var stringBundleService =
Cc[
"@mozilla.org/intl/stringbundle;1"].getService(
Ci.nsIStringBundleService);
86 var brand = stringBundleService.createBundle(
"chrome://branding/locale/brand.properties");
87 this.SEARCHENGINE_NAME_SONGBIRD = brand.GetStringFromName(
"brandShortName");
89 this.internalSearchService =
Cc[
"@getnightingale.com/Nightingale/internal-search-service;1"].getService(
Ci.ngIInternalSearchEnginesService);
92 this.internalSearchService.registerInternalSearchEngine(this.SEARCHENGINE_NAME_SONGBIRD,
"songbird-internal-search",
true);
99 uninit:
function SearchHandler_uninit() {
101 this.internalSearchService.unregisterInternalSearchEngine(this.SEARCHENGINE_NAME_SONGBIRD);
112 _previousSearchEngine:
null,
125 onLinkAdded:
function SearchHandler_onLinkAdded(
event) {
128 const target =
event.target;
130 const searchRelRegex = /(^|\s)
search($|\s)/
i;
131 const searchHrefRegex = /^(https?|ftp):\/\
141 if (etype ==
"application/opensearchdescription+xml" &&
142 searchRelRegex.test(
target.rel) && searchHrefRegex.test(
target.href))
144 const targetDoc =
target.ownerDocument;
146 var searchButton = document.getAnonymousElementByAttribute(this.getSearchBar(),
147 "anonid",
"searchbar-engine-button");
149 var
browser = gBrowser.getBrowserForDocument(targetDoc);
152 if (gBrowser.shouldLoadFavIcon(browser.currentURI)) {
153 var faviconService =
Cc[
"@mozilla.org/browser/favicon-service;1"]
154 .getService(
Ci.nsIFaviconService);
156 iconURL = faviconService.getFaviconForPage(
157 browser.contentDocument.documentURIObject).spec;
160 iconURL = iconURL.replace(/^moz-anno:favicon:/,
'');
163 if (Components.lastResult !=
Cr.NS_ERROR_NOT_AVAILABLE)
167 iconURL = browser.currentURI.prePath +
"/favicon.ico";
177 var searchService =
Cc[
"@mozilla.org/browser/search-service;1"]
178 .getService(
Ci.nsIBrowserSearchService);
179 if (searchService.getEngineByName(
target.title))
184 if (browser.hiddenEngines)
185 engines = browser.hiddenEngines;
189 engines = browser.engines;
197 browser.hiddenEngines = engines;
200 browser.engines = engines;
201 if (browser == gBrowser || browser == gBrowser.mCurrentBrowser)
202 this.updateSearchButton();
215 onSearchEvent:
function SearchHandler_onSearchEvent( evt )
217 var searchBar = this.getSearchBar();
218 if (searchBar ==
null) {
219 throw(
"gSearchHandler: Could not process search event. " +
220 "Target did not have a currentEngine property.");
223 var currentEngine = searchBar.currentEngine;
225 var engineEntry = this.internalSearchService.getInternalSearchEngine(currentEngine.name);
226 if (engineEntry!==
null)
234 "@songbirdnest.com/Songbird/" + engineEntry.contractID +
";1";
235 if (contractID in
Cc) {
236 var searchEngine = Cc[
contractID].getService(
Ci.sbISearchEngine);
237 searchEngine.doSearch(
window, searchBar.value);
244 if (searchBar.value ==
"")
248 var submission = currentEngine.getSubmission(searchBar.value,
null);
251 var where =
"current";
257 onTabChanged:
function SearchHandler_onTabChanged(
event) {
260 BrowserSearch.updateSearchButton();
264 BrowserSearch.updateSearchMode();
281 updateSearchButton:
function SearchHandler_updateSearchButton() {
282 var searchButton = document.getAnonymousElementByAttribute(this.getSearchBar(),
283 "anonid",
"searchbar-engine-button");
286 var engines = gBrowser.mCurrentBrowser.engines;
287 if (!engines || engines.length == 0) {
288 if (searchButton.hasAttribute(
"addengines"))
289 searchButton.removeAttribute(
"addengines");
292 searchButton.setAttribute(
"addengines",
"true");
301 webSearch:
function SearchHandler_webSearch() {
302 var searchBar = this.getSearchBar();
307 var ss =
Cc[
"@mozilla.org/browser/search-service;1"].
309 var searchForm = ss.defaultEngine.searchForm;
325 loadSearch:
function SearchHandler_loadSearch(searchText, useNewTab) {
326 var ss =
Cc[
"@mozilla.org/browser/search-service;1"].
332 if (this.getSearchBar())
333 engine = ss.currentEngine;
335 engine = ss.defaultEngine;
337 var submission = engine.getSubmission(searchText,
null);
348 submission.postData,
null,
false);
350 loadURI(submission.uri.spec,
null, submission.postData,
false);
357 getSearchBar:
function SearchHandler_getSearchBar() {
359 var elements = document.getElementsByTagName(
"searchbar");
360 if (elements && elements.length > 0) {
369 getSongbirdSearchEngines:
function SearchHandler_getSongbirdSearchEngines() {
370 var songbirdEngines = this.getSearchBar().searchService.getEngines({});
371 if (!songbirdEngines) {
372 LOG(
"\n\nCould not find any search engines for Songbird.\n");
374 return songbirdEngines;
380 getSongbirdSearchEngine:
381 function SearchHandler_getSongbirdSearchEngine(
aName) {
383 aName = this.SEARCHENGINE_NAME_SONGBIRD;
384 var songbirdEngine = this.getSearchBar()
386 .getEngineByName(
aName);
387 if (!songbirdEngine) {
388 LOG(
"\n\nThe Songbird search engine with name \"" +
aName +
389 "\" could not be found.\n");
391 return songbirdEngine;
394 loadAddEngines:
function SearchHandler_loadAddEngines() {
397 var regionBundle = document.getElementById(
"bundle_browser_region");
399 var formatter =
Cc[
"@mozilla.org/toolkit/URLFormatterService;1"].getService(
Ci.nsIURLFormatter);
400 var searchEnginesURL = formatter.formatURLPref(
"browser.search.searchEnginesURL");
415 updateSearchMode:
function SearchHandler_updateSearchMode() {
419 if (gBrowser.loading) {
425 if (this._isMediaTabOrMediaPageShowing())
427 this._switchToInternalSearch();
433 this._switchToWebSearch();
443 _switchToInternalSearch:
function SearchHandler__switchToInternalSearch() {
444 var searchBar = this.getSearchBar();
445 var currentEngine = searchBar.currentEngine;
448 if (this.internalSearchService.getInternalSearchEngine(currentEngine.name)===
null)
450 this._previousSearchEngine = currentEngine;
451 this._previousSearch = searchBar.value;
460 name = node.searchtype;
463 var engine = this.getSongbirdSearchEngine(name);
467 var liveSearchEnabled =
false;
469 if (this.internalSearchService.getInternalSearchEngine(name).liveSearch) {
471 Application.prefs.getValue(
"songbird.livesearch.enabled",
true);
475 searchBar.setLiveSearchMode(engine, liveSearchEnabled);
478 if (currentEngine != engine) {
482 searchBar.value =
"";
483 searchBar.currentEngine = engine;
487 this._syncSearchBarToMediaPage(name);
489 searchBar.updateDisplay();
496 _switchToWebSearch:
function SearchHandler__switchToWebSearch() {
497 var searchBar = this.getSearchBar()
498 var currentEngine = searchBar.currentEngine;
503 if (searchBar.isInLiveSearchMode(currentEngine)) {
504 searchBar.setLiveSearchMode(currentEngine,
false);
505 searchBar.setEngineDisplayText(currentEngine,
null);
510 if (this.internalSearchService.getInternalSearchEngine(currentEngine.name)!==
null)
515 if (this._previousSearchEngine)
517 searchBar.value =
"";
518 searchBar.currentEngine = this._previousSearchEngine;
521 searchBar.value = this._previousSearch;
526 BrowserSearch._syncSearchBarToMediaPage();
528 searchBar.updateDisplay();
537 _isMediaTabOrMediaPageShowing:
538 function SearchHandler__isMediaTabOrMediaPageShowing() {
539 return (gBrowser.mediaTab == gBrowser.selectedTab ||
540 gBrowser.currentMediaPage !=
null);
548 _getSearchEventTarget:
function SearchHandler__getSearchEventTarget(evt) {
552 if (evt.target && evt.target.currentEngine) {
555 }
else if (evt.originalTarget && evt.originalTarget.currentEngine) {
556 target = evt.originalTarget;
558 }
else if (evt.target && evt.target.wrappedJSObject &&
559 evt.target.wrappedJSObject.currentEngine)
561 target = evt.target.wrappedJSObject;
563 }
else if (evt.originalTarget && evt.originalTarget.wrappedJSObject &&
564 evt.originalTarget.wrappedJSObject.currentEngine)
566 target = evt.originalTarget.wrappedJSObject;
569 dump(
"\ngSearchHandler: Error! Search event received from" +
570 " a target with no currentEngine!\n");
571 try { dump(
"\ttarget " + evt.target.tagName +
"\n"); }
catch (e) {};
572 try { dump(
"\toriginalTarget " + evt.originalTarget.tagName +
"\n"); }
catch (e) {};
582 _getMediaPageSearch:
function SearchHandler__getMediaPageSearch() {
584 var mediaListView = this._getCurrentMediaListView();
588 if (mediaListView && mediaListView.searchConstraint) {
589 var
search = mediaListView.searchConstraint;
591 var groupCount = search.groupCount;
592 for (var
i = 0;
i < groupCount;
i++) {
593 var
group = search.getGroup(
i);
594 var
property = group.properties.getNext();
595 terms.push(group.getValues(property).getNext());
597 return terms.join(
" ");
607 _setMediaPageSearch:
function SearchHandler__setMediaPageSearch(query) {
609 var mediaListView = this._getCurrentMediaListView();
612 if (!mediaListView || !mediaListView.cascadeFilterSet) {
613 Cu.reportError(
"Error: no cascade filter set!");
618 var filters = mediaListView.cascadeFilterSet;
620 var searchIndex = -1;
621 for (let
i = 0;
i < filters.length; ++
i) {
622 if (filters.isSearch(
i)) {
627 if (searchIndex < 0) {
628 searchIndex = filters.appendSearch([
"*"], 1);
631 if (query ==
"" || query ==
null) {
632 filters.set(searchIndex, [], 0);
635 var stringTransform =
636 Cc[
"@songbirdnest.com/Songbird/Intl/StringTransform;1"]
637 .createInstance(
Ci.sbIStringTransform);
639 query = stringTransform.normalizeString(
"",
640 Ci.sbIStringTransform.TRANSFORM_IGNORE_NONSPACE,
643 var valArray = query.split(
" ");
645 filters.set(searchIndex, valArray, valArray.length);
654 _getMediaPageDisplayName:
function SearchHandler__getMediaPageDisplayName() {
657 var mediaListView = this._getCurrentMediaListView();
660 return mediaListView?mediaListView.mediaList.name:
"";
668 _getCurrentMediaListView:
function SearchHandler__getCurrentMediaListView() {
669 if (gBrowser.currentMediaPage && gBrowser.currentMediaPage.mediaListView) {
670 return gBrowser.currentMediaPage.mediaListView;
681 _syncSearchBarToMediaPage:
682 function SearchHandler__syncSearchBarToMediaPage(
aName) {
686 var mediaListView = this._getCurrentMediaListView();
687 if (!mediaListView)
return;
690 var searchBar = this.getSearchBar();
691 if (searchBar ==
null) {
697 var mediaPageName = this._getMediaPageDisplayName();
698 if (mediaPageName !=
"") {
699 var engine = this.getSongbirdSearchEngine(
aName);
700 searchBar.setEngineDisplayText(engine, mediaPageName);
704 var queryString = this._getMediaPageSearch();
705 searchBar.value = queryString;
716 window.addEventListener(
"load",
723 window.addEventListener(
"unload",
function openUILinkIn(url, where, allowThirdPartyFixup, postData, referrerUrl)
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
const gSearchHandler
Songbird Search Handler.
getService(Ci.sbIFaceplateManager)
function search(aFolderId, aSearchStr, aExpectedScopeButtonId)
function loadURI(uri, referrer, postData, allowThirdPartyFixup)
_getSelectedPageStyle s i