Skip to content

Commit

Permalink
Convert Q_FOREACH loops to C++11 for loops.
Browse files Browse the repository at this point in the history
Q_FOREACH will de deprecated soon.
  • Loading branch information
debfx committed Sep 2, 2016
1 parent 1a87331 commit b654fde
Show file tree
Hide file tree
Showing 31 changed files with 202 additions and 143 deletions.
18 changes: 10 additions & 8 deletions src/autotype/AutoType.cpp
Expand Up @@ -157,7 +157,7 @@ void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow, const QS

QCoreApplication::processEvents(QEventLoop::AllEvents, 10);

Q_FOREACH (AutoTypeAction* action, actions) {
for (AutoTypeAction* action : asConst(actions)) {
if (m_plugin->activeWindow() != window) {
qWarning("Active window changed, interrupting auto-type.");
break;
Expand Down Expand Up @@ -187,8 +187,9 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
QList<Entry*> entryList;
QHash<Entry*, QString> sequenceHash;

Q_FOREACH (Database* db, dbList) {
Q_FOREACH (Entry* entry, db->rootGroup()->entriesRecursive()) {
for (Database* db : dbList) {
const QList<Entry*> dbEntries = db->rootGroup()->entriesRecursive();
for (Entry* entry : dbEntries) {
QString sequence = autoTypeSequence(entry, windowTitle);
if (!sequence.isEmpty()) {
entryList << entry;
Expand Down Expand Up @@ -300,7 +301,7 @@ bool AutoType::parseActions(const QString& sequence, const Entry* entry, QList<A
QString tmpl;
bool inTmpl = false;

Q_FOREACH (const QChar& ch, sequence) {
for (const QChar& ch : sequence) {
// TODO: implement support for {{}, {}} and {DELAY=X}

if (inTmpl) {
Expand Down Expand Up @@ -482,10 +483,10 @@ QList<AutoTypeAction*> AutoType::createActionFromTemplate(const QString& tmpl, c
}


QString placeholder = QString("{%1}").arg(tmplName);
QString resolved = entry->resolvePlaceholders(placeholder);
const QString placeholder = QString("{%1}").arg(tmplName);
const QString resolved = entry->resolvePlaceholders(placeholder);
if (placeholder != resolved) {
Q_FOREACH (const QChar& ch, resolved) {
for (const QChar& ch : resolved) {
if (ch == '\n') {
list.append(new AutoTypeKey(Qt::Key_Enter));
}
Expand All @@ -511,7 +512,8 @@ QString AutoType::autoTypeSequence(const Entry* entry, const QString& windowTitl
QString sequence;
if (!windowTitle.isEmpty()) {
bool match = false;
Q_FOREACH (const AutoTypeAssociations::Association& assoc, entry->autoTypeAssociations()->getAll()) {
const QList<AutoTypeAssociations::Association> assocList = entry->autoTypeAssociations()->getAll();
for (const AutoTypeAssociations::Association& assoc : assocList) {
if (windowMatches(windowTitle, assoc.window)) {
if (!assoc.sequence.isEmpty()) {
sequence = assoc.sequence;
Expand Down
2 changes: 1 addition & 1 deletion src/autotype/WildcardMatcher.cpp
Expand Up @@ -70,7 +70,7 @@ bool WildcardMatcher::startOrEndDoesNotMatch(const QStringList& parts)
bool WildcardMatcher::partsMatch(const QStringList& parts)
{
int index = 0;
Q_FOREACH (const QString& part, parts) {
for (const QString& part : parts) {
int matchIndex = getMatchIndex(part, index);
if (noMatchFound(matchIndex)) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion src/autotype/xcb/AutoTypeXCB.cpp
Expand Up @@ -344,7 +344,7 @@ QList<Window> AutoTypePlatformX11::widgetsToX11Windows(const QWidgetList& widget
{
QList<Window> windows;

Q_FOREACH (const QWidget* widget, widgetList) {
for (const QWidget* widget : widgetList) {
windows.append(widget->effectiveWinId());
}

Expand Down
9 changes: 6 additions & 3 deletions src/core/Database.cpp
Expand Up @@ -91,13 +91,15 @@ Entry* Database::resolveEntry(const Uuid& uuid)

Entry* Database::recFindEntry(const Uuid& uuid, Group* group)
{
Q_FOREACH (Entry* entry, group->entries()) {
const QList<Entry*> entryList = group->entries();
for (Entry* entry : entryList) {
if (entry->uuid() == uuid) {
return entry;
}
}

Q_FOREACH (Group* child, group->children()) {
const QList<Group*> children = group->children();
for (Group* child : children) {
Entry* result = recFindEntry(uuid, child);
if (result) {
return result;
Expand All @@ -118,7 +120,8 @@ Group* Database::recFindGroup(const Uuid& uuid, Group* group)
return group;
}

Q_FOREACH (Group* child, group->children()) {
const QList<Group*> children = group->children();
for (Group* child : children) {
Group* result = recFindGroup(uuid, child);
if (result) {
return result;
Expand Down
8 changes: 4 additions & 4 deletions src/core/Entry.cpp
Expand Up @@ -382,7 +382,7 @@ void Entry::removeHistoryItems(const QList<Entry*>& historyEntries)
return;
}

Q_FOREACH (Entry* entry, historyEntries) {
for (Entry* entry : historyEntries) {
Q_ASSERT(!entry->parent());
Q_ASSERT(entry->uuid() == uuid());
Q_ASSERT(m_history.contains(entry));
Expand Down Expand Up @@ -431,8 +431,8 @@ void Entry::truncateHistory()
if (size <= histMaxSize) {
size += historyItem->attributes()->attributesSize();

QSet<QByteArray> newAttachments = historyItem->attachments()->values().toSet() - foundAttachements;
Q_FOREACH (const QByteArray& attachment, newAttachments) {
const QSet<QByteArray> newAttachments = historyItem->attachments()->values().toSet() - foundAttachements;
for (const QByteArray& attachment : newAttachments) {
size += attachment.size();
}
foundAttachements += newAttachments;
Expand Down Expand Up @@ -461,7 +461,7 @@ Entry* Entry::clone(CloneFlags flags) const
entry->m_attachments->copyDataFrom(m_attachments);
entry->m_autoTypeAssociations->copyDataFrom(this->m_autoTypeAssociations);
if (flags & CloneIncludeHistory) {
Q_FOREACH (Entry* historyItem, m_history) {
for (Entry* historyItem : m_history) {
Entry* historyItemClone = historyItem->clone(flags & ~CloneIncludeHistory & ~CloneNewUuid);
historyItemClone->setUpdateTimeinfo(false);
historyItemClone->setUuid(entry->uuid());
Expand Down
14 changes: 9 additions & 5 deletions src/core/EntryAttributes.cpp
Expand Up @@ -44,7 +44,8 @@ bool EntryAttributes::hasKey(const QString& key) const
QList<QString> EntryAttributes::customKeys()
{
QList<QString> customKeys;
Q_FOREACH (const QString& key, keys()) {
const QList<QString> keyList = keys();
for (const QString& key : keyList) {
if (!isDefaultAttribute(key)) {
customKeys.append(key);
}
Expand Down Expand Up @@ -162,14 +163,16 @@ void EntryAttributes::copyCustomKeysFrom(const EntryAttributes* other)
Q_EMIT aboutToBeReset();

// remove all non-default keys
Q_FOREACH (const QString& key, keys()) {
const QList<QString> keyList = keys();
for (const QString& key : keyList) {
if (!isDefaultAttribute(key)) {
m_attributes.remove(key);
m_protectedAttributes.remove(key);
}
}

Q_FOREACH (const QString& key, other->keys()) {
const QList<QString> otherKeyList = other->keys();
for (const QString& key : otherKeyList) {
if (!isDefaultAttribute(key)) {
m_attributes.insert(key, other->value(key));
if (other->isProtected(key)) {
Expand All @@ -189,7 +192,8 @@ bool EntryAttributes::areCustomKeysDifferent(const EntryAttributes* other)
return true;
}

Q_FOREACH (const QString& key, keys()) {
const QList<QString> keyList = keys();
for (const QString& key : keyList) {
if (isDefaultAttribute(key)) {
continue;
}
Expand Down Expand Up @@ -234,7 +238,7 @@ void EntryAttributes::clear()
m_attributes.clear();
m_protectedAttributes.clear();

Q_FOREACH (const QString& key, DefaultAttributes) {
for (const QString& key : DefaultAttributes) {
m_attributes.insert(key, "");
}

Expand Down
13 changes: 8 additions & 5 deletions src/core/EntrySearcher.cpp
Expand Up @@ -34,10 +34,13 @@ QList<Entry*> EntrySearcher::searchEntries(const QString& searchTerm, const Grou
{
QList<Entry*> searchResult;

Q_FOREACH (Entry* entry, group->entries()) {
searchResult.append(matchEntry(searchTerm, entry, caseSensitivity));
const QList<Entry*> entryList = group->entries();
for (Entry* entry : entryList) {
searchResult.append(matchEntry(searchTerm, entry, caseSensitivity));
}
Q_FOREACH (Group* childGroup, group->children()) {

const QList<Group*> children = group->children();
for (Group* childGroup : children) {
if (childGroup->searchingEnabled() != Group::Disable) {
searchResult.append(searchEntries(searchTerm, childGroup, caseSensitivity));
}
Expand All @@ -49,8 +52,8 @@ QList<Entry*> EntrySearcher::searchEntries(const QString& searchTerm, const Grou
QList<Entry*> EntrySearcher::matchEntry(const QString& searchTerm, Entry* entry,
Qt::CaseSensitivity caseSensitivity)
{
QStringList wordList = searchTerm.split(QRegExp("\\s"), QString::SkipEmptyParts);
Q_FOREACH (const QString& word, wordList) {
const QStringList wordList = searchTerm.split(QRegExp("\\s"), QString::SkipEmptyParts);
for (const QString& word : wordList) {
if (!wordMatch(word, entry, caseSensitivity)) {
return QList<Entry*>();
}
Expand Down
20 changes: 10 additions & 10 deletions src/core/FilePath.cpp
Expand Up @@ -22,6 +22,7 @@
#include <QLibrary>

#include "config-keepassx.h"
#include "core/Global.h"

FilePath* FilePath::m_instance(nullptr);

Expand All @@ -40,7 +41,8 @@ QString FilePath::pluginPath(const QString& name)
QStringList pluginPaths;

QDir buildDir(QCoreApplication::applicationDirPath() + "/autotype");
Q_FOREACH (const QString& dir, buildDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot)) {
const QStringList buildDirEntryList = buildDir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
for (const QString& dir : buildDirEntryList) {
pluginPaths << QCoreApplication::applicationDirPath() + "/autotype/" + dir;
}

Expand Down Expand Up @@ -68,10 +70,10 @@ QString FilePath::pluginPath(const QString& name)
QStringList dirFilter;
dirFilter << QString("*%1*").arg(name);

Q_FOREACH (const QString& path, pluginPaths) {
QStringList fileCandidates = QDir(path).entryList(dirFilter, QDir::Files);
for (const QString& path : asConst(pluginPaths)) {
const QStringList fileCandidates = QDir(path).entryList(dirFilter, QDir::Files);

Q_FOREACH (const QString& file, fileCandidates) {
for (const QString& file : fileCandidates) {
QString filePath = path + "/" + file;

if (QLibrary::isLibrary(filePath)) {
Expand Down Expand Up @@ -103,10 +105,9 @@ QIcon FilePath::icon(const QString& category, const QString& name, bool fromThem
}

if (icon.isNull()) {
QList<int> pngSizes;
pngSizes << 16 << 22 << 24 << 32 << 48 << 64 << 128;
const QList<int> pngSizes = { 16, 22, 24, 32, 48, 64, 128 };
QString filename;
Q_FOREACH (int size, pngSizes) {
for (int size : pngSizes) {
filename = QString("%1/icons/application/%2x%2/%3.png").arg(m_dataPath, QString::number(size),
combinedName);
if (QFile::exists(filename)) {
Expand Down Expand Up @@ -148,10 +149,9 @@ QIcon FilePath::onOffIcon(const QString& category, const QString& name)
stateName = "on";
}

QList<int> pngSizes;
pngSizes << 16 << 22 << 24 << 32 << 48 << 64 << 128;
const QList<int> pngSizes = { 16, 22, 24, 32, 48, 64, 128 };
QString filename;
Q_FOREACH (int size, pngSizes) {
for (int size : pngSizes) {
filename = QString("%1/icons/application/%2x%2/%3-%4.png").arg(m_dataPath, QString::number(size),
combinedName, stateName);
if (QFile::exists(filename)) {
Expand Down
9 changes: 9 additions & 0 deletions src/core/Global.h
Expand Up @@ -36,4 +36,13 @@
#define QUINT32_MAX 4294967295U
#endif

template <typename T> struct AddConst { typedef const T Type; };

// this adds const to non-const objects (like std::as_const)
template <typename T>
constexpr typename AddConst<T>::Type& asConst(T &t) noexcept { return t; }
// prevent rvalue arguments:
template <typename T>
void asConst(const T&&) = delete;

#endif // KEEPASSX_GLOBAL_H

0 comments on commit b654fde

Please sign in to comment.