Clean up code

* Use compiler generated comparison function
* Use designated initializers
* Convert to proper type
* Use reference
* Remove redundant text
  The `msg` already contain the text `Reason:` so it isn't needed.

PR #20312.
This commit is contained in:
Chocobo1 2024-01-25 02:56:12 +08:00 committed by GitHub
parent bab9c15913
commit 94e80d01a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 42 additions and 58 deletions

View file

@ -34,7 +34,6 @@
#include <algorithm>
#include <csignal>
#include <tuple>
#ifdef Q_OS_UNIX
#include <unistd.h>

View file

@ -28,8 +28,6 @@
#include "addtorrentparams.h"
#include <tuple>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
@ -101,51 +99,38 @@ namespace
}
}
bool BitTorrent::operator==(const AddTorrentParams &lhs, const AddTorrentParams &rhs)
{
return std::tie(lhs.name, lhs.category, lhs.tags,
lhs.savePath, lhs.useDownloadPath, lhs.downloadPath,
lhs.sequential, lhs.firstLastPiecePriority, lhs.addForced,
lhs.addToQueueTop, lhs.addPaused, lhs.stopCondition,
lhs.filePaths, lhs.filePriorities, lhs.skipChecking,
lhs.contentLayout, lhs.useAutoTMM, lhs.uploadLimit,
lhs.downloadLimit, lhs.seedingTimeLimit, lhs.inactiveSeedingTimeLimit, lhs.ratioLimit)
== std::tie(rhs.name, rhs.category, rhs.tags,
rhs.savePath, rhs.useDownloadPath, rhs.downloadPath,
rhs.sequential, rhs.firstLastPiecePriority, rhs.addForced,
rhs.addToQueueTop, rhs.addPaused, rhs.stopCondition,
rhs.filePaths, rhs.filePriorities, rhs.skipChecking,
rhs.contentLayout, rhs.useAutoTMM, rhs.uploadLimit,
rhs.downloadLimit, rhs.seedingTimeLimit, rhs.inactiveSeedingTimeLimit, rhs.ratioLimit);
}
BitTorrent::AddTorrentParams BitTorrent::parseAddTorrentParams(const QJsonObject &jsonObj)
{
AddTorrentParams params;
params.category = jsonObj.value(PARAM_CATEGORY).toString();
params.tags = parseTagSet(jsonObj.value(PARAM_TAGS).toArray());
params.savePath = Path(jsonObj.value(PARAM_SAVEPATH).toString());
params.useDownloadPath = getOptionalBool(jsonObj, PARAM_USEDOWNLOADPATH);
params.downloadPath = Path(jsonObj.value(PARAM_DOWNLOADPATH).toString());
params.addForced = (getEnum<TorrentOperatingMode>(jsonObj, PARAM_OPERATINGMODE) == TorrentOperatingMode::Forced);
params.addToQueueTop = getOptionalBool(jsonObj, PARAM_QUEUETOP);
params.addPaused = getOptionalBool(jsonObj, PARAM_STOPPED);
params.stopCondition = getOptionalEnum<Torrent::StopCondition>(jsonObj, PARAM_STOPCONDITION);
params.skipChecking = jsonObj.value(PARAM_SKIPCHECKING).toBool();
params.contentLayout = getOptionalEnum<TorrentContentLayout>(jsonObj, PARAM_CONTENTLAYOUT);
params.useAutoTMM = getOptionalBool(jsonObj, PARAM_AUTOTMM);
params.uploadLimit = jsonObj.value(PARAM_UPLOADLIMIT).toInt(-1);
params.downloadLimit = jsonObj.value(PARAM_DOWNLOADLIMIT).toInt(-1);
params.seedingTimeLimit = jsonObj.value(PARAM_SEEDINGTIMELIMIT).toInt(Torrent::USE_GLOBAL_SEEDING_TIME);
params.inactiveSeedingTimeLimit = jsonObj.value(PARAM_INACTIVESEEDINGTIMELIMIT).toInt(Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME);
params.ratioLimit = jsonObj.value(PARAM_RATIOLIMIT).toDouble(Torrent::USE_GLOBAL_RATIO);
const AddTorrentParams params
{
.name = {},
.category = jsonObj.value(PARAM_CATEGORY).toString(),
.tags = parseTagSet(jsonObj.value(PARAM_TAGS).toArray()),
.savePath = Path(jsonObj.value(PARAM_SAVEPATH).toString()),
.useDownloadPath = getOptionalBool(jsonObj, PARAM_USEDOWNLOADPATH),
.downloadPath = Path(jsonObj.value(PARAM_DOWNLOADPATH).toString()),
.addForced = (getEnum<TorrentOperatingMode>(jsonObj, PARAM_OPERATINGMODE) == TorrentOperatingMode::Forced),
.addToQueueTop = getOptionalBool(jsonObj, PARAM_QUEUETOP),
.addPaused = getOptionalBool(jsonObj, PARAM_STOPPED),
.stopCondition = getOptionalEnum<Torrent::StopCondition>(jsonObj, PARAM_STOPCONDITION),
.filePaths = {},
.filePriorities = {},
.skipChecking = jsonObj.value(PARAM_SKIPCHECKING).toBool(),
.contentLayout = getOptionalEnum<TorrentContentLayout>(jsonObj, PARAM_CONTENTLAYOUT),
.useAutoTMM = getOptionalBool(jsonObj, PARAM_AUTOTMM),
.uploadLimit = jsonObj.value(PARAM_UPLOADLIMIT).toInt(-1),
.downloadLimit = jsonObj.value(PARAM_DOWNLOADLIMIT).toInt(-1),
.seedingTimeLimit = jsonObj.value(PARAM_SEEDINGTIMELIMIT).toInt(Torrent::USE_GLOBAL_SEEDING_TIME),
.inactiveSeedingTimeLimit = jsonObj.value(PARAM_INACTIVESEEDINGTIMELIMIT).toInt(Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME),
.ratioLimit = jsonObj.value(PARAM_RATIOLIMIT).toDouble(Torrent::USE_GLOBAL_RATIO)
};
return params;
}
QJsonObject BitTorrent::serializeAddTorrentParams(const AddTorrentParams &params)
{
QJsonObject jsonObj {
QJsonObject jsonObj
{
{PARAM_CATEGORY, params.category},
{PARAM_TAGS, serializeTagSet(params.tags)},
{PARAM_SAVEPATH, params.savePath.data()},

View file

@ -69,9 +69,9 @@ namespace BitTorrent
int seedingTimeLimit = Torrent::USE_GLOBAL_SEEDING_TIME;
int inactiveSeedingTimeLimit = Torrent::USE_GLOBAL_INACTIVE_SEEDING_TIME;
qreal ratioLimit = Torrent::USE_GLOBAL_RATIO;
};
bool operator==(const AddTorrentParams &lhs, const AddTorrentParams &rhs);
friend bool operator==(const AddTorrentParams &lhs, const AddTorrentParams &rhs) = default;
};
AddTorrentParams parseAddTorrentParams(const QJsonObject &jsonObj);
QJsonObject serializeAddTorrentParams(const AddTorrentParams &params);

View file

@ -76,7 +76,7 @@ namespace
template <typename LTStr>
QString fromLTString(const LTStr &str)
{
return QString::fromUtf8(str.data(), static_cast<int>(str.size()));
return QString::fromUtf8(str.data(), static_cast<qsizetype>(str.size()));
}
using ListType = lt::entry::list_type;

View file

@ -148,7 +148,7 @@ namespace
template <typename LTStr>
QString fromLTString(const LTStr &str)
{
return QString::fromUtf8(str.data(), static_cast<int>(str.size()));
return QString::fromUtf8(str.data(), static_cast<qsizetype>(str.size()));
}
QString quoted(const QString &name)

View file

@ -227,20 +227,20 @@ void TorrentCreatorDialog::onCreateButtonClicked()
.replace(QRegularExpression(u"\n\n[\n]+"_s), u"\n\n"_s).split(u'\n');
const BitTorrent::TorrentCreatorParams params
{
m_ui->checkPrivate->isChecked()
.isPrivate = m_ui->checkPrivate->isChecked(),
#ifdef QBT_USES_LIBTORRENT2
, getTorrentFormat()
.torrentFormat = getTorrentFormat(),
#else
, m_ui->checkOptimizeAlignment->isChecked()
, getPaddedFileSizeLimit()
.isAlignmentOptimized = m_ui->checkOptimizeAlignment->isChecked(),
.paddedFileSizeLimit = getPaddedFileSizeLimit(),
#endif
, getPieceSize()
, inputPath
, destPath
, m_ui->txtComment->toPlainText()
, m_ui->lineEditSource->text()
, trackers
, m_ui->URLSeedsList->toPlainText().split(u'\n', Qt::SkipEmptyParts)
.pieceSize = getPieceSize(),
.inputPath = inputPath,
.savePath = destPath,
.comment = m_ui->txtComment->toPlainText(),
.source = m_ui->lineEditSource->text(),
.trackers = trackers,
.urlSeeds = m_ui->URLSeedsList->toPlainText().split(u'\n', Qt::SkipEmptyParts)
};
auto *torrentCreator = new BitTorrent::TorrentCreator(params);
@ -257,7 +257,7 @@ void TorrentCreatorDialog::handleCreationFailure(const QString &msg)
{
// Remove busy cursor
setCursor(QCursor(Qt::ArrowCursor));
QMessageBox::information(this, tr("Torrent creation failed"), tr("Reason: %1").arg(msg));
QMessageBox::information(this, tr("Torrent creation failed"), msg);
setInteractionEnabled(true);
}

View file

@ -754,7 +754,7 @@ void TorrentsController::addAction()
}
}
const DataMap torrents = data();
const DataMap &torrents = data();
for (auto it = torrents.constBegin(); it != torrents.constEnd(); ++it)
{
if (const auto loadResult = BitTorrent::TorrentDescriptor::load(it.value()))