2008-07-10 23:57:46 +00:00
|
|
|
/*
|
2011-08-20 21:17:12 +00:00
|
|
|
* Copyright © Jordan Lee, Dave Perrett and Malcolm Jarvis
|
2008-07-10 23:57:46 +00:00
|
|
|
* 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-24 02:04:35 +00:00
|
|
|
function Torrent(data)
|
2011-08-20 21:17:12 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
this.initialize(data);
|
2011-08-20 21:17:12 +00:00
|
|
|
}
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2011-08-20 21:17:12 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Constants
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
// Torrent.fields.status
|
2011-08-24 02:04:35 +00:00
|
|
|
Torrent._StatusStopped = 0;
|
|
|
|
Torrent._StatusCheckWait = 1;
|
|
|
|
Torrent._StatusCheck = 2;
|
|
|
|
Torrent._StatusDownloadWait = 3;
|
|
|
|
Torrent._StatusDownload = 4;
|
|
|
|
Torrent._StatusSeedWait = 5;
|
|
|
|
Torrent._StatusSeed = 6;
|
2011-08-20 21:17:12 +00:00
|
|
|
|
|
|
|
// Torrent.fields.seedRatioMode
|
2011-08-24 02:04:35 +00:00
|
|
|
Torrent._RatioUseGlobal = 0;
|
|
|
|
Torrent._RatioUseLocal = 1;
|
|
|
|
Torrent._RatioUnlimited = 2;
|
2011-08-20 21:17:12 +00:00
|
|
|
|
|
|
|
// Torrent.fields.error
|
2011-08-24 02:04:35 +00:00
|
|
|
Torrent._ErrNone = 0;
|
|
|
|
Torrent._ErrTrackerWarning = 1;
|
|
|
|
Torrent._ErrTrackerError = 2;
|
|
|
|
Torrent._ErrLocalError = 3;
|
2011-08-20 21:17:12 +00:00
|
|
|
|
|
|
|
// TrackerStats' announceState
|
2011-08-24 02:04:35 +00:00
|
|
|
Torrent._TrackerInactive = 0;
|
|
|
|
Torrent._TrackerWaiting = 1;
|
|
|
|
Torrent._TrackerQueued = 2;
|
|
|
|
Torrent._TrackerActive = 3;
|
2011-08-20 21:17:12 +00:00
|
|
|
|
|
|
|
|
2011-08-25 23:06:41 +00:00
|
|
|
Torrent.Fields = { };
|
|
|
|
|
|
|
|
// commonly used fields which only need to be loaded once,
|
|
|
|
// either on startup or when a magnet finishes downloading its metadata
|
|
|
|
// finishes downloading its metadata
|
|
|
|
Torrent.Fields.Metadata = [
|
|
|
|
'addedDate',
|
|
|
|
'name',
|
|
|
|
'totalSize'
|
|
|
|
];
|
|
|
|
|
|
|
|
// commonly used fields which need to be periodically refreshed
|
|
|
|
Torrent.Fields.Stats = [
|
|
|
|
'error',
|
|
|
|
'errorString',
|
|
|
|
'eta',
|
|
|
|
'isFinished',
|
|
|
|
'isStalled',
|
|
|
|
'leftUntilDone',
|
|
|
|
'metadataPercentComplete',
|
|
|
|
'peersConnected',
|
|
|
|
'peersGettingFromUs',
|
|
|
|
'peersSendingToUs',
|
|
|
|
'percentDone',
|
|
|
|
'queuePosition',
|
|
|
|
'rateDownload',
|
|
|
|
'rateUpload',
|
|
|
|
'recheckProgress',
|
|
|
|
'seedRatioMode',
|
|
|
|
'sizeWhenDone',
|
|
|
|
'status',
|
|
|
|
'trackers',
|
|
|
|
'uploadedEver',
|
|
|
|
'uploadRatio'
|
|
|
|
];
|
|
|
|
|
|
|
|
// fields used by the inspector which only need to be loaded once
|
|
|
|
Torrent.Fields.InfoExtra = [
|
|
|
|
'comment',
|
|
|
|
'creator',
|
|
|
|
'dateCreated',
|
|
|
|
'files',
|
|
|
|
'hashString',
|
|
|
|
'isPrivate',
|
|
|
|
'pieceCount',
|
|
|
|
'pieceSize'
|
|
|
|
];
|
|
|
|
|
|
|
|
// fields used in the inspector which need to be periodically refreshed
|
|
|
|
Torrent.Fields.StatsExtra = [
|
|
|
|
'desiredAvailable',
|
|
|
|
'downloadDir',
|
|
|
|
'downloadedEver',
|
|
|
|
'fileStats',
|
|
|
|
'haveUnchecked',
|
|
|
|
'haveValid',
|
|
|
|
'peers',
|
|
|
|
'seedRatioLimit',
|
|
|
|
'trackerStats',
|
|
|
|
'webseedsSendingToUs'
|
|
|
|
];
|
2011-08-20 21:17:12 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** Methods
|
|
|
|
****
|
|
|
|
***/
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.prototype =
|
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
initialize: function(data)
|
2011-08-20 21:17:12 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
this.fields = {};
|
2011-08-25 23:06:41 +00:00
|
|
|
this.refresh (data);
|
2011-08-20 21:17:12 +00:00
|
|
|
},
|
2010-02-01 01:08:17 +00:00
|
|
|
|
2011-08-25 23:06:41 +00:00
|
|
|
setField: function(o, name, value)
|
|
|
|
{
|
2011-08-27 21:35:19 +00:00
|
|
|
if (o[name] === value)
|
|
|
|
return false;
|
|
|
|
o[name] = value;
|
|
|
|
return true;
|
2011-08-20 21:17:12 +00:00
|
|
|
},
|
2010-02-01 01:08:17 +00:00
|
|
|
|
2011-08-25 23:06:41 +00:00
|
|
|
// fields.files is an array of unions of RPC's "files" and "fileStats" objects.
|
2011-08-28 13:57:25 +00:00
|
|
|
updateFiles: function(files)
|
|
|
|
{
|
|
|
|
var changed = false,
|
|
|
|
myfiles = this.fields.files || [],
|
|
|
|
keys = [ 'length', 'name', 'bytesCompleted', 'wanted', 'priority' ],
|
|
|
|
i, f, j, key, myfile;
|
|
|
|
|
|
|
|
for (i=0; f=files[i]; ++i) {
|
|
|
|
myfile = myfiles[i] || {};
|
|
|
|
for (j=0; key=keys[j]; ++j)
|
2011-08-25 23:06:41 +00:00
|
|
|
if(key in f)
|
|
|
|
changed |= this.setField(myfile,key,f[key]);
|
|
|
|
myfiles[i] = myfile;
|
2010-02-06 16:43:48 +00:00
|
|
|
}
|
2011-08-25 23:06:41 +00:00
|
|
|
this.fields.files = myfiles;
|
2011-08-24 02:04:35 +00:00
|
|
|
return changed;
|
2010-02-06 16:43:48 +00:00
|
|
|
},
|
|
|
|
|
2011-08-28 13:57:25 +00:00
|
|
|
collateTrackers: function(trackers)
|
|
|
|
{
|
|
|
|
var i, t, announces = [];
|
|
|
|
|
|
|
|
for (i=0; t=trackers[i]; ++i)
|
2011-08-25 23:06:41 +00:00
|
|
|
announces.push(t.announce.toLowerCase());
|
|
|
|
return announces.join('\t');
|
2010-02-06 16:43:48 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-25 23:06:41 +00:00
|
|
|
refreshFields: function(data)
|
2010-02-06 16:43:48 +00:00
|
|
|
{
|
2011-08-28 13:57:25 +00:00
|
|
|
var key,
|
|
|
|
changed = false;
|
2011-08-24 02:04:35 +00:00
|
|
|
|
2011-08-28 13:57:25 +00:00
|
|
|
for (key in data) {
|
2011-08-26 21:51:14 +00:00
|
|
|
switch (key) {
|
2011-08-25 23:06:41 +00:00
|
|
|
case 'files':
|
|
|
|
case 'fileStats': // merge files and fileStats together
|
|
|
|
changed |= this.updateFiles(data[key]);
|
|
|
|
break;
|
|
|
|
case 'trackerStats': // 'trackerStats' is a superset of 'trackers'...
|
|
|
|
changed |= this.setField(this.fields,'trackers',data[key]);
|
2011-08-26 18:10:53 +00:00
|
|
|
break;
|
2011-08-25 23:06:41 +00:00
|
|
|
case 'trackers': // ...so only save 'trackers' if we don't have it already
|
|
|
|
if (!(key in this.fields))
|
|
|
|
changed |= this.setField(this.fields,key,data[key]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
changed |= this.setField(this.fields,key,data[key]);
|
2011-08-24 02:04:35 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-20 21:17:12 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
return changed;
|
2009-05-22 22:45:09 +00:00
|
|
|
},
|
|
|
|
|
2011-08-25 23:06:41 +00:00
|
|
|
refresh: function(data)
|
2011-08-20 22:30:34 +00:00
|
|
|
{
|
2011-08-25 23:06:41 +00:00
|
|
|
if (this.refreshFields(data))
|
2011-08-27 21:35:19 +00:00
|
|
|
$(this).trigger('dataChanged', this);
|
2011-08-20 22:30:34 +00:00
|
|
|
},
|
|
|
|
|
2011-08-20 21:17:12 +00:00
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
|
|
|
// simple accessors
|
2011-08-24 02:04:35 +00:00
|
|
|
getComment: function() { return this.fields.comment; },
|
|
|
|
getCreator: function() { return this.fields.creator; },
|
|
|
|
getDateAdded: function() { return this.fields.addedDate; },
|
|
|
|
getDateCreated: function() { return this.fields.dateCreated; },
|
|
|
|
getDesiredAvailable: function() { return this.fields.desiredAvailable; },
|
|
|
|
getDownloadDir: function() { return this.fields.downloadDir; },
|
|
|
|
getDownloadSpeed: function() { return this.fields.rateDownload; },
|
|
|
|
getDownloadedEver: function() { return this.fields.downloadedEver; },
|
|
|
|
getError: function() { return this.fields.error; },
|
|
|
|
getErrorString: function() { return this.fields.errorString; },
|
|
|
|
getETA: function() { return this.fields.eta; },
|
2011-08-25 23:06:41 +00:00
|
|
|
getFile: function(i) { return this.fields.files[i]; },
|
|
|
|
getFileCount: function() { return this.fields.files ? this.fields.files.length : 0; },
|
2011-08-24 02:04:35 +00:00
|
|
|
getHashString: function() { return this.fields.hashString; },
|
|
|
|
getHaveValid: function() { return this.fields.haveValid; },
|
|
|
|
getHave: function() { return this.getHaveValid() + this.fields.haveUnchecked; },
|
|
|
|
getId: function() { return this.fields.id; },
|
|
|
|
getLeftUntilDone: function() { return this.fields.leftUntilDone; },
|
|
|
|
getMetadataPercentComplete: function() { return this.fields.metadataPercentComplete; },
|
2011-08-26 23:34:43 +00:00
|
|
|
getName: function() { return this.fields.name || 'Unknown'; },
|
2011-08-24 02:04:35 +00:00
|
|
|
getPeers: function() { return this.fields.peers; },
|
|
|
|
getPeersConnected: function() { return this.fields.peersConnected; },
|
|
|
|
getPeersGettingFromUs: function() { return this.fields.peersGettingFromUs; },
|
|
|
|
getPeersSendingToUs: function() { return this.fields.peersSendingToUs; },
|
|
|
|
getPieceCount: function() { return this.fields.pieceCount; },
|
|
|
|
getPieceSize: function() { return this.fields.pieceSize; },
|
|
|
|
getPrivateFlag: function() { return this.fields.isPrivate; },
|
|
|
|
getQueuePosition: function() { return this.fields.queuePosition; },
|
|
|
|
getRecheckProgress: function() { return this.fields.recheckProgress; },
|
|
|
|
getSeedRatioLimit: function() { return this.fields.seedRatioLimit; },
|
|
|
|
getSeedRatioMode: function() { return this.fields.seedRatioMode; },
|
|
|
|
getSizeWhenDone: function() { return this.fields.sizeWhenDone; },
|
|
|
|
getStatus: function() { return this.fields.status; },
|
|
|
|
getTotalSize: function() { return this.fields.totalSize; },
|
2011-08-25 23:06:41 +00:00
|
|
|
getTrackers: function() { return this.fields.trackers; },
|
2011-08-24 02:04:35 +00:00
|
|
|
getUploadSpeed: function() { return this.fields.rateUpload; },
|
|
|
|
getUploadRatio: function() { return this.fields.uploadRatio; },
|
|
|
|
getUploadedEver: function() { return this.fields.uploadedEver; },
|
|
|
|
getWebseedsSendingToUs: function() { return this.fields.webseedsSendingToUs; },
|
|
|
|
isFinished: function() { return this.fields.isFinished; },
|
2011-08-20 21:17:12 +00:00
|
|
|
|
|
|
|
// derived accessors
|
2011-08-30 02:49:01 +00:00
|
|
|
hasExtraInfo: function() { return 'hashString' in this.fields; },
|
2011-08-24 02:04:35 +00:00
|
|
|
isSeeding: function() { return this.getStatus() === Torrent._StatusSeed; },
|
|
|
|
isStopped: function() { return this.getStatus() === Torrent._StatusStopped; },
|
|
|
|
isChecking: function() { return this.getStatus() === Torrent._StatusCheck; },
|
|
|
|
isDownloading: function() { return this.getStatus() === Torrent._StatusDownload; },
|
|
|
|
isDone: function() { return this.getLeftUntilDone() < 1; },
|
|
|
|
needsMetaData: function(){ return this.getMetadataPercentComplete() < 1; },
|
|
|
|
getActivity: function() { return this.getDownloadSpeed() + this.getUploadSpeed(); },
|
|
|
|
getPercentDoneStr: function() { return Transmission.fmt.percentString(100*this.getPercentDone()); },
|
2011-08-27 21:35:19 +00:00
|
|
|
getPercentDone: function() { return this.fields.percentDone; },
|
2011-08-20 21:17:12 +00:00
|
|
|
getStateString: function() {
|
|
|
|
switch(this.getStatus()) {
|
2011-08-24 02:04:35 +00:00
|
|
|
case Torrent._StatusStopped: return this.isFinished() ? 'Seeding complete' : 'Paused';
|
|
|
|
case Torrent._StatusCheckWait: return 'Queued for verification';
|
|
|
|
case Torrent._StatusCheck: return 'Verifying local data';
|
|
|
|
case Torrent._StatusDownloadWait: return 'Queued for download';
|
|
|
|
case Torrent._StatusDownload: return 'Downloading';
|
|
|
|
case Torrent._StatusSeedWait: return 'Queued for seeding';
|
|
|
|
case Torrent._StatusSeed: return 'Seeding';
|
2011-08-26 23:34:43 +00:00
|
|
|
case null:
|
|
|
|
case undefined: return 'Unknown';
|
|
|
|
default: return 'Error';
|
2011-08-20 21:17:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
seedRatioLimit: function(controller){
|
|
|
|
switch(this.getSeedRatioMode()) {
|
2011-08-24 02:04:35 +00:00
|
|
|
case Torrent._RatioUseGlobal: return controller.seedRatioLimit();
|
|
|
|
case Torrent._RatioUseLocal: return this.getSeedRatioLimit();
|
|
|
|
default: return -1;
|
2011-08-20 21:17:12 +00:00
|
|
|
}
|
|
|
|
},
|
2011-08-28 13:57:25 +00:00
|
|
|
getErrorMessage: function() {
|
2011-08-24 02:04:35 +00:00
|
|
|
var str = this.getErrorString();
|
2011-08-20 21:17:12 +00:00
|
|
|
switch(this.getError()) {
|
|
|
|
case Torrent._ErrTrackerWarning:
|
2011-08-24 02:04:35 +00:00
|
|
|
return 'Tracker returned a warning: ' + str;
|
2011-08-20 21:17:12 +00:00
|
|
|
case Torrent._ErrTrackerError:
|
2011-08-24 02:04:35 +00:00
|
|
|
return 'Tracker returned an error: ' + str;
|
2011-08-20 21:17:12 +00:00
|
|
|
case Torrent._ErrLocalError:
|
2011-08-24 02:04:35 +00:00
|
|
|
return 'Error: ' + str;
|
2011-08-20 21:17:12 +00:00
|
|
|
default:
|
2011-08-24 02:04:35 +00:00
|
|
|
return null;
|
2011-08-20 21:17:12 +00:00
|
|
|
}
|
|
|
|
},
|
2011-08-26 01:58:12 +00:00
|
|
|
getCollatedName: function() {
|
|
|
|
var f = this.fields;
|
2011-08-27 00:22:56 +00:00
|
|
|
if (!f.collatedName && f.name)
|
|
|
|
f.collatedName = f.name.toLowerCase();
|
2011-08-26 22:59:42 +00:00
|
|
|
return f.collatedName || '';
|
2011-08-26 01:58:12 +00:00
|
|
|
},
|
|
|
|
getCollatedTrackers: function() {
|
|
|
|
var f = this.fields;
|
2011-08-27 00:22:56 +00:00
|
|
|
if (!f.collatedTrackers && f.trackers)
|
|
|
|
f.collatedTrackers = this.collateTrackers(f.trackers);
|
2011-08-26 22:59:42 +00:00
|
|
|
return f.collatedTrackers || '';
|
2011-08-26 01:58:12 +00:00
|
|
|
},
|
2011-08-20 21:17:12 +00:00
|
|
|
|
|
|
|
/****
|
|
|
|
*****
|
|
|
|
****/
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
testState: function(state)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
var s = this.getStatus();
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
switch(state)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
2010-06-17 04:38:03 +00:00
|
|
|
case Prefs._FilterActive:
|
2011-08-24 02:04:35 +00:00
|
|
|
return this.getPeersGettingFromUs() > 0
|
2011-08-20 21:17:12 +00:00
|
|
|
|| this.getPeersSendingToUs() > 0
|
|
|
|
|| this.getWebseedsSendingToUs() > 0
|
2011-08-24 02:04:35 +00:00
|
|
|
|| this.isChecking();
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._FilterSeeding:
|
2011-08-24 02:04:35 +00:00
|
|
|
return (s === Torrent._StatusSeed)
|
|
|
|
|| (s === Torrent._StatusSeedWait);
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._FilterDownloading:
|
2011-08-24 02:04:35 +00:00
|
|
|
return (s === Torrent._StatusDownload)
|
|
|
|
|| (s === Torrent._StatusDownloadWait);
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._FilterPaused:
|
2011-08-24 02:04:35 +00:00
|
|
|
return this.isStopped();
|
2010-09-03 00:20:40 +00:00
|
|
|
case Prefs._FilterFinished:
|
2011-08-24 02:04:35 +00:00
|
|
|
return this.isFinished();
|
2008-07-10 23:57:46 +00:00
|
|
|
default:
|
2011-08-24 02:04:35 +00:00
|
|
|
return true;
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
},
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-24 02:04:35 +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(state, search, tracker)
|
|
|
|
{
|
|
|
|
// flter by state...
|
|
|
|
var pass = this.testState(state);
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
// maybe filter by text...
|
|
|
|
if (pass && search && search.length)
|
|
|
|
pass = this.getCollatedName().indexOf(search.toLowerCase()) !== -1;
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
// maybe filter by tracker...
|
|
|
|
if (pass && tracker && tracker.length)
|
|
|
|
pass = this.getCollatedTrackers().indexOf(tracker) !== -1;
|
|
|
|
|
|
|
|
return pass;
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
2011-08-24 02:04:35 +00:00
|
|
|
};
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2011-08-01 22:24:24 +00:00
|
|
|
|
2011-08-20 21:17:12 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
**** SORTING
|
|
|
|
****
|
|
|
|
***/
|
2008-07-10 23:57:46 +00:00
|
|
|
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.compareById = function(ta, tb)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
return ta.getId() - tb.getId();
|
|
|
|
};
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.compareByName = function(ta, tb)
|
|
|
|
{
|
2011-08-30 07:27:59 +00:00
|
|
|
return ta.getCollatedName().localeCompare(tb.getCollatedName())
|
2011-08-26 01:58:12 +00:00
|
|
|
|| Torrent.compareById(ta, tb);
|
2011-08-24 02:04:35 +00:00
|
|
|
};
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.compareByQueue = function(ta, tb)
|
|
|
|
{
|
2011-08-24 02:04:35 +00:00
|
|
|
return ta.getQueuePosition() - tb.getQueuePosition();
|
|
|
|
};
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.compareByAge = function(ta, tb)
|
|
|
|
{
|
2011-08-28 13:57:25 +00:00
|
|
|
var a = ta.getDateAdded(),
|
|
|
|
b = tb.getDateAdded();
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
return (b - a) || Torrent.compareByQueue(ta, tb);
|
|
|
|
};
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.compareByState = function(ta, tb)
|
2011-08-01 22:24:24 +00:00
|
|
|
{
|
2011-08-28 13:57:25 +00:00
|
|
|
var a = ta.getStatus(),
|
|
|
|
b = tb.getStatus();
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
return (b - a) || Torrent.compareByQueue(ta, tb);
|
|
|
|
};
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.compareByActivity = function(ta, tb)
|
|
|
|
{
|
2011-08-28 13:57:25 +00:00
|
|
|
var a = ta.getActivity(),
|
|
|
|
b = tb.getActivity();
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
return (b - a) || Torrent.compareByState(ta, tb);
|
|
|
|
};
|
|
|
|
Torrent.compareByRatio = function(ta, tb)
|
2011-08-20 21:17:12 +00:00
|
|
|
{
|
2011-08-28 13:57:25 +00:00
|
|
|
var a = ta.getUploadRatio(),
|
|
|
|
b = tb.getUploadRatio();
|
|
|
|
|
2011-08-24 19:20:22 +00:00
|
|
|
if (a < b) return 1;
|
|
|
|
if (a > b) return -1;
|
|
|
|
return Torrent.compareByState(ta, tb);
|
2011-08-24 02:04:35 +00:00
|
|
|
};
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.compareByProgress = function(ta, tb)
|
|
|
|
{
|
2011-08-28 13:57:25 +00:00
|
|
|
var a = ta.getPercentDone(),
|
|
|
|
b = tb.getPercentDone();
|
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
return (a - b) || Torrent.compareByRatio(ta, tb);
|
|
|
|
};
|
2011-06-10 14:04:32 +00:00
|
|
|
|
2011-08-27 21:35:19 +00:00
|
|
|
Torrent.compareTorrents = function(a, b, sortMethod, sortDirection)
|
|
|
|
{
|
|
|
|
var i;
|
|
|
|
|
|
|
|
switch(sortMethod)
|
|
|
|
{
|
|
|
|
case Prefs._SortByActivity:
|
|
|
|
i = Torrent.compareByActivity(a,b);
|
|
|
|
break;
|
|
|
|
case Prefs._SortByAge:
|
|
|
|
i = Torrent.compareByAge(a,b);
|
|
|
|
break;
|
|
|
|
case Prefs._SortByQueue:
|
|
|
|
i = Torrent.compareByQueue(a,b);
|
|
|
|
break;
|
|
|
|
case Prefs._SortByProgress:
|
|
|
|
i = Torrent.compareByProgress(a,b);
|
|
|
|
break;
|
|
|
|
case Prefs._SortByState:
|
|
|
|
i = Torrent.compareByState(a,b);
|
|
|
|
break;
|
|
|
|
case Prefs._SortByRatio:
|
|
|
|
i = Torrent.compareByRatio(a,b);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
i = Torrent.compareByName(a,b);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sortDirection === Prefs._SortDescending)
|
|
|
|
i = -i;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
*/
|
2011-08-20 21:17:12 +00:00
|
|
|
Torrent.sortTorrents = function(torrents, sortMethod, sortDirection)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
2011-08-20 21:17:12 +00:00
|
|
|
switch(sortMethod)
|
2008-07-10 23:57:46 +00:00
|
|
|
{
|
|
|
|
case Prefs._SortByActivity:
|
2011-08-24 02:04:35 +00:00
|
|
|
torrents.sort(this.compareByActivity);
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._SortByAge:
|
2011-08-24 02:04:35 +00:00
|
|
|
torrents.sort(this.compareByAge);
|
|
|
|
break;
|
2011-08-02 03:59:54 +00:00
|
|
|
case Prefs._SortByQueue:
|
2011-08-24 02:04:35 +00:00
|
|
|
torrents.sort(this.compareByQueue);
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._SortByProgress:
|
2011-08-24 02:04:35 +00:00
|
|
|
torrents.sort(this.compareByProgress);
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
case Prefs._SortByState:
|
2011-08-24 02:04:35 +00:00
|
|
|
torrents.sort(this.compareByState);
|
|
|
|
break;
|
2011-06-10 14:04:32 +00:00
|
|
|
case Prefs._SortByRatio:
|
2011-08-24 02:04:35 +00:00
|
|
|
torrents.sort(this.compareByRatio);
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
default:
|
2011-08-24 02:04:35 +00:00
|
|
|
torrents.sort(this.compareByName);
|
|
|
|
break;
|
2008-07-10 23:57:46 +00:00
|
|
|
}
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
if (sortDirection === Prefs._SortDescending)
|
|
|
|
torrents.reverse();
|
2010-06-21 13:14:33 +00:00
|
|
|
|
2011-08-24 02:04:35 +00:00
|
|
|
return torrents;
|
|
|
|
};
|