From 8edb1d499d4a0fce14035e04e7081ebd90d1e160 Mon Sep 17 00:00:00 2001 From: CompileNix Date: Sun, 8 Mar 2020 18:50:39 +0100 Subject: [PATCH 1/4] Add DateTime placeholders to AutoType feature --- src/autotype/AutoType.cpp | 2 +- src/core/Entry.cpp | 91 ++++++++++++++++++++++++++++++++++++++- src/core/Entry.h | 17 +++++++- 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/autotype/AutoType.cpp b/src/autotype/AutoType.cpp index fa7537373e..f5c8a73b2b 100644 --- a/src/autotype/AutoType.cpp +++ b/src/autotype/AutoType.cpp @@ -684,7 +684,7 @@ bool AutoType::checkSyntax(const QString& string) QString allowRepetition = "(?:\\s\\d+)?"; // the ":" allows custom commands with syntax S:Field // exclude BEEP otherwise will be checked as valid - QString normalCommands = "(?!BEEP\\s)[A-Z:]*" + allowRepetition; + QString normalCommands = "(?!BEEP\\s)[A-Z:_]*" + allowRepetition; QString specialLiterals = "[\\^\\%\\(\\)~\\{\\}\\[\\]\\+]" + allowRepetition; QString functionKeys = "(?:F[1-9]" + allowRepetition + "|F1[0-2])" + allowRepetition; QString numpad = "NUMPAD\\d" + allowRepetition; diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index d53fa64689..e2106d981f 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -30,6 +30,7 @@ #include #include #include +#include const int Entry::DefaultIconNumber = 0; const int Entry::ResolveMaximumDepth = 10; @@ -916,11 +917,84 @@ QString Entry::resolvePlaceholderRecursive(const QString& placeholder, int maxDe } case PlaceholderType::Reference: return resolveReferencePlaceholderRecursive(placeholder, maxDepth); + case PlaceholderType::DateTimeSimple: + case PlaceholderType::DateTimeYear: + case PlaceholderType::DateTimeMonth: + case PlaceholderType::DateTimeDay: + case PlaceholderType::DateTimeHour: + case PlaceholderType::DateTimeMinute: + case PlaceholderType::DateTimeSecond: + case PlaceholderType::DateTimeUtcSimple: + case PlaceholderType::DateTimeUtcYear: + case PlaceholderType::DateTimeUtcMonth: + case PlaceholderType::DateTimeUtcDay: + case PlaceholderType::DateTimeUtcHour: + case PlaceholderType::DateTimeUtcMinute: + case PlaceholderType::DateTimeUtcSecond: + return resolveMultiplePlaceholdersRecursive(resolveDateTimePlaceholder(typeOfPlaceholder), maxDepth - 1); } return placeholder; } +QString Entry::resolveDateTimePlaceholder(Entry::PlaceholderType placeholderType) const +{ + time_t time = std::time(NULL); + char date_simple_formatted[50] {}; + char date_part_formatted[50] {}; + + switch (placeholderType) { + case PlaceholderType::DateTimeSimple: + std::strftime(date_simple_formatted, sizeof(date_simple_formatted), "%Y%m%d%H%M", localtime(&time)); + break; + case PlaceholderType::DateTimeYear: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%Y", localtime(&time)); + break; + case PlaceholderType::DateTimeMonth: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%m", localtime(&time)); + break; + case PlaceholderType::DateTimeDay: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%d", localtime(&time)); + break; + case PlaceholderType::DateTimeHour: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%H", localtime(&time)); + break; + case PlaceholderType::DateTimeMinute: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%M", localtime(&time)); + break; + case PlaceholderType::DateTimeSecond: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%S", localtime(&time)); + break; + case PlaceholderType::DateTimeUtcSimple: + std::strftime(date_simple_formatted, sizeof(date_simple_formatted), "%Y%m%d%H%M", gmtime(&time)); + break; + case PlaceholderType::DateTimeUtcYear: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%Y", gmtime(&time)); + break; + case PlaceholderType::DateTimeUtcMonth: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%m", gmtime(&time)); + break; + case PlaceholderType::DateTimeUtcDay: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%d", gmtime(&time)); + break; + case PlaceholderType::DateTimeUtcHour: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%H", gmtime(&time)); + break; + case PlaceholderType::DateTimeUtcMinute: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%M", gmtime(&time)); + break; + case PlaceholderType::DateTimeUtcSecond: + std::strftime(date_part_formatted, sizeof(date_part_formatted), "%S", gmtime(&time)); + break; + default: { + Q_ASSERT_X(false, "Entry::resolveDateTimePlaceholder", "Bad DateTime placeholder type"); + break; + } + } + + return date_simple_formatted[0] == 0x0 ? QString(date_part_formatted) : QString(date_simple_formatted); +} + QString Entry::resolveReferencePlaceholderRecursive(const QString& placeholder, int maxDepth) const { if (maxDepth <= 0) { @@ -1141,7 +1215,22 @@ Entry::PlaceholderType Entry::placeholderType(const QString& placeholder) const {QStringLiteral("{URL:FRAGMENT}"), PlaceholderType::UrlFragment}, {QStringLiteral("{URL:USERINFO}"), PlaceholderType::UrlUserInfo}, {QStringLiteral("{URL:USERNAME}"), PlaceholderType::UrlUserName}, - {QStringLiteral("{URL:PASSWORD}"), PlaceholderType::UrlPassword}}; + {QStringLiteral("{URL:PASSWORD}"), PlaceholderType::UrlPassword}, + {QStringLiteral("{DT_SIMPLE}"), PlaceholderType::DateTimeSimple}, + {QStringLiteral("{DT_YEAR}"), PlaceholderType::DateTimeYear}, + {QStringLiteral("{DT_MONTH}"), PlaceholderType::DateTimeMonth}, + {QStringLiteral("{DT_DAY}"), PlaceholderType::DateTimeDay}, + {QStringLiteral("{DT_HOUR}"), PlaceholderType::DateTimeHour}, + {QStringLiteral("{DT_MINUTE}"), PlaceholderType::DateTimeMinute}, + {QStringLiteral("{DT_SECOND}"), PlaceholderType::DateTimeSecond}, + {QStringLiteral("{DT_UTC_SIMPLE}"), PlaceholderType::DateTimeUtcSimple}, + {QStringLiteral("{DT_UTC_YEAR}"), PlaceholderType::DateTimeUtcYear}, + {QStringLiteral("{DT_UTC_MONTH}"), PlaceholderType::DateTimeUtcMonth}, + {QStringLiteral("{DT_UTC_DAY}"), PlaceholderType::DateTimeUtcDay}, + {QStringLiteral("{DT_UTC_HOUR}"), PlaceholderType::DateTimeUtcHour}, + {QStringLiteral("{DT_UTC_MINUTE}"), PlaceholderType::DateTimeUtcMinute}, + {QStringLiteral("{DT_UTC_SECOND}"), PlaceholderType::DateTimeUtcSecond} + }; return placeholders.value(placeholder.toUpper(), PlaceholderType::Unknown); } diff --git a/src/core/Entry.h b/src/core/Entry.h index 8b52b51092..326728d112 100644 --- a/src/core/Entry.h +++ b/src/core/Entry.h @@ -190,7 +190,21 @@ class Entry : public QObject UrlUserName, UrlPassword, Reference, - CustomAttribute + CustomAttribute, + DateTimeSimple, + DateTimeYear, + DateTimeMonth, + DateTimeDay, + DateTimeHour, + DateTimeMinute, + DateTimeSecond, + DateTimeUtcSimple, + DateTimeUtcYear, + DateTimeUtcMonth, + DateTimeUtcDay, + DateTimeUtcHour, + DateTimeUtcMinute, + DateTimeUtcSecond }; /** @@ -206,6 +220,7 @@ class Entry : public QObject QString resolveMultiplePlaceholders(const QString& str) const; QString resolvePlaceholder(const QString& str) const; QString resolveUrlPlaceholder(const QString& str, PlaceholderType placeholderType) const; + QString resolveDateTimePlaceholder(PlaceholderType placeholderType) const; PlaceholderType placeholderType(const QString& placeholder) const; QString resolveUrl(const QString& url) const; From 79804bba033786a4ec7c11986d67eb9ce6e2da76 Mon Sep 17 00:00:00 2001 From: CompileNix Date: Wed, 11 Mar 2020 21:46:26 +0100 Subject: [PATCH 2/4] merge "date_simple_formatted" and "date_part_formatted" --- src/core/Entry.cpp | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index e2106d981f..2f8ec5a33d 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -940,51 +940,50 @@ QString Entry::resolvePlaceholderRecursive(const QString& placeholder, int maxDe QString Entry::resolveDateTimePlaceholder(Entry::PlaceholderType placeholderType) const { time_t time = std::time(NULL); - char date_simple_formatted[50] {}; - char date_part_formatted[50] {}; + char date_formatted[50]{}; switch (placeholderType) { case PlaceholderType::DateTimeSimple: - std::strftime(date_simple_formatted, sizeof(date_simple_formatted), "%Y%m%d%H%M", localtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%Y%m%d%H%M", localtime(&time)); break; case PlaceholderType::DateTimeYear: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%Y", localtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%Y", localtime(&time)); break; case PlaceholderType::DateTimeMonth: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%m", localtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%m", localtime(&time)); break; case PlaceholderType::DateTimeDay: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%d", localtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%d", localtime(&time)); break; case PlaceholderType::DateTimeHour: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%H", localtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%H", localtime(&time)); break; case PlaceholderType::DateTimeMinute: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%M", localtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%M", localtime(&time)); break; case PlaceholderType::DateTimeSecond: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%S", localtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%S", localtime(&time)); break; case PlaceholderType::DateTimeUtcSimple: - std::strftime(date_simple_formatted, sizeof(date_simple_formatted), "%Y%m%d%H%M", gmtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%Y%m%d%H%M", gmtime(&time)); break; case PlaceholderType::DateTimeUtcYear: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%Y", gmtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%Y", gmtime(&time)); break; case PlaceholderType::DateTimeUtcMonth: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%m", gmtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%m", gmtime(&time)); break; case PlaceholderType::DateTimeUtcDay: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%d", gmtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%d", gmtime(&time)); break; case PlaceholderType::DateTimeUtcHour: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%H", gmtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%H", gmtime(&time)); break; case PlaceholderType::DateTimeUtcMinute: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%M", gmtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%M", gmtime(&time)); break; case PlaceholderType::DateTimeUtcSecond: - std::strftime(date_part_formatted, sizeof(date_part_formatted), "%S", gmtime(&time)); + std::strftime(date_formatted, sizeof(date_formatted), "%S", gmtime(&time)); break; default: { Q_ASSERT_X(false, "Entry::resolveDateTimePlaceholder", "Bad DateTime placeholder type"); @@ -992,7 +991,7 @@ QString Entry::resolveDateTimePlaceholder(Entry::PlaceholderType placeholderType } } - return date_simple_formatted[0] == 0x0 ? QString(date_part_formatted) : QString(date_simple_formatted); + return QString(date_formatted); } QString Entry::resolveReferencePlaceholderRecursive(const QString& placeholder, int maxDepth) const From ad5de5d0403d99f91c72aa5d6dfbfdff2b1b31d5 Mon Sep 17 00:00:00 2001 From: CompileNix Date: Wed, 11 Mar 2020 21:46:39 +0100 Subject: [PATCH 3/4] make format --- src/core/Entry.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 2f8ec5a33d..0cf2c59491 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -29,8 +29,8 @@ #include #include -#include #include +#include const int Entry::DefaultIconNumber = 0; const int Entry::ResolveMaximumDepth = 10; @@ -1228,8 +1228,7 @@ Entry::PlaceholderType Entry::placeholderType(const QString& placeholder) const {QStringLiteral("{DT_UTC_DAY}"), PlaceholderType::DateTimeUtcDay}, {QStringLiteral("{DT_UTC_HOUR}"), PlaceholderType::DateTimeUtcHour}, {QStringLiteral("{DT_UTC_MINUTE}"), PlaceholderType::DateTimeUtcMinute}, - {QStringLiteral("{DT_UTC_SECOND}"), PlaceholderType::DateTimeUtcSecond} - }; + {QStringLiteral("{DT_UTC_SECOND}"), PlaceholderType::DateTimeUtcSecond}}; return placeholders.value(placeholder.toUpper(), PlaceholderType::Unknown); } From 11f28b62f1f37c6e2ad18255396c7ea4ef287847 Mon Sep 17 00:00:00 2001 From: CompileNix Date: Wed, 18 Mar 2020 18:56:05 +0100 Subject: [PATCH 4/4] replace usages of time_t with QDateTime using Clock --- src/core/Entry.cpp | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/src/core/Entry.cpp b/src/core/Entry.cpp index 0cf2c59491..c88e4a2fbf 100644 --- a/src/core/Entry.cpp +++ b/src/core/Entry.cpp @@ -29,7 +29,6 @@ #include #include -#include #include const int Entry::DefaultIconNumber = 0; @@ -939,51 +938,52 @@ QString Entry::resolvePlaceholderRecursive(const QString& placeholder, int maxDe QString Entry::resolveDateTimePlaceholder(Entry::PlaceholderType placeholderType) const { - time_t time = std::time(NULL); - char date_formatted[50]{}; + QDateTime time = Clock::currentDateTime(); + QDateTime time_utc = Clock::currentDateTimeUtc(); + QString date_formatted{}; switch (placeholderType) { case PlaceholderType::DateTimeSimple: - std::strftime(date_formatted, sizeof(date_formatted), "%Y%m%d%H%M", localtime(&time)); + date_formatted = time.toString("yyyyMMddhhmmss"); break; case PlaceholderType::DateTimeYear: - std::strftime(date_formatted, sizeof(date_formatted), "%Y", localtime(&time)); + date_formatted = time.toString("yyyy"); break; case PlaceholderType::DateTimeMonth: - std::strftime(date_formatted, sizeof(date_formatted), "%m", localtime(&time)); + date_formatted = time.toString("MM"); break; case PlaceholderType::DateTimeDay: - std::strftime(date_formatted, sizeof(date_formatted), "%d", localtime(&time)); + date_formatted = time.toString("dd"); break; case PlaceholderType::DateTimeHour: - std::strftime(date_formatted, sizeof(date_formatted), "%H", localtime(&time)); + date_formatted = time.toString("hh"); break; case PlaceholderType::DateTimeMinute: - std::strftime(date_formatted, sizeof(date_formatted), "%M", localtime(&time)); + date_formatted = time.toString("mm"); break; case PlaceholderType::DateTimeSecond: - std::strftime(date_formatted, sizeof(date_formatted), "%S", localtime(&time)); + date_formatted = time.toString("ss"); break; case PlaceholderType::DateTimeUtcSimple: - std::strftime(date_formatted, sizeof(date_formatted), "%Y%m%d%H%M", gmtime(&time)); + date_formatted = time_utc.toString("yyyyMMddhhmmss"); break; case PlaceholderType::DateTimeUtcYear: - std::strftime(date_formatted, sizeof(date_formatted), "%Y", gmtime(&time)); + date_formatted = time_utc.toString("yyyy"); break; case PlaceholderType::DateTimeUtcMonth: - std::strftime(date_formatted, sizeof(date_formatted), "%m", gmtime(&time)); + date_formatted = time_utc.toString("MM"); break; case PlaceholderType::DateTimeUtcDay: - std::strftime(date_formatted, sizeof(date_formatted), "%d", gmtime(&time)); + date_formatted = time_utc.toString("dd"); break; case PlaceholderType::DateTimeUtcHour: - std::strftime(date_formatted, sizeof(date_formatted), "%H", gmtime(&time)); + date_formatted = time_utc.toString("hh"); break; case PlaceholderType::DateTimeUtcMinute: - std::strftime(date_formatted, sizeof(date_formatted), "%M", gmtime(&time)); + date_formatted = time_utc.toString("mm"); break; case PlaceholderType::DateTimeUtcSecond: - std::strftime(date_formatted, sizeof(date_formatted), "%S", gmtime(&time)); + date_formatted = time_utc.toString("ss"); break; default: { Q_ASSERT_X(false, "Entry::resolveDateTimePlaceholder", "Bad DateTime placeholder type"); @@ -991,7 +991,7 @@ QString Entry::resolveDateTimePlaceholder(Entry::PlaceholderType placeholderType } } - return QString(date_formatted); + return date_formatted; } QString Entry::resolveReferencePlaceholderRecursive(const QString& placeholder, int maxDepth) const