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

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

mercurial