Browse Source

Add builder for Torrent and migrate QBittorrent

pull/600/head
Nathaniel Brandes 3 years ago
parent
commit
72c64700b1
  1. 200
      app/src/main/java/org/transdroid/daemon/Torrent.java
  2. 98
      app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java

200
app/src/main/java/org/transdroid/daemon/Torrent.java

@ -64,6 +64,7 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -64,6 +64,7 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
private boolean sequentialDownload;
private boolean firstLastPieceDownload;
private Torrent(Parcel in) {
this.id = in.readLong();
this.hash = in.readString();
@ -146,6 +147,54 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -146,6 +147,54 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
this.daemon = daemon;
}
Torrent(Builder builder) {
this.id = builder.id;
this.hash = builder.hash;
this.name = builder.name;
this.statusCode = builder.statusCode;
this.locationDir = builder.locationDir;
this.rateDownload = builder.rateDownload;
this.rateUpload = builder.rateUpload;
this.seedersConnected = builder.seedersConnected;
this.seedersKnown = builder.seedersKnown;
this.leechersConnected = builder.leechersConnected;
this.leechersKnown = builder.leechersKnown;
this.eta = builder.eta;
this.downloadedEver = builder.downloadedEver;
this.uploadedEver = builder.uploadedEver;
this.totalSize = builder.totalSize;
this.partDone = builder.partDone;
this.available = builder.available;
this.label = builder.label;
this.dateAdded = builder.dateAdded;
if (builder.realDateDone != null) {
this.dateDone = builder.realDateDone;
} else {
if (this.partDone == 1) {
// Finished but no finished date: set so move to bottom of list
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(1900, Calendar.DECEMBER, 31);
this.dateDone = cal.getTime();
} else if (eta == -1 || eta == -2) {
// UNknown eta: move to the top of the list
this.dateDone = new Date(Long.MAX_VALUE);
} else {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, eta);
this.dateDone = cal.getTime();
}
}
this.error = builder.error;
this.daemon = builder.daemon;
this.sequentialDownload = builder.sequentialDownload;
this.firstLastPieceDownload = builder.firstLastPieceDownload;
}
public String getName() {
return name;
}
@ -436,4 +485,155 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl @@ -436,4 +485,155 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
dest.writeString(daemon.name());
}
public static class Builder {
private long id;
private String hash;
private String name;
private TorrentStatus statusCode;
private String locationDir;
private int rateDownload;
private int rateUpload;
private int seedersConnected;
private int seedersKnown;
private int leechersConnected;
private int leechersKnown;
private int eta;
private long downloadedEver;
private long uploadedEver;
private long totalSize;
private float partDone;
private float available;
private String label;
private Date dateAdded;
private Date realDateDone;
private String error;
private Daemon daemon;
private boolean sequentialDownload;
private boolean firstLastPieceDownload;
public Builder setId(long id) {
this.id = id;
return this;
}
public Builder setHash(String hash) {
this.hash = hash;
return this;
}
public Builder setName(String name) {
this.name = name;
return this;
}
public Builder setStatusCode(TorrentStatus statusCode) {
this.statusCode = statusCode;
return this;
}
public Builder setLocationDir(String locationDir) {
this.locationDir = locationDir;
return this;
}
public Builder setRateDownload(int rateDownload) {
this.rateDownload = rateDownload;
return this;
}
public Builder setRateUpload(int rateUpload) {
this.rateUpload = rateUpload;
return this;
}
public Builder setSeedersConnected(int seedersConnected) {
this.seedersConnected = seedersConnected;
return this;
}
public Builder setSeedersKnown(int seedersKnown) {
this.seedersKnown = seedersKnown;
return this;
}
public Builder setLeechersConnected(int leechersConnected) {
this.leechersConnected = leechersConnected;
return this;
}
public Builder setLeechersKnown(int leechersKnown) {
this.leechersKnown = leechersKnown;
return this;
}
public Builder setEta(int eta) {
this.eta = eta;
return this;
}
public Builder setDownloadedEver(long downloadedEver) {
this.downloadedEver = downloadedEver;
return this;
}
public Builder setUploadedEver(long uploadedEver) {
this.uploadedEver = uploadedEver;
return this;
}
public Builder setTotalSize(long totalSize) {
this.totalSize = totalSize;
return this;
}
public Builder setPartDone(float partDone) {
this.partDone = partDone;
return this;
}
public Builder setAvailable(float available) {
this.available = available;
return this;
}
public Builder setLabel(String label) {
this.label = label;
return this;
}
public Builder setDateAdded(Date dateAdded) {
this.dateAdded = dateAdded;
return this;
}
public Builder setRealDateDone(Date realDateDone) {
this.realDateDone = realDateDone;
return this;
}
public Builder setError(String error) {
this.error = error;
return this;
}
public Builder setDaemon(Daemon daemon) {
this.daemon = daemon;
return this;
}
public Builder setSequentialDownload(boolean sequentialDownload) {
this.sequentialDownload = sequentialDownload;
return this;
}
public Builder setFirstLastPieceDownload(boolean firstLastPieceDownload) {
this.firstLastPieceDownload = firstLastPieceDownload;
return this;
}
public Torrent createTorrent() {
return new Torrent(this);
}
}
}

