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
private boolean sequentialDownload; private boolean sequentialDownload;
private boolean firstLastPieceDownload; private boolean firstLastPieceDownload;
private Torrent(Parcel in) { private Torrent(Parcel in) {
this.id = in.readLong(); this.id = in.readLong();
this.hash = in.readString(); this.hash = in.readString();
@ -146,6 +147,54 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
this.daemon = daemon; 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() { public String getName() {
return name; return name;
} }
@ -436,4 +485,155 @@ public final class Torrent implements Parcelable, Comparable<Torrent>, Finishabl
dest.writeString(daemon.name()); 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 {
for (int i = 0; i < response.length(); i++) { for (int i = 0; i < response.length(); i++) {
JSONObject tor = response.getJSONObject(i); JSONObject tor = response.getJSONObject(i);
double progress = tor.getDouble("progress"); double progress = tor.getDouble("progress");
int[] leechers;
int[] seeders;
double ratio; double ratio;
long size; long size;
long uploaded; long uploaded;
int dlspeed; int dlspeed;
int upspeed; int upspeed;
boolean dlseq = false;
boolean dlflp = false; Torrent.Builder torrentBuilder = new Torrent.Builder()
Date addedOn = null; .setId(i)
Date completionOn = null; .setHash(tor.getString("hash"))
String label = null; .setName(tor.getString("name"))
.setStatusCode(parseStatus(tor.getString("state")))
.setDaemon(settings.getType())
.setPartDone((float) progress);
if (version >= 30200) { if (version >= 30200) {
leechers = new int[2]; torrentBuilder
leechers[0] = tor.getInt("num_leechs"); .setLeechersConnected(tor.getInt("num_leechs"))
leechers[1] = tor.getInt("num_complete") + tor.getInt("num_incomplete"); .setLeechersKnown(tor.getInt("num_complete") + tor.getInt("num_incomplete"))
seeders = new int[2]; .setSeedersConnected(tor.getInt("num_seeds"))
seeders[0] = tor.getInt("num_seeds"); .setSeedersKnown(tor.getInt("num_complete"));
seeders[1] = tor.getInt("num_complete");
size = tor.getLong("size"); size = tor.getLong("size");
ratio = tor.getDouble("ratio"); ratio = tor.getDouble("ratio");
dlspeed = tor.getInt("dlspeed"); dlspeed = tor.getInt("dlspeed");
upspeed = tor.getInt("upspeed"); upspeed = tor.getInt("upspeed");
if (tor.has("seq_dl")) { if (tor.has("seq_dl")) {
dlseq = tor.getBoolean("seq_dl"); torrentBuilder.setSequentialDownload(tor.getBoolean("seq_dl"));
} }
if (tor.has("f_l_piece_prio")) { 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")) { if (tor.has("uploaded")) {
uploaded = tor.getLong("uploaded"); uploaded = tor.getLong("uploaded");
@ -694,16 +695,27 @@ public class QBittorrentAdapter implements IDaemonAdapter {
uploaded = (long) (size * ratio); uploaded = (long) (size * ratio);
} }
final long addedOnTime = tor.optLong("added_on"); 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"); final long completionOnTime = tor.optLong("completion_on");
completionOn = (completionOnTime > 0) ? new Date(completionOnTime * 1000L) : null; if(completionOnTime > 0) {
label = tor.optString("category"); torrentBuilder.setRealDateDone(new Date(completionOnTime * 1000L));
if (label.length() == 0) { }
label = null;
String label = tor.optString("category");
if (!label.isEmpty()) {
torrentBuilder.setLabel(label);
} }
} else { } else {
leechers = parsePeers(tor.getString("num_leechs")); int[] leechers = parsePeers(tor.getString("num_leechs"));
seeders = parsePeers(tor.getString("num_seeds")); 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")); size = parseSize(tor.getString("size"));
ratio = parseRatio(tor.getString("ratio")); ratio = parseRatio(tor.getString("ratio"));
uploaded = (long) (size * ratio); uploaded = (long) (size * ratio);
@ -712,42 +724,26 @@ public class QBittorrentAdapter implements IDaemonAdapter {
} }
long eta = -1L; long eta = -1L;
if (dlspeed > 0) if (dlspeed > 0) {
eta = (long) (size - (size * progress)) / dlspeed; 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 // 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); torrents.add(torrent);
// @formatter:on
} }
// Return the list // Return the list
return torrents; return torrents;
} }
private double parseRatio(String string) { private double parseRatio(String string) {

Loading…
Cancel
Save