toolkit/content/customizeCharset.js

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:fd578e268f41
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/. */
4
5 //get prefInt services
6
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;
12
13 function Init()
14 {
15 var applicationArea = "";
16
17 if ("arguments" in window && window.arguments[0])
18 applicationArea = window.arguments[0];
19
20 prefBranch = Components.classes["@mozilla.org/preferences-service;1"];
21
22 if (prefBranch) {
23 prefBranch = prefBranch.getService(Components.interfaces.nsIPrefBranch);
24
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 }
31
32 pref_string_object = prefBranch.getComplexValue(pref_string_title, Components.interfaces.nsIPrefLocalizedString)
33 pref_string_content = pref_string_object.data;
34
35 AddRemoveLatin1('add');
36 }
37
38 LoadAvailableCharSets();
39 LoadActiveCharSets();
40 }
41
42
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 }
51
52
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);
64
65 rdfContainer.Init(rdfDataSource, kNC_Root);
66 var availableCharsets = rdfContainer.GetElements();
67 var charset;
68
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;
74
75 AddListItem(document,
76 available_charsets_listbox,
77 availCharsetDict[i][1],
78 availCharsetDict[i][0]);
79 }
80 }
81 catch (e) {}
82 }
83
84
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 }
96
97 function AddRemoveLatin1(action)
98 {
99 var arrayOfPrefs = [];
100 arrayOfPrefs = pref_string_content.split(', ');
101
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 }
109
110 pref_string_content = arrayOfPrefs.join(', ');
111 return;
112 }
113 }
114
115 if (action == 'add') {
116 arrayOfPrefs[arrayOfPrefs.length] = 'ISO-8859-1';
117 pref_string_content = arrayOfPrefs.join(', ');
118 }
119 }
120 }
121
122
123 function LoadActiveCharSets()
124 {
125 var active_charsets = document.getElementById('active_charsets');
126 var arrayOfPrefs = [];
127 var str;
128 var tit;
129
130 arrayOfPrefs = pref_string_content.split(', ');
131
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 }
141
142 function enable_save()
143 {
144 var save_button = document.documentElement.getButton("accept");
145 save_button.removeAttribute('disabled');
146 }
147
148
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');
157
158 var activeCharsetSelected = (active_charsets.selectedItems.length > 0);
159 remove_button.disabled = !activeCharsetSelected;
160
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 }
169
170 add_button.disabled = (available_charsets.selectedItems.length == 0);
171 }
172
173
174
175 function AddAvailableCharset()
176 {
177 var active_charsets = document.getElementById('active_charsets');
178 var available_charsets = document.getElementById('available_charsets');
179
180 for (var nodeIndex=0; nodeIndex < available_charsets.selectedItems.length; nodeIndex++)
181 {
182 var selItem = available_charsets.selectedItems[nodeIndex];
183
184 var charsetname = selItem.label;
185 var charsetid = selItem.id;
186 var already_active = false;
187
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
193
194 }//for
195
196 if (already_active == false) {
197 AddListItem(document, active_charsets, charsetid, charsetname);
198 }//if
199
200 }//for
201
202 available_charsets.clearSelection();
203 enable_save();
204
205 } //AddAvailableCharset
206
207
208
209 function RemoveActiveCharset()
210 {
211 var listbox = document.getElementById('active_charsets');
212 var numSelectedItems = listbox.selectedItems.length;
213
214 for (var count = 0; count < numSelectedItems; count ++) {
215 listbox.removeChild(listbox.selectedItems[0]);
216 }
217
218 listbox.clearSelection();
219
220 enable_save();
221 } //RemoveActiveCharset
222
223
224
225 function Save()
226 {
227 // Iterate through the 'active charsets tree to collect the charsets
228 // that the user has chosen.
229
230 var active_charsets = document.getElementById('active_charsets');
231
232 var charsetid = "";
233 var num_charsets = 0;
234 var pref_string_content = '';
235
236 for (var item = active_charsets.firstChild; item != null; item = item.nextSibling) {
237 charsetid = item.id;
238
239 if (charsetid.length > 1) {
240 num_charsets++;
241
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 }
249
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
263
264
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 }
276
277 enable_save();
278 } //MoveUp
279
280
281
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 }
294
295 enable_save();
296 } //MoveDown
297
298 function AddListItem(doc, listbox, ID, UIstring)
299 {
300 // Create a treerow for the new item
301 var item = doc.createElement('listitem');
302
303 // Copy over the attributes
304 item.setAttribute('label', UIstring);
305 item.setAttribute('id', ID);
306
307 listbox.appendChild(item);
308 }

mercurial