qt: Replace Q_FOREACH with range-for

* lang/qt/src/qgpgmekeyformailboxjob.cpp (do_work): Replace Q_FOREACH
with range-for.
* lang/qt/src/qgpgmekeylistjob.cpp (QGpgMEKeyListJob::resultHook):
Ditto.
* lang/qt/src/threadedjobmixin.cpp (_detail::audit_log_as_html): Ditto.
* lang/qt/tests/t-keylist.cpp (KeyListTest::testPubkeyAlgoAsString):
Ditto.
* lang/qt/tests/t-keylocate.cpp (KeyLocateTest::testDaneKeyLocate,
KeyLocateTest::testKeyLocateSingle): Ditto.
* lang/qt/src/qgpgmesignencryptjob.cpp (sign_encrypt): Replace Q_FOREACH
with range-for. Add braces around single statement blocks.
* lang/qt/src/qgpgmesignjob.cpp (sign): Ditto.
* lang/qt/src/qgpgmenewcryptoconfig.cpp
(QGpgMENewCryptoConfig::reloadConfiguration,
QGpgMENewCryptoConfig::sync,
QGpgMENewCryptoConfigComponent::setComponent,
QGpgMENewCryptoConfigEntry::urlValueList,
QGpgMENewCryptoConfigEntry::setURLValueList): Replace Q_FOREACH with
range-for. Fix indentation. Add braces around single statement blocks.
--

Where necessary the iterated object is wrapped with qAsConst to avoid
deep copies of the iterated object.
This commit is contained in:
Ingo Klöcker 2023-12-13 11:45:26 +01:00
parent f7d69de030
commit d5dafb2ae3
No known key found for this signature in database
GPG Key ID: F5A5D1692277A1E9
8 changed files with 44 additions and 36 deletions

View File

