Custom devices

This commit is contained in:
Richard Liebscher 2020-08-18 16:55:48 +02:00
parent f953678e2e
commit 8d16d05c8a
8 changed files with 84 additions and 7 deletions

View file

@ -117,7 +117,7 @@ void LanLinkProvider::onStop()
void LanLinkProvider::onNetworkChange()
{
if (m_combineBroadcastsTimer.isActive()) {
qCDebug(KDECONNECT_CORE()) << "Preventing duplicate broadcasts";
qCDebug(KDECONNECT_CORE) << "Preventing duplicate broadcasts";
return;
}
m_combineBroadcastsTimer.start();
@ -126,7 +126,6 @@ void LanLinkProvider::onNetworkChange()
//I'm in a new network, let's be polite and introduce myself
void LanLinkProvider::broadcastToNetwork()
{
if (!m_server->isListening()) {
//Not started
return;
@ -136,9 +135,9 @@ void LanLinkProvider::broadcastToNetwork()
qCDebug(KDECONNECT_CORE()) << "Broadcasting identity packet";
QHostAddress destAddress = m_testMode? QHostAddress::LocalHost : QHostAddress(QStringLiteral("255.255.255.255"));
QList<QHostAddress> destinations = getBroadcastAddresses();
NetworkPacket np(QLatin1String(""));
NetworkPacket np;
NetworkPacket::createIdentityPacket(&np);
np.set(QStringLiteral("tcpPort"), m_tcpPort);
@ -154,16 +153,48 @@ void LanLinkProvider::broadcastToNetwork()
QHostAddress sourceAddress = ifaceAddress.ip();
if (sourceAddress.protocol() == QAbstractSocket::IPv4Protocol && sourceAddress != QHostAddress::LocalHost) {
qCDebug(KDECONNECT_CORE()) << "Broadcasting as" << sourceAddress;
sendSocket.writeDatagram(np.serialize(), destAddress, m_udpBroadcastPort);
sendBroadcasts(sendSocket, np, destinations);
sendSocket.close();
}
}
}
}
#else
m_udpSocket.writeDatagram(np.serialize(), destAddress, m_udpBroadcastPort);
sendBroadcasts(m_udpSocket, np, destinations);
#endif
}
QList<QHostAddress> LanLinkProvider::getBroadcastAddresses()
{
const QStringList customDevices = KdeConnectConfig::instance().customDevices();
QList<QHostAddress> destinations;
destinations.reserve(customDevices.length() + 1);
// Default broadcast address
destinations.append(m_testMode ? QHostAddress::LocalHost : QHostAddress::Broadcast);
// Custom device addresses
for (auto& customDevice : customDevices) {
QHostAddress address(customDevice);
if (address.isNull()) {
qCWarning(KDECONNECT_CORE) << "Invalid custom device address" << customDevice;
} else {
destinations.append(address);
}
}
return destinations;
}
void LanLinkProvider::sendBroadcasts(
QUdpSocket& socket, const NetworkPacket& np, const QList<QHostAddress>& addresses)
{
const QByteArray payload = np.serialize();
for (auto& address : addresses) {
socket.writeDatagram(payload, address, m_udpBroadcastPort);
}
}
//I'm the existing device, a new device is kindly introducing itself.

View file

@ -76,6 +76,8 @@ private:
void onNetworkConfigurationChanged(const QNetworkConfiguration& config);
void addLink(const QString& deviceId, QSslSocket* socket, NetworkPacket* receivedPacket, LanDeviceLink::ConnectionStarted connectionOrigin);
QList<QHostAddress> getBroadcastAddresses();
void sendBroadcasts(QUdpSocket& socket, const NetworkPacket &np, const QList<QHostAddress>& addresses);
Server* m_server;
QUdpSocket m_udpSocket;

View file

