forked from RichGuk/deluge-in-chrome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
torrent_status.html
349 lines (309 loc) · 12.8 KB
/
torrent_status.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Deluge - torrent information in Chrome</title>
<link rel="stylesheet" href="css/master.css" type="text/css" media="screen" />
</head>
<body>
<div id="disconnected">
<p>Currently unable to connect to the Deluge web interface. Please make sure that the extension is configured with the correct address, port and password.</p>
</div>
<div id="torrent-status" style="display: none">
<div id="header">
<div id="logo">
<a href="#" title="Go to Deluge WebUI!">
<span class="title">Deluge</span>
<span class="subtitle">Torrent information in Chrome</span>
</a>
</div>
<div id="global-information">
Total torrents: <span class="all">0</span><br />
Downloading: <span class="downloading">0</span>
Seeding: <span class="seeding">0</span><br />
Paused: <span class="paused">0</span>
Queued: <span class="queued">0</span>
</div>
<div class="clr"></div>
</div>
<div id="torrents">
<table class="nice-table" width="100%" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Size</th>
<th>Progress</th>
<th>Speed <span class="small">(D) - (U)</span></th>
<th>ETA</th>
<th class="right" width="77"> </th>
</tr>
<tbody>
</tbody>
</table>
</div>
<div id="key">
<span>Key:</span>
<img src="images/start.png" alt="S" /> Start
<img src="images/pause.png" alt="P" /> Pause
<img src="images/up.png" alt="U" /> Move up
<img src="images/down.png" alt="D" /> Move down
<img src="images/remove.png" alt="X" /> Delete options
<img src="images/cancel.png" alt="C" /> Cancel
<img src="images/trash.png" alt="DT" /> Delete torrent & data
<img src="images/file.png" alt="T" /> Delete just torrent file
</div>
</div> <!-- torrent-status -->
<script src="js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/Deluge.Formatters.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var ENDPOINT = localStorage.deluge_address + '/json';
// Used to store the timer.
var INTERVAL_UI = 0;
// Notice: This might not be in order (order being queue position).
var STORED_TORRENT_STATE = {};
var COUNTER = 0;
// How often should we refresh the torrent information.
var STATUS_REFRESH_INTERVAL = 2000;
/**
* Update the torrent state UI elements.
* Number of downloading torrents, seeding etc...
*
* @param {object} states The different type of states returned by Deluge.
*/
function update_torrent_states(states) {
for(var i in states) {
if (states.hasOwnProperty(i)) {
var id = states[i][0].toLowerCase();
var val = states[i][1];
$('#global-information .' + id).html(val);
}
}
}
/**
* Update the torrent listings table with the latest torrent information.
*
* @params {object} List of torrent information.
*/
function update_torrents(torrents) {
if(!torrents) {
console.log('deluge: missing torrent information.');
return false;
}
var sorted_torrents = [];
for(var torrent_id in torrents) {
if(torrents.hasOwnProperty(torrent_id)) {
sorted_torrents.push([torrent_id, torrents[torrent_id]]);
}
}
// Sort the torrents list based on queue position.
sorted_torrents = sorted_torrents.sort(function(a, b) {
var q1 = a[1].queue, q2 = b[1].queue;
if(q1 < q2) {
return -1;
}
if(q1 > q2) {
return 1;
}
return 0;
});
// Simple method of updating the table, lets just replace the tbody content.
var tbody = $('<tbody>');
for(var i = 0; i < sorted_torrents.length; i++) {
var id = sorted_torrents[i][0];
var torrent = sorted_torrents[i][1];
// Update / add to the stored torrents variable - so we can access
// the latest state of torrents in other functions.
eval('STORED_TORRENT_STATE._' + id + '= torrent');
var buttons = $('<td>').addClass('right buttons');
buttons.append('<a href="#delete" class="delete" title="Delete"><img src="images/remove.png" alt="X" /></a>');
if(torrent.state.toLowerCase() == 'paused') {
buttons.append('<a href="#resume" class="pause-resume" title="Resume"><img src="images/start.png" alt="P" /></a>');
} else {
buttons.append('<a href="#pause" class="pause-resume" title="Pause"><img src="images/pause.png" alt="P" /></a>');
}
buttons.append('<a href="#up" class="queue-up" title="Move up"><img src="images/up.png" alt="U" /></a>');
buttons.append('<a href="#down" class="queue-down" title="Move down"><img src="images/down.png" alt="D" /></a>');
if(torrent.is_auto_managed) {
buttons.append('<a href="#managed" class="auto-managed" title="Turn off auto managed"><img src="images/manage_active.png" alt="M" /></a>');
} else {
buttons.append('<a href="#managed" class="auto-managed" title="Turn on auto managed"><img src="images/manage.png" alt="M" /></a>');
}
var progress_bar = $('<div>').addClass('progress-bar');
var progress_bar_inner = $('<div>').addClass('progress');
var percent = (Math.round(torrent.progress * Math.pow(10,2)) / Math.pow(10, 2)) + '%';
// Set the width to the current progress of the torrent, so x percent.
progress_bar_inner.css('width', percent);
// Add some text based on state and percent like "Downloadng 80%"
progress_bar_inner.append($('<span>').html(torrent.state + ' ' + percent));
progress_bar.append(progress_bar_inner);
// Work out the torrent size.
var torrent_size = fsize(torrent.total_size);
var torrent_speed = fspeed(torrent.download_payload_rate) + ' - ' + fspeed(torrent.upload_payload_rate);
var queue = torrent.queue + 1;
if(queue <= 0) {
queue = '';
}
var tr = $('<tr>').attr('id', id);
tr.append($('<td>').addClass('left').html(queue));
tr.append($('<td>').html(torrent.name).addClass('name'));
tr.append($('<td>').html(torrent_size));
tr.append($('<td>').html(progress_bar));
tr.append($('<td>').html(torrent_speed));
tr.append($('<td>').html(ftime(torrent.eta)));
tr.append(buttons);
tbody.append(tr);
}
// Now update the old tbody with the new content.
$('#torrents table tbody').replaceWith(tbody);
}
/**
* Fetch latest torrent information from Deluge and update various
* variables and UI elements.
*/
function update_ui() {
COUNTER++;
$.post(ENDPOINT,
JSON.stringify({method: 'web.update_ui',
params: [[
'queue', 'name', 'total_size', 'state', 'progress',
'download_payload_rate', 'upload_payload_rate', 'eta',
'ratio', 'is_auto_managed'
], {}], id: COUNTER}),
function(obj, status) {
if(!obj || obj.error !== null || !obj.result) {
console.log('deluge: error fetching torrent status information!');
return;
}
update_torrent_states(obj.result.filters.state);
update_torrents(obj.result.torrents);
}, 'json');
}
$('.buttons .delete').live('click', function() {
// Stop the table refresh, so we can show the delete options for
// this torrent.
clearInterval(INTERVAL_UI);
var parent = $(this).parents('tr');
var parent_td = $(this).parent('td');
var new_elm = $('<div>');
new_elm.addClass('delete-options');
parent_td.append(new_elm);
new_elm.animate({width: '100px'}, 'fast', function() {
var tmp = $(this);
tmp.append('<a href="#cancel" title="Cancel" rel="cancel"><img src="images/cancel.png" alt="C" /></a>');
tmp.append('<a href="#delete-data" title="Delete torrent AND data" rel="data"><img src="images/trash.png" alt="TD" /></a>');
tmp.append('<a href="#delete-torrent" title="Just delete torrent file" rel="torrent"><img src="images/file.png" alt="T" /></a>');
});
});
$('.buttons .delete-options a').live('click', function() {
var elm = $(this);
var parent = $(this).parents('tr');
var torrent_id = parent.attr('id');
var action = $(this).attr('rel') || 'cancel';
// If canceling remove overlay and resume refresh now and return.
if(action == 'cancel') {
$('.delete-options').fadeOut('fast', function() {
INTERVAL_UI = setInterval('update_ui()', STATUS_REFRESH_INTERVAL);
// Force an update.
update_ui();
});
return;
}
var del_data = (action == 'data') ? true : false;
COUNTER++;
$.post(ENDPOINT,
JSON.stringify({
method: 'core.remove_torrent',
params: [torrent_id, del_data],
id: COUNTER
}),
function(obj, status) {
// Remove buttons, resume refresh.
$('.delete-options').fadeOut('fast', function() {
INTERVAL_UI = setInterval('update_ui()', STATUS_REFRESH_INTERVAL);
// Force an update.
update_ui();
});
},'json');
});
$('.buttons .auto-managed').live('click', function() {
var parent = $(this).parents('tr');
var torrent_id = parent.attr('id');
var torrent = eval('STORED_TORRENT_STATE._' + torrent_id);
var auto_managed = !torrent.is_auto_managed;
COUNTER++;
$.post(ENDPOINT,
JSON.stringify({method: 'core.set_torrent_auto_managed', params: [
torrent_id,
auto_managed
], id: COUNTER}),
function(obj, status) {
update_ui();
},'json');
});
$('.buttons .pause-resume').live('click', function() {
var parent = $(this).parents('tr');
var torrent_id = parent.attr('id');
var torrent = eval('STORED_TORRENT_STATE._' + torrent_id);
var _method = torrent.state == 'Paused' ? 'core.resume_torrent' : 'core.pause_torrent';
COUNTER++;
$.post(ENDPOINT,
JSON.stringify({method: _method, params: [[torrent_id]], id: COUNTER}),
function(obj, status) {
// Just update the UI, should reflect any changes.
update_ui();
}, 'json');
return false;
});
$('.buttons .queue-up').live('click', function() {
var parent = $(this).parents('tr');
var torrent_id = parent.attr('id');
COUNTER++;
$.post(ENDPOINT,
JSON.stringify({method: 'core.queue_up', params: [[
torrent_id
]], id: COUNTER}),
update_ui,
'json');
});
$('.buttons .queue-down').live('click', function() {
var parent = $(this).parents('tr');
var torrent_id = parent.attr('id');
COUNTER++;
$.post(ENDPOINT,
JSON.stringify({method: 'core.queue_down', params: [[
torrent_id
]], id: COUNTER}),
update_ui,
'json');
});
$('#logo a').click(function() {
// TODO: Add a check to make sure it's not already open in a tab.
// Switch to that tab if it is.
chrome.tabs.create({url: localStorage.deluge_address});
window.close();
return false;
});
$(document).ready(function() {
// Call the status check method in the background page on load.
var background = chrome.extension.getBackgroundPage();
background.deluge_status_check({
success: function(obj) {
background.enable_icon();
// Make sure we have the latest UI.
update_ui();
// And we'll udate the UI every x seconds too.
if(!INTERVAL_UI) {
INTERVAL_UI = setInterval('update_ui()', STATUS_REFRESH_INTERVAL);
}
},
error: function(obj) {
background.disable_icon();
// No need to pointlessly update the UI.
clearInterval(INTERVAL_UI);
}
});
});
</script>
</body>
</html>