toolkit/content/customizeCharset.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/customizeCharset.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,308 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +//get prefInt services
     1.9 +
    1.10 +var availCharsetDict     = [];
    1.11 +var prefBranch           = null; //Preferences Interface
    1.12 +var pref_string_title    = "";
    1.13 +var pref_string_content  = "";
    1.14 +var pref_string_object   = null;
    1.15 +
    1.16 +function Init()
    1.17 +{
    1.18 +  var applicationArea = "";
    1.19 +
    1.20 +  if ("arguments" in window && window.arguments[0])
    1.21 +    applicationArea = window.arguments[0];
    1.22 +
    1.23 +  prefBranch = Components.classes["@mozilla.org/preferences-service;1"];
    1.24 +
    1.25 +  if (prefBranch) {
    1.26 +    prefBranch = prefBranch.getService(Components.interfaces.nsIPrefBranch);
    1.27 +
    1.28 +    if (applicationArea.indexOf("mail") != -1) {
    1.29 +      pref_string_title = "intl.charsetmenu.mailedit";
    1.30 +    } else {
    1.31 +    //default is the browser
    1.32 +      pref_string_title = "intl.charsetmenu.browser.static";
    1.33 +    }
    1.34 +
    1.35 +    pref_string_object = prefBranch.getComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString)
    1.36 +    pref_string_content = pref_string_object.data;
    1.37 +
    1.38 +    AddRemoveLatin1('add');
    1.39 +  }
    1.40 +
    1.41 +  LoadAvailableCharSets();
    1.42 +  LoadActiveCharSets();
    1.43 +}
    1.44 +
    1.45 +
    1.46 +function readRDFString(aDS,aRes,aProp) 
    1.47 +{
    1.48 +  var n = aDS.GetTarget(aRes, aProp, true);
    1.49 +  if (n)
    1.50 +    return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
    1.51 +  else 
    1.52 +    return "";
    1.53 +}
    1.54 +
    1.55 +
    1.56 +function LoadAvailableCharSets()
    1.57 +{
    1.58 +  try {
    1.59 +    var available_charsets_listbox = document.getElementById('available_charsets');
    1.60 +    var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); 
    1.61 +    var kNC_Root = rdf.GetResource("NC:DecodersRoot");
    1.62 +    var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
    1.63 +    var rdfDataSource = rdf.GetDataSource("rdf:charset-menu"); 
    1.64 +    var rdfContainer =
    1.65 +      Components.classes["@mozilla.org/rdf/container;1"]
    1.66 +                .createInstance(Components.interfaces.nsIRDFContainer);
    1.67 +
    1.68 +    rdfContainer.Init(rdfDataSource, kNC_Root);
    1.69 +    var availableCharsets = rdfContainer.GetElements();
    1.70 +    var charset;
    1.71 +
    1.72 +    for (var i = 0; i < rdfContainer.GetCount(); i++) {
    1.73 +      charset = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
    1.74 +      availCharsetDict[i] = new Array(2);
    1.75 +      availCharsetDict[i][0] = readRDFString(rdfDataSource, charset, kNC_name);
    1.76 +      availCharsetDict[i][1] = charset.Value;
    1.77 +
    1.78 +      AddListItem(document,
    1.79 +                  available_charsets_listbox,
    1.80 +                  availCharsetDict[i][1],
    1.81 +                  availCharsetDict[i][0]);
    1.82 +    }
    1.83 +  }
    1.84 +  catch (e) {}
    1.85 +}
    1.86 +
    1.87 +
    1.88 +function GetCharSetTitle(id)
    1.89 +{
    1.90 +  if (availCharsetDict) {
    1.91 +    for (var j = 0; j < availCharsetDict.length; j++) {
    1.92 +      if (availCharsetDict[j][1] == id) {
    1.93 +        return availCharsetDict[j][0];
    1.94 +      }
    1.95 +    }
    1.96 +  }
    1.97 +  return '';
    1.98 +}
    1.99 +
   1.100 +function AddRemoveLatin1(action)
   1.101 +{
   1.102 +  var arrayOfPrefs = [];
   1.103 +  arrayOfPrefs = pref_string_content.split(', ');
   1.104 +
   1.105 +  if (arrayOfPrefs.length > 0) {
   1.106 +    for (var i = 0; i < arrayOfPrefs.length; i++) {
   1.107 +      if (arrayOfPrefs[i] == 'ISO-8859-1') {
   1.108 +        if (action == 'remove') {
   1.109 +          arrayOfPrefs[i] = arrayOfPrefs[arrayOfPrefs.length-1];
   1.110 +          arrayOfPrefs.length = arrayOfPrefs.length - 1;
   1.111 +        }
   1.112 +
   1.113 +        pref_string_content = arrayOfPrefs.join(', ');
   1.114 +        return;
   1.115 +      }
   1.116 +    }
   1.117 +
   1.118 +    if (action == 'add') {
   1.119 +      arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1';
   1.120 +      pref_string_content = arrayOfPrefs.join(', ');
   1.121 +    }
   1.122 +  }
   1.123 +}
   1.124 +
   1.125 +
   1.126 +function LoadActiveCharSets()
   1.127 +{
   1.128 +  var active_charsets = document.getElementById('active_charsets');
   1.129 +  var arrayOfPrefs = [];
   1.130 +  var str;
   1.131 +  var tit;
   1.132 +
   1.133 +  arrayOfPrefs = pref_string_content.split(', ');
   1.134 +
   1.135 +  if (arrayOfPrefs.length > 0) {
   1.136 +    for (var i = 0; i < arrayOfPrefs.length; i++) {
   1.137 +      str = arrayOfPrefs[i];
   1.138 +      tit = GetCharSetTitle(str);
   1.139 +      if (str && tit)
   1.140 +        AddListItem(document, active_charsets, str, tit);
   1.141 +    }
   1.142 +  }
   1.143 +}
   1.144 +
   1.145 +function enable_save()
   1.146 +{
   1.147 +  var save_button = document.documentElement.getButton("accept");
   1.148 +  save_button.removeAttribute('disabled');
   1.149 +}
   1.150 +
   1.151 +
   1.152 +function update_buttons()
   1.153 +{
   1.154 +  var available_charsets = document.getElementById('available_charsets');
   1.155 +  var active_charsets = document.getElementById('active_charsets');
   1.156 +  var remove_button = document.getElementById('remove_button');
   1.157 +  var add_button = document.getElementById('add_button');
   1.158 +  var up_button = document.getElementById('up_button');
   1.159 +  var down_button = document.getElementById('down_button');
   1.160 +
   1.161 +  var activeCharsetSelected = (active_charsets.selectedItems.length > 0);
   1.162 +  remove_button.disabled = !activeCharsetSelected;
   1.163 +
   1.164 +  if (activeCharsetSelected) {
   1.165 +    up_button.disabled = !(active_charsets.selectedItems[0].previousSibling);
   1.166 +    down_button.disabled = !(active_charsets.selectedItems[0].nextSibling);
   1.167 +  }
   1.168 +  else {
   1.169 +    up_button.disabled = true;
   1.170 +    down_button.disabled = true;
   1.171 +  }
   1.172 +
   1.173 +  add_button.disabled = (available_charsets.selectedItems.length == 0);
   1.174 +}
   1.175 +
   1.176 +
   1.177 +
   1.178 +function AddAvailableCharset()
   1.179 +{
   1.180 +  var active_charsets = document.getElementById('active_charsets');
   1.181 +  var available_charsets = document.getElementById('available_charsets');
   1.182 +
   1.183 +  for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length;  nodeIndex++)
   1.184 +  {
   1.185 +    var selItem =  available_charsets.selectedItems[nodeIndex];
   1.186 +    
   1.187 +    var charsetname  = selItem.label;
   1.188 +    var charsetid = selItem.id;
   1.189 +    var already_active = false;
   1.190 +
   1.191 +    for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
   1.192 +      if (charsetid == item.id) {
   1.193 +        already_active = true;
   1.194 +        break;
   1.195 +      }//if
   1.196 +
   1.197 +    }//for
   1.198 +
   1.199 +    if (already_active == false) {
   1.200 +      AddListItem(document, active_charsets, charsetid, charsetname);
   1.201 +    }//if
   1.202 +
   1.203 +  }//for
   1.204 +
   1.205 +  available_charsets.clearSelection();
   1.206 +  enable_save();
   1.207 +
   1.208 +} //AddAvailableCharset
   1.209 +
   1.210 +
   1.211 +
   1.212 +function RemoveActiveCharset()
   1.213 +{
   1.214 +  var listbox = document.getElementById('active_charsets');
   1.215 +  var numSelectedItems = listbox.selectedItems.length;
   1.216 +
   1.217 +  for (var count = 0; count < numSelectedItems; count ++) {
   1.218 +    listbox.removeChild(listbox.selectedItems[0]);
   1.219 +  }
   1.220 +
   1.221 +  listbox.clearSelection();
   1.222 +
   1.223 +  enable_save();
   1.224 +} //RemoveActiveCharset
   1.225 +
   1.226 +
   1.227 +
   1.228 +function Save()
   1.229 +{
   1.230 +  // Iterate through the 'active charsets  tree to collect the charsets
   1.231 +  // that the user has chosen.
   1.232 +
   1.233 +  var active_charsets = document.getElementById('active_charsets');
   1.234 +
   1.235 +  var charsetid    = "";
   1.236 +  var num_charsets = 0;
   1.237 +  var pref_string_content = '';
   1.238 +
   1.239 +  for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
   1.240 +    charsetid = item.id;
   1.241 +
   1.242 +    if (charsetid.length > 1) {
   1.243 +      num_charsets++;
   1.244 +
   1.245 +      //separate >1 charsets by commas
   1.246 +      if (num_charsets > 1)
   1.247 +        pref_string_content = pref_string_content + ", " + charsetid;
   1.248 +      else
   1.249 +        pref_string_content = charsetid;
   1.250 +    }
   1.251 +  }
   1.252 +
   1.253 +  try
   1.254 +  {
   1.255 +    if (prefBranch) {
   1.256 +      pref_string_object.data = pref_string_content;
   1.257 +      prefBranch.setComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString, pref_string_object);
   1.258 +      window.close();
   1.259 +    }
   1.260 +  }
   1.261 +  catch(ex) {
   1.262 +    confirm('exception' + ex);
   1.263 +  }
   1.264 +  return true;
   1.265 +} //Save
   1.266 +
   1.267 +
   1.268 +function MoveUp() {
   1.269 +  var listbox = document.getElementById('active_charsets');
   1.270 +  if (listbox.selectedItems.length == 1) {
   1.271 +    var selected = listbox.selectedItems[0];
   1.272 +    var before = selected.previousSibling
   1.273 +    if (before) {
   1.274 +      listbox.insertBefore(selected, before);
   1.275 +      listbox.selectItem(selected);
   1.276 +      listbox.ensureElementIsVisible(selected);
   1.277 +    }
   1.278 +  }
   1.279 +
   1.280 +  enable_save();
   1.281 +} //MoveUp
   1.282 +
   1.283 +
   1.284 +
   1.285 +function MoveDown() {
   1.286 +  var listbox = document.getElementById('active_charsets');
   1.287 +  if (listbox.selectedItems.length == 1) {
   1.288 +    var selected = listbox.selectedItems[0];
   1.289 +    if (selected.nextSibling) {
   1.290 +      if (selected.nextSibling.nextSibling)
   1.291 +        listbox.insertBefore(selected, selected.nextSibling.nextSibling);
   1.292 +      else
   1.293 +        selected.parentNode.appendChild(selected);
   1.294 +      listbox.selectItem(selected);
   1.295 +    }
   1.296 +  }
   1.297 +
   1.298 +  enable_save();
   1.299 +} //MoveDown
   1.300 +
   1.301 +function AddListItem(doc, listbox, ID, UIstring)
   1.302 +{
   1.303 +  // Create a treerow for the new item
   1.304 +  var item = doc.createElement('listitem');
   1.305 +
   1.306 +  // Copy over the attributes
   1.307 +  item.setAttribute('label', UIstring);
   1.308 +  item.setAttribute('id', ID);
   1.309 +
   1.310 +  listbox.appendChild(item);
   1.311 +}

mercurial