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
This commit is contained in:
Nicolas Fella 2019-09-08 17:09:52 +02:00
parent 8d6136f953
commit 39fe270cd1
13 changed files with 53 additions and 70 deletions

View file

@ -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()));
}
}

View file

@ -212,7 +212,7 @@ void LanLinkProvider::udpBroadcastReceived()
continue;
}
if (receivedPacket->get<QString>(QStringLiteral("deviceId")) == KdeConnectConfig::instance()->deviceId()) {
if (receivedPacket->get<QString>(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<int>(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<int>(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 {

View file

@ -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();
}

View file

@ -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; i<localSha1.size(); i += 3) {
localSha1.insert(i, QStringLiteral(":")); // Improve readability
}
result += i18n("SHA1 fingerprint of your device certificate is: %1\n", localSha1);
std::string remotePem = KdeConnectConfig::instance()->getDeviceProperty(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) {

View file

@ -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;

View file

@ -35,7 +35,7 @@ public:
QString deviceType;
};
static KdeConnectConfig* instance();
static KdeConnectConfig& instance();
/*
* Our own info

View file

@ -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);

View file

@ -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<QIODevice>();
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());

View file

@ -82,7 +82,7 @@ void Mounter::onPackageReceived(const NetworkPacket& np)
unmount(false);
return;
}
if (np.has(QStringLiteral("errorMessage"))) {
Q_EMIT failed(np.get<QString>(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

View file

@ -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;

View file

@ -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"));
}

View file

@ -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)

View file

@ -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();