toolkit/components/downloads/nsDownloadManagerUI.js

branch
TOR_BUG_9701
changeset 14
925c144e1f1f
equal deleted inserted replaced
-1:000000000000 0:8bf97a5ae5d9
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
6
7 ////////////////////////////////////////////////////////////////////////////////
8 //// Constants
9
10 const Cc = Components.classes;
11 const Ci = Components.interfaces;
12 const Cr = Components.results;
13 const DOWNLOAD_MANAGER_URL = "chrome://mozapps/content/downloads/downloads.xul";
14 const PREF_FLASH_COUNT = "browser.download.manager.flashCount";
15
16 ////////////////////////////////////////////////////////////////////////////////
17 //// nsDownloadManagerUI class
18
19 function nsDownloadManagerUI() {}
20
21 nsDownloadManagerUI.prototype = {
22 classID: Components.ID("7dfdf0d1-aff6-4a34-bad1-d0fe74601642"),
23
24 //////////////////////////////////////////////////////////////////////////////
25 //// nsIDownloadManagerUI
26
27 show: function show(aWindowContext, aDownload, aReason, aUsePrivateUI)
28 {
29 if (!aReason)
30 aReason = Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED;
31
32 // First we see if it is already visible
33 let window = this.recentWindow;
34 if (window) {
35 window.focus();
36
37 // If we are being asked to show again, with a user interaction reason,
38 // set the appropriate variable.
39 if (aReason == Ci.nsIDownloadManagerUI.REASON_USER_INTERACTED)
40 window.gUserInteracted = true;
41 return;
42 }
43
44 let parent = null;
45 // We try to get a window to use as the parent here. If we don't have one,
46 // the download manager will close immediately after opening if the pref
47 // browser.download.manager.closeWhenDone is set to true.
48 try {
49 if (aWindowContext)
50 parent = aWindowContext.getInterface(Ci.nsIDOMWindow);
51 } catch (e) { /* it's OK to not have a parent window */ }
52
53 // We pass the download manager and the nsIDownload we want selected (if any)
54 var params = Cc["@mozilla.org/array;1"].createInstance(Ci.nsIMutableArray);
55 params.appendElement(aDownload, false);
56
57 // Pass in the reason as well
58 let reason = Cc["@mozilla.org/supports-PRInt16;1"].
59 createInstance(Ci.nsISupportsPRInt16);
60 reason.data = aReason;
61 params.appendElement(reason, false);
62
63 var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
64 getService(Ci.nsIWindowWatcher);
65 ww.openWindow(parent,
66 DOWNLOAD_MANAGER_URL,
67 "Download:Manager",
68 "chrome,dialog=no,resizable",
69 params);
70 },
71
72 get visible() {
73 return (null != this.recentWindow);
74 },
75
76 getAttention: function getAttention()
77 {
78 if (!this.visible)
79 throw Cr.NS_ERROR_UNEXPECTED;
80
81 var prefs = Cc["@mozilla.org/preferences-service;1"].
82 getService(Ci.nsIPrefBranch);
83 // This preference may not be set, so defaulting to two.
84 let flashCount = 2;
85 try {
86 flashCount = prefs.getIntPref(PREF_FLASH_COUNT);
87 } catch (e) { }
88
89 var win = this.recentWindow.QueryInterface(Ci.nsIDOMChromeWindow);
90 win.getAttentionWithCycleCount(flashCount);
91 },
92
93 //////////////////////////////////////////////////////////////////////////////
94 //// nsDownloadManagerUI
95
96 get recentWindow() {
97 var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
98 getService(Ci.nsIWindowMediator);
99 return wm.getMostRecentWindow("Download:Manager");
100 },
101
102 //////////////////////////////////////////////////////////////////////////////
103 //// nsISupports
104
105 QueryInterface: XPCOMUtils.generateQI([Ci.nsIDownloadManagerUI])
106 };
107
108 ////////////////////////////////////////////////////////////////////////////////
109 //// Module
110
111 let components = [nsDownloadManagerUI];
112 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
113

mercurial