ksysguarddaemon.cpp 7.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
    Copyright (c) 2019 David Edmundson <davidedmundson@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "ksysguarddaemon.h"

#include <QDBusArgument>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusMetaType>
#include <QDBusServiceWatcher>

#include <QTimer>

#include "SensorPlugin.h"
#include "SensorObject.h"
#include "SensorContainer.h"
#include "SensorProperty.h"

#include <KPluginLoader>
#include <KPluginMetaData>
#include <KPluginFactory>

#include "ksysguard_ifaceadaptor.h"
#include "client.h"

KSysGuardDaemon::KSysGuardDaemon()
    : m_serviceWatcher(new QDBusServiceWatcher(this))
{
    qDBusRegisterMetaType<SensorData>();
    qDBusRegisterMetaType<SensorInfo>();
    qRegisterMetaType<SensorDataList>("SDL");
    qDBusRegisterMetaType<SensorDataList>();
    qDBusRegisterMetaType<SensorInfoMap>();
    qDBusRegisterMetaType<QStringList>();

    new KSysGuardDaemonAdaptor(this);

    m_serviceWatcher->setConnection(QDBusConnection::sessionBus());
    connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &KSysGuardDaemon::onServiceDisconnected);

    auto timer = new QTimer(this);
    timer->setInterval(2000);
    connect(timer, &QTimer::timeout, this, &KSysGuardDaemon::sendFrame);
    timer->start();
}

63 64 65 66 67 68 69
KSysGuardDaemon::~KSysGuardDaemon()
{
    for (Client* c : m_clients) {
        delete c;
    }
}

70 71 72 73
void KSysGuardDaemon::init()
{
    loadProviders();
    QDBusConnection::sessionBus().registerObject("/", this, QDBusConnection::ExportAdaptors);
74 75 76 77
    if (!QDBusConnection::sessionBus().registerService("org.kde.ksystemstats")) {
        qCritical() << "Unable to register DBus service org.kde.ksystemstats. Maybe it is already running?";
        exit(1);
    }
78 79 80 81 82
}

void KSysGuardDaemon::loadProviders()
{
    //instantiate all plugins
83 84 85 86 87 88 89 90 91 92 93
    const auto plugins = KPluginLoader::instantiatePlugins(QStringLiteral("ksysguard"), [this](const KPluginMetaData &metaData) {
        auto providerName = metaData.rawData().value("providerName").toString();
        auto itr = std::find_if(m_providers.cbegin(), m_providers.cend(), [providerName](SensorPlugin *plugin) {
            return plugin->providerName() == providerName;
        });
        if (itr != m_providers.cend()) {
            return false;
        }

        return true;
    }, this);
94

95 96
    for (auto object : plugins) {
        auto factory = qobject_cast<KPluginFactory*>(object);
97
        if (!factory) {
98 99
            qWarning() << "Plugin object" << object << "did not provide a proper KPluginFactory";
            continue;
100
        }
101
        auto provider = factory->create<SensorPlugin>(this);
102
        if (!provider) {
103 104
            qWarning() << "Plugin object" << object << "did not provide a proper SensorPlugin";
            continue;
105 106
        }
        registerProvider(provider);
107
    }
108 109 110 111 112 113 114

    if (m_providers.isEmpty()) {
        qWarning() << "No plugins found";
    }
}

void KSysGuardDaemon::registerProvider(SensorPlugin *provider) {
Arjen Hiemstra's avatar
Arjen Hiemstra committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    m_providers.append(provider);
    const auto containers = provider->containers();
    for (auto container : containers) {
        m_containers[container->id()] = container;
        connect(container, &SensorContainer::objectAdded, this, [this](SensorObject *obj) {
            for (auto sensor: obj->sensors()) {
                emit sensorAdded(sensor->path());
            }
        });
        connect(container, &SensorContainer::objectRemoved, this, [this](SensorObject *obj) {
            for (auto sensor: obj->sensors()) {
                emit sensorRemoved(sensor->path());
            }
        });
    }
130 131 132 133 134 135
}

SensorInfoMap KSysGuardDaemon::allSensors() const
{
    SensorInfoMap infoMap;
    for (auto c : qAsConst(m_containers)) {
136 137 138 139
        auto containerInfo = SensorInfo{};
        containerInfo.name = c->name();
        infoMap.insert(c->id(), containerInfo);

140 141
        const auto objects = c->objects();
        for(auto object : objects) {
142 143 144 145
            auto objectInfo = SensorInfo{};
            objectInfo.name = object->name();
            infoMap.insert(object->path(), objectInfo);

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
            const auto sensors = object->sensors();
            for (auto sensor : sensors) {
                infoMap[sensor->path()] = sensor->info();
            }
        }
    }
    return infoMap;
}

SensorInfoMap KSysGuardDaemon::sensors(const QStringList &sensorPaths) const
{
    SensorInfoMap si;
    for (const QString &path : sensorPaths) {
        if (auto sensor = findSensor(path)) {
            si[path] = sensor->info();
        }
    }
    return si;
}

void KSysGuardDaemon::subscribe(const QStringList &sensorIds)
{
    const QString sender = QDBusContext::message().service();
    m_serviceWatcher->addWatchedService(sender);

    Client *client = m_clients.value(sender);
    if (!client) {
        client = new Client(this, sender);
        m_clients[sender] = client;
    }
    client->subscribeSensors(sensorIds);
}

void KSysGuardDaemon::unsubscribe(const QStringList &sensorIds)
{
    const QString sender = QDBusContext::message().service();
    Client *client = m_clients.value(sender);
    if (!client) {
        return;
    }
    client->unsubscribeSensors(sensorIds);
}

SensorDataList KSysGuardDaemon::sensorData(const QStringList &sensorIds)
{
    SensorDataList sensorData;
    for (const QString &sensorId: sensorIds) {
        if (SensorProperty *sensorProperty = findSensor(sensorId)) {
            const QVariant value = sensorProperty->value();
            if (value.isValid()) {
                sensorData << SensorData(sensorId, value);
            }
        }
    }
    return sensorData;
}

SensorProperty *KSysGuardDaemon::findSensor(const QString &path) const
{
    int subsystemIndex = path.indexOf('/');
    int propertyIndex = path.lastIndexOf('/');

    const QString subsystem = path.left(subsystemIndex);
    const QString object = path.mid(subsystemIndex + 1, propertyIndex - (subsystemIndex + 1));
    const QString property = path.mid(propertyIndex + 1);

    auto c = m_containers.value(subsystem);
    if (!c) {
        return nullptr;
    }
    auto o = c->object(object);
    if (!o) {
        return nullptr;
    }
    return o->sensor(property);
}

void KSysGuardDaemon::onServiceDisconnected(const QString &service)
{
    delete m_clients.take(service);
}

void KSysGuardDaemon::sendFrame()
{
    for (auto provider : qAsConst(m_providers)) {
        provider->update();
    }

    for (auto client: qAsConst(m_clients)) {
        client->sendFrame();
    }
}