michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */ 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: /** michael@0: * This component implements the nsIDownloadManagerUI interface and opens the michael@0: * Downloads view for the most recent browser window when requested. michael@0: */ michael@0: michael@0: "use strict"; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Globals michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cu = Components.utils; michael@0: const Cr = Components.results; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon", michael@0: "resource:///modules/DownloadsCommon.jsm"); michael@0: XPCOMUtils.defineLazyServiceGetter(this, "gBrowserGlue", michael@0: "@mozilla.org/browser/browserglue;1", michael@0: "nsIBrowserGlue"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "RecentWindow", michael@0: "resource:///modules/RecentWindow.jsm"); michael@0: XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils", michael@0: "resource://gre/modules/PrivateBrowsingUtils.jsm"); michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// DownloadsUI michael@0: michael@0: function DownloadsUI() michael@0: { michael@0: } michael@0: michael@0: DownloadsUI.prototype = { michael@0: classID: Components.ID("{4d99321e-d156-455b-81f7-e7aa2308134f}"), michael@0: michael@0: _xpcom_factory: XPCOMUtils.generateSingletonFactory(DownloadsUI), michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: //// nsISupports michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]), michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: //// nsIDownloadManagerUI michael@0: michael@0: show: function DUI_show(aWindowContext, aDownload, aReason, aUsePrivateUI) michael@0: { michael@0: if (!aReason) { michael@0: aReason = Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED; michael@0: } michael@0: michael@0: if (aReason == Ci.nsIDownloadManagerUI.REASON_NEW_DOWNLOAD) { michael@0: const kMinimized = Ci.nsIDOMChromeWindow.STATE_MINIMIZED; michael@0: let browserWin = gBrowserGlue.getMostRecentBrowserWindow(); michael@0: michael@0: if (!browserWin || browserWin.windowState == kMinimized) { michael@0: this._showDownloadManagerUI(aWindowContext, aUsePrivateUI); michael@0: } michael@0: else { michael@0: // If the indicator is visible, then new download notifications are michael@0: // already handled by the panel service. michael@0: browserWin.DownloadsButton.checkIsVisible(function(isVisible) { michael@0: if (!isVisible) { michael@0: this._showDownloadManagerUI(aWindowContext, aUsePrivateUI); michael@0: } michael@0: }.bind(this)); michael@0: } michael@0: } else { michael@0: this._showDownloadManagerUI(aWindowContext, aUsePrivateUI); michael@0: } michael@0: }, michael@0: michael@0: get visible() true, michael@0: michael@0: getAttention: function () {}, michael@0: michael@0: ////////////////////////////////////////////////////////////////////////////// michael@0: //// Private michael@0: michael@0: /** michael@0: * Helper function that opens the download manager UI. michael@0: */ michael@0: _showDownloadManagerUI: function (aWindowContext, aUsePrivateUI) michael@0: { michael@0: // If we weren't given a window context, try to find a browser window michael@0: // to use as our parent - and if that doesn't work, error out and give up. michael@0: let parentWindow = aWindowContext; michael@0: if (!parentWindow) { michael@0: parentWindow = RecentWindow.getMostRecentBrowserWindow({ private: !!aUsePrivateUI }); michael@0: if (!parentWindow) { michael@0: Components.utils.reportError( michael@0: "Couldn't find a browser window to open the Places Downloads View " + michael@0: "from."); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: // If window is private then show it in a tab. michael@0: if (PrivateBrowsingUtils.isWindowPrivate(parentWindow)) { michael@0: parentWindow.openUILinkIn("about:downloads", "tab"); michael@0: return; michael@0: } else { michael@0: let organizer = Services.wm.getMostRecentWindow("Places:Organizer"); michael@0: if (!organizer) { michael@0: parentWindow.openDialog("chrome://browser/content/places/places.xul", michael@0: "", "chrome,toolbar=yes,dialog=no,resizable", michael@0: "Downloads"); michael@0: } else { michael@0: organizer.PlacesOrganizer.selectLeftPaneQuery("Downloads"); michael@0: organizer.focus(); michael@0: } michael@0: } michael@0: } michael@0: }; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Module michael@0: michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DownloadsUI]);