Port away from deprecated QtNetwork classes

This commit is contained in:
Alexander Lohnau 2023-04-28 14:32:31 +02:00
parent b3163d8725
commit 5b5265f7a8
2 changed files with 26 additions and 17 deletions

View file

@ -20,9 +20,7 @@
#include <QHostInfo> #include <QHostInfo>
#include <QMetaEnum> #include <QMetaEnum>
#include <QNetworkConfigurationManager>
#include <QNetworkProxy> #include <QNetworkProxy>
#include <QNetworkSession>
#include <QSslCipher> #include <QSslCipher>
#include <QSslConfiguration> #include <QSslConfiguration>
#include <QSslKey> #include <QSslKey>
@ -60,21 +58,26 @@ LanLinkProvider::LanLinkProvider(bool testMode, quint16 udpBroadcastPort, quint1
m_udpSocket.setProxy(QNetworkProxy::NoProxy); m_udpSocket.setProxy(QNetworkProxy::NoProxy);
// Detect when a network interface changes status, so we announce ourselves in the new network
QNetworkConfigurationManager *networkManager = new QNetworkConfigurationManager(this);
connect(networkManager, &QNetworkConfigurationManager::configurationChanged, this, &LanLinkProvider::onNetworkConfigurationChanged);
connect(&m_udpSocket, &QAbstractSocket::errorOccurred, [](QAbstractSocket::SocketError socketError) { connect(&m_udpSocket, &QAbstractSocket::errorOccurred, [](QAbstractSocket::SocketError socketError) {
qWarning() << "Error sending UDP packet:" << socketError; qWarning() << "Error sending UDP packet:" << socketError;
}); });
}
void LanLinkProvider::onNetworkConfigurationChanged(const QNetworkConfiguration &config) #if QT_VERSION_MAJOR < 6
{ QNetworkConfigurationManager *networkManager = new QNetworkConfigurationManager(this);
if (m_lastConfig != config && config.state() == QNetworkConfiguration::Active) { connect(networkManager, &QNetworkConfigurationManager::configurationChanged, this, [this](QNetworkConfiguration config) {
m_lastConfig = config; if (m_lastConfig != config && config.state() == QNetworkConfiguration::Active) {
onNetworkChange(); m_lastConfig = config;
} onNetworkChange();
}
});
#else
// Detect when a network interface changes status, so we announce ourselves in the new network
connect(QNetworkInformation::instance(), &QNetworkInformation::reachabilityChanged, this, [this]() {
if (QNetworkInformation::instance()->reachability() == QNetworkInformation::Reachability::Online) {
onNetworkChange();
}
});
#endif
} }
LanLinkProvider::~LanLinkProvider() LanLinkProvider::~LanLinkProvider()
@ -566,7 +569,7 @@ void LanLinkProvider::configureSocket(QSslSocket *socket)
#endif #endif
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
int maxIdle = 5 * 60 * 1000; // 5 minutes of idle before sending keep-alives int maxIdle = 5 * 60 * 1000; // 5 minutes of idle before sending keep-alive
int interval = 5 * 1000; // 5 seconds interval between probes after 5 minute delay int interval = 5 * 1000; // 5 seconds interval between probes after 5 minute delay
DWORD nop; DWORD nop;

View file

@ -7,12 +7,17 @@
#ifndef LANLINKPROVIDER_H #ifndef LANLINKPROVIDER_H
#define LANLINKPROVIDER_H #define LANLINKPROVIDER_H
#include <QNetworkSession>
#include <QObject> #include <QObject>
#include <QSslSocket> #include <QSslSocket>
#include <QTcpServer> #include <QTcpServer>
#include <QTimer> #include <QTimer>
#include <QUdpSocket> #include <QUdpSocket>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#else
#include <QNetworkInformation>
#endif
#include "backends/linkprovider.h" #include "backends/linkprovider.h"
#include "kdeconnectcore_export.h" #include "kdeconnectcore_export.h"
@ -69,7 +74,6 @@ private Q_SLOTS:
void broadcastToNetwork(); void broadcastToNetwork();
private: private:
void onNetworkConfigurationChanged(const QNetworkConfiguration &config);
void addLink(QSslSocket *socket, const DeviceInfo &deviceInfo); void addLink(QSslSocket *socket, const DeviceInfo &deviceInfo);
QList<QHostAddress> getBroadcastAddresses(); QList<QHostAddress> getBroadcastAddresses();
void sendUdpIdentityPacket(QUdpSocket &socket, const QList<QHostAddress> &addresses); void sendUdpIdentityPacket(QUdpSocket &socket, const QList<QHostAddress> &addresses);
@ -89,13 +93,15 @@ private:
QHostAddress sender; QHostAddress sender;
}; };
QMap<QSslSocket *, PendingConnect> m_receivedIdentityPackets; QMap<QSslSocket *, PendingConnect> m_receivedIdentityPackets;
QNetworkConfiguration m_lastConfig;
const bool m_testMode; const bool m_testMode;
QTimer m_combineBroadcastsTimer; QTimer m_combineBroadcastsTimer;
#ifdef KDECONNECT_MDNS #ifdef KDECONNECT_MDNS
MdnsDiscovery m_mdnsDiscovery; MdnsDiscovery m_mdnsDiscovery;
#endif #endif
#if QT_VERSION_MAJOR < 6
QNetworkConfiguration m_lastConfig;
#endif
}; };
#endif #endif