File indexing completed on 2024-05-12 17:08:30

0001 /*
0002  * SPDX-FileCopyrightText: 2018-2019 Daniel Vrátil <dvratil@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "fakemanager.h"
0008 #include "fakedevice.h"
0009 #include "fakemanageradaptor.h"
0010 
0011 #include <QDBusConnection>
0012 #include <QDBusError>
0013 #include <QJsonArray>
0014 
0015 #include <QDebug>
0016 
0017 #include <chrono>
0018 #include <thread>
0019 
0020 using namespace std::chrono_literals;
0021 
0022 namespace
0023 {
0024 static const QString kManagerDBusPath = QStringLiteral("/org/freedesktop/bolt");
0025 }
0026 
0027 FakeManager::FakeManager(const QJsonObject &json, QObject *parent)
0028     : QObject(parent)
0029     , mProbing(json[QStringLiteral("Probing")].toBool())
0030     , mDefaultPolicy(json[QStringLiteral("DefaultPolicy")].toString())
0031     , mSecurityLevel(json[QStringLiteral("SecurityLevel")].toString())
0032     , mAuthMode(json[QStringLiteral("AuthMode")].toString())
0033 {
0034     new FakeManagerAdaptor(this);
0035     if (!QDBusConnection::sessionBus().registerObject(kManagerDBusPath, this)) {
0036         throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1").arg(QDBusConnection::sessionBus().lastError().message()));
0037     }
0038 
0039     const auto jsonDevices = json[QStringLiteral("Devices")].toArray();
0040     for (const auto &jsonDevice : jsonDevices) {
0041         auto device = std::make_unique<FakeDevice>(jsonDevice.toObject(), this);
0042         mDevices.emplace(device->uid(), std::move(device));
0043     }
0044 }
0045 
0046 FakeManager::FakeManager(QObject *parent)
0047     : QObject(parent)
0048 {
0049     new FakeManagerAdaptor(this);
0050     if (!QDBusConnection::sessionBus().registerObject(kManagerDBusPath, this)) {
0051         throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1").arg(QDBusConnection::sessionBus().lastError().message()));
0052     }
0053 }
0054 
0055 FakeManager::~FakeManager()
0056 {
0057     QDBusConnection::sessionBus().unregisterObject(kManagerDBusPath);
0058 }
0059 
0060 FakeDevice *FakeManager::addDevice(std::unique_ptr<FakeDevice> device)
0061 {
0062     const auto it = mDevices.emplace(device->uid(), std::move(device)).first;
0063     Q_EMIT DeviceAdded(it->second->dbusPath());
0064     return it->second.get();
0065 }
0066 
0067 void FakeManager::removeDevice(const QString &uid)
0068 {
0069     auto deviceIt = mDevices.find(uid);
0070     if (deviceIt == mDevices.end()) {
0071         return;
0072     }
0073     auto device = std::move(deviceIt->second);
0074     mDevices.erase(deviceIt);
0075     Q_EMIT DeviceRemoved(device->dbusPath());
0076 }
0077 
0078 QList<FakeDevice *> FakeManager::devices() const
0079 {
0080     QList<FakeDevice *> rv;
0081     rv.reserve(mDevices.size());
0082     std::transform(mDevices.cbegin(), mDevices.cend(), std::back_inserter(rv), [](const auto &v) {
0083         return v.second.get();
0084     });
0085     return rv;
0086 }
0087 
0088 unsigned int FakeManager::version() const
0089 {
0090     return 1;
0091 }
0092 
0093 bool FakeManager::isProbing() const
0094 {
0095     return mProbing;
0096 }
0097 
0098 QString FakeManager::defaultPolicy() const
0099 {
0100     return mDefaultPolicy;
0101 }
0102 
0103 QString FakeManager::securityLevel() const
0104 {
0105     return mSecurityLevel;
0106 }
0107 
0108 QString FakeManager::authMode() const
0109 {
0110     return mAuthMode;
0111 }
0112 
0113 void FakeManager::setAuthMode(const QString &authMode)
0114 {
0115     qDebug("Manager: authMode changed to %s", qUtf8Printable(authMode));
0116     mAuthMode = authMode;
0117     Q_EMIT authModeChanged(authMode);
0118 }
0119 
0120 QList<QDBusObjectPath> FakeManager::ListDevices() const
0121 {
0122     QList<QDBusObjectPath> rv;
0123     rv.reserve(mDevices.size());
0124     for (const auto &device : mDevices) {
0125         rv.push_back(device.second->dbusPath());
0126     }
0127     return rv;
0128 }
0129 
0130 QDBusObjectPath FakeManager::DeviceByUid(const QString &uid) const
0131 {
0132     auto device = mDevices.find(uid);
0133     if (device == mDevices.cend()) {
0134         return QDBusObjectPath();
0135     } else {
0136         return device->second->dbusPath();
0137     }
0138 }
0139 
0140 QDBusObjectPath FakeManager::EnrollDevice(const QString &uid, const QString &policy, const QString &flags)
0141 {
0142     std::this_thread::sleep_for(1s); // simulate this operation taking time
0143 
0144     auto deviceIt = mDevices.find(uid);
0145     if (deviceIt == mDevices.end()) {
0146         return QDBusObjectPath();
0147     }
0148     auto &device = deviceIt->second;
0149     if (policy == QLatin1String("default")) {
0150         device->setPolicy(defaultPolicy());
0151     } else {
0152         device->setPolicy(policy);
0153     }
0154     device->setAuthFlags(flags);
0155     device->setStored(true);
0156     device->setStatus(QLatin1String("authorized"));
0157 
0158     return device->dbusPath();
0159 }
0160 
0161 void FakeManager::ForgetDevice(const QString &uid)
0162 {
0163     std::this_thread::sleep_for(1s); // simulate this operation taking time
0164 
0165     auto deviceIt = mDevices.find(uid);
0166     if (deviceIt == mDevices.end()) {
0167         return;
0168     }
0169     auto &device = deviceIt->second;
0170     device->setStored(false);
0171     device->setStatus(QLatin1String("connected"));
0172 }