Replace remaining instances of Q_FOREACH with C++11 range-based for loops

This commit is contained in:
Janek Bevendorff 2017-03-10 15:45:00 +01:00
parent 2872f1706c
commit cb51ec61f7
No known key found for this signature in database
GPG key ID: CFEC2F6850BFFA53
6 changed files with 72 additions and 44 deletions

View file

@ -97,11 +97,11 @@ QString PasswordGenerator::generatePassword() const
int PasswordGenerator::getbits() const int PasswordGenerator::getbits() const
{ {
QVector<PasswordGroup> groups = passwordGroups(); const QVector<PasswordGroup> groups = passwordGroups();
int bits = 0; int bits = 0;
QVector<QChar> passwordChars; QVector<QChar> passwordChars;
Q_FOREACH (const PasswordGroup& group, groups) { for (const PasswordGroup& group: groups) {
bits += group.size(); bits += group.size();
} }

View file

@ -21,6 +21,7 @@
#include "KMessageWidget.h" #include "KMessageWidget.h"
#include "core/FilePath.h" #include "core/FilePath.h"
#include "core/Global.h"
#include <QAction> #include <QAction>
#include <QEvent> #include <QEvent>
@ -117,7 +118,8 @@ void KMessageWidgetPrivate::createLayout()
qDeleteAll(buttons); qDeleteAll(buttons);
buttons.clear(); buttons.clear();
Q_FOREACH (QAction *action, q->actions()) { const auto actions = q->actions();
for (QAction *action: actions) {
QToolButton *button = new QToolButton(content); QToolButton *button = new QToolButton(content);
button->setDefaultAction(action); button->setDefaultAction(action);
button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
@ -137,7 +139,7 @@ void KMessageWidgetPrivate::createLayout()
QHBoxLayout *buttonLayout = new QHBoxLayout; QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(); buttonLayout->addStretch();
Q_FOREACH (QToolButton *button, buttons) { for (QToolButton* button: asConst(buttons)) {
// For some reason, calling show() is necessary if wordwrap is true, // For some reason, calling show() is necessary if wordwrap is true,
// otherwise the buttons do not show up. It is not needed if // otherwise the buttons do not show up. It is not needed if
// wordwrap is false. // wordwrap is false.
@ -151,7 +153,7 @@ void KMessageWidgetPrivate::createLayout()
layout->addWidget(iconLabel); layout->addWidget(iconLabel);
layout->addWidget(textLabel); layout->addWidget(textLabel);
Q_FOREACH (QToolButton *button, buttons) { for (QToolButton* button: asConst(buttons)) {
layout->addWidget(button); layout->addWidget(button);
} }

View file

@ -23,6 +23,7 @@
#include "core/Config.h" #include "core/Config.h"
#include "core/Translator.h" #include "core/Translator.h"
#include "core/FilePath.h" #include "core/FilePath.h"
#include "core/Global.h"
class SettingsWidget::ExtraPage class SettingsWidget::ExtraPage
{ {
@ -144,8 +145,9 @@ void SettingsWidget::loadSettings()
m_secUi->passwordRepeatCheckBox->setChecked(config()->get("security/passwordsrepeat").toBool()); m_secUi->passwordRepeatCheckBox->setChecked(config()->get("security/passwordsrepeat").toBool());
Q_FOREACH (const ExtraPage& page, m_extraPages) for (const ExtraPage& page: asConst(m_extraPages)) {
page.loadSettings(); page.loadSettings();
}
setCurrentPage(0); setCurrentPage(0);
} }
@ -190,8 +192,9 @@ void SettingsWidget::saveSettings()
config()->set("security/passwordscleartext", m_secUi->passwordCleartextCheckBox->isChecked()); config()->set("security/passwordscleartext", m_secUi->passwordCleartextCheckBox->isChecked());
config()->set("security/passwordsrepeat", m_secUi->passwordRepeatCheckBox->isChecked()); config()->set("security/passwordsrepeat", m_secUi->passwordRepeatCheckBox->isChecked());
Q_FOREACH (const ExtraPage& page, m_extraPages) for (const ExtraPage& page: asConst(m_extraPages)) {
page.saveSettings(); page.saveSettings();
}
Q_EMIT editFinished(true); Q_EMIT editFinished(true);
} }

View file

@ -36,9 +36,10 @@ void AccessControlDialog::setUrl(const QString &url)
void AccessControlDialog::setItems(const QList<Entry*> &items) void AccessControlDialog::setItems(const QList<Entry*> &items)
{ {
Q_FOREACH (Entry * entry, items) for (Entry* entry: items) {
ui->itemsList->addItem(entry->title() + " - " + entry->username()); ui->itemsList->addItem(entry->title() + " - " + entry->username());
} }
}
bool AccessControlDialog::remember() const bool AccessControlDialog::remember() const
{ {

View file

@ -17,6 +17,7 @@
#include "crypto/Random.h" #include "crypto/Random.h"
#include "crypto/SymmetricCipher.h" #include "crypto/SymmetricCipher.h"
#include "crypto/SymmetricCipherGcrypt.h" #include "crypto/SymmetricCipherGcrypt.h"
#include "core/Global.h"
namespace KeepassHttpProtocol namespace KeepassHttpProtocol
{ {
@ -370,8 +371,9 @@ QVariant Response::getEntries() const
QList<QVariant> res; QList<QVariant> res;
res.reserve(m_entries.size()); res.reserve(m_entries.size());
Q_FOREACH (const Entry &entry, m_entries) for (const Entry& entry: asConst(m_entries)) {
res.append(qobject2qvariant(&entry)); res.append(qobject2qvariant(&entry));
}
return res; return res;
} }
@ -383,14 +385,16 @@ void Response::setEntries(const QList<Entry> &entries)
QList<Entry> encryptedEntries; QList<Entry> encryptedEntries;
encryptedEntries.reserve(m_count); encryptedEntries.reserve(m_count);
Q_FOREACH (const Entry &entry, entries) { for (const Entry& entry: entries) {
Entry encryptedEntry(encrypt(entry.name(), m_cipher), Entry encryptedEntry(encrypt(entry.name(), m_cipher),
encrypt(entry.login(), m_cipher), encrypt(entry.login(), m_cipher),
entry.password().isNull() ? QString() : encrypt(entry.password(), m_cipher), entry.password().isNull() ? QString() : encrypt(entry.password(), m_cipher),
encrypt(entry.uuid(), m_cipher)); encrypt(entry.uuid(), m_cipher));
Q_FOREACH (const StringField & field, entry.stringFields()) const auto stringFields = entry.stringFields();
for (const StringField& field: stringFields) {
encryptedEntry.addStringField(encrypt(field.key(), m_cipher), encryptedEntry.addStringField(encrypt(field.key(), m_cipher),
encrypt(field.value(), m_cipher)); encrypt(field.value(), m_cipher));
}
encryptedEntries << encryptedEntry; encryptedEntries << encryptedEntry;
} }
m_entries = encryptedEntries; m_entries = encryptedEntries;
@ -508,8 +512,9 @@ QVariant Entry::getStringFields() const
QList<QVariant> res; QList<QVariant> res;
res.reserve(m_stringFields.size()); res.reserve(m_stringFields.size());
Q_FOREACH (const StringField &stringfield, m_stringFields) for (const StringField& stringfield: asConst(m_stringFields)) {
res.append(qobject2qvariant(&stringfield)); res.append(qobject2qvariant(&stringfield));
}
return res; return res;
} }

View file

@ -23,6 +23,7 @@
#include "core/Database.h" #include "core/Database.h"
#include "core/Entry.h" #include "core/Entry.h"
#include "core/Global.h"
#include "core/Group.h" #include "core/Group.h"
#include "core/EntrySearcher.h" #include "core/EntrySearcher.h"
#include "core/Metadata.h" #include "core/Metadata.h"
@ -189,8 +190,9 @@ bool Service::removeFirstDomain(QString & hostname)
QList<Entry*> Service::searchEntries(Database* db, const QString& hostname) QList<Entry*> Service::searchEntries(Database* db, const QString& hostname)
{ {
QList<Entry*> entries; QList<Entry*> entries;
if (Group* rootGroup = db->rootGroup()) if (Group* rootGroup = db->rootGroup()) {
Q_FOREACH (Entry* entry, EntrySearcher().search(hostname, rootGroup, Qt::CaseInsensitive)) { const auto results = EntrySearcher().search(hostname, rootGroup, Qt::CaseInsensitive);
for (Entry* entry: results) {
QString title = entry->title(); QString title = entry->title();
QString url = entry->url(); QString url = entry->url();
@ -201,6 +203,7 @@ QList<Entry*> Service::searchEntries(Database* db, const QString& hostname)
|| (matchUrlScheme(url) && hostname.endsWith(QUrl(url).host())) ) || (matchUrlScheme(url) && hostname.endsWith(QUrl(url).host())) )
entries.append(entry); entries.append(entry);
} }
}
return entries; return entries;
} }
@ -223,8 +226,9 @@ QList<Entry*> Service::searchEntries(const QString& text)
QString hostname = QUrl(text).host(); QString hostname = QUrl(text).host();
QList<Entry*> entries; QList<Entry*> entries;
do { do {
Q_FOREACH (Database* db, databases) for (Database* db: asConst(databases)) {
entries << searchEntries(db, hostname); entries << searchEntries(db, hostname);
}
} while(entries.isEmpty() && removeFirstDomain(hostname)); } while(entries.isEmpty() && removeFirstDomain(hostname));
return entries; return entries;
@ -249,10 +253,13 @@ KeepassHttpProtocol::Entry Service::prepareEntry(const Entry* entry)
KeepassHttpProtocol::Entry res(entry->resolvePlaceholder(entry->title()), entry->resolvePlaceholder(entry->username()), entry->resolvePlaceholder(entry->password()), entry->uuid().toHex()); KeepassHttpProtocol::Entry res(entry->resolvePlaceholder(entry->title()), entry->resolvePlaceholder(entry->username()), entry->resolvePlaceholder(entry->password()), entry->uuid().toHex());
if (HttpSettings::supportKphFields()) { if (HttpSettings::supportKphFields()) {
const EntryAttributes * attr = entry->attributes(); const EntryAttributes * attr = entry->attributes();
Q_FOREACH (const QString& key, attr->keys()) const auto keys = attr->keys();
if (key.startsWith(QLatin1String("KPH: "))) for (const QString& key: keys) {
if (key.startsWith(QLatin1String("KPH: "))) {
res.addStringField(key, attr->value(key)); res.addStringField(key, attr->value(key));
} }
}
}
return res; return res;
} }
@ -318,7 +325,8 @@ QList<KeepassHttpProtocol::Entry> Service::findMatchingEntries(const QString& /*
//Check entries for authorization //Check entries for authorization
QList<Entry*> pwEntriesToConfirm; QList<Entry*> pwEntriesToConfirm;
QList<Entry*> pwEntries; QList<Entry*> pwEntries;
Q_FOREACH (Entry * entry, searchEntries(url)) { const auto entries = searchEntries(url);
for (Entry* entry: entries) {
switch(checkAccess(entry, host, submitHost, realm)) { switch(checkAccess(entry, host, submitHost, realm)) {
case Denied: case Denied:
continue; continue;
@ -352,7 +360,7 @@ QList<KeepassHttpProtocol::Entry> Service::findMatchingEntries(const QString& /*
int res = dlg.exec(); int res = dlg.exec();
if (dlg.remember()) { if (dlg.remember()) {
Q_FOREACH (Entry * entry, pwEntriesToConfirm) { for (Entry* entry: asConst(pwEntriesToConfirm)) {
EntryConfig config; EntryConfig config;
config.load(entry); config.load(entry);
if (res == QDialog::Accepted) { if (res == QDialog::Accepted) {
@ -385,26 +393,20 @@ QList<KeepassHttpProtocol::Entry> Service::findMatchingEntries(const QString& /*
//Cache priorities //Cache priorities
QHash<const Entry*, int> priorities; QHash<const Entry*, int> priorities;
priorities.reserve(pwEntries.size()); priorities.reserve(pwEntries.size());
Q_FOREACH (const Entry * entry, pwEntries) for (const Entry* entry: asConst(pwEntries)) {
priorities.insert(entry, sortPriority(entry, host, submitUrl, baseSubmitURL)); priorities.insert(entry, sortPriority(entry, host, submitUrl, baseSubmitURL));
}
//Sort by priorities //Sort by priorities
std::sort(pwEntries.begin(), pwEntries.end(), SortEntries(priorities, HttpSettings::sortByTitle() ? "Title" : "UserName")); std::sort(pwEntries.begin(), pwEntries.end(), SortEntries(priorities, HttpSettings::sortByTitle() ? "Title" : "UserName"));
} }
//if (pwEntries.count() > 0)
//{
// var names = (from e in resp.Entries select e.Name).Distinct<string>();
// var n = String.Join("\n ", names.ToArray<string>());
// if (HttpSettings::receiveCredentialNotification())
// ShowNotification(QString("%0: %1 is receiving credentials for:\n%2").arg(Id).arg(host).arg(n)));
//}
//Fill the list //Fill the list
QList<KeepassHttpProtocol::Entry> result; QList<KeepassHttpProtocol::Entry> result;
result.reserve(pwEntries.count()); result.reserve(pwEntries.count());
Q_FOREACH (Entry * entry, pwEntries) for (Entry* entry: asConst(pwEntries)) {
result << prepareEntry(entry); result << prepareEntry(entry);
}
return result; return result;
} }
@ -416,12 +418,19 @@ int Service::countMatchingEntries(const QString &, const QString &url, const QSt
QList<KeepassHttpProtocol::Entry> Service::searchAllEntries(const QString &) QList<KeepassHttpProtocol::Entry> Service::searchAllEntries(const QString &)
{ {
QList<KeepassHttpProtocol::Entry> result; QList<KeepassHttpProtocol::Entry> result;
if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget()) if (DatabaseWidget* dbWidget = m_dbTabWidget->currentDatabaseWidget()) {
if (Database * db = dbWidget->database()) if (Database* db = dbWidget->database()) {
if (Group * rootGroup = db->rootGroup()) if (Group* rootGroup = db->rootGroup()) {
Q_FOREACH (Entry * entry, rootGroup->entriesRecursive()) const auto entries = rootGroup->entriesRecursive();
if (!entry->url().isEmpty() || QUrl(entry->title()).isValid()) for (Entry* entry: entries) {
result << KeepassHttpProtocol::Entry(entry->title(), entry->username(), QString(), entry->uuid().toHex()); if (!entry->url().isEmpty() || QUrl(entry->title()).isValid()) {
result << KeepassHttpProtocol::Entry(entry->title(), entry->username(),
QString(), entry->uuid().toHex());
}
}
}
}
}
return result; return result;
} }
@ -430,11 +439,15 @@ Group * Service::findCreateAddEntryGroup()
if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget()) if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget())
if (Database * db = dbWidget->database()) if (Database * db = dbWidget->database())
if (Group * rootGroup = db->rootGroup()) { if (Group * rootGroup = db->rootGroup()) {
const QString groupName = QLatin1String(KEEPASSHTTP_GROUP_NAME);//TODO: setting to decide where new keys are created //TODO: setting to decide where new keys are created
const QString groupName = QLatin1String(KEEPASSHTTP_GROUP_NAME);
Q_FOREACH (const Group * g, rootGroup->groupsRecursive(true)) const auto groups = rootGroup->groupsRecursive(true);
if (g->name() == groupName) for (const Group * g: groups) {
if (g->name() == groupName) {
return db->resolveGroup(g->uuid()); return db->resolveGroup(g->uuid());
}
}
Group * group; Group * group;
group = new Group(); group = new Group();
@ -507,14 +520,18 @@ void Service::removeSharedEncryptionKeys()
QMessageBox::Ok); QMessageBox::Ok);
} else if (Entry* entry = getConfigEntry()) { } else if (Entry* entry = getConfigEntry()) {
QStringList keysToRemove; QStringList keysToRemove;
Q_FOREACH (const QString& key, entry->attributes()->keys()) const auto keys = entry->attributes()->keys();
if (key.startsWith(ASSOCIATE_KEY_PREFIX)) for (const QString& key: keys) {
if (key.startsWith(ASSOCIATE_KEY_PREFIX)) {
keysToRemove << key; keysToRemove << key;
}
}
if(keysToRemove.count()) { if(keysToRemove.count()) {
entry->beginUpdate(); entry->beginUpdate();
Q_FOREACH (const QString& key, keysToRemove) for (const QString& key: asConst(keysToRemove)) {
entry->attributes()->remove(key); entry->attributes()->remove(key);
}
entry->endUpdate(); entry->endUpdate();
const int count = keysToRemove.count(); const int count = keysToRemove.count();
@ -548,7 +565,7 @@ void Service::removeStoredPermissions()
progress.setWindowModality(Qt::WindowModal); progress.setWindowModality(Qt::WindowModal);
uint counter = 0; uint counter = 0;
Q_FOREACH (Entry* entry, entries) { for (Entry* entry: asConst(entries)) {
if (progress.wasCanceled()) if (progress.wasCanceled())
return; return;
if (entry->attributes()->contains(KEEPASSHTTP_NAME)) { if (entry->attributes()->contains(KEEPASSHTTP_NAME)) {