diff --git a/src/misc.cpp b/src/misc.cpp index 89208b71d..fb0e1ebe3 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -259,7 +259,9 @@ int misc::pythonVersion() { // use Binary prefix standards from IEC 60027-2 // see http://en.wikipedia.org/wiki/Kilobyte // 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) return QCoreApplication::translate("misc", "Unknown", "Unknown (size)"); int i = 0; @@ -269,7 +271,7 @@ QString misc::friendlyUnit(qreal val, bool is_speed) { if (i == 0) ret = QString::number((long)val) + " " + QCoreApplication::translate("misc", units[0].source, units[0].comment); 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) ret += QCoreApplication::translate("misc", "/s", "per second"); return ret; @@ -585,7 +587,9 @@ bool misc::naturalSort(QString left, QString right, bool &result) { // uses less } #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 ** 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 @@ -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. */ 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); } diff --git a/src/misc.h b/src/misc.h index 00dae66af..3e7eae607 100644 --- a/src/misc.h +++ b/src/misc.h @@ -92,7 +92,7 @@ namespace misc // use Binary prefix standards from IEC 60027-2 // see http://en.wikipedia.org/wiki/Kilobyte // 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); QString magnetUriToName(const QString& magnet_uri); QString magnetUriToHash(const QString& magnet_uri); @@ -109,7 +109,7 @@ namespace misc QList boolListfromStringList(const QStringList &l); 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 bool naturalSort(QString left, QString right, bool& result); diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 77096eb80..ad2be1dca 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -128,10 +128,10 @@ static QVariantMap toMap(const QTorrentHandle& h) QVariantMap ret; ret[KEY_TORRENT_HASH] = h.hash(); 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_DLSPEED] = misc::friendlyUnit(status.download_payload_rate, 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_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, true); // FIXME: Should be passed as a Number if (QBtSession::instance()->isQueueingEnabled() && h.queue_position(status) >= 0) ret[KEY_TORRENT_PRIORITY] = QString::number(h.queue_position(status)); else @@ -145,7 +145,7 @@ static QVariantMap toMap(const QTorrentHandle& h) leechs += " ("+QString::number(status.num_incomplete)+")"; ret[KEY_TORRENT_LEECHS] = leechs; 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 state; if (h.is_paused(status)) { @@ -295,20 +295,20 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash) save_path = fsutils::toNativePath(h.save_path()); data[KEY_PROP_SAVE_PATH] = save_path; 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_WASTED] = misc::friendlyUnit(status.total_failed_bytes + status.total_redundant_bytes); - data[KEY_PROP_UPLOADED] = QString(misc::friendlyUnit(status.all_time_upload) + " (" + misc::friendlyUnit(status.total_payload_upload) + " " + 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_UP_LIMIT] = h.upload_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.upload_limit(), true); - data[KEY_PROP_DL_LIMIT] = h.download_limit() <= 0 ? QString::fromUtf8("∞") : misc::friendlyUnit(h.download_limit(), true); + 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, false, true) + " (" + misc::friendlyUnit(status.total_payload_upload, false, true) + " " + 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, 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); if (h.is_seed(status)) 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_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); - 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) { qWarning() << Q_FUNC_INFO << "Invalid torrent: " << e.what(); return QByteArray(); @@ -346,7 +346,7 @@ QByteArray btjson::getFilesForTorrent(const QString& hash) fileName.chop(4); file_dict[KEY_FILE_NAME] = fsutils::toNativePath(fileName); 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_PRIORITY] = priorities[i]; if (i == 0) @@ -374,7 +374,7 @@ QByteArray btjson::getTransferInfo() { CACHED_VARIABLE(QVariantMap, info, CACHE_DURATION_MS); 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_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_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, true, true)).arg(misc::friendlyUnit(sessionStatus.total_payload_upload, false, true)); return json::toJson(info); }