1 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
2 // SPDX-FileCopyrightText: 2020-2021 Harald Sitter <sitter@kde.org>
3
4 #include <KDEDModule>
5 #include <KPluginFactory>
6 #include <QCoreApplication>
7
8 #include "dbusobjectmanagerserver.h"
9 #include "device.h"
10 #include "smartctl.h"
11 #include "smartmonitor.h"
12 #include "smartnotifier.h"
13 #include "soliddevicenotifier.h"
14
15 #ifdef WITH_SIMULATION
16 #include "simulationctl.h"
17 #include "simulationdevicenotifier.h"
18 #endif
19
20 #ifdef WITH_SIMULATION
isSimulation()21 static bool isSimulation()
22 {
23 return qEnvironmentVariableIntValue("PLASMA_DISKS_SIMULATION") == 1;
24 }
25 #endif
26
27 template<class... Args>
make_unique_smartctl(Args &&...args)28 static std::unique_ptr<AbstractSMARTCtl> make_unique_smartctl(Args &&...args)
29 {
30 #ifdef WITH_SIMULATION
31 if (isSimulation()) {
32 return std::make_unique<SimulationCtl>(std::forward<Args>(args)...);
33 }
34 #endif
35 return std::make_unique<SMARTCtl>(std::forward<Args>(args)...);
36 }
37
38 template<class... Args>
make_unique_devicenotifier(Args &&...args)39 static std::unique_ptr<DeviceNotifier> make_unique_devicenotifier(Args &&...args)
40 {
41 #ifdef WITH_SIMULATION
42 if (isSimulation()) {
43 return std::make_unique<SimulationDeviceNotifier>(std::forward<Args>(args)...);
44 }
45 #endif
46 return std::make_unique<SolidDeviceNotifier>(std::forward<Args>(args)...);
47 }
48
49 class SMARTModule : public KDEDModule
50 {
51 Q_OBJECT
52 public:
SMARTModule(QObject * parent,const QVariantList & args)53 explicit SMARTModule(QObject *parent, const QVariantList &args)
54 : KDEDModule(parent)
55 {
56 #ifdef WITH_SIMULATION
57 Q_INIT_RESOURCE(simulation);
58 #endif
59 Q_UNUSED(args);
60 connect(&m_monitor, &SMARTMonitor::deviceAdded, this, [this](Device *device) {
61 dbusDeviceServer.serve(device);
62 });
63 connect(&m_monitor, &SMARTMonitor::deviceRemoved, &dbusDeviceServer, [this](Device *device) {
64 dbusDeviceServer.unserve(device);
65 });
66 m_monitor.start();
67 }
68
69 private:
70 SMARTMonitor m_monitor{make_unique_smartctl(), make_unique_devicenotifier()};
71 SMARTNotifier m_notifier{&m_monitor};
72 KDBusObjectManagerServer dbusDeviceServer;
73 };
74
75 K_PLUGIN_FACTORY_WITH_JSON(SMARTModuleFactory, "smart.json", registerPlugin<SMARTModule>();)
76
77 #include "main.moc"
78