<?xml version="1.0"?>
<!-- ***** 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 Mobile Browser.
   -
   - The Initial Developer of the Original Code is
   - Mozilla Corporation.
   - Portions created by the Initial Developer are Copyright (C) 2008
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -   Daniel Brooks <db48x@yahoo.com>
   -   Mark Finkle <mfinkle@mozilla.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
   - 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 LGPL or the GPL. 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 ***** -->

<bindings xmlns="http://www.mozilla.org/xbl"
          xmlns:xbl="http://www.mozilla.org/xbl"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="settings">
    <content orient="vertical">
      <xul:label class="settings-title" xbl:inherits="value=label" crop="end" flex="1"/>
      <children />
    </content>
  </binding>

  <binding id="setting-base">
    <implementation>
      <constructor><![CDATA[
        this.preferenceChanged();
        
        if (this.usePref)
          Services.prefs.addObserver(this.pref, this._observer, true);
      ]]></constructor>

      <field name="_observer"><![CDATA[({
        _self: this,

        QueryInterface: function(aIID) {
          const Ci = Components.interfaces;
          if (aIID.equals(Ci.nsIObserver) ||
              aIID.equals(Ci.nsISupportsWeakReference) ||
              aIID.equals(Ci.nsISupports))
            return this;

          throw Components.results.NS_ERROR_NO_INTERFACE;
        },

        observe: function(aSubject, aTopic, aPrefName) {
          if (aTopic != "nsPref:changed")
            return;

          if (this._self.pref == aPrefName)
            this._self.preferenceChanged();
        }
      })]]>
      </field>

      <method name="fireEvent">
        <parameter name="eventName"/>
        <parameter name="funcStr"/>
        <body>
          <![CDATA[
            let body = funcStr || this.getAttribute(eventName);
            if (!body)
              return;

            try {
              let event = document.createEvent("Events");
              event.initEvent(eventName, true, true);
              let f = new Function("event", body);
              f.call(this, event);
            }
            catch (e) {
              Cu.reportError(e);
            }
          ]]>
        </body>
      </method>

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          // Should be code to set the from the preference input.value
          throw "No valueFromPreference implementation";
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          // Should be code to set the input.value from the preference
          throw "No valueToPreference implementation";
        ]]>
        </body>
      </method>

      <method name="inputChanged">
        <body>
        <![CDATA[
          if (this.usePref && !this._updatingInput) {
            this.valueToPreference();
            this.fireEvent("oninputchanged");
          }
        ]]>
        </body>
      </method>

      <method name="preferenceChanged">
        <body>
        <![CDATA[
          if (this.usePref) {
            this._updatingInput = true;
            try {
              this.valueFromPreference();
              this.fireEvent("onpreferencechanged");
            } catch (e) {}
            this._updatingInput = false;
          }
        ]]>
        </body>
      </method>

      <property name="usePref" readonly="true" onget="return this.hasAttribute('pref');"/>
      <property name="pref" readonly="true" onget="return this.getAttribute('pref');"/>
      <property name="type" readonly="true" onget="return this.getAttribute('type');"/>
      <property name="value" onget="return this.input.value;" onset="return this.input.value = val;"/>

      <field name="_updatingInput">false</field>
      <field name="input">document.getAnonymousElementByAttribute(this, "anonid", "input");</field>

      <field name="settings">
        this.parentNode.localName == "settings" ? this.parentNode : null;
      </field>
    </implementation>
  </binding>

  <binding id="setting-bool" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox class="setting-label">
        <xul:label class="preferences-title" xbl:inherits="value=title" crop="end" flex="1"/>
        <xul:label class="preferences-description" xbl:inherits="value=desc" crop="end" flex="1">
          <children/>
        </xul:label>
      </xul:vbox>
      <xul:hbox anonid="input-container" class="setting-input">
        <xul:checkbox anonid="input" xbl:inherits="disabled,onlabel,offlabel" oncommand="inputChanged();"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getBoolPref(this.pref);
          this.value = this.inverted ? !val : val;
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          let val = this.value;
          Services.prefs.setBoolPref(this.pref, this.inverted ? !val : val);
        ]]>
        </body>
      </method>

      <property name="value" onget="return this.input.checked;" onset="return this.input.setChecked(val);"/>
      <property name="inverted" readonly="true" onget="return this.getAttribute('inverted');"/>
    </implementation>
  </binding>

  <binding id="setting-boolint" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox class="setting-label">
        <xul:label class="preferences-title" xbl:inherits="value=title" crop="end" flex="1"/>
        <xul:label class="preferences-description" xbl:inherits="value=desc" crop="end" flex="1">
          <children/>
        </xul:label>
      </xul:vbox>
      <xul:hbox anonid="input-container" class="setting-input">
        <xul:checkbox anonid="input" xbl:inherits="disabled" oncommand="inputChanged();"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getIntPref(this.pref);
          this.value = (val == this.getAttribute("on"));
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Services.prefs.setIntPref(this.pref, this.getAttribute(this.value ? "on" : "off"));
        ]]>
        </body>
      </method>

      <property name="value" onget="return this.input.checked;" onset="return this.input.setChecked(val);"/>
      <property name="inverted" readonly="true" onget="return this.getAttribute('inverted');"/>
    </implementation>
  </binding>

  <binding id="setting-localized-bool" extends="chrome://mozapps/content/extensions/setting.xml#setting-bool">
    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getComplexValue(this.pref, Components.interfaces.nsIPrefLocalizedString).data;
          if(this.inverted) val = !val;
          this.value = (val == "true");
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          let val = this.value;
          if(this.inverted) val = !val;
          let pref = Components.classes["@mozilla.org/pref-localizedstring;1"].createInstance(Components.interfaces.nsIPrefLocalizedString);
          pref.data = this.inverted ? (!val).toString() : val.toString();
          Services.prefs.setComplexValue(this.pref, Components.interfaces.nsIPrefLocalizedString, pref);
        ]]>
        </body>
      </method>

    </implementation>
  </binding>

  <binding id="setting-integer" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox class="setting-label">
        <xul:label class="preferences-title" xbl:inherits="value=title" crop="end" flex="1"/>
        <xul:label class="preferences-description" xbl:inherits="value=desc" crop="end" flex="1">
          <children/>
        </xul:label>
      </xul:vbox>
      <xul:hbox anonid="input-container" class="setting-input">
        <xul:textbox type="number" anonid="input" xbl:inherits="disabled,emptytext,min,max,increment,hidespinbuttons,wraparound" oninput="inputChanged();" onchange="inputChanged();"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Services.prefs.getIntPref(this.pref);
          this.value = val;
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Services.prefs.setIntPref(this.pref, this.value);
        ]]>
        </body>
      </method>

      <property name="type" readonly="true" onget="return this.getAttribute('type');"/>
      <property name="value" onget="return this.input.value;" onset="return this.input.value = val;"/>
    </implementation>
  </binding>

  <binding id="setting-control" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox class="setting-label">
        <xul:label class="preferences-title" xbl:inherits="value=title" crop="end" flex="1"/>
        <xul:label class="preferences-description" xbl:inherits="value=desc" crop="end" flex="1">
          <children/>
        </xul:label>
      </xul:vbox>
      <xul:hbox anonid="input-container" class="setting-input">
        <children includes="button|menulist"/>
      </xul:hbox>
    </content>
  </binding>

  <binding id="setting-string" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox class="setting-label">
        <xul:label class="preferences-title" xbl:inherits="value=title" crop="end" flex="1"/>
        <xul:label class="preferences-description" xbl:inherits="value=desc" crop="end" flex="1">
          <children/>
        </xul:label>
      </xul:vbox>
      <xul:hbox anonid="input-container" class="setting-input">
        <xul:textbox xbl:inherits="disabled,emptytext,type=inputtype,min,max,increment,hidespinbuttons,decimalplaces,wraparound" anonid="input" oninput="inputChanged();"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          const nsISupportsString = Components.interfaces.nsISupportsString;
          this.value = Services.prefs.getComplexValue(this.pref, nsISupportsString).data;
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          const nsISupportsString = Components.interfaces.nsISupportsString;
          let iss = Components.classes["@mozilla.org/supports-string;1"].createInstance(nsISupportsString);
          iss.data = this.value;
          Services.prefs.setComplexValue(this.pref, nsISupportsString, iss);
        ]]>
        </body>
      </method>

      <property name="value" onget="return this.input.value;" onset="return this.input.value=val;"/>
    </implementation>
  </binding>
</bindings>
