bug 24027, Charset Menu <Static> area: read from prefs and have editing UI

r=matt
a=bobj
This commit is contained in:
jbetak%netscape.com 2006-05-17 02:24:01 +00:00
parent 45f6915f51
commit 6eadb5bf28
3 changed files with 784 additions and 0 deletions

View File

@ -0,0 +1,552 @@
//get prefInt services
var availCharsetList = new Array();
var activeCharsetList = new Array();
var availCharsetDict = new Array();
var ccm = null; //Charset Coverter Mgr.
var prefInt = null; //Preferences Interface
var charsets_pref_string = new String();
function Init()
{
try
{
prefInt = Components.classes["component://netscape/preferences"];
ccm = Components.classes['component://netscape/charset-converter-manager'];
if (ccm) {
ccm = ccm.getService();
ccm = ccm.QueryInterface(Components.interfaces.nsICharsetConverterManager2);
availCharsetList = ccm.GetDecoderList();
availCharsetList = availCharsetList.QueryInterface(Components.interfaces.nsISupportsArray);
availCharsetList.sort;
}
if (prefInt) {
prefInt = prefInt.getService();
prefInt = prefInt.QueryInterface(Components.interfaces.nsIPref);
charsets_pref_string = prefInt.CopyCharPref("intl.charset_menu.static");
AddRemoveLatin1('add');
dump("*** Charset PrefString: " + charsets_pref_string + "\n");
}
}
catch(ex)
{
dump("failed to get prefs or charset mgr. services!\n");
ccm = null;
prefInt = null;
}
LoadAvailableCharSets();
LoadActiveCharSets();
}
function LoadAvailableCharSets()
{
var available_charsets = document.getElementById('available_charsets');
var available_charsets_treeroot = document.getElementById('available_charsets_root');
var invisible = new String();
//if (confirm('Load available Charset Decoders ?')) {
if (availCharsetList) for (i = 0; i < availCharsetList.Count(); i++) {
atom = availCharsetList.GetElementAt(i);
atom = atom.QueryInterface(Components.interfaces.nsIAtom);
if (atom) {
str = atom.GetUnicode();
try {
tit = ccm.GetCharsetTitle(atom);
} catch (ex) {
//tit = str; //ignore charset detectors without a title
}
try {
visible = ccm.GetCharsetData(atom,'.notForBrowser');
visible = false;
} catch (ex) {
visible = true;
dump('Getting invisible for:' + str + ' failed!\n');
}
} //atom
if (str) if (tit) {
availCharsetDict[i] = new Array(2);
availCharsetDict[i][0] = tit;
availCharsetDict[i][1] = str;
availCharsetDict[i][2] = visible;
} //str
} //for
availCharsetDict.sort();
if (availCharsetDict) for (i = 0; i < availCharsetDict.length; i++) {
if (availCharsetDict[i][2]) {
// Create a treerow for the new charset
var item = document.createElement('treeitem');
var row = document.createElement('treerow');
var cell = document.createElement('treecell');
tit = availCharsetDict[i][0];
str = availCharsetDict[i][1];
// Copy over the attributes
cell.setAttribute('value', tit);
cell.setAttribute('id', str);
// Add it to the active charsets tree
item.appendChild(row);
row.appendChild(cell);
available_charsets_treeroot.appendChild(item);
// Select first item
if (i == 0) available_charsets.selectItem(item);
available_charsets.ensureElementIsVisible(item);
dump("*** Added Available Charset: " + tit + "\n");
} //if visible
} //for
//} //confirm
}
function GetCharSetTitle(id)
{
dump("looking up title for:" + id + "\n");
if (availCharsetDict) for (j = 0; j < availCharsetDict.length; j++) {
//dump("ID:" + availCharsetDict[j][1] + " ==> " + availCharsetDict[j][0] + "\n");
if ( availCharsetDict[j][1] == id) {
//title =
dump("found title for:" + id + " ==> " + availCharsetDict[j][0] + "\n");
return availCharsetDict[j][0];
}
}
return '';
}
function GetCharSetVisibility(id)
{
dump("looking up visibility for:" + id + "\n");
if (availCharsetDict) for (j = 0; j < availCharsetDict.length; j++) {
//dump("ID:" + availCharsetDict[j][1] + " ==> " + availCharsetDict[j][2] + "\n");
if ( availCharsetDict[j][1] == id) {
//title =
dump("found visibility for:" + id + " ==> " + availCharsetDict[j][2] + "\n");
return availCharsetDict[j][2];
}
}
return false;
}
function AddRemoveLatin1(action)
{
try {
arrayOfPrefs = charsets_pref_string.split(', ');
}
catch (ex) {
dump("failed to split the preference string!\n");
}
if (arrayOfPrefs) for (i = 0; i < arrayOfPrefs.length; i++) {
str = arrayOfPrefs[i];
if (str == 'iso-8859-1') {
if (action == 'remove') {
arrayOfPrefs[i]=arrayOfPrefs[arrayOfPrefs.length-1];
arrayOfPrefs.length = arrayOfPrefs.length - 1;
}
charsets_pref_string = arrayOfPrefs.join(', ');
return;
}
} //for
if (action == 'add') {
arrayOfPrefs[arrayOfPrefs.length]= 'iso-8859-1';
charsets_pref_string = arrayOfPrefs.join(', ');
}
}
function LoadActiveCharSets()
{
var active_charsets = document.getElementById('active_charsets');
var active_charsets_treeroot = document.getElementById('active_charsets_root');
var visible;
//var charsets_pref_string = new String();
try {
arrayOfPrefs = charsets_pref_string.split(', ');
}
catch (ex) {
dump("failed to split the preference string!\n");
}
//if (confirm('Load active Charset Decoders ?')) {
if (arrayOfPrefs) for (i = 0; i < arrayOfPrefs.length; i++) {
str = arrayOfPrefs[i];
try {
atom = atom.QueryInterface(Components.interfaces.nsIAtom);
} catch (ex) {
dump("failed to load atom interface...\n" );
}
try {
atom.ToString(str); } catch (ex) {
dump("failed to create an atom from:" + str + "\n" );
}
if (atom) {
try {
tit = ccm.GetCharsetTitle(NS_NewAtom(str));
}
catch (ex) {
tit = GetCharSetTitle(str);
visible = GetCharSetVisibility(str);
if (tit == '') tit = str;
//if (title != '') tit = title;
dump("failed to get title for:" + str + "\n" );
}
} //atom
//dump("*** Charset: " + str + "\n");
//tit = 'Test';
//str = 'Test';
if (str) if (tit) if (visible) {
dump("Adding Active Charset: " + str + " ==> " + tit + "\n");
// Create a treerow for the new charset
var item = document.createElement('treeitem');
var row = document.createElement('treerow');
var cell = document.createElement('treecell');
// Copy over the attributes
cell.setAttribute('value', tit);
cell.setAttribute('id', str);
// Add it to the active charsets tree
item.appendChild(row);
row.appendChild(cell);
active_charsets_treeroot.appendChild(item);
// Select is only if the caller wants to.
active_charsets.selectItem(item);
active_charsets.ensureElementIsVisible(item);
dump("*** Added Active Charset: " + tit + "\n");
} //if
} //for
//confirm('intl.charset_menu.static: '+charsets_pref_string+' ...');
//} //confirm
}
function SelectAvailableCharset()
{
//Remove the selection in the active charsets list
var active_charsets = document.getElementById('active_charsets');
if (active_charsets.selectedCells.length > 0)
active_charsets.clearCellSelection();
if (active_charsets.selectedItems.length > 0)
active_charsets.clearItemSelection();
enable_add_button();
} //SelectAvailableCharset
function SelectActiveCharset()
{
//Remove the selection in the available charsets list
var available_charsets = document.getElementById('available_charsets');
if (available_charsets.selectedCells.length > 0)
available_charsets.clearCellSelection();
if (available_charsets.selectedItems.length > 0)
available_charsets.clearItemSelection();
enable_remove_button();
} //SelectActiveCharset
function enable_remove_button()
{
var remove_button = document.getElementById('remove_button');
remove_button.setAttribute('disabled','false');
}
function enable_save()
{
var save_button = document.getElementById('save_button');
save_button.setAttribute('disabled','false');
}
function enable_add_button()
{
var add_button = document.getElementById('add_button');
add_button.setAttribute('disabled','false');
}
function AddAvailableCharset()
{
var active_charsets = document.getElementById('active_charsets');
var active_charsets_treeroot = document.getElementById('active_charsets_root');
var available_charsets = document.getElementById('available_charsets');
for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length; nodeIndex++) {
var selItem = available_charsets.selectedItems[nodeIndex];
var selRow = selItem.firstChild;
var selCell = selRow.firstChild;
var charsetname = selCell.getAttribute('value');
var charsetid = selCell.getAttribute('id');
var already_active = false;
for (var item = active_charsets_treeroot.firstChild; item != null; item = item.nextSibling) {
var row = item.firstChild;
var cell = row.firstChild;
var active_charsetid = cell.getAttribute('id');
if (active_charsetid == charsetid) {
already_active = true;
break;
}
}
if (already_active == false) {
// Create a treerow for the new charset
var item = document.createElement('treeitem');
var row = document.createElement('treerow');
var cell = document.createElement('treecell');
// Copy over the attributes
cell.setAttribute('value', charsetname);
cell.setAttribute('id', charsetid);
// Add it to the active charsets tree
item.appendChild(row);
row.appendChild(cell);
active_charsets_treeroot.appendChild(item);
// Select is only if the caller wants to.
active_charsets.selectItem(item);
active_charsets.ensureElementIsVisible(item);
}//add new item
} //loop selected charsets
available_charsets.clearItemSelection();
enable_save();
} //AddAvailableCharset
function RemoveActiveCharset()
{
var active_charsets_treeroot = document.getElementById('active_charsets_root');
var tree = document.getElementById('active_charsets');
var nextNode = null;
var numSelected = tree.selectedItems.length;
var deleted_all = false;
while (tree.selectedItems.length > 0) {
var selectedNode = tree.selectedItems[0];
nextNode = selectedNode.nextSibling;
if (!nextNode)
if (selectedNode.previousSibling)
nextNode = selectedNode.previousSibling;
var row = selectedNode.firstChild;
var cell = row.firstChild;
row.removeChild(cell);
selectedNode.removeChild(row);
active_charsets_treeroot.removeChild(selectedNode);
} //while
if (nextNode) {
tree.selectItem(nextNode)
} else {
//tree.clearItemSelection();
}
enable_save();
} //RemoveActiveCharset
function Save()
{
// Iterate through the 'active charsets tree to collect the charsets
// that the user has chosen.
var active_charsets = document.getElementById('active_charsets');
var active_charsets_treeroot = document.getElementById('active_charsets_root');
var row = null;
var cell = null;
var charsetid = new String();
var num_charsets = 0;
charsets_pref_string = '';
for (var item = active_charsets_treeroot.firstChild; item != null; item = item.nextSibling) {
row = item.firstChild;
cell = row.firstChild;
charsetid = cell.getAttribute('id');
if (charsetid.length > 4) {
num_charsets++;
//separate >1 charsets by commas
if (num_charsets > 1) {
charsets_pref_string = charsets_pref_string + "," + " " + charsetid;
} else {
charsets_pref_string = charsetid;
}
}
}
try
{
if (prefInt)
{
//AddRemoveLatin1('remove');
prefInt.SetCharPref("intl.charset_menu.static", charsets_pref_string);
//prefInt.SetCharPref("browser.startup.homepage", charsets_pref_string);
//prefInt.CopyCharPref("intl.charset_menu.static", charsets_pref_string);
//prefInt.SetCharPref("browser.startup.homepage", "www.betak.net");
confirm_text = document.getElementById('confirm_text');
dump('intl.charset_menu.static set to ' + charsets_pref_string + '.\n');
window.close();
confirm(confirm_text.getAttribute('value'));
}
}
catch(ex)
{
confirm('exception' + ex);
//prefInt.SetDefaultCharPref("intl.charset_menu.static", "iso-8859-1, iso-2022-jp, shift_jis, euc-jp");
//window.close();
}
} //Save
function MoveUp() {
var tree = document.getElementById('active_charsets');
if (tree.selectedItems.length == 1) {
var selected = tree.selectedItems[0];
var before = selected.previousSibling
if (before) {
before.parentNode.insertBefore(selected, before);
tree.selectItem(selected);
tree.ensureElementIsVisible(selected);
}
}
//enable_buttons_for_current_panels();
enable_save();
} //MoveUp
function MoveDown() {
var tree = document.getElementById('active_charsets');
if (tree.selectedItems.length == 1) {
var selected = tree.selectedItems[0];
if (selected.nextSibling) {
if (selected.nextSibling.nextSibling) {
selected.parentNode.insertBefore(selected, selected.nextSibling.nextSibling);
}
else {
selected.parentNode.appendChild(selected);
}
tree.selectItem(selected);
}
}
//enable_buttons_for_current_panels();
enable_save();
} //MoveDown

