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