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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
// Copyright (c) 2011 by Kris Maglione <maglione.k@gmail.com>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
"use strict";
Components.utils.import("resource://dactyl/bootstrap.jsm");
defineModule("downloads", {
exports: ["Download", "Downloads", "downloads"],
use: ["io", "messages", "prefs", "services", "util"]
}, this);
Cu.import("resource://gre/modules/DownloadUtils.jsm", this);
let prefix = "DOWNLOAD_";
var states = iter([v, k.slice(prefix.length).toLowerCase()]
for ([k, v] in Iterator(Ci.nsIDownloadManager))
if (k.indexOf(prefix) == 0))
.toObject();
var Download = Class("Download", {
init: function init(id, list) {
let self = XPCSafeJSObjectWrapper(services.downloadManager.getDownload(id));
self.__proto__ = this;
this.instance = this;
this.list = list;
this.nodes = {
commandTarget: self
};
util.xmlToDom(
<tr highlight="Download" key="row" xmlns:dactyl={NS} xmlns={XHTML}>
<td highlight="DownloadTitle">
<span highlight="Link">
<a key="launch" dactyl:command="download.command"
href={self.target.spec} path={self.targetFile.path}>{self.displayName}</a>
<span highlight="LinkInfo">{self.targetFile.path}</span>
</span>
</td>
<td highlight="DownloadState" key="state"/>
<td highlight="DownloadButtons Buttons">
<a highlight="Button" key="pause">{_("download.action.Pause")}</a>
<a highlight="Button" key="remove">{_("download.action.Remove")}</a>
<a highlight="Button" key="resume">{_("download.action.Resume")}</a>
<a highlight="Button" key="retry">{_("download.action.Retry")}</a>
<a highlight="Button" key="cancel">{_("download.action.Cancel")}</a>
<a highlight="Button" key="delete">{_("download.action.Delete")}</a>
</td>
<td highlight="DownloadProgress" key="progress">
<span highlight="DownloadProgressHave" key="progressHave"
/>/<span highlight="DownloadProgressTotal" key="progressTotal"/>
</td>
<td highlight="DownloadPercent" key="percent"/>
<td highlight="DownloadSpeed" key="speed"/>
<td highlight="DownloadTime" key="time"/>
<td><a highlight="DownloadSource" key="source" href={self.source.spec}>{self.source.spec}</a></td>
</tr>,
this.list.document, this.nodes);
self.updateStatus();
return self;
},
get status() states[this.state],
inState: function inState(states) states.indexOf(this.status) >= 0,
get alive() this.inState(["downloading", "notstarted", "paused", "queued", "scanning"]),
allowedCommands: Class.memoize(function () let (self = this) ({
get cancel() self.cancelable && self.inState(["downloading", "paused", "starting"]),
get delete() !this.cancel && self.targetFile.exists(),
get launch() self.targetFile.exists() && self.inState(["finished"]),
get pause() self.inState(["downloading"]),
get remove() self.inState(["blocked_parental", "blocked_policy",
"canceled", "dirty", "failed", "finished"]),
get resume() self.resumable && self.inState(["paused"]),
get retry() self.inState(["canceled", "failed"])
})),
command: function command(name) {
util.assert(set.has(this.allowedCommands, name), _("download.unknownCommand"));
util.assert(this.allowedCommands[name], _("download.commandNotAllowed"));
services.downloadManager[name + "Download"](this.id);
},
commands: {
delete: function delete_() {
this.targetFile.remove(false);
this.updateStatus();
},
launch: function launch() {
let self = this;
// Behavior mimics that of the builtin Download Manager.
function action() {
try {
if (this.MIMEInfo && this.MIMEInfo.preferredAction == this.MIMEInfo.useHelperApp)
this.MIMEInfo.launchWithFile(file)
else
file.launch();
}
catch (e) {
services.externalProtocol.loadUrl(this.target);
}
}
let file = io.File(this.targetFile);
if (file.isExecutable() && prefs.get("browser.download.manager.alertOnEXEOpen", true))
this.list.modules.commandline.input(_("download.prompt.launchExecutable") + " ",
function (resp) {
if (/^a(lways)$/i.test(resp)) {
prefs.set("browser.download.manager.alertOnEXEOpen", false);
resp = "yes";
}
if (/^y(es)?$/i.test(resp))
action.call(self);
});
else
action.call(this);
}
},
_compare: {
active: function (a, b) a.alive - b.alive,
complete: function (a, b) a.percentComplete - b.percentComplete,
date: function (a, b) a.startTime - b.startTime,
filename: function (a, b) String.localeCompare(a.targetFile.leafName, b.targetFile.leafName),
size: function (a, b) a.size - b.size,
speed: function (a, b) a.speed - b.speed,
time: function (a, b) a.timeRemaining - b.timeRemaining,
url: function (a, b) String.localeCompare(a.source.spec, b.source.spec)
},
compare: function compare(other) values(this.list.sortOrder).map(function (order) {
let val = this._compare[order.substr(1)](this, other);
return (order[0] == "-") ? -val : val;
}, this).nth(util.identity, 0) || 0,
timeRemaining: Infinity,
updateProgress: function updateProgress() {
let self = this.__proto__;
if (this.amountTransferred === this.size) {
this.nodes.speed.textContent = "";
this.nodes.time.textContent = "";
}
else {
this.nodes.speed.textContent = util.formatBytes(this.speed, 1, true) + "/s";
if (this.speed == 0 || this.size == 0)
this.nodes.time.textContent = _("download.unknown");
else {
let seconds = (this.size - this.amountTransferred) / this.speed;
[, self.timeRemaining] = DownloadUtils.getTimeLeft(seconds, this.timeRemaining);
if (this.timeRemaining)
this.nodes.time.textContent = util.formatSeconds(this.timeRemaining);
else
this.nodes.time.textContent = _("download.almostDone");
}
}
let total = this.nodes.progressTotal.textContent = this.size ? util.formatBytes(this.size, 1, true) : _("download.unknown");
let suffix = RegExp(/( [a-z]+)?$/i.exec(total)[0] + "$");
this.nodes.progressHave.textContent = util.formatBytes(this.amountTransferred, 1, true).replace(suffix, "");
this.nodes.percent.textContent = this.size ? Math.round(this.amountTransferred * 100 / this.size) + "%" : "";
},
updateStatus: function updateStatus() {
this.nodes.row[this.alive ? "setAttribute" : "removeAttribute"]("active", "true");
this.nodes.row.setAttribute("status", this.status);
this.nodes.state.textContent = util.capitalize(this.status);
for (let node in values(this.nodes))
if (node.update)
node.update();
this.updateProgress();
}
});
var DownloadList = Class("DownloadList",
XPCOM([Ci.nsIDownloadProgressListener,
Ci.nsIObserver,
Ci.nsISupportsWeakReference]), {
init: function init(modules, filter, sort) {
this.sortOrder = sort;
this.modules = modules;
this.filter = filter && filter.toLowerCase();
this.nodes = {
commandTarget: this
};
this.downloads = {};
},
cleanup: function cleanup() {
this.observe.unregister();
services.downloadManager.removeListener(this);
},
message: Class.memoize(function () {
util.xmlToDom(<table highlight="Downloads" key="list" xmlns={XHTML}>
<tr highlight="DownloadHead">
<span>{_("title.Title")}</span>
<span>{_("title.Status")}</span>
<span/>
<span>{_("title.Progress")}</span>
<span/>
<span>{_("title.Speed")}</span>
<span>{_("title.Time remaining")}</span>
<span>{_("title.Source")}</span>
</tr>
<tr highlight="Download"><span><div style="min-height: 1ex; /* FIXME */"/></span></tr>
<tr highlight="Download" key="totals" active="true">
<td><span highlight="Title">{_("title.Totals")}:</span> <span key="total"/></td>
<td/>
<td highlight="DownloadButtons">
<a highlight="Button" key="clear">{_("download.action.Clear")}</a>
</td>
<td highlight="DownloadProgress" key="progress">
<span highlight="DownloadProgressHave" key="progressHave"
/>/<span highlight="DownloadProgressTotal" key="progressTotal"/>
</td>
<td highlight="DownloadPercent" key="percent"/>
<td highlight="DownloadSpeed" key="speed"/>
<td highlight="DownloadTime" key="time"/>
<td/>
</tr>
</table>, this.document, this.nodes);
for (let row in iter(services.downloadManager.DBConnection
.createStatement("SELECT id FROM moz_downloads")))
this.addDownload(row.id);
this.update();
util.addObserver(this);
services.downloadManager.addListener(this);
return this.nodes.list;
}),
addDownload: function addDownload(id) {
if (!(id in this.downloads)) {
let download = Download(id, this);
if (this.filter && download.displayName.indexOf(this.filter) === -1)
return;
this.downloads[id] = download;
let index = values(this.downloads).sort(function (a, b) a.compare(b))
.indexOf(download);
this.nodes.list.insertBefore(download.nodes.row,
this.nodes.list.childNodes[index + 1]);
}
},
removeDownload: function removeDownload(id) {
if (id in this.downloads) {
this.nodes.list.removeChild(this.downloads[id].nodes.row);
delete this.downloads[id];
}
},
leave: function leave(stack) {
if (stack.pop)
this.cleanup();
},
allowedCommands: Class.memoize(function () let (self = this) ({
get clear() values(self.downloads).some(function (dl) dl.allowedCommands.remove)
})),
commands: {
clear: function () {
services.downloadManager.cleanUp();
}
},
sort: function sort() {
let list = values(this.downloads).sort(function (a, b) a.compare(b));
for (let [i, download] in iter(list))
if (this.nodes.list.childNodes[i + 1] != download.nodes.row)
this.nodes.list.insertBefore(download.nodes.row,
this.nodes.list.childNodes[i + 1]);
},
shouldSort: function shouldSort() Array.some(arguments, function (val) this.sortOrder.some(function (v) v.substr(1) == val), this),
update: function update() {
for (let node in values(this.nodes))
if (node.update && node.update != update)
node.update();
this.updateProgress();
let event = this.document.createEvent("Events");
event.initEvent("dactyl-commandupdate", true, false);
this.document.dispatchEvent(event);
},
timeRemaining: Infinity,
updateProgress: function updateProgress() {
let downloads = values(this.downloads).toArray();
let self = Object.create(this);
for (let prop in values(["amountTransferred", "size", "speed", "timeRemaining"]))
this[prop] = downloads.reduce(function (acc, dl) dl[prop] + acc, 0);
Download.prototype.updateProgress.call(self);
let active = downloads.filter(function (dl) dl.alive).length;
if (active)
this.nodes.total.textContent = _("download.nActive", active);
else for (let key in values(["total", "percent", "speed", "time"]))
this.nodes[key].textContent = "";
if (this.shouldSort("complete", "size", "speed", "time"))
this.sort();
},
observers: {
"download-manager-remove-download": function (id) {
if (id == null)
id = [k for ([k, dl] in iter(this.downloads)) if (dl.allowedCommands.remove)];
else
id = [id.QueryInterface(Ci.nsISupportsPRUint32).data];
Array.concat(id).map(this.closure.removeDownload);
this.update();
}
},
onDownloadStateChange: function (state, download) {
try {
if (download.id in this.downloads)
this.downloads[download.id].updateStatus();
else {
this.addDownload(download.id);
this.modules.mow.resize(false);
this.nodes.list.scrollIntoView(false);
}
this.update();
if (this.shouldSort("active"))
this.sort();
}
catch (e) {
util.reportError(e);
}
},
onProgressChange: function (webProgress, request,
curProgress, maxProgress,
curTotalProgress, maxTotalProgress,
download) {
try {
if (download.id in this.downloads)
this.downloads[download.id].updateProgress();
this.updateProgress();
}
catch (e) {
util.reportError(e);
}
}
});
var Downloads = Module("downloads", {
}, {
}, {
commands: function initCommands(dactyl, modules, window) {
const { commands, CommandOption } = modules;
commands.add(["downl[oads]", "dl"],
"Display the downloads list",
function (args) {
let downloads = DownloadList(modules, args[0], args["-sort"]);
modules.commandline.echo(downloads);
},
{
argCount: "?",
options: [
{
names: ["-sort", "-s"],
description: "Sort order (see 'downloadsort')",
type: CommandOption.LIST,
get default() modules.options["downloadsort"],
completer: function (context, args) modules.options.get("downloadsort").completer(context, { values: args["-sort"] }),
validator: function (value) modules.options.get("downloadsort").validator(value)
}
]
});
commands.add(["dlc[lear]"],
"Clear completed downloads",
function (args) { services.downloadManager.cleanUp(); });
},
options: function initOptions(dactyl, modules, window) {
const { options } = modules;
if (false)
options.add(["downloadcolumns", "dlc"],
"The columns to show in the download manager",
"stringlist", "filename,state,buttons,progress,percent,time,url",
{
values: {
buttons: "Control buttons",
filename: "Target filename",
percent: "Percent complete",
size: "File size",
speed: "Download speed",
state: "The download's state",
time: "Time remaining",
url: "Source URL"
}
});
options.add(["downloadsort", "dlsort", "dls"],
":downloads sort order",
"stringlist", "-active,+filename",
{
values: {
active: "Whether download is active",
complete: "Percent complete",
date: "Date and time the download began",
filename: "Target filename",
size: "File size",
speed: "Download speed",
time: "Time remaining",
url: "Source URL"
},
completer: function (context, extra) {
let seen = set.has(set(extra.values.map(function (val) val.substr(1))));
context.completions = iter(this.values).filter(function ([k, v]) !seen(k))
.map(function ([k, v]) [["+" + k, [v, " (", _("sort.ascending"), ")"].join("")],
["-" + k, [v, " (", _("sort.descending"), ")"].join("")]])
.flatten().array;
},
has: function () Array.some(arguments, function (val) this.value.some(function (v) v.substr(1) == val)),
validator: function (value) {
let seen = {};
return value.every(function (val) /^[+-]/.test(val) && set.has(this.values, val.substr(1))
&& !set.add(seen, val.substr(1)),
this) && value.length;
}
});
}
});
endModule();
// catch(e){ if (isString(e)) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
|