View File

@ -0,0 +1,195 @@
<?xml version="1.0"?> <!-- -*- Mode: SGML; indent-tabs-mode: nil; -*- -->
<!--
The contents of this file are subject to the Netscape Public License
Version 1.1 (the "NPL"); you may not use this file except in
compliance with the NPL. You may obtain a copy of the NPL at
http://www.mozilla.org/NPL/
Software distributed under the NPL is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
for the specific language governing rights and limitations under the
NPL.
The Initial Developer of this code under the NPL is Netscape
Communications Corporation. Portions created by Netscape are
Copyright (C) 1999 Netscape Communications Corporation. All Rights
Reserved.
-->
<?xml-stylesheet href="chrome://pref/skin/" type="text/css"?>
<!DOCTYPE window SYSTEM "chrome://pref/locale/pref-charset.dtd" >
<window
xmlns:html="http://www.w3.org/TR/REC-html40"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
id="main-box"
title="&charset.customize.title.label;"
class="dialog"
windowtype="sidebar:customize"
onload="Init();"
align="vertical"
persist="width height"
width="500" height="320">
<html:script language="javascript" src="pref-charset.js"/>
<!-- horz 1 -->
<box class="header" align="horizontal">
<titledbutton class="left-header-text" value="&charset.customize.left.header;"/>
<spring class="header-spring" flex="1"/>
<titledbutton class="right-header-text" value="&charset.customize.right.header;"/>
</box>
<!-- horz1 -->
<!-- The two-pane -->
<!-- horz 2 -->
<box align="horizontal" flex="1">
<!-- Spacing between the left window border and first list box -->
<spring style="width:2em"/>
<!-- All of the panels that are available -->
<!-- vert 1 -->
<box align="vertical" flex="1">
<html:div class="vertical-gap">
&charset.customize.additional.label;
</html:div>
<!-- horz 2a -->
<box align="horizontal" class="box-group" flex="1">
<!-- Hidden text for our save confirm popup -->
<box align="horizontal" style="max-width: 0px; max-height: 0px">
<titledbutton
id="confirm_text"
style="max-width: 0px; max-height: 0px"
class="left-header-text"
value="&charset.customize.save.popup;" />
</box>
<!-- Available Charsets Tree -->
<tree id="available_charsets" flex="1"
onclick="SelectAvailableCharset();"
datasources="rdf:null"
indent="false"
multiple="true"
style="height:0px">
<!-- We explicitly create a 'treechildren' so we can refer
to it from the script -->
<treechildren id="available_charsets_root">
</treechildren>
</tree>
</box>
<!-- horz 2a -->
<!-- Add Charset Button -->
<!-- horz 3 -->
<box align="horizontal" class="button-group">
<titledbutton
id="add_button"
onclick="AddAvailableCharset();"
class="dialog push"
value="&charset.customize.add.label;"
disabled="true"/>
</box>
<!-- horz 3 -->
</box>
<!-- vert 1 -->
<!-- Spacing between the left and right halves of the dialog -->
<spring style="width:2em"/>
<!-- The charsets that the user currently has chosen -->
<!-- vert 2 -->
<box align="vertical" flex="1">
<html:div class="vertical-gap">
&charset.customize.current.label;
</html:div>
<!-- ??? <spring class="vgap"/> ???? -->
<!-- horz 4 -->
<box align="horizontal" class="box-group" flex="1">
<!-- Active Charsets Tree -->
<tree id="active_charsets" flex="1"
onclick="SelectActiveCharset();"
datasources="rdf:null"
indent="false"
multiple="true"
style="height:0px">
<!-- We explicitly create a 'treechildren' so we can refer
to it from the script -->
<treechildren id="active_charsets_root">
</treechildren>
</tree>
</box>
<!-- koriz 4 -->
<!-- The Remove Charset Button -->
<!-- horz 5 -->
<box align="horizontal" class="button-group">
<titledbutton id="remove_button" onclick="RemoveActiveCharset();"
class="dialog push" disabled="true"
value="&charset.customize.remove.label;" />
</box>
<!-- horz 5 -->
</box>
<!-- vert 2 -->
<!-- Spacing between the right selector and "Reorder" -->
<spring style="width:8px"/>
<!-- The Reorder Button -->
<!-- vert 3 -->
<box align="vertical">
<spring flex="50%"/>
<titledbutton onclick="MoveUp();" id="up" class="borderless up" />
<html:div>
&charset.customize.reorder.label;
</html:div>
<titledbutton onclick="MoveDown();" id="down"
class="borderless down" />
<spring flex="50%"/>
</box>
<!-- vert 3 -->
</box>
<!-- horz 2 -->
<html:div>
<html:hr />
</html:div>
<!-- The 'Save' and 'Cancel' buttons -->
<!-- horz 6 -->
<box align="horizontal">
<spring flex="48%"/>
<titledbutton onclick="window.close()"
class="dialog push"
value="&charset.customize.cancel.label;" />
<spring flex="4%"/>
<titledbutton id="save_button" onclick="Save();"
class="dialog push"
value="&charset.customize.save.label;"
disabled="true"/>
<spring flex="48%"/>
</box>
<!-- horz 6 -->
</window>

