diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,7 @@ ########### kcmutils ############### set(kcmutils_LIB_SRCS kcmoduleinfo.cpp + kcmoduledata.cpp kcmoduleloader.cpp kcmoduleqml.cpp kcmultidialog.cpp @@ -57,6 +58,7 @@ KCModuleProxy KPluginSelector KCModuleContainer + KCModuleData REQUIRED_HEADERS KCMUtils_HEADERS ) diff --git a/src/kcmoduledata.h b/src/kcmoduledata.h new file mode 100644 --- /dev/null +++ b/src/kcmoduledata.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2020 Benjamin Port + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KCMODULEDATA_H +#define KCMODULEDATA_H + +#include +#include +#include + +class KCModuleDataPrivate; + +/** + * @short A base class that offers information about a KCMModule state + * + * @author Benjamin Port + * + * @since 5.70 + */ +class KCMUTILS_EXPORT KCModuleData : public QObject +{ + Q_OBJECT +public: + explicit KCModuleData(QObject *parent = nullptr, const QVariantList &args = QVariantList()); + ~KCModuleData() override; + + virtual bool isDefaults() const; + +protected Q_SLOTS: + /** + * Allow to register manually skeleton class. + * Used by derived class when automatic discovery is not possible. + */ + void registerSkeleton(KCoreConfigSkeleton *skeleton); + + /** + * Automatically register child skeletons + * Call it in your subclass constructor after skeleton creation + */ + void autoRegisterSkeletons(); + +protected: + virtual void virtual_hook(int id, void *data); + +private: + KCModuleDataPrivate *const d; + friend class KCModuleDataPrivate; +}; + +#endif diff --git a/src/kcmoduledata.cpp b/src/kcmoduledata.cpp new file mode 100644 --- /dev/null +++ b/src/kcmoduledata.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2020 Benjamin Port + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "kcmoduledata.h" + +#include +#include +#include +#include + +#include + +class KCModuleDataPrivate +{ +public: + explicit KCModuleDataPrivate(KCModuleData *probe) + : _q(probe) + { + } + + KCModuleData *_q; + QList> _skeletons; +}; + +KCModuleData::KCModuleData(QObject *parent, const QVariantList &) + : QObject(parent), d(new KCModuleDataPrivate(this)) +{ +} + +KCModuleData::~KCModuleData() +{ + delete d; +} + +void KCModuleData::virtual_hook(int, void *) +{ +} + +void KCModuleData::registerSkeleton(KCoreConfigSkeleton *skeleton) +{ + if (!skeleton || d->_skeletons.contains(skeleton)) { + return; + } + + d->_skeletons.append(skeleton); +} + +bool KCModuleData::isDefaults() const +{ + bool defaults = true; + for (const auto &skeleton : qAsConst(d->_skeletons)) { + defaults &= skeleton->isDefaults(); + } + return defaults; +} + +void KCModuleData::autoRegisterSkeletons() +{ + const auto skeletons = findChildren(); + for (auto *skeleton : skeletons) { + registerSkeleton(skeleton); + } +} + +#include "moc_kcmoduledata.cpp" diff --git a/src/kcmoduleloader.h b/src/kcmoduleloader.h --- a/src/kcmoduleloader.h +++ b/src/kcmoduleloader.h @@ -104,6 +104,15 @@ KCMUTILS_EXPORT KCModule *reportError(ErrorReporting report, const QString &text, const QString &details, QWidget *parent); +/** + * For a specified module, return false if configuration is not the default one and true in all other case including if module is not found + * @param module what module to load + * + * @return a boolean representing if module configuration is in default state + * + * @since 5.69 + */ +KCMUTILS_EXPORT bool isDefaults(const KCModuleInfo &module, const QStringList &args = QStringList()); } #endif // KCMODULELOADER_H diff --git a/src/kcmoduleloader.cpp b/src/kcmoduleloader.cpp --- a/src/kcmoduleloader.cpp +++ b/src/kcmoduleloader.cpp @@ -22,6 +22,7 @@ */ #include "kcmoduleloader.h" +#include "kcmoduledata.h" #include "kcmoduleqml_p.h" #include @@ -154,6 +155,32 @@ loader.unload(); } +bool KCModuleLoader::isDefaults(const KCModuleInfo &mod, const QStringList &args) +{ + if (!mod.service() || mod.service()->noDisplay() || mod.library().isEmpty()) { + return true; + } + + QVariantList args2(args.cbegin(), args.cend()); + + KPluginLoader loader(KPluginLoader::findPlugin(QLatin1String("kcms/") + mod.service()->library())); + KPluginFactory* factory = loader.factory(); + if (factory) { + std::unique_ptr probe(factory->create(nullptr, args2)); + if (probe) { + return probe->isDefaults(); + } + } + + std::unique_ptr probe(mod.service()->createInstance(nullptr, args2)); + if (probe) { + return probe->isDefaults(); + } + + return true; +} + + KCModule *KCModuleLoader::reportError(ErrorReporting report, const QString &text, const QString &details, QWidget *parent) {