File indexing completed on 2024-05-12 16:59:20

0001 /*
0002  *   SPDX-FileCopyrightText: 2012-2016 Ivan Cukic <ivan.cukic@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 // Self
0008 #include "Features.h"
0009 
0010 // Qt
0011 #include <QDBusConnection>
0012 
0013 // Utils
0014 #include <utils/d_ptr_implementation.h>
0015 
0016 // Local
0017 #include "common/dbus/common.h"
0018 #include "featuresadaptor.h"
0019 
0020 class Features::Private
0021 {
0022 };
0023 
0024 Features::Features(QObject *parent)
0025     : Module(QStringLiteral("features"), parent)
0026     , d()
0027 {
0028     new FeaturesAdaptor(this);
0029     QDBusConnection::sessionBus().registerObject(KAMD_DBUS_OBJECT_PATH("Features"), this);
0030 }
0031 
0032 Features::~Features()
0033 {
0034 }
0035 
0036 // Features object is just a gateway to the other KAMD modules.
0037 // This is a convenience method to pass the request down to the module
0038 
0039 template<typename RetType, typename Function>
0040 static RetType passToModule(const QString &key, RetType defaultResult, Function f)
0041 {
0042     if (key.isEmpty()) {
0043         return defaultResult;
0044     }
0045 
0046     const auto params = key.split(QLatin1Char('/'));
0047     const auto module = Module::get(params.first());
0048 
0049     if (!module) {
0050         return defaultResult;
0051     }
0052 
0053     return f(static_cast<Module *>(module), params.mid(1));
0054 }
0055 
0056 #define FEATURES_PASS_TO_MODULE(RetType, DefaultResult, What)                                                                                                  \
0057     passToModule(key, DefaultResult, [=](Module *module, const QStringList &params) -> RetType {                                                               \
0058         What                                                                                                                                                   \
0059     });
0060 
0061 bool Features::IsFeatureOperational(const QString &key) const
0062 {
0063     return FEATURES_PASS_TO_MODULE(bool, false, return module->isFeatureOperational(params););
0064 }
0065 
0066 QStringList Features::ListFeatures(const QString &key) const
0067 {
0068     if (key.isEmpty()) {
0069         return Module::get().keys();
0070     }
0071 
0072     return FEATURES_PASS_TO_MODULE(QStringList, QStringList(), return module->listFeatures(params););
0073 }
0074 
0075 QDBusVariant Features::GetValue(const QString &key) const
0076 {
0077     return FEATURES_PASS_TO_MODULE(QDBusVariant, QDBusVariant(), return module->featureValue(params););
0078 }
0079 
0080 void Features::SetValue(const QString &key, const QDBusVariant &value)
0081 {
0082     FEATURES_PASS_TO_MODULE(bool, true, module->setFeatureValue(params, value); return true;);
0083 }
0084 
0085 #undef FEATURES_PASS_TO_MODULE