diff --git a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java index 8bba9b5d..34cf3771 100644 --- a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java +++ b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeCommon.java @@ -1,3 +1,20 @@ +/* + * This file is part of Transdroid + * + * Transdroid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Transdroid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Transdroid. If not, see . + * + */ package org.transdroid.daemon.Deluge; import android.support.annotation.NonNull; @@ -32,6 +49,7 @@ class DelugeCommon { static final String RPC_METHOD_FORCERECHECK = "core.force_recheck"; static final String RPC_METHOD_GET = "web.update_ui"; static final String RPC_METHOD_GET_LABELS = "label.get_labels"; + static final String RPC_METHOD_GET_METHOD_LIST = "daemon.get_method_list"; static final String RPC_METHOD_GET_TORRENTS_STATUS = "core.get_torrents_status"; static final String RPC_METHOD_INFO = "daemon.info"; static final String RPC_METHOD_MOVESTORAGE = "core.move_storage"; diff --git a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java index 9c2c92ee..1119e909 100644 --- a/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java +++ b/app/src/main/java/org/transdroid/daemon/Deluge/DelugeRpcAdapter.java @@ -17,6 +17,59 @@ */ package org.transdroid.daemon.Deluge; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_DETAILS; +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_FIELDS_ARRAY; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FILEPRIORITIES; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FILEPROGRESS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FILE_FIELDS_ARRAY; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_HASH; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_INDEX; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_LABEL; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_MAXDOWNLOAD; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_MAXUPLOAD; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_MESSAGE; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_ADD; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_ADD_FILE; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_ADD_MAGNET; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_FORCERECHECK; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_GET_LABELS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_GET_METHOD_LIST; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_GET_TORRENTS_STATUS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_INFO; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_MOVESTORAGE; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_PAUSE; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_PAUSE_ALL; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_REMOVE; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_RESUME; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_RESUME_ALL; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SETCONFIG; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SETLABEL; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SETTRACKERS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SET_TORRENT_OPTIONS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_STATUS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_NAME; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_NUMPEERS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_NUMSEEDS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_PARTDONE; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_PATH; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_RATEDOWNLOAD; +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_SIZE; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS_FIELDS_ARRAY; +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_TOTALSEEDS; +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_TRACKER_STATUS; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_TIER; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_URL; +import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; + import android.support.annotation.NonNull; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; @@ -68,58 +121,6 @@ import org.transdroid.daemon.task.SetLabelTask; import org.transdroid.daemon.task.SetTrackersTask; import org.transdroid.daemon.task.SetTransferRatesTask; -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_DETAILS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FILEPRIORITIES; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FILEPROGRESS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_HASH; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_INDEX; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_LABEL; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_MAXDOWNLOAD; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_MAXUPLOAD; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_MESSAGE; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_ADD; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_ADD_FILE; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_ADD_MAGNET; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_FORCERECHECK; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_GET_LABELS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_GET_TORRENTS_STATUS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_INFO; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_MOVESTORAGE; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_PAUSE; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_PAUSE_ALL; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_REMOVE; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_RESUME; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_RESUME_ALL; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SETCONFIG; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SETLABEL; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SETTRACKERS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_SET_TORRENT_OPTIONS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_METHOD_STATUS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_NAME; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_NUMPEERS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_NUMSEEDS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_PARTDONE; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_PATH; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_RATEDOWNLOAD; -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_SIZE; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS; -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_TOTALSEEDS; -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_TRACKER_STATUS; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_TIER; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_TRACKER_URL; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_UPLOADEDEVER; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FIELDS_ARRAY; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_FILE_FIELDS_ARRAY; -import static org.transdroid.daemon.Deluge.DelugeCommon.RPC_STATUS_FIELDS_ARRAY; - /** * The daemon adapter from the Deluge torrent client using deluged API directly. * @@ -130,53 +131,53 @@ public class DelugeRpcAdapter implements IDaemonAdapter { public static final int DEFAULT_PORT = 58846; private final DaemonSettings settings; - private final DelugeRpcClient client; private int version = -1; public DelugeRpcAdapter(DaemonSettings settings) { this.settings = settings; - client = new DelugeRpcClient(settings); } @Override public DaemonTaskResult executeTask(Log log, DaemonTask task) { + final DelugeRpcClient client = new DelugeRpcClient(); try { + client.connect(settings.getAddress(), settings.getPort(), settings.getUsername(), settings.getPassword()); switch (task.getMethod()) { case Retrieve: - return doRetrieve((RetrieveTask) task); + return doRetrieve(client, (RetrieveTask) task); case AddByUrl: - return doAddByUrl((AddByUrlTask) task); + return doAddByUrl(client, (AddByUrlTask) task); case AddByMagnetUrl: - return doAddByMagnetUrl((AddByMagnetUrlTask) task); + return doAddByMagnetUrl(client, (AddByMagnetUrlTask) task); case AddByFile: - return doAddByFile((AddByFileTask) task); + return doAddByFile(client, (AddByFileTask) task); case Remove: - return doRemove((RemoveTask) task); + return doRemove(client, (RemoveTask) task); case Pause: - return doControl(task, RPC_METHOD_PAUSE); + return doControl(client, task, RPC_METHOD_PAUSE); case PauseAll: - return doControlAll(task, RPC_METHOD_PAUSE_ALL); + return doControlAll(client, task, RPC_METHOD_PAUSE_ALL); case Resume: - return doControl(task, RPC_METHOD_RESUME); + return doControl(client, task, RPC_METHOD_RESUME); case ResumeAll: - return doControlAll(task, RPC_METHOD_RESUME_ALL); + return doControlAll(client, task, RPC_METHOD_RESUME_ALL); case GetFileList: - return doGetFileList((GetFileListTask) task); + return doGetFileList(client, (GetFileListTask) task); case SetFilePriorities: - return doSetFilePriorities((SetFilePriorityTask) task); + return doSetFilePriorities(client, (SetFilePriorityTask) task); case SetTransferRates: - return doSetTransferRates((SetTransferRatesTask) task); + return doSetTransferRates(client, (SetTransferRatesTask) task); case SetLabel: - return doSetLabel((SetLabelTask) task); + return doSetLabel(client, (SetLabelTask) task); case SetDownloadLocation: - return doSetDownloadLocation((SetDownloadLocationTask) task); + return doSetDownloadLocation(client, (SetDownloadLocationTask) task); case GetTorrentDetails: - return doGetTorrentDetails((GetTorrentDetailsTask) task); + return doGetTorrentDetails(client, (GetTorrentDetailsTask) task); case SetTrackers: - return doSetTrackers((SetTrackersTask) task); + return doSetTrackers(client, (SetTrackersTask) task); case ForceRecheck: - return doForceRecheck((ForceRecheckTask) task); + return doForceRecheck(client, (ForceRecheckTask) task); default: return new DaemonTaskFailureResult(task, new DaemonException(ExceptionType.MethodUnsupported, @@ -184,6 +185,8 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } } catch (DaemonException e) { return new DaemonTaskFailureResult(task, e); + } finally { + client.close(); } } @@ -198,24 +201,32 @@ public class DelugeRpcAdapter implements IDaemonAdapter { } @NonNull - private RetrieveTaskSuccessResult doRetrieve(RetrieveTask task) throws DaemonException { - - final List results = client.sendRequests( - new Request(RPC_METHOD_INFO), - new Request(RPC_METHOD_GET_TORRENTS_STATUS, new HashMap<>(), RPC_FIELDS_ARRAY), - new Request(RPC_METHOD_GET_LABELS)); - version = DelugeCommon.getVersionString((String) results.get(0)); + private RetrieveTaskSuccessResult doRetrieve(DelugeRpcClient client, RetrieveTask task) throws DaemonException { + // Get torrents //noinspection unchecked - final Map> torrentsStatus = (Map>) results - .get(1); + final Map> torrentsStatus = (Map>) client + .sendRequest(RPC_METHOD_GET_TORRENTS_STATUS, new HashMap<>(), RPC_FIELDS_ARRAY); final List torrents = getTorrents(torrentsStatus.values()); + + // Check if Label plugin is enabled + //noinspection unchecked + final List methods = (List) client.sendRequest(RPC_METHOD_GET_METHOD_LIST); + final boolean hasLabelPlugin = methods.contains(RPC_METHOD_GET_LABELS); + + // Get label list from server //noinspection unchecked - final List