| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2025 Martin Rodriguez Reboredo <yakoyoku@gmail.com> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "wifip2pdevice.h" |
| 8 | #include "wifip2pdevice_p.h" |
| 9 | #include "wifip2ppeer.h" |
| 10 | |
| 11 | #undef signals |
| 12 | #include <libnm/NetworkManager.h> |
| 13 | #define signals Q_SIGNALS |
| 14 | |
| 15 | #include "manager.h" |
| 16 | #include "manager_p.h" |
| 17 | |
| 18 | #include "nmdebug.h" |
| 19 | #include "utils.h" |
| 20 | |
| 21 | #include <QDBusMetaType> |
| 22 | |
| 23 | using namespace Qt::Literals; |
| 24 | |
| 25 | NetworkManager::WifiP2PDevicePrivate::WifiP2PDevicePrivate(const QString &path, WifiP2PDevice *q) |
| 26 | : DevicePrivate(path, q) |
| 27 | #ifdef NMQT_STATIC |
| 28 | , wifip2pIface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::sessionBus()) |
| 29 | #else |
| 30 | , wifip2pIface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::systemBus()) |
| 31 | #endif |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | NetworkManager::WifiP2PDevice::WifiP2PDevice(const QString &path, QObject *parent) |
| 36 | : Device(*new WifiP2PDevicePrivate(path, this), parent) |
| 37 | { |
| 38 | Q_D(WifiP2PDevice); |
| 39 | |
| 40 | qDBusRegisterMetaType<QList<QDBusObjectPath>>(); |
| 41 | |
| 42 | #ifdef NMQT_STATIC |
| 43 | connect(&d->wifip2pIface, &OrgFreedesktopNetworkManagerDeviceWifiP2PInterface::PropertiesChanged, d, &WifiP2PDevicePrivate::propertiesChanged); |
| 44 | #endif |
| 45 | |
| 46 | connect(sender: &d->wifip2pIface, signal: &OrgFreedesktopNetworkManagerDeviceWifiP2PInterface::PeerAdded, context: d, slot: &WifiP2PDevicePrivate::peerAdded); |
| 47 | connect(sender: &d->wifip2pIface, signal: &OrgFreedesktopNetworkManagerDeviceWifiP2PInterface::PeerRemoved, context: d, slot: &WifiP2PDevicePrivate::peerRemoved); |
| 48 | |
| 49 | const QList<QDBusObjectPath> peers = d->wifip2pIface.peers(); |
| 50 | for (const QDBusObjectPath &op : peers) { |
| 51 | d->peerAdded(peer: op); |
| 52 | } |
| 53 | |
| 54 | // Retrieve all WifiP2PDevice's properties at once |
| 55 | const QVariantMap initialProperties = NetworkManagerPrivate::retrieveInitialProperties(interfaceName: d->wifip2pIface.staticInterfaceName(), path); |
| 56 | if (!initialProperties.isEmpty()) { |
| 57 | d->propertiesChanged(properties: initialProperties); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | NetworkManager::WifiP2PDevice::~WifiP2PDevice() = default; |
| 62 | |
| 63 | NetworkManager::Device::Type NetworkManager::WifiP2PDevice::type() const |
| 64 | { |
| 65 | return NetworkManager::Device::WifiP2P; |
| 66 | } |
| 67 | |
| 68 | QString NetworkManager::WifiP2PDevice::hardwareAddress() const |
| 69 | { |
| 70 | Q_D(const WifiP2PDevice); |
| 71 | return d->hardwareAddress; |
| 72 | } |
| 73 | |
| 74 | QStringList NetworkManager::WifiP2PDevice::peers() const |
| 75 | { |
| 76 | Q_D(const WifiP2PDevice); |
| 77 | return d->peerMap.keys(); |
| 78 | } |
| 79 | |
| 80 | NetworkManager::WifiP2PPeer::Ptr NetworkManager::WifiP2PDevice::findPeer(const QString &uni) |
| 81 | { |
| 82 | Q_D(WifiP2PDevice); |
| 83 | NetworkManager::WifiP2PPeer::Ptr peer; |
| 84 | |
| 85 | QMap<QString, NetworkManager::WifiP2PPeer::Ptr>::ConstIterator mapIt = d->peerMap.constFind(key: uni); |
| 86 | if (mapIt != d->peerMap.constEnd()) { |
| 87 | peer = mapIt.value(); |
| 88 | } else if (!uni.isEmpty() && uni != QLatin1String("/" )) { |
| 89 | d->peerAdded(peer: QDBusObjectPath(uni)); |
| 90 | mapIt = d->peerMap.constFind(key: uni); |
| 91 | if (mapIt != d->peerMap.constEnd()) { |
| 92 | peer = mapIt.value(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return peer; |
| 97 | } |
| 98 | |
| 99 | QDBusPendingReply<> NetworkManager::WifiP2PDevice::startFind(const QVariantMap &options) |
| 100 | { |
| 101 | Q_D(WifiP2PDevice); |
| 102 | return d->wifip2pIface.StartFind(options); |
| 103 | } |
| 104 | |
| 105 | QDBusPendingReply<> NetworkManager::WifiP2PDevice::stopFind() |
| 106 | { |
| 107 | Q_D(WifiP2PDevice); |
| 108 | return d->wifip2pIface.StopFind(); |
| 109 | } |
| 110 | |
| 111 | void NetworkManager::WifiP2PDevicePrivate::peerAdded(const QDBusObjectPath &peer) |
| 112 | { |
| 113 | Q_Q(WifiP2PDevice); |
| 114 | if (!peerMap.contains(key: peer.path())) { |
| 115 | NetworkManager::WifiP2PPeer::Ptr peerPtr(new NetworkManager::WifiP2PPeer(peer.path()), &QObject::deleteLater); |
| 116 | peerMap.insert(key: peer.path(), value: peerPtr); |
| 117 | Q_EMIT q->peerAppeared(peer: peer.path()); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void NetworkManager::WifiP2PDevicePrivate::peerRemoved(const QDBusObjectPath &peer) |
| 122 | { |
| 123 | Q_Q(WifiP2PDevice); |
| 124 | if (!peerMap.contains(key: peer.path())) { |
| 125 | qCDebug(NMQT) << "Peer list lookup failed for " << peer.path(); |
| 126 | } |
| 127 | Q_EMIT q->peerDisappeared(peer: peer.path()); |
| 128 | peerMap.remove(key: peer.path()); |
| 129 | } |
| 130 | |
| 131 | void NetworkManager::WifiP2PDevicePrivate::propertyChanged(const QString &property, const QVariant &value) |
| 132 | { |
| 133 | Q_Q(WifiP2PDevice); |
| 134 | |
| 135 | if (property == "HwAddress"_L1 ) { |
| 136 | hardwareAddress = value.toString(); |
| 137 | Q_EMIT q->hardwareAddressChanged(hardwareAddress); |
| 138 | } else if (property == "Peers"_L1 ) { |
| 139 | } else { |
| 140 | DevicePrivate::propertyChanged(property, value); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #include "moc_wifip2pdevice.cpp" |
| 145 | #include "moc_wifip2pdevice_p.cpp" |
| 146 | |