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
{
QVector<PasswordGroup> groups = passwordGroups();
const QVector<PasswordGroup> groups = passwordGroups();
int bits = 0;
QVector<QChar> passwordChars;
Q_FOREACH (const PasswordGroup& group, groups) {
for (const PasswordGroup& group: groups) {
bits += group.size();
}

View File

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

View File

@ -23,6 +23,7 @@
#include "core/Config.h"
#include "core/Translator.h"
#include "core/FilePath.h"
#include "core/Global.h"
class SettingsWidget::ExtraPage
{
@ -144,8 +145,9 @@ void SettingsWidget::loadSettings()
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();
}
setCurrentPage(0);
}
@ -190,8 +192,9 @@ void SettingsWidget::saveSettings()
config()->set("security/passwordscleartext", m_secUi->passwordCleartextCheckBox->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();
}
Q_EMIT editFinished(true);
}

View File

@ -34,10 +34,11 @@ void AccessControlDialog::setUrl(const QString &url)
"Please select whether you want to allow access.")).arg(QUrl(url).host()));
}
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());
}
}
bool AccessControlDialog::remember() const

View File

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

View File

@ -23,6 +23,7 @@
#include "core/Database.h"
#include "core/Entry.h"
#include "core/Global.h"
#include "core/Group.h"
#include "core/EntrySearcher.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*> entries;
if (Group* rootGroup = db->rootGroup())
Q_FOREACH (Entry* entry, EntrySearcher().search(hostname, rootGroup, Qt::CaseInsensitive)) {
if (Group* rootGroup = db->rootGroup()) {
const auto results = EntrySearcher().search(hostname, rootGroup, Qt::CaseInsensitive);
for (Entry* entry: results) {
QString title = entry->title();
QString url = entry->url();
@ -201,6 +203,7 @@ QList<Entry*> Service::searchEntries(Database* db, const QString& hostname)
|| (matchUrlScheme(url) && hostname.endsWith(QUrl(url).host())) )
entries.append(entry);
}
}
return entries;
}
@ -223,8 +226,9 @@ QList<Entry*> Service::searchEntries(const QString& text)
QString hostname = QUrl(text).host();
QList<Entry*> entries;
do {
Q_FOREACH (Database* db, databases)
for (Database* db: asConst(databases)) {
entries << searchEntries(db, hostname);
}
} while(entries.isEmpty() && removeFirstDomain(hostname));
return entries;
@ -249,9 +253,12 @@ 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());
if (HttpSettings::supportKphFields()) {
const EntryAttributes * attr = entry->attributes();
Q_FOREACH (const QString& key, attr->keys())
if (key.startsWith(QLatin1String("KPH: ")))
const auto keys = attr->keys();
for (const QString& key: keys) {
if (key.startsWith(QLatin1String("KPH: "))) {
res.addStringField(key, attr->value(key));
}
}
}
return res;
}
@ -318,7 +325,8 @@ QList<KeepassHttpProtocol::Entry> Service::findMatchingEntries(const QString& /*
//Check entries for authorization
QList<Entry*> pwEntriesToConfirm;
QList<Entry*> pwEntries;
Q_FOREACH (Entry * entry, searchEntries(url)) {
const auto entries = searchEntries(url);
for (Entry* entry: entries) {
switch(checkAccess(entry, host, submitHost, realm)) {
case Denied:
continue;
@ -352,7 +360,7 @@ QList<KeepassHttpProtocol::Entry> Service::findMatchingEntries(const QString& /*
int res = dlg.exec();
if (dlg.remember()) {
Q_FOREACH (Entry * entry, pwEntriesToConfirm) {
for (Entry* entry: asConst(pwEntriesToConfirm)) {
EntryConfig config;
config.load(entry);
if (res == QDialog::Accepted) {
@ -383,28 +391,22 @@ QList<KeepassHttpProtocol::Entry> Service::findMatchingEntries(const QString& /*
const QString baseSubmitURL = url.toString(QUrl::StripTrailingSlash | QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment);
//Cache priorities
QHash<const Entry *, int> priorities;
QHash<const Entry*, int> priorities;
priorities.reserve(pwEntries.size());
Q_FOREACH (const Entry * entry, pwEntries)
for (const Entry* entry: asConst(pwEntries)) {
priorities.insert(entry, sortPriority(entry, host, submitUrl, baseSubmitURL));
}
//Sort by priorities
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
QList<KeepassHttpProtocol::Entry> result;
result.reserve(pwEntries.count());
Q_FOREACH (Entry * entry, pwEntries)
for (Entry* entry: asConst(pwEntries)) {
result << prepareEntry(entry);
}
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> result;
if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget())
if (Database * db = dbWidget->database())
if (Group * rootGroup = db->rootGroup())
Q_FOREACH (Entry * entry, rootGroup->entriesRecursive())
if (!entry->url().isEmpty() || QUrl(entry->title()).isValid())
result << KeepassHttpProtocol::Entry(entry->title(), entry->username(), QString(), entry->uuid().toHex());
if (DatabaseWidget* dbWidget = m_dbTabWidget->currentDatabaseWidget()) {
if (Database* db = dbWidget->database()) {
if (Group* rootGroup = db->rootGroup()) {
const auto entries = rootGroup->entriesRecursive();
for (Entry* entry: entries) {
if (!entry->url().isEmpty() || QUrl(entry->title()).isValid()) {
result << KeepassHttpProtocol::Entry(entry->title(), entry->username(),
QString(), entry->uuid().toHex());
}
}
}
}
}
return result;
}
@ -430,11 +439,15 @@ Group * Service::findCreateAddEntryGroup()
if (DatabaseWidget * dbWidget = m_dbTabWidget->currentDatabaseWidget())
if (Database * db = dbWidget->database())
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))
if (g->name() == groupName)
const auto groups = rootGroup->groupsRecursive(true);
for (const Group * g: groups) {
if (g->name() == groupName) {
return db->resolveGroup(g->uuid());
}
}
Group * group;
group = new Group();
@ -507,14 +520,18 @@ void Service::removeSharedEncryptionKeys()
QMessageBox::Ok);
} else if (Entry* entry = getConfigEntry()) {
QStringList keysToRemove;
Q_FOREACH (const QString& key, entry->attributes()->keys())
if (key.startsWith(ASSOCIATE_KEY_PREFIX))
const auto keys = entry->attributes()->keys();
for (const QString& key: keys) {
if (key.startsWith(ASSOCIATE_KEY_PREFIX)) {
keysToRemove << key;
}
}
if(keysToRemove.count()) {
entry->beginUpdate();
Q_FOREACH (const QString& key, keysToRemove)
for (const QString& key: asConst(keysToRemove)) {
entry->attributes()->remove(key);
}
entry->endUpdate();
const int count = keysToRemove.count();
@ -548,7 +565,7 @@ void Service::removeStoredPermissions()
progress.setWindowModality(Qt::WindowModal);
uint counter = 0;
Q_FOREACH (Entry* entry, entries) {
for (Entry* entry: asConst(entries)) {
if (progress.wasCanceled())
return;
if (entry->attributes()->contains(KEEPASSHTTP_NAME)) {