toolkit/content/customizeCharset.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 //get prefInt services
     7 var availCharsetDict     = [];
     8 var prefBranch           = null; //Preferences Interface
     9 var pref_string_title    = "";
    10 var pref_string_content  = "";
    11 var pref_string_object   = null;
    13 function Init()
    14 {
    15   var applicationArea = "";
    17   if ("arguments" in window && window.arguments[0])
    18     applicationArea = window.arguments[0];
    20   prefBranch = Components.classes["@mozilla.org/preferences-service;1"];
    22   if (prefBranch) {
    23     prefBranch = prefBranch.getService(Components.interfaces.nsIPrefBranch);
    25     if (applicationArea.indexOf("mail") != -1) {
    26       pref_string_title = "intl.charsetmenu.mailedit";
    27     } else {
    28     //default is the browser
    29       pref_string_title = "intl.charsetmenu.browser.static";
    30     }
    32     pref_string_object = prefBranch.getComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString)
    33     pref_string_content = pref_string_object.data;
    35     AddRemoveLatin1('add');
    36   }
    38   LoadAvailableCharSets();
    39   LoadActiveCharSets();
    40 }
    43 function readRDFString(aDS,aRes,aProp) 
    44 {
    45   var n = aDS.GetTarget(aRes, aProp, true);
    46   if (n)
    47     return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
    48   else 
    49     return "";
    50 }
    53 function LoadAvailableCharSets()
    54 {
    55   try {
    56     var available_charsets_listbox = document.getElementById('available_charsets');
    57     var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); 
    58     var kNC_Root = rdf.GetResource("NC:DecodersRoot");
    59     var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name");
    60     var rdfDataSource = rdf.GetDataSource("rdf:charset-menu"); 
    61     var rdfContainer =
    62       Components.classes["@mozilla.org/rdf/container;1"]
    63                 .createInstance(Components.interfaces.nsIRDFContainer);
    65     rdfContainer.Init(rdfDataSource, kNC_Root);
    66     var availableCharsets = rdfContainer.GetElements();
    67     var charset;
    69     for (var i = 0; i < rdfContainer.GetCount(); i++) {
    70       charset = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
    71       availCharsetDict[i] = new Array(2);
    72       availCharsetDict[i][0] = readRDFString(rdfDataSource, charset, kNC_name);
    73       availCharsetDict[i][1] = charset.Value;
    75       AddListItem(document,
    76                   available_charsets_listbox,
    77                   availCharsetDict[i][1],
    78                   availCharsetDict[i][0]);
    79     }
    80   }
    81   catch (e) {}
    82 }
    85 function GetCharSetTitle(id)
    86 {
    87   if (availCharsetDict) {
    88     for (var j = 0; j < availCharsetDict.length; j++) {
    89       if (availCharsetDict[j][1] == id) {
    90         return availCharsetDict[j][0];
    91       }
    92     }
    93   }
    94   return '';
    95 }
    97 function AddRemoveLatin1(action)
    98 {
    99   var arrayOfPrefs = [];
   100   arrayOfPrefs = pref_string_content.split(', ');
   102   if (arrayOfPrefs.length > 0) {
   103     for (var i = 0; i < arrayOfPrefs.length; i++) {
   104       if (arrayOfPrefs[i] == 'ISO-8859-1') {
   105         if (action == 'remove') {
   106           arrayOfPrefs[i] = arrayOfPrefs[arrayOfPrefs.length-1];
   107           arrayOfPrefs.length = arrayOfPrefs.length - 1;
   108         }
   110         pref_string_content = arrayOfPrefs.join(', ');
   111         return;
   112       }
   113     }
   115     if (action == 'add') {
   116       arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1';
   117       pref_string_content = arrayOfPrefs.join(', ');
   118     }
   119   }
   120 }
   123 function LoadActiveCharSets()
   124 {
   125   var active_charsets = document.getElementById('active_charsets');
   126   var arrayOfPrefs = [];
   127   var str;
   128   var tit;
   130   arrayOfPrefs = pref_string_content.split(', ');
   132   if (arrayOfPrefs.length > 0) {
   133     for (var i = 0; i < arrayOfPrefs.length; i++) {
   134       str = arrayOfPrefs[i];
   135       tit = GetCharSetTitle(str);
   136       if (str && tit)
   137         AddListItem(document, active_charsets, str, tit);
   138     }
   139   }
   140 }
   142 function enable_save()
   143 {
   144   var save_button = document.documentElement.getButton("accept");
   145   save_button.removeAttribute('disabled');
   146 }
   149 function update_buttons()
   150 {
   151   var available_charsets = document.getElementById('available_charsets');
   152   var active_charsets = document.getElementById('active_charsets');
   153   var remove_button = document.getElementById('remove_button');
   154   var add_button = document.getElementById('add_button');
   155   var up_button = document.getElementById('up_button');
   156   var down_button = document.getElementById('down_button');
   158   var activeCharsetSelected = (active_charsets.selectedItems.length > 0);
   159   remove_button.disabled = !activeCharsetSelected;
   161   if (activeCharsetSelected) {
   162     up_button.disabled = !(active_charsets.selectedItems[0].previousSibling);
   163     down_button.disabled = !(active_charsets.selectedItems[0].nextSibling);
   164   }
   165   else {
   166     up_button.disabled = true;
   167     down_button.disabled = true;
   168   }
   170   add_button.disabled = (available_charsets.selectedItems.length == 0);
   171 }
   175 function AddAvailableCharset()
   176 {
   177   var active_charsets = document.getElementById('active_charsets');
   178   var available_charsets = document.getElementById('available_charsets');
   180   for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length;  nodeIndex++)
   181   {
   182     var selItem =  available_charsets.selectedItems[nodeIndex];
   184     var charsetname  = selItem.label;
   185     var charsetid = selItem.id;
   186     var already_active = false;
   188     for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
   189       if (charsetid == item.id) {
   190         already_active = true;
   191         break;
   192       }//if
   194     }//for
   196     if (already_active == false) {
   197       AddListItem(document, active_charsets, charsetid, charsetname);
   198     }//if
   200   }//for
   202   available_charsets.clearSelection();
   203   enable_save();
   205 } //AddAvailableCharset
   209 function RemoveActiveCharset()
   210 {
   211   var listbox = document.getElementById('active_charsets');
   212   var numSelectedItems = listbox.selectedItems.length;
   214   for (var count = 0; count < numSelectedItems; count ++) {
   215     listbox.removeChild(listbox.selectedItems[0]);
   216   }
   218   listbox.clearSelection();
   220   enable_save();
   221 } //RemoveActiveCharset
   225 function Save()
   226 {
   227   // Iterate through the 'active charsets  tree to collect the charsets
   228   // that the user has chosen.
   230   var active_charsets = document.getElementById('active_charsets');
   232   var charsetid    = "";
   233   var num_charsets = 0;
   234   var pref_string_content = '';
   236   for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
   237     charsetid = item.id;
   239     if (charsetid.length > 1) {
   240       num_charsets++;
   242       //separate >1 charsets by commas
   243       if (num_charsets > 1)
   244         pref_string_content = pref_string_content + ", " + charsetid;
   245       else
   246         pref_string_content = charsetid;
   247     }
   248   }
   250   try
   251   {
   252     if (prefBranch) {
   253       pref_string_object.data = pref_string_content;
   254       prefBranch.setComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString, pref_string_object);
   255       window.close();
   256     }
   257   }
   258   catch(ex) {
   259     confirm('exception' + ex);
   260   }
   261   return true;
   262 } //Save
   265 function MoveUp() {
   266   var listbox = document.getElementById('active_charsets');
   267   if (listbox.selectedItems.length == 1) {
   268     var selected = listbox.selectedItems[0];
   269     var before = selected.previousSibling
   270     if (before) {
   271       listbox.insertBefore(selected, before);
   272       listbox.selectItem(selected);
   273       listbox.ensureElementIsVisible(selected);
   274     }
   275   }
   277   enable_save();
   278 } //MoveUp
   282 function MoveDown() {
   283   var listbox = document.getElementById('active_charsets');
   284   if (listbox.selectedItems.length == 1) {
   285     var selected = listbox.selectedItems[0];
   286     if (selected.nextSibling) {
   287       if (selected.nextSibling.nextSibling)
   288         listbox.insertBefore(selected, selected.nextSibling.nextSibling);
   289       else
   290         selected.parentNode.appendChild(selected);
   291       listbox.selectItem(selected);
   292     }
   293   }
   295   enable_save();
   296 } //MoveDown
   298 function AddListItem(doc, listbox, ID, UIstring)
   299 {
   300   // Create a treerow for the new item
   301   var item = doc.createElement('listitem');
   303   // Copy over the attributes
   304   item.setAttribute('label', UIstring);
   305   item.setAttribute('id', ID);
   307   listbox.appendChild(item);
   308 }

mercurial