convert inAutoType from boolean block to QMutex

This commit is contained in:
thez3ro 2018-01-29 19:44:32 +01:00
parent a76c92ed9a
commit b4cf98998e
No known key found for this signature in database
GPG Key ID: F628F9E41DD7C073
3 changed files with 20 additions and 21 deletions

View File

@ -41,7 +41,6 @@ AutoType* AutoType::m_instance = nullptr;
AutoType::AutoType(QObject* parent, bool test) AutoType::AutoType(QObject* parent, bool test)
: QObject(parent) : QObject(parent)
, m_inAutoType(false)
, m_autoTypeDelay(0) , m_autoTypeDelay(0)
, m_currentGlobalKey(static_cast<Qt::Key>(0)) , m_currentGlobalKey(static_cast<Qt::Key>(0))
, m_currentGlobalModifiers(0) , m_currentGlobalModifiers(0)
@ -143,9 +142,7 @@ QStringList AutoType::windowTitles()
void AutoType::resetInAutoType() void AutoType::resetInAutoType()
{ {
Q_ASSERT(m_inAutoType); m_inAutoType.unlock();
m_inAutoType = false;
} }
void AutoType::raiseWindow() void AutoType::raiseWindow()
@ -200,11 +197,6 @@ int AutoType::callEventFilter(void* event)
*/ */
void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, const QString& sequence, WId window) void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, const QString& sequence, WId window)
{ {
Q_ASSERT(m_inAutoType);
if (!m_inAutoType) {
return;
}
// no edit to the sequence beyond this point // no edit to the sequence beyond this point
if (!verifyAutoTypeSyntax(sequence)) { if (!verifyAutoTypeSyntax(sequence)) {
return; return;
@ -250,7 +242,7 @@ void AutoType::executeAutoTypeActions(const Entry* entry, QWidget* hideWindow, c
*/ */
void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow) void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow)
{ {
if (m_inAutoType || !m_plugin) { if (!m_plugin) {
return; return;
} }
@ -259,11 +251,13 @@ void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow)
return; return;
} }
m_inAutoType = true; if (!m_inAutoType.tryLock()) {
return;
}
executeAutoTypeActions(entry, hideWindow, sequences.first()); executeAutoTypeActions(entry, hideWindow, sequences.first());
m_inAutoType = false; m_inAutoType.unlock();
} }
/** /**
@ -272,7 +266,7 @@ void AutoType::performAutoType(const Entry* entry, QWidget* hideWindow)
*/ */
void AutoType::performGlobalAutoType(const QList<Database*>& dbList) void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
{ {
if (m_inAutoType || !m_plugin) { if (!m_plugin) {
return; return;
} }
@ -282,7 +276,9 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
return; return;
} }
m_inAutoType = true; if (!m_inAutoType.tryLock()) {
return;
}
QList<AutoTypeMatch> matchList; QList<AutoTypeMatch> matchList;
@ -290,7 +286,7 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
const QList<Entry*> dbEntries = db->rootGroup()->entriesRecursive(); const QList<Entry*> dbEntries = db->rootGroup()->entriesRecursive();
for (Entry* entry : dbEntries) { for (Entry* entry : dbEntries) {
const QList<QString> sequences = autoTypeSequences(entry, windowTitle); const QList<QString> sequences = autoTypeSequences(entry, windowTitle);
for (QString sequence : sequences) { for (const QString& sequence : sequences) {
if (!sequence.isEmpty()) { if (!sequence.isEmpty()) {
matchList << AutoTypeMatch(entry,sequence); matchList << AutoTypeMatch(entry,sequence);
} }
@ -299,14 +295,14 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
} }
if (matchList.isEmpty()) { if (matchList.isEmpty()) {
m_inAutoType = false; m_inAutoType.unlock();
QString message = tr("Couldn't find an entry that matches the window title:"); QString message = tr("Couldn't find an entry that matches the window title:");
message.append("\n\n"); message.append("\n\n");
message.append(windowTitle); message.append(windowTitle);
MessageBox::information(nullptr, tr("Auto-Type - KeePassXC"), message); MessageBox::information(nullptr, tr("Auto-Type - KeePassXC"), message);
} else if ((matchList.size() == 1) && !config()->get("security/autotypeask").toBool()) { } else if ((matchList.size() == 1) && !config()->get("security/autotypeask").toBool()) {
executeAutoTypeActions(matchList.first().entry, nullptr, matchList.first().sequence); executeAutoTypeActions(matchList.first().entry, nullptr, matchList.first().sequence);
m_inAutoType = false; m_inAutoType.unlock();
} else { } else {
m_windowFromGlobal = m_plugin->activeWindow(); m_windowFromGlobal = m_plugin->activeWindow();
AutoTypeSelectDialog* selectDialog = new AutoTypeSelectDialog(); AutoTypeSelectDialog* selectDialog = new AutoTypeSelectDialog();
@ -326,13 +322,14 @@ void AutoType::performGlobalAutoType(const QList<Database*>& dbList)
void AutoType::performAutoTypeFromGlobal(AutoTypeMatch match) void AutoType::performAutoTypeFromGlobal(AutoTypeMatch match)
{ {
Q_ASSERT(m_inAutoType); // We don't care about the result here, the mutex should already be locked. Now it's locked for sure
m_inAutoType.tryLock();
m_plugin->raiseWindow(m_windowFromGlobal); m_plugin->raiseWindow(m_windowFromGlobal);
executeAutoTypeActions(match.entry, nullptr, match.sequence, m_windowFromGlobal); executeAutoTypeActions(match.entry, nullptr, match.sequence, m_windowFromGlobal);
m_inAutoType = false; m_inAutoType.unlock();
} }
/** /**

View File

@ -1,4 +1,4 @@
/* /*
* Copyright (C) 2012 Felix Geyer <debfx@fobos.de> * Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
* Copyright (C) 2017 KeePassXC Team <team@keepassxc.org> * Copyright (C) 2017 KeePassXC Team <team@keepassxc.org>
* *
@ -22,6 +22,7 @@
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include <QWidget> #include <QWidget>
#include <QMutex>
#include "core/AutoTypeMatch.h" #include "core/AutoTypeMatch.h"
@ -84,7 +85,7 @@ private:
bool windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl); bool windowMatchesUrl(const QString& windowTitle, const QString& resolvedUrl);
bool windowMatches(const QString& windowTitle, const QString& windowPattern); bool windowMatches(const QString& windowTitle, const QString& windowPattern);
bool m_inAutoType; QMutex m_inAutoType;
int m_autoTypeDelay; int m_autoTypeDelay;
Qt::Key m_currentGlobalKey; Qt::Key m_currentGlobalKey;
Qt::KeyboardModifiers m_currentGlobalModifiers; Qt::KeyboardModifiers m_currentGlobalModifiers;

View File

@ -20,6 +20,7 @@
#include <QChar> #include <QChar>
#include <Qt> #include <Qt>
#include <QObject>
#include "core/Global.h" #include "core/Global.h"