~ubuntu-branches/debian/stretch/downthemall/stretch

« back to all changes in this revision

Viewing changes to chrome/chrome.jar!/content/dta/addurl.js

  • Committer: Package Import Robot
  • Author(s): Michael Meskes
  • Date: 2016-09-21 13:33:55 UTC
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: package-import@ubuntu.com-20160921133355-ylst3mzzo82mghyw
Tags: upstream-3.0.6
ImportĀ upstreamĀ versionĀ 3.0.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* You may find the license in the LICENSE file */
2
 
 
3
 
let Prompts = {};
4
 
Components.utils.import('resource://dta/prompts.jsm', Prompts);
5
 
Components.utils.import('resource://dta/version.jsm');
6
 
 
7
 
ServiceGetter(this, "Clipboard", "@mozilla.org/widget/clipboard;1", "nsIClipboard");
8
 
ServiceGetter(this, "Fixups", "@mozilla.org/docshell/urifixup;1", "nsIURIFixup");
9
 
 
10
 
const Transferable = new Components.Constructor("@mozilla.org/widget/transferable;1", "nsITransferable");
11
 
 
12
 
var dropDowns = {};
13
 
 
14
 
setNewGetter(this, "BatchGenerator", function() {
15
 
        let bg = {};
16
 
        Components.utils.import("resource://dta/support/batchgen.jsm", bg);
17
 
        return bg.BatchGenerator;
18
 
});
19
 
 
20
 
 
21
 
