home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / communicator / utilityOverlay.js < prev    next >
Encoding:
Text File  |  2001-10-16  |  13.3 KB  |  429 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Alec Flett <alecf@netscape.com>
  22.  */
  23.  
  24. /**
  25.  * Communicator Shared Utility Library
  26.  * for shared application glue for the Communicator suite of applications
  27.  **/
  28.  
  29. /**
  30.  * Go into online/offline mode
  31.  **/
  32.  
  33. const kIOServiceProgID = "@mozilla.org/network/io-service;1";
  34. const kObserverServiceProgID = "@mozilla.org/observer-service;1";
  35.  
  36. function toggleOfflineStatus()
  37. {
  38.   var checkfunc;
  39.   try {
  40.     checkfunc = document.getElementById("offline-status").getAttribute('checkfunc');
  41.   }
  42.   catch (ex) {
  43.     checkfunc = null;
  44.   }
  45.  
  46.   var ioService = Components.classes[kIOServiceProgID]
  47.                             .getService(Components.interfaces.nsIIOService);
  48.   if (checkfunc) {
  49.     if (!eval(checkfunc)) {
  50.       // the pre-offline check function returned false, so don't go offline
  51.       return;
  52.     }
  53.   }
  54.   ioService.offline = !ioService.offline;
  55. }
  56.  
  57. function setOfflineUI(offline)
  58. {
  59.   var broadcaster = document.getElementById("Communicator:WorkMode");
  60.   if (!broadcaster) return;
  61.  
  62.   //Checking for a preference "network.online", if it's locked, disabling 
  63.   // network icon and menu item
  64.   var prefService = Components.classes["@mozilla.org/preferences-service;1"];
  65.   prefService = prefService.getService();
  66.   prefService = prefService.QueryInterface(Components.interfaces.nsIPrefService);
  67.   
  68.   var prefBranch = prefService.getBranch(null);
  69.   
  70.   var offlineLocked = prefBranch.prefIsLocked("network.online"); 
  71.   
  72.   if (offlineLocked ) {
  73.       broadcaster.setAttribute("disabled","true");
  74.   }
  75.  
  76.   var bundle = srGetStrBundle("chrome://communicator/locale/utilityOverlay.properties");
  77.  
  78.   if (offline)
  79.     {
  80.       broadcaster.setAttribute("offline", "true");
  81.       broadcaster.setAttribute("tooltiptext", bundle.GetStringFromName("offlineTooltip"));
  82.       broadcaster.setAttribute("label", bundle.GetStringFromName("goonline"));
  83.     }
  84.   else
  85.     {
  86.       broadcaster.removeAttribute("offline");
  87.       broadcaster.setAttribute("tooltiptext", bundle.GetStringFromName("onlineTooltip"));
  88.       broadcaster.setAttribute("label", bundle.GetStringFromName("gooffline"));
  89.     }
  90. }
  91.  
  92. var goPrefWindow = 0;
  93.  
  94. function getBrowserURL() {
  95.  
  96.   try {
  97.     var prefs = Components.classes["@mozilla.org/preferences;1"];
  98.     if (prefs) {
  99.       prefs = prefs.getService();
  100.       if (prefs)
  101.         prefs = prefs.QueryInterface(Components.interfaces.nsIPref);
  102.     }
  103.     if (prefs) {
  104.       var url = prefs.CopyCharPref("browser.chromeURL");
  105.       if (url)
  106.         return url;
  107.     }
  108.   } catch(e) {
  109.   }
  110.   return "chrome://navigator/content/navigator.xul";
  111. }
  112.  
  113. function goPageSetup()
  114. {
  115.   // This code brings up the native page setup dialog (for platforms that
  116.   // implement nsIPrintOptions.ShowNativeDialog()).  
  117.   var printOptionsService = Components.classes["@mozilla.org/gfx/printoptions;1"]
  118.                                            .getService(Components.interfaces.nsIPrintOptions);
  119.   printOptionsService.ReadPrefs();
  120.   printOptionsService.ShowNativeDialog();
  121.   printOptionsService.WritePrefs();
  122. }
  123.  
  124. function goEditCardDialog(abURI, card, okCallback, abCardURI)
  125. {
  126.   window.openDialog("chrome://messenger/content/addressbook/abEditCardDialog.xul",
  127.                       "",
  128.                       "chrome,resizeable=no,modal,titlebar",
  129.                       {abURI:abURI, card:card, okCallback:okCallback, abCardURI:abCardURI});
  130. }
  131.  
  132. function goPreferences(containerID, paneURL, itemID)
  133. {
  134.   var resizable;
  135.   var pref = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  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/rdf/datasource;1?name=window-mediator";
  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;1"].getService();
  192.     if( pref )
  193.     pref = pref.QueryInterface( Components.interfaces.nsIPref );
  194.     url = pref.getLocalizedUnicharPref(urlPref);
  195.   }
  196.  
  197.   catch(e) {
  198.     url = null;
  199.   }
  200.  
  201.   if ( url )
  202.     openTopWin(url);
  203. }
  204.  
  205.  
  206. //No longer needed.  Rip this out since we are using openTopWin
  207. function goHelpMenu( url )
  208. {
  209.   /* note that this chrome url should probably change to not have all of the navigator controls */
  210.   /* also, do we want to limit the number of help windows that can be spawned? */
  211.   window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  212. }
  213.  
  214.  
  215. function openTopWin( url )
  216. {
  217.   /* note that this chrome url should probably change to not have all of the navigator controls,
  218.      but if we do this we need to have the option for chrome controls because goClickThrobber()
  219.      needs to use this function with chrome controls */
  220.   /* also, do we want to limit the number of help windows that can be spawned? */
  221.     if ((url == null) || (url == "")) return;
  222.  
  223.     // xlate the URL if necessary
  224.     if (url.indexOf("urn:") == 0)
  225.     {
  226.         url = xlateURL(url);        // does RDF urn expansion
  227.     }
  228.  
  229.     // avoid loading "", since this loads a directory listing
  230.     if (url == "") {
  231.         url = "about:blank";
  232.     }
  233.  
  234.     var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();
  235.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  236.  
  237.     var topWindowOfType = windowManagerInterface.getMostRecentWindow( "navigator:browser" );
  238.     if ( topWindowOfType )
  239.     {
  240.         topWindowOfType.focus();
  241.     topWindowOfType._content.location.href = url;
  242.     }
  243.     else
  244.     {
  245.         window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", url );
  246.     }
  247. }
  248.  
  249. function goAboutDialog()
  250. {
  251.   var defaultAboutState = false;
  252.   try {
  253.     var pref = Components.classes["@mozilla.org/preferences;1"].getService();
  254.     if( pref )
  255.       pref = pref.QueryInterface( Components.interfaces.nsIPref );
  256.     defaultAboutState = pref.GetBoolPref("browser.show_about_as_stupid_modal_window");
  257.   }
  258.   catch(e) {
  259.     defaultAboutState = false;
  260.   }
  261.   if( defaultAboutState )
  262.     window.openDialog("chrome:global/content/about.xul", "About", "modal,chrome,resizable=yes,height=450,width=550");
  263.   else
  264.     window.openDialog( getBrowserURL(), "_blank", "chrome,all,dialog=no", 'about:' );
  265. }
  266.  
  267. // update menu items that rely on focus
  268. function goUpdateGlobalEditMenuItems()
  269. {
  270.   goUpdateCommand('cmd_undo');
  271.   goUpdateCommand('cmd_redo');
  272.   goUpdateCommand('cmd_cut');
  273.   goUpdateCommand('cmd_copy');
  274.   goUpdateCommand('cmd_paste');
  275.   goUpdateCommand('cmd_selectAll');
  276.   goUpdateCommand('cmd_delete');
  277. }
  278.  
  279. // update menu items that rely on the current selection
  280. function goUpdateSelectEditMenuItems()
  281. {
  282.   goUpdateCommand('cmd_cut');
  283.   goUpdateCommand('cmd_copy');
  284.   goUpdateCommand('cmd_delete');
  285. }
  286.  
  287. // update menu items that relate to undo/redo
  288. function goUpdateUndoEditMenuItems()
  289. {
  290.   goUpdateCommand('cmd_undo');
  291.   goUpdateCommand('cmd_redo');
  292. }
  293.  
  294. // update menu items that depend on clipboard contents
  295. function goUpdatePasteMenuItems()
  296. {
  297.   goUpdateCommand('cmd_paste');
  298. }
  299.  
  300. // This used to be BrowserNewEditorWindow in navigator.js
  301. function NewEditorWindow(aPageURL)
  302. {
  303.   // Open editor window with blank page
  304.   // Kludge to leverage openDialog non-modal!
  305.   window.openDialog( "chrome://editor/content", "_blank", "chrome,all,dialog=no", "about:blank");
  306. }
  307.  
  308. function NewEditorFromTemplate()
  309. {
  310.   // XXX not implemented
  311. }
  312.  
  313. function NewEditorFromDraft()
  314. {
  315.   // XXX not implemented
  316. }
  317.  
  318. // Any non-editor window wanting to create an editor with a URL
  319. //   should use this instead of "window.openDialog..."
  320. //  We must always find an existing window with requested URL
  321. // (When calling from a dialog, "launchWindow" is dialog's "opener"
  322. //   and we need a delay to let dialog close)
  323. function editPage(url, launchWindow, delay)
  324. {
  325.   // User may not have supplied a window
  326.   if (!launchWindow)
  327.   {
  328.     if (window)
  329.     {
  330.       launchWindow = window;
  331.     }
  332.     else
  333.     {
  334.       dump("No window to launch an editor from!\n");
  335.       return;
  336.     }
  337.   }
  338.  
  339.   var webNav = null;
  340.   if (launchWindow && "getWebNavigation" in launchWindow)
  341.     webNav = launchWindow.getWebNavigation();
  342.   if (webNav && webNav.postData)
  343.     return;
  344.  
  345.   // if the current window is a browser window, then extract the current charset menu setting from the current 
  346.   // document and use it to initialize the new composer window...
  347.  
  348.   var wintype = document.firstChild.getAttribute('windowtype');
  349.   var charsetArg;
  350.  
  351.   if (launchWindow && (wintype == "navigator:browser"))
  352.     charsetArg = "charset=" + launchWindow._content.document.characterSet;
  353.  
  354.   var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();
  355.   if (!windowManager) return;
  356.   var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  357.   if ( !windowManagerInterface ) return;
  358.   var enumerator = windowManagerInterface.getEnumerator( "composer:html" );
  359.   if ( !enumerator ) return;
  360.  
  361.   while ( enumerator.hasMoreElements() )
  362.   {
  363.     var window = windowManagerInterface.convertISupportsToDOMWindow( enumerator.getNext() );
  364.     if ( window && window.editorShell)
  365.     {
  366.       if (window.editorShell.checkOpenWindowForURLMatch(url, window))
  367.       {
  368.         // We found an editor with our url
  369.         window.focus();
  370.         return;
  371.       }
  372.     }
  373.   }
  374.  
  375.   // Create new Composer window
  376.   if (delay)
  377.     launchWindow.delayedOpenWindow("chrome://editor/content", "chrome,all,dialog=no", url, charsetArg);
  378.   else
  379.     launchWindow.openDialog("chrome://editor/content", "_blank", "chrome,all,dialog=no", url, charsetArg);
  380. }
  381.  
  382. // function that extracts the filename from a url
  383. function extractFileNameFromUrl(urlstr)
  384. {
  385.   if (!urlstr) return null;
  386.   return urlstr.slice(urlstr.lastIndexOf( "/" )+1);
  387. }
  388.  
  389. var offlineObserver = {
  390.   Observe: function(subject, topic, state) {
  391.     // sanity checks
  392.     if (topic != "network:offline-status-changed") return;
  393.     setOfflineUI(state == "offline");
  394.   }
  395. }
  396.  
  397. function utilityOnLoad(aEvent)
  398. {
  399.   var broadcaster = document.getElementById("Communicator:WorkMode");
  400.   if (!broadcaster) return;
  401.  
  402.   var observerService = Components.classes[kObserverServiceProgID]
  403.                   .getService(Components.interfaces.nsIObserverService);
  404.  
  405.   // crude way to prevent registering twice.
  406.   try {
  407.     observerService.RemoveObserver(offlineObserver, "network:offline-status-changed");
  408.   }
  409.   catch (ex) {
  410.   }
  411.   observerService.AddObserver(offlineObserver, "network:offline-status-changed");
  412.   // make sure we remove this observer later
  413.   addEventListener("unload",utilityOnUnload,false);
  414.  
  415.   // set the initial state
  416.   var ioService = Components.classes[kIOServiceProgID]
  417.               .getService(Components.interfaces.nsIIOService);
  418.   setOfflineUI(ioService.offline);
  419. }
  420.  
  421. function utilityOnUnload(aEvent) 
  422. {
  423.   var observerService = Components.classes[kObserverServiceProgID]
  424.               .getService(Components.interfaces.nsIObserverService);
  425.   observerService.RemoveObserver(offlineObserver, "network:offline-status-changed");
  426. }
  427.  
  428. addEventListener("load",utilityOnLoad,true);
  429.