-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
105 lines (88 loc) · 2.56 KB
/
main.js
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
require('Common');
var throttle = require('lodash.throttle');
var WebTorrent = require('webtorrent');
var TorrentDownloadTable = require('./download-view.js');
var wt = new WebTorrent();
var win = new Window();
var settings = {};
// Error Logging
process.on('uncaughtException', function(err) {
console.log(err, err.stack);
});
settings.downloadPath = System.home + '/Downloads';
win.visible = true;
win.title = 'Tint Torrent App';
var toolbar = new Toolbar();
win.toolbar = toolbar;
var addTorrent = new ToolbarItem();
addTorrent.tooltip = 'Add torrent files to download';
addTorrent.image = 'add';
addTorrent.addEventListener('click', function() {
var fileDialog = new FileDialog('open');
fileDialog.allowFileTypes = ['torrent'];
fileDialog.open(win);
fileDialog.on('select', function() {
var torrentFile = fileDialog.selection[0];
wt.download(torrentFile, { path: settings.downloadPath }, function(torrent) {
logview.addTorrent(torrent, settings.downloadPath + '/' + torrent.name);
if (!settings.torrents) {
settings.torrents = {};
settings.torrents[torrent.name] = torrentFile;
} else {
settings.torrents[torrent.name] = torrentFile;
}
});
});
});
var removeTorrent = new ToolbarItem({
tooltip: 'Remove Selected Torrent From The App',
image: 'remove'
});
removeTorrent.addEventListener('click', function() {
logview.removeTorrent();
});
toolbar.appendChild([
addTorrent,
removeTorrent
]);
var logview = new TorrentDownloadTable({form: {visible: true, top: 50}});
win.appendChild(logview.form);
/*
* Torrent Instance
*/
var update = throttle(function(torrent) {
logview.updateProgress(torrent);
}, 200);
wt.on('torrent', function(torrent) {
torrent.on('download', function(){
update(torrent);
});
torrent.on('done', function() {
update.flush();
update(torrent);
Notification.requestPermission(function(response) {
if (response) {
var notif = new Notification();
notif.title = 'Torrent has finished downloading.';
notif.subtitle = torrent.name + ' is complete';
notif.text = torrent.name + ' is complete';
notif.buttonLabel = 'Show';
notif.addEventListener('click', function() {
System.openFile(settings.downloadPath + '/' + torrent.name);
});
notif.dispatch();
}
});
});
});
/*
* If torrents exist from previous session re-start them
*/
// var prevTor = settings.torrents;
// if (prevTor) {
// for (var i in prevTor) {
// wt.download(prevTor[i], { path: settings.downloadPath }, function(torrent) {
// logview.addTorrent(torrent, settings.downloadPath + '/' + torrent.name);
// });
// }
// }