home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / comm.jar / content / communicator / utilityOverlay.js < prev    next >
Text File  |  2004-01-07  |  19KB  |  567 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Alec Flett <alecf@netscape.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /**
  40.  * Communicator Shared Utility Library
  41.  * for shared application glue for the Communicator suite of applications
  42.  **/
  43.  
  44. /*
  45.   Note: All Editor/Composer-related methods have been moved to editorApplicationOverlay.js,
  46.   so app windows that require those must include editorNavigatorOverlay.xul
  47. */
  48.  
  49. /**
  50.  * Go into online/offline mode
  51.  **/
  52.  
  53. const kIOServiceProgID = "@mozilla.org/network/io-service;1";
  54. const kObserverServiceProgID = "@mozilla.org/observer-service;1";
  55. const kProxyManual = ["network.proxy.ftp",
  56.                       "network.proxy.gopher",
  57.                       "network.proxy.http",
  58.                       "network.proxy.socks",
  59.                       "network.proxy.ssl"];
  60.  
  61. function toggleOfflineStatus()
  62. {
  63.   var checkfunc;
  64.   try {
  65.     checkfunc = document.getElementById("offline-status").getAttribute('checkfunc');
  66.   }
  67.   catch (ex) {
  68.     checkfunc = null;
  69.   }
  70.  
  71.   var ioService = Components.classes[kIOServiceProgID]
  72.                             .getService(Components.interfaces.nsIIOService);
  73.   if (checkfunc) {
  74.     if (!eval(checkfunc)) {
  75.       // the pre-offline check function returned false, so don't go offline
  76.       return;
  77.     }
  78.   }
  79.   ioService.offline = !ioService.offline;
  80. }
  81.  
  82. function setNetworkStatus(networkProxyType)
  83. {
  84.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  85.   prefService = prefService.getService(Components.interfaces.nsIPrefService);
  86.   var prefBranch = prefService.getBranch(null);
  87.   try {
  88.     prefBranch.setIntPref("network.proxy.type", networkProxyType);
  89.   }
  90.   catch (ex) {}
  91. }
  92.  
  93. function InitProxyMenu()
  94. {
  95.   var networkProxyNo = document.getElementById("network-proxy-no");
  96.   var networkProxyManual = document.getElementById("network-proxy-manual");
  97.   var networkProxyPac = document.getElementById("network-proxy-pac");
  98.   if (!networkProxyNo || !networkProxyManual || !networkProxyPac)
  99.     return;
  100.  
  101.   var networkProxyStatus = [networkProxyNo, networkProxyManual, networkProxyPac];
  102.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  103.   prefService = prefService.getService(Components.interfaces.nsIPrefService);
  104.   var prefBranch = prefService.getBranch(null);
  105.  
  106.   var proxyLocked = prefBranch.prefIsLocked("network.proxy.type");
  107.   if (proxyLocked) {
  108.         networkProxyNo.setAttribute("disabled", "true");
  109.   }
  110.   else {
  111.         networkProxyNo.removeAttribute("disabled");
  112.   }
  113.  
  114.   // If no proxy is configured, disable the menuitems.
  115.   // Checking for proxy manual settings.
  116.   var proxyManuallyConfigured = false;
  117.   for (var i = 0; i < kProxyManual.length; i++) {
  118.     if (GetStringPref(kProxyManual[i]) != "") {
  119.       proxyManuallyConfigured = true;
  120.       break;
  121.     }
  122.   }
  123.  
  124.   if (proxyManuallyConfigured && !proxyLocked) {
  125.     networkProxyManual.removeAttribute("disabled");
  126.   }
  127.   else {
  128.     networkProxyManual.setAttribute("disabled", "true");
  129.   }
  130.  
  131.   //Checking for proxy PAC settings.
  132.   var proxyAutoConfigured = false;
  133.   if (GetStringPref("network.proxy.autoconfig_url") != "")
  134.     proxyAutoConfigured = true;
  135.  
  136.   if (proxyAutoConfigured && !proxyLocked) {
  137.     networkProxyPac.removeAttribute("disabled");
  138.   }
  139.   else {
  140.     networkProxyPac.setAttribute("disabled", "true");
  141.   }
  142.  
  143.   var networkProxyType;
  144.   try {
  145.     networkProxyType = prefBranch.getIntPref("network.proxy.type");
  146.   } catch(e) {}
  147.  
  148.   networkProxyStatus[networkProxyType].setAttribute("checked", "true");
  149. }
  150.  
  151. function setProxyTypeUI()
  152. {
  153.   var panel = document.getElementById("offline-status");
  154.   if (!panel)
  155.     return;
  156.  
  157.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  158.   prefService = prefService.getService(Components.interfaces.nsIPrefService);
  159.   var prefBranch = prefService.getBranch(null);
  160.  
  161.   try {
  162.     var networkProxyType = prefBranch.getIntPref("network.proxy.type");
  163.   } catch(e) {}
  164.  
  165.   var onlineTooltip = "onlineTooltip" + networkProxyType;
  166.   var bundle = srGetStrBundle("chrome://communicator/locale/utilityOverlay.properties");
  167.   panel.setAttribute("tooltiptext", bundle.GetStringFromName(onlineTooltip));
  168. }
  169.  
  170. function GetStringPref(name)
  171. {
  172.   try {
  173.     return pref.getComplexValue(name, Components.interfaces.nsISupportsString).data;
  174.   } catch (e) {}
  175.   return "";
  176. }
  177.  
  178. function setOfflineUI(offline)
  179. {
  180.   var broadcaster = document.getElementById("Communicator:WorkMode");
  181.   var panel = document.getElementById("offline-status");
  182.   if (!broadcaster || !panel) return;
  183.  
  184.   //Checking for a preference "network.online", if it's locked, disabling 
  185.   // network icon and menu item
  186.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  187.   prefService = prefService.getService(Components.interfaces.nsIPrefService);
  188.   var prefBranch = prefService.getBranch(null);
  189.   
  190.   var offlineLocked = prefBranch.prefIsLocked("network.online"); 
  191.   
  192.   if (offlineLocked ) {
  193.       broadcaster.setAttribute("disabled","true");
  194.   }
  195.  
  196.   var bundle = srGetStrBundle("chrome://communicator/locale/utilityOverlay.properties");
  197.  
  198.   if (offline)
  199.     {
  200.       broadcaster.setAttribute("offline", "true");
  201.       broadcaster.setAttribute("checked", "true");
  202.       panel.removeAttribute("context");
  203.       panel.setAttribute("tooltiptext", bundle.GetStringFromName("offlineTooltip"));
  204.     }
  205.   else
  206.     {
  207.       broadcaster.removeAttribute("offline");
  208.       broadcaster.removeAttribute("checked");
  209.       panel.setAttribute("context", "networkProperties");
  210.       try {
  211.         var networkProxyType = prefBranch.getIntPref("network.proxy.type");
  212.       } catch(e) {}
  213.       var onlineTooltip = "onlineTooltip" + networkProxyType;
  214.       panel.setAttribute("tooltiptext", bundle.GetStringFromName(onlineTooltip));
  215.     }
  216. }
  217.  
  218. var goPrefWindow = 0;
  219.  
  220. function getBrowserURL() {
  221.  
  222.   try {
  223.     var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  224.                          .getService(Components.interfaces.nsIPrefBranch);
  225.     var url = prefs.getCharPref("browser.chromeURL");
  226.     if (url)
  227.       return url;
  228.   } catch(e) {
  229.   }
  230.   return "chrome://navigator/content/navigator.xul";
  231. }
  232.  
  233. function goPreferences(containerID, paneURL, itemID)
  234. {
  235.   var resizable;
  236.   var pref = Components.classes["@mozilla.org/preferences-service;1"]
  237.                        .getService(Components.interfaces.nsIPrefBranch);
  238.   try {
  239.     // We are resizable ONLY if in box debugging mode, because in
  240.     // this special debug mode it is often impossible to see the 
  241.     // content of the debug panel in order to disable debug mode.
  242.     resizable = pref.getBoolPref("xul.debug.box");
  243.   }
  244.   catch (e) {
  245.     resizable = false;
  246.   }
  247.  
  248.   //check for an existing pref window and focus it; it's not application modal
  249.   const kWindowMediatorContractID = "@mozilla.org/appshell/window-mediator;1";
  250.   const kWindowMediatorIID = Components.interfaces.nsIWindowMediator;
  251.   const kWindowMediator = Components.classes[kWindowMediatorContractID].getService(kWindowMediatorIID);
  252.   var lastPrefWindow = kWindowMediator.getMostRecentWindow("mozilla:preferences");
  253.   if (lastPrefWindow)
  254.     lastPrefWindow.focus();
  255.   else {
  256.     var resizability = resizable ? "yes" : "no";
  257.     var features = "chrome,titlebar,resizable=" + resizability;
  258.     openDialog("chrome://communicator/content/pref/pref.xul","PrefWindow", 
  259.                features, paneURL, containerID, itemID);
  260.   }
  261. }
  262.  
  263. function goToggleToolbar( id, elementID )
  264. {
  265.   var toolbar = document.getElementById( id );
  266.   var element = document.getElementById( elementID );
  267.   if ( toolbar )
  268.   {
  269.     var attribValue = toolbar.getAttribute("hidden") ;
  270.  
  271.     if ( attribValue == "true" )
  272.     {
  273.       toolbar.setAttribute("hidden", "false" );
  274.       if ( element )
  275.         element.setAttribute("checked","true")
  276.     }
  277.     else
  278.     {
  279.       toolbar.setAttribute("hidden", true );
  280.       if ( element )
  281.         element.setAttribute("checked","false")
  282.     }
  283.     document.persist(id, 'hidden');
  284.     document.persist(elementID, 'checked');
  285.   }
  286. }
  287.  
  288.  
  289. function goClickThrobber( urlPref )
  290. {
  291.   var url;
  292.   try {
  293.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  294.                          .getService(Components.interfaces.nsIPrefBranch);
  295.     url = pref.getComplexValue(urlPref, Components.interfaces.nsIPrefLocalizedString).data;
  296.   }
  297.  
  298.   catch(e) {
  299.     url = null;
  300.   }
  301.  
  302.   if ( url )
  303.     openTopWin(url);
  304. }
  305.  
  306.  
  307. //No longer needed.  Rip this out since we are using openTopWin
  308. function goHelpMenu( url )
  309. {
  310.   /* note that this chrome url should probably change to not have all of the navigator controls */
  311.   /* also, do we want to limit the number of help windows that can be spawned? */
  312.   window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  313. }
  314.  
  315. function getTopWin()
  316. {
  317.     var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  318.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  319.     var topWindowOfType = windowManagerInterface.getMostRecentWindow( "navigator:browser" );
  320.  
  321.     if (topWindowOfType) {
  322.         return topWindowOfType;
  323.     }
  324.     return null;
  325. }
  326.  
  327. function openTopWin( url )
  328. {
  329.     /* note that this chrome url should probably change to not have
  330.        all of the navigator controls, but if we do this we need to have
  331.        the option for chrome controls because goClickThrobber() needs to
  332.        use this function with chrome controls */
  333.     /* also, do we want to
  334.        limit the number of help windows that can be spawned? */
  335.     if ((url == null) || (url == "")) return null;
  336.  
  337.     // xlate the URL if necessary
  338.     if (url.indexOf("urn:") == 0)
  339.     {
  340.         url = xlateURL(url);        // does RDF urn expansion
  341.     }
  342.  
  343.     // avoid loading "", since this loads a directory listing
  344.     if (url == "") {
  345.         url = "about:blank";
  346.     }
  347.  
  348.     var topWindowOfType = getTopWin();
  349.     if ( topWindowOfType )
  350.     {
  351.         topWindowOfType.focus();
  352.         topWindowOfType.loadURI(url);
  353.         return topWindowOfType;
  354.     }
  355.     return window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  356. }
  357.  
  358. function goAboutDialog()
  359. {
  360.   var defaultAboutState = false;
  361.   try {
  362.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  363.                          .getService(Components.interfaces.nsIPrefBranch);
  364.     defaultAboutState = pref.getBoolPref("browser.show_about_as_stupid_modal_window");
  365.   }
  366.   catch(e) {
  367.     defaultAboutState = false;
  368.   }
  369.   if( defaultAboutState )
  370.     window.openDialog("chrome://global/content/about.xul", "About", "modal,chrome,resizable=yes,height=450,width=550");
  371.   else
  372.     window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", 'about:' );
  373. }
  374.  
  375. // update menu items that rely on focus
  376. function goUpdateGlobalEditMenuItems()
  377. {
  378.   goUpdateCommand('cmd_undo');
  379.   goUpdateCommand('cmd_redo');
  380.   goUpdateCommand('cmd_cut');
  381.   goUpdateCommand('cmd_copy');
  382.   goUpdateCommand('cmd_paste');
  383.   goUpdateCommand('cmd_selectAll');
  384.   goUpdateCommand('cmd_delete');
  385. }
  386.  
  387. // update menu items that rely on the current selection
  388. function goUpdateSelectEditMenuItems()
  389. {
  390.   goUpdateCommand('cmd_cut');
  391.   goUpdateCommand('cmd_copy');
  392.   goUpdateCommand('cmd_delete');
  393.   goUpdateCommand('cmd_selectAll');
  394. }
  395.  
  396. // update menu items that relate to undo/redo
  397. function goUpdateUndoEditMenuItems()
  398. {
  399.   goUpdateCommand('cmd_undo');
  400.   goUpdateCommand('cmd_redo');
  401. }
  402.  
  403. // update menu items that depend on clipboard contents
  404. function goUpdatePasteMenuItems()
  405. {
  406.   goUpdateCommand('cmd_paste');
  407. }
  408.  
  409. // update Find As You Type menu items, they rely on focus
  410. function goUpdateFindTypeMenuItems()
  411. {
  412.   goUpdateCommand('cmd_findTypeText');
  413.   goUpdateCommand('cmd_findTypeLinks');
  414. }
  415.  
  416. // function that extracts the filename from a url
  417. function extractFileNameFromUrl(urlstr)
  418. {
  419.   if (!urlstr) return null;
  420.  
  421.   // For "http://foo/bar/cheese.jpg", return "cheese.jpg".
  422.   // For "imap://user@host.com:143/fetch>UID>/INBOX>951?part=1.2&type=image/gif&filename=foo.jpeg", return "foo.jpeg".
  423.   // The 2nd url (ie, "imap://...") is generated for inline images by MimeInlineImage_parse_begin() in mimeiimg.cpp.
  424.   var lastSlash = urlstr.slice(urlstr.lastIndexOf( "/" )+1);
  425.   if (lastSlash)
  426.   { 
  427.     var nameIndex = lastSlash.lastIndexOf( "filename=" );
  428.     if (nameIndex != -1)
  429.       return (lastSlash.slice(nameIndex+9));
  430.     else
  431.       return lastSlash;
  432.   }
  433.   return null; 
  434. }
  435.  
  436. // Gather all descendent text under given document node.
  437. function gatherTextUnder ( root ) 
  438. {
  439.   var text = "";
  440.   var node = root.firstChild;
  441.   var depth = 1;
  442.   while ( node && depth > 0 ) {
  443.     // See if this node is text.
  444.     if ( node.nodeName == "#text" ) {
  445.       // Add this text to our collection.
  446.       text += " " + node.data;
  447.     } else if ( node.nodeType == Node.ELEMENT_NODE 
  448.                 && node.localName.toUpperCase() == "IMG" ) {
  449.       // If it has an alt= attribute, use that.
  450.       var altText = node.getAttribute( "alt" );
  451.       if ( altText && altText != "" ) {
  452.         text = altText;
  453.         break;
  454.       }
  455.     }
  456.     // Find next node to test.
  457.     // First, see if this node has children.
  458.     if ( node.hasChildNodes() ) {
  459.       // Go to first child.
  460.       node = node.firstChild;
  461.       depth++;
  462.     } else {
  463.       // No children, try next sibling.
  464.       if ( node.nextSibling ) {
  465.         node = node.nextSibling;
  466.       } else {
  467.         // Last resort is our next oldest uncle/aunt.
  468.         node = node.parentNode.nextSibling;
  469.         depth--;
  470.       }
  471.     }
  472.   }
  473.   // Strip leading whitespace.
  474.   text = text.replace( /^\s+/, "" );
  475.   // Strip trailing whitespace.
  476.   text = text.replace( /\s+$/, "" );
  477.   // Compress remaining whitespace.
  478.   text = text.replace( /\s+/g, " " );
  479.   return text;
  480. }
  481.  
  482. var offlineObserver = {
  483.   observe: function(subject, topic, state) {
  484.     // sanity checks
  485.     if (topic != "network:offline-status-changed") return;
  486.     setOfflineUI(state == "offline");
  487.   }
  488. }
  489.  
  490. var proxyTypeObserver = {
  491.   observe: function(subject, topic, state) {
  492.     // sanity checks
  493.     var ioService = Components.classes[kIOServiceProgID]
  494.                               .getService(Components.interfaces.nsIIOService);
  495.     if (state == "network.proxy.type" && !ioService.offline)
  496.       setProxyTypeUI();
  497.   }
  498. }
  499.  
  500. function utilityOnLoad(aEvent)
  501. {
  502.   var broadcaster = document.getElementById("Communicator:WorkMode");
  503.   if (!broadcaster) return;
  504.  
  505.   var observerService = Components.classes[kObserverServiceProgID]
  506.                   .getService(Components.interfaces.nsIObserverService);
  507.   observerService.addObserver(offlineObserver, "network:offline-status-changed", false);
  508.   // make sure we remove this observer later
  509.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  510.   prefService = prefService.getService(Components.interfaces.nsIPrefService);
  511.   var prefBranch = prefService.getBranch(null);
  512.   prefBranch = prefBranch.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  513.  
  514.   prefBranch.addObserver("network.proxy.type", proxyTypeObserver, false);
  515.  
  516.   addEventListener("unload", utilityOnUnload, false);
  517.  
  518.   // set the initial state
  519.   var ioService = Components.classes[kIOServiceProgID]
  520.               .getService(Components.interfaces.nsIIOService);
  521.   setOfflineUI(ioService.offline);
  522. }
  523.  
  524. function utilityOnUnload(aEvent) 
  525. {
  526.   var observerService = Components.classes[kObserverServiceProgID]
  527.               .getService(Components.interfaces.nsIObserverService);
  528.   observerService.removeObserver(offlineObserver, "network:offline-status-changed");
  529.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  530.   prefService = prefService.getService(Components.interfaces.nsIPrefService);
  531.   var prefBranch = prefService.getBranch(null);
  532.   prefBranch = prefBranch.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
  533.   
  534.   prefBranch.removeObserver("network.proxy.type", proxyTypeObserver);
  535. }
  536.  
  537. addEventListener("load", utilityOnLoad, false);
  538.  
  539. function GenerateValidFilename(filename, extension)
  540. {
  541.   if (filename) // we have a title; let's see if it's usable
  542.   {
  543.     // clean up the filename to make it usable and
  544.     // then trim whitespace from beginning and end
  545.     filename = validateFileName(filename).replace(/^\s+|\s+$/g, "");
  546.     if (filename.length > 0)
  547.       return filename + extension;
  548.   }
  549.   return null;
  550. }
  551.  
  552. function validateFileName(aFileName)
  553. {
  554.   var re = /[\/]+/g;
  555.   if (navigator.appVersion.indexOf("Windows") != -1) {
  556.     re = /[\\\/\|]+/g;
  557.     aFileName = aFileName.replace(/[\"]+/g, "'");
  558.     aFileName = aFileName.replace(/[\*\:\?]+/g, " ");
  559.     aFileName = aFileName.replace(/[\<]+/g, "(");
  560.     aFileName = aFileName.replace(/[\>]+/g, ")");
  561.   }
  562.   else if (navigator.appVersion.indexOf("Macintosh") != -1)
  563.     re = /[\:\/]+/g;
  564.   
  565.   return aFileName.replace(re, "_");
  566. }
  567.