<?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/.
-->

<bindings id="charsetListBinding"
          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="charsetpicker"
           extends="chrome://global/content/bindings/menulist.xml#menulist">
    <implementation>
      <constructor><![CDATA[
        let charsetValues = "";

        if (this.getAttribute("subset") == "sending")
          charsetValues = ["UTF-8", "EUC-KR", "gbk", "gb18030", "ISO-2022-JP",
                           "ISO-8859-1", "ISO-8859-7", "windows-1252"];

        else if (!this.getAttribute("subset")
                 || this.getAttribute("subset") == "viewing")
          charsetValues = ["UTF-8", "Big5", "EUC-KR", "gbk", "ISO-2022-JP",
                           "ISO-8859-1", "ISO-8859-2", "ISO-8859-7",
                           "windows-874", "windows-1250", "windows-1251",
                           "windows-1252", "windows-1255", "windows-1256",
                           "windows-1257", "windows-1258"];

        Components.utils.import("resource://gre/modules/Services.jsm");

        let charsetBundle = Services.strings.createBundle(
          "chrome://messenger/locale/charsetTitles.properties");
        let aMenuList = this;
        let menuLabels = [];

        charsetValues.forEach(function(item) {
          let strCharset = charsetBundle.GetStringFromName(
            item.toLowerCase() + ".title");

          menuLabels.push({label: strCharset, value: item});
        });

        menuLabels.sort(function(a, b) {
          if (a.value == "UTF-8" || a.label < b.label)
            return -1;
          else if (b.value == "UTF-8" || a.label > b.label)
            return 1;
          return 0;
        });

        menuLabels.forEach(function(item) {
          aMenuList.appendItem(item.label, item.value);
        });

        // Selecting appropiate menu item corresponding to preference stored
        // value.
        if (this.hasAttribute("preference")) {
          const Ci = Components.interfaces;

          let preference = Services.prefs.getComplexValue(
            this.getAttribute("preference"), Ci.nsIPrefLocalizedString);
          this.value = preference.data;
        }
     ]]></constructor>
    </implementation>
  </binding>
</bindings>
