|
|
@ -1,23 +1,77 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* This file is part of Transdroid <http://www.transdroid.org>
|
|
|
|
* This file is part of Transdroid <http://www.transdroid.org>
|
|
|
|
* |
|
|
|
* |
|
|
|
* Transdroid is free software: you can redistribute it and/or modify |
|
|
|
* Transdroid is free software: you can redistribute it and/or modify |
|
|
|
* it under the terms of the GNU General Public License as published by |
|
|
|
* it under the terms of the GNU General Public License as published by |
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
|
|
* (at your option) any later version. |
|
|
|
* (at your option) any later version. |
|
|
|
* |
|
|
|
* |
|
|
|
* Transdroid is distributed in the hope that it will be useful, |
|
|
|
* Transdroid is distributed in the hope that it will be useful, |
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
* GNU General Public License for more details. |
|
|
|
* GNU General Public License for more details. |
|
|
|
* |
|
|
|
* |
|
|
|
* You should have received a copy of the GNU General Public License |
|
|
|
* You should have received a copy of the GNU General Public License |
|
|
|
* along with Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* along with Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* |
|
|
|
* |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
package org.transdroid.daemon.Deluge; |
|
|
|
package org.transdroid.daemon.Deluge; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.support.annotation.NonNull; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.base64.android.Base64; |
|
|
|
|
|
|
|
import org.transdroid.core.gui.log.Log; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Daemon; |
|
|
|
|
|
|
|
import org.transdroid.daemon.DaemonException; |
|
|
|
|
|
|
|
import org.transdroid.daemon.DaemonException.ExceptionType; |
|
|
|
|
|
|
|
import org.transdroid.daemon.DaemonSettings; |
|
|
|
|
|
|
|
import org.transdroid.daemon.IDaemonAdapter; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Label; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Priority; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Torrent; |
|
|
|
|
|
|
|
import org.transdroid.daemon.TorrentDetails; |
|
|
|
|
|
|
|
import org.transdroid.daemon.TorrentFile; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.AddByFileTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.AddByMagnetUrlTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.AddByUrlTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTaskFailureResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTaskResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.ForceRecheckTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetFileListTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetFileListTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetTorrentDetailsTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.RemoveTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.RetrieveTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.RetrieveTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetDownloadLocationTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetFilePriorityTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetLabelTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetTrackersTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetTransferRatesTask; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream; |
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collection; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.HashSet; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.Map.Entry; |
|
|
|
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS; |
|
|
|
|
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS_FIELDS_ARRAY; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DOWNLOADEDEVER; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DOWNLOADEDEVER; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_ETA; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_ETA; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FIELDS_ARRAY; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FIELDS_ARRAY; |
|
|
@ -59,510 +113,414 @@ import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_RATEUPLOAD; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SAVEPATH; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SAVEPATH; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SIZE; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_SIZE; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS_FIELDS_ARRAY; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIER; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIMEADDED; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TIMEADDED; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALPEERS; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALPEERS; |
|
|
|
import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TOTALSEEDS; |
|
|
|
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_TIER; |
|
|
|
|
|
|
|
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 static org.transdroid.daemon.Deluge.DelugeCommon.RPC_URL; |
|
|
|
import android.support.annotation.NonNull; |
|
|
|
|
|
|
|
import java.io.BufferedInputStream; |
|
|
|
|
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
|
|
|
import java.io.FileNotFoundException; |
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collection; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.Date; |
|
|
|
|
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.HashSet; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import java.util.Map.Entry; |
|
|
|
|
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
import org.base64.android.Base64; |
|
|
|
|
|
|
|
import org.transdroid.core.gui.log.Log; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Daemon; |
|
|
|
|
|
|
|
import org.transdroid.daemon.DaemonException; |
|
|
|
|
|
|
|
import org.transdroid.daemon.DaemonException.ExceptionType; |
|
|
|
|
|
|
|
import org.transdroid.daemon.DaemonSettings; |
|
|
|
|
|
|
|
import org.transdroid.daemon.IDaemonAdapter; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Label; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Priority; |
|
|
|
|
|
|
|
import org.transdroid.daemon.Torrent; |
|
|
|
|
|
|
|
import org.transdroid.daemon.TorrentDetails; |
|
|
|
|
|
|
|
import org.transdroid.daemon.TorrentFile; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.AddByFileTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.AddByMagnetUrlTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.AddByUrlTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTaskFailureResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTaskResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.DaemonTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.ForceRecheckTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetFileListTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetFileListTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetTorrentDetailsTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.RemoveTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.RetrieveTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.RetrieveTaskSuccessResult; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetDownloadLocationTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetFilePriorityTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetLabelTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetTrackersTask; |
|
|
|
|
|
|
|
import org.transdroid.daemon.task.SetTransferRatesTask; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* The daemon adapter from the Deluge torrent client using deluged API directly. |
|
|
|
* The daemon adapter from the Deluge torrent client using deluged API directly. |
|
|
|
* |
|
|
|
|
|
|
|
* @author alon.albert |
|
|
|
* @author alon.albert |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class DelugeRpcAdapter implements IDaemonAdapter { |
|
|
|
public class DelugeRpcAdapter implements IDaemonAdapter { |
|
|
|
|
|
|
|
|
|
|
|
public static final int DEFAULT_PORT = 58846; |
|
|
|
public static final int DEFAULT_PORT = 58846; |
|
|
|
|
|
|
|
|
|
|
|
private final DaemonSettings settings; |
|
|
|
private final DaemonSettings settings; |
|
|
|
|
|
|
|
|
|
|
|
private int version = -1; |
|
|
|
private int version = -1; |
|
|
|
|
|
|
|
|
|
|
|
public DelugeRpcAdapter(DaemonSettings settings) { |
|
|
|
public DelugeRpcAdapter(DaemonSettings settings) { |
|
|
|
this.settings = settings; |
|
|
|
this.settings = settings; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DaemonTaskResult executeTask(Log log, DaemonTask task) { |
|
|
|
public DaemonTaskResult executeTask(Log log, DaemonTask task) { |
|
|
|
final DelugeRpcClient client = new DelugeRpcClient(); |
|
|
|
final DelugeRpcClient client = new DelugeRpcClient(); |
|
|
|
try { |
|
|
|
try { |
|
|
|
client.connect(settings.getAddress(), settings.getPort(), settings.getUsername(), settings.getPassword()); |
|
|
|
client.connect(settings); |
|
|
|
switch (task.getMethod()) { |
|
|
|
switch (task.getMethod()) { |
|
|
|
case Retrieve: |
|
|
|
case Retrieve: |
|
|
|
return doRetrieve(client, (RetrieveTask) task); |
|
|
|
return doRetrieve(client, (RetrieveTask) task); |
|
|
|
case AddByUrl: |
|
|
|
case AddByUrl: |
|
|
|
return doAddByUrl(client, (AddByUrlTask) task); |
|
|
|
return doAddByUrl(client, (AddByUrlTask) task); |
|
|
|
case AddByMagnetUrl: |
|
|
|
case AddByMagnetUrl: |
|
|
|
return doAddByMagnetUrl(client, (AddByMagnetUrlTask) task); |
|
|
|
return doAddByMagnetUrl(client, (AddByMagnetUrlTask) task); |
|
|
|
case AddByFile: |
|
|
|
case AddByFile: |
|
|
|
return doAddByFile(client, (AddByFileTask) task); |
|
|
|
return doAddByFile(client, (AddByFileTask) task); |
|
|
|
case Remove: |
|
|
|
case Remove: |
|
|
|
return doRemove(client, (RemoveTask) task); |
|
|
|
return doRemove(client, (RemoveTask) task); |
|
|
|
case Pause: |
|
|
|
case Pause: |
|
|
|
return doControl(client, task, RPC_METHOD_PAUSE); |
|
|
|
return doControl(client, task, RPC_METHOD_PAUSE); |
|
|
|
case PauseAll: |
|
|
|
case PauseAll: |
|
|
|
return doControlAll(client, task, RPC_METHOD_PAUSE_ALL); |
|
|
|
return doControlAll(client, task, RPC_METHOD_PAUSE_ALL); |
|
|
|
case Resume: |
|
|
|
case Resume: |
|
|
|
return doControl(client, task, RPC_METHOD_RESUME); |
|
|
|
return doControl(client, task, RPC_METHOD_RESUME); |
|
|
|
case ResumeAll: |
|
|
|
case ResumeAll: |
|
|
|
return doControlAll(client, task, RPC_METHOD_RESUME_ALL); |
|
|
|
return doControlAll(client, task, RPC_METHOD_RESUME_ALL); |
|
|
|
case GetFileList: |
|
|
|
case GetFileList: |
|
|
|
return doGetFileList(client, (GetFileListTask) task); |
|
|
|
return doGetFileList(client, (GetFileListTask) task); |
|
|
|
case SetFilePriorities: |
|
|
|
case SetFilePriorities: |
|
|
|
return doSetFilePriorities(client, (SetFilePriorityTask) task); |
|
|
|
return doSetFilePriorities(client, (SetFilePriorityTask) task); |
|
|
|
case SetTransferRates: |
|
|
|
case SetTransferRates: |
|
|
|
return doSetTransferRates(client, (SetTransferRatesTask) task); |
|
|
|
return doSetTransferRates(client, (SetTransferRatesTask) task); |
|
|
|
case SetLabel: |
|
|
|
case SetLabel: |
|
|
|
return doSetLabel(client, (SetLabelTask) task); |
|
|
|
return doSetLabel(client, (SetLabelTask) task); |
|
|
|
case SetDownloadLocation: |
|
|
|
case SetDownloadLocation: |
|
|
|
return doSetDownloadLocation(client, (SetDownloadLocationTask) task); |
|
|
|
return doSetDownloadLocation(client, (SetDownloadLocationTask) task); |
|
|
|
case GetTorrentDetails: |
|
|
|
case GetTorrentDetails: |
|
|
|
return doGetTorrentDetails(client, (GetTorrentDetailsTask) task); |
|
|
|
return doGetTorrentDetails(client, (GetTorrentDetailsTask) task); |
|
|
|
case SetTrackers: |
|
|
|
case SetTrackers: |
|
|
|
return doSetTrackers(client, (SetTrackersTask) task); |
|
|
|
return doSetTrackers(client, (SetTrackersTask) task); |
|
|
|
case ForceRecheck: |
|
|
|
case ForceRecheck: |
|
|
|
return doForceRecheck(client, (ForceRecheckTask) task); |
|
|
|
return doForceRecheck(client, (ForceRecheckTask) task); |
|
|
|
default: |
|
|
|
default: |
|
|
|
return new DaemonTaskFailureResult(task, |
|
|
|
return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported, task.getMethod() + " is not " + |
|
|
|
new DaemonException(ExceptionType.MethodUnsupported, |
|
|
|
"supported by " + getType())); |
|
|
|
task.getMethod() + " is not supported by " + getType())); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (DaemonException e) { |
|
|
|
} catch (DaemonException e) { |
|
|
|
return new DaemonTaskFailureResult(task, e); |
|
|
|
return new DaemonTaskFailureResult(task, e); |
|
|
|
} finally { |
|
|
|
} finally { |
|
|
|
client.close(); |
|
|
|
client.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public Daemon getType() { |
|
|
|
public Daemon getType() { |
|
|
|
return Daemon.DelugeRpc; |
|
|
|
return Daemon.DelugeRpc; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public DaemonSettings getSettings() { |
|
|
|
public DaemonSettings getSettings() { |
|
|
|
return settings; |
|
|
|
return settings; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
private RetrieveTaskSuccessResult doRetrieve(DelugeRpcClient client, RetrieveTask task) throws DaemonException { |
|
|
|
private RetrieveTaskSuccessResult doRetrieve(DelugeRpcClient client, RetrieveTask task) throws DaemonException { |
|
|
|
// Get torrents
|
|
|
|
// Get torrents
|
|
|
|
//noinspection unchecked
|
|
|
|
//noinspection unchecked
|
|
|
|
final Map<String, Map<String, Object>> torrentsStatus = (Map<String, Map<String, Object>>) client.sendRequest |
|
|
|
final Map<String, Map<String, Object>> torrentsStatus = (Map<String, Map<String, Object>>) client |
|
|
|
(RPC_METHOD_GET_TORRENTS_STATUS, new HashMap<>(), RPC_FIELDS_ARRAY); |
|
|
|
.sendRequest(RPC_METHOD_GET_TORRENTS_STATUS, new HashMap<>(), RPC_FIELDS_ARRAY); |
|
|
|
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
|
|
|
|
final boolean hasLabelPlugin = hasMethod(client, RPC_METHOD_GET_LABELS); |
|
|
|
final boolean hasLabelPlugin = hasMethod(client, RPC_METHOD_GET_LABELS); |
|
|
|
|
|
|
|
|
|
|
|
// Get label list from server
|
|
|
|
// Get label list from server
|
|
|
|
//noinspection unchecked
|
|
|
|
//noinspection unchecked
|
|
|
|
final List<String> labelNames = hasLabelPlugin ? (List<String>) client.sendRequest(RPC_METHOD_GET_LABELS) : new ArrayList<String>(); |
|
|
|
final List<String> labelNames = hasLabelPlugin |
|
|
|
|
|
|
|
? (List<String>) client.sendRequest(RPC_METHOD_GET_LABELS) |
|
|
|
// Extract labels & counts from torrents.
|
|
|
|
: new ArrayList<String>(); |
|
|
|
final List<Label> labels = getLabels(labelNames, torrents); |
|
|
|
|
|
|
|
|
|
|
|
// Extract labels & counts from torrents.
|
|
|
|
return new RetrieveTaskSuccessResult(task, torrents, labels); |
|
|
|
final List<Label> labels = getLabels(labelNames, torrents); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return new RetrieveTaskSuccessResult(task, torrents, labels); |
|
|
|
private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails(DelugeRpcClient client, GetTorrentDetailsTask task) throws DaemonException { |
|
|
|
} |
|
|
|
//noinspection unchecked
|
|
|
|
|
|
|
|
final Map<String, Object> response = (Map<String, Object>) client.sendRequest(RPC_METHOD_STATUS, task.getTargetTorrent().getUniqueID(), |
|
|
|
private GetTorrentDetailsTaskSuccessResult doGetTorrentDetails(DelugeRpcClient client, |
|
|
|
RPC_DETAILS_FIELDS_ARRAY); |
|
|
|
GetTorrentDetailsTask task) |
|
|
|
|
|
|
|
throws DaemonException { |
|
|
|
//noinspection unchecked
|
|
|
|
//noinspection unchecked
|
|
|
|
final List<Map<String, Object>> trackerResponses = (List<Map<String, Object>>) response.get(RPC_TRACKERS); |
|
|
|
final Map<String, Object> response = (Map<String, Object>) client.sendRequest( |
|
|
|
final List<String> trackers = new ArrayList<>(); |
|
|
|
RPC_METHOD_STATUS, |
|
|
|
for (Map<String, Object> trackerResponse : trackerResponses) { |
|
|
|
task.getTargetTorrent().getUniqueID(), |
|
|
|
trackers.add((String) trackerResponse.get(RPC_URL)); |
|
|
|
RPC_DETAILS_FIELDS_ARRAY); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//noinspection unchecked
|
|
|
|
return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails(trackers, Collections.singletonList((String) response.get |
|
|
|
final List<Map<String, Object>> trackerResponses = (List<Map<String, Object>>) response |
|
|
|
(RPC_TRACKER_STATUS)))); |
|
|
|
.get(RPC_TRACKERS); |
|
|
|
} |
|
|
|
final List<String> trackers = new ArrayList<>(); |
|
|
|
|
|
|
|
for (Map<String, Object> trackerResponse : trackerResponses) { |
|
|
|
private GetFileListTaskSuccessResult doGetFileList(DelugeRpcClient client, GetFileListTask task) throws DaemonException { |
|
|
|
trackers.add((String) trackerResponse.get(RPC_URL)); |
|
|
|
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent()); |
|
|
|
} |
|
|
|
return new GetFileListTaskSuccessResult(task, files); |
|
|
|
|
|
|
|
} |
|
|
|
return new GetTorrentDetailsTaskSuccessResult(task, new TorrentDetails( |
|
|
|
|
|
|
|
trackers, |
|
|
|
private DaemonTaskResult doControl(DelugeRpcClient client, DaemonTask task, String method) throws DaemonException { |
|
|
|
Collections.singletonList((String) response.get(RPC_TRACKER_STATUS)))); |
|
|
|
client.sendRequest(method, (Object) getTorrentIdsArg(task)); |
|
|
|
} |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
|
|
|
|
} |
|
|
|
private GetFileListTaskSuccessResult doGetFileList(DelugeRpcClient client, |
|
|
|
|
|
|
|
GetFileListTask task) throws DaemonException { |
|
|
|
private DaemonTaskResult doRemove(DelugeRpcClient client, RemoveTask task) throws DaemonException { |
|
|
|
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent()); |
|
|
|
client.sendRequest(RPC_METHOD_REMOVE, task.getTargetTorrent().getUniqueID(), task.includingData()); |
|
|
|
return new GetFileListTaskSuccessResult(task, files); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private DaemonTaskResult doControl(DelugeRpcClient client, DaemonTask task, |
|
|
|
@NonNull |
|
|
|
String method) throws DaemonException { |
|
|
|
private DaemonTaskResult doControlAll(DelugeRpcClient client, DaemonTask task, String method) throws DaemonException { |
|
|
|
client.sendRequest(method, (Object) getTorrentIdsArg(task)); |
|
|
|
client.sendRequest(method); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private DaemonTaskResult doRemove(DelugeRpcClient client, RemoveTask task) throws DaemonException { |
|
|
|
@NonNull |
|
|
|
client.sendRequest(RPC_METHOD_REMOVE, task.getTargetTorrent().getUniqueID(), |
|
|
|
private DaemonTaskResult doAddByFile(DelugeRpcClient client, AddByFileTask task) throws DaemonException { |
|
|
|
task.includingData()); |
|
|
|
final String file = task.getFile(); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
final String fileContent = Base64.encodeBytes(loadFile(file)); |
|
|
|
} |
|
|
|
client.sendRequest(RPC_METHOD_ADD_FILE, file, fileContent, new HashMap<>()); |
|
|
|
|
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
@NonNull |
|
|
|
} |
|
|
|
private DaemonTaskResult doControlAll(DelugeRpcClient client, DaemonTask task, |
|
|
|
|
|
|
|
String method) throws DaemonException { |
|
|
|
@NonNull |
|
|
|
client.sendRequest(method); |
|
|
|
private DaemonTaskResult doAddByUrl(DelugeRpcClient client, AddByUrlTask task) throws DaemonException { |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
client.sendRequest(RPC_METHOD_ADD, task.getUrl(), new HashMap<>()); |
|
|
|
} |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
|
|
|
|
} |
|
|
|
@NonNull |
|
|
|
|
|
|
|
private DaemonTaskResult doAddByFile(DelugeRpcClient client, AddByFileTask task) throws DaemonException { |
|
|
|
@NonNull |
|
|
|
final String file = task.getFile(); |
|
|
|
private DaemonTaskResult doAddByMagnetUrl(DelugeRpcClient client, AddByMagnetUrlTask task) throws DaemonException { |
|
|
|
final String fileContent = Base64.encodeBytes(loadFile(file)); |
|
|
|
client.sendRequest(RPC_METHOD_ADD_MAGNET, task.getUrl(), new HashMap<>()); |
|
|
|
client.sendRequest(RPC_METHOD_ADD_FILE, file, fileContent, new HashMap<>()); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
@NonNull |
|
|
|
private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) throws DaemonException { |
|
|
|
private DaemonTaskResult doAddByUrl(DelugeRpcClient client, AddByUrlTask task) throws DaemonException { |
|
|
|
if (!hasMethod(client, RPC_METHOD_SETLABEL)) { |
|
|
|
client.sendRequest(RPC_METHOD_ADD, task.getUrl(), new HashMap<>()); |
|
|
|
throw new DaemonException(ExceptionType.MethodUnsupported, "Label plugin not installed"); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
} |
|
|
|
} |
|
|
|
final String torrentId = task.getTargetTorrent().getUniqueID(); |
|
|
|
|
|
|
|
final String label = task.getNewLabel() == null ? "" : task.getNewLabel(); |
|
|
|
@NonNull |
|
|
|
client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label); |
|
|
|
private DaemonTaskResult doAddByMagnetUrl(DelugeRpcClient client, |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
AddByMagnetUrlTask task) throws DaemonException { |
|
|
|
} |
|
|
|
client.sendRequest(RPC_METHOD_ADD_MAGNET, task.getUrl(), new HashMap<>()); |
|
|
|
|
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
@NonNull |
|
|
|
} |
|
|
|
private DaemonTaskResult doSetFilePriorities(DelugeRpcClient client, SetFilePriorityTask task) throws DaemonException { |
|
|
|
|
|
|
|
// We first need a listing of all the files (because we can only set the priorities all at once)
|
|
|
|
@NonNull |
|
|
|
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent()); |
|
|
|
private DaemonTaskResult doSetLabel(DelugeRpcClient client, SetLabelTask task) throws DaemonException { |
|
|
|
|
|
|
|
if (!hasMethod(client, RPC_METHOD_SETLABEL)) { |
|
|
|
// prepare options arg
|
|
|
|
throw new DaemonException(ExceptionType.MethodUnsupported, "Label plugin not installed"); |
|
|
|
final Map<String, Object> optionsArgs = new HashMap<>(); |
|
|
|
} |
|
|
|
|
|
|
|
final String torrentId = task.getTargetTorrent().getUniqueID(); |
|
|
|
// Build a fast access set of file to change
|
|
|
|
final String label = task.getNewLabel() == null ? "" : task.getNewLabel(); |
|
|
|
final Set<String> changedFiles = new HashSet<>(); |
|
|
|
client.sendRequest(RPC_METHOD_SETLABEL, torrentId, label); |
|
|
|
for (TorrentFile file : task.getForFiles()) { |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
changedFiles.add(file.getKey()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
// Build array of converted priorities
|
|
|
|
private DaemonTaskResult doSetFilePriorities(DelugeRpcClient client, |
|
|
|
final ArrayList<Integer> priorities = new ArrayList<>(); |
|
|
|
SetFilePriorityTask task) throws DaemonException { |
|
|
|
final Priority newPriority = task.getNewPriority(); |
|
|
|
// We first need a listing of all the files (because we can only set the priorities all at once)
|
|
|
|
for (TorrentFile file : files) { |
|
|
|
final ArrayList<TorrentFile> files = getTorrentFiles(client, task.getTargetTorrent()); |
|
|
|
final Priority priority = changedFiles.contains(file.getKey()) ? newPriority : file.getPriority(); |
|
|
|
|
|
|
|
priorities.add(convertPriority(client, priority)); |
|
|
|
// prepare options arg
|
|
|
|
} |
|
|
|
final Map<String, Object> optionsArgs = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
optionsArgs.put(RPC_FILEPRIORITIES, priorities); |
|
|
|
// Build a fast access set of file to change
|
|
|
|
client.sendRequest(RPC_METHOD_SET_TORRENT_OPTIONS, getTorrentIdsArg(task), optionsArgs); |
|
|
|
final Set<String> changedFiles = new HashSet<>(); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
for (TorrentFile file : task.getForFiles()) { |
|
|
|
} |
|
|
|
changedFiles.add(file.getKey()); |
|
|
|
|
|
|
|
} |
|
|
|
@NonNull |
|
|
|
|
|
|
|
private DaemonTaskResult doSetTransferRates(DelugeRpcClient client, SetTransferRatesTask task) throws DaemonException { |
|
|
|
// Build array of converted priorities
|
|
|
|
final Map<String, Object> config = new HashMap<>(); |
|
|
|
final ArrayList<Integer> priorities = new ArrayList<>(); |
|
|
|
config.put(RPC_MAXDOWNLOAD, task.getDownloadRate() == null ? -1 : task.getDownloadRate()); |
|
|
|
final Priority newPriority = task.getNewPriority(); |
|
|
|
config.put(RPC_MAXUPLOAD, task.getUploadRate() == null ? -1 : task.getUploadRate()); |
|
|
|
for (TorrentFile file : files) { |
|
|
|
client.sendRequest(RPC_METHOD_SETCONFIG, config); |
|
|
|
final Priority priority = changedFiles.contains(file.getKey()) ? newPriority : file.getPriority(); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
priorities.add(convertPriority(client, priority)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
optionsArgs.put(RPC_FILEPRIORITIES, priorities); |
|
|
|
private DaemonTaskResult doSetTrackers(DelugeRpcClient client, SetTrackersTask task) throws DaemonException { |
|
|
|
client.sendRequest(RPC_METHOD_SET_TORRENT_OPTIONS, getTorrentIdsArg(task), optionsArgs); |
|
|
|
final List<Map<String, Object>> trackers = new ArrayList<>(); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
final ArrayList<String> newTrackers = task.getNewTrackers(); |
|
|
|
} |
|
|
|
for (int i = 0, n = newTrackers.size(); i < n; i++) { |
|
|
|
|
|
|
|
final Map<String, Object> tracker = new HashMap<>(); |
|
|
|
@NonNull |
|
|
|
tracker.put(RPC_TIER, i); |
|
|
|
private DaemonTaskResult doSetTransferRates(DelugeRpcClient client, |
|
|
|
tracker.put(RPC_URL, newTrackers.get(i)); |
|
|
|
SetTransferRatesTask task) throws DaemonException { |
|
|
|
trackers.add(tracker); |
|
|
|
final Map<String, Object> config = new HashMap<>(); |
|
|
|
} |
|
|
|
config.put(RPC_MAXDOWNLOAD, task.getDownloadRate() == null ? -1 : task.getDownloadRate()); |
|
|
|
client.sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers); |
|
|
|
config.put(RPC_MAXUPLOAD, task.getUploadRate() == null ? -1 : task.getUploadRate()); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
client.sendRequest(RPC_METHOD_SETCONFIG, config); |
|
|
|
} |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
|
|
|
|
} |
|
|
|
@NonNull |
|
|
|
|
|
|
|
private DaemonTaskResult doForceRecheck(DelugeRpcClient client, ForceRecheckTask task) throws DaemonException { |
|
|
|
@NonNull |
|
|
|
client.sendRequest(RPC_METHOD_FORCERECHECK, getTorrentIdsArg(task)); |
|
|
|
private DaemonTaskResult doSetTrackers(DelugeRpcClient client, SetTrackersTask task) throws DaemonException { |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
final List<Map<String, Object>> trackers = new ArrayList<>(); |
|
|
|
} |
|
|
|
final ArrayList<String> newTrackers = task.getNewTrackers(); |
|
|
|
|
|
|
|
for (int i = 0, n = newTrackers.size(); i < n; i++) { |
|
|
|
@NonNull |
|
|
|
final Map<String, Object> tracker = new HashMap<>(); |
|
|
|
private DaemonTaskResult doSetDownloadLocation(DelugeRpcClient client, SetDownloadLocationTask task) throws DaemonException { |
|
|
|
tracker.put(RPC_TIER, i); |
|
|
|
client.sendRequest(RPC_METHOD_MOVESTORAGE, getTorrentIdsArg(task), task.getNewLocation()); |
|
|
|
tracker.put(RPC_URL, newTrackers.get(i)); |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
trackers.add(tracker); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
client.sendRequest(RPC_METHOD_SETTRACKERS, task.getTargetTorrent().getUniqueID(), trackers); |
|
|
|
@NonNull |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
private List<Torrent> getTorrents(Collection<Map<String, Object>> torrentMaps) { |
|
|
|
} |
|
|
|
final List<Torrent> torrents = new ArrayList<>(); |
|
|
|
|
|
|
|
int id = 0; |
|
|
|
@NonNull |
|
|
|
for (Map<String, Object> torrentMap : torrentMaps) { |
|
|
|
private DaemonTaskResult doForceRecheck(DelugeRpcClient client, ForceRecheckTask task) throws DaemonException { |
|
|
|
final Object timeAdded = torrentMap.get(RPC_TIMEADDED); |
|
|
|
client.sendRequest(RPC_METHOD_FORCERECHECK, getTorrentIdsArg(task)); |
|
|
|
final Date timeAddedDate; |
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
if (timeAdded != null) { |
|
|
|
} |
|
|
|
final long seconds = (long) (float) timeAdded; |
|
|
|
|
|
|
|
timeAddedDate = new Date(seconds * 1000L); |
|
|
|
@NonNull |
|
|
|
} else { |
|
|
|
private DaemonTaskResult doSetDownloadLocation(DelugeRpcClient client, |
|
|
|
timeAddedDate = null; |
|
|
|
SetDownloadLocationTask task) throws DaemonException { |
|
|
|
} |
|
|
|
client.sendRequest(RPC_METHOD_MOVESTORAGE, getTorrentIdsArg(task), task.getNewLocation()); |
|
|
|
|
|
|
|
return new DaemonTaskSuccessResult(task); |
|
|
|
final String message = (String) torrentMap.get(RPC_MESSAGE); |
|
|
|
} |
|
|
|
final String trackerStatus = (String) torrentMap.get(RPC_TRACKER_STATUS); |
|
|
|
|
|
|
|
final String error; |
|
|
|
@NonNull |
|
|
|
if (trackerStatus.indexOf("Error") > 0) { |
|
|
|
private List<Torrent> getTorrents(Collection<Map<String, Object>> torrentMaps) throws DaemonException { |
|
|
|
error = message + (message.length() > 0 ? "\n" : "") + trackerStatus; |
|
|
|
final List<Torrent> torrents = new ArrayList<>(); |
|
|
|
} else { |
|
|
|
int id = 0; |
|
|
|
error = message; |
|
|
|
for (Map<String, Object> torrentMap : torrentMaps) { |
|
|
|
} |
|
|
|
final Object timeAdded = torrentMap.get(RPC_TIMEADDED); |
|
|
|
|
|
|
|
final Date timeAddedDate; |
|
|
|
torrents.add(new Torrent(id++, (String) torrentMap.get(RPC_HASH), (String) torrentMap.get(RPC_NAME), DelugeCommon.convertDelugeState( |
|
|
|
if (timeAdded != null) { |
|
|
|
(String) torrentMap.get(RPC_STATUS)), torrentMap.get(RPC_SAVEPATH) + settings.getOS().getPathSeperator(), ((Number) torrentMap |
|
|
|
final long seconds = (long) (float) timeAdded; |
|
|
|
.get(RPC_RATEDOWNLOAD)).intValue(), ((Number) torrentMap.get(RPC_RATEUPLOAD)).intValue(), ((Number) torrentMap.get |
|
|
|
timeAddedDate = new Date(seconds * 1000L); |
|
|
|
(RPC_NUMSEEDS)).intValue(), ((Number) torrentMap.get(RPC_TOTALSEEDS)).intValue(), ((Number) torrentMap.get(RPC_NUMPEERS)) |
|
|
|
} else { |
|
|
|
.intValue(), ((Number) torrentMap.get(RPC_TOTALPEERS)).intValue(), ((Number) torrentMap.get(RPC_ETA)).intValue(), ((Number) |
|
|
|
timeAddedDate = null; |
|
|
|
torrentMap.get(RPC_DOWNLOADEDEVER)).longValue(), ((Number) torrentMap.get(RPC_UPLOADEDEVER)).longValue(), ((Number) torrentMap |
|
|
|
} |
|
|
|
.get(RPC_TOTALSIZE)).longValue(), ((Number) torrentMap.get(RPC_PARTDONE)).floatValue() / 100f, 0f, // Not available
|
|
|
|
|
|
|
|
(String) torrentMap.get(RPC_LABEL), timeAddedDate, null, // Not available
|
|
|
|
final String message = (String) torrentMap.get(RPC_MESSAGE); |
|
|
|
error, getType())); |
|
|
|
final String trackerStatus = (String) torrentMap.get(RPC_TRACKER_STATUS); |
|
|
|
} |
|
|
|
final String error; |
|
|
|
return torrents; |
|
|
|
if (trackerStatus.indexOf("Error") > 0) { |
|
|
|
} |
|
|
|
error = message + (message.length() > 0 ? "\n" : "") + trackerStatus; |
|
|
|
|
|
|
|
} else { |
|
|
|
@NonNull |
|
|
|
error = message; |
|
|
|
private List<Label> getLabels(List<String> labelsResponse, List<Torrent> torrents) { |
|
|
|
} |
|
|
|
// First get all labels that torrents and count them
|
|
|
|
|
|
|
|
final Map<String, MutableInt> labelCounters = new HashMap<>(); |
|
|
|
torrents.add(new Torrent( |
|
|
|
for (Torrent torrent : torrents) { |
|
|
|
id++, |
|
|
|
final String label = torrent.getLabelName(); |
|
|
|
(String) torrentMap.get(RPC_HASH), |
|
|
|
if (label != null) { |
|
|
|
(String) torrentMap.get(RPC_NAME), |
|
|
|
final MutableInt count = labelCounters.get(label); |
|
|
|
DelugeCommon.convertDelugeState((String) torrentMap.get(RPC_STATUS)), |
|
|
|
if (count == null) { |
|
|
|
torrentMap.get(RPC_SAVEPATH) + settings.getOS().getPathSeperator(), |
|
|
|
labelCounters.put(label, new MutableInt(1)); |
|
|
|
((Number) torrentMap.get(RPC_RATEDOWNLOAD)).intValue(), |
|
|
|
} else { |
|
|
|
((Number) torrentMap.get(RPC_RATEUPLOAD)).intValue(), |
|
|
|
count.increment(); |
|
|
|
((Number) torrentMap.get(RPC_NUMSEEDS)).intValue(), |
|
|
|
} |
|
|
|
((Number) torrentMap.get(RPC_TOTALSEEDS)).intValue(), |
|
|
|
} |
|
|
|
((Number) torrentMap.get(RPC_NUMPEERS)).intValue(), |
|
|
|
} |
|
|
|
((Number) torrentMap.get(RPC_TOTALPEERS)).intValue(), |
|
|
|
final List<Label> labels = new ArrayList<>(); |
|
|
|
((Number) torrentMap.get(RPC_ETA)).intValue(), |
|
|
|
for (Entry<String, MutableInt> entry : labelCounters.entrySet()) { |
|
|
|
((Number) torrentMap.get(RPC_DOWNLOADEDEVER)).longValue(), |
|
|
|
labels.add(new Label(entry.getKey(), entry.getValue().get())); |
|
|
|
((Number) torrentMap.get(RPC_UPLOADEDEVER)).longValue(), |
|
|
|
} |
|
|
|
((Number) torrentMap.get(RPC_TOTALSIZE)).longValue(), |
|
|
|
|
|
|
|
((Number) torrentMap.get(RPC_PARTDONE)).floatValue() / 100f, |
|
|
|
// Now get all labels and add labels that have no torrents.
|
|
|
|
0f, // Not available
|
|
|
|
for (String label : labelsResponse) { |
|
|
|
(String) torrentMap.get(RPC_LABEL), |
|
|
|
if (!labelCounters.containsKey(label)) { |
|
|
|
timeAddedDate, |
|
|
|
labels.add(new Label(label, 0)); |
|
|
|
null, // Not available
|
|
|
|
} |
|
|
|
error, |
|
|
|
} |
|
|
|
getType())); |
|
|
|
return labels; |
|
|
|
} |
|
|
|
} |
|
|
|
return torrents; |
|
|
|
|
|
|
|
} |
|
|
|
@NonNull |
|
|
|
|
|
|
|
private ArrayList<TorrentFile> getTorrentFiles(DelugeRpcClient client, Torrent torrent) throws DaemonException { |
|
|
|
@NonNull |
|
|
|
final ArrayList<TorrentFile> files = new ArrayList<>(); |
|
|
|
private List<Label> getLabels(List<String> labelsResponse, List<Torrent> torrents) throws DaemonException { |
|
|
|
//noinspection unchecked
|
|
|
|
// First get all labels that torrents and count them
|
|
|
|
final Map<String, Object> response = (Map<String, Object>) client.sendRequest(RPC_METHOD_STATUS, torrent.getUniqueID(), |
|
|
|
final Map<String, MutableInt> labelCounters = new HashMap<>(); |
|
|
|
RPC_FILE_FIELDS_ARRAY); |
|
|
|
for (Torrent torrent : torrents) { |
|
|
|
|
|
|
|
final String label = torrent.getLabelName(); |
|
|
|
//noinspection unchecked
|
|
|
|
if (label != null) { |
|
|
|
final List<Map<String, Object>> fileMaps = (List<Map<String, Object>>) response.get(RPC_DETAILS); |
|
|
|
final MutableInt count = labelCounters.get(label); |
|
|
|
//noinspection unchecked
|
|
|
|
if (count == null) { |
|
|
|
final List<Integer> priorities = (List<Integer>) response.get(RPC_FILEPRIORITIES); |
|
|
|
labelCounters.put(label, new MutableInt(1)); |
|
|
|
//noinspection unchecked
|
|
|
|
} else { |
|
|
|
final List<Float> progresses = (List<Float>) response.get(RPC_FILEPROGRESS); |
|
|
|
count.increment(); |
|
|
|
|
|
|
|
} |
|
|
|
for (int i = 0, n = fileMaps.size(); i < n; i++) { |
|
|
|
} |
|
|
|
final Map<String, Object> fileMap = fileMaps.get(i); |
|
|
|
} |
|
|
|
final int priority = priorities.get(i); |
|
|
|
final List<Label> labels = new ArrayList<>(); |
|
|
|
final float progress = progresses.get(i); |
|
|
|
for (Entry<String, MutableInt> entry : labelCounters.entrySet()) { |
|
|
|
|
|
|
|
labels.add(new Label(entry.getKey(), entry.getValue().get())); |
|
|
|
final String path = (String) fileMap.get(RPC_PATH); |
|
|
|
} |
|
|
|
final long size = ((Number) fileMap.get(RPC_SIZE)).longValue(); |
|
|
|
|
|
|
|
files.add(new TorrentFile(fileMap.get(RPC_INDEX).toString(), path, path, torrent.getLocationDir() + path, size, (long) (size * progress) |
|
|
|
// Now get all labels and add labels that have no torrents.
|
|
|
|
, convertDelugePriority(client, priority))); |
|
|
|
for (String label : labelsResponse) { |
|
|
|
} |
|
|
|
if (!labelCounters.containsKey(label)) { |
|
|
|
return files; |
|
|
|
labels.add(new Label(label, 0)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
@NonNull |
|
|
|
return labels; |
|
|
|
private byte[] loadFile(String url) throws DaemonException { |
|
|
|
} |
|
|
|
final File file = new File(URI.create(url)); |
|
|
|
|
|
|
|
final BufferedInputStream in; |
|
|
|
@NonNull |
|
|
|
try { |
|
|
|
private ArrayList<TorrentFile> getTorrentFiles(DelugeRpcClient client, Torrent torrent) throws DaemonException { |
|
|
|
in = new BufferedInputStream(new FileInputStream(file)); |
|
|
|
final ArrayList<TorrentFile> files = new ArrayList<>(); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
//noinspection unchecked
|
|
|
|
throw new DaemonException(ExceptionType.FileAccessError, "File not found: " + file.getAbsolutePath()); |
|
|
|
final Map<String, Object> response = (Map<String, Object>) client.sendRequest( |
|
|
|
} |
|
|
|
RPC_METHOD_STATUS, |
|
|
|
final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
|
|
torrent.getUniqueID(), |
|
|
|
try { |
|
|
|
RPC_FILE_FIELDS_ARRAY); |
|
|
|
final byte[] buffer = new byte[1024]; |
|
|
|
|
|
|
|
while (true) { |
|
|
|
//noinspection unchecked
|
|
|
|
final int n = in.read(buffer); |
|
|
|
final List<Map<String, Object>> fileMaps = (List<Map<String, Object>>) response |
|
|
|
if (n < 0) { |
|
|
|
.get(RPC_DETAILS); |
|
|
|
break; |
|
|
|
//noinspection unchecked
|
|
|
|
} |
|
|
|
final List<Integer> priorities = (List<Integer>) response.get(RPC_FILEPRIORITIES); |
|
|
|
out.write(buffer, 0, n); |
|
|
|
//noinspection unchecked
|
|
|
|
} |
|
|
|
final List<Float> progresses = (List<Float>) response.get(RPC_FILEPROGRESS); |
|
|
|
return out.toByteArray(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
for (int i = 0, n = fileMaps.size(); i < n; i++) { |
|
|
|
throw new DaemonException(ExceptionType.FileAccessError, "Error reading file: " + file.getAbsolutePath()); |
|
|
|
final Map<String, Object> fileMap = fileMaps.get(i); |
|
|
|
} finally { |
|
|
|
final int priority = priorities.get(i); |
|
|
|
try { |
|
|
|
final float progress = progresses.get(i); |
|
|
|
in.close(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
final String path = (String) fileMap.get(RPC_PATH); |
|
|
|
// ignore
|
|
|
|
final long size = ((Number) fileMap.get(RPC_SIZE)).longValue(); |
|
|
|
} |
|
|
|
files.add(new TorrentFile( |
|
|
|
} |
|
|
|
fileMap.get(RPC_INDEX).toString(), |
|
|
|
} |
|
|
|
path, |
|
|
|
|
|
|
|
path, |
|
|
|
@NonNull |
|
|
|
torrent.getLocationDir() + path, |
|
|
|
private Priority convertDelugePriority(DelugeRpcClient client, int priority) throws DaemonException { |
|
|
|
size, |
|
|
|
ensureVersion(client); |
|
|
|
(long) (size * progress), |
|
|
|
return DelugeCommon.convertDelugePriority(priority, version); |
|
|
|
convertDelugePriority(client, priority))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return files; |
|
|
|
private int convertPriority(DelugeRpcClient client, Priority priority) throws DaemonException { |
|
|
|
} |
|
|
|
ensureVersion(client); |
|
|
|
|
|
|
|
return DelugeCommon.convertPriority(priority, version); |
|
|
|
@NonNull |
|
|
|
} |
|
|
|
private byte[] loadFile(String url) throws DaemonException { |
|
|
|
|
|
|
|
final File file = new File(URI.create(url)); |
|
|
|
private void ensureVersion(DelugeRpcClient client) throws DaemonException { |
|
|
|
final BufferedInputStream in; |
|
|
|
if (version > 0) { |
|
|
|
try { |
|
|
|
return; |
|
|
|
in = new BufferedInputStream(new FileInputStream(file)); |
|
|
|
} |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
version = DelugeCommon.getVersionString((String) client.sendRequest(RPC_METHOD_INFO)); |
|
|
|
throw new DaemonException(ExceptionType.FileAccessError, |
|
|
|
} |
|
|
|
"File not found: " + file.getAbsolutePath()); |
|
|
|
|
|
|
|
} |
|
|
|
// Return an Object so it doesn't confuse our varargs sendRequest methods.
|
|
|
|
final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
|
|
@NonNull |
|
|
|
try { |
|
|
|
private Object getTorrentIdsArg(DaemonTask task) { |
|
|
|
final byte[] buffer = new byte[1024]; |
|
|
|
return new String[]{task.getTargetTorrent().getUniqueID()}; |
|
|
|
while (true) { |
|
|
|
} |
|
|
|
final int n = in.read(buffer); |
|
|
|
|
|
|
|
if (n < 0) { |
|
|
|
private boolean hasMethod(DelugeRpcClient client, String method) throws DaemonException { |
|
|
|
break; |
|
|
|
//noinspection unchecked
|
|
|
|
} |
|
|
|
final List<String> methods = (List<String>) client.sendRequest(RPC_METHOD_GET_METHOD_LIST); |
|
|
|
out.write(buffer, 0, n); |
|
|
|
return methods.contains(method); |
|
|
|
} |
|
|
|
} |
|
|
|
return out.toByteArray(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
/** |
|
|
|
throw new DaemonException(ExceptionType.FileAccessError, |
|
|
|
* Used to count torrents in labels. |
|
|
|
"Error reading file: " + file.getAbsolutePath()); |
|
|
|
*/ |
|
|
|
} finally { |
|
|
|
private static class MutableInt { |
|
|
|
try { |
|
|
|
|
|
|
|
in.close(); |
|
|
|
int value = 1; |
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
// ignore
|
|
|
|
MutableInt(int value) { |
|
|
|
} |
|
|
|
this.value = value; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void increment() { |
|
|
|
@NonNull |
|
|
|
++value; |
|
|
|
private Priority convertDelugePriority(DelugeRpcClient client, int priority) |
|
|
|
} |
|
|
|
throws DaemonException { |
|
|
|
|
|
|
|
ensureVersion(client); |
|
|
|
int get() { |
|
|
|
return DelugeCommon.convertDelugePriority(priority, version); |
|
|
|
return value; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
private int convertPriority(DelugeRpcClient client, Priority priority) throws DaemonException { |
|
|
|
|
|
|
|
ensureVersion(client); |
|
|
|
|
|
|
|
return DelugeCommon.convertPriority(priority, version); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ensureVersion(DelugeRpcClient client) throws DaemonException { |
|
|
|
|
|
|
|
if (version > 0) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
version = DelugeCommon.getVersionString((String) client.sendRequest(RPC_METHOD_INFO)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return an Object so it doesn't confuse our varargs sendRequest methods.
|
|
|
|
|
|
|
|
@NonNull |
|
|
|
|
|
|
|
private Object getTorrentIdsArg(DaemonTask task) { |
|
|
|
|
|
|
|
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. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private static class MutableInt { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int value = 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MutableInt(int value) { |
|
|
|
|
|
|
|
this.value = value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void increment() { |
|
|
|
|
|
|
|
++value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int get() { |
|
|
|
|
|
|
|
return value; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|