From 0b0a4464fb3d1145eb612b1ab7edacfa9581c8c9 Mon Sep 17 00:00:00 2001 From: Alexander Lohnau Date: Tue, 14 Sep 2021 07:30:32 +0200 Subject: Allow KConfigXT to use KSharedConfig::openStateConfig Otherwise we force consumers to use the config location for state data, which is what we are trying to avoid. Task: https://phabricator.kde.org/T12549 --- autotests/kconfig_compiler/CMakeLists.txt | 9 +++++ autotests/kconfig_compiler/test_state_config.kcfg | 14 ++++++++ autotests/kconfig_compiler/test_state_config.kcfgc | 6 ++++ .../kconfig_compiler/test_state_config_main.cpp | 41 ++++++++++++++++++++++ src/kconfig_compiler/KConfigCommonStructs.h | 1 + src/kconfig_compiler/KConfigHeaderGenerator.cpp | 7 ++++ src/kconfig_compiler/KConfigSourceGenerator.cpp | 8 +++-- src/kconfig_compiler/KConfigXmlParser.cpp | 1 + src/kconfig_compiler/README.dox | 5 +++ src/kconfig_compiler/kcfg.xsd | 1 + 10 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 autotests/kconfig_compiler/test_state_config.kcfg create mode 100644 autotests/kconfig_compiler/test_state_config.kcfgc create mode 100644 autotests/kconfig_compiler/test_state_config_main.cpp diff --git a/autotests/kconfig_compiler/CMakeLists.txt b/autotests/kconfig_compiler/CMakeLists.txt index 2a4b343b..4641c0e0 100644 --- a/autotests/kconfig_compiler/CMakeLists.txt +++ b/autotests/kconfig_compiler/CMakeLists.txt @@ -172,6 +172,15 @@ target_link_libraries(test13 KF5::ConfigGui) ########### next target ############### +set(test_state_config_SRCS test_state_config_main.cpp) + +gen_kcfg_test_source(test_state_config test_state_config_SRCS GENERATE_MOC) + +ecm_add_test(TEST_NAME test_state_config ${test_state_config_SRCS}) +target_link_libraries(test_state_config KF5::ConfigGui Qt5::Test) + +########### next target ############### + set(test_emptyentries_SRCS test_emptyentries_main.cpp ) gen_kcfg_test_source(test_emptyentries test_emptyentries_SRCS GENERATE_MOC) diff --git a/autotests/kconfig_compiler/test_state_config.kcfg b/autotests/kconfig_compiler/test_state_config.kcfg new file mode 100644 index 00000000..9c9e96c3 --- /dev/null +++ b/autotests/kconfig_compiler/test_state_config.kcfg @@ -0,0 +1,14 @@ + + + + + + + 0 + + + + diff --git a/autotests/kconfig_compiler/test_state_config.kcfgc b/autotests/kconfig_compiler/test_state_config.kcfgc new file mode 100644 index 00000000..35e2de7d --- /dev/null +++ b/autotests/kconfig_compiler/test_state_config.kcfgc @@ -0,0 +1,6 @@ +# Code generation options for kconfig_compiler_kf5 +File=test_state_config.kcfg +ClassName=MyStateConfig +Singleton=false +Mutators=true +ItemAccessors=true diff --git a/autotests/kconfig_compiler/test_state_config_main.cpp b/autotests/kconfig_compiler/test_state_config_main.cpp new file mode 100644 index 00000000..07044bb5 --- /dev/null +++ b/autotests/kconfig_compiler/test_state_config_main.cpp @@ -0,0 +1,41 @@ +/* + SPDX-FileCopyrightText: 2021 Alexander Lohnau + + SPDX-License-Identifier: MIT +*/ + +#include "test_state_config.h" +#include + +class TestStateConfig : public QObject +{ + Q_OBJECT + +private Q_SLOTS: + void testStateConfig() + { + auto stateConfig = KSharedConfig::openStateConfig(QStringLiteral("test_statedatarc")); + + // Clean the group at every start + stateConfig->deleteGroup("General"); + stateConfig->sync(); + + // It should have the default value + QCOMPARE(MyStateConfig().someStateData(), 0); + + // the updated value should be read from the generated config class + stateConfig->group("General").writeEntry("SomeStateData", 1); + QCOMPARE(MyStateConfig().someStateData(), 1); + + // Make sure writing the value works as expected + MyStateConfig cfg; + cfg.setSomeStateData(2); + QVERIFY(cfg.isSaveNeeded()); + cfg.save(); + stateConfig->reparseConfiguration(); + QCOMPARE(stateConfig->group("General").readEntry("SomeStateData", -1), 2); + } +}; +QTEST_MAIN(TestStateConfig) + +#include "test_state_config_main.moc" diff --git a/src/kconfig_compiler/KConfigCommonStructs.h b/src/kconfig_compiler/KConfigCommonStructs.h index 43e981cb..bac10c70 100644 --- a/src/kconfig_compiler/KConfigCommonStructs.h +++ b/src/kconfig_compiler/KConfigCommonStructs.h @@ -114,6 +114,7 @@ public: struct ParseResult { QString cfgFileName; bool cfgFileNameArg = false; + bool cfgStateConfig = false; QList parameters; QList signalList; QStringList includes; diff --git a/src/kconfig_compiler/KConfigHeaderGenerator.cpp b/src/kconfig_compiler/KConfigHeaderGenerator.cpp index 9938599c..ea5a2c75 100644 --- a/src/kconfig_compiler/KConfigHeaderGenerator.cpp +++ b/src/kconfig_compiler/KConfigHeaderGenerator.cpp @@ -324,10 +324,17 @@ void KConfigHeaderGenerator::createConstructor() if (parseResult.cfgFileNameArg) { if (cfg().forceStringFilename) { stream() << " const QString &cfgfilename" << (parseResult.parameters.isEmpty() ? " = QString()" : ", "); + } else if (parseResult.cfgStateConfig) { + stream() << " KSharedConfig::Ptr config" << (parseResult.parameters.isEmpty() ? " = KSharedConfig::openStateConfig()" : ", "); } else { stream() << " KSharedConfig::Ptr config" << (parseResult.parameters.isEmpty() ? " = KSharedConfig::openConfig()" : ", "); } } + if (cfg().forceStringFilename && parseResult.cfgStateConfig) { + std::cerr << "One can not use ForceStringFilename and use the stateConfig attribute, consider " + "removing the ForceStringFilename kcfgc option if you want to use state data" + << std::endl; + } bool first = true; for (const auto ¶meter : std::as_const(parseResult.parameters)) { diff --git a/src/kconfig_compiler/KConfigSourceGenerator.cpp b/src/kconfig_compiler/KConfigSourceGenerator.cpp index a8a05f4b..45299a4f 100644 --- a/src/kconfig_compiler/KConfigSourceGenerator.cpp +++ b/src/kconfig_compiler/KConfigSourceGenerator.cpp @@ -170,7 +170,9 @@ void KConfigSourceGenerator::createSingletonImplementation() stream() << " return;\n"; stream() << " }\n"; stream() << " new " << cfg().className << "("; - if (isString) { + if (parseResult.cfgStateConfig) { + stream() << "KSharedConfig::openStateConfig(" << arg << ")"; + } else if (isString) { stream() << "KSharedConfig::openConfig(" << arg << ")"; } else { stream() << "std::move(" << arg << ")"; @@ -232,7 +234,9 @@ void KConfigSourceGenerator::createConstructorParameterList() void KConfigSourceGenerator::createParentConstructorCall() { stream() << cfg().inherits << "("; - if (!parseResult.cfgFileName.isEmpty()) { + if (parseResult.cfgStateConfig) { + stream() << " KSharedConfig::openStateConfig(QStringLiteral( \"" << parseResult.cfgFileName << "\") "; + } else if (!parseResult.cfgFileName.isEmpty()) { stream() << " QStringLiteral( \"" << parseResult.cfgFileName << "\" "; } if (parseResult.cfgFileNameArg) { diff --git a/src/kconfig_compiler/KConfigXmlParser.cpp b/src/kconfig_compiler/KConfigXmlParser.cpp index 3d054522..fbcd4b92 100644 --- a/src/kconfig_compiler/KConfigXmlParser.cpp +++ b/src/kconfig_compiler/KConfigXmlParser.cpp @@ -522,6 +522,7 @@ void KConfigXmlParser::readGroupTag(const QDomElement &e) void KConfigXmlParser::readKcfgfileTag(const QDomElement &e) { mParseResult.cfgFileName = e.attribute(QStringLiteral("name")); + mParseResult.cfgStateConfig = e.attribute(QStringLiteral("stateConfig")).toLower() == QLatin1String("true"); mParseResult.cfgFileNameArg = e.attribute(QStringLiteral("arg")).toLower() == QLatin1String("true"); for (QDomElement e2 = e.firstChildElement(); !e2.isNull(); e2 = e2.nextSiblingElement()) { if (e2.tagName() == QLatin1String("parameter")) { diff --git a/src/kconfig_compiler/README.dox b/src/kconfig_compiler/README.dox index 671e9c5b..4c22a452 100644 --- a/src/kconfig_compiler/README.dox +++ b/src/kconfig_compiler/README.dox @@ -385,6 +385,11 @@ Example 1: In this case passing "Group2" as the 'groupname' parameter to the generated class will make it use group "Group2" for the entry "Text". +By setting the stateConfig attribute of kcfgfile to "true", KSharedConfig::openStateConfig is used. +This should be used when one stores volatile data, like window sizes or autocompletion texts. +It is recommended to have at least two separate kcfg files for the different kinds of data. +NOTE: This option is ignored when ForceStringFilename is set. + \subsection enums Enums By default, if GlobalEnums is set to false, a separate named enum will be generated diff --git a/src/kconfig_compiler/kcfg.xsd b/src/kconfig_compiler/kcfg.xsd index f9175a1d..6825819b 100644 --- a/src/kconfig_compiler/kcfg.xsd +++ b/src/kconfig_compiler/kcfg.xsd @@ -68,6 +68,7 @@ + -- cgit v1.2.1