<!--
  The contents of this file are subject to the Mozilla Public
  License Version 1.1 (the "MPL"); you may not use this file
  except in compliance with the MPL. You may obtain a copy of
  the MPL at http://www.mozilla.org/MPL/

  Software distributed under the MPL is distributed on an "AS
  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  implied. See the MPL for the specific language governing
  rights and limitations under the MPL.

  The Original Code is Enigmail.

  The Initial Developer of this code is Patrick Brunschwig.
  Portions created by Patrick Brunschwig <patrick.brunschwig@gmx.net>
  are Copyright (C) 2005 Patrick Brunschwig.
  All Rights Reserved.

  Contributor(s):

  Alternatively, the contents of this file may be used under the
  terms of the GNU General Public License (the "GPL"), in which case
  the provisions of the GPL are applicable instead of
  those above. If you wish to allow use of your version of this
  file only under the terms of the GPL and not to allow
  others to use your version of this file under the MPL, indicate
  your decision by deleting the provisions above and replace them
  with the notice and other provisions required by the GPL.
  If you do not delete the provisions above, a recipient
  may use your version of this file under either the MPL or the
  GPL.
-->

<?xml-stylesheet href="chrome://communicator/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://enigmail/skin/enigmail.css" type="text/css"?>

<!DOCTYPE window [
<!ENTITY % brandDTD SYSTEM "chrome://global/locale/brand.dtd" >
%brandDTD;
<!ENTITY % enigMailDTD SYSTEM "chrome://enigmail/locale/enigmail.dtd" >
%enigMailDTD;
]>

