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: function MultiplexHandler(aEvent) michael@0: { michael@0: MultiplexHandlerEx( michael@0: aEvent, michael@0: function Browser_SelectDetector(event) { michael@0: BrowserCharsetReload(); michael@0: /* window.content.location.reload() will re-download everything */ michael@0: SelectDetector(event, null); michael@0: }, michael@0: function Browser_SetForcedCharset(charset, isPredefined) { michael@0: BrowserSetForcedCharacterSet(charset); michael@0: } michael@0: ); michael@0: } michael@0: michael@0: function MailMultiplexHandler(aEvent) michael@0: { michael@0: MultiplexHandlerEx( michael@0: aEvent, michael@0: function Mail_SelectDetector(event) { michael@0: SelectDetector( michael@0: event, michael@0: function Mail_Reload() { michael@0: messenger.setDocumentCharset(msgWindow.mailCharacterSet); michael@0: } michael@0: ); michael@0: }, michael@0: function Mail_SetForcedCharset(charset, isPredefined) { michael@0: MessengerSetForcedCharacterSet(charset); michael@0: } michael@0: ); michael@0: } michael@0: michael@0: function ComposerMultiplexHandler(aEvent) michael@0: { michael@0: MultiplexHandlerEx( michael@0: aEvent, michael@0: function Composer_SelectDetector(event) { michael@0: SelectDetector( michael@0: event, michael@0: function Composer_Reload() { michael@0: EditorLoadUrl(GetDocumentUrl()); michael@0: } michael@0: ); michael@0: }, michael@0: function Composer_SetForcedCharset(charset, isPredefined) { michael@0: if ((!isPredefined) && charset.length > 0) { michael@0: gCharsetMenu.SetCurrentComposerCharset(charset); michael@0: } michael@0: EditorSetDocumentCharacterSet(charset); michael@0: } michael@0: ); michael@0: } michael@0: michael@0: function MultiplexHandlerEx(aEvent, aSelectDetector, aSetForcedCharset) michael@0: { michael@0: try { michael@0: var node = aEvent.target; michael@0: var name = node.getAttribute('name'); michael@0: michael@0: if (name == 'detectorGroup') { michael@0: aSelectDetector(aEvent); michael@0: } else if (name == 'charsetGroup') { michael@0: var charset = node.getAttribute('id'); michael@0: charset = charset.substring('charset.'.length, charset.length) michael@0: aSetForcedCharset(charset, true); michael@0: } else if (name == 'charsetCustomize') { michael@0: //do nothing - please remove this else statement, once the charset prefs moves to the pref window michael@0: } else { michael@0: aSetForcedCharset(node.getAttribute('id'), false); michael@0: } michael@0: } catch(ex) { michael@0: alert(ex); michael@0: } michael@0: } michael@0: michael@0: function SelectDetector(event, doReload) michael@0: { michael@0: dump("Charset Detector menu item pressed: " + event.target.getAttribute('id') + "\n"); michael@0: michael@0: var uri = event.target.getAttribute("id"); michael@0: var prefvalue = uri.substring('chardet.'.length, uri.length); michael@0: if ("off" == prefvalue) { // "off" is special value to turn off the detectors michael@0: prefvalue = ""; michael@0: } michael@0: michael@0: try { michael@0: var pref = Components.classes["@mozilla.org/preferences-service;1"] michael@0: .getService(Components.interfaces.nsIPrefBranch); michael@0: var str = Components.classes["@mozilla.org/supports-string;1"] michael@0: .createInstance(Components.interfaces.nsISupportsString); michael@0: michael@0: str.data = prefvalue; michael@0: pref.setComplexValue("intl.charset.detector", michael@0: Components.interfaces.nsISupportsString, str); michael@0: if (typeof doReload == "function") doReload(); michael@0: } michael@0: catch (ex) { michael@0: dump("Failed to set the intl.charset.detector preference.\n"); michael@0: } michael@0: } michael@0: michael@0: var gPrevCharset = null; michael@0: function UpdateCurrentCharset() michael@0: { michael@0: // extract the charset from DOM michael@0: var wnd = document.commandDispatcher.focusedWindow; michael@0: if ((window == wnd) || (wnd == null)) wnd = window.content; michael@0: michael@0: // Uncheck previous item michael@0: if (gPrevCharset) { michael@0: var pref_item = document.getElementById('charset.' + gPrevCharset); michael@0: if (pref_item) michael@0: pref_item.setAttribute('checked', 'false'); michael@0: } michael@0: michael@0: var menuitem = document.getElementById('charset.' + wnd.document.characterSet); michael@0: if (menuitem) { michael@0: menuitem.setAttribute('checked', 'true'); michael@0: } michael@0: } michael@0: michael@0: function UpdateCurrentMailCharset() michael@0: { michael@0: var charset = msgWindow.mailCharacterSet; michael@0: var menuitem = document.getElementById('charset.' + charset); michael@0: michael@0: if (menuitem) { michael@0: menuitem.setAttribute('checked', 'true'); michael@0: } michael@0: } michael@0: michael@0: function UpdateCharsetDetector() michael@0: { michael@0: var prefvalue; michael@0: michael@0: try { michael@0: var pref = Components.classes["@mozilla.org/preferences-service;1"] michael@0: .getService(Components.interfaces.nsIPrefBranch); michael@0: prefvalue = pref.getComplexValue("intl.charset.detector", michael@0: Components.interfaces.nsIPrefLocalizedString).data; michael@0: } michael@0: catch (ex) { michael@0: prefvalue = ""; michael@0: } michael@0: michael@0: if (prefvalue == "") prefvalue = "off"; michael@0: dump("intl.charset.detector = "+ prefvalue + "\n"); michael@0: michael@0: prefvalue = 'chardet.' + prefvalue; michael@0: var menuitem = document.getElementById(prefvalue); michael@0: michael@0: if (menuitem) { michael@0: menuitem.setAttribute('checked', 'true'); michael@0: } michael@0: } michael@0: michael@0: function UpdateMenus(event) michael@0: { michael@0: // use setTimeout workaround to delay checkmark the menu michael@0: // when onmenucomplete is ready then use it instead of oncreate michael@0: // see bug 78290 for the detail michael@0: UpdateCurrentCharset(); michael@0: setTimeout(UpdateCurrentCharset, 0); michael@0: UpdateCharsetDetector(); michael@0: setTimeout(UpdateCharsetDetector, 0); michael@0: } michael@0: michael@0: function CreateMenu(node) michael@0: { michael@0: var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); michael@0: observerService.notifyObservers(null, "charsetmenu-selected", node); michael@0: } michael@0: michael@0: function UpdateMailMenus(event) michael@0: { michael@0: // use setTimeout workaround to delay checkmark the menu michael@0: // when onmenucomplete is ready then use it instead of oncreate michael@0: // see bug 78290 for the detail michael@0: UpdateCurrentMailCharset(); michael@0: setTimeout(UpdateCurrentMailCharset, 0); michael@0: UpdateCharsetDetector(); michael@0: setTimeout(UpdateCharsetDetector, 0); michael@0: } michael@0: michael@0: var gCharsetMenu = Components.classes['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService().QueryInterface(Components.interfaces.nsICurrentCharsetListener); michael@0: var gLastBrowserCharset = null; michael@0: michael@0: function charsetLoadListener (event) michael@0: { michael@0: var charset = window.content.document.characterSet; michael@0: michael@0: if (charset.length > 0 && (charset != gLastBrowserCharset)) { michael@0: gCharsetMenu.SetCurrentCharset(charset); michael@0: gPrevCharset = gLastBrowserCharset; michael@0: gLastBrowserCharset = charset; michael@0: } michael@0: } michael@0: michael@0: michael@0: function composercharsetLoadListener (event) michael@0: { michael@0: var charset = window.content.document.characterSet; michael@0: michael@0: michael@0: if (charset.length > 0 ) { michael@0: gCharsetMenu.SetCurrentComposerCharset(charset); michael@0: } michael@0: } michael@0: michael@0: function SetForcedEditorCharset(charset) michael@0: { michael@0: if (charset.length > 0 ) { michael@0: gCharsetMenu.SetCurrentComposerCharset(charset); michael@0: } michael@0: EditorSetDocumentCharacterSet(charset); michael@0: } michael@0: michael@0: michael@0: var gLastMailCharset = null; michael@0: michael@0: function mailCharsetLoadListener (event) michael@0: { michael@0: if (msgWindow) { michael@0: var charset = msgWindow.mailCharacterSet; michael@0: if (charset.length > 0 && (charset != gLastMailCharset)) { michael@0: gCharsetMenu.SetCurrentMailCharset(charset); michael@0: gLastMailCharset = charset; michael@0: } michael@0: } michael@0: } michael@0: michael@0: function InitCharsetMenu() michael@0: { michael@0: removeEventListener("load", InitCharsetMenu, true); michael@0: michael@0: var wintype = document.documentElement.getAttribute('windowtype'); michael@0: if (window && (wintype == "navigator:browser")) michael@0: { michael@0: var contentArea = window.document.getElementById("appcontent"); michael@0: if (contentArea) michael@0: contentArea.addEventListener("pageshow", charsetLoadListener, true); michael@0: } michael@0: else michael@0: { michael@0: var arrayOfStrings = wintype.split(":"); michael@0: if (window && arrayOfStrings[0] == "mail") michael@0: { michael@0: var messageContent = window.document.getElementById("messagepane"); michael@0: if (messageContent) michael@0: messageContent.addEventListener("pageshow", mailCharsetLoadListener, true); michael@0: } michael@0: else michael@0: if (window && arrayOfStrings[0] == "composer") michael@0: { michael@0: contentArea = window.document.getElementById("appcontent"); michael@0: if (contentArea) michael@0: contentArea.addEventListener("pageshow", composercharsetLoadListener, true); michael@0: } michael@0: } michael@0: } michael@0: michael@0: addEventListener("load", InitCharsetMenu, true);