url += "&dummy_param=1";
}
URL torrent_url = new URL( url );
Torrent torrent;
try{
TorrentDownloader dl =
plugin_interface.getTorrentManager().getURLDownloader( torrent_url, null, null );
torrent = dl.download( Constants.DEFAULT_ENCODING );
}catch( Throwable e ){
e.printStackTrace();
throw( new IOException( "torrent download failed: " + Debug.getNestedExceptionMessage( e )));
}
Download download = addTorrent( torrent, add_stopped );
JSONObject torrent_details = new JSONObject();
torrent_details.put( "id", new Long( getID( download )));
torrent_details.put( "name", escapeXML( download.getName()));
torrent_details.put( "hashString", ByteFormatter.encodeString( torrent.getHash()));
result.put( "torrent-added", torrent_details );
}else if ( method.equals( "torrent-start" )){
checkUpdatePermissions();
Object ids = args.get( "ids" );
List<Download> downloads = getDownloads( ids );
for ( Download download: downloads ){
try{
int state = download.getState();
if ( state != Download.ST_DOWNLOADING && state != Download.ST_SEEDING ){
download.restart();
}
}catch( Throwable e ){
}
}
}else if ( method.equals( "torrent-stop" )){
checkUpdatePermissions();
Object ids = args.get( "ids" );
List<Download> downloads = getDownloads( ids );
for ( Download download: downloads ){
try{
int state = download.getState();
if ( state != Download.ST_STOPPED ){
download.stop();
}
}catch( Throwable e ){
}
}
}else if ( method.equals( "torrent-verify" )){
checkUpdatePermissions();
Object ids = args.get( "ids" );
List<Download> downloads = getDownloads( ids );
for ( Download download: downloads ){
try{
int state = download.getState();
if ( state != Download.ST_STOPPED ){
download.stop();
}
download.recheckData();
}catch( Throwable e ){
}
}
}else if ( method.equals( "torrent-remove" )){
checkUpdatePermissions();
Object ids = args.get( "ids" );
boolean delete_data = getBoolean( args.get( "delete-local-data" ));
List<Download> downloads = getDownloads( ids );
for ( Download download: downloads ){
try{
int state = download.getState();
if ( state != Download.ST_STOPPED ){
download.stop();
}
if ( delete_data ){
download.remove( true, true );
}else{
download.remove();
}
synchronized( recently_removed ){
recently_removed.add( getID( download ));
}
}catch( Throwable e ){
}
}
}else if ( method.equals( "torrent-set" )){
Object ids = args.get( "ids" );
if ( ids != null && ids instanceof String && ((String)ids).equals( "recently-active" )){
synchronized( recently_removed ){
if ( recently_removed.size() > 0 ){
List<Long> removed = new ArrayList<Long>( recently_removed );
recently_removed.clear();
result.put( "removed", removed );
}
}
}
List<Download> downloads = getDownloads( ids );
JSONArray files_unwanted = (JSONArray)args.get( "files-unwanted" );
JSONArray files_wanted = (JSONArray)args.get( "files-wanted" );
JSONArray priority_high = (JSONArray)args.get( "priority-high" );
JSONArray priority_normal = (JSONArray)args.get( "priority-normal" );
JSONArray priority_low = (JSONArray)args.get( "priority-low" );
Long speed_limit_down = (Long)args.get( "speedLimitDownload" );
Long speed_limit_up = (Long)args.get( "speedLimitUpload" );
Long l_uploaded_ever = (Long)args.get( "uploadedEver" );
Long l_downloaded_ever = (Long)args.get( "downloadedEver" );
long uploaded_ever = l_uploaded_ever==null?-1:l_uploaded_ever.longValue();
long downloaded_ever = l_downloaded_ever==null?-1:l_downloaded_ever.longValue();
for ( Download download: downloads ){
Torrent t = download.getTorrent();
if ( t == null ){
continue;
}
if ( speed_limit_down != null ){
download.setDownloadRateLimitBytesPerSecond( speed_limit_down.intValue());
}
if ( speed_limit_up != null ){
download.setUploadRateLimitBytesPerSecond( speed_limit_up.intValue());
}
DownloadManager core_download = PluginCoreUtils.unwrap( download );
DiskManagerFileInfo[] files = core_download.getDiskManagerFileInfo();
if ( files_unwanted != null ){
for ( int i=0;i<files_unwanted.size();i++){
int index = ((Long)files_unwanted.get( i )).intValue();
if ( index >= 0 && index <= files.length ){
files[index].setSkipped( true );
}
}
}
if ( files_wanted != null ){
for ( int i=0;i<files_wanted.size();i++){
int index = ((Long)files_wanted.get( i )).intValue();
if ( index >= 0 && index <= files.length ){
files[index].setSkipped( false );
}
}
}
if ( priority_high != null ){
for ( int i=0;i<priority_high.size();i++){
int index = ((Long)priority_high.get( i )).intValue();
if ( index >= 0 && index <= files.length ){
files[index].setPriority( 1 );
}
}
}
if ( priority_normal != null ){
for ( int i=0;i<priority_normal.size();i++){
int index = ((Long)priority_normal.get( i )).intValue();
if ( index >= 0 && index <= files.length ){
files[index].setPriority( 0 );
}
}
}
if ( priority_low != null ){
for ( int i=0;i<priority_low.size();i++){
int index = ((Long)priority_low.get( i )).intValue();
if ( index >= 0 && index <= files.length ){
files[index].setPriority( 0 );
}
}
}
if ( uploaded_ever != -1 || downloaded_ever != -1 ){
// new method in 4511 B31
try{
download.getStats().resetUploadedDownloaded( uploaded_ever, downloaded_ever );
}catch( Throwable e ){
}
}
}
}else if ( method.equals( "torrent-get" )){
List<String> fields = (List<String>)args.get( "fields" );
if ( fields == null ){
fields = new ArrayList();
}
Object ids = args.get( "ids" );
if ( ids != null && ids instanceof String && ((String)ids).equals( "recently-active" )){
synchronized( recently_removed ){
if ( recently_removed.size() > 0 ){
List<Long> removed = new ArrayList<Long>( recently_removed );
recently_removed.clear();
result.put( "removed", removed );
}
}
}
List<Download> downloads = getDownloads( ids );
JSONArray torrents = new JSONArray();
result.put( "torrents", torrents );
for ( Download download: downloads ){
Torrent t = download.getTorrent();
if ( t == null ){
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
// 2 - verifying
// 4 - downloading
// 5 - queued (incomplete)
// 8 - seeding
// 9 - queued (complete)
// 16 - paused
int status_int = 7;
if ( download.isPaused()){
status_int = 16;
}else{
int state = download.getState();
if ( state == Download.ST_DOWNLOADING ){
status_int = 4;
}else if ( state == Download.ST_SEEDING ){
status_int = 8;
}else if ( state == Download.ST_QUEUED ){
if ( download.isComplete()){
status_int = 9;
}else{
status_int = 5;
}
}else if ( state == Download.ST_STOPPED || state == Download.ST_STOPPING ){
status_int = 16;
}else if ( state == Download.ST_ERROR ){
status_int = 0;
}else{
if ( core_download.getState() == DownloadManager.STATE_CHECKING ){
status_int = 2;
}else{
status_int = 1;
}
}
}
value = new Long(status_int);
}else if ( field.equals( "swarmSpeed" )){
value = new Long( core_download.getStats().getTotalAveragePerPeer());
}else if ( field.equals( "totalSize" )){
value = new Long( t.getSize());
}else if ( field.equals( "pieceCount" )){
value = new Long( t.getPieceCount());
}else if ( field.equals( "pieceSize" )){
value = new Long( t.getPieceSize());
}else if ( field.equals( "metadataPercentComplete" )){
value = new Long( 100 );
}else if ( field.equals( "uploadedEver" )){
value = new Long( stats.getUploaded());
}else if ( field.equals( "recheckProgress" )){