<?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 bindings [
<!ENTITY % datamanDTD SYSTEM "chrome://communicator/locale/dataman/dataman.dtd">
%datamanDTD;
]>

<bindings id="datamanBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
          xmlns:xbl="http://www.mozilla.org/xbl">

  <binding id="perm-base-item"
           extends="chrome://global/content/bindings/richlistbox.xml#richlistitem">
    <implementation>
      <constructor><![CDATA[
        var permLabel = this.type;
        try {
          permLabel = gDataman.bundle.getString("perm." + this.type + ".label");
        }
        catch (e) {
        }
        this.setAttribute("label", permLabel);
        this._updateRadio();
      ]]></constructor>

      <property name="capability">
        <getter><![CDATA[
          if (this.hasAttribute("capability"))
            return this.getAttribute("capability");
          return -1;
        ]]></getter>
        <setter><![CDATA[
          this.setAttribute("capability", val);
          this._updateRadio();
        ]]></setter>
      </property>

      <property name="host" readonly="true"
                onget="return this.getAttribute('host');"/>

      <property name="type" readonly="true"
                onget="return this.getAttribute('type');"/>

      <method name="_updateRadio">
        <body><![CDATA[
          let radio = document.getAnonymousElementByAttribute(this, "anonid",
                                                              "permSetting-" + this.capability);
          if (radio)
            radio.radioGroup.selectedItem = radio;
          else {
            let radioGroup = document.getAnonymousElementByAttribute(this, "anonid",
                                                                     "radioGroup");
            radioGroup.selectedIndex = -1;
          }
        ]]></body>
      </method>

      <method name="useDefault">
        <parameter name="aChecked"/>
        <parameter name="aUIUpdateOnly"/>
        <body><![CDATA[
          let checkbox = document.getAnonymousElementByAttribute(this, "anonid",
                                                                 "useDefault");
          if (checkbox.checked != aChecked)
            checkbox.checked = aChecked;
          let radioGroup = document.getAnonymousElementByAttribute(this, "anonid",
                                                                   "radioGroup");
          radioGroup.disabled = aChecked;
          if (aChecked) {
            if (!aUIUpdateOnly)
              gPerms.removeItem(this.host, this.type);

            this.capability = gPerms.getDefault(this.type);
          }
          this._updateRadio();
        ]]></body>
      </method>

      <method name="setCapability">
        <parameter name="aValue"/>
        <parameter name="aUIUpdateOnly"/>
        <body><![CDATA[
          this.capability = aValue;
          let radio = document.getAnonymousElementByAttribute(this, "anonid",
                                                              "permSetting-" + aValue);
          if (radio && !radio.selected)
            radio.radioGroup.selectedItem = radio;
          if (!aUIUpdateOnly)
            gPerms.updateItem(this.host, this.type, aValue);
        ]]></body>
      </method>

      <method name="handleKeyPress">
        <parameter name="aEvent"/>
        <body><![CDATA[
          if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE) {
            this.useDefault(true);
          }
       ]]></body>
      </method>
    </implementation>

    <handlers>
      <handler event="keypress" phase="capturing"
               action="return this.handleKeyPress(event);"/>
    </handlers>
  </binding>

  <binding id="perm-generic-item"
           extends="chrome://communicator/content/dataman/dataman.xml#perm-base-item">
    <content>
      <xul:hbox>
        <xul:label anonid="permHost" class="hostLabel" xbl:inherits="value=displayHost"/>
        <xul:label anonid="permLabel" class="permissionLabel" xbl:inherits="value=label" control="radioGroup"/>
      </xul:hbox>
      <xul:hbox role="group" aria-labelledby="permLabel">
        <xul:checkbox class="indent" anonid="useDefault" label="&perm.UseDefault;"
                      oncommand="document.getBindingParent(this).useDefault(this.checked);"/>
        <xul:spacer flex="1"/>
        <xul:radiogroup anonid="radioGroup" orient="horizontal">
          <xul:radio anonid="permSetting-1" label="&perm.Allow;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.ALLOW_ACTION);"/>
          <xul:radio anonid="permSetting-2" label="&perm.Block;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.DENY_ACTION);"/>
        </xul:radiogroup>
      </xul:hbox>
    </content>
  </binding>

  <binding id="perm-cookie-item"
           extends="chrome://communicator/content/dataman/dataman.xml#perm-base-item">
    <content>
      <xul:hbox>
        <xul:label anonid="permHost" class="hostLabel" xbl:inherits="value=displayHost"/>
        <xul:label anonid="permLabel" class="permissionLabel" xbl:inherits="value=label" control="radioGroup"/>
      </xul:hbox>
      <xul:hbox role="group" aria-labelledby="permLabel">
        <xul:checkbox class="indent" anonid="useDefault" label="&perm.UseDefault;"
                      oncommand="document.getBindingParent(this).useDefault(this.checked);"/>
        <xul:spacer flex="1"/>
        <xul:radiogroup anonid="radioGroup" orient="horizontal">
          <xul:radio anonid="permSetting-1" label="&perm.Allow;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.ALLOW_ACTION);"/>
          <xul:radio anonid="permSetting-8" label="&perm.AllowSession;"
                     oncommand="document.getBindingParent(this).setCapability(Components.interfaces.nsICookiePermission.ACCESS_SESSION);"/>
          <xul:radio anonid="permSetting-2" label="&perm.Block;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.DENY_ACTION);"/>
        </xul:radiogroup>
      </xul:hbox>
    </content>
  </binding>

  <binding id="perm-geo-item"
           extends="chrome://communicator/content/dataman/dataman.xml#perm-base-item">
    <content>
      <xul:hbox>
        <xul:label anonid="permHost" class="hostLabel" xbl:inherits="value=displayHost"/>
        <xul:label anonid="permLabel" class="permissionLabel" xbl:inherits="value=label" control="radioGroup"/>
      </xul:hbox>
      <xul:hbox role="group" aria-labelledby="permLabel">
        <xul:checkbox class="indent" anonid="useDefault" label="&perm.AskAlways;"
                      oncommand="document.getBindingParent(this).useDefault(this.checked);"/>
        <xul:spacer flex="1"/>
        <xul:radiogroup anonid="radioGroup" orient="horizontal">
          <xul:radio anonid="permSetting-1" label="&perm.Allow;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.ALLOW_ACTION);"/>
          <xul:radio anonid="permSetting-2" label="&perm.Block;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.DENY_ACTION);"/>
        </xul:radiogroup>
      </xul:hbox>
    </content>
  </binding>

  <binding id="perm-password-item" extends="chrome://communicator/content/dataman/dataman.xml#perm-base-item">
    <content>
      <xul:hbox>
        <xul:label anonid="permHost" class="hostLabel" xbl:inherits="value=displayHost"/>
        <xul:label anonid="permLabel" class="permissionLabel" xbl:inherits="value=label" control="radioGroup"/>
      </xul:hbox>
      <xul:hbox role="group" aria-labelledby="permLabel">
        <xul:checkbox class="indent" anonid="useDefault" hidden="true"/>
        <xul:spacer flex="1"/>
        <xul:radiogroup anonid="radioGroup" orient="horizontal">
          <xul:radio anonid="permSetting-1" label="&perm.AskAlways;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.ALLOW_ACTION);"/>
          <xul:radio anonid="permSetting-2" label="&perm.NeverSave;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.DENY_ACTION);"/>
        </xul:radiogroup>
      </xul:hbox>
    </content>

    <implementation>
      <method name="useDefault">
        <parameter name="aChecked"/>
        <body><![CDATA[
          // just for compat, makes it easier to generically "delete" perms
          if (aChecked)
            this.setCapability(Services.perms.ALLOW_ACTION);
        ]]></body>
      </method>

      <method name="setCapability">
        <parameter name="aValue"/>
        <parameter name="aUIUpdateOnly"/>
        <body><![CDATA[
          this.capability = aValue;
          let radio = document.getAnonymousElementByAttribute(this, "anonid",
                                                              "permSetting-" + aValue);
          if (radio && !radio.selected)
            radio.radioGroup.selectedItem = radio;
          if (!aUIUpdateOnly)
            Services.logins.setLoginSavingEnabled(this.host, aValue == Services.perms.ALLOW_ACTION);
        ]]></body>
      </method>
    </implementation>
  </binding>

  <binding id="perm-content-item"
           extends="chrome://communicator/content/dataman/dataman.xml#perm-base-item">
    <content>
      <xul:hbox>
        <xul:label anonid="permHost" class="hostLabel" xbl:inherits="value=displayHost"/>
        <xul:label anonid="permLabel" class="permissionLabel" xbl:inherits="value=label" control="radioGroup"/>
      </xul:hbox>
      <xul:hbox role="group" aria-labelledby="permLabel">
        <xul:checkbox class="indent" anonid="useDefault" label="&perm.UseDefault;"
                      oncommand="document.getBindingParent(this).useDefault(this.checked);"/>
        <xul:spacer flex="1"/>
        <xul:radiogroup anonid="radioGroup" orient="horizontal">
          <xul:radio anonid="permSetting-1" label="&perm.Allow;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.ALLOW_ACTION);"/>
          <xul:radio anonid="permSetting-3" label="&perm.AllowSameDomain;"
                     oncommand="document.getBindingParent(this).setCapability(NOFOREIGN);"/>
          <xul:radio anonid="permSetting-2" label="&perm.Block;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.DENY_ACTION);"/>
        </xul:radiogroup>
      </xul:hbox>
    </content>
  </binding>

  <binding id="permpwd-base-item"
           extends="chrome://global/content/bindings/richlistbox.xml#richlistitem">
    <content>
      <xul:hbox>
        <xul:label anonid="permPwdHost" class="hostLabel" xbl:inherits="value=displayHost"/>
        <xul:label anonid="permPwdLabel" class="permissionLabel" xbl:inherits="value=label" control="radioGroup"/>
      </xul:hbox>
      <xul:hbox role="group" aria-labelledby="permPwdLabel">
        <xul:checkbox class="indent" anonid="useDefault" hidden="true"/>
        <xul:spacer flex="1"/>
        <xul:radiogroup anonid="radioGroup" orient="horizontal">
          <xul:radio anonid="permPwdSetting-1" label="&permpwd.AskAlways;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.ALLOW_ACTION);"/>
          <xul:radio anonid="permPwdSetting-2" label="&permpwd.NeverSave;"
                     oncommand="document.getBindingParent(this).setCapability(Services.perms.DENY_ACTION);"/>
        </xul:radiogroup>
      </xul:hbox>
    </content>

    <implementation>
      <constructor><![CDATA[
        var permPwdLabel = gDataman.bundle.getString("permpwd." + this.type + ".label");
        this.setAttribute("label", permPwdLabel);
        this._updateRadio();
      ]]></constructor>

      <property name="capability">
        <getter><![CDATA[
          if (this.hasAttribute("capability"))
            return this.getAttribute("capability");
          return -1;
        ]]></getter>
        <setter><![CDATA[
          this.setAttribute("capability", val);
          this._updateRadio();
        ]]></setter>
      </property>

      <property name="host" readonly="true"
                onget="return this.getAttribute('host');"/>

      <property name="type" readonly="true"
                onget="return this.getAttribute('type');"/>

      <method name="_updateRadio">
        <body><![CDATA[
          let radio = document.getAnonymousElementByAttribute(this, "anonid",
                                                              "permPwdSetting-" + this.capability);
          if (radio)
            radio.radioGroup.selectedItem = radio;
          else {
            let radioGroup = document.getAnonymousElementByAttribute(this, "anonid",
                                                                     "radioGroup");
            radioGroup.selectedIndex = -1;
          }
        ]]></body>
      </method>

      <method name="useDefault">
        <parameter name="aChecked"/>
        <parameter name="aUIUpdateOnly"/>
        <body><![CDATA[
          let checkbox = document.getAnonymousElementByAttribute(this, "anonid",
                                                                 "useDefault");
          if (checkbox.checked != aChecked)
            checkbox.checked = aChecked;

          let radioGroup = document.getAnonymousElementByAttribute(this, "anonid",
                                                                   "radioGroup");
          radioGroup.disabled = aChecked;
          if (aChecked) {
            if (!aUIUpdateOnly)
              gPermsPwd.removeItem(this.host, this.type);

            this.capability = Services.perms.DENY_ACTION;
          }
          this._updateRadio();
        ]]></body>
      </method>

      <method name="handleKeyPress">
        <parameter name="aEvent"/>
        <body><![CDATA[
          if (aEvent.keyCode == KeyEvent.DOM_VK_DELETE) {
            this.useDefault(true);
          }
       ]]></body>
      </method>
      <method name="useDefault">
        <parameter name="aChecked"/>
        <body><![CDATA[
          // just for compat, makes it easier to generically "delete" perms
          if (aChecked)
            this.setCapability(Services.perms.ALLOW_ACTION);
        ]]></body>
      </method>

      <method name="setCapability">
        <parameter name="aValue"/>
        <parameter name="aUIUpdateOnly"/>
        <body><![CDATA[
          this.capability = aValue;
          let radio = document.getAnonymousElementByAttribute(this, "anonid",
                                                              "permPwdSetting-" + aValue);
          if (radio && !radio.selected)
            radio.radioGroup.selectedItem = radio;
          if (!aUIUpdateOnly)
            Services.logins.setLoginSavingEnabled(this.host, aValue == Services.perms.ALLOW_ACTION);
        ]]></body>
      </method>

    </implementation>

    <handlers>
      <handler event="keypress" phase="capturing"
               action="return this.handleKeyPress(event);"/>
    </handlers>
  </binding>
  </bindings>