View File

@ -0,0 +1,37 @@
<!--
- The contents of this file are subject to the Mozilla Public
- License Version 1.1 (the "License"); you may not use this file
- except in compliance with the License. You may obtain a copy of
- the License at http://www.mozilla.org/MPL/
-
- Software distributed under the License is distributed on an "AS
- IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
- implied. See the License for the specific language governing
- rights and limitations under the License.
-
- The Original Code is Mozilla Communicator.
-
- The Initial Developer of the Original Code is Netscape
- Communications Corp. Portions created by Netscape Communications
- Corp. are Copyright (C) 1999 Netscape Communications Corp. All
- Rights Reserved.
-
- Contributor(s): Stephen Lamm <slamm@netscape.com>
-->
<!-- extracted from ./pref-charset.xul -->
<!-- LOCALIZATION NOTE Charset Coding Preferences Dialog: Do NOT localize the term "Character Coding" -->
<!ENTITY charset.customize.title.label "Customize Character Coding">
<!ENTITY charset.customize.left.header "Character Coding">
<!ENTITY charset.customize.right.header "Select Supported Charsets">
<!ENTITY charset.customize.current.label "Active Charactersets">
<!ENTITY charset.customize.reorder.label "Reorder">
<!ENTITY charset.customize.remove.label "Remove">
<!ENTITY charset.customize.additional.label "Available Charactersets">
<!ENTITY charset.customize.add.label "Add">
<!ENTITY charset.customize.cancel.label "Cancel">
<!ENTITY charset.customize.save.label "Save">
<!ENTITY charset.customize.save.popup "Please restart the browser for changes to take effect...">