1
/* You may find the license in the LICENSE file */
4
Components.utils.import('resource://dta/prompts.jsm', Prompts);
5
Components.utils.import('resource://dta/version.jsm');
7
ServiceGetter(this, "Clipboard", "@mozilla.org/widget/clipboard;1", "nsIClipboard");
8
ServiceGetter(this, "Fixups", "@mozilla.org/docshell/urifixup;1", "nsIURIFixup");
10
const Transferable = new Components.Constructor("@mozilla.org/widget/transferable;1", "nsITransferable");
14
setNewGetter(this, "BatchGenerator", function() {
16
Components.utils.import("resource://dta/support/batchgen.jsm", bg);
17
return bg.BatchGenerator;
22
load: function DTA_load() {
24
this.ddDirectory = $("directory");
25
if (!this.ddDirectory.value) {
27
getDefaultDownloadsDirectory(function(path) {
28
tp.ddDirectory.value = path;
31
this.ddRenaming = $("renaming");
32
var address = $('address');
35
if (window.arguments) {
36
var a = window.arguments[0];
40
else if (typeof(a.url) == 'string') {
41
address.value = a.url;
43
else if (typeof(a.url) == 'object' && 'url' in a.url) {
44
// we've got a DTA.URL.
45
// In this case it is not safe to modify it because of encoding
47
address.value = a.url.usable;
48
// JS does not preserve types between windows (as each window gets an
50
// This hack makes our URL a DTA.URL again ;)
51
address._realURL = a.url;
52
address.readOnly = true;
53
$('batcheslabel').style.display = 'none';
54
$('batches').collapsed = true;
56
var referrer = DTA.isLinkOpenable(a.referrer) ? a.referrer : null;
59
referrer = decodeURIComponent(referrer);
61
$("URLref").value = referrer;
64
this.ddRenaming.value = a.mask;
67
$('description').value = a.description;
69
// check if there's some URL in clipboard
71
let trans = new Transferable();
73
trans.addDataFlavor("text/unicode");
74
Clipboard.getData(trans, Clipboard.kGlobalClipboard);
76
let str = {}, length = {};
77
trans.getTransferData(
84
.QueryInterface(Ci.nsISupportsString);
86
if (str.length && DTA.isLinkOpenable(str)) {
87
hash = DTA.getLinkPrintHash(str);
88
address.value = str.replace(/#.*$/, '');
94
Debug.log("Not able to gather data from the clipboard!", ex);
98
$('hash').value = hash;
103
Debug.log("load():", ex);
106
download: function DTA_download(start) {
110
// check the directory
111
var dir = this.ddDirectory.value.trim();
112
dir = this.ddDirectory.value = !!dir ? dir.addFinalSlash() : '';
113
if (!dir.length || !Utils.validateDir(dir)) {
114
errors.push('directory');
118
var mask = this.ddRenaming.value.trim();
119
mask = this.ddRenaming.value = mask || '';
121
errors.push('renaming');
124
var address = $('address');
125
var url = address.value;
126
if ('_realURL' in address) {
127
url = address._realURL;
132
throw new Components.Exception("Empty url");
134
let uri = Fixups.createFixupURI(url, 0);
136
url = decodeURIComponent(uri.spec);
141
var hash = DTA.getLinkPrintHash(url);
143
$('hash').value = hash;
145
url = url.replace(/#.*$/, '');
147
url = new DTA.URL(IOService.newURI(url, null, null));
150
errors.push('address');
155
if (!$('hash').isValid) {
159
hash = $('hash').value;
162
$('directory', 'renaming', 'address', 'hash').forEach(
165
if (e.hasAttribute('error')) {
166
e.removeAttribute('error');
174
$(e).setAttribute('error', 'true');
180
let num = DTA.currentSeries();
183
var batch = new BatchGenerator(url);
186
Debug.log("Cannot create batch", ex);
190
var rv = !('_realURL' in address) && batch.length > 1;
194
[batch.length, batch.parts, batch.first, batch.last]
196
if (batch.length > 1000) {
197
message += _('manytasks');
199
rv = Prompts.confirm(window, _('batchtitle'), message, _('batchtitle'), Prompts.CANCEL, _('single'));
206
let downloads = (function() {
207
let desc = $('description').value;
208
let ref = $('URLref').value;
210
let newURI = IOService.newURI;
212
function QueueItem(url) {
213
this.url = new URL(newURI(url, null, null));
215
this.url.hash = hash;
218
QueueItem.prototype = {
221
referrer: $('URLref').value,
228
let g = batch.getURLs();
231
yield new QueueItem(i);
236
return batch = [new QueueItem(url)];
239
DTA.sendLinksToManager(window, start, downloads);
241
DTA.incrementSeries();
242
Preferences.setExt("lastqueued", !start);
244
['ddRenaming', 'ddDirectory'].forEach(function(e) { Dialog[e].save(); });
250
browseDir: function DTA_browseDir() {
251
// let's check and create the directory
252
var newDir = Utils.askForDir(
253
this.ddDirectory.value,
254
_("validdestination")
257
this.ddDirectory.value = newDir;