@ -238,12 +238,31 @@ void Daemon::onDeviceStatusChanged()
void Daemon::setAnnouncedName(const QString& name)
{
qCDebug(KDECONNECT_CORE()) << "Announcing name";
qCDebug(KDECONNECT_CORE) << "Announcing name";
KdeConnectConfig::instance().setName(name);
forceOnNetworkChange();
Q_EMIT announcedNameChanged(name);
}
void Daemon::setCustomDevices(const QStringList& addresses)
{
auto& config = KdeConnectConfig::instance();
auto customDevices = config.customDevices();
if (customDevices != addresses) {
qCDebug(KDECONNECT_CORE) << "Changed list of custom device addresses:" << addresses;
config.setCustomDevices(addresses);
Q_EMIT customDevicesChanged(addresses);
forceOnNetworkChange();
}
}
QStringList Daemon::customDevices() const
{
return KdeConnectConfig::instance().customDevices();
}
QString Daemon::announcedName()
{
return KdeConnectConfig::instance().name();

View file

@ -27,6 +27,8 @@ class KDECONNECTCORE_EXPORT Daemon
Q_CLASSINFO("D-Bus Interface", "org.kde.kdeconnect.daemon")
Q_PROPERTY(bool isDiscoveringDevices READ isDiscoveringDevices)
Q_PROPERTY(QStringList pairingRequests READ pairingRequests NOTIFY pairingRequestsChanged)
Q_PROPERTY(QStringList customDevices
READ customDevices WRITE setCustomDevices NOTIFY customDevicesChanged)
public:
explicit Daemon(QObject* parent, bool testMode = false);
@ -48,6 +50,10 @@ public:
QStringList pairingRequests() const;
// Custom devices
QStringList customDevices() const;
void setCustomDevices(const QStringList& addresses);
Q_SCRIPTABLE QString selfId() const;
public Q_SLOTS:
Q_SCRIPTABLE void acquireDiscoveryMode(const QString& id);
@ -75,6 +81,7 @@ Q_SIGNALS:
Q_SCRIPTABLE void deviceListChanged(); //Emitted when any of deviceAdded, deviceRemoved or deviceVisibilityChanged is emitted
Q_SCRIPTABLE void announcedNameChanged(const QString& announcedName);
Q_SCRIPTABLE void pairingRequestsChanged();
Q_SCRIPTABLE void customDevicesChanged(const QStringList& customDevices);
private Q_SLOTS:
void onNewDeviceLink(const NetworkPacket& identityPacket, DeviceLink* dl);

View file

@ -219,6 +219,16 @@ QString KdeConnectConfig::getDeviceProperty(const QString& deviceId, const QStri
return value;
}
void KdeConnectConfig::setCustomDevices(const QStringList& addresses)
{
d->m_config->setValue(QStringLiteral("customDevices"), addresses);
d->m_config->sync();
}
QStringList KdeConnectConfig::customDevices() const
{
return d->m_config->value(QStringLiteral("customDevices")).toStringList();
}
QDir KdeConnectConfig::deviceConfigDir(const QString& deviceId)
{

View file

@ -49,6 +49,10 @@ public:
void setDeviceProperty(const QString& deviceId, const QString& name, const QString& value);
QString getDeviceProperty(const QString& deviceId, const QString& name, const QString& defaultValue = QString());
// Custom devices
void setCustomDevices(const QStringList& addresses);
QStringList customDevices() const;
/*
* Paths for config files, there is no guarantee the directories already exist
*/

View file

@ -24,6 +24,7 @@ DaemonDbusInterface::DaemonDbusInterface(QObject* parent)
: OrgKdeKdeconnectDaemonInterface(DaemonDbusInterface::activatedService(), QStringLiteral("/modules/kdeconnect"), DBusHelper::sessionBus(), parent)
{
connect(this, &OrgKdeKdeconnectDaemonInterface::pairingRequestsChanged, this, &DaemonDbusInterface::pairingRequestsChangedProxy);
connect(this, &OrgKdeKdeconnectDaemonInterface::customDevicesChanged, this, &DaemonDbusInterface::customDevicesChangedProxy);
}
DaemonDbusInterface::~DaemonDbusInterface()

View file

@ -35,6 +35,8 @@ class KDECONNECTINTERFACES_EXPORT DaemonDbusInterface
: public OrgKdeKdeconnectDaemonInterface
{
Q_OBJECT
Q_PROPERTY(QStringList customDevices
READ customDevices WRITE setCustomDevices NOTIFY customDevicesChangedProxy)
public:
explicit DaemonDbusInterface(QObject* parent = nullptr);
~DaemonDbusInterface() override;
@ -44,6 +46,7 @@ public:
Q_SIGNALS:
void deviceAdded(const QString& id);
void pairingRequestsChangedProxy();
void customDevicesChangedProxy();
};
class KDECONNECTINTERFACES_EXPORT DeviceDbusInterface