diff options
author | Hannah von Reth <hannah.vonreth@owncloud.com> | 2022-06-07 19:11:38 +0300 |
---|---|---|
committer | Hannah von Reth <vonreth@kde.org> | 2022-06-22 16:02:13 +0300 |
commit | 44a72a011342711d7b663fc8a0f9639485d6b591 (patch) | |
tree | 90a4808bb48ef2e8fe07316d9d08a1561e358cab /src | |
parent | 7323cc157110fabafb4152a0a9da542b1785a28c (diff) |
Implement renderTemplate
The function alows to fill template files
Diffstat (limited to 'src')
-rw-r--r-- | src/common/utility.cpp | 23 | ||||
-rw-r--r-- | src/common/utility.h | 5 | ||||
-rw-r--r-- | src/gui/newwizard/setupwizardwindow.cpp | 10 |
3 files changed, 32 insertions, 6 deletions
diff --git a/src/common/utility.cpp b/src/common/utility.cpp index 0b9bb244a..444e31284 100644 --- a/src/common/utility.cpp +++ b/src/common/utility.cpp @@ -32,6 +32,7 @@ #include <QObject> #include <QProcess> #include <QRandomGenerator> +#include <QRegularExpression> #include <QSettings> #include <QStandardPaths> #include <QSysInfo> @@ -606,4 +607,26 @@ QDebug &operator<<(QDebug &debug, nanoseconds in) << ms.count() << "ms)"; } +QString Utility::renderTemplate(QString templ, const QMap<QString, QString> &values) +{ + static const QRegularExpression pattern(QStringLiteral("@{([^{}]+)}")); + const auto replace = [&templ, &values](QRegularExpressionMatchIterator it) { + while (it.hasNext()) { + const auto match = it.next(); + Q_ASSERT(match.lastCapturedIndex() == 1); + Q_ASSERT(values.contains(match.captured(1))); + templ.replace(match.captured(0), values.value(match.captured(1))); + } + }; + + auto matches = pattern.globalMatch(templ); + do { + replace(matches); + // the placeholder can again contain a placeholder + matches = pattern.globalMatch(templ); + } while (matches.hasNext()); + + return templ; +} + } // namespace OCC diff --git a/src/common/utility.h b/src/common/utility.h index ea715f5ef..753b674d6 100644 --- a/src/common/utility.h +++ b/src/common/utility.h @@ -358,6 +358,11 @@ OCSYNC_EXPORT Q_DECLARE_LOGGING_CATEGORY(lcUtility) Q_UNREACHABLE(); } + /** + * Replace all occurances of @{} values in template with the values from values + */ + OCSYNC_EXPORT QString renderTemplate(QString templ, const QMap<QString, QString> &values); + #ifdef Q_OS_LINUX OCSYNC_EXPORT QString appImageLocation(); OCSYNC_EXPORT bool runningInAppImage(); diff --git a/src/gui/newwizard/setupwizardwindow.cpp b/src/gui/newwizard/setupwizardwindow.cpp index 68d4a941c..179665c26 100644 --- a/src/gui/newwizard/setupwizardwindow.cpp +++ b/src/gui/newwizard/setupwizardwindow.cpp @@ -17,12 +17,10 @@ using namespace OCC; QString replaceCssColors(QString stylesheet) { - QString rv = stylesheet; - - rv = stylesheet.replace(QStringLiteral("@WIZARD_BACKGROUND_COLOR@"), Theme::instance()->wizardHeaderBackgroundColor().name()); - rv = stylesheet.replace(QStringLiteral("@WIZARD_FONT_COLOR@"), Theme::instance()->wizardHeaderTitleColor().name()); - - return rv; + return Utility::renderTemplate(stylesheet, { + { QStringLiteral("WIZARD_BACKGROUND_COLOR"), Theme::instance()->wizardHeaderBackgroundColor().name() }, // + { QStringLiteral("WIZARD_FONT_COLOR"), Theme::instance()->wizardHeaderTitleColor().name() } // + }); } } |