From 39fe270cd182b819f452fbe63b55ff3af205d905 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Sun, 8 Sep 2019 17:09:52 +0200 Subject: [PATCH] Port Kdeconnectconfig to reference based singleton It's more expressive in the sense that it makes clear that the user should not delete the object --- core/backends/lan/landevicelink.cpp | 12 ++++++------ core/backends/lan/lanlinkprovider.cpp | 12 ++++++------ core/daemon.cpp | 8 ++++---- core/device.cpp | 16 ++++++++-------- core/kdeconnectconfig.cpp | 6 +++--- core/kdeconnectconfig.h | 2 +- core/kdeconnectpluginconfig.cpp | 2 +- core/networkpacket.cpp | 7 +++---- plugins/sftp/mounter.cpp | 4 ++-- tests/devicetest.cpp | 8 +++----- tests/kdeconnectconfigtest.cpp | 17 ++++------------- tests/lanlinkprovidertest.cpp | 18 +++++++----------- tests/sendfiletest.cpp | 11 +++++------ 13 files changed, 53 insertions(+), 70 deletions(-) diff --git a/core/backends/lan/landevicelink.cpp b/core/backends/lan/landevicelink.cpp index 3b157e47d..8a65fb92e 100644 --- a/core/backends/lan/landevicelink.cpp +++ b/core/backends/lan/landevicelink.cpp @@ -56,7 +56,7 @@ void LanDeviceLink::reset(QSslSocket* socket, ConnectionStarted connectionSource m_connectionSource = connectionSource; - QString certString = KdeConnectConfig::instance()->getDeviceProperty(deviceId(), QStringLiteral("certificate")); + QString certString = KdeConnectConfig::instance().getDeviceProperty(deviceId(), QStringLiteral("certificate")); DeviceLink::setPairStatus(certString.isEmpty()? PairStatus::NotPaired : PairStatus::Paired); } @@ -89,9 +89,9 @@ bool LanDeviceLink::sendPacket(NetworkPacket& np) if (!m_compositeUploadJob || !m_compositeUploadJob->isRunning()) { m_compositeUploadJob = new CompositeUploadJob(deviceId(), true); } - + m_compositeUploadJob->addSubjob(new UploadJob(np)); - + if (!m_compositeUploadJob->isRunning()) { m_compositeUploadJob->start(); } @@ -100,7 +100,7 @@ bool LanDeviceLink::sendPacket(NetworkPacket& np) fireAndForgetJob->addSubjob(new UploadJob(np)); fireAndForgetJob->start(); } - + return true; } else { int written = m_socketLineReader->write(np.serialize()); @@ -177,9 +177,9 @@ void LanDeviceLink::setPairStatus(PairStatus status) DeviceLink::setPairStatus(status); if (status == Paired) { - Q_ASSERT(KdeConnectConfig::instance()->trustedDevices().contains(deviceId())); + Q_ASSERT(KdeConnectConfig::instance().trustedDevices().contains(deviceId())); Q_ASSERT(!m_socketLineReader->peerCertificate().isNull()); - KdeConnectConfig::instance()->setDeviceProperty(deviceId(), QStringLiteral("certificate"), QString::fromLatin1(m_socketLineReader->peerCertificate().toPem().data())); + KdeConnectConfig::instance().setDeviceProperty(deviceId(), QStringLiteral("certificate"), QString::fromLatin1(m_socketLineReader->peerCertificate().toPem().data())); } } diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index 1893ccff5..6cdb33d7e 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -212,7 +212,7 @@ void LanLinkProvider::udpBroadcastReceived() continue; } - if (receivedPacket->get(QStringLiteral("deviceId")) == KdeConnectConfig::instance()->deviceId()) { + if (receivedPacket->get(QStringLiteral("deviceId")) == KdeConnectConfig::instance().deviceId()) { //qCDebug(KDECONNECT_CORE) << "Ignoring my own broadcast"; delete receivedPacket; continue; @@ -281,7 +281,7 @@ void LanLinkProvider::tcpSocketConnected() // if ssl supported if (receivedPacket->get(QStringLiteral("protocolVersion")) >= MIN_VERSION_WITH_SSL_SUPPORT) { - bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId); + bool isDeviceTrusted = KdeConnectConfig::instance().trustedDevices().contains(deviceId); configureSslSocket(socket, deviceId, isDeviceTrusted); qCDebug(KDECONNECT_CORE) << "Starting server ssl (I'm the client TCP socket)"; @@ -403,7 +403,7 @@ void LanLinkProvider::dataReceived() if (np->get(QStringLiteral("protocolVersion")) >= MIN_VERSION_WITH_SSL_SUPPORT) { - bool isDeviceTrusted = KdeConnectConfig::instance()->trustedDevices().contains(deviceId); + bool isDeviceTrusted = KdeConnectConfig::instance().trustedDevices().contains(deviceId); configureSslSocket(socket, deviceId, isDeviceTrusted); qCDebug(KDECONNECT_CORE) << "Starting client ssl (but I'm the server TCP socket)"; @@ -453,12 +453,12 @@ void LanLinkProvider::configureSslSocket(QSslSocket* socket, const QString& devi sslConfig.setCiphers(socketCiphers); socket->setSslConfiguration(sslConfig); - socket->setLocalCertificate(KdeConnectConfig::instance()->certificate()); - socket->setPrivateKey(KdeConnectConfig::instance()->privateKeyPath()); + socket->setLocalCertificate(KdeConnectConfig::instance().certificate()); + socket->setPrivateKey(KdeConnectConfig::instance().privateKeyPath()); socket->setPeerVerifyName(deviceId); if (isDeviceTrusted) { - QString certString = KdeConnectConfig::instance()->getDeviceProperty(deviceId, QStringLiteral("certificate"), QString()); + QString certString = KdeConnectConfig::instance().getDeviceProperty(deviceId, QStringLiteral("certificate"), QString()); socket->addCaCertificate(QSslCertificate(certString.toLatin1())); socket->setPeerVerifyMode(QSslSocket::VerifyPeer); } else { diff --git a/core/daemon.cpp b/core/daemon.cpp index 11065e0b3..dce5abf63 100644 --- a/core/daemon.cpp +++ b/core/daemon.cpp @@ -94,7 +94,7 @@ void Daemon::init() } //Read remembered paired devices - const QStringList& list = KdeConnectConfig::instance()->trustedDevices(); + const QStringList& list = KdeConnectConfig::instance().trustedDevices(); for (const QString& id : list) { addDevice(new Device(this, id)); } @@ -253,14 +253,14 @@ void Daemon::onDeviceStatusChanged() void Daemon::setAnnouncedName(const QString& name) { qCDebug(KDECONNECT_CORE()) << "Announcing name"; - KdeConnectConfig::instance()->setName(name); + KdeConnectConfig::instance().setName(name); forceOnNetworkChange(); Q_EMIT announcedNameChanged(name); } QString Daemon::announcedName() { - return KdeConnectConfig::instance()->name(); + return KdeConnectConfig::instance().name(); } QNetworkAccessManager* Daemon::networkAccessManager() @@ -324,5 +324,5 @@ Daemon::~Daemon() QString Daemon::selfId() const { - return KdeConnectConfig::instance()->deviceId(); + return KdeConnectConfig::instance().deviceId(); } diff --git a/core/device.cpp b/core/device.cpp index 18e35ec94..32e201796 100644 --- a/core/device.cpp +++ b/core/device.cpp @@ -81,7 +81,7 @@ Device::Device(QObject* parent, const QString& id) , d(new Device::DevicePrivate(id)) { d->m_protocolVersion = NetworkPacket::s_protocolVersion; - KdeConnectConfig::DeviceInfo info = KdeConnectConfig::instance()->getTrustedDevice(d->m_deviceId); + KdeConnectConfig::DeviceInfo info = KdeConnectConfig::instance().getTrustedDevice(d->m_deviceId); d->m_deviceName = info.deviceName; d->m_deviceType = str2type(info.deviceType); @@ -216,7 +216,7 @@ void Device::reloadPlugins() QString Device::pluginsConfigFile() const { - return KdeConnectConfig::instance()->deviceConfigDir(id()).absoluteFilePath(QStringLiteral("config")); + return KdeConnectConfig::instance().deviceConfigDir(id()).absoluteFilePath(QStringLiteral("config")); } void Device::requestPair() @@ -241,14 +241,14 @@ void Device::unpair() for (DeviceLink* dl : qAsConst(d->m_deviceLinks)) { dl->userRequestsUnpair(); } - KdeConnectConfig::instance()->removeTrustedDevice(id()); + KdeConnectConfig::instance().removeTrustedDevice(id()); Q_EMIT trustedChanged(false); } void Device::pairStatusChanged(DeviceLink::PairStatus status) { if (status == DeviceLink::NotPaired) { - KdeConnectConfig::instance()->removeTrustedDevice(id()); + KdeConnectConfig::instance().removeTrustedDevice(id()); for (DeviceLink* dl : qAsConst(d->m_deviceLinks)) { if (dl != sender()) { @@ -256,7 +256,7 @@ void Device::pairStatusChanged(DeviceLink::PairStatus status) } } } else { - KdeConnectConfig::instance()->addTrustedDevice(id(), name(), type()); + KdeConnectConfig::instance().addTrustedDevice(id(), name(), type()); } reloadPlugins(); //Will load/unload plugins @@ -419,7 +419,7 @@ void Device::privateReceivedPacket(const NetworkPacket& np) bool Device::isTrusted() const { - return KdeConnectConfig::instance()->trustedDevices().contains(id()); + return KdeConnectConfig::instance().trustedDevices().contains(id()); } QStringList Device::availableLinks() const @@ -542,13 +542,13 @@ QString Device::encryptionInfo() const QString result; QCryptographicHash::Algorithm digestAlgorithm = QCryptographicHash::Algorithm::Sha1; - QString localSha1 = QString::fromLatin1(KdeConnectConfig::instance()->certificate().digest(digestAlgorithm).toHex()); + QString localSha1 = QString::fromLatin1(KdeConnectConfig::instance().certificate().digest(digestAlgorithm).toHex()); for (int i = 2; igetDeviceProperty(id(), QStringLiteral("certificate")).toStdString(); + std::string remotePem = KdeConnectConfig::instance().getDeviceProperty(id(), QStringLiteral("certificate")).toStdString(); QSslCertificate remoteCertificate = QSslCertificate(QByteArray(remotePem.c_str(), (int)remotePem.size())); QString remoteSha1 = QString::fromLatin1(remoteCertificate.digest(digestAlgorithm).toHex()); for (int i = 2; i < remoteSha1.size(); i += 3) { diff --git a/core/kdeconnectconfig.cpp b/core/kdeconnectconfig.cpp index b705a583b..a23f0924b 100644 --- a/core/kdeconnectconfig.cpp +++ b/core/kdeconnectconfig.cpp @@ -58,9 +58,9 @@ struct KdeConnectConfigPrivate { #endif }; -KdeConnectConfig* KdeConnectConfig::instance() +KdeConnectConfig& KdeConnectConfig::instance() { - static KdeConnectConfig* kcc = new KdeConnectConfig(); + static KdeConnectConfig kcc; return kcc; } @@ -369,7 +369,7 @@ QString KdeConnectConfig::privateDBusAddress() } qCDebug(KDECONNECT_CORE) << "Private dbus address: " << addr; - + d->m_privateDBusAddress = addr; return addr; diff --git a/core/kdeconnectconfig.h b/core/kdeconnectconfig.h index fad9b6235..ffef00ae5 100644 --- a/core/kdeconnectconfig.h +++ b/core/kdeconnectconfig.h @@ -35,7 +35,7 @@ public: QString deviceType; }; - static KdeConnectConfig* instance(); + static KdeConnectConfig& instance(); /* * Our own info diff --git a/core/kdeconnectpluginconfig.cpp b/core/kdeconnectpluginconfig.cpp index 7a560c169..ed5e440de 100644 --- a/core/kdeconnectpluginconfig.cpp +++ b/core/kdeconnectpluginconfig.cpp @@ -37,7 +37,7 @@ struct KdeConnectPluginConfigPrivate KdeConnectPluginConfig::KdeConnectPluginConfig(const QString& deviceId, const QString& pluginName) : d(new KdeConnectPluginConfigPrivate()) { - d->m_configDir = KdeConnectConfig::instance()->pluginConfigDir(deviceId, pluginName); + d->m_configDir = KdeConnectConfig::instance().pluginConfigDir(deviceId, pluginName); QDir().mkpath(d->m_configDir.path()); d->m_config = new QSettings(d->m_configDir.absoluteFilePath(QStringLiteral("config")), QSettings::IniFormat); diff --git a/core/networkpacket.cpp b/core/networkpacket.cpp index a6eb26cf2..5da65afa8 100644 --- a/core/networkpacket.cpp +++ b/core/networkpacket.cpp @@ -57,14 +57,13 @@ NetworkPacket::NetworkPacket(const QString& type, const QVariantMap& body) void NetworkPacket::createIdentityPacket(NetworkPacket* np) { - KdeConnectConfig* config = KdeConnectConfig::instance(); np->m_id = QString::number(QDateTime::currentMSecsSinceEpoch()); np->m_type = PACKET_TYPE_IDENTITY; np->m_payload = QSharedPointer(); np->m_payloadSize = 0; - np->set(QStringLiteral("deviceId"), config->deviceId()); - np->set(QStringLiteral("deviceName"), config->name()); - np->set(QStringLiteral("deviceType"), config->deviceType()); + np->set(QStringLiteral("deviceId"), KdeConnectConfig::instance().deviceId()); + np->set(QStringLiteral("deviceName"), KdeConnectConfig::instance().name()); + np->set(QStringLiteral("deviceType"), KdeConnectConfig::instance().deviceType()); np->set(QStringLiteral("protocolVersion"), NetworkPacket::s_protocolVersion); np->set(QStringLiteral("incomingCapabilities"), PluginLoader::instance()->incomingCapabilities()); np->set(QStringLiteral("outgoingCapabilities"), PluginLoader::instance()->outgoingCapabilities()); diff --git a/plugins/sftp/mounter.cpp b/plugins/sftp/mounter.cpp index 9144e6833..fca9907cb 100644 --- a/plugins/sftp/mounter.cpp +++ b/plugins/sftp/mounter.cpp @@ -82,7 +82,7 @@ void Mounter::onPackageReceived(const NetworkPacket& np) unmount(false); return; } - + if (np.has(QStringLiteral("errorMessage"))) { Q_EMIT failed(np.get(QStringLiteral("errorMessage"))); return; @@ -136,7 +136,7 @@ void Mounter::onPackageReceived(const NetworkPacket& np) << QStringLiteral("-s") // This fixes a bug where file chunks are sent out of order and get corrupted on reception << QStringLiteral("-f") << QStringLiteral("-F") << QStringLiteral("/dev/null") //Do not use ~/.ssh/config - << QStringLiteral("-o") << QStringLiteral("IdentityFile=") + KdeConnectConfig::instance()->privateKeyPath() + << QStringLiteral("-o") << QStringLiteral("IdentityFile=") + KdeConnectConfig::instance().privateKeyPath() << QStringLiteral("-o") << QStringLiteral("StrictHostKeyChecking=no") //Do not ask for confirmation because it is not a known host << QStringLiteral("-o") << QStringLiteral("UserKnownHostsFile=/dev/null") //Prevent storing as a known host << QStringLiteral("-o") << QStringLiteral("HostKeyAlgorithms=+ssh-dss") //https://bugs.kde.org/show_bug.cgi?id=351725 diff --git a/tests/devicetest.cpp b/tests/devicetest.cpp index ef4d76edf..6f5193b43 100644 --- a/tests/devicetest.cpp +++ b/tests/devicetest.cpp @@ -58,9 +58,8 @@ void DeviceTest::initTestCase() void DeviceTest::testPairedDevice() { - KdeConnectConfig* kcc = KdeConnectConfig::instance(); - kcc->addTrustedDevice(deviceId, deviceName, deviceType); - kcc->setDeviceProperty(deviceId, QStringLiteral("certificate"), QString::fromLatin1(kcc->certificate().toPem())); // Using same certificate from kcc, instead of generating one + KdeConnectConfig::instance().addTrustedDevice(deviceId, deviceName, deviceType); + KdeConnectConfig::instance().setDeviceProperty(deviceId, QStringLiteral("certificate"), QString::fromLatin1(KdeConnectConfig::instance().certificate().toPem())); // Using same certificate from kcc, instead of generating one Device device(this, deviceId); @@ -94,8 +93,7 @@ void DeviceTest::testPairedDevice() void DeviceTest::testUnpairedDevice() { - KdeConnectConfig* kcc = KdeConnectConfig::instance(); - kcc->removeTrustedDevice(deviceId); + KdeConnectConfig::instance().removeTrustedDevice(deviceId); LanLinkProvider linkProvider; QSslSocket socket; diff --git a/tests/kdeconnectconfigtest.cpp b/tests/kdeconnectconfigtest.cpp index 94975c699..781d94834 100644 --- a/tests/kdeconnectconfigtest.cpp +++ b/tests/kdeconnectconfigtest.cpp @@ -32,26 +32,17 @@ class KdeConnectConfigTest : public QObject Q_OBJECT private Q_SLOTS: - void initTestCase(); void addTrustedDevice(); /* void remoteCertificateTest(); */ void removeTrustedDevice(); - -private: - KdeConnectConfig* kcc; }; -void KdeConnectConfigTest::initTestCase() -{ - kcc = KdeConnectConfig::instance(); -} - void KdeConnectConfigTest::addTrustedDevice() { - kcc->addTrustedDevice(QStringLiteral("testdevice"), QStringLiteral("Test Device"), QStringLiteral("phone")); - KdeConnectConfig::DeviceInfo devInfo = kcc->getTrustedDevice(QStringLiteral("testdevice")); + KdeConnectConfig::instance().addTrustedDevice(QStringLiteral("testdevice"), QStringLiteral("Test Device"), QStringLiteral("phone")); + KdeConnectConfig::DeviceInfo devInfo = KdeConnectConfig::instance().getTrustedDevice(QStringLiteral("testdevice")); QCOMPARE(devInfo.deviceName, QStringLiteral("Test Device")); QCOMPARE(devInfo.deviceType, QStringLiteral("phone")); } @@ -83,8 +74,8 @@ void KdeConnectConfigTest::remoteCertificateTest() void KdeConnectConfigTest::removeTrustedDevice() { - kcc->removeTrustedDevice(QStringLiteral("testdevice")); - KdeConnectConfig::DeviceInfo devInfo = kcc->getTrustedDevice(QStringLiteral("testdevice")); + KdeConnectConfig::instance().removeTrustedDevice(QStringLiteral("testdevice")); + KdeConnectConfig::DeviceInfo devInfo = KdeConnectConfig::instance().getTrustedDevice(QStringLiteral("testdevice")); QCOMPARE(devInfo.deviceName, QStringLiteral("unnamed")); QCOMPARE(devInfo.deviceType, QStringLiteral("unknown")); } diff --git a/tests/lanlinkprovidertest.cpp b/tests/lanlinkprovidertest.cpp index 3d0ed68ab..953e6c511 100644 --- a/tests/lanlinkprovidertest.cpp +++ b/tests/lanlinkprovidertest.cpp @@ -178,7 +178,6 @@ void LanLinkProviderTest::pairedDeviceTcpPacketReceived() LanLinkProvider testlanLinkProvider(true, udpBroadcastPort, udpListenPort); testlanLinkProvider.onStart(); - KdeConnectConfig* kcc = KdeConnectConfig::instance(); addTrustedDevice(); QUdpSocket* mUdpServer = new QUdpSocket; @@ -217,9 +216,9 @@ void LanLinkProviderTest::pairedDeviceTcpPacketReceived() QSignalSpy spy3(&socket, SIGNAL(encrypted())); setSocketAttributes(&socket); - socket.addCaCertificate(kcc->certificate()); + socket.addCaCertificate(KdeConnectConfig::instance().certificate()); socket.setPeerVerifyMode(QSslSocket::VerifyPeer); - socket.setPeerVerifyName(kcc->name()); + socket.setPeerVerifyName(KdeConnectConfig::instance().name()); socket.startServerEncryption(); QVERIFY(spy3.wait()); @@ -235,7 +234,6 @@ void LanLinkProviderTest::pairedDeviceTcpPacketReceived() void LanLinkProviderTest::pairedDeviceUdpPacketReceived() { - KdeConnectConfig* kcc = KdeConnectConfig::instance(); addTrustedDevice(); m_server = new Server(this); @@ -269,9 +267,9 @@ void LanLinkProviderTest::pairedDeviceUdpPacketReceived() this, [](QAbstractSocket::SocketError error){ qDebug() << "error:" << error; })); setSocketAttributes(serverSocket); - serverSocket->addCaCertificate(kcc->certificate()); + serverSocket->addCaCertificate(KdeConnectConfig::instance().certificate()); serverSocket->setPeerVerifyMode(QSslSocket::VerifyPeer); - serverSocket->setPeerVerifyName(kcc->deviceId()); + serverSocket->setPeerVerifyName(KdeConnectConfig::instance().deviceId()); serverSocket->startClientEncryption(); // Its TCP server. but SSL client QVERIFY(!serverSocket->isEncrypted()); @@ -432,15 +430,13 @@ void LanLinkProviderTest::setSocketAttributes(QSslSocket* socket) void LanLinkProviderTest::addTrustedDevice() { - KdeConnectConfig* kcc = KdeConnectConfig::instance(); - kcc->addTrustedDevice(m_deviceId, m_name, QStringLiteral("phone")); - kcc->setDeviceProperty(m_deviceId, QStringLiteral("certificate"), QString::fromLatin1(m_certificate.toPem())); + KdeConnectConfig::instance().addTrustedDevice(m_deviceId, m_name, QStringLiteral("phone")); + KdeConnectConfig::instance().setDeviceProperty(m_deviceId, QStringLiteral("certificate"), QString::fromLatin1(m_certificate.toPem())); } void LanLinkProviderTest::removeTrustedDevice() { - KdeConnectConfig* kcc = KdeConnectConfig::instance(); - kcc->removeTrustedDevice(m_deviceId); + KdeConnectConfig::instance().removeTrustedDevice(m_deviceId); } void LanLinkProviderTest::socketBindErrorFail(const QUdpSocket& socket) diff --git a/tests/sendfiletest.cpp b/tests/sendfiletest.cpp index 02d87f236..f16c143d0 100644 --- a/tests/sendfiletest.cpp +++ b/tests/sendfiletest.cpp @@ -101,13 +101,12 @@ class TestSendFile : public QObject const QString destFile = QDir::tempPath() + QStringLiteral("/kdeconnect-test-sentfile"); QFile(destFile).remove(); - const QString deviceId = KdeConnectConfig::instance()->deviceId() + const QString deviceId = KdeConnectConfig::instance().deviceId() , deviceName = QStringLiteral("testdevice") - , deviceType = KdeConnectConfig::instance()->deviceType(); + , deviceType = KdeConnectConfig::instance().deviceType(); - KdeConnectConfig* kcc = KdeConnectConfig::instance(); - kcc->addTrustedDevice(deviceId, deviceName, deviceType); - kcc->setDeviceProperty(deviceId, QStringLiteral("certificate"), QString::fromLatin1(kcc->certificate().toPem())); // Using same certificate from kcc, instead of generating + KdeConnectConfig::instance().addTrustedDevice(deviceId, deviceName, deviceType); + KdeConnectConfig::instance().setDeviceProperty(deviceId, QStringLiteral("certificate"), QString::fromLatin1(KdeConnectConfig::instance().certificate().toPem())); // Using same certificate from kcc, instead of generating //We need the device to be loaded on the daemon, otherwise CompositeUploadJob will get a null device Device* device = new Device(this, deviceId); @@ -120,7 +119,7 @@ class TestSendFile : public QObject CompositeUploadJob* job = new CompositeUploadJob(deviceId, false); UploadJob* uj = new UploadJob(np); job->addSubjob(uj); - + QSignalSpy spyUpload(job, &KJob::result); job->start();