michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function closeWindow(aClose, aPromptFunction) michael@0: { michael@0: # Closing the last window doesn't quit the application on OS X. michael@0: #ifndef XP_MACOSX michael@0: var windowCount = 0; michael@0: var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] michael@0: .getService(Components.interfaces.nsIWindowMediator); michael@0: var e = wm.getEnumerator(null); michael@0: michael@0: while (e.hasMoreElements()) { michael@0: var w = e.getNext(); michael@0: if (w.closed) { michael@0: continue; michael@0: } michael@0: if (++windowCount == 2) michael@0: break; michael@0: } michael@0: michael@0: // If we're down to the last window and someone tries to shut down, check to make sure we can! michael@0: if (windowCount == 1 && !canQuitApplication("lastwindow")) michael@0: return false; michael@0: else if (windowCount != 1) michael@0: #endif michael@0: if (typeof(aPromptFunction) == "function" && !aPromptFunction()) michael@0: return false; michael@0: michael@0: if (aClose) michael@0: window.close(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: function canQuitApplication(aData) michael@0: { michael@0: var os = Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Components.interfaces.nsIObserverService); michael@0: if (!os) return true; michael@0: michael@0: try { michael@0: var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"] michael@0: .createInstance(Components.interfaces.nsISupportsPRBool); michael@0: os.notifyObservers(cancelQuit, "quit-application-requested", aData || null); michael@0: michael@0: // Something aborted the quit process. michael@0: if (cancelQuit.data) michael@0: return false; michael@0: } michael@0: catch (ex) { } michael@0: return true; michael@0: } michael@0: michael@0: function goQuitApplication() michael@0: { michael@0: if (!canQuitApplication()) michael@0: return false; michael@0: michael@0: var appStartup = Components.classes['@mozilla.org/toolkit/app-startup;1']. michael@0: getService(Components.interfaces.nsIAppStartup); michael@0: michael@0: appStartup.quit(Components.interfaces.nsIAppStartup.eAttemptQuit); michael@0: return true; michael@0: } michael@0: michael@0: // michael@0: // Command Updater functions michael@0: // michael@0: function goUpdateCommand(aCommand) michael@0: { michael@0: try { michael@0: var controller = top.document.commandDispatcher michael@0: .getControllerForCommand(aCommand); michael@0: michael@0: var enabled = false; michael@0: if (controller) michael@0: enabled = controller.isCommandEnabled(aCommand); michael@0: michael@0: goSetCommandEnabled(aCommand, enabled); michael@0: } michael@0: catch (e) { michael@0: Components.utils.reportError("An error occurred updating the " + michael@0: aCommand + " command: " + e); michael@0: } michael@0: } michael@0: michael@0: function goDoCommand(aCommand) michael@0: { michael@0: try { michael@0: var controller = top.document.commandDispatcher michael@0: .getControllerForCommand(aCommand); michael@0: if (controller && controller.isCommandEnabled(aCommand)) michael@0: controller.doCommand(aCommand); michael@0: } michael@0: catch (e) { michael@0: Components.utils.reportError("An error occurred executing the " + michael@0: aCommand + " command: " + e); michael@0: } michael@0: } michael@0: michael@0: michael@0: function goSetCommandEnabled(aID, aEnabled) michael@0: { michael@0: var node = document.getElementById(aID); michael@0: michael@0: if (node) { michael@0: if (aEnabled) michael@0: node.removeAttribute("disabled"); michael@0: else michael@0: node.setAttribute("disabled", "true"); michael@0: } michael@0: } michael@0: michael@0: function goSetMenuValue(aCommand, aLabelAttribute) michael@0: { michael@0: var commandNode = top.document.getElementById(aCommand); michael@0: if (commandNode) { michael@0: var label = commandNode.getAttribute(aLabelAttribute); michael@0: if (label) michael@0: commandNode.setAttribute("label", label); michael@0: } michael@0: } michael@0: michael@0: function goSetAccessKey(aCommand, aValueAttribute) michael@0: { michael@0: var commandNode = top.document.getElementById(aCommand); michael@0: if (commandNode) { michael@0: var value = commandNode.getAttribute(aValueAttribute); michael@0: if (value) michael@0: commandNode.setAttribute("accesskey", value); michael@0: } michael@0: } michael@0: michael@0: // this function is used to inform all the controllers attached to a node that an event has occurred michael@0: // (e.g. the tree controllers need to be informed of blur events so that they can change some of the michael@0: // menu items back to their default values) michael@0: function goOnEvent(aNode, aEvent) michael@0: { michael@0: var numControllers = aNode.controllers.getControllerCount(); michael@0: var controller; michael@0: michael@0: for (var controllerIndex = 0; controllerIndex < numControllers; controllerIndex++) { michael@0: controller = aNode.controllers.getControllerAt(controllerIndex); michael@0: if (controller) michael@0: controller.onEvent(aEvent); michael@0: } michael@0: } michael@0: michael@0: function visitLink(aEvent) { michael@0: var node = aEvent.target; michael@0: while (node.nodeType != Node.ELEMENT_NODE) michael@0: node = node.parentNode; michael@0: var url = node.getAttribute("link"); michael@0: if (!url) michael@0: return; michael@0: michael@0: var protocolSvc = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"] michael@0: .getService(Components.interfaces.nsIExternalProtocolService); michael@0: var ioService = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var uri = ioService.newURI(url, null, null); michael@0: michael@0: // if the scheme is not an exposed protocol, then opening this link michael@0: // should be deferred to the system's external protocol handler michael@0: if (protocolSvc.isExposedProtocol(uri.scheme)) { michael@0: var win = window.top; michael@0: if (win instanceof Components.interfaces.nsIDOMChromeWindow) { michael@0: while (win.opener && !win.opener.closed) michael@0: win = win.opener; michael@0: } michael@0: win.open(uri.spec); michael@0: } michael@0: else michael@0: protocolSvc.loadUrl(uri); michael@0: } michael@0: michael@0: function setTooltipText(aID, aTooltipText) michael@0: { michael@0: var element = document.getElementById(aID); michael@0: if (element) michael@0: element.setAttribute("tooltiptext", aTooltipText); michael@0: } michael@0: michael@0: this.__defineGetter__("NS_ASSERT", function() { michael@0: delete this.NS_ASSERT; michael@0: var tmpScope = {}; michael@0: Components.utils.import("resource://gre/modules/debug.js", tmpScope); michael@0: return this.NS_ASSERT = tmpScope.NS_ASSERT; michael@0: });