Browse Source

Protect setLabel in case plugin isn't installed.

pull/407/head
Alon Albert 7 years ago
parent
commit
c7a6946e0a
  1. 8
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java
  2. 5
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java
  3. 23
      app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java

8
app/src/main/java/org/transdroid/daemon/Deluge/DelugeAdapter.java

@ -69,8 +69,8 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_TIER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_URL; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER;
import com.android.internalcopy.http.multipart.FilePart; import com.android.internalcopy.http.multipart.FilePart;
@ -388,8 +388,8 @@ public class DelugeAdapter implements IDaemonAdapter {
// Build an JSON arrays of objcts that each have a tier (order) number and an url // Build an JSON arrays of objcts that each have a tier (order) number and an url
for (int i = 0; i < trackersTask.getNewTrackers().size(); i++) { for (int i = 0; i < trackersTask.getNewTrackers().size(); i++) {
JSONObject trackerObj = new JSONObject(); JSONObject trackerObj = new JSONObject();
trackerObj.put(RPC_TRACKER_TIER, i); trackerObj.put(RPC_TIER, i);
trackerObj.put(RPC_TRACKER_URL, trackersTask.getNewTrackers().get(i)); trackerObj.put(RPC_URL, trackersTask.getNewTrackers().get(i));
trackers.put(trackerObj); trackers.put(trackerObj);
} }
params.put(new JSONArray().put(task.getTargetTorrent().getUniqueID())); params.put(new JSONArray().put(task.getTargetTorrent().getUniqueID()));

5
app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java

@ -28,6 +28,7 @@ import org.transdroid.daemon.TorrentStatus;
* @author alon.albert * @author alon.albert
*/ */
class DelugeCommon { class DelugeCommon {
static final String RPC_DETAILS = "files"; static final String RPC_DETAILS = "files";
static final String RPC_DOWNLOADEDEVER = "total_done"; static final String RPC_DOWNLOADEDEVER = "total_done";
static final String RPC_ETA = "eta"; static final String RPC_ETA = "eta";
@ -81,11 +82,11 @@ class DelugeCommon {
static final String RPC_TORRENTS = "torrents"; static final String RPC_TORRENTS = "torrents";
static final String RPC_TOTALPEERS = "total_peers"; static final String RPC_TOTALPEERS = "total_peers";
static final String RPC_TOTALSEEDS = "total_seeds"; static final String RPC_TOTALSEEDS = "total_seeds";
static final String RPC_TIER = "tier";
static final String RPC_TOTALSIZE = "total_size"; static final String RPC_TOTALSIZE = "total_size";
static final String RPC_TRACKERS = "trackers"; static final String RPC_TRACKERS = "trackers";
static final String RPC_TRACKER_STATUS = "tracker_status"; static final String RPC_TRACKER_STATUS = "tracker_status";
static final String RPC_TRACKER_TIER = "tier"; static final String RPC_URL = "url";
static final String RPC_TRACKER_URL = "url";
static final String RPC_UPLOADEDEVER = "total_uploaded"; static final String RPC_UPLOADEDEVER = "total_uploaded";
static final String[] RPC_DETAILS_FIELDS_ARRAY = { static final String[] RPC_DETAILS_FIELDS_ARRAY = {

23
app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java

@ -66,8 +66,8 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSIZE;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKERS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_STATUS;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_TIER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_URL; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL;
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -209,9 +209,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
final List<Torrent> torrents = getTorrents(torrentsStatus.values()); final List<Torrent> torrents = getTorrents(torrentsStatus.values());
// Check if Label plugin is enabled // Check if Label plugin is enabled
//noinspection unchecked final boolean hasLabelPlugin = hasMethod(client, RPC_METHOD_GET_LABELS);
final List<String> methods = (List<String>) client.sendRequest(RPC_METHOD_GET_METHOD_LIST);
final boolean hasLabelPlugin = methods.contains(RPC_METHOD_GET_LABELS);
// Get label list from server // Get label list from server
//noinspection unchecked //noinspection unchecked
@ -239,7 +237,7 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
.get(RPC_TRACKERS); .get(RPC_TRACKERS);
final List<String> trackers = new ArrayList<>(); final List<String> trackers = new ArrayList<>();
for (Map<String, Object> trackerResponse : trackerResponses) { for (Map<String, Object> trackerResponse : trackerResponses) {
trackers.add((String) trackerResponse.get(RPC_TRACKER_URL)); trackers.add((String) trackerResponse.get(RPC_URL));
} }
return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails( return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails(
@ -295,6 +293,9 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
@NonNull @NonNull
private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) throws DaemonException { private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) throws DaemonException {
if (!hasMethod(client, RPC_METHOD_SETLABEL)) {
throw new DaemonException(ExceptionType.MethodUnsupported, "Label plugin not installed");
}
final String torrentId = task.getTargetTorrent().getUniqueID(); final String torrentId = task.getTargetTorrent().getUniqueID();
final String label = task.getNewLabel() == null ? "" : task.getNewLabel(); final String label = task.getNewLabel() == null ? "" : task.getNewLabel();
client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label); client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label);
@ -345,8 +346,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
final ArrayList<String> newTrackers = task.getNewTrackers(); final ArrayList<String> newTrackers = task.getNewTrackers();
for (int i = 0, n = newTrackers.size(); i < n; i++) { for (int i = 0, n = newTrackers.size(); i < n; i++) {
final Map<String, Object> tracker = new HashMap<>(); final Map<String, Object> tracker = new HashMap<>();
tracker.put(RPC_TRACKER_TIER, i); tracker.put(RPC_TIER, i);
tracker.put(RPC_TRACKER_URL, newTrackers.get(i)); tracker.put(RPC_URL, newTrackers.get(i));
trackers.add(tracker); trackers.add(tracker);
} }
client.sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers); client.sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers);
@ -539,6 +540,12 @@ public class DelugeRpcAdapter implements IDaemonAdapter {
return new String[]{task.getTargetTorrent().getUniqueID()}; return new String[]{task.getTargetTorrent().getUniqueID()};
} }
private boolean hasMethod(DelugeRpcClient client, String method) throws DaemonException {
//noinspection unchecked
final List<String> methods = (List<String>) client.sendRequest(RPC_METHOD_GET_METHOD_LIST);
return methods.contains(method);
}
/** /**
* Used to count torrents in labels. * Used to count torrents in labels.
*/ */

Loading…
Cancel
Save