Skip to content

Commit

Permalink
Another attempt to fix the localized qBittorrent issues as referenced…
Browse files Browse the repository at this point in the history
… in #115 and hopefully fixes #142. Who would think of localizing JSON?
  • Loading branch information
erickok committed Jun 17, 2014
1 parent 9eed4ac commit e4b5bc4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
Expand Up @@ -20,7 +20,7 @@
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.transdroid.core.R;
import org.transdroid.core.app.settings.ApplicationSettings_;
import org.transdroid.core.app.settings.*;

import android.annotation.TargetApi;
import android.app.AlertDialog;
Expand Down
Expand Up @@ -20,7 +20,7 @@
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.transdroid.core.R;
import org.transdroid.core.app.settings.ApplicationSettings_;
import org.transdroid.core.app.settings.*;
import org.transdroid.daemon.Daemon;

import android.annotation.TargetApi;
Expand Down
Expand Up @@ -20,7 +20,7 @@
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.transdroid.core.R;
import org.transdroid.core.app.settings.ApplicationSettings_;
import org.transdroid.core.app.settings.*;

import android.annotation.TargetApi;
import android.app.AlertDialog;
Expand Down
40 changes: 24 additions & 16 deletions lib/src/org/transdroid/daemon/Qbittorrent/QbittorrentAdapter.java
Expand Up @@ -399,9 +399,7 @@ private ArrayList<Torrent> parseJsonTorrents(JSONArray response) throws JSONExce
private double parseRatio(String string) {
// Ratio is given in "1.5" string format
try {
// FIXME Hack for issue #115: Strip the possible . and , separators in a hopefully reliable fashion, for now
string = string.replace(",", ".");
return Double.parseDouble(string);
return Double.parseDouble(normalizeNumber(string));
} catch (Exception e) {
return 0D;
}
Expand All @@ -412,20 +410,14 @@ private long parseSize(String string) {
if (string.equals("Unknown"))
return -1;
// Sizes are given in "1,023.3 MiB"-like string format
// FIXME Hack for issue #115: Strip the possible . and , separators in a hopefully reliable fashion, for now
String[] parts = string.split(" ");
String part1 = "";
if (parts[0].length() >= 3)
part1 = parts[0].substring(0, parts[0].length() - 3);
String part2 = parts[0].substring(parts[0].length() - 3);
parts[0] = part1.replace("Ê", "").replace(" ", "").replace(",", "").replace(".", "") + part2.replace(",", ".");
// Returns size in B-based long
double number;
try {
number = Double.parseDouble(parts[0]);
number = Double.parseDouble(normalizeNumber(parts[0]));
} catch (Exception e) {
return -1L;
}
// Returns size in B-based long
if (parts[1].equals("TiB")) {
return (long) (number * 1024L * 1024L * 1024L * 1024L);
} else if (parts[1].equals("GiB")) {
Expand Down Expand Up @@ -453,19 +445,35 @@ private int parseSpeed(String speed) {
// See https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-Documentation
if (speed.equals("Unknown"))
return -1;
// Speeds are in "21.9 KiB/s"-like string format
// Returns speed in B/s-based integer
// Sizes are given in "1,023.3 KiB/s"-like string format
String[] parts = speed.split(" ");
double number;
try {
number = Double.parseDouble(normalizeNumber(parts[0]));
} catch (Exception e) {
return -1;
}
// Returns size in B-based int
if (parts[1].equals("GiB/s")) {
return (int) (Double.parseDouble(parts[0]) * 1024 * 1024 * 1024);
return (int) (number * 1024 * 1024 * 1024);
} else if (parts[1].equals("MiB/s")) {
return (int) (Double.parseDouble(parts[0]) * 1024 * 1024);
return (int) (number * 1024 * 1024);
} else if (parts[1].equals("KiB/s")) {
return (int) (Double.parseDouble(parts[0]) * 1024);
return (int) (number * 1024);
}
return (int) (Double.parseDouble(parts[0]));
}

private String normalizeNumber(String in) {
// FIXME Hack for issue #115: Strip the possible . and , separators in a hopefully reliable fashion, for now
if (in.length() >= 3) {
String part1 = in.substring(0, in.length() - 3);
String part2 = in.substring(in.length() - 3);
return part1.replace("Ê", "").replace(" ", "").replace(",", "").replace(".", "") + part2.replace(",", ".");
}
return in.replace(",", ".");
}

private TorrentStatus parseStatus(String state) {
// Status is given as a descriptive string
if (state.equals("downloading")) {
Expand Down

0 comments on commit e4b5bc4

Please sign in to comment.