File indexing completed on 2024-04-21 16:19:52

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2020-2021 Harald Sitter <sitter@kde.org>
0003 
0004 #include <KDEDModule>
0005 #include <KPluginFactory>
0006 #include <QCoreApplication>
0007 
0008 #include "dbusobjectmanagerserver.h"
0009 #include "device.h"
0010 #include "smartctl.h"
0011 #include "smartmonitor.h"
0012 #include "smartnotifier.h"
0013 #include "soliddevicenotifier.h"
0014 
0015 #ifdef WITH_SIMULATION
0016 #include "simulationctl.h"
0017 #include "simulationdevicenotifier.h"
0018 #endif
0019 
0020 #ifdef WITH_SIMULATION
0021 static bool isSimulation()
0022 {
0023     return qEnvironmentVariableIntValue("PLASMA_DISKS_SIMULATION") == 1;
0024 }
0025 #endif
0026 
0027 template<class... Args>
0028 static std::unique_ptr<AbstractSMARTCtl> make_unique_smartctl(Args &&...args)
0029 {
0030 #ifdef WITH_SIMULATION
0031     if (isSimulation()) {
0032         return std::make_unique<SimulationCtl>(std::forward<Args>(args)...);
0033     }
0034 #endif
0035     return std::make_unique<SMARTCtl>(std::forward<Args>(args)...);
0036 }
0037 
0038 template<class... Args>
0039 static std::unique_ptr<DeviceNotifier> make_unique_devicenotifier(Args &&...args)
0040 {
0041 #ifdef WITH_SIMULATION
0042     if (isSimulation()) {
0043         return std::make_unique<SimulationDeviceNotifier>(std::forward<Args>(args)...);
0044     }
0045 #endif
0046     return std::make_unique<SolidDeviceNotifier>(std::forward<Args>(args)...);
0047 }
0048 
0049 class SMARTModule : public KDEDModule
0050 {
0051     Q_OBJECT
0052 public:
0053     explicit SMARTModule(QObject *parent, const QVariantList &args)
0054         : KDEDModule(parent)
0055     {
0056 #ifdef WITH_SIMULATION
0057         Q_INIT_RESOURCE(simulation);
0058 #endif
0059         Q_UNUSED(args);
0060         connect(&m_monitor, &SMARTMonitor::deviceAdded, this, [this](Device *device) {
0061             dbusDeviceServer.serve(device);
0062         });
0063         connect(&m_monitor, &SMARTMonitor::deviceRemoved, &dbusDeviceServer, [this](Device *device) {
0064             dbusDeviceServer.unserve(device);
0065         });
0066         m_monitor.start();
0067     }
0068 
0069 private:
0070     SMARTMonitor m_monitor{make_unique_smartctl(), make_unique_devicenotifier()};
0071     SMARTNotifier m_notifier{&m_monitor};
0072     KDBusObjectManagerServer dbusDeviceServer;
0073 };
0074 
0075 K_PLUGIN_FACTORY_WITH_JSON(SMARTModuleFactory, "smart.json", registerPlugin<SMARTModule>();)
0076 
0077 #include "main.moc"