Browse Source

Don't localize double numbers in the webui. Closes #1525.

adaptive-webui-19844
sledgehammer999 10 years ago
parent
commit
d44df4f985
  1. 15
      src/misc.cpp
  2. 4
      src/misc.h
  3. 28
      src/webui/btjson.cpp

15
src/misc.cpp

@ -259,7 +259,9 @@ int misc::pythonVersion() {
// use Binary prefix standards from IEC 60027-2 // use Binary prefix standards from IEC 60027-2
// see http://en.wikipedia.org/wiki/Kilobyte // see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes // value must be given in bytes
QString misc::friendlyUnit(qreal val, bool is_speed) { // FIXME: Remove the 'webui' variable, when the webui API is reworked
// to send numbers instead of strings with suffixes
QString misc::friendlyUnit(qreal val, bool is_speed, bool webui) {
if (val < 0) if (val < 0)
return QCoreApplication::translate("misc", "Unknown", "Unknown (size)"); return QCoreApplication::translate("misc", "Unknown", "Unknown (size)");
int i = 0; int i = 0;
@ -269,7 +271,7 @@ QString misc::friendlyUnit(qreal val, bool is_speed) {
if (i == 0) if (i == 0)
ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment); ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment);
else else
ret = accurateDoubleToString(val, 1) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment); ret = accurateDoubleToString(val, 1, !webui) + " " + QCoreApplication::translate("misc", units[i].source, units[i].comment);
if (is_speed) if (is_speed)
ret += QCoreApplication::translate("misc", "/s", "per second"); ret += QCoreApplication::translate("misc", "/s", "per second");
return ret; return ret;
@ -585,7 +587,9 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less
} }
#endif #endif
QString misc::accurateDoubleToString(const double &n, const int &precision) { // FIXME: Remove the 'localized' variable, when the webui API is reworked
// to send numbers instead of strings with suffixes
QString misc::accurateDoubleToString(const double &n, const int &precision, bool localized) {
/* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9 /* HACK because QString rounds up. Eg QString::number(0.999*100.0, 'f' ,1) == 99.9
** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when ** but QString::number(0.9999*100.0, 'f' ,1) == 100.0 The problem manifests when
** the number has more digits after the decimal than we want AND the digit after ** the number has more digits after the decimal than we want AND the digit after
@ -593,5 +597,8 @@ QString misc::accurateDoubleToString(const double &n, const int &precision) {
** precision we add an extra 0 behind 1 in the below algorithm. */ ** precision we add an extra 0 behind 1 in the below algorithm. */
double prec = std::pow(10.0, precision); double prec = std::pow(10.0, precision);
return QLocale::system().toString(std::floor(n*prec)/prec, 'f', precision); if (localized)
return QLocale::system().toString(std::floor(n*prec)/prec, 'f', precision);
else
return QString::number(std::floor(n*prec)/prec, 'f', precision);
} }

4
src/misc.h

@ -92,7 +92,7 @@ namespace misc
// use Binary prefix standards from IEC 60027-2 // use Binary prefix standards from IEC 60027-2
// see http://en.wikipedia.org/wiki/Kilobyte // see http://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes // value must be given in bytes
QString friendlyUnit(qreal val, bool is_speed = false); QString friendlyUnit(qreal val, bool is_speed = false, bool webui=false);
bool isPreviewable(const QString& extension); bool isPreviewable(const QString& extension);
QString magnetUriToName(const QString& magnet_uri); QString magnetUriToName(const QString& magnet_uri);
QString magnetUriToHash(const QString& magnet_uri); QString magnetUriToHash(const QString& magnet_uri);
@ -109,7 +109,7 @@ namespace misc
QList<bool> boolListfromStringList(const QStringList &l); QList<bool> boolListfromStringList(const QStringList &l);
QString toQString(time_t t); QString toQString(time_t t);
QString accurateDoubleToString(const double &n, const int &precision); QString accurateDoubleToString(const double &n, const int &precision, bool localized=true);
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
bool naturalSort(QString left, QString right, bool& result); bool naturalSort(QString left, QString right, bool& result);

28
src/webui/btjson.cpp