<dialog id="enigmailSetCardPingDlg"
        title="&enigmail.cardPin.title;"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="onLoad();"
        ondialogaccept="return onAccept();"
        >


  <script type="application/x-javascript" src="chrome://enigmail/content/enigmailCommon.js"/>


  <vbox>
    <hbox align="center">
      <label value="&enigmail.cardPin.action.label;" control="actionMenu"/>
      <menulist id="actionMenu" label="&enigmail.cardPin.changePin.label;" value="P">
        <menupopup id="actionMenuPopup">
          <menuitem id="changePin"
                    label="&enigmail.cardPin.changePin.label;"
                    selected="true"
                    oncommand="setDlgContent('P')"/>
          <menuitem id="changeAPin"
                    label="&enigmail.cardPin.changeAdmPin.label;"
                    oncommand="setDlgContent('A')"/>
          <menuitem id="unblockPin"
                    label="&enigmail.cardPin.unblockPin.label;"
                    oncommand="setDlgContent('U')"/>
        </menupopup>
      </menulist>
    </hbox>
    <separator/>
    <grid>
      <columns>
        <column/>
        <column flex="1"/>
      </columns>
      <rows>
        <row align="center" id="currAdminPinRow" collapsed="true">
          <label value="&enigmail.cardPin.currAdmPin.label; :" type="password" control="currAdmPinTxt"/>
          <textbox id="currAdmPinTxt" value="" type="password"/>
        </row>
        <row align="center" id="adminPinRow" collapsed="true">
          <label value="&enigmail.cardPin.newAdminPin.label; :" control="adminPinTxt"/>
          <textbox id="adminPinTxt" value="" type="password"/>
        </row>
        <row align="center" id="adminPinRepeatRow" collapsed="true">
          <label value="&enigmail.cardPin.adminPinRepeat.label; :" control="adminPinRepeatTxt"/>
          <textbox id="adminPinRepeatTxt" value="" type="password"/>
        </row>
        <row align="center" id="currPinRow">
          <label value="&enigmail.cardPin.currPin.label; :" control="currPinTxt"/>
          <textbox id="currPinTxt" value="" type="password"/>
        </row>
        <row align="center" id="pinRow">
          <label value="&enigmail.cardPin.newPin.label; :" control="pinTxt"/>
          <textbox id="pinTxt" value="" type="password"/>
        </row>
        <row align="center" id="pinRepeatRow">
          <label value="&enigmail.cardPin.pinRepeat.label; :" control="pinRepeatTxt"/>
          <textbox id="pinRepeatTxt" value="" type="password"/>
        </row>
      </rows>
    </grid>
  </vbox>

  <script type="application/x-javascript">
  <![CDATA[

  EnigInitCommon("enigmailSetCardPinDlg");
  
  const CHANGE_PIN  = 'P';
  const ADMIN_PIN   = 'A';
  const UNBLOCK_PIN = 'U';

  var gAction = null;

  function onLoad() {
    setDlgContent(CHANGE_PIN);
  }

  function onAccept() {
    var enigmailSvc = GetEnigmailSvc();
    if (!enigmailSvc)
      return false;
      
    var pinItem1;
    var pinItem2;
    var minLen=0;
    var action;
      
    switch(gAction) {
      case CHANGE_PIN:
        pinItem1="pinTxt";
        pinItem2="pinRepeatTxt";
        minLen=6;
        action = nsIEnigmail.CARD_PIN_CHANGE;
        break;
      case UNBLOCK_PIN:
        pinItem1="pinTxt";
        pinItem2="pinRepeatTxt";
        minLen=6;
        action = nsIEnigmail.CARD_PIN_UNBLOCK;
        break;
      case ADMIN_PIN:
        pinItem1="adminPinTxt";
        pinItem2="adminPinRepeatTxt";
        minLen=8;
        action = nsIEnigmail.CARD_ADMIN_PIN_CHANGE;
        break;
    }
    var adminPin = document.getElementById("currAdmPinTxt").value;
    var oldPin = document.getElementById("currPinTxt").value;
    var newPin = document.getElementById(pinItem1).value;
    if (newPin.length < minLen) {
      EnigAlert(EnigGetString("cardPin.minLength", minLen));
      return false;
    }
    if (newPin != document.getElementById(pinItem2).value) {
      EnigAlert(EnigGetString("cardPin.dontMatch"));
      return false;
    }
    
    var errorMsgObj = new Object();
    var pinObserver = new changePinObserver();
    
    var r = enigmailSvc.cardChangePin(window,
                                     action,
                                     oldPin,
                                     newPin,
                                     adminPin,
                                     pinObserver,
                                     errorMsgObj);
                                     
    if (r != 0) {
      EnigAlert(EnigGetString("cardPin.processFailed")+"\n"+pinObserver.result);
      return false;
    }
    return true;
  }

  function dlgEnable(item) {
    document.getElementById(item).removeAttribute("collapsed");
  }
  
  function dlgDisable(item) {
    document.getElementById(item).setAttribute("collapsed", "true");
  }
  
  function setDlgContent(sel) {
    gAction = sel;
    switch(sel) {
    case 'P':
      dlgDisable("currAdminPinRow");
      dlgDisable("adminPinRow");
      dlgDisable("adminPinRepeatRow");
      dlgEnable("currPinRow");
      dlgEnable("pinRow");
      dlgEnable("pinRepeatRow");
      break;
    case 'A':
      dlgEnable("currAdminPinRow");
      dlgEnable("adminPinRow");
      dlgEnable("adminPinRepeatRow");
      dlgDisable("currPinRow");
      dlgDisable("pinRow");
      dlgDisable("pinRepeatRow");
      break;
    case 'U':
      dlgEnable("currAdminPinRow");
      dlgDisable("adminPinRow");
      dlgDisable("adminPinRepeatRow");
      dlgDisable("currPinRow");
      dlgEnable("pinRow");
      dlgEnable("pinRepeatRow");
      break;
    }
  }

  function changePinObserver() {
  }

  changePinObserver.prototype =
  {
    _data: "",
    result: "",

    QueryInterface : function(iid)
    {
      if (iid.equals(Components.interfaces.nsIEnigMimeReadCallback) ||
          iid.equals(Components.interfaces.nsISupports) )
        return this;

      throw Components.results.NS_NOINTERFACE;
    },

    onDataAvailable: function (data) {
      var ret="";
      DEBUG_LOG("enigmailSetCardPin: changePinObserver.onDataAvailable: data="+data+"\n");
      if (data.indexOf("[GNUPG:] SC_OP_FAILURE")>=0) {
        this.result = this._data;
      }
      else {
        this._data = data
      }
      return "";
    }
  }

  ]]>
  </script>
</dialog>