98
app/src/main/java/org/transdroid/daemon/adapters/qBittorrent/QBittorrentAdapter.java

@ -658,35 +658,36 @@ public class QBittorrentAdapter implements IDaemonAdapter { @@ -658,35 +658,36 @@ public class QBittorrentAdapter implements IDaemonAdapter {
for (int i = 0; i < response.length(); i++) {
JSONObject tor = response.getJSONObject(i);
double progress = tor.getDouble("progress");
int[] leechers;
int[] seeders;
double ratio;
long size;
long uploaded;
int dlspeed;
int upspeed;
boolean dlseq = false;
boolean dlflp = false;
Date addedOn = null;
Date completionOn = null;
String label = null;
Torrent.Builder torrentBuilder = new Torrent.Builder()
.setId(i)
.setHash(tor.getString("hash"))
.setName(tor.getString("name"))
.setStatusCode(parseStatus(tor.getString("state")))
.setDaemon(settings.getType())
.setPartDone((float) progress);
if (version >= 30200) {
leechers = new int[2];
leechers[0] = tor.getInt("num_leechs");
leechers[1] = tor.getInt("num_complete") + tor.getInt("num_incomplete");
seeders = new int[2];
seeders[0] = tor.getInt("num_seeds");
seeders[1] = tor.getInt("num_complete");
torrentBuilder
.setLeechersConnected(tor.getInt("num_leechs"))
.setLeechersKnown(tor.getInt("num_complete") + tor.getInt("num_incomplete"))
.setSeedersConnected(tor.getInt("num_seeds"))
.setSeedersKnown(tor.getInt("num_complete"));
size = tor.getLong("size");
ratio = tor.getDouble("ratio");
dlspeed = tor.getInt("dlspeed");
upspeed = tor.getInt("upspeed");
if (tor.has("seq_dl")) {
dlseq = tor.getBoolean("seq_dl");
torrentBuilder.setSequentialDownload(tor.getBoolean("seq_dl"));
}
if (tor.has("f_l_piece_prio")) {
dlflp = tor.getBoolean("f_l_piece_prio");
torrentBuilder.setFirstLastPieceDownload(tor.getBoolean("f_l_piece_prio"));
}
if (tor.has("uploaded")) {
uploaded = tor.getLong("uploaded");
@ -694,16 +695,27 @@ public class QBittorrentAdapter implements IDaemonAdapter { @@ -694,16 +695,27 @@ public class QBittorrentAdapter implements IDaemonAdapter {
uploaded = (long) (size * ratio);
}
final long addedOnTime = tor.optLong("added_on");
addedOn = (addedOnTime > 0) ? new Date(addedOnTime * 1000L) : null;
if(addedOnTime > 0) {
torrentBuilder.setDateAdded(new Date(addedOnTime * 1000L));
}
final long completionOnTime = tor.optLong("completion_on");
completionOn = (completionOnTime > 0) ? new Date(completionOnTime * 1000L) : null;
label = tor.optString("category");
if (label.length() == 0) {
label = null;
if(completionOnTime > 0) {
torrentBuilder.setRealDateDone(new Date(completionOnTime * 1000L));
}
String label = tor.optString("category");
if (!label.isEmpty()) {
torrentBuilder.setLabel(label);
}
} else {
leechers = parsePeers(tor.getString("num_leechs"));
seeders = parsePeers(tor.getString("num_seeds"));
int[] leechers = parsePeers(tor.getString("num_leechs"));
int[] seeders = parsePeers(tor.getString("num_seeds"));
torrentBuilder
.setSeedersConnected(seeders[0])
.setSeedersKnown(seeders[1])
.setLeechersConnected(leechers[0])
.setLeechersKnown(leechers[1]);
size = parseSize(tor.getString("size"));
ratio = parseRatio(tor.getString("ratio"));
uploaded = (long) (size * ratio);
@ -712,42 +724,26 @@ public class QBittorrentAdapter implements IDaemonAdapter { @@ -712,42 +724,26 @@ public class QBittorrentAdapter implements IDaemonAdapter {
}
long eta = -1L;
if (dlspeed > 0)
if (dlspeed > 0) {
eta = (long) (size - (size * progress)) / dlspeed;
}
Torrent torrent = torrentBuilder
.setRateDownload(dlspeed)
.setRateUpload(upspeed)
.setEta((int) eta)
.setDownloadedEver((long) (size * progress))
.setUploadedEver(uploaded)
.setTotalSize(size)
.setAvailable(0f)
.createTorrent();
// Add the parsed torrent to the list
// @formatter:off
Torrent torrent = new Torrent(
(long) i,
tor.getString("hash"),
tor.getString("name"),
parseStatus(tor.getString("state")),
null,
dlspeed,
upspeed,
seeders[0],
seeders[1],
leechers[0],
leechers[1],
(int) eta,
(long) (size * progress),
uploaded,
size,
(float) progress,
0f,
label,
addedOn,
completionOn,
null,
settings.getType());
torrent.mimicSequentialDownload(dlseq);
torrent.mimicFirstLastPieceDownload(dlflp);
torrents.add(torrent);
// @formatter:on
}
// Return the list
return torrents;
}
private double parseRatio(String string) {

Loading…
Cancel
Save