@ -86,12 +86,12 @@ static QGpgMEKeyForMailboxJob::result_type do_work(Context *ctx, const QString &
// See: https://bugs.gnupg.org/gnupg/issue2359 // See: https://bugs.gnupg.org/gnupg/issue2359
Key keyC; Key keyC;
UserID uidC; UserID uidC;
Q_FOREACH (const Key k, keys) { for (const Key &k : keys) {
if (canEncrypt && !k.canEncrypt()) { if (canEncrypt && !k.canEncrypt()) {
continue; continue;
} }
/* First get the uid that matches the mailbox */ /* First get the uid that matches the mailbox */
Q_FOREACH (const UserID u, k.userIDs()) { for (const UserID &u : k.userIDs()) {
if (QString::fromUtf8(u.email()).toLower() == mailbox.toLower()) { if (QString::fromUtf8(u.email()).toLower() == mailbox.toLower()) {
if (uidC.isNull()) { if (uidC.isNull()) {
keyC = k; keyC = k;
@ -103,13 +103,13 @@ static QGpgMEKeyForMailboxJob::result_type do_work(Context *ctx, const QString &
} else if (uidC.validity() == u.validity() && uidIsOk(u)) { } else if (uidC.validity() == u.validity() && uidIsOk(u)) {
/* Both are the same check which one is newer. */ /* Both are the same check which one is newer. */
time_t oldTime = 0; time_t oldTime = 0;
Q_FOREACH (const Subkey s, keyC.subkeys()) { for (const Subkey &s : keyC.subkeys()) {
if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) { if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
oldTime = s.creationTime(); oldTime = s.creationTime();
} }
} }
time_t newTime = 0; time_t newTime = 0;
Q_FOREACH (const Subkey s, k.subkeys()) { for (const Subkey &s : k.subkeys()) {
if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) { if ((canEncrypt && s.canEncrypt()) && subkeyIsOk(s)) {
newTime = s.creationTime(); newTime = s.creationTime();
} }

View File

@ -152,7 +152,7 @@ KeyListResult QGpgMEKeyListJob::exec(const QStringList &patterns, bool secretOnl
void QGpgMEKeyListJob::resultHook(const result_type &tuple) void QGpgMEKeyListJob::resultHook(const result_type &tuple)
{ {
mResult = std::get<0>(tuple); mResult = std::get<0>(tuple);
Q_FOREACH (const Key &key, std::get<1>(tuple)) { for (const Key &key : std::get<1>(tuple)) {
Q_EMIT nextKey(key); Q_EMIT nextKey(key);
} }
} }

View File

@ -97,7 +97,7 @@ void QGpgMENewCryptoConfig::reloadConfiguration(bool)
KMessageBox::error(0, wmsg); KMessageBox::error(0, wmsg);
} }
#endif #endif
Q_FOREACH(const Component & c, components) { for (const Component &c : components) {
const std::shared_ptr<QGpgMENewCryptoConfigComponent> comp(new QGpgMENewCryptoConfigComponent); const std::shared_ptr<QGpgMENewCryptoConfigComponent> comp(new QGpgMENewCryptoConfigComponent);
comp->setComponent(c); comp->setComponent(c);
m_componentsByName[ comp->name() ] = comp; m_componentsByName[ comp->name() ] = comp;
@ -127,9 +127,10 @@ QGpgMENewCryptoConfigComponent *QGpgMENewCryptoConfig::component(const QString &
void QGpgMENewCryptoConfig::sync(bool runtime) void QGpgMENewCryptoConfig::sync(bool runtime)
{ {
Q_FOREACH(const std::shared_ptr<QGpgMENewCryptoConfigComponent> &c, m_componentsByName) for (const std::shared_ptr<QGpgMENewCryptoConfigComponent> &c : qAsConst(m_componentsByName)) {
c->sync(runtime); c->sync(runtime);
} }
}
void QGpgMENewCryptoConfig::clear() void QGpgMENewCryptoConfig::clear()
{ {
@ -156,7 +157,7 @@ void QGpgMENewCryptoConfigComponent::setComponent(const Component &component)
std::shared_ptr<QGpgMENewCryptoConfigGroup> group; std::shared_ptr<QGpgMENewCryptoConfigGroup> group;
const std::vector<Option> options = m_component.options(); const std::vector<Option> options = m_component.options();
Q_FOREACH(const Option & o, options) for (const Option &o : options) {
if (o.flags() & Group) { if (o.flags() & Group) {
if (group) { if (group) {
m_groupsByName[group->name()] = group; m_groupsByName[group->name()] = group;
@ -170,6 +171,7 @@ void QGpgMENewCryptoConfigComponent::setComponent(const Component &component)
} else { } else {
qCWarning(QGPGME_LOG) << "found no group for entry" << o.name() << "of component" << name(); qCWarning(QGPGME_LOG) << "found no group for entry" << o.name() << "of component" << name();
} }
}
if (group) { if (group) {
m_groupsByName[group->name()] = group; m_groupsByName[group->name()] = group;
} }
@ -578,7 +580,7 @@ QList<QUrl> QGpgMENewCryptoConfigEntry::urlValueList() const
const Argument arg = m_option.currentValue(); const Argument arg = m_option.currentValue();
const std::vector<const char *> values = arg.stringValues(); const std::vector<const char *> values = arg.stringValues();
QList<QUrl> ret; QList<QUrl> ret;
Q_FOREACH(const char *value, values) for (const char *value : values) {
if (type == FilenameType) { if (type == FilenameType) {
QUrl url; QUrl url;
url.setPath(QFile::decodeName(value)); url.setPath(QFile::decodeName(value));
@ -586,6 +588,7 @@ QList<QUrl> QGpgMENewCryptoConfigEntry::urlValueList() const
} else { } else {
ret << parseURL(type, QString::fromUtf8(value)); ret << parseURL(type, QString::fromUtf8(value));
} }
}
return ret; return ret;
} }
@ -678,12 +681,13 @@ void QGpgMENewCryptoConfigEntry::setURLValueList(const QList<QUrl> &urls)
Q_ASSERT(isList()); Q_ASSERT(isList());
std::vector<std::string> values; std::vector<std::string> values;
values.reserve(urls.size()); values.reserve(urls.size());
Q_FOREACH (const QUrl &url, urls) for (const QUrl &url : urls) {
if (type == FilenameType) { if (type == FilenameType) {
values.push_back(QFile::encodeName(url.path()).constData()); values.push_back(QFile::encodeName(url.path()).constData());
} else { } else {
values.push_back(splitURL(type, url).toUtf8().constData()); values.push_back(splitURL(type, url).toUtf8().constData());
} }
}
const auto err = m_option.setNewValue(m_option.createStringListArgument(values)); const auto err = m_option.setNewValue(m_option.createStringListArgument(values));
if (err) { if (err) {
qCWarning(QGPGME_LOG) << "setURLValueList: failed to set new value:" << err; qCWarning(QGPGME_LOG) << "setURLValueList: failed to set new value:" << err;

View File

@ -124,11 +124,13 @@ static QGpgMESignEncryptJob::result_type sign_encrypt(Context *ctx, QThread *thr
} }
ctx->clearSigningKeys(); ctx->clearSigningKeys();
Q_FOREACH (const Key &signer, signers) for (const Key &signer : signers) {
if (!signer.isNull()) if (!signer.isNull()) {
if (const Error err = ctx->addSigningKey(signer)) { if (const Error err = ctx->addSigningKey(signer)) {
return std::make_tuple(SigningResult(err), EncryptionResult(), QByteArray(), QString(), Error()); return std::make_tuple(SigningResult(err), EncryptionResult(), QByteArray(), QString(), Error());
} }
}
}
if (!cipherText) { if (!cipherText) {
QGpgME::QByteArrayDataProvider out; QGpgME::QByteArrayDataProvider out;

View File

@ -87,11 +87,13 @@ static QGpgMESignJob::result_type sign(Context *ctx, QThread *thread,
} }
ctx->clearSigningKeys(); ctx->clearSigningKeys();
Q_FOREACH (const Key &signer, signers) for (const Key &signer : signers) {
if (!signer.isNull()) if (!signer.isNull()) {
if (const Error err = ctx->addSigningKey(signer)) { if (const Error err = ctx->addSigningKey(signer)) {
return std::make_tuple(SigningResult(err), QByteArray(), QString(), Error()); return std::make_tuple(SigningResult(err), QByteArray(), QString(), Error());
} }
}
}
if (!signature) { if (!signature) {
QGpgME::QByteArrayDataProvider out; QGpgME::QByteArrayDataProvider out;

View File

@ -108,7 +108,7 @@ QString _detail::audit_log_as_html(Context *ctx, GpgME::Error &err)
static QList<QByteArray> from_sl(const QStringList &sl) static QList<QByteArray> from_sl(const QStringList &sl)
{ {
QList<QByteArray> result; QList<QByteArray> result;
Q_FOREACH (const QString &str, sl) { for (const QString &str : sl) {
result.append(str.toUtf8()); result.append(str.toUtf8());
} }

View File

@ -119,7 +119,7 @@ private Q_SLOTS:
{ Subkey::AlgoEDDSA, QStringLiteral("EdDSA") }, { Subkey::AlgoEDDSA, QStringLiteral("EdDSA") },
{ Subkey::AlgoUnknown, QString() } { Subkey::AlgoUnknown, QString() }
}; };
Q_FOREACH (Subkey::PubkeyAlgo algo, expected.keys()) { for (Subkey::PubkeyAlgo algo : expected.keys()) {
QVERIFY(QString::fromUtf8(Subkey::publicKeyAlgorithmAsString(algo)) == QVERIFY(QString::fromUtf8(Subkey::publicKeyAlgorithmAsString(algo)) ==
expected.value(algo)); expected.value(algo));
} }

View File

@ -75,7 +75,7 @@ private Q_SLOTS:
Key k = keys.front(); Key k = keys.front();
QVERIFY(k.numUserIDs()); QVERIFY(k.numUserIDs());
bool found = false; bool found = false;
Q_FOREACH (const UserID uid, k.userIDs()) { for (const UserID &uid : k.userIDs()) {
const QString mailBox = QString::fromUtf8(uid.email()); const QString mailBox = QString::fromUtf8(uid.email());
if (mTestpattern.toLower() == mailBox.toLower()) { if (mTestpattern.toLower() == mailBox.toLower()) {
found = true; found = true;
@ -109,7 +109,7 @@ private Q_SLOTS:
Key k = keys.front(); Key k = keys.front();
QVERIFY(k.numUserIDs()); QVERIFY(k.numUserIDs());
bool found = false; bool found = false;
Q_FOREACH (const UserID uid, k.userIDs()) { for (const UserID &uid : k.userIDs()) {
const QString mailBox = QString::fromUtf8(uid.email()); const QString mailBox = QString::fromUtf8(uid.email());
if (mTestpattern.toLower() == mailBox.toLower()) { if (mTestpattern.toLower() == mailBox.toLower()) {
found = true; found = true;