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 "fakedevice.h" 0008 #include "fakedeviceadaptor.h" 0009 0010 #include <QDBusConnection> 0011 #include <QDBusError> 0012 0013 #include <chrono> 0014 #include <thread> 0015 0016 using namespace std::chrono_literals; 0017 0018 FakeDevice::FakeDevice(const QString &uid, QObject *parent) 0019 : QObject(parent) 0020 , mUid(uid) 0021 { 0022 new FakeDeviceAdaptor(this); 0023 auto bus = QDBusConnection::sessionBus(); 0024 if (!bus.registerObject(dbusPath().path(), this)) { 0025 throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2").arg(mUid, bus.lastError().message())); 0026 } 0027 } 0028 0029 FakeDevice::FakeDevice(const QJsonObject &json, QObject *parent) 0030 : QObject(parent) 0031 , mUid(json[QStringLiteral("Uid")].toString()) 0032 , mName(json[QStringLiteral("Name")].toString()) 0033 , mVendor(json[QStringLiteral("Vendor")].toString()) 0034 , mType(json[QStringLiteral("Type")].toString()) 0035 , mStatus(json[QStringLiteral("Status")].toString()) 0036 , mAuthFlags(json[QStringLiteral("AuthFlags")].toString()) 0037 , mParent(json[QStringLiteral("Parent")].toString()) 0038 , mSysfsPath(json[QStringLiteral("SysfsPath")].toString()) 0039 , mPolicy(json[QStringLiteral("Policy")].toString()) 0040 , mKey(json[QStringLiteral("Key")].toString()) 0041 , mLabel(json[QStringLiteral("Label")].toString()) 0042 , mConnectTime(json[QStringLiteral("ConnectTime")].toVariant().value<quint64>()) 0043 , mAuthorizeTime(json[QStringLiteral("AuthorizeTime")].toVariant().value<quint64>()) 0044 , mStoreTime(json[QStringLiteral("StoreTime")].toVariant().value<quint64>()) 0045 , mStored(json[QStringLiteral("Stored")].toBool()) 0046 { 0047 new FakeDeviceAdaptor(this); 0048 auto bus = QDBusConnection::sessionBus(); 0049 if (!bus.registerObject(dbusPath().path(), this)) { 0050 throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2").arg(mUid, bus.lastError().message())); 0051 } 0052 } 0053 0054 FakeDevice::~FakeDevice() 0055 { 0056 QDBusConnection::sessionBus().unregisterObject(dbusPath().path()); 0057 } 0058 0059 QDBusObjectPath FakeDevice::dbusPath() const 0060 { 0061 return QDBusObjectPath(QStringLiteral("/org/freedesktop/bolt/devices/%1").arg(QString(mUid).replace(QLatin1Char('-'), QLatin1Char('_')))); 0062 } 0063 0064 QString FakeDevice::uid() const 0065 { 0066 return mUid; 0067 } 0068 0069 QString FakeDevice::name() const 0070 { 0071 return mName; 0072 } 0073 0074 void FakeDevice::setName(const QString &name) 0075 { 0076 mName = name; 0077 } 0078 0079 QString FakeDevice::vendor() const 0080 { 0081 return mVendor; 0082 } 0083 0084 void FakeDevice::setVendor(const QString &vendor) 0085 { 0086 mVendor = vendor; 0087 } 0088 0089 QString FakeDevice::type() const 0090 { 0091 return mType; 0092 } 0093 0094 void FakeDevice::setType(const QString &type) 0095 { 0096 mType = type; 0097 } 0098 0099 QString FakeDevice::status() const 0100 { 0101 return mStatus; 0102 } 0103 0104 void FakeDevice::setStatus(const QString &status) 0105 { 0106 mStatus = status; 0107 } 0108 0109 QString FakeDevice::authFlags() const 0110 { 0111 return mAuthFlags; 0112 } 0113 0114 void FakeDevice::setAuthFlags(const QString &authFlags) 0115 { 0116 mAuthFlags = authFlags; 0117 } 0118 0119 QString FakeDevice::parent() const 0120 { 0121 return mParent; 0122 } 0123 0124 void FakeDevice::setParent(const QString &parent) 0125 { 0126 mParent = parent; 0127 } 0128 0129 QString FakeDevice::sysfsPath() const 0130 { 0131 return mSysfsPath; 0132 } 0133 0134 void FakeDevice::setSysfsPath(const QString &sysfsPath) 0135 { 0136 mSysfsPath = sysfsPath; 0137 } 0138 0139 bool FakeDevice::stored() const 0140 { 0141 return mStored; 0142 } 0143 0144 void FakeDevice::setStored(bool stored) 0145 { 0146 mStored = stored; 0147 } 0148 0149 QString FakeDevice::policy() const 0150 { 0151 return mPolicy; 0152 } 0153 0154 void FakeDevice::setPolicy(const QString &policy) 0155 { 0156 mPolicy = policy; 0157 } 0158 0159 QString FakeDevice::key() const 0160 { 0161 return mKey; 0162 } 0163 0164 void FakeDevice::setKey(const QString &key) 0165 { 0166 mKey = key; 0167 } 0168 0169 QString FakeDevice::label() const 0170 { 0171 return mLabel; 0172 } 0173 0174 void FakeDevice::setLabel(const QString &label) 0175 { 0176 mLabel = label; 0177 Q_EMIT labelChanged(label); 0178 } 0179 0180 quint64 FakeDevice::connectTime() const 0181 { 0182 return mConnectTime; 0183 } 0184 0185 void FakeDevice::setConnectTime(quint64 connectTime) 0186 { 0187 mConnectTime = connectTime; 0188 } 0189 0190 quint64 FakeDevice::authorizeTime() const 0191 { 0192 return mAuthorizeTime; 0193 } 0194 0195 void FakeDevice::setAuthorizeTime(quint64 authorizeTime) 0196 { 0197 mAuthorizeTime = authorizeTime; 0198 } 0199 0200 quint64 FakeDevice::storeTime() const 0201 { 0202 return mStoreTime; 0203 } 0204 0205 void FakeDevice::setStoreTime(quint64 storeTime) 0206 { 0207 mStoreTime = storeTime; 0208 } 0209 0210 void FakeDevice::Authorize(const QString &flags) 0211 { 0212 std::this_thread::sleep_for(1s); // simulate this operation taking time 0213 mAuthFlags = flags; 0214 mStatus = QLatin1String("authorized"); 0215 }