home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Komunik / phoenix / chrome / browser.jar / content / browser / search.xml < prev    next >
Extensible Markup Language  |  2002-09-29  |  6KB  |  157 lines

  1. <?xml version="1.0"?>
  2.  
  3. <bindings id="SearchBindings"
  4.           xmlns="http://www.mozilla.org/xbl"
  5.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  6.           xmlns:xbl="http://www.mozilla.org/xbl">
  7.  
  8.   <binding id="searchbar" extends="chrome://global/content/widgets/autocomplete.xml#autocomplete">
  9.     <implementation>
  10.       <constructor><![CDATA[
  11.         var pbi = this.mPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  12.         if (pbi)
  13.           pbi.addObserver("browser.search", this.mPrefObserver, false);
  14.  
  15.         if (this.hasAttribute("searchmode"))
  16.           updateEngines();
  17.       ]]></constructor>
  18.  
  19.       <destructor><![CDATA[
  20.         var pbi = this.mPrefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  21.         if (pbi)
  22.           pbi.removeObserver("browser.search", this.mPrefObserver);
  23.       ]]></destructor>
  24.  
  25.       <field name="mPrefs">
  26.         var svc = Components.classes["@mozilla.org/preferences-service;1"]
  27.                             .getService(Components.interfaces.nsIPrefService);
  28.         svc.getBranch(null);
  29.       </field>
  30.  
  31.       <field name="mPrefObserver"><![CDATA[
  32.         ({
  33.           searchBar: this,
  34.           
  35.           observe: function(aObserver, aBlah, aPref) {
  36.             if (aPref.indexOf("browser.search") == 0)
  37.               this.searchBar.updateEngines();
  38.           }
  39.         });
  40.       ]]></field>
  41.       
  42.       <field name="enginesReady">false</field>
  43.  
  44.       <field name="currentSearchId">""</field>
  45.  
  46.       <field name="currentSearchURL">""</field>
  47.  
  48.       <field name="currentSearchName">""</field>
  49.  
  50.       <field name="currentSearchIcon">""</field>
  51.  
  52.       <property name="searchMode"
  53.                 onget="return this.getAttribute('searchmode');"
  54.                 onset="this.setAttribute('searchmode', val); return val;"/>
  55.       
  56.       <property name="searchValue" readonly="true">
  57.         <getter>
  58.         <![CDATA[
  59.           if (!this.enginesReady)
  60.             this.updateEngines();
  61.  
  62.           const ISEARCH_CONTRACTID = "@mozilla.org/rdf/datasource;1?name=internetsearch";
  63.           const nsIInternetSearchService = Components.interfaces.nsIInternetSearchService;           
  64.           var searchService = Components.classes[ISEARCH_CONTRACTID].getService(nsIInternetSearchService);
  65.           if (this.currentSearchURL)
  66.             return this.currentSearchURL;
  67.  
  68.           return searchService.GetInternetSearchURL(this.currentSearchId, escape(this.value), 0, 0, {value:0});
  69.         ]]>
  70.         </getter>
  71.       </property>
  72.  
  73.       <method name="updateEngines">
  74.         <body><![CDATA[
  75.           var rdf = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  76.                       .getService(Components.interfaces.nsIRDFService);
  77.           try {
  78.             var ds = rdf.GetDataSource("rdf:internetsearch");
  79.           } catch (ex) {
  80.             // sometimes bad profiles cause this error, which horks the searchBar
  81.             return;
  82.           }
  83.                               
  84.           const kNC_Name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
  85.           const kNC_Icon = rdf.GetResource("http://home.netscape.com/NC-rdf#Icon");
  86.           const kNC_searchBarUrl = rdf.GetResource("http://home.netscape.com/NC-rdf#actionBar");
  87.         
  88.           var defaultEngine = null;
  89.           try {
  90.             defaultEngine = this.mPrefs.getComplexValue("browser.search.defaultengine",
  91.                                                         Components.interfaces.nsISupportsString).data;
  92.           } catch(ex) {
  93.             this.ensureDefaultEnginePrefs(rdf, ds);
  94.             defaultEngine = this.mPrefs.getComplexValue("browser.search.defaultengine",
  95.                                                         Components.interfaces.nsISupportsString).data;
  96.           }
  97.           
  98.           if (defaultEngine) {
  99.             if (ds) {
  100.               var res = rdf.GetResource(defaultEngine);
  101.               try {
  102.                 this.currentSearchURL = this.readRDFString(ds, res, kNC_searchBarUrl);
  103.               } catch(ex) { }
  104.               
  105.               this.currentSearchId = res.Value;
  106.               this.currentSearchName = this.readRDFString(ds, res, kNC_Name);
  107.               this.currentSearchIcon = this.readRDFString(ds, res, kNC_Icon);
  108.  
  109.               if (this.firstChild && this.firstChild.localName == "image");
  110.                 this.firstChild.setAttribute("src", this.currentSearchIcon);
  111.             }
  112.           }
  113.           
  114.           this.enginesReady = true;
  115.         ]]></body>
  116.       </method>
  117.  
  118.       <method name="ensureDefaultEnginePrefs">
  119.         <parameter name="aRDF"/>
  120.         <parameter name="aDS"/>
  121.         <body><![CDATA[
  122.           var defaultName = this.mPrefs.getComplexValue("browser.search.defaultenginename",
  123.                                                         Components.interfaces.nsIPrefLocalizedString).data;
  124.           const kNC_Root = aRDF.GetResource("NC:SearchEngineRoot");
  125.           const kNC_child = aRDF.GetResource("http://home.netscape.com/NC-rdf#child");
  126.           const kNC_Name = aRDF.GetResource("http://home.netscape.com/NC-rdf#Name");
  127.           
  128.           var arcs = aDS.GetTargets(kNC_Root, kNC_child, true);
  129.           while (arcs.hasMoreElements()) {
  130.             var engineRes = arcs.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  131.             var name = this.readRDFString(aDS, engineRes, kNC_Name);
  132.             if (name == defaultName) {
  133.               var str = Components.classes["@mozilla.org/supports-string;1"]
  134.                                   .createInstance(Components.interfaces.nsISupportsString);
  135.               str.data = engineRes.Value;
  136.               this.mPrefs.setComplexValue("browser.search.defaultengine",
  137.                                           Components.interfaces.nsISupportsString,
  138.                                           str);
  139.             }
  140.           }
  141.         ]]></body>
  142.       </method>
  143.  
  144.       <method name="readRDFString">
  145.         <parameter name="aDS"/>
  146.         <parameter name="aRes"/>
  147.         <parameter name="aProp"/>
  148.         <body><![CDATA[
  149.           var n = aDS.GetTarget(aRes, aProp, true);
  150.           return n ? n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value : null;
  151.         ]]></body>
  152.       </method>
  153.     </implementation>
  154.   </binding>
  155.  
  156. </bindings>
  157.