2008-07-10 23:57:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright © Dave Perrett and Malcolm Jarvis
|
|
|
|
* This code is licensed under the GPL version 2.
|
|
|
|
* For details, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
*
|
|
|
|
* Class Torrent
|
|
|
|
*/
|
|
|
|
|
2009-05-24 19:34:54 +00:00
|
|
|
function Torrent( transferListParent, fileListParent, controller, data) {
|
|
|
|
this.initialize( transferListParent, fileListParent, controller, data);
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Constants
|
|
|
|
Torrent._StatusWaitingToCheck = 1;
|
|
|
|
Torrent._StatusChecking = 2;
|
|
|
|
Torrent._StatusDownloading = 4;
|
|
|
|
Torrent._StatusSeeding = 8;
|
|
|
|
Torrent._StatusPaused = 16;
|
|
|
|
Torrent._InfiniteTimeRemaining = 215784000; // 999 Hours - may as well be infinite
|
|
|
|
|
|
|
|
Torrent.prototype =
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
2009-05-24 19:34:54 +00:00
|
|
|
initialize: function( transferListParent, fileListParent, controller, data) {
|
2009-05-22 22:45:09 +00:00
|
|
|
this._id = data.id;
|
|
|
|
this._is_private = data.isPrivate;
|
|
|
|
this._hashString = data.hashString;
|
|
|
|
this._date = data.addedDate;
|
|
|
|
this._size = data.totalSize;
|
|
|
|
this._tracker = data.announceURL;
|
|
|
|
this._comment = data.comment;
|
|
|
|
this._creator = data.creator;
|
|
|
|
this._creator_date = data.dateCreated;
|
|
|
|
this._sizeWhenDone = data.sizeWhenDone;
|
|
|
|
this._name = data.name;
|
|
|
|
this._name_lc = this._name.toLowerCase( );
|
2009-05-23 20:39:55 +00:00
|
|
|
this._file_model = [ ];
|
|
|
|
this._file_view = [ ];
|
2009-05-22 22:45:09 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
// Create a new <li> element
|
2009-05-25 13:31:03 +00:00
|
|
|
var top_e = document.createElement( 'li' );
|
|
|
|
top_e.className = 'torrent';
|
|
|
|
top_e.id = 'torrent_' + data.id;
|
2009-05-25 15:46:25 +00:00
|
|
|
top_e._torrent = this;
|
2009-05-25 13:31:03 +00:00
|
|
|
var element = $(top_e);
|
2008-07-10 23:57:46 +00:00
|
|
|
element._torrent = this;
|
|
|
|
this._element = element;
|
|
|
|
this._controller = controller;
|
|
|
|
controller._rows.push( element );
|
|
|
|
|
|
|
|
// Create the 'name' <div>
|
2009-05-23 15:05:58 +00:00
|
|
|
var e = document.createElement( 'div' );
|
|
|
|
e.className = 'torrent_name';
|
2009-05-25 13:31:03 +00:00
|
|
|
top_e.appendChild( e );
|
2008-07-10 23:57:46 +00:00
|
|
|
element._name_container = e;
|
|
|
|
|
|
|
|
// Create the 'progress details' <div>
|
2009-05-23 15:05:58 +00:00
|
|
|
e = document.createElement( 'div' );
|
|
|
|
e.className = 'torrent_progress_details';
|
2009-05-25 13:31:03 +00:00
|
|
|
top_e.appendChild( e );
|
2008-07-10 23:57:46 +00:00
|
|
|
element._progress_details_container = e;
|
|
|
|
|
|
|
|
// Create the 'in progress' bar
|
2009-05-23 15:05:58 +00:00
|
|
|
e = document.createElement( 'div' );
|
|
|
|
e.className = 'torrent_progress_bar incomplete';
|
|
|
|
e.style.width = '0%';
|
2009-05-25 13:31:03 +00:00
|
|
|
top_e.appendChild( e );
|
2008-07-10 23:57:46 +00:00
|
|
|
element._progress_complete_container = e;
|
|
|
|
|
|
|
|
// Create the 'incomplete' bar (initially hidden)
|
2009-05-23 15:05:58 +00:00
|
|
|
e = document.createElement( 'div' );
|
|
|
|
e.className = 'torrent_progress_bar incomplete';
|
|
|
|
e.style.display = 'none';
|
2009-05-25 13:31:03 +00:00
|
|
|
top_e.appendChild( e );
|
2008-07-10 23:57:46 +00:00
|
|
|
element._progress_incomplete_container = e;
|
|
|
|
|
|
|
|
// Add the pause/resume button - don't specify the
|
|
|
|
// image or alt text until the 'refresh()' function
|
|
|
|
// (depends on torrent state)
|
2009-05-23 15:05:58 +00:00
|
|
|
var image = document.createElement( 'div' );
|
|
|
|
image.className = 'torrent_pause';
|
|
|
|
e = document.createElement( 'a' );
|
|
|
|
e.appendChild( image );
|
2009-05-25 13:31:03 +00:00
|
|
|
top_e.appendChild( e );
|
2008-07-10 23:57:46 +00:00
|
|
|
element._pause_resume_button_image = image;
|
2009-06-23 01:52:20 +00:00
|
|
|
if (!iPhone) $(e).bind('click', function(e) { element._torrent.clickPauseResumeButton(e); });
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Create the 'peer details' <div>
|
2009-05-23 15:05:58 +00:00
|
|
|
e = document.createElement( 'div' );
|
|
|
|
e.className = 'torrent_peer_details';
|
2009-05-25 13:31:03 +00:00
|
|
|
top_e.appendChild( e );
|
2008-07-10 23:57:46 +00:00
|
|
|
element._peer_details_container = e;
|
2009-03-14 21:33:08 +00:00
|
|
|
|
2009-05-25 13:31:03 +00:00
|
|
|
// Set the torrent click observer
|
2009-06-23 01:52:20 +00:00
|
|
|
element.bind('click', function(e){ element._torrent.clickTorrent(e) });
|
2009-05-25 13:31:03 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
// Safari hack - first torrent needs to be moved down for some reason. Seems to be ok when
|
|
|
|
// using <li>'s in straight html, but adding through the DOM gets a bit odd.
|
|
|
|
if ($.browser.safari)
|
|
|
|
this._element.css('margin-top', '7px');
|
2009-05-22 22:45:09 +00:00
|
|
|
|
2009-05-24 19:34:54 +00:00
|
|
|
this.initializeTorrentFilesInspectorGroup( fileListParent );
|
2009-05-23 20:39:55 +00:00
|
|
|
|
2009-05-24 15:48:18 +00:00
|
|
|
if( data.files ) {
|
|
|
|
for( var i=0, row; row=data.files[i]; ++i ) {
|
|
|
|
this._file_model[i] = {
|
|
|
|
'index': i,
|
|
|
|
'torrent': this,
|
|
|
|
'length': row.length,
|
|
|
|
'name': row.name
|
|
|
|
};
|
|
|
|
}
|
2009-03-14 21:33:08 +00:00
|
|
|
}
|
2009-05-22 22:45:09 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
// Update all the labels etc
|
|
|
|
this.refresh(data);
|
2009-05-24 15:48:18 +00:00
|
|
|
|
|
|
|
// insert the element
|
2009-05-25 13:31:03 +00:00
|
|
|
transferListParent.appendChild(top_e);
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
2009-03-14 21:33:08 +00:00
|
|
|
|
2009-05-24 19:34:54 +00:00
|
|
|
initializeTorrentFilesInspectorGroup: function( fileListParent ) {
|
2009-05-23 18:09:56 +00:00
|
|
|
var e = document.createElement( 'ul' );
|
|
|
|
e.className = 'inspector_torrent_file_list inspector_group';
|
|
|
|
e.style.display = 'none';
|
2009-05-24 19:34:54 +00:00
|
|
|
fileListParent.appendChild( e );
|
2009-05-23 18:09:56 +00:00
|
|
|
this._fileList = e;
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fileList: function() {
|
2009-05-23 18:09:56 +00:00
|
|
|
return $(this._fileList);
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
/*--------------------------------------------
|
|
|
|
*
|
|
|
|
* S E T T E R S / G E T T E R S
|
|
|
|
*
|
|
|
|
*--------------------------------------------*/
|
|
|
|
|
|
|
|
/* Return the DOM element for this torrent (a <LI> element) */
|
|
|
|
element: function() {
|
|
|
|
return this._element;
|
|
|
|
},
|
|
|
|
|
|
|
|
setElement: function( element ) {
|
|
|
|
this._element = element;
|
|
|
|
element._torrent = this;
|
2009-05-25 15:46:25 +00:00
|
|
|
element[0]._torrent = this;
|
2008-07-10 23:57:46 +00:00
|
|
|
this.refreshHTML( );
|
|
|
|
},
|
|
|
|
|
|
|
|
activity: function() { return this._download_speed + this._upload_speed; },
|
|
|
|
comment: function() { return this._comment; },
|
|
|
|
completed: function() { return this._completed; },
|
|
|
|
creator: function() { return this._creator; },
|
|
|
|
dateAdded: function() { return this._date; },
|
|
|
|
downloadSpeed: function() { return this._download_speed; },
|
|
|
|
downloadTotal: function() { return this._download_total; },
|
|
|
|
errorMessage: function() { return this._error_message; },
|
|
|
|
hash: function() { return this._hashString; },
|
|
|
|
id: function() { return this._id; },
|
|
|
|
isActive: function() { return this.state() != Torrent._StatusPaused; },
|
|
|
|
isDownloading: function() { return this.state() == Torrent._StatusDownloading; },
|
|
|
|
isSeeding: function() { return this.state() == Torrent._StatusSeeding; },
|
|
|
|
name: function() { return this._name; },
|
2008-07-25 14:34:25 +00:00
|
|
|
peersSendingToUs: function() { return this._peers_sending_to_us; },
|
|
|
|
peersGettingFromUs: function() { return this._peers_getting_from_us; },
|
2008-07-16 14:35:58 +00:00
|
|
|
getPercentDone: function() {
|
|
|
|
if( !this._sizeWhenDone ) return 1.0;
|
2008-09-19 16:17:35 +00:00
|
|
|
if( !this._leftUntilDone ) return 1.0;
|
2009-03-14 21:33:08 +00:00
|
|
|
return ( this._sizeWhenDone - this._leftUntilDone ) / this._sizeWhenDone;
|
2008-07-16 14:35:58 +00:00
|
|
|
},
|
|
|
|
getPercentDoneStr: function() {
|
2009-04-24 03:21:15 +00:00
|
|
|
return Math.floor(100 * Math.ratio( 100 * ( this._sizeWhenDone - this._leftUntilDone ),
|
|
|
|
this._sizeWhenDone )) / 100;
|
2008-07-16 14:35:58 +00:00
|
|
|
},
|
2008-07-10 23:57:46 +00:00
|
|
|
size: function() { return this._size; },
|
|
|
|
state: function() { return this._state; },
|
|
|
|
stateStr: function() {
|
|
|
|
switch( this.state() ) {
|
|
|
|
case Torrent._StatusSeeding: return 'Seeding';
|
|
|
|
case Torrent._StatusDownloading: return 'Downloading';
|
|
|
|
case Torrent._StatusPaused: return 'Paused';
|
|
|
|
case Torrent._StatusChecking: return 'Verifying local data';
|
|
|
|
case Torrent._StatusWaitingToCheck: return 'Waiting to verify';
|
|
|
|
default: return 'error';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
swarmSpeed: function() { return this._swarm_speed; },
|
|
|
|
totalLeechers: function() { return this._total_leechers; },
|
|
|
|
totalSeeders: function() { return this._total_seeders; },
|
|
|
|
uploadSpeed: function() { return this._upload_speed; },
|
|
|
|
uploadTotal: function() { return this._upload_total; },
|
2009-05-25 00:41:44 +00:00
|
|
|
showFileList: function() {
|
|
|
|
if(this.fileList().is(':visible'))
|
|
|
|
return;
|
|
|
|
this.ensureFileListExists();
|
|
|
|
this.refreshFileView();
|
|
|
|
this.fileList().show();
|
|
|
|
},
|
2009-04-29 11:59:29 +00:00
|
|
|
hideFileList: function() { this.fileList().hide(); },
|
2009-03-14 21:33:08 +00:00
|
|
|
|
2009-05-25 13:31:03 +00:00
|
|
|
/*--------------------------------------------
|
|
|
|
*
|
|
|
|
* E V E N T F U N C T I O N S
|
|
|
|
*
|
|
|
|
*--------------------------------------------*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a click event on this torrent
|
|
|
|
*/
|
|
|
|
clickTorrent: function( event )
|
|
|
|
{
|
|
|
|
// Prevents click carrying to parent element
|
|
|
|
// which deselects all on click
|
|
|
|
event.stopPropagation();
|
2009-06-23 01:52:20 +00:00
|
|
|
var torrent = this;
|
2009-05-25 13:31:03 +00:00
|
|
|
|
|
|
|
// 'Apple' button emulation on PC :
|
|
|
|
// Need settable meta-key and ctrl-key variables for mac emulation
|
|
|
|
var meta_key = event.metaKey;
|
|
|
|
var ctrl_key = event.ctrlKey;
|
|
|
|
if (event.ctrlKey && navigator.appVersion.toLowerCase().indexOf("mac") == -1) {
|
|
|
|
meta_key = true;
|
|
|
|
ctrl_key = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift-Click - Highlight a range between this torrent and the last-clicked torrent
|
|
|
|
if (iPhone) {
|
2009-06-23 01:52:20 +00:00
|
|
|
if ( torrent.isSelected() )
|
|
|
|
torrent._controller.showInspector();
|
2009-05-25 13:31:03 +00:00
|
|
|
torrent._controller.setSelectedTorrent( torrent, true );
|
|
|
|
|
|
|
|
} else if (event.shiftKey) {
|
|
|
|
torrent._controller.selectRange( torrent, true );
|
|
|
|
// Need to deselect any selected text
|
|
|
|
window.focus();
|
|
|
|
|
|
|
|
// Apple-Click, not selected
|
|
|
|
} else if (!torrent.isSelected() && meta_key) {
|
|
|
|
torrent._controller.selectTorrent( torrent, true );
|
|
|
|
|
|
|
|
// Regular Click, not selected
|
|
|
|
} else if (!torrent.isSelected()) {
|
|
|
|
torrent._controller.setSelectedTorrent( torrent, true );
|
|
|
|
|
|
|
|
// Apple-Click, selected
|
|
|
|
} else if (torrent.isSelected() && meta_key) {
|
|
|
|
torrent._controller.deselectTorrent( torrent, true );
|
|
|
|
|
|
|
|
// Regular Click, selected
|
|
|
|
} else if (torrent.isSelected()) {
|
|
|
|
torrent._controller.setSelectedTorrent( torrent, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent._controller.setLastTorrentClicked(torrent);
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process a click event on the pause/resume button
|
|
|
|
*/
|
|
|
|
clickPauseResumeButton: function( event )
|
|
|
|
{
|
|
|
|
// prevent click event resulting in selection of torrent
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
// either stop or start the torrent
|
2009-06-23 01:52:20 +00:00
|
|
|
var torrent = this;
|
2009-05-25 13:31:03 +00:00
|
|
|
if( torrent.isActive( ) )
|
|
|
|
torrent._controller.stopTorrent( torrent );
|
|
|
|
else
|
|
|
|
torrent._controller.startTorrent( torrent );
|
|
|
|
},
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
/*--------------------------------------------
|
|
|
|
*
|
|
|
|
* I N T E R F A C E F U N C T I O N S
|
|
|
|
*
|
|
|
|
*--------------------------------------------*/
|
|
|
|
|
|
|
|
refresh: function(data) {
|
|
|
|
this.refreshData( data );
|
|
|
|
this.refreshHTML( );
|
|
|
|
},
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Refresh display
|
|
|
|
*/
|
2009-03-14 21:33:08 +00:00
|
|
|
refreshData: function(data) {
|
2008-07-25 14:34:25 +00:00
|
|
|
this._completed = data.haveUnchecked + data.haveValid;
|
|
|
|
this._verified = data.haveValid;
|
2009-05-19 13:19:51 +00:00
|
|
|
this._leftUntilDone = data.leftUntilDone;
|
2008-07-25 14:34:25 +00:00
|
|
|
this._download_total = data.downloadedEver;
|
|
|
|
this._upload_total = data.uploadedEver;
|
|
|
|
this._download_speed = data.rateDownload;
|
|
|
|
this._upload_speed = data.rateUpload;
|
2008-09-28 16:11:12 +00:00
|
|
|
this._peers_connected = data.peersConnected;
|
2008-07-25 14:34:25 +00:00
|
|
|
this._peers_getting_from_us = data.peersGettingFromUs;
|
|
|
|
this._peers_sending_to_us = data.peersSendingToUs;
|
|
|
|
this._error = data.error;
|
|
|
|
this._error_message = data.errorString;
|
|
|
|
this._eta = data.eta;
|
|
|
|
this._swarm_speed = data.swarmSpeed;
|
|
|
|
this._total_leechers = Math.max( 0, data.leechers );
|
|
|
|
this._total_seeders = Math.max( 0, data.seeders );
|
|
|
|
this._state = data.status;
|
2009-05-23 20:39:55 +00:00
|
|
|
|
|
|
|
if (data.fileStats)
|
|
|
|
this.refreshFileModel( data );
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
2009-05-23 20:39:55 +00:00
|
|
|
refreshFileModel: function(data) {
|
|
|
|
for( var i=0; i<data.fileStats.length; ++i ) {
|
|
|
|
var src = data.fileStats[i];
|
|
|
|
var tgt = this._file_model[i];
|
|
|
|
if( !tgt )
|
|
|
|
tgt = this._file_model[i] = { };
|
|
|
|
tgt.wanted = src.wanted;
|
|
|
|
tgt.priority = src.priority;
|
|
|
|
tgt.bytesCompleted = src.bytesCompleted;
|
2009-05-22 22:45:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2009-03-14 21:33:08 +00:00
|
|
|
refreshHTML: function() {
|
2009-05-25 01:18:51 +00:00
|
|
|
var c;
|
|
|
|
var e;
|
2008-07-10 23:57:46 +00:00
|
|
|
var progress_details;
|
|
|
|
var peer_details;
|
|
|
|
var root = this._element;
|
2008-07-16 14:35:58 +00:00
|
|
|
var MaxBarWidth = 100; // reduce this to make the progress bar shorter (%)
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2009-05-23 15:05:58 +00:00
|
|
|
setInnerHTML( root._name_container, this._name );
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Add the progress bar
|
2008-07-16 14:35:58 +00:00
|
|
|
var notDone = this._leftUntilDone > 0;
|
2008-08-10 19:24:26 +00:00
|
|
|
|
|
|
|
// Fix for situation
|
|
|
|
// when a verifying/downloading torrent gets state seeding
|
2009-05-25 01:18:51 +00:00
|
|
|
if( this._state === Torrent._StatusSeeding )
|
2008-08-10 19:24:26 +00:00
|
|
|
notDone = false ;
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
if( notDone )
|
|
|
|
{
|
|
|
|
var eta = '';
|
|
|
|
|
|
|
|
if( this.isActive( ) )
|
|
|
|
{
|
|
|
|
eta = '-';
|
|
|
|
if (this._eta < 0 || this._eta >= Torrent._InfiniteTimeRemaining )
|
|
|
|
eta += 'remaining time unknown';
|
|
|
|
else
|
|
|
|
eta += Math.formatSeconds(this._eta) + ' remaining';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the 'progress details' label
|
|
|
|
// Eg: '101 MB of 631 MB (16.02%) - 2 hr remaining'
|
2009-05-25 01:18:51 +00:00
|
|
|
c = Math.formatBytes( this._sizeWhenDone - this._leftUntilDone );
|
|
|
|
c += ' of ';
|
|
|
|
c += Math.formatBytes( this._sizeWhenDone );
|
|
|
|
c += ' (';
|
|
|
|
c += this.getPercentDoneStr();
|
|
|
|
c += '%)';
|
|
|
|
c += eta;
|
|
|
|
progress_details = c;
|
2008-07-16 14:35:58 +00:00
|
|
|
|
|
|
|
// Figure out the percent completed
|
2009-05-31 00:48:43 +00:00
|
|
|
var css_completed_width = Math.floor( this.getPercentDone() * 100 * MaxBarWidth ) / 100;
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Update the 'in progress' bar
|
2009-05-25 01:18:51 +00:00
|
|
|
e = root._progress_complete_container;
|
|
|
|
c = 'torrent_progress_bar';
|
|
|
|
c += this.isActive() ? ' in_progress' : ' incomplete_stopped';
|
|
|
|
if(css_completed_width === 0) { c += ' empty'; }
|
|
|
|
e.className = c;
|
2009-05-23 15:05:58 +00:00
|
|
|
e.style.width = css_completed_width + '%';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Update the 'incomplete' bar
|
|
|
|
e = root._progress_incomplete_container;
|
2009-05-25 01:18:51 +00:00
|
|
|
if( e.className.indexOf( 'incomplete' ) === -1 )
|
2009-05-23 15:05:58 +00:00
|
|
|
e.className = 'torrent_progress_bar in_progress';
|
|
|
|
e.style.width = (MaxBarWidth - css_completed_width) + '%';
|
|
|
|
e.style.display = 'block';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Create the 'peer details' label
|
|
|
|
// Eg: 'Downloading from 36 of 40 peers - DL: 60.2 KB/s UL: 4.3 KB/s'
|
|
|
|
if( !this.isDownloading( ) )
|
2009-05-25 01:18:51 +00:00
|
|
|
c = this.stateStr( );
|
2008-07-10 23:57:46 +00:00
|
|
|
else {
|
2009-05-25 01:18:51 +00:00
|
|
|
c = 'Downloading from ';
|
|
|
|
c += this.peersSendingToUs();
|
|
|
|
c += ' of ';
|
|
|
|
c += this._peers_connected;
|
|
|
|
c += ' peers - DL: ';
|
|
|
|
c += Math.formatBytes(this._download_speed);
|
|
|
|
c += '/s UL: ';
|
|
|
|
c += Math.formatBytes(this._upload_speed);
|
|
|
|
c += '/s';
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
2009-05-25 01:18:51 +00:00
|
|
|
peer_details = c;
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Update the 'in progress' bar
|
2009-05-25 01:18:51 +00:00
|
|
|
e = root._progress_complete_container;
|
|
|
|
c = 'torrent_progress_bar';
|
|
|
|
c += (this.isActive()) ? ' complete' : ' complete_stopped';
|
|
|
|
e.className = c;
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Create the 'progress details' label
|
|
|
|
// Eg: '698.05 MB, uploaded 8.59 GB (Ratio: 12.3)'
|
2009-05-25 01:18:51 +00:00
|
|
|
c = Math.formatBytes( this._size );
|
|
|
|
c += ', uploaded ';
|
|
|
|
c += Math.formatBytes( this._upload_total );
|
|
|
|
c += ' (Ratio ';
|
|
|
|
c += Math.ratio( this._upload_total, this._download_total );
|
|
|
|
c += ')';
|
|
|
|
progress_details = c;
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Hide the 'incomplete' bar
|
2009-05-23 15:05:58 +00:00
|
|
|
root._progress_incomplete_container.style.display = 'none';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Set progress to maximum
|
2009-05-23 15:05:58 +00:00
|
|
|
root._progress_complete_container.style.width = MaxBarWidth + '%';
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Create the 'peer details' label
|
|
|
|
// Eg: 'Seeding to 13 of 22 peers - UL: 36.2 KB/s'
|
|
|
|
if( !this.isSeeding( ) )
|
2009-05-25 01:18:51 +00:00
|
|
|
c = this.stateStr( );
|
|
|
|
else {
|
|
|
|
c = 'Seeding to ';
|
|
|
|
c += this.peersGettingFromUs();
|
|
|
|
c += ' of ';
|
|
|
|
c += this._peers_connected;
|
|
|
|
c += ' peers - UL: ';
|
|
|
|
c += Math.formatBytes(this._upload_speed);
|
|
|
|
c += '/s';
|
|
|
|
}
|
|
|
|
peer_details = c;
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the progress details
|
2009-05-23 15:05:58 +00:00
|
|
|
setInnerHTML( root._progress_details_container, progress_details );
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Update the peer details and pause/resume button
|
2009-05-23 15:05:58 +00:00
|
|
|
e = root._pause_resume_button_image;
|
2009-05-25 01:18:51 +00:00
|
|
|
if ( this.state() === Torrent._StatusPaused ) {
|
2008-07-10 23:57:46 +00:00
|
|
|
e.alt = 'Resume';
|
|
|
|
e.className = "torrent_resume";
|
|
|
|
} else {
|
|
|
|
e.alt = 'Pause';
|
|
|
|
e.className = "torrent_pause";
|
|
|
|
}
|
|
|
|
|
|
|
|
if( this._error_message &&
|
2009-05-25 01:18:51 +00:00
|
|
|
this._error_message !== '' &&
|
|
|
|
this._error_message !== 'other' ) {
|
2008-07-10 23:57:46 +00:00
|
|
|
peer_details = this._error_message;
|
|
|
|
}
|
|
|
|
|
2009-05-23 15:05:58 +00:00
|
|
|
setInnerHTML( root._peer_details_container, peer_details );
|
2009-04-29 11:59:29 +00:00
|
|
|
|
2009-05-23 20:39:55 +00:00
|
|
|
this.refreshFileView( );
|
2009-04-29 11:59:29 +00:00
|
|
|
},
|
|
|
|
|
2009-05-23 20:39:55 +00:00
|
|
|
refreshFileView: function() {
|
|
|
|
if( this._file_view.length )
|
|
|
|
for( var i=0; i<this._file_model.length; ++i )
|
|
|
|
this._file_view[i].update( this._file_model[i] );
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureFileListExists: function() {
|
|
|
|
if( this._file_view.length == 0 ) {
|
2009-05-25 04:13:08 +00:00
|
|
|
if(this._file_model.length == 1)
|
|
|
|
this._fileList.className += ' single_file';
|
2009-05-25 01:18:51 +00:00
|
|
|
var v, e;
|
2009-05-23 20:39:55 +00:00
|
|
|
for( var i=0; i<this._file_model.length; ++i ) {
|
2009-05-25 01:18:51 +00:00
|
|
|
v = new TorrentFile( this._file_model[i] );
|
2009-05-23 20:39:55 +00:00
|
|
|
this._file_view[i] = v;
|
2009-05-25 01:18:51 +00:00
|
|
|
e = v.domElement( );
|
2009-05-23 20:39:55 +00:00
|
|
|
e.className = (i % 2 ? 'even' : 'odd') + ' inspector_torrent_file_list_entry';
|
|
|
|
this._fileList.appendChild( e );
|
|
|
|
}
|
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
2009-05-27 23:12:06 +00:00
|
|
|
deleteFiles: function(){
|
|
|
|
if (this._fileList)
|
|
|
|
$(this._fileList).remove();
|
|
|
|
},
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
/*
|
|
|
|
* Return true if this torrent is selected
|
|
|
|
*/
|
|
|
|
isSelected: function() {
|
2009-05-25 13:31:03 +00:00
|
|
|
return this.element()[0].className.indexOf('selected') != -1;
|
2008-07-10 23:57:46 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param filter one of Prefs._Filter*
|
|
|
|
* @param search substring to look for, or null
|
|
|
|
* @return true if it passes the test, false if it fails
|
|
|
|
*/
|
|
|
|
test: function( filter, search )
|
|
|
|
{
|
|
|
|
var pass = false;
|
|
|
|
|
|
|
|
switch( filter )
|
|
|
|
{
|
|
|
|
case Prefs._FilterSeeding:
|
|
|
|
pass = this.isSeeding();
|
|
|
|
break;
|
|
|
|
case Prefs._FilterDownloading:
|
|
|
|
pass = this.isDownloading();
|
|
|
|
break;
|
|
|
|
case Prefs._FilterPaused:
|
|
|
|
pass = !this.isActive();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
pass = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( !pass )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if( !search || !search.length )
|
|
|
|
return pass;
|
|
|
|
|
2009-05-25 01:18:51 +00:00
|
|
|
return this._name_lc.indexOf( search.toLowerCase() ) !== -1;
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for Torrent.sortTorrents(). */
|
|
|
|
Torrent.compareById = function( a, b ) {
|
|
|
|
return a.id() - b.id();
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
|
|
|
Torrent.compareByAge = function( a, b ) {
|
|
|
|
return a.dateAdded() - b.dateAdded();
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
|
|
|
Torrent.compareByName = function( a, b ) {
|
|
|
|
return a._name_lc.compareTo( b._name_lc );
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
|
|
|
Torrent.compareByTracker = function( a, b ) {
|
|
|
|
return a._tracker.compareTo( b._tracker );
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
|
|
|
Torrent.compareByState = function( a, b ) {
|
|
|
|
return a.state() - b.state();
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
|
|
|
Torrent.compareByActivity = function( a, b ) {
|
|
|
|
return a.activity() - b.activity();
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
|
|
|
Torrent.compareByProgress = function( a, b ) {
|
2008-09-19 16:17:35 +00:00
|
|
|
if( a.getPercentDone() !== b.getPercentDone() )
|
|
|
|
return a.getPercentDone() - b.getPercentDone();
|
2008-07-10 23:57:46 +00:00
|
|
|
var a_ratio = Math.ratio( a._upload_total, a._download_total );
|
|
|
|
var b_ratio = Math.ratio( b._upload_total, b._download_total );
|
|
|
|
return a_ratio - b_ratio;
|
2009-05-25 01:18:51 +00:00
|
|
|
};
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param torrents an array of Torrent objects
|
|
|
|
* @param sortMethod one of Prefs._SortBy*
|
|
|
|
* @param sortDirection Prefs._SortAscending or Prefs._SortDescending
|
|
|
|
*/
|
|
|
|
Torrent.sortTorrents = function( torrents, sortMethod, sortDirection )
|
|
|
|
{
|
|
|
|
switch( sortMethod )
|
|
|
|
{
|
|
|
|
case Prefs._SortByActivity:
|
|
|
|
torrents.sort( this.compareByActivity );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByAge:
|
|
|
|
torrents.sort( this.compareByAge );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByQueue:
|
|
|
|
torrents.sort( this.compareById );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByProgress:
|
|
|
|
torrents.sort( this.compareByProgress );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByState:
|
|
|
|
torrents.sort( this.compareByState );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByTracker:
|
|
|
|
torrents.sort( this.compareByTracker );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByName:
|
|
|
|
torrents.sort( this.compareByName );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.warn( "unknown sort method: " + sortMethod );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-05-25 01:18:51 +00:00
|
|
|
if( sortDirection === Prefs._SortDescending )
|
2008-07-10 23:57:46 +00:00
|
|
|
torrents.reverse( );
|
|
|
|
|
|
|
|
return torrents;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief fast binary search to find a torrent
|
|
|
|
* @param torrents an array of torrents sorted by Id
|
|
|
|
* @param id the id to search for
|
|
|
|
* @return the index, or -1
|
|
|
|
*/
|
|
|
|
Torrent.indexOf = function( torrents, id )
|
|
|
|
{
|
|
|
|
var low = 0;
|
|
|
|
var high = torrents.length;
|
|
|
|
while( low < high ) {
|
|
|
|
var mid = Math.floor( ( low + high ) / 2 );
|
|
|
|
if( torrents[mid].id() < id )
|
|
|
|
low = mid + 1;
|
|
|
|
else
|
|
|
|
high = mid;
|
|
|
|
}
|
|
|
|
if( ( low < torrents.length ) && ( torrents[low].id() == id ) ) {
|
|
|
|
return low;
|
|
|
|
} else {
|
|
|
|
return -1; // not found
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-14 21:33:08 +00:00
|
|
|
function TorrentFile(file_data) {
|
|
|
|
this.initialize(file_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
TorrentFile.prototype = {
|
|
|
|
initialize: function(file_data) {
|
2009-04-29 11:59:29 +00:00
|
|
|
this._dirty = true;
|
2009-03-14 21:33:08 +00:00
|
|
|
this._torrent = file_data.torrent;
|
2009-05-24 00:02:43 +00:00
|
|
|
this._index = file_data.index;
|
2009-05-25 01:18:51 +00:00
|
|
|
var name = file_data.name.substring (file_data.name.lastIndexOf('/')+1);
|
2009-03-14 21:33:08 +00:00
|
|
|
this.readAttributes(file_data);
|
2009-05-23 06:09:57 +00:00
|
|
|
|
|
|
|
var li = document.createElement('li');
|
2009-05-25 04:07:48 +00:00
|
|
|
li.id = 't' + this._torrent.id() + 'f' + this._index;
|
2009-05-24 16:05:28 +00:00
|
|
|
li.classNameConst = 'inspector_torrent_file_list_entry ' + ((this._index%2)?'odd':'even');
|
2009-05-24 00:02:43 +00:00
|
|
|
li.className = li.classNameConst;
|
2009-05-23 06:09:57 +00:00
|
|
|
|
|
|
|
var wanted_div = document.createElement('div');
|
|
|
|
wanted_div.className = "file_wanted_control";
|
|
|
|
|
|
|
|
var pri_div = document.createElement('div');
|
2009-05-24 00:02:43 +00:00
|
|
|
pri_div.classNameConst = "file_priority_control";
|
|
|
|
pri_div.className = pri_div.classNameConst;
|
2009-05-23 06:09:57 +00:00
|
|
|
|
|
|
|
var file_div = document.createElement('div');
|
|
|
|
file_div.className = "inspector_torrent_file_list_entry_name";
|
2009-05-25 01:18:51 +00:00
|
|
|
file_div.innerHTML = name.replace(/([\/_\.])/g, "$1​");
|
2009-05-23 06:09:57 +00:00
|
|
|
|
|
|
|
var prog_div = document.createElement('div');
|
|
|
|
prog_div.className = "inspector_torrent_file_list_entry_progress";
|
|
|
|
|
|
|
|
li.appendChild(wanted_div);
|
|
|
|
li.appendChild(pri_div);
|
|
|
|
li.appendChild(file_div);
|
|
|
|
li.appendChild(prog_div);
|
2009-03-14 21:33:08 +00:00
|
|
|
|
2009-05-23 18:09:56 +00:00
|
|
|
this._element = li;
|
2009-05-24 00:02:43 +00:00
|
|
|
this._priority_control = pri_div;
|
2009-05-23 06:09:57 +00:00
|
|
|
this._progress = $(prog_div);
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
2009-05-23 20:39:55 +00:00
|
|
|
|
|
|
|
update: function(file_data) {
|
|
|
|
this.readAttributes(file_data);
|
|
|
|
this.refreshHTML();
|
|
|
|
},
|
2009-05-25 01:18:51 +00:00
|
|
|
|
|
|
|
isDone: function () {
|
|
|
|
return this._done >= this._size;
|
|
|
|
},
|
|
|
|
|
|
|
|
isEditable: function () {
|
|
|
|
return (this._torrent._file_model.length>1) && !this.isDone();
|
|
|
|
},
|
2009-03-14 21:33:08 +00:00
|
|
|
|
|
|
|
readAttributes: function(file_data) {
|
2009-05-25 01:18:51 +00:00
|
|
|
if( file_data.index !== undefined && file_data.index !== this._index ) {
|
2009-04-29 11:59:29 +00:00
|
|
|
this._index = file_data.index;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
2009-05-25 01:18:51 +00:00
|
|
|
if( file_data.bytesCompleted !== undefined && file_data.bytesCompleted !== this._done ) {
|
2009-04-29 11:59:29 +00:00
|
|
|
this._done = file_data.bytesCompleted;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
2009-05-25 01:18:51 +00:00
|
|
|
if( file_data.length !== undefined && file_data.length !== this._size ) {
|
2009-04-29 11:59:29 +00:00
|
|
|
this._size = file_data.length;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
2009-05-25 01:18:51 +00:00
|
|
|
if( file_data.priority !== undefined && file_data.priority !== this._prio ) {
|
2009-04-29 11:59:29 +00:00
|
|
|
this._prio = file_data.priority;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
2009-05-25 01:18:51 +00:00
|
|
|
if( file_data.wanted !== undefined && file_data.wanted !== this._wanted ) {
|
2009-04-29 11:59:29 +00:00
|
|
|
this._wanted = file_data.wanted;
|
|
|
|
this._dirty = true;
|
|
|
|
}
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
element: function() {
|
2009-05-23 18:09:56 +00:00
|
|
|
return $(this._element);
|
|
|
|
},
|
|
|
|
|
|
|
|
domElement: function() {
|
2009-03-14 21:33:08 +00:00
|
|
|
return this._element;
|
|
|
|
},
|
|
|
|
|
2009-05-25 01:18:51 +00:00
|
|
|
setPriority: function(prio) {
|
|
|
|
if (this.isEditable()) {
|
|
|
|
var cmd;
|
|
|
|
switch( prio ) {
|
|
|
|
case 1: cmd = 'priority-high'; break;
|
|
|
|
case -1: cmd = 'priority-low'; break;
|
|
|
|
default: cmd = 'priority-normal'; break;
|
|
|
|
}
|
|
|
|
this._prio = prio;
|
|
|
|
this._dirty = true;
|
|
|
|
this._torrent._controller.changeFileCommand( cmd, this._torrent, this );
|
|
|
|
}
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
setWanted: function(wanted) {
|
2009-04-29 11:59:29 +00:00
|
|
|
this._dirty = true;
|
2009-03-14 21:33:08 +00:00
|
|
|
this._wanted = wanted;
|
2009-04-29 11:59:29 +00:00
|
|
|
this.element().toggleClass( 'skip', !wanted );
|
|
|
|
var command = wanted ? 'files-wanted' : 'files-unwanted';
|
2009-03-14 21:33:08 +00:00
|
|
|
this._torrent._controller.changeFileCommand(command, this._torrent, this);
|
|
|
|
},
|
|
|
|
|
|
|
|
toggleWanted: function() {
|
2009-05-25 01:18:51 +00:00
|
|
|
if (this.isEditable())
|
|
|
|
this.setWanted( !this._wanted );
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshHTML: function() {
|
2009-04-29 11:59:29 +00:00
|
|
|
if( this._dirty ) {
|
|
|
|
this._dirty = false;
|
|
|
|
this.refreshProgressHTML();
|
|
|
|
this.refreshWantedHTML();
|
|
|
|
this.refreshPriorityHTML();
|
|
|
|
}
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshProgressHTML: function() {
|
2009-05-25 01:18:51 +00:00
|
|
|
var c = Math.formatBytes(this._done);
|
|
|
|
c += ' of ';
|
|
|
|
c += Math.formatBytes(this._size);
|
|
|
|
c += ' (';
|
|
|
|
c += Math.ratio(100 * this._done, this._size);
|
|
|
|
c += '%)';
|
|
|
|
setInnerHTML(this._progress[0], c);
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshWantedHTML: function() {
|
2009-05-25 01:18:51 +00:00
|
|
|
var e = this.domElement();
|
2009-05-24 00:02:43 +00:00
|
|
|
var c = e.classNameConst;
|
2009-05-25 01:18:51 +00:00
|
|
|
if(!this._wanted) { c += ' skip'; }
|
|
|
|
if(this.isDone()) { c += ' complete'; }
|
2009-05-24 00:02:43 +00:00
|
|
|
e.className = c;
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
refreshPriorityHTML: function() {
|
2009-05-24 00:02:43 +00:00
|
|
|
var e = this._priority_control;
|
|
|
|
var c = e.classNameConst;
|
|
|
|
switch( this._prio ) {
|
|
|
|
case 1: c += ' high'; break;
|
|
|
|
case -1: c += ' low'; break;
|
|
|
|
default: c += ' normal'; break;
|
|
|
|
}
|
|
|
|
e.className = c;
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
fileWantedControlClicked: function(event) {
|
2009-05-25 04:07:48 +00:00
|
|
|
this.toggleWanted();
|
2009-03-14 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
2009-05-25 04:07:48 +00:00
|
|
|
filePriorityControlClicked: function(event, element) {
|
2009-03-14 21:33:08 +00:00
|
|
|
var x = event.pageX;
|
2009-05-25 04:07:48 +00:00
|
|
|
while (element !== null) {
|
|
|
|
x = x - element.offsetLeft;
|
|
|
|
element = element.offsetParent;
|
2009-03-14 21:33:08 +00:00
|
|
|
}
|
2009-05-25 04:07:48 +00:00
|
|
|
|
2009-05-25 01:18:51 +00:00
|
|
|
var prio;
|
|
|
|
if( x < 12 ) prio = -1;
|
|
|
|
else if( x < 23 ) prio = 0;
|
|
|
|
else prio = 1;
|
2009-05-25 04:07:48 +00:00
|
|
|
this.setPriority( prio );
|
2009-03-14 21:33:08 +00:00
|
|
|
}
|
|
|
|
};
|