home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / utilityOverlay.js < prev    next >
Text File  |  2003-06-08  |  13KB  |  413 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.  
  56. function toggleOfflineStatus()
  57. {
  58.   var checkfunc;
  59.   try {
  60.     checkfunc = document.getElementById("offline-status").getAttribute('checkfunc');
  61.   }
  62.   catch (ex) {
  63.     checkfunc = null;
  64.   }
  65.  
  66.   var ioService = Components.classes[kIOServiceProgID]
  67.                             .getService(Components.interfaces.nsIIOService);
  68.   if (checkfunc) {
  69.     if (!eval(checkfunc)) {
  70.       // the pre-offline check function returned false, so don't go offline
  71.       return;
  72.     }
  73.   }
  74.   ioService.offline = !ioService.offline;
  75. }
  76.  
  77. function setOfflineUI(offline)
  78. {
  79.   var broadcaster = document.getElementById("Communicator:WorkMode");
  80.   var panel = document.getElementById("offline-status");
  81.   var menuItem = document.getElementById("offlineGoOfflineCmd");
  82.   if (!broadcaster || !panel) return;
  83.  
  84.   //Checking for a preference "network.online", if it's locked, disabling 
  85.   // network icon and menu item
  86.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  87.   prefService = prefService.getService();
  88.   prefService = prefService.QueryInterface(Components.interfaces.nsIPrefService);
  89.   
  90.   var prefBranch = prefService.getBranch(null);
  91.   
  92.   var offlineLocked = prefBranch.prefIsLocked("network.online"); 
  93.   
  94.   if (offlineLocked ) {
  95.       broadcaster.setAttribute("disabled","true");
  96.   }
  97.  
  98.   var bundle = srGetStrBundle("chrome://communicator/locale/utilityOverlay.properties");
  99.  
  100.   if (offline)
  101.     {
  102.       broadcaster.setAttribute("offline", "true");
  103.       panel.setAttribute("tooltiptext", bundle.GetStringFromName("offlineTooltip"));
  104.       if (menuItem)
  105.         menuItem.setAttribute("checked", "true");
  106.     }
  107.   else
  108.     {
  109.       broadcaster.removeAttribute("offline");
  110.       panel.setAttribute("tooltiptext", bundle.GetStringFromName("onlineTooltip"));
  111.       if (menuItem)
  112.         broadcaster.removeAttribute("checked");
  113.     }
  114. }
  115.  
  116. var goPrefWindow = 0;
  117.  
  118. function getBrowserURL() {
  119.  
  120.   try {
  121.     var prefs = Components.classes["@mozilla.org/preferences-service;1"]
  122.                          .getService(Components.interfaces.nsIPrefBranch);
  123.     var url = prefs.getCharPref("browser.chromeURL");
  124.     if (url)
  125.       return url;
  126.   } catch(e) {
  127.   }
  128.   return "chrome://navigator/content/navigator.xul";
  129. }
  130.  
  131. function goPreferences(containerID, paneURL, itemID)
  132. {
  133.   var resizable;
  134.   var pref = Components.classes["@mozilla.org/preferences-service;1"]
  135.                        .getService(Components.interfaces.nsIPrefBranch);
  136.   try {
  137.     // We are resizable ONLY if in box debugging mode, because in
  138.     // this special debug mode it is often impossible to see the 
  139.     // content of the debug panel in order to disable debug mode.
  140.     resizable = pref.getBoolPref("xul.debug.box");
  141.   }
  142.   catch (e) {
  143.     resizable = false;
  144.   }
  145.  
  146.   //check for an existing pref window and focus it; it's not application modal
  147.   const kWindowMediatorContractID = "@mozilla.org/appshell/window-mediator;1";
  148.   const kWindowMediatorIID = Components.interfaces.nsIWindowMediator;
  149.   const kWindowMediator = Components.classes[kWindowMediatorContractID].getService(kWindowMediatorIID);
  150.   var lastPrefWindow = kWindowMediator.getMostRecentWindow("mozilla:preferences");
  151.   if (lastPrefWindow)
  152.     lastPrefWindow.focus();
  153.   else {
  154.     var resizability = resizable ? "yes" : "no";
  155.     var features = "chrome,titlebar,resizable=" + resizability;
  156.     openDialog("chrome://communicator/content/pref/pref.xul","PrefWindow", 
  157.                features, paneURL, containerID, itemID);
  158.   }
  159. }
  160.  
  161. function goToggleToolbar( id, elementID )
  162. {
  163.   var toolbar = document.getElementById( id );
  164.   var element = document.getElementById( elementID );
  165.   if ( toolbar )
  166.   {
  167.     var attribValue = toolbar.getAttribute("hidden") ;
  168.  
  169.     if ( attribValue == "true" )
  170.     {
  171.       toolbar.setAttribute("hidden", "false" );
  172.       if ( element )
  173.         element.setAttribute("checked","true")
  174.     }
  175.     else
  176.     {
  177.       toolbar.setAttribute("hidden", true );
  178.       if ( element )
  179.         element.setAttribute("checked","false")
  180.     }
  181.     document.persist(id, 'hidden');
  182.     document.persist(elementID, 'checked');
  183.   }
  184. }
  185.  
  186.  
  187. function goClickThrobber( urlPref )
  188. {
  189.   var url;
  190.   try {
  191.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  192.                          .getService(Components.interfaces.nsIPrefBranch);
  193.     url = pref.getComplexValue(urlPref, Components.interfaces.nsIPrefLocalizedString).data;
  194.   }
  195.  
  196.   catch(e) {
  197.     url = null;
  198.   }
  199.  
  200.   if ( url )
  201.     openTopWin(url);
  202. }
  203.  
  204.  
  205. //No longer needed.  Rip this out since we are using openTopWin
  206. function goHelpMenu( url )
  207. {
  208.   /* note that this chrome url should probably change to not have all of the navigator controls */
  209.   /* also, do we want to limit the number of help windows that can be spawned? */
  210.   window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  211. }
  212.  
  213. function getTopWin()
  214. {
  215.     var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  216.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  217.     var topWindowOfType = windowManagerInterface.getMostRecentWindow( "navigator:browser" );
  218.  
  219.     if (topWindowOfType) {
  220.         return topWindowOfType;
  221.     }
  222.     return null;
  223. }
  224.  
  225. function openTopWin( url )
  226. {
  227.     /* note that this chrome url should probably change to not have
  228.        all of the navigator controls, but if we do this we need to have
  229.        the option for chrome controls because goClickThrobber() needs to
  230.        use this function with chrome controls */
  231.     /* also, do we want to
  232.        limit the number of help windows that can be spawned? */
  233.     if ((url == null) || (url == "")) return null;
  234.  
  235.     // xlate the URL if necessary
  236.     if (url.indexOf("urn:") == 0)
  237.     {
  238.         url = xlateURL(url);        // does RDF urn expansion
  239.     }
  240.  
  241.     // avoid loading "", since this loads a directory listing
  242.     if (url == "") {
  243.         url = "about:blank";
  244.     }
  245.  
  246.     var topWindowOfType = getTopWin();
  247.     if ( topWindowOfType )
  248.     {
  249.         topWindowOfType.focus();
  250.         topWindowOfType.loadURI(url);
  251.         return topWindowOfType;
  252.     }
  253.     return window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  254. }
  255.  
  256. function goAboutDialog()
  257. {
  258.   var defaultAboutState = false;
  259.   try {
  260.     var pref = Components.classes["@mozilla.org/preferences-service;1"]
  261.                          .getService(Components.interfaces.nsIPrefBranch);
  262.     defaultAboutState = pref.getBoolPref("browser.show_about_as_stupid_modal_window");
  263.   }
  264.   catch(e) {
  265.     defaultAboutState = false;
  266.   }
  267.   if( defaultAboutState )
  268.     window.openDialog("chrome://global/content/about.xul", "About", "modal,chrome,resizable=yes,height=450,width=550");
  269.   else
  270.     window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", 'about:' );
  271. }
  272.  
  273. // update menu items that rely on focus
  274. function goUpdateGlobalEditMenuItems()
  275. {
  276.   goUpdateCommand('cmd_undo');
  277.   goUpdateCommand('cmd_redo');
  278.   goUpdateCommand('cmd_cut');
  279.   goUpdateCommand('cmd_copy');
  280.   goUpdateCommand('cmd_paste');
  281.   goUpdateCommand('cmd_selectAll');
  282.   goUpdateCommand('cmd_delete');
  283. }
  284.  
  285. // update menu items that rely on the current selection
  286. function goUpdateSelectEditMenuItems()
  287. {
  288.   goUpdateCommand('cmd_cut');
  289.   goUpdateCommand('cmd_copy');
  290.   goUpdateCommand('cmd_delete');
  291.   goUpdateCommand('cmd_selectAll');
  292. }
  293.  
  294. // update menu items that relate to undo/redo
  295. function goUpdateUndoEditMenuItems()
  296. {
  297.   goUpdateCommand('cmd_undo');
  298.   goUpdateCommand('cmd_redo');
  299. }
  300.  
  301. // update menu items that depend on clipboard contents
  302. function goUpdatePasteMenuItems()
  303. {
  304.   goUpdateCommand('cmd_paste');
  305. }
  306.  
  307. // function that extracts the filename from a url
  308. function extractFileNameFromUrl(urlstr)
  309. {
  310.   if (!urlstr) return null;
  311.  
  312.   // For "http://foo/bar/cheese.jpg", return "cheese.jpg".
  313.   // For "imap://user@host.com:143/fetch>UID>/INBOX>951?part=1.2&type=image/gif&filename=foo.jpeg", return "foo.jpeg".
  314.   // The 2nd url (ie, "imap://...") is generated for inline images by MimeInlineImage_parse_begin() in mimeiimg.cpp.
  315.   var lastSlash = urlstr.slice(urlstr.lastIndexOf( "/" )+1);
  316.   if (lastSlash)
  317.   { 
  318.     var nameIndex = lastSlash.lastIndexOf( "filename=" );
  319.     if (nameIndex != -1)
  320.       return (lastSlash.slice(nameIndex+9));
  321.     else
  322.       return lastSlash;
  323.   }
  324.   return null; 
  325. }
  326.  
  327. // Gather all descendent text under given document node.
  328. function gatherTextUnder ( root ) 
  329. {
  330.   var text = "";
  331.   var node = root.firstChild;
  332.   var depth = 1;
  333.   while ( node && depth > 0 ) {
  334.     // See if this node is text.
  335.     if ( node.nodeName == "#text" ) {
  336.       // Add this text to our collection.
  337.       text += " " + node.data;
  338.     } else if ( node.nodeType == Node.ELEMENT_NODE 
  339.                 && node.localName.toUpperCase() == "IMG" ) {
  340.       // If it has an alt= attribute, use that.
  341.       var altText = node.getAttribute( "alt" );
  342.       if ( altText && altText != "" ) {
  343.         text = altText;
  344.         break;
  345.       }
  346.     }
  347.     // Find next node to test.
  348.     // First, see if this node has children.
  349.     if ( node.hasChildNodes() ) {
  350.       // Go to first child.
  351.       node = node.firstChild;
  352.       depth++;
  353.     } else {
  354.       // No children, try next sibling.
  355.       if ( node.nextSibling ) {
  356.         node = node.nextSibling;
  357.       } else {
  358.         // Last resort is our next oldest uncle/aunt.
  359.         node = node.parentNode.nextSibling;
  360.         depth--;
  361.       }
  362.     }
  363.   }
  364.   // Strip leading whitespace.
  365.   text = text.replace( /^\s+/, "" );
  366.   // Strip trailing whitespace.
  367.   text = text.replace( /\s+$/, "" );
  368.   // Compress remaining whitespace.
  369.   text = text.replace( /\s+/g, " " );
  370.   return text;
  371. }
  372.  
  373. var offlineObserver = {
  374.   observe: function(subject, topic, state) {
  375.     // sanity checks
  376.     if (topic != "network:offline-status-changed") return;
  377.     setOfflineUI(state == "offline");
  378.   }
  379. }
  380.  
  381. function utilityOnLoad(aEvent)
  382. {
  383.   var broadcaster = document.getElementById("Communicator:WorkMode");
  384.   if (!broadcaster) return;
  385.  
  386.   var observerService = Components.classes[kObserverServiceProgID]
  387.                   .getService(Components.interfaces.nsIObserverService);
  388.  
  389.   // crude way to prevent registering twice.
  390.   try {
  391.     observerService.removeObserver(offlineObserver, "network:offline-status-changed");
  392.   }
  393.   catch (ex) {
  394.   }
  395.   observerService.addObserver(offlineObserver, "network:offline-status-changed", false);
  396.   // make sure we remove this observer later
  397.   addEventListener("unload",utilityOnUnload,false);
  398.  
  399.   // set the initial state
  400.   var ioService = Components.classes[kIOServiceProgID]
  401.               .getService(Components.interfaces.nsIIOService);
  402.   setOfflineUI(ioService.offline);
  403. }
  404.  
  405. function utilityOnUnload(aEvent) 
  406. {
  407.   var observerService = Components.classes[kObserverServiceProgID]
  408.               .getService(Components.interfaces.nsIObserverService);
  409.   observerService.removeObserver(offlineObserver, "network:offline-status-changed");
  410. }
  411.  
  412. addEventListener("load",utilityOnLoad,true);
  413.