browser/components/downloads/src/DownloadsUI.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:e5212afee61e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80 filetype=javascript: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /**
8 * This component implements the nsIDownloadManagerUI interface and opens the
9 * Downloads view for the most recent browser window when requested.
10 */
11
12 "use strict";
13
14 ////////////////////////////////////////////////////////////////////////////////
15 //// Globals
16
17 const Cc = Components.classes;
18 const Ci = Components.interfaces;
19 const Cu = Components.utils;
20 const Cr = Components.results;
21
22 Cu.import("resource://gre/modules/Services.jsm");
23 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
24
25 XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
26 "resource:///modules/DownloadsCommon.jsm");
27 XPCOMUtils.defineLazyServiceGetter(this, "gBrowserGlue",
28 "@mozilla.org/browser/browserglue;1",
29 "nsIBrowserGlue");
30 XPCOMUtils.defineLazyModuleGetter(this, "RecentWindow",
31 "resource:///modules/RecentWindow.jsm");
32 XPCOMUtils.defineLazyModuleGetter(this, "PrivateBrowsingUtils",
33 "resource://gre/modules/PrivateBrowsingUtils.jsm");
34
35 ////////////////////////////////////////////////////////////////////////////////
36 //// DownloadsUI
37
38 function DownloadsUI()
39 {
40 }
41
42 DownloadsUI.prototype = {
43 classID: Components.ID("{4d99321e-d156-455b-81f7-e7aa2308134f}"),
44
45 _xpcom_factory: XPCOMUtils.generateSingletonFactory(DownloadsUI),
46
47 //////////////////////////////////////////////////////////////////////////////
48 //// nsISupports
49
50 QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI]),
51
52 //////////////////////////////////////////////////////////////////////////////
53 //// nsIDownloadManagerUI
54
55 show: function DUI_show(aWindowContext, aDownload, aReason, aUsePrivateUI)
56 {
57 if (!aReason) {
58 aReason = Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED;
59 }
60
61 if (aReason == Ci.nsIDownloadManagerUI.REASON_NEW_DOWNLOAD) {
62 const kMinimized = Ci.nsIDOMChromeWindow.STATE_MINIMIZED;
63 let browserWin = gBrowserGlue.getMostRecentBrowserWindow();
64
65 if (!browserWin || browserWin.windowState == kMinimized) {
66 this._showDownloadManagerUI(aWindowContext, aUsePrivateUI);
67 }
68 else {
69 // If the indicator is visible, then new download notifications are
70 // already handled by the panel service.
71 browserWin.DownloadsButton.checkIsVisible(function(isVisible) {
72 if (!isVisible) {
73 this._showDownloadManagerUI(aWindowContext, aUsePrivateUI);
74 }
75 }.bind(this));
76 }
77 } else {
78 this._showDownloadManagerUI(aWindowContext, aUsePrivateUI);
79 }
80 },
81
82 get visible() true,
83
84 getAttention: function () {},
85
86 //////////////////////////////////////////////////////////////////////////////
87 //// Private
88
89 /**
90 * Helper function that opens the download manager UI.
91 */
92 _showDownloadManagerUI: function (aWindowContext, aUsePrivateUI)
93 {
94 // If we weren't given a window context, try to find a browser window
95 // to use as our parent - and if that doesn't work, error out and give up.
96 let parentWindow = aWindowContext;
97 if (!parentWindow) {
98 parentWindow = RecentWindow.getMostRecentBrowserWindow({ private: !!aUsePrivateUI });
99 if (!parentWindow) {
100 Components.utils.reportError(
101 "Couldn't find a browser window to open the Places Downloads View " +
102 "from.");
103 return;
104 }
105 }
106
107 // If window is private then show it in a tab.
108 if (PrivateBrowsingUtils.isWindowPrivate(parentWindow)) {
109 parentWindow.openUILinkIn("about:downloads", "tab");
110 return;
111 } else {
112 let organizer = Services.wm.getMostRecentWindow("Places:Organizer");
113 if (!organizer) {
114 parentWindow.openDialog("chrome://browser/content/places/places.xul",
115 "", "chrome,toolbar=yes,dialog=no,resizable",
116 "Downloads");
117 } else {
118 organizer.PlacesOrganizer.selectLeftPaneQuery("Downloads");
119 organizer.focus();
120 }
121 }
122 }
123 };
124
125 ////////////////////////////////////////////////////////////////////////////////
126 //// Module
127
128 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([DownloadsUI]);

mercurial