home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / toolkit.jar / content / global / charsetOverlay.js < prev    next >
Text File  |  2004-03-01  |  10KB  |  306 lines

  1. function MultiplexHandler(event)
  2. {
  3.     var node = event.target;
  4.     var name = node.getAttribute('name');
  5.  
  6.     if (name == 'detectorGroup') {
  7.         SetForcedDetector(true);
  8.         SelectDetector(event, false);
  9.     } else if (name == 'charsetGroup') {
  10.         var charset = node.getAttribute('id');
  11.         charset = charset.substring('charset.'.length, charset.length)
  12.         SetForcedCharset(charset);
  13.         SetDefaultCharacterSet(charset);
  14.     } else if (name == 'charsetCustomize') {
  15.         // please remove this else statement, once the charset prefs moves to the pref window
  16.         window.openDialog("chrome://communicator/content/pref/pref-charset.xul",
  17.                           "_blank", "chrome,modal,resizable", "browser");
  18.     } else {
  19.         SetForcedCharset(node.getAttribute('id'));
  20.         SetDefaultCharacterSet(node.getAttribute('id'));
  21.     }
  22. }
  23.  
  24. function MailMultiplexHandler(event)
  25. {
  26.     var node = event.target;
  27.     var name = node.getAttribute('name');
  28.  
  29.     if (name == 'detectorGroup') {
  30.         SelectDetector(event, true);
  31.     } else if (name == 'charsetGroup') {
  32.         var charset = node.getAttribute('id');
  33.         charset = charset.substring('charset.'.length, charset.length)
  34.         MessengerSetDefaultCharacterSet(charset);
  35.     } else if (name == 'charsetCustomize') {
  36.         // please remove this else statement, once the charset prefs moves to the pref window
  37.         window.openDialog("chrome://communicator/content/pref/pref-charset.xul",
  38.                           "_blank", "chrome,modal,resizable", "browser");
  39.     } else {
  40.         MessengerSetDefaultCharacterSet(node.getAttribute('id'));
  41.     }
  42. }
  43.  
  44. function ComposerMultiplexHandler(event)
  45. {
  46.     var node = event.target;
  47.     var name = node.getAttribute('name');
  48.  
  49.     if (name == 'detectorGroup') {
  50.         ComposerSelectDetector(event, true);
  51.     } else if (name == 'charsetGroup') {
  52.         var charset = node.getAttribute('id');
  53.         charset = charset.substring('charset.'.length, charset.length)
  54.         EditorSetDocumentCharacterSet(charset);
  55.     } else if (name == 'charsetCustomize') {
  56.         // please remove this else statement, once the charset prefs moves to the pref window
  57.         window.openDialog("chrome://communicator/content/pref/pref-charset.xul",
  58.                           "_blank", "chrome,modal,resizable", "browser");
  59.     } else {
  60.         SetForcedEditorCharset(node.getAttribute('id'));
  61.     }
  62. }
  63.  
  64. function MaileditMultiplexHandler(event)
  65. {
  66.     var node = event.target;
  67.     var name = node.getAttribute('name');
  68.  
  69.     if (name == 'charsetCustomize') {
  70.         // please remove this else statement, once the charset prefs moves to the pref window
  71.         window.openDialog("chrome://communicator/content/pref/pref-charset.xul",
  72.                           "_blank", "chrome,modal,resizable", "mailedit");
  73.     } else {
  74.         SetDocumentCharacterSet(node.id);
  75.     }
  76. }
  77.  
  78. function SetDefaultCharacterSet(charset)
  79. {
  80.     dump("Charset Overlay menu item pressed: " + charset + "\n");
  81.     BrowserSetDefaultCharacterSet(charset);
  82. }
  83.  
  84. function SelectDetector(event, doReload)
  85. {
  86.     dump("Charset Detector menu item pressed: " + event.target.getAttribute('id') + "\n");
  87.  
  88.     var uri =  event.target.getAttribute("id");
  89.     var prefvalue = uri.substring('chardet.'.length, uri.length);
  90.     if ("off" == prefvalue) { // "off" is special value to turn off the detectors
  91.         prefvalue = "";
  92.     }
  93.  
  94.     try {
  95.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  96.                              .getService(Components.interfaces.nsIPrefBranch);
  97.         var str =  Components.classes["@mozilla.org/supports-string;1"]
  98.                              .createInstance(Components.interfaces.nsISupportsString);
  99.  
  100.         str.data = prefvalue;
  101.         pref.setComplexValue("intl.charset.detector",
  102.                              Components.interfaces.nsISupportsString, str);
  103.         if (doReload) window._content.location.reload();
  104.     }
  105.     catch (ex) {
  106.         dump("Failed to set the intl.charset.detector preference.\n");
  107.     }
  108. }
  109.  
  110. function ComposerSelectDetector(event)
  111. {
  112.     //dump("Charset Detector menu item pressed: " + event.target.getAttribute('id') + "\n");
  113.  
  114.     var uri =  event.target.getAttribute("id");
  115.     var prefvalue = uri.substring('chardet.'.length, uri.length);
  116.     if ("off" == prefvalue) { // "off" is special value to turn off the detectors
  117.         prefvalue = "";
  118.     }
  119.  
  120.     try {
  121.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  122.                              .getService(Components.interfaces.nsIPrefBranch);
  123.         var str =  Components.classes["@mozilla.org/supports-string;1"]
  124.                              .createInstance(Components.interfaces.nsISupportsString);
  125.  
  126.         str.data = prefvalue;
  127.         pref.setComplexValue("intl.charset.detector",
  128.                              Components.interfaces.nsISupportsString, str);
  129.         EditorLoadUrl(GetDocumentUrl());    
  130.     }
  131.     catch (ex) {
  132.         dump("Failed to set the intl.charset.detector preference.\n");
  133.     }
  134. }
  135.  
  136. function SetForcedDetector(doReload)
  137. {
  138.     BrowserSetForcedDetector(doReload);
  139. }
  140.  
  141. function SetForcedCharset(charset)
  142. {
  143.     BrowserSetForcedCharacterSet(charset);
  144. }
  145.  
  146. var gPrevCharset = null;
  147. function UpdateCurrentCharset()
  148. {
  149.     var menuitem = null;
  150.  
  151.     // exctract the charset from DOM
  152.     var wnd = document.commandDispatcher.focusedWindow;
  153.     if ((window == wnd) || (wnd == null)) wnd = window._content;
  154.     menuitem = document.getElementById('charset.' + wnd.document.characterSet);
  155.  
  156.     if (menuitem) {
  157.         // uncheck previously checked item to workaround Mac checkmark problem
  158.         // bug 98625
  159.         if (gPrevCharset) {
  160.             var pref_item = document.getElementById('charset.' + gPrevCharset);
  161.             if (pref_item)
  162.               pref_item.setAttribute('checked', 'false');
  163.         }
  164.         menuitem.setAttribute('checked', 'true');
  165.     }
  166. }
  167.  
  168. function UpdateCurrentMailCharset()
  169. {
  170.     var charset = msgWindow.mailCharacterSet;
  171.     dump("Update current mail charset: " + charset + " \n");
  172.  
  173.     var menuitem = document.getElementById('charset.' + charset);
  174.  
  175.     if (menuitem) {
  176.         menuitem.setAttribute('checked', 'true');
  177.     }
  178. }
  179.  
  180. function UpdateCharsetDetector()
  181. {
  182.     var prefvalue;
  183.  
  184.     try {
  185.         var pref = Components.classes["@mozilla.org/preferences-service;1"]
  186.                              .getService(Components.interfaces.nsIPrefBranch);
  187.         prefvalue = pref.getComplexValue("intl.charset.detector",
  188.                                          Components.interfaces.nsIPrefLocalizedString).data;
  189.     }
  190.     catch (ex) {
  191.         prefvalue = "";
  192.     }
  193.  
  194.     if (prefvalue == "") prefvalue = "off";
  195.     dump("intl.charset.detector = "+ prefvalue + "\n");
  196.  
  197.     prefvalue = 'chardet.' + prefvalue;
  198.     var menuitem = document.getElementById(prefvalue);
  199.  
  200.     if (menuitem) {
  201.         menuitem.setAttribute('checked', 'true');
  202.     }
  203. }
  204.  
  205. function UpdateMenus(event)
  206. {
  207.     // use setTimeout workaround to delay checkmark the menu
  208.     // when onmenucomplete is ready then use it instead of oncreate
  209.     // see bug 78290 for the detail
  210.     UpdateCurrentCharset();
  211.     setTimeout("UpdateCurrentCharset()", 0);
  212.     UpdateCharsetDetector();
  213.     setTimeout("UpdateCharsetDetector()", 0);
  214. }
  215.  
  216. function CreateMenu(node)
  217. {
  218.   var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  219.   observerService.notifyObservers(null, "charsetmenu-selected", node);
  220. }
  221.  
  222. function UpdateMailMenus(event)
  223. {
  224.     // use setTimeout workaround to delay checkmark the menu
  225.     // when onmenucomplete is ready then use it instead of oncreate
  226.     // see bug 78290 for the detail
  227.     UpdateCurrentMailCharset();
  228.     setTimeout("UpdateCurrentMailCharset()", 0);
  229.     UpdateCharsetDetector();
  230.     setTimeout("UpdateCharsetDetector()", 0);
  231. }
  232.  
  233. var gCharsetMenu = Components.classes['@mozilla.org/rdf/datasource;1?name=charset-menu'].getService().QueryInterface(Components.interfaces.nsICurrentCharsetListener);
  234. var gLastBrowserCharset = null;
  235.  
  236. function charsetLoadListener (event)
  237. {
  238.     var charset = window._content.document.characterSet;
  239.  
  240.     if (charset.length > 0 && (charset != gLastBrowserCharset)) {
  241.         gCharsetMenu.SetCurrentCharset(charset);
  242.         gPrevCharset = gLastBrowserCharset;
  243.         gLastBrowserCharset = charset;
  244.     }
  245. }
  246.  
  247.  
  248. function composercharsetLoadListener (event)
  249. {
  250.     var charset = window._content.document.characterSet;
  251.  
  252.  
  253.     if (charset.length > 0 ) {
  254.        gCharsetMenu.SetCurrentComposerCharset(charset);
  255.     }
  256.  }
  257.  
  258. function SetForcedEditorCharset(charset)
  259. {
  260.     if (charset.length > 0 ) {
  261.        gCharsetMenu.SetCurrentComposerCharset(charset);
  262.     }
  263.     EditorSetDocumentCharacterSet(charset);
  264. }
  265.  
  266.  
  267. var gLastMailCharset = null;
  268.  
  269. function mailCharsetLoadListener (event)
  270. {
  271.     if (msgWindow) {
  272.         var charset = msgWindow.mailCharacterSet;
  273.         if (charset.length > 0 && (charset != gLastMailCharset)) {
  274.             gCharsetMenu.SetCurrentMailCharset(charset);
  275.             gLastMailCharset = charset;
  276.             dump("mailCharsetLoadListener: " + charset + " \n");
  277.         }
  278.     }
  279. }
  280.  
  281. var wintype = document.firstChild.getAttribute('windowtype');
  282. if (window && (wintype == "navigator:browser"))
  283. {
  284.     var contentArea = window.document.getElementById("appcontent");
  285.     if (contentArea)
  286.         contentArea.addEventListener("load", charsetLoadListener, true);
  287. }
  288. else
  289. {
  290.     var arrayOfStrings = wintype.split(":");
  291.     if (window && arrayOfStrings[0] == "mail") 
  292.     {
  293.         var messageContent = window.document.getElementById("messagepane");
  294.         if (messageContent)
  295.             messageContent.addEventListener("load", mailCharsetLoadListener, true);
  296.     }
  297.     else
  298.     if (window && arrayOfStrings[0] == "composer") 
  299.     {
  300.         contentArea = window.document.getElementById("appcontent");
  301.         if (contentArea)
  302.             contentArea.addEventListener("load", composercharsetLoadListener, true);
  303.     }
  304.  
  305. }
  306.