From 70127bad4b5ab4a21d663ffd3929daceda9a7a5a Mon Sep 17 00:00:00 2001 From: Marco Date: Mon, 15 May 2017 17:45:17 +0200 Subject: [PATCH] extract syntax checking methods as static methods to the Autotype class --- src/autotype/AutoType.cpp | 40 ++++++++++++++++++ src/autotype/AutoType.h | 4 ++ src/gui/entry/EditEntryWidget.cpp | 69 ++++--------------------------- 3 files changed, 51 insertions(+), 62 deletions(-) diff --git a/src/autotype/AutoType.cpp b/src/autotype/AutoType.cpp index f69431f9..02160738 100644 --- a/src/autotype/AutoType.cpp +++ b/src/autotype/AutoType.cpp @@ -692,3 +692,43 @@ bool AutoType::windowMatchesUrl(const QString& windowTitle, const QString& resol return false; } + +bool AutoType::checkSynatx(const QString &string) +{ + //checks things like {word 23}{F1 23}{~ 23}{% 23}{^}{F12}{(}{) 23}{[}{[}{]}{Delay=23}{+}{-}~+%@fixedstring + QString allowRepetition = "(\\s[0-9]*){0,1}"; + QString normalCommands = "[A-Z]*" + allowRepetition; + QString specialLiterals = "[\\^\\%\\(\\)~\\{\\}\\[\\]\\+-]" + allowRepetition; + QString functionKeys = "(F[1-9]" + allowRepetition + "|F1[0-2])" + allowRepetition; + QString numpad = "NUMPAD[0-9]" + allowRepetition; + QString delay = "DELAY=[0-9]+"; + QString beep = "BEEP\\s[0-9]*\\s[0-9]*"; + QString vkey = "VKEY(-[EN]X){0,1}" + allowRepetition; + + //these arent in parenthesis + QString shortcutKeys = "[\\^\\%~\\+@]"; + QString fixedStrings = "[^\\^\\%~\\+@\\{\\}]*"; + + QRegExp autoTypeSyntax + ("(" + shortcutKeys + "|" + fixedStrings + "|\\{(" + normalCommands + "|" + specialLiterals + "|" + + functionKeys + + "|" + numpad + "|" + delay + "|" + beep + "|" + vkey + ")\\})*"); + autoTypeSyntax.setCaseSensitivity(Qt::CaseInsensitive); + autoTypeSyntax.setPatternSyntax(QRegExp::RegExp); + return autoTypeSyntax.exactMatch(string); +} + +bool AutoType::checkHighDelay(const QString &string) +{ + QRegExp highDelay(".*\\{Delay\\s[0-9]{5,}\\}.*"); //the 3 means 3 digitnumbers are too much + highDelay.setCaseSensitivity(Qt::CaseInsensitive); + highDelay.setPatternSyntax(QRegExp::RegExp); + return highDelay.exactMatch(string); +} + +bool AutoType::checkHighRepetition(const QString &string) +{ + QRegExp highRepetition(".*\\s[0-9]{3,}.*"); + highRepetition.setPatternSyntax(QRegExp::RegExp); + return highRepetition.exactMatch(string); +} diff --git a/src/autotype/AutoType.h b/src/autotype/AutoType.h index e881975a..f38c19f6 100644 --- a/src/autotype/AutoType.h +++ b/src/autotype/AutoType.h @@ -34,6 +34,7 @@ class AutoType : public QObject { Q_OBJECT + public: QStringList windowTitles(); void performAutoType(const Entry* entry, QWidget* hideWindow = nullptr, @@ -41,6 +42,9 @@ public: bool registerGlobalShortcut(Qt::Key key, Qt::KeyboardModifiers modifiers); void unregisterGlobalShortcut(); int callEventFilter(void* event); + static bool checkSynatx(const QString &string); + static bool checkHighRepetition(const QString &string); + static bool checkHighDelay(const QString &string); inline bool isAvailable() { return m_plugin; diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index 45b62e11..cb04e900 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include "core/Config.h" #include "core/Database.h" @@ -765,73 +766,17 @@ void EditEntryWidget::updateEntryData(Entry* entry) const entry->setDefaultAutoTypeSequence(QString()); } else { - //checks things like {word 23}{F1 23}{~ 23}{% 23}{^}{F12}{(}{) 23}{[}{[}{]}{Delay=23}{+}{-}~+%@fixedstring - - QString allowRepetition = "(\\s[0-9]*){0,1}"; - QString normalCommands = "[A-Z]*" + allowRepetition; - QString specialLiterals = "[\\^\\%\\(\\)~\\{\\}\\[\\]\\+-]" + allowRepetition; - QString functionKeys = "(F[1-9]" + allowRepetition + "|F1[0-2])" + allowRepetition; - QString numpad = "NUMPAD[0-9]" + allowRepetition; - QString delay = "DELAY=[0-9]+"; - QString beep = "BEEP\\s[0-9]*\\s[0-9]*"; - QString vkey = "VKEY(-[EN]X){0,1}" + allowRepetition; - - //these arent in parenthesis - QString shortcutKeys = "[\\^\\%~\\+@]"; - QString fixedStrings = "[^\\^\\%~\\+@\\{\\}]*"; - - QRegExp autoTypeSyntax - ("(" + shortcutKeys + "|" + fixedStrings + "|\\{(" + normalCommands + "|" + specialLiterals + "|" - + functionKeys - + "|" + numpad + "|" + delay + "|" + beep + "|" + vkey + ")\\})*"); - autoTypeSyntax.setCaseSensitivity(Qt::CaseInsensitive); - autoTypeSyntax.setPatternSyntax(QRegExp::RegExp); - - QRegExp highDelay(".*\\{Delay\\s[0-9]{5,}\\}.*"); //the 3 means 3 digitnumbers are too much - highDelay.setCaseSensitivity(Qt::CaseInsensitive); - highDelay.setPatternSyntax(QRegExp::RegExp); - - QRegExp highRepetition(".*\\s[0-9]{3,}.*"); - highRepetition.setPatternSyntax(QRegExp::RegExp); - - - //small test @TODO delete - if (autoTypeSyntax.exactMatch(QString( - "{word 23}{F1 23}{~ 23}{% 23}{^}{F12}{(}{) 23}{[}{[}{]}{Delay=23}{+}{-}~+%@fixeds{Beep 23 32}{Vkey-NX 34}"))) { - std::cout << "yes\n"; - } - if (autoTypeSyntax - .exactMatch(QString("word 23}{F1 23}{~ 23}{% 23}{^}{F12}{(}{) 23}{[}{[}{]}{Delay=23}{+}{-}~+%@fixeds"))) { - std::cout << "no1\n"; - } - if (autoTypeSyntax - .exactMatch(QString("{@}{F1 23}{~ 23}{% 23}{^}{F12}{(}{) 23}{[}{[}{]}{Delay=23}{+}{-}~+%@fixeds"))) { - std::cout << "no2\n"; - } - if (highDelay.exactMatch("{asfd}{DELAY 10000}{dasf}")) { - std::cout << "yes1\n"; - } - if (highDelay.exactMatch("{asfd}{DELAY 1000}{dasf}")) { - std::cout << "no3\n"; - } - if (highRepetition.exactMatch("{asfd}{DELAY 100}{dasf}")) { - std::cout << "yes2\n"; - } - if (highRepetition.exactMatch("{asfd}{DELAY 10}{dasf}")) { - std::cout << "no4\n"; - } - - - - - if (!autoTypeSyntax.exactMatch(m_autoTypeUi->sequenceEdit->text())) { + if (!AutoType::checkSynatx(m_autoTypeUi->sequenceEdit->text())) { //@TODO handle wrong syntax + std::cout << "wrong syntax\n"; } - else if (highDelay.exactMatch(m_autoTypeUi->sequenceEdit->text())) { + else if (AutoType::checkHighDelay(m_autoTypeUi->sequenceEdit->text())) { //@TODO handle too long delay + std::cout << "too long delay\n"; } - else if (highRepetition.exactMatch(m_autoTypeUi->sequenceEdit->text())) { + else if (AutoType::checkHighRepetition(m_autoTypeUi->sequenceEdit->text())) { //@TODO handle too much repetition + std::cout << "too much repetition\n"; } entry->setDefaultAutoTypeSequence(m_autoTypeUi->sequenceEdit->text()); }