michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: //get prefInt services michael@0: michael@0: var availCharsetDict = []; michael@0: var prefBranch = null; //Preferences Interface michael@0: var pref_string_title = ""; michael@0: var pref_string_content = ""; michael@0: var pref_string_object = null; michael@0: michael@0: function Init() michael@0: { michael@0: var applicationArea = ""; michael@0: michael@0: if ("arguments" in window && window.arguments[0]) michael@0: applicationArea = window.arguments[0]; michael@0: michael@0: prefBranch = Components.classes["@mozilla.org/preferences-service;1"]; michael@0: michael@0: if (prefBranch) { michael@0: prefBranch = prefBranch.getService(Components.interfaces.nsIPrefBranch); michael@0: michael@0: if (applicationArea.indexOf("mail") != -1) { michael@0: pref_string_title = "intl.charsetmenu.mailedit"; michael@0: } else { michael@0: //default is the browser michael@0: pref_string_title = "intl.charsetmenu.browser.static"; michael@0: } michael@0: michael@0: pref_string_object = prefBranch.getComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString) michael@0: pref_string_content = pref_string_object.data; michael@0: michael@0: AddRemoveLatin1('add'); michael@0: } michael@0: michael@0: LoadAvailableCharSets(); michael@0: LoadActiveCharSets(); michael@0: } michael@0: michael@0: michael@0: function readRDFString(aDS,aRes,aProp) michael@0: { michael@0: var n = aDS.GetTarget(aRes, aProp, true); michael@0: if (n) michael@0: return n.QueryInterface(Components.interfaces.nsIRDFLiteral).Value; michael@0: else michael@0: return ""; michael@0: } michael@0: michael@0: michael@0: function LoadAvailableCharSets() michael@0: { michael@0: try { michael@0: var available_charsets_listbox = document.getElementById('available_charsets'); michael@0: var rdf=Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService); michael@0: var kNC_Root = rdf.GetResource("NC:DecodersRoot"); michael@0: var kNC_name = rdf.GetResource("http://home.netscape.com/NC-rdf#Name"); michael@0: var rdfDataSource = rdf.GetDataSource("rdf:charset-menu"); michael@0: var rdfContainer = michael@0: Components.classes["@mozilla.org/rdf/container;1"] michael@0: .createInstance(Components.interfaces.nsIRDFContainer); michael@0: michael@0: rdfContainer.Init(rdfDataSource, kNC_Root); michael@0: var availableCharsets = rdfContainer.GetElements(); michael@0: var charset; michael@0: michael@0: for (var i = 0; i < rdfContainer.GetCount(); i++) { michael@0: charset = availableCharsets.getNext().QueryInterface(Components.interfaces.nsIRDFResource); michael@0: availCharsetDict[i] = new Array(2); michael@0: availCharsetDict[i][0] = readRDFString(rdfDataSource, charset, kNC_name); michael@0: availCharsetDict[i][1] = charset.Value; michael@0: michael@0: AddListItem(document, michael@0: available_charsets_listbox, michael@0: availCharsetDict[i][1], michael@0: availCharsetDict[i][0]); michael@0: } michael@0: } michael@0: catch (e) {} michael@0: } michael@0: michael@0: michael@0: function GetCharSetTitle(id) michael@0: { michael@0: if (availCharsetDict) { michael@0: for (var j = 0; j < availCharsetDict.length; j++) { michael@0: if (availCharsetDict[j][1] == id) { michael@0: return availCharsetDict[j][0]; michael@0: } michael@0: } michael@0: } michael@0: return ''; michael@0: } michael@0: michael@0: function AddRemoveLatin1(action) michael@0: { michael@0: var arrayOfPrefs = []; michael@0: arrayOfPrefs = pref_string_content.split(', '); michael@0: michael@0: if (arrayOfPrefs.length > 0) { michael@0: for (var i = 0; i < arrayOfPrefs.length; i++) { michael@0: if (arrayOfPrefs[i] == 'ISO-8859-1') { michael@0: if (action == 'remove') { michael@0: arrayOfPrefs[i] = arrayOfPrefs[arrayOfPrefs.length-1]; michael@0: arrayOfPrefs.length = arrayOfPrefs.length - 1; michael@0: } michael@0: michael@0: pref_string_content = arrayOfPrefs.join(', '); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: if (action == 'add') { michael@0: arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1'; michael@0: pref_string_content = arrayOfPrefs.join(', '); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: function LoadActiveCharSets() michael@0: { michael@0: var active_charsets = document.getElementById('active_charsets'); michael@0: var arrayOfPrefs = []; michael@0: var str; michael@0: var tit; michael@0: michael@0: arrayOfPrefs = pref_string_content.split(', '); michael@0: michael@0: if (arrayOfPrefs.length > 0) { michael@0: for (var i = 0; i < arrayOfPrefs.length; i++) { michael@0: str = arrayOfPrefs[i]; michael@0: tit = GetCharSetTitle(str); michael@0: if (str && tit) michael@0: AddListItem(document, active_charsets, str, tit); michael@0: } michael@0: } michael@0: } michael@0: michael@0: function enable_save() michael@0: { michael@0: var save_button = document.documentElement.getButton("accept"); michael@0: save_button.removeAttribute('disabled'); michael@0: } michael@0: michael@0: michael@0: function update_buttons() michael@0: { michael@0: var available_charsets = document.getElementById('available_charsets'); michael@0: var active_charsets = document.getElementById('active_charsets'); michael@0: var remove_button = document.getElementById('remove_button'); michael@0: var add_button = document.getElementById('add_button'); michael@0: var up_button = document.getElementById('up_button'); michael@0: var down_button = document.getElementById('down_button'); michael@0: michael@0: var activeCharsetSelected = (active_charsets.selectedItems.length > 0); michael@0: remove_button.disabled = !activeCharsetSelected; michael@0: michael@0: if (activeCharsetSelected) { michael@0: up_button.disabled = !(active_charsets.selectedItems[0].previousSibling); michael@0: down_button.disabled = !(active_charsets.selectedItems[0].nextSibling); michael@0: } michael@0: else { michael@0: up_button.disabled = true; michael@0: down_button.disabled = true; michael@0: } michael@0: michael@0: add_button.disabled = (available_charsets.selectedItems.length == 0); michael@0: } michael@0: michael@0: michael@0: michael@0: function AddAvailableCharset() michael@0: { michael@0: var active_charsets = document.getElementById('active_charsets'); michael@0: var available_charsets = document.getElementById('available_charsets'); michael@0: michael@0: for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length; nodeIndex++) michael@0: { michael@0: var selItem = available_charsets.selectedItems[nodeIndex]; michael@0: michael@0: var charsetname = selItem.label; michael@0: var charsetid = selItem.id; michael@0: var already_active = false; michael@0: michael@0: for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) { michael@0: if (charsetid == item.id) { michael@0: already_active = true; michael@0: break; michael@0: }//if michael@0: michael@0: }//for michael@0: michael@0: if (already_active == false) { michael@0: AddListItem(document, active_charsets, charsetid, charsetname); michael@0: }//if michael@0: michael@0: }//for michael@0: michael@0: available_charsets.clearSelection(); michael@0: enable_save(); michael@0: michael@0: } //AddAvailableCharset michael@0: michael@0: michael@0: michael@0: function RemoveActiveCharset() michael@0: { michael@0: var listbox = document.getElementById('active_charsets'); michael@0: var numSelectedItems = listbox.selectedItems.length; michael@0: michael@0: for (var count = 0; count < numSelectedItems; count ++) { michael@0: listbox.removeChild(listbox.selectedItems[0]); michael@0: } michael@0: michael@0: listbox.clearSelection(); michael@0: michael@0: enable_save(); michael@0: } //RemoveActiveCharset michael@0: michael@0: michael@0: michael@0: function Save() michael@0: { michael@0: // Iterate through the 'active charsets tree to collect the charsets michael@0: // that the user has chosen. michael@0: michael@0: var active_charsets = document.getElementById('active_charsets'); michael@0: michael@0: var charsetid = ""; michael@0: var num_charsets = 0; michael@0: var pref_string_content = ''; michael@0: michael@0: for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) { michael@0: charsetid = item.id; michael@0: michael@0: if (charsetid.length > 1) { michael@0: num_charsets++; michael@0: michael@0: //separate >1 charsets by commas michael@0: if (num_charsets > 1) michael@0: pref_string_content = pref_string_content + ", " + charsetid; michael@0: else michael@0: pref_string_content = charsetid; michael@0: } michael@0: } michael@0: michael@0: try michael@0: { michael@0: if (prefBranch) { michael@0: pref_string_object.data = pref_string_content; michael@0: prefBranch.setComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString, pref_string_object); michael@0: window.close(); michael@0: } michael@0: } michael@0: catch(ex) { michael@0: confirm('exception' + ex); michael@0: } michael@0: return true; michael@0: } //Save michael@0: michael@0: michael@0: function MoveUp() { michael@0: var listbox = document.getElementById('active_charsets'); michael@0: if (listbox.selectedItems.length == 1) { michael@0: var selected = listbox.selectedItems[0]; michael@0: var before = selected.previousSibling michael@0: if (before) { michael@0: listbox.insertBefore(selected, before); michael@0: listbox.selectItem(selected); michael@0: listbox.ensureElementIsVisible(selected); michael@0: } michael@0: } michael@0: michael@0: enable_save(); michael@0: } //MoveUp michael@0: michael@0: michael@0: michael@0: function MoveDown() { michael@0: var listbox = document.getElementById('active_charsets'); michael@0: if (listbox.selectedItems.length == 1) { michael@0: var selected = listbox.selectedItems[0]; michael@0: if (selected.nextSibling) { michael@0: if (selected.nextSibling.nextSibling) michael@0: listbox.insertBefore(selected, selected.nextSibling.nextSibling); michael@0: else michael@0: selected.parentNode.appendChild(selected); michael@0: listbox.selectItem(selected); michael@0: } michael@0: } michael@0: michael@0: enable_save(); michael@0: } //MoveDown michael@0: michael@0: function AddListItem(doc, listbox, ID, UIstring) michael@0: { michael@0: // Create a treerow for the new item michael@0: var item = doc.createElement('listitem'); michael@0: michael@0: // Copy over the attributes michael@0: item.setAttribute('label', UIstring); michael@0: item.setAttribute('id', ID); michael@0: michael@0: listbox.appendChild(item); michael@0: }