continue;
}
DownloadManager core_download = PluginCoreUtils.unwrap( download );
PEPeerManager pm = core_download.getPeerManager();
DownloadStats stats = download.getStats();
JSONObject torrent = new JSONObject();
torrents.add( torrent );
int peers_from_us = 0;
int peers_to_us = 0;
if ( pm != null ){
List<PEPeer> peers = pm.getPeers();
for ( PEPeer peer: peers ){
PEPeerStats pstats = peer.getStats();
if ( pstats.getDataReceiveRate() > 0 ){
peers_to_us++;
}
if ( pstats.getDataSendRate() > 0 ){
peers_from_us++;
}
}
}
for ( String field: fields ){
Object value = null;
if ( field.equals( "addedDate" )){
value = new Long(core_download.getDownloadState().getLongParameter(DownloadManagerState.PARAM_DOWNLOAD_ADDED_TIME)/1000);
}else if ( field.equals( "announceURL" )){
value = t.getAnnounceURL().toExternalForm();
}else if ( field.equals( "comment" )){
value = t.getComment();
}else if ( field.equals( "creator" )){
value = t.getCreatedBy();
}else if ( field.equals( "dateCreated" )){
value = new Long( t.getCreationDate());
}else if ( field.equals( "downloadedEver" )){
value = new Long( stats.getDownloaded() + stats.getDiscarded() + stats.getHashFails());
}else if ( field.equals( "error" )){
String str = download.getErrorStateDetails();
// o = none, 1=tracker warn,2=tracker error,2=other
if ( str != null && str.length() > 0 ){
value = new Long(3);
}else{
value = ZERO;
TRTrackerAnnouncer tracker_client = core_download.getTrackerClient();
if ( tracker_client != null ){
TRTrackerAnnouncerResponse x = tracker_client.getBestAnnouncer().getLastResponse();
if ( x != null ){
if ( x.getStatus() == TRTrackerAnnouncerResponse.ST_REPORTED_ERROR ){
value = new Long(2);
}
}
}else{
DownloadScrapeResult x = download.getLastScrapeResult();
if ( x != null ){
if ( x.getResponseType() == DownloadScrapeResult.RT_ERROR ){
String status = x.getStatus();
if ( status != null && status.length() > 0 ){
value = new Long(2);
}
}
}
}
}
}else if ( field.equals( "errorString" )){
String str = download.getErrorStateDetails();
if ( str != null && str.length() > 0 ){
value = str;
}else{
value = "";
TRTrackerAnnouncer tracker_client = core_download.getTrackerClient();
if ( tracker_client != null ){
TRTrackerAnnouncerResponse x = tracker_client.getBestAnnouncer().getLastResponse();
if ( x != null ){
if ( x.getStatus() == TRTrackerAnnouncerResponse.ST_REPORTED_ERROR ){
value = x.getStatusString();
}
}
}else{
DownloadScrapeResult x = download.getLastScrapeResult();
if ( x != null ){
if ( x.getResponseType() == DownloadScrapeResult.RT_ERROR ){
value = x.getStatus();
}
}
}
}
}else if ( field.equals( "eta" )){
// infinite -> 215784000
long eta_secs = stats.getETASecs();
eta_secs = Math.min( eta_secs, 215784000 );
value = new Long( eta_secs );
}else if ( field.equals( "hashString" )){
value = ByteFormatter.encodeString( t.getHash());
}else if ( field.equals( "haveUnchecked" )){
value = ZERO;
}else if ( field.equals( "haveValid" )){
value = new Long( stats.getDownloaded());
}else if ( field.equals( "id" )){
value = new Long( getID( download ));
}else if ( field.equals( "trackerSeeds" )){
DownloadScrapeResult scrape = download.getLastScrapeResult();
value = new Long( scrape==null?0:scrape.getSeedCount());
}else if ( field.equals( "trackerLeechers" )){
DownloadScrapeResult scrape = download.getLastScrapeResult();
value = new Long( scrape==null?0:scrape.getNonSeedCount());
}else if ( field.equals( "leechers" )){
if ( pm == null ){
value = new Long(0);
}else{
value = new Long( pm.getNbPeers());
}
}else if ( field.equals( "leftUntilDone" )){
value = new Long( stats.getRemaining());
}else if ( field.equals( "name" )){
value = download.getName();
}else if ( field.equals( "peersConnected" )){
if ( pm == null ){
value = new Long(0);
}else{
value = new Long( pm.getNbPeers() + pm.getNbSeeds());
}
}else if ( field.equals( "peersGettingFromUs" )){
value = new Long( peers_from_us );
}else if ( field.equals( "peersSendingToUs" )){
value = new Long( peers_to_us );
}else if ( field.equals( "isPrivate" )){
value = t.isPrivate()?TRUE:FALSE;
}else if ( field.equals( "rateDownload" )){
value = new Long( stats.getDownloadAverage());
}else if ( field.equals( "rateUpload" )){
value = new Long( stats.getUploadAverage());
}else if ( field.equals( "speedLimitDownload" )){
value = new Long( download.getDownloadRateLimitBytesPerSecond());
}else if ( field.equals( "speedLimitUpload" )){
value = new Long( download.getUploadRateLimitBytesPerSecond());
}else if ( field.equals( "seeders" )){
if ( pm == null ){
value = new Long(-1);
}else{
value = new Long( pm.getNbSeeds());
}
}else if ( field.equals( "sizeWhenDone" )){
value = new Long( t.getSize()); // TODO: excluded DND
}else if ( field.equals( "status" )){
// 1 - waiting to verify