summaryrefslogtreecommitdiffstats
path: root/comm/suite/components/downloads/content/progressDialog.js
blob: dae93f7fe94983e46006b89e65adaddc6c3b3b98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
var {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetters(this, {
  DownloadUtils: "resource://gre/modules/DownloadUtils.jsm",
  DownloadsCommon: "resource:///modules/DownloadsCommon.jsm",
});

var gDownload;
var gDownloadBundle;

var gDlList;
var gDlStatus;
var gDlListener;
var gDlSize;
var gTimeLeft;
var gProgressMeter;
var gProgressText;
var gCloseWhenDone;

function progressStartup() {
  gDownload = window.arguments[0].wrappedJSObject;
  Downloads.getList(gDownload.source.isPrivate ? Downloads.PRIVATE : Downloads.PUBLIC).then(progressAsyncStartup);
}

function progressAsyncStartup(aList) {
  gDlList = aList;

  // cache elements to save .getElementById() calls
  gDownloadBundle = document.getElementById("dmBundle");
  gDlStatus = document.getElementById("dlStatus");
  gDlSize = document.getElementById("dlSize");
  gTimeLeft = document.getElementById("timeLeft");
  gProgressMeter = document.getElementById("progressMeter");
  gProgressText = document.getElementById("progressText");
  gCloseWhenDone = document.getElementById("closeWhenDone");

  // Insert as first controller on the whole window
  window.controllers.insertControllerAt(0, ProgressDlgController);

  if (gDownload.isPrivate)
    gCloseWhenDone.hidden = true;
  else
    gCloseWhenDone.checked = Services.prefs.getBoolPref("browser.download.progress.closeWhenDone");

  if (gDownload.succeeded) {
    if (gCloseWhenDone.checked && !window.arguments[1])
      window.close();
  }

  var fName = document.getElementById("fileName");
  var fSource = document.getElementById("fileSource");
  fName.label = gDownload.displayName;
  fName.tooltipText = gDownload.target.path;
  var uri = Services.io.newURI(gDownload.source.url);
  var fromString;
  try {
    fromString = uri.host;
  }
  catch (e) { }
  if (!fromString)
    fromString = uri.prePath;
  fSource.label = gDownloadBundle.getFormattedString("fromSource", [fromString]);
  fSource.tooltipText = gDownload.source.url;

  // The DlProgressListener handles progress notifications.
  gDlListener = new DlProgressListener();
  gDlList.addView(gDlListener);

  updateDownload();
  updateButtons();
  window.updateCommands("dlstate-change");
}

function progressShutdown() {
  gDlList.removeView(gDlListener);
  window.controllers.removeController(ProgressDlgController);
  if (!gCloseWhenDone.hidden)
    Services.prefs.setBoolPref("browser.download.progress.closeWhenDone",
                               gCloseWhenDone.checked);
}

function updateDownload() {
  if (gDownload.hasProgress) {
    gProgressText.value = gDownloadBundle.getFormattedString("percentFormat",
                                                             [gDownload.progress]);
    gProgressText.hidden = false;
    gProgressMeter.value = gDownload.progress;
    gProgressMeter.mode = "determined";
  } else {
    gProgressText.hidden = true;
    gProgressMeter.mode = "undetermined";
  }
  if (gDownload.stopped) {
    gProgressMeter.style.opacity = 0.5;
  } else {
    gProgressMeter.style.opacity = 1;
  }
  // Update window title
  let statusString = DownloadsCommon.stateOfDownloadText(gDownload);

  if (gDownload.hasProgress) {
    document.title = gDownloadBundle.getFormattedString("progressTitlePercent",
                                                        [gDownload.progress,
                                                         gDownload.displayName,
                                                         statusString]);
  }
  else {
    document.title = gDownloadBundle.getFormattedString("progressTitle",
                                                        [gDownload.displayName,
                                                         statusString]);
  }

  // download size / transferred bytes
  gDlSize.value = DownloadsCommon.getTransferredBytes(gDownload);

  // time remaining
  gTimeLeft.value = DownloadsCommon.getTimeRemaining(gDownload);

  // download status
  gDlStatus.value = statusString;

}

function updateButtons() {
  document.getElementById("pauseButton").hidden = !ProgressDlgController.isCommandEnabled("cmd_pause");
  document.getElementById("resumeButton").hidden = !ProgressDlgController.isCommandEnabled("cmd_resume");
  document.getElementById("retryButton").hidden = !ProgressDlgController.isCommandEnabled("cmd_retry");
  document.getElementById("cancelButton").hidden = !ProgressDlgController.isCommandEnabled("cmd_cancel");
}

/**
 * DlProgressListener "class" is used to help update download items shown
 * in the progress dialog such as displaying amount transferred, transfer
 * rate, and time left for the download.
 *
 * This class implements the downloadProgressListener interface.
 */
function DlProgressListener() {}

DlProgressListener.prototype = {
  onDownloadChanged: function(aDownload) {
    if (aDownload == gDownload) {
      if (gCloseWhenDone.checked && aDownload.succeeded) {
        window.close();
      }
      updateDownload();
      updateButtons();
      window.updateCommands("dlstate-change");
    }
  },

  onDownloadRemoved: function(aDownload) {
    if (aDownload == gDownload)
      window.close();
  }
};

var ProgressDlgController = {
  supportsCommand: function(aCommand) {
    switch (aCommand) {
      case "cmd_pause":
      case "cmd_resume":
      case "cmd_retry":
      case "cmd_cancel":
      case "cmd_open":
      case "cmd_show":
      case "cmd_openReferrer":
      case "cmd_copyLocation":
        return true;
    }
    return false;
  },

  isCommandEnabled: function(aCommand) {
    switch (aCommand) {
      case "cmd_pause":
        return !gDownload.stopped && gDownload.hasPartialData;
      case "cmd_resume":
        return gDownload.stopped && gDownload.hasPartialData;
      case "cmd_open":
        return gDownload.succeeded && gDownload.target.exists;
      case "cmd_show":
        return gDownload.target.exists;
      case "cmd_cancel":
        return !gDownload.stopped || gDownload.hasPartialData;
      case "cmd_retry":
        return !gDownload.succeeded && gDownload.stopped && !gDownload.hasPartialData;
      case "cmd_openReferrer":
        return !!gDownload.source.referrer;
      case "cmd_copyLocation":
        return true;
      default:
        return false;
    }
  },

  doCommand: function(aCommand) {
    switch (aCommand) {
      case "cmd_pause":
        gDownload.cancel();
        break;
      case "cmd_resume":
      case "cmd_retry":
        gDownload.start();
        break;
      case "cmd_cancel":
        cancelDownload(gDownload);
        break;
      case "cmd_open":
        openDownload(gDownload);
        break;
      case "cmd_show":
        showDownload(gDownload);
        break;
      case "cmd_openReferrer":
        openUILink(gDownload.source.referrer);
        break;
      case "cmd_copyLocation":
        var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
                          .getService(Ci.nsIClipboardHelper);
        clipboard.copyString(gDownload.source.url);
        break;
    }
  },

  onEvent: function(aEvent) {
  },

  onCommandUpdate: function() {
    var cmds = ["cmd_pause", "cmd_resume", "cmd_retry", "cmd_cancel",
                "cmd_open", "cmd_show", "cmd_openReferrer", "cmd_copyLocation"];
    for (let command in cmds)
      goUpdateCommand(cmds[command]);
  }
};