var Dialog = {
22
 
        load: function DTA_load() {
23
 
                try {
24
 
                        this.ddDirectory = $("directory");
25
 
                        if (!this.ddDirectory.value) {
26
 
                                let tp = this;
27
 
                                getDefaultDownloadsDirectory(function(path) {
28
 
                                        tp.ddDirectory.value = path;
29
 
                                });
30
 
                        }                       
31
 
                        this.ddRenaming = $("renaming");                        
32
 
                        var address = $('address');
33
 
                        
34
 
                        var hash = null;
35
 
                        if (window.arguments) {
36
 
                                var a = window.arguments[0];
37
 
                                var url = a.url;
38
 
                                if (!('url' in a))
39
 
                                        ;
40
 
                                else if (typeof(a.url) == 'string') {
41
 
                                        address.value = a.url;
42
 
                                }
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
46
 
                                        // issues.
47
 
                                        address.value = a.url.usable;
48
 
                                        // JS does not preserve types between windows (as each window gets an
49
 
                                        // own sandbox)
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;
55
 
                                }
56
 
                                var referrer = DTA.isLinkOpenable(a.referrer) ? a.referrer : null;
57
 
                                if (referrer) {
58
 
                                        try {
59
 
                                                referrer = decodeURIComponent(referrer);
60
 
                                        } catch (ex) {}
61
 
                                        $("URLref").value        = referrer;
62
 
                                }
63
 
                                if (a.mask) {
64
 
                                        this.ddRenaming.value = a.mask;
65
 
                                }
66
 
                                hash = a.url.hash;
67
 
                                $('description').value = a.description;
68
 
                        }
69
 
                        // check if there's some URL in clipboard
70
 
                        else {
71
 
                                let trans = new Transferable();
72
 
                                try {
73
 
                                        trans.addDataFlavor("text/unicode");
74
 
                                        Clipboard.getData(trans, Clipboard.kGlobalClipboard);
75
 
                                        
76
 
                                        let str = {}, length = {};
77
 
                                        trans.getTransferData(
78
 
                                                "text/unicode",
79
 
                                                str,
80
 
                                                length
81
 
                                        );
82
 
                                        if (length.value) {
83
 
                                                str = str.value
84
 
                                                        .QueryInterface(Ci.nsISupportsString);
85
 
                                                str = str.data;
86
 
                                                if (str.length && DTA.isLinkOpenable(str)) {
87
 
                                                        hash = DTA.getLinkPrintHash(str);
88
 
                                                        address.value = str.replace(/#.*$/, '');
89
 
                                                        address.select();
90
 
                                                }
91
 
                                        }
92
 
                                }
93
 
                                catch (ex) {
94
 
                                        Debug.log("Not able to gather data from the clipboard!", ex);
95
 
                                }
96
 
                        }
97
 
                        if (hash) {
98
 
                                $('hash').value = hash;
99
 
                        }
100
 
                        sizeToContent();
101
 
                }
102
 
                catch(ex) {
103
 
                        Debug.log("load():", ex);
104
 
                }               
105
 
        },
106
 
        download: function DTA_download(start) {
107
 
                
108
 
                var errors = [];
109
 
                
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');
115
 
                }
116
 
                
117
 
                // check mask
118
 
                var mask = this.ddRenaming.value.trim();
119
 
                mask = this.ddRenaming.value = mask || '';
120
 
                if (!mask.length) {
121
 
                        errors.push('renaming');
122
 
                }
123
 
                
124
 
                var address = $('address');
125
 
                var url = address.value;
126
 
                if ('_realURL' in address) {
127
 
                        url = address._realURL;
128
 
                }
129
 
                else {
130
 
                        try {
131
 
                                if (url == '') {
132
 
                                        throw new Components.Exception("Empty url");
133
 
                                }
134
 
                                let uri = Fixups.createFixupURI(url, 0);
135
 
                                try {
136
 
                                        url = decodeURIComponent(uri.spec);
137
 
                                }
138
 
                                catch (ex) {
139
 
                                        url = uri.spec;
140
 
                                }
141
 
                                var hash = DTA.getLinkPrintHash(url);
142
 
                                if (hash) {
143
 
                                        $('hash').value = hash;
144
 
                                }
145
 
                                url = url.replace(/#.*$/, '');
146
 
                                address.value = url;
147
 
                                url = new DTA.URL(IOService.newURI(url, null, null));                           
148
 
                        }
149
 
                        catch (ex) {
150
 
                                errors.push('address');
151
 
                        }
152
 
                }
153
 
                
154
 
                var hash = null;
155
 
                if (!$('hash').isValid) {
156
 
                        errors.push('hash');
157
 
                }
158
 
                else {
159
 
                        hash = $('hash').value;
160
 
                }
161
 
 
162
 
                $('directory', 'renaming', 'address', 'hash').forEach(
163
 
                        function(e) {
164
 
                                // reset the styles
165
 
                                if (e.hasAttribute('error')) {
166
 
                                        e.removeAttribute('error');
167
 
                                }
168
 
                        }
169
 
                );
170
 
                
171
 
                if (errors.length) {
172
 
                        errors.forEach(
173
 
                                function(e) {
174
 
                                        $(e).setAttribute('error', 'true');
175
 
                                }
176
 
                        );
177
 
                        return false;
178
 
                }               
179
 
 
180
 
                let num = DTA.currentSeries();
181
 
                
182
 
                try {
183
 
                        var batch = new BatchGenerator(url);
184
 
                }
185
 
                catch (ex) {
186
 
                        Debug.log("Cannot create batch", ex);
187
 
                        return;
188
 
                }
189
 
        
190
 
                var rv = !('_realURL' in address) && batch.length > 1;
191
 
                if (rv) {
192
 
                        var message = _(
193
 
                                'tasks',
194
 
                                [batch.length, batch.parts, batch.first, batch.last]
195
 
                        );
196
 
                        if (batch.length > 1000) {
197
 
                                message += _('manytasks');
198
 
                        }
199
 
                        rv = Prompts.confirm(window, _('batchtitle'), message, _('batchtitle'), Prompts.CANCEL, _('single'));
200
 
                        if (rv == 1) {
201
 
                                return false;
202
 
                        }
203
 
                        rv = rv == 0;
204
 
                }
205
 
                
206
 
                let downloads = (function() {
207
 
                        let desc = $('description').value;
208
 
                        let ref = $('URLref').value;
209
 
                        let URL = DTA.URL;
210
 
                        let newURI = IOService.newURI;
211
 
 
212
 
                        function QueueItem(url) {
213
 
                                this.url = new URL(newURI(url, null, null));
214
 
                                if (hash) {
215
 
                                        this.url.hash = hash;
216
 
                                }
217
 
                        }
218
 
                        QueueItem.prototype = {
219
 
                                title: '',
220
 
                                description: desc,
221
 
                                referrer: $('URLref').value,
222
 
                                numIstance: num,
223
 
                                mask: mask,
224
 
                                dirSave: dir
225
 
                        };
226
 
                
227
 
                        if (rv) {
228
 
                                let g = batch.getURLs();
229
 
                                return (function() {
230
 
                                        for (let i in g) {
231
 
                                                yield new QueueItem(i);
232
 
                                        }
233
 
                                })();
234
 
                        }
235
 
 
236
 
                        return batch = [new QueueItem(url)];
237
 
                })();
238
 
                
239
 
                DTA.sendLinksToManager(window, start, downloads);
240
 
 
241
 
                DTA.incrementSeries();
242
 
                Preferences.setExt("lastqueued", !start);
243
 
        
244
 
                ['ddRenaming', 'ddDirectory'].forEach(function(e) { Dialog[e].save(); });
245
 
                
246
 
                self.close();
247
 
                
248
 
                return false;
249
 
        },
250
 
        browseDir: function DTA_browseDir() {
251
 
                // let's check and create the directory
252
 
                var newDir = Utils.askForDir(
253
 
                        this.ddDirectory.value,
254
 
                        _("validdestination")
255
 
                );
256
 
                if (newDir) {
257
 
                        this.ddDirectory.value = newDir;
258
 
                }
259
 
        }
260
 
}