1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/content/charsetOverlay.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,267 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +function MultiplexHandler(aEvent) 1.9 +{ 1.10 + MultiplexHandlerEx( 1.11 + aEvent, 1.12 + function Browser_SelectDetector(event) { 1.13 + BrowserCharsetReload(); 1.14 + /* window.content.location.reload() will re-download everything */ 1.15 + SelectDetector(event, null); 1.16 + }, 1.17 + function Browser_SetForcedCharset(charset, isPredefined) { 1.18 + BrowserSetForcedCharacterSet(charset); 1.19 + } 1.20 + ); 1.21 +} 1.22 + 1.23 +function MailMultiplexHandler(aEvent) 1.24 +{ 1.25 + MultiplexHandlerEx( 1.26 + aEvent, 1.27 + function Mail_SelectDetector(event) { 1.28 + SelectDetector( 1.29 + event, 1.30 + function Mail_Reload() { 1.31 + messenger.setDocumentCharset(msgWindow.mailCharacterSet); 1.32 + } 1.33 + ); 1.34 + }, 1.35 + function Mail_SetForcedCharset(charset, isPredefined) { 1.36 + MessengerSetForcedCharacterSet(charset); 1.37 + } 1.38 + ); 1.39 +} 1.40 + 1.41 +function ComposerMultiplexHandler(aEvent) 1.42 +{ 1.43 + MultiplexHandlerEx( 1.44 + aEvent, 1.45 + function Composer_SelectDetector(event) { 1.46 + SelectDetector( 1.47 + event, 1.48 + function Composer_Reload() { 1.49 + EditorLoadUrl(GetDocumentUrl()); 1.50 + } 1.51 + ); 1.52 + }, 1.53 + function Composer_SetForcedCharset(charset, isPredefined) { 1.54 + if ((!isPredefined) && charset.length > 0) { 1.55 + gCharsetMenu.SetCurrentComposerCharset(charset); 1.56 + } 1.57 + EditorSetDocumentCharacterSet(charset); 1.58 + } 1.59 + ); 1.60 +} 1.61 + 1.62 +function MultiplexHandlerEx(aEvent, aSelectDetector, aSetForcedCharset) 1.63 +{ 1.64 + try { 1.65 + var node = aEvent.target; 1.66 + var name = node.getAttribute('name'); 1.67 + 1.68 + if (name == 'detectorGroup') { 1.69 + aSelectDetector(aEvent); 1.70 + } else if (name == 'charsetGroup') { 1.71 + var charset = node.getAttribute('id'); 1.72 + charset = charset.substring('charset.'.length, charset.length) 1.73 + aSetForcedCharset(charset, true); 1.74 + } else if (name == 'charsetCustomize') { 1.75 + //do nothing - please remove this else statement, once the charset prefs moves to the pref window 1.76 + } else { 1.77 + aSetForcedCharset(node.getAttribute('id'), false); 1.78 + } 1.79 + } catch(ex) { 1.80 + alert(ex); 1.81 + } 1.82 +} 1.83 + 1.84 +function SelectDetector(event, doReload) 1.85 +{ 1.86 + dump("Charset Detector menu item pressed: " + event.target.getAttribute('id') + "\n"); 1.87 + 1.88 + var uri = event.target.getAttribute("id"); 1.89 + var prefvalue = uri.substring('chardet.'.length, uri.length); 1.90 + if ("off" == prefvalue) { // "off" is special value to turn off the detectors 1.91 + prefvalue = ""; 1.92 + } 1.93 + 1.94 + try { 1.95 + var pref = Components.classes["@mozilla.org/preferences-service;1"] 1.96 + .getService(Components.interfaces.nsIPrefBranch); 1.97 + var str = Components.classes["@mozilla.org/supports-string;1"] 1.98 + .createInstance(Components.interfaces.nsISupportsString); 1.99 + 1.100 + str.data = prefvalue; 1.101 + pref.setComplexValue("intl.charset.detector", 1.102 + Components.interfaces.nsISupportsString, str); 1.103 + if (typeof doReload == "function") doReload(); 1.104 + } 1.105 + catch (ex) { 1.106 + dump("Failed to set the intl.charset.detector preference.\n"); 1.107 + } 1.108 +} 1.109 + 1.110 +var gPrevCharset = null; 1.111 +function UpdateCurrentCharset() 1.112 +{ 1.113 + // extract the charset from DOM 1.114 + var wnd = document.commandDispatcher.focusedWindow; 1.115 + if ((window == wnd) || (wnd == null)) wnd = window.content; 1.116 + 1.117 + // Uncheck previous item 1.118 + if (gPrevCharset) { 1.119 + var pref_item = document.getElementById('charset.' + gPrevCharset); 1.120 + if (pref_item) 1.121 + pref_item.setAttribute('checked', 'false'); 1.122 + } 1.123 + 1.124 + var menuitem = document.getElementById('charset.' + wnd.document.characterSet); 1.125 + if (menuitem) { 1.126 + menuitem.setAttribute('checked', 'true'); 1.127 + } 1.128 +} 1.129 + 1.130 +function UpdateCurrentMailCharset() 1.131 +{ 1.132 + var charset = msgWindow.mailCharacterSet; 1.133 + var menuitem = document.getElementById('charset.' + charset); 1.134 + 1.135 + if (menuitem) { 1.136 + menuitem.setAttribute('checked', 'true'); 1.137 + } 1.138 +} 1.139 + 1.140 +function UpdateCharsetDetector() 1.141 +{ 1.142 + var prefvalue; 1.143 + 1.144 + try { 1.145 + var pref = Components.classes["@mozilla.org/preferences-service;1"] 1.146 + .getService(Components.interfaces.nsIPrefBranch); 1.147 + prefvalue = pref.getComplexValue("intl.charset.detector", 1.148 + Components.interfaces.nsIPrefLocalizedString).data; 1.149 + } 1.150 + catch (ex) { 1.151 + prefvalue = ""; 1.152 + } 1.153 + 1.154 + if (prefvalue == "") prefvalue = "off"; 1.155 + dump("intl.charset.detector = "+ prefvalue + "\n"); 1.156 + 1.157 + prefvalue = 'chardet.' + prefvalue; 1.158 + var menuitem = document.getElementById(prefvalue); 1.159 + 1.160 + if (menuitem) { 1.161 + menuitem.setAttribute('checked', 'true'); 1.162 + } 1.163 +} 1.164 + 1.165 +function UpdateMenus(event) 1.166 +{ 1.167 + // use setTimeout workaround to delay checkmark the menu 1.168 + // when onmenucomplete is ready then use it instead of oncreate 1.169 + // see bug 78290 for the detail 1.170 + UpdateCurrentCharset(); 1.171 + setTimeout(UpdateCurrentCharset, 0); 1.172 + UpdateCharsetDetector(); 1.173 + setTimeout(UpdateCharsetDetector, 0); 1.174 +} 1.175 + 1.176 +function CreateMenu(node) 1.177 +{ 1.178 + var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); 1.179 + observerService.notifyObservers(null, "charsetmenu-selected", node); 1.180 +} 1.181 + 1.182 +function UpdateMailMenus(event) 1.183 +{ 1.184 + // use setTimeout workaround to delay checkmark the menu 1.185 + // when onmenucomplete is ready then use it instead of oncreate 1.186 + // see bug 78290 for the detail 1.187 + UpdateCurrentMailCharset(); 1.188 + setTimeout(UpdateCurrentMailCharset, 0); 1.189 + UpdateCharsetDetector(); 1.190 + setTimeout(UpdateCharsetDetector, 0); 1.191 +} 1.192 + 1.193 +var gCharsetMenu = Components.classes['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService().QueryInterface(Components.interfaces.nsICurrentCharsetListener); 1.194 +var gLastBrowserCharset = null; 1.195 + 1.196 +function charsetLoadListener (event) 1.197 +{ 1.198 + var charset = window.content.document.characterSet; 1.199 + 1.200 + if (charset.length > 0 && (charset != gLastBrowserCharset)) { 1.201 + gCharsetMenu.SetCurrentCharset(charset); 1.202 + gPrevCharset = gLastBrowserCharset; 1.203 + gLastBrowserCharset = charset; 1.204 + } 1.205 +} 1.206 + 1.207 + 1.208 +function composercharsetLoadListener (event) 1.209 +{ 1.210 + var charset = window.content.document.characterSet; 1.211 + 1.212 + 1.213 + if (charset.length > 0 ) { 1.214 + gCharsetMenu.SetCurrentComposerCharset(charset); 1.215 + } 1.216 + } 1.217 + 1.218 +function SetForcedEditorCharset(charset) 1.219 +{ 1.220 + if (charset.length > 0 ) { 1.221 + gCharsetMenu.SetCurrentComposerCharset(charset); 1.222 + } 1.223 + EditorSetDocumentCharacterSet(charset); 1.224 +} 1.225 + 1.226 + 1.227 +var gLastMailCharset = null; 1.228 + 1.229 +function mailCharsetLoadListener (event) 1.230 +{ 1.231 + if (msgWindow) { 1.232 + var charset = msgWindow.mailCharacterSet; 1.233 + if (charset.length > 0 && (charset != gLastMailCharset)) { 1.234 + gCharsetMenu.SetCurrentMailCharset(charset); 1.235 + gLastMailCharset = charset; 1.236 + } 1.237 + } 1.238 +} 1.239 + 1.240 +function InitCharsetMenu() 1.241 +{ 1.242 + removeEventListener("load", InitCharsetMenu, true); 1.243 + 1.244 + var wintype = document.documentElement.getAttribute('windowtype'); 1.245 + if (window && (wintype == "navigator:browser")) 1.246 + { 1.247 + var contentArea = window.document.getElementById("appcontent"); 1.248 + if (contentArea) 1.249 + contentArea.addEventListener("pageshow", charsetLoadListener, true); 1.250 + } 1.251 + else 1.252 + { 1.253 + var arrayOfStrings = wintype.split(":"); 1.254 + if (window && arrayOfStrings[0] == "mail") 1.255 + { 1.256 + var messageContent = window.document.getElementById("messagepane"); 1.257 + if (messageContent) 1.258 + messageContent.addEventListener("pageshow", mailCharsetLoadListener, true); 1.259 + } 1.260 + else 1.261 + if (window && arrayOfStrings[0] == "composer") 1.262 + { 1.263 + contentArea = window.document.getElementById("appcontent"); 1.264 + if (contentArea) 1.265 + contentArea.addEventListener("pageshow", composercharsetLoadListener, true); 1.266 + } 1.267 + } 1.268 +} 1.269 + 1.270 +addEventListener("load", InitCharsetMenu, true);