<?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>
   -   Geoff Lankow <geoff@darktrojan.net>
   -
   - 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 ***** -->

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

<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>

  <binding id="setting-color" 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: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 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" align="center" class="setting-input">
        <xul:button type="button" anonid="button" label="&settings.path.button.label;" xbl:inherits="disabled" oncommand="showPicker();" />
        <xul:label anonid="input" flex="1" crop="start" 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.nsILocalFile);
              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 = Services.prefs.getCharPref(this.pref);
        ]]>
        </body>
      </method>

      <method name="valueToPreference">
        <body>
        <![CDATA[
          Services.prefs.setCharPref(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.nsILocalFile);
              file.initWithPath(val);
              label = this.hasAttribute("fullpath") ? file.path : file.leafName;
            } catch (e) {}
          }
          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 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 class="setting-input">
        <children includes="radiogroup|menulist" />
      </xul:hbox>
    </content>

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

      <method name="valueFromPreference">
        <body>
        <![CDATA[
          let val;
          switch (Services.prefs.getPrefType(this.pref)) {
            case Ci.nsIPrefBranch.PREF_STRING:
              val = Services.prefs.getCharPref(this.pref);
              break;
            case Ci.nsIPrefBranch.PREF_INT:
              val = Services.prefs.getIntPref(this.pref);
              break;
            case Ci.nsIPrefBranch.PREF_BOOL:
              val = Services.prefs.getBoolPref(this.pref).toString();
              break;
            default:
              return;
          }

          for (let i = 0; i < this.control.itemCount; i++) {
            if (this.control.getItemAtIndex(i).value == val) {
              this.control.selectedIndex = i;
              break;
            }
          }
        ]]>
        </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")
            Services.prefs.setBoolPref(this.pref, val == "true");
          else if (/^-?\d+$/.test(val))
            Services.prefs.setIntPref(this.pref, val);
          else
            Services.prefs.setCharPref(this.pref, val);
        ]]>
        </body>
      </method>

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