<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<!DOCTYPE page [
<!ENTITY % extensionsDTD SYSTEM "chrome://mozapps/locale/extensions/extensions.dtd">
%extensionsDTD;
]>

<!-- import-globals-from extensions.js -->

<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="setting-base">
    <implementation>
      <constructor><![CDATA[
        this.preferenceChanged();

        this.addEventListener("keypress", function(event) {
          event.stopPropagation();
        }, false);

        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.Exception("No interface", 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 Components.Exception("No valueFromPreference implementation",
                                     Components.results.NS_ERROR_NOT_IMPLEMENTED);
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          // Should be code to set the input.value from the preference
          throw Components.Exception("No valueToPreference implementation",
                                     Components.results.NS_ERROR_NOT_IMPLEMENTED);
        ]]>
        </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>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
        <xul:label class="preferences-learnmore text-link"
                   onclick="document.getBindingParent(this).openLearnMore()">&setting.learnmore;</xul:label>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:checkbox anonid="input" xbl:inherits="disabled,onlabel,offlabel,label=checkboxlabel" 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');"/>

      <method name="openLearnMore">
        <body>
        <![CDATA[
          window.open(this.getAttribute("learnmore"), "_blank");
        ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="setting-boolint" extends="chrome://mozapps/content/extensions/setting.xml#setting-bool">
    <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>
    </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>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:textbox type="number" anonid="input" oninput="inputChanged();" onchange="inputChanged();"
                     xbl:inherits="disabled,emptytext,min,max,increment,hidespinbuttons,wraparound,size"/>
      </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>
    </implementation>
  </binding>

  <binding id="setting-control" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <children/>
      </xul:hbox>
    </content>
  </binding>

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

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          this.value = Preferences.get(this.pref, "");
         ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Preferences.set(this.pref, this.value);
        ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="setting-color" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:colorpicker type="button" anonid="input" xbl:inherits="disabled" onchange="document.getBindingParent(this).inputChanged();"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="valueFromPreference">
        <body>
        <![CDATA[
          // We must wait for the colorpicker's binding to be applied before setting the value
          if (!this.input.color)
            this.input.initialize();
          this.value = Services.prefs.getCharPref(this.pref);
        ]]>
        </body>
      </method>

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

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

  <binding id="setting-path" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <xul:button type="button" anonid="button" label="&settings.path.button.label;" xbl:inherits="disabled" oncommand="showPicker();"/>
        <xul:label anonid="input" flex="1" crop="center" xbl:inherits="disabled"/>
      </xul:hbox>
    </content>

    <implementation>
      <method name="showPicker">
        <body>
        <![CDATA[
          var filePicker = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
          filePicker.init(window, this.getAttribute("title"),
                          this.type == "file" ? Ci.nsIFilePicker.modeOpen : Ci.nsIFilePicker.modeGetFolder);
          if (this.value) {
            try {
              let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
              file.initWithPath(this.value);
              filePicker.displayDirectory = this.type == "file" ? file.parent : file;
              if (this.type == "file") {
                filePicker.defaultString = file.leafName;
              }
            } catch (e) {}
          }
          if (filePicker.show() != Ci.nsIFilePicker.returnCancel) {
            this.value = filePicker.file.path;
            this.inputChanged();
          }
        ]]>
        </body>
      </method>

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          this.value = Preferences.get(this.pref, "");
        ]]>
        </body>
      </method>

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

      <field name="_value"></field>

      <property name="value">
        <getter>
        <![CDATA[
          return this._value;
        ]]>
        </getter>
        <setter>
        <![CDATA[
          this._value = val;
          let label = "";
          if (val) {
            try {
              let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
              file.initWithPath(val);
              label = this.hasAttribute("fullpath") ? file.path : file.leafName;
            } catch (e) {}
          }
          this.input.tooltipText = val;
          return this.input.value = label;
       ]]>
        </setter>
      </property>
    </implementation>
  </binding>

  <binding id="setting-multi" extends="chrome://mozapps/content/extensions/setting.xml#setting-base">
    <content>
      <xul:vbox>
        <xul:hbox class="preferences-alignment">
          <xul:label class="preferences-title" flex="1" xbl:inherits="xbl:text=title"/>
        </xul:hbox>
        <xul:description class="preferences-description" flex="1" xbl:inherits="xbl:text=desc"/>
      </xul:vbox>
      <xul:hbox class="preferences-alignment">
        <children includes="radiogroup|menulist"/>
      </xul:hbox>
    </content>

    <implementation>
      <constructor>
      <![CDATA[
        this.control.addEventListener("command", this.inputChanged.bind(this), false);
      ]]>
      </constructor>

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val = Preferences.get(this.pref, "").toString();

          if ("itemCount" in this.control) {
            for (let i = 0; i < this.control.itemCount; i++) {
              if (this.control.getItemAtIndex(i).value == val) {
                this.control.selectedIndex = i;
                break;
              }
            }
          } else {
            this.control.setAttribute("value", val);
          }
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          // We might not have a pref already set, so we guess the type from the value attribute
          let val = this.control.selectedItem.value;
          if (val == "true" || val == "false") {
            val = val == "true";
          } else if (/^-?\d+$/.test(val)) {
            val = parseInt(val, 10);
          }
          Preferences.set(this.pref, val);
        ]]>
        </body>
      </method>

      <field name="control">this.getElementsByTagName(this.getAttribute("type") == "radio" ? "radiogroup" : "menulist")[0];</field>
    </implementation>
  </binding>
</bindings>
