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
|
|
|
|
*/
|
|
|
|
|
2011-08-16 18:49:26 +00:00
|
|
|
function Torrent( controller, data) {
|
|
|
|
this.initialize( controller, data);
|
2009-08-12 14:40:32 +00:00
|
|
|
}
|
2008-07-10 23:57:46 +00:00
|
|
|
|
|
|
|
// Constants
|
2011-08-01 22:24:24 +00:00
|
|
|
Torrent._StatusStopped = 0; /* torrent is stopped */
|
|
|
|
Torrent._StatusCheckWait = 1; /* waiting in queue to check files */
|
|
|
|
Torrent._StatusCheck = 2; /* checking files */
|
|
|
|
Torrent._StatusDownloadWait = 3; /* queued to download */
|
|
|
|
Torrent._StatusDownload = 4; /* downloading */
|
|
|
|
Torrent._StatusSeedWait = 5; /* queeud to seed */
|
|
|
|
Torrent._StatusSeed = 6; /* seeding */
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
Torrent._InfiniteTimeRemaining = 215784000; // 999 Hours - may as well be infinite
|
|
|
|
|
2009-07-28 04:52:52 +00:00
|
|
|
Torrent._RatioUseGlobal = 0;
|
|
|
|
Torrent._RatioUseLocal = 1;
|
|
|
|
Torrent._RatioUnlimited = 2;
|
|
|
|
|
2009-08-05 01:25:36 +00:00
|
|
|
Torrent._ErrNone = 0;
|
|
|
|
Torrent._ErrTrackerWarning = 1;
|
|
|
|
Torrent._ErrTrackerError = 2;
|
|
|
|
Torrent._ErrLocalError = 3;
|
|
|
|
|
2011-08-01 22:24:24 +00:00
|
|
|
Torrent._TrackerInactive = 0;
|
|
|
|
Torrent._TrackerWaiting = 1;
|
|
|
|
Torrent._TrackerQueued = 2;
|
|
|
|
Torrent._TrackerActive = 3;
|
2010-02-01 01:08:17 +00:00
|
|
|
|
|
|
|
|
2010-02-06 16:43:48 +00:00
|
|
|
Torrent._StaticFields = [ 'hashString', 'id' ]
|
|
|
|
|
|
|
|
Torrent._MetaDataFields = [ 'addedDate', 'comment', 'creator', 'dateCreated',
|
|
|
|
'isPrivate', 'name', 'totalSize', 'pieceCount', 'pieceSize' ]
|
|
|
|
|
2009-08-07 00:39:07 +00:00
|
|
|
Torrent._DynamicFields = [ 'downloadedEver', 'error', 'errorString', 'eta',
|
2011-08-01 22:24:24 +00:00
|
|
|
'haveUnchecked', 'haveValid', 'leftUntilDone', 'metadataPercentComplete',
|
|
|
|
'peers', 'peersConnected', 'peersGettingFromUs', 'peersSendingToUs',
|
|
|
|
'queuePosition', 'rateDownload', 'rateUpload', 'recheckProgress',
|
|
|
|
'sizeWhenDone', 'status', 'trackerStats', 'desiredAvailable',
|
|
|
|
'uploadedEver', 'uploadRatio', 'seedRatioLimit', 'seedRatioMode',
|
|
|
|
'downloadDir', 'isFinished' ]
|
2009-08-07 00:39:07 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
Torrent.prototype =
|
|
|
|
{
|
2010-02-06 16:43:48 +00:00
|
|
|
initMetaData: function( data ) {
|
|
|
|
this._date = data.addedDate;
|
|
|
|
this._comment = data.comment;
|
|
|
|
this._creator = data.creator;
|
|
|
|
this._creator_date = data.dateCreated;
|
|
|
|
this._is_private = data.isPrivate;
|
|
|
|
this._name = data.name;
|
|
|
|
this._name_lc = this._name.toLowerCase( );
|
|
|
|
this._size = data.totalSize;
|
|
|
|
this._pieceCount = data.pieceCount;
|
|
|
|
this._pieceSize = data.pieceSize;
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
/*
|
|
|
|
* Constructor
|
|
|
|
*/
|
2011-08-16 18:49:26 +00:00
|
|
|
initialize: function( controller, data) {
|
2009-05-22 22:45:09 +00:00
|
|
|
this._id = data.id;
|
|
|
|
this._hashString = data.hashString;
|
|
|
|
this._sizeWhenDone = data.sizeWhenDone;
|
2010-01-31 02:42:48 +00:00
|
|
|
this._trackerStats = this.buildTrackerStats(data.trackerStats);
|
2009-05-23 20:39:55 +00:00
|
|
|
this._file_model = [ ];
|
2010-02-06 16:43:48 +00:00
|
|
|
this.initMetaData( data );
|
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-03-14 21:33:08 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2010-01-31 02:42:48 +00:00
|
|
|
buildTrackerStats: function(trackerStats) {
|
|
|
|
result = [];
|
|
|
|
for( var i=0, tracker; tracker=trackerStats[i]; ++i ) {
|
2010-02-15 23:52:41 +00:00
|
|
|
tier = result[tracker.tier] || [];
|
2010-01-31 02:42:48 +00:00
|
|
|
tier[tier.length] = {
|
|
|
|
'host': tracker.host,
|
2010-02-01 01:08:17 +00:00
|
|
|
'announce': tracker.announce,
|
|
|
|
'hasAnnounced': tracker.hasAnnounced,
|
|
|
|
'lastAnnounceTime': tracker.lastAnnounceTime,
|
|
|
|
'lastAnnounceSucceeded': tracker.lastAnnounceSucceeded,
|
|
|
|
'lastAnnounceResult': tracker.lastAnnounceResult,
|
|
|
|
'lastAnnouncePeerCount': tracker.lastAnnouncePeerCount,
|
|
|
|
'announceState': tracker.announceState,
|
|
|
|
'nextAnnounceTime': tracker.nextAnnounceTime,
|
|
|
|
'isBackup': tracker.isBackup,
|
|
|
|
'hasScraped': tracker.hasScraped,
|
|
|
|
'lastScrapeTime': tracker.lastScrapeTime,
|
|
|
|
'lastScrapeSucceeded': tracker.lastScrapeSucceeded,
|
|
|
|
'lastScrapeResult': tracker.lastScrapeResult,
|
2010-01-31 02:42:48 +00:00
|
|
|
'seederCount': tracker.seederCount,
|
|
|
|
'leecherCount': tracker.leecherCount,
|
|
|
|
'downloadCount': tracker.downloadCount
|
|
|
|
};
|
2010-02-15 23:52:41 +00:00
|
|
|
result[tracker.tier] = tier;
|
2010-01-31 02:42:48 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
/*--------------------------------------------
|
2009-08-12 14:40:32 +00:00
|
|
|
*
|
2008-07-10 23:57:46 +00:00
|
|
|
* S E T T E R S / G E T T E R S
|
2009-08-12 14:40:32 +00:00
|
|
|
*
|
2008-07-10 23:57:46 +00:00
|
|
|
*--------------------------------------------*/
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-16 18:49:26 +00:00
|
|
|
activity: function() { return this.downloadSpeed() + this.uploadSpeed(); },
|
2008-07-10 23:57:46 +00:00
|
|
|
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; },
|
|
|
|
hash: function() { return this._hashString; },
|
|
|
|
id: function() { return this._id; },
|
2010-06-17 04:38:03 +00:00
|
|
|
isActiveFilter: function() { return this.peersGettingFromUs() > 0
|
2011-01-19 00:07:59 +00:00
|
|
|
|| this.peersSendingToUs() > 0
|
|
|
|
|| this.webseedsSendingToUs() > 0
|
2011-08-01 22:24:24 +00:00
|
|
|
|| this.state() == Torrent._StatusCheck; },
|
2011-08-16 18:49:26 +00:00
|
|
|
isStopped: function() { return this.state() === Torrent._StatusStopped; },
|
2011-08-01 22:24:24 +00:00
|
|
|
isActive: function() { return this.state() != Torrent._StatusStopped; },
|
|
|
|
isDownloading: function() { return this.state() == Torrent._StatusDownload; },
|
2010-09-03 00:20:40 +00:00
|
|
|
isFinished: function() { return this._isFinishedSeeding; },
|
2011-08-16 18:49:26 +00:00
|
|
|
isDone: function() { return this._leftUntilDone < 1; },
|
2011-08-01 22:24:24 +00:00
|
|
|
isSeeding: function() { return this.state() == Torrent._StatusSeed; },
|
2008-07-10 23:57:46 +00:00
|
|
|
name: function() { return this._name; },
|
2011-08-01 22:24:24 +00:00
|
|
|
queuePosition: function() { return this._queue_position; },
|
2011-01-19 00:07:59 +00:00
|
|
|
webseedsSendingToUs: function() { return this._webseeds_sending_to_us; },
|
2008-07-25 14:34:25 +00:00
|
|
|
peersSendingToUs: function() { return this._peers_sending_to_us; },
|
|
|
|
peersGettingFromUs: function() { return this._peers_getting_from_us; },
|
2009-12-04 04:31:29 +00:00
|
|
|
needsMetaData: function(){ return this._metadataPercentComplete < 1 },
|
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() {
|
2010-06-22 22:30:58 +00:00
|
|
|
return Transmission.fmt.percentString( 100 * this.getPercentDone() );
|
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() ) {
|
2011-08-01 22:24:24 +00:00
|
|
|
case Torrent._StatusStopped: return this.isFinished() ? 'Seeding complete' : 'Paused';
|
2011-08-08 16:31:52 +00:00
|
|
|
case Torrent._StatusCheckWait: return 'Queued for verification';
|
2011-08-01 22:24:24 +00:00
|
|
|
case Torrent._StatusCheck: return 'Verifying local data';
|
2011-08-08 16:31:52 +00:00
|
|
|
case Torrent._StatusDownloadWait: return 'Queued for download';
|
2011-08-01 22:24:24 +00:00
|
|
|
case Torrent._StatusDownload: return 'Downloading';
|
2011-08-08 16:31:52 +00:00
|
|
|
case Torrent._StatusSeedWait: return 'Queued for seeding';
|
2011-08-01 22:24:24 +00:00
|
|
|
case Torrent._StatusSeed: return 'Seeding';
|
2008-07-10 23:57:46 +00:00
|
|
|
default: return 'error';
|
|
|
|
}
|
|
|
|
},
|
2010-01-31 02:42:48 +00:00
|
|
|
trackerStats: function() { return this._trackerStats; },
|
2008-07-10 23:57:46 +00:00
|
|
|
uploadSpeed: function() { return this._upload_speed; },
|
|
|
|
uploadTotal: function() { return this._upload_total; },
|
2011-08-16 18:49:26 +00:00
|
|
|
seedRatioLimit: function(controller){
|
2009-07-28 04:52:52 +00:00
|
|
|
switch( this._seed_ratio_mode ) {
|
2011-08-16 18:49:26 +00:00
|
|
|
case Torrent._RatioUseGlobal: return controller.seedRatioLimit();
|
2009-07-28 04:52:52 +00:00
|
|
|
case Torrent._RatioUseLocal: return this._seed_ratio_limit;
|
|
|
|
default: return -1;
|
|
|
|
}
|
|
|
|
},
|
2011-08-16 18:49:26 +00:00
|
|
|
getErrorMessage: function() {
|
|
|
|
if( this._error == Torrent._ErrTrackerWarning )
|
|
|
|
return 'Tracker returned a warning: ' + this._error_string;
|
|
|
|
if( this._error == Torrent._ErrTrackerError )
|
|
|
|
return 'Tracker returned an error: ' + this._error_string;
|
|
|
|
if( this._error == Torrent._ErrLocalError )
|
|
|
|
return 'Error: ' + this._error_string;
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2009-05-25 13:31:03 +00:00
|
|
|
/*--------------------------------------------
|
2009-08-12 14:40:32 +00:00
|
|
|
*
|
2011-08-16 18:49:26 +00:00
|
|
|
* I N T E R F A C E F U N C T I O N S
|
2009-08-12 14:40:32 +00:00
|
|
|
*
|
2009-05-25 13:31:03 +00:00
|
|
|
*--------------------------------------------*/
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-16 18:49:26 +00:00
|
|
|
fireDataChanged: function()
|
2009-05-25 13:31:03 +00:00
|
|
|
{
|
2011-08-16 18:49:26 +00:00
|
|
|
$(this).trigger('dataChanged',[]);
|
2009-05-25 13:31:03 +00:00
|
|
|
},
|
|
|
|
|
2011-08-16 18:49:26 +00:00
|
|
|
refreshMetaData: function(data)
|
2009-05-25 13:31:03 +00:00
|
|
|
{
|
2010-02-06 16:43:48 +00:00
|
|
|
this.initMetaData( data );
|
2011-08-16 18:49:26 +00:00
|
|
|
this.fireDataChanged();
|
2010-02-06 16:43:48 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-16 18:49:26 +00:00
|
|
|
refresh: function(data)
|
2010-02-06 16:43:48 +00:00
|
|
|
{
|
|
|
|
if( this.needsMetaData() && ( data.metadataPercentComplete >= 1 ) )
|
|
|
|
transmission.refreshMetaData( [ this._id ] );
|
|
|
|
|
2009-12-04 04:31:29 +00:00
|
|
|
this._completed = data.haveUnchecked + data.haveValid;
|
|
|
|
this._verified = data.haveValid;
|
|
|
|
this._leftUntilDone = data.leftUntilDone;
|
|
|
|
this._download_total = data.downloadedEver;
|
|
|
|
this._upload_total = data.uploadedEver;
|
|
|
|
this._upload_ratio = data.uploadRatio;
|
|
|
|
this._seed_ratio_limit = data.seedRatioLimit;
|
|
|
|
this._seed_ratio_mode = data.seedRatioMode;
|
|
|
|
this._download_speed = data.rateDownload;
|
|
|
|
this._upload_speed = data.rateUpload;
|
2010-06-17 04:40:06 +00:00
|
|
|
this._peers = data.peers;
|
2009-12-04 04:31:29 +00:00
|
|
|
this._peers_connected = data.peersConnected;
|
|
|
|
this._peers_getting_from_us = data.peersGettingFromUs;
|
|
|
|
this._peers_sending_to_us = data.peersSendingToUs;
|
2011-08-01 22:24:24 +00:00
|
|
|
this._queue_position = data.queuePosition;
|
2011-01-19 00:07:59 +00:00
|
|
|
this._webseeds_sending_to_us = data.webseedsSendingToUs;
|
2009-12-04 04:31:29 +00:00
|
|
|
this._sizeWhenDone = data.sizeWhenDone;
|
|
|
|
this._recheckProgress = data.recheckProgress;
|
|
|
|
this._error = data.error;
|
|
|
|
this._error_string = data.errorString;
|
|
|
|
this._eta = data.eta;
|
2010-01-31 02:42:48 +00:00
|
|
|
this._trackerStats = this.buildTrackerStats(data.trackerStats);
|
2009-12-04 04:31:29 +00:00
|
|
|
this._state = data.status;
|
|
|
|
this._download_dir = data.downloadDir;
|
|
|
|
this._metadataPercentComplete = data.metadataPercentComplete;
|
2010-04-02 17:57:25 +00:00
|
|
|
this._isFinishedSeeding = data.isFinished;
|
2010-04-20 01:48:40 +00:00
|
|
|
this._desiredAvailable = data.desiredAvailable;
|
2009-05-23 20:39:55 +00:00
|
|
|
|
|
|
|
if (data.fileStats)
|
|
|
|
this.refreshFileModel( data );
|
2011-08-16 18:49:26 +00:00
|
|
|
|
|
|
|
this.fireDataChanged();
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
|
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;
|
2011-08-01 22:24:24 +00:00
|
|
|
var s = this.state( );
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
switch( filter )
|
|
|
|
{
|
2010-06-17 04:38:03 +00:00
|
|
|
case Prefs._FilterActive:
|
|
|
|
pass = this.isActiveFilter();
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._FilterSeeding:
|
2011-08-01 22:24:24 +00:00
|
|
|
pass = ( s == Torrent._StatusSeed ) || ( s == Torrent._StatusSeedWait );
|
2008-07-10 23:57:46 +00:00
|
|
|
break;
|
|
|
|
case Prefs._FilterDownloading:
|
2011-08-01 22:24:24 +00:00
|
|
|
pass = ( s == Torrent._StatusDownload ) || ( s == Torrent._StatusDownloadWait );
|
2008-07-10 23:57:46 +00:00
|
|
|
break;
|
|
|
|
case Prefs._FilterPaused:
|
|
|
|
pass = !this.isActive();
|
|
|
|
break;
|
2010-09-03 00:20:40 +00:00
|
|
|
case Prefs._FilterFinished:
|
|
|
|
pass = this.isFinished();
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
default:
|
|
|
|
pass = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
if( !pass )
|
|
|
|
return false;
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
if( !search || !search.length )
|
|
|
|
return pass;
|
2010-06-21 13:14:33 +00:00
|
|
|
|
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(). */
|
2011-08-01 22:24:24 +00:00
|
|
|
Torrent.compareByName = function( a, b ) {
|
|
|
|
var i = a._name_lc.compareTo( b._name_lc );
|
|
|
|
if( i )
|
|
|
|
return i;
|
|
|
|
return Torrent.compareById( a, b );
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
2011-08-01 22:24:24 +00:00
|
|
|
Torrent.compareByQueue = function( a, b )
|
|
|
|
{
|
2011-08-02 03:59:54 +00:00
|
|
|
return a.queuePosition( ) - b.queuePosition();
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
2011-08-01 22:24:24 +00:00
|
|
|
Torrent.compareByAge = function( a, b )
|
|
|
|
{
|
|
|
|
var a_age = a.dateAdded();
|
|
|
|
var b_age = b.dateAdded();
|
|
|
|
if( a_age != b_age )
|
|
|
|
return a_age - b_age;
|
|
|
|
|
|
|
|
return Torrent.compareByQueue( a, b );
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
2011-08-01 22:24:24 +00:00
|
|
|
Torrent.compareByState = function( a, b )
|
|
|
|
{
|
|
|
|
var a_state = a.state( );
|
|
|
|
var b_state = b.state( );
|
|
|
|
if( a_state != b_state )
|
|
|
|
return b_state - a_state;
|
|
|
|
|
|
|
|
return Torrent.compareByQueue( a, b );
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
|
|
|
Torrent.compareByActivity = function( a, b )
|
|
|
|
{
|
|
|
|
var a_activity = a.activity( );
|
|
|
|
var b_activity = b.activity( );
|
|
|
|
if( a_activity != b_activity )
|
|
|
|
return a_activity - b_activity;
|
|
|
|
|
|
|
|
return Torrent.compareByState( a, b );
|
2008-07-10 23:57:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Helper function for sortTorrents(). */
|
2011-06-10 14:04:32 +00:00
|
|
|
Torrent.compareByRatio = function( a, b ) {
|
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 );
|
2011-08-01 22:24:24 +00:00
|
|
|
if( a_ratio != b_ratio )
|
|
|
|
return a_ratio - b_ratio;
|
|
|
|
return Torrent.compareByState( a, b );
|
2009-05-25 01:18:51 +00:00
|
|
|
};
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2011-06-10 14:04:32 +00:00
|
|
|
Torrent.compareByProgress = function( a, b ) {
|
|
|
|
if( a.getPercentDone() !== b.getPercentDone() )
|
|
|
|
return a.getPercentDone() - b.getPercentDone();
|
2011-08-01 22:24:24 +00:00
|
|
|
return Torrent.compareByRatio( a, b );
|
2011-06-10 14:04:32 +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;
|
2011-08-02 03:59:54 +00:00
|
|
|
case Prefs._SortByQueue:
|
|
|
|
torrents.sort( this.compareByQueue );
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._SortByProgress:
|
|
|
|
torrents.sort( this.compareByProgress );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByState:
|
|
|
|
torrents.sort( this.compareByState );
|
|
|
|
break;
|
|
|
|
case Prefs._SortByName:
|
|
|
|
torrents.sort( this.compareByName );
|
|
|
|
break;
|
2011-06-10 14:04:32 +00:00
|
|
|
case Prefs._SortByRatio:
|
|
|
|
torrents.sort( this.compareByRatio );
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
default:
|
|
|
|
console.warn( "unknown sort method: " + sortMethod );
|
|
|
|
break;
|
|
|
|
}
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2009-05-25 01:18:51 +00:00
|
|
|
if( sortDirection === Prefs._SortDescending )
|
2008-07-10 23:57:46 +00:00
|
|
|
torrents.reverse( );
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2008-07-10 23:57:46 +00:00
|
|
|
return torrents;
|
|
|
|
};
|