@ -128,10 +128,10 @@ static QVariantMap toMap(const QTorrentHandle& h)
QVariantMap ret; QVariantMap ret;
ret[KEY_TORRENT_HASH] = h.hash(); ret[KEY_TORRENT_HASH] = h.hash();
ret[KEY_TORRENT_NAME] = h.name(); ret[KEY_TORRENT_NAME] = h.name();
ret[KEY_TORRENT_SIZE] = misc::friendlyUnit(status.total_wanted); // FIXME: Should pass as Number, not formatted String (for sorting). ret[KEY_TORRENT_SIZE] = misc::friendlyUnit(status.total_wanted, false, true); // FIXME: Should pass as Number, not formatted String (for sorting).
ret[KEY_TORRENT_PROGRESS] = (double)h.progress(status); ret[KEY_TORRENT_PROGRESS] = (double)h.progress(status);
ret[KEY_TORRENT_DLSPEED] = misc::friendlyUnit(status.download_payload_rate, true); // FIXME: Should be passed as a Number ret[KEY_TORRENT_DLSPEED] = misc::friendlyUnit(status.download_payload_rate, true, true); // FIXME: Should be passed as a Number
ret[KEY_TORRENT_UPSPEED] = misc::friendlyUnit(status.upload_payload_rate, true); // FIXME: Should be passed as a Number ret[KEY_TORRENT_UPSPEED] = misc::friendlyUnit(status.upload_payload_rate, true, true); // FIXME: Should be passed as a Number
if (QBtSession::instance()->isQueueingEnabled() && h.queue_position(status) >= 0) if (QBtSession::instance()->isQueueingEnabled() && h.queue_position(status) >= 0)
ret[KEY_TORRENT_PRIORITY] = QString::number(h.queue_position(status)); ret[KEY_TORRENT_PRIORITY] = QString::number(h.queue_position(status));
else else
@ -145,7 +145,7 @@ static QVariantMap toMap(const QTorrentHandle& h)
leechs += " ("+QString::number(status.num_incomplete)+")"; leechs += " ("+QString::number(status.num_incomplete)+")";
ret[KEY_TORRENT_LEECHS] = leechs; ret[KEY_TORRENT_LEECHS] = leechs;
const qreal ratio = QBtSession::instance()->getRealRatio(status); const qreal ratio = QBtSession::instance()->getRealRatio(status);
ret[KEY_TORRENT_RATIO] = (ratio > 100.) ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio, 1); ret[KEY_TORRENT_RATIO] = (ratio > 100.) ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio, 1, false);
QString eta; QString eta;
QString state; QString state;
if (h.is_paused(status)) { if (h.is_paused(status)) {
@ -295,20 +295,20 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash)
save_path = fsutils::toNativePath(h.save_path()); save_path = fsutils::toNativePath(h.save_path());
data[KEY_PROP_SAVE_PATH] = save_path; data[KEY_PROP_SAVE_PATH] = save_path;
data[KEY_PROP_CREATION_DATE] = h.creation_date(); data[KEY_PROP_CREATION_DATE] = h.creation_date();
data[KEY_PROP_PIECE_SIZE] = misc::friendlyUnit(h.piece_length()); data[KEY_PROP_PIECE_SIZE] = misc::friendlyUnit(h.piece_length(), false, true);
data[KEY_PROP_COMMENT] = h.comment(); data[KEY_PROP_COMMENT] = h.comment();
data[KEY_PROP_WASTED] = misc::friendlyUnit(status.total_failed_bytes + status.total_redundant_bytes); data[KEY_PROP_WASTED] = misc::friendlyUnit(status.total_failed_bytes + status.total_redundant_bytes, false, true);
data[KEY_PROP_UPLOADED] = QString(misc::friendlyUnit(status.all_time_upload) + " (" + misc::friendlyUnit(status.total_payload_upload) + " " + tr("this session") + ")"); data[KEY_PROP_UPLOADED] = QString(misc::friendlyUnit(status.all_time_upload, false, true) + " (" + misc::friendlyUnit(status.total_payload_upload, false, true) + " " + tr("this session") + ")");
data[KEY_PROP_DOWNLOADED] = QString(misc::friendlyUnit(status.all_time_download) + " (" + misc::friendlyUnit(status.total_payload_download) + " " + tr("this session") + ")"); data[KEY_PROP_DOWNLOADED] = QString(misc::friendlyUnit(status.all_time_download, false, true) + " (" + misc::friendlyUnit(status.total_payload_download, false, true) + " " + tr("this session") + ")");
data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? QString::fromUtf8("") : misc::friendlyUnit(h.upload_limit(), true); data[KEY_PROP_UP_LIMIT] = h.upload_limit() <= 0 ? QString::fromUtf8("") : misc::friendlyUnit(h.upload_limit(), true, true);
data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? QString::fromUtf8("") : misc::friendlyUnit(h.download_limit(), true); data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? QString::fromUtf8("") : misc::friendlyUnit(h.download_limit(), true, true);
QString elapsed_txt = misc::userFriendlyDuration(status.active_time); QString elapsed_txt = misc::userFriendlyDuration(status.active_time);
if (h.is_seed(status)) if (h.is_seed(status))
elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(status.seeding_time))+")"; elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(status.seeding_time))+")";
data[KEY_PROP_TIME_ELAPSED] = elapsed_txt; data[KEY_PROP_TIME_ELAPSED] = elapsed_txt;
data[KEY_PROP_CONNECT_COUNT] = QString(QString::number(status.num_connections) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(status.connections_limit)) + ")"); data[KEY_PROP_CONNECT_COUNT] = QString(QString::number(status.num_connections) + " (" + tr("%1 max", "e.g. 10 max").arg(QString::number(status.connections_limit)) + ")");
const qreal ratio = QBtSession::instance()->getRealRatio(status); const qreal ratio = QBtSession::instance()->getRealRatio(status);
data[KEY_PROP_RATIO] = ratio > 100. ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio, 1); data[KEY_PROP_RATIO] = ratio > 100. ? QString::fromUtf8("") : misc::accurateDoubleToString(ratio, 1, false);
} catch(const std::exception& e) { } catch(const std::exception& e) {
qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what(); qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what();
return QByteArray(); return QByteArray();
@ -346,7 +346,7 @@ QByteArray btjson::getFilesForTorrent(const QString& hash)
fileName.chop(4); fileName.chop(4);
file_dict[KEY_FILE_NAME] = fsutils::toNativePath(fileName); file_dict[KEY_FILE_NAME] = fsutils::toNativePath(fileName);
const size_type size = h.filesize_at(i); const size_type size = h.filesize_at(i);
file_dict[KEY_FILE_SIZE] = misc::friendlyUnit(size); file_dict[KEY_FILE_SIZE] = misc::friendlyUnit(size, false, true);
file_dict[KEY_FILE_PROGRESS] = (size > 0) ? (fp[i] / (double) size) : 1.; file_dict[KEY_FILE_PROGRESS] = (size > 0) ? (fp[i] / (double) size) : 1.;
file_dict[KEY_FILE_PRIORITY] = priorities[i]; file_dict[KEY_FILE_PRIORITY] = priorities[i];
if (i == 0) if (i == 0)
@ -374,7 +374,7 @@ QByteArray btjson::getTransferInfo()
{ {
CACHED_VARIABLE(QVariantMap, info, CACHE_DURATION_MS); CACHED_VARIABLE(QVariantMap, info, CACHE_DURATION_MS);
session_status sessionStatus = QBtSession::instance()->getSessionStatus(); session_status sessionStatus = QBtSession::instance()->getSessionStatus();
info[KEY_TRANSFER_DLSPEED] = tr("D: %1/s - T: %2", "Download speed: x KiB/s - Transferred: x MiB").arg(misc::friendlyUnit(sessionStatus.payload_download_rate)).arg(misc::friendlyUnit(sessionStatus.total_payload_download)); info[KEY_TRANSFER_DLSPEED] = tr("D: %1/s - T: %2", "Download speed: x KiB/s - Transferred: x MiB").arg(misc::friendlyUnit(sessionStatus.payload_download_rate, true, true)).arg(misc::friendlyUnit(sessionStatus.total_payload_download, false, true));
info[KEY_TRANSFER_UPSPEED] = tr("U: %1/s - T: %2", "Upload speed: x KiB/s - Transferred: x MiB").arg(misc::friendlyUnit(sessionStatus.payload_upload_rate)).arg(misc::friendlyUnit(sessionStatus.total_payload_upload)); info[KEY_TRANSFER_UPSPEED] = tr("U: %1/s - T: %2", "Upload speed: x KiB/s - Transferred: x MiB").arg(misc::friendlyUnit(sessionStatus.payload_upload_rate, true, true)).arg(misc::friendlyUnit(sessionStatus.total_payload_upload, false, true));
return json::toJson(info); return json::toJson(info);
} }

Loading…
Cancel
Save