| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2011 Ilia Kats <ilia-kats@gmx.net> |
| 3 | SPDX-FileCopyrightText: 2013 Jan Grulich <jgrulich@redhat.com> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 6 | */ |
| 7 | |
| 8 | #include "connection.h" |
| 9 | #include "device.h" |
| 10 | #include "nmdebug.h" |
| 11 | #include "settings.h" |
| 12 | #include "vpnconnection_p.h" |
| 13 | |
| 14 | NetworkManager::VpnConnectionPrivate::VpnConnectionPrivate(const QString &path, VpnConnection *q) |
| 15 | : ActiveConnectionPrivate(path, q) |
| 16 | #ifdef NMQT_STATIC |
| 17 | , iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::sessionBus()) |
| 18 | #else |
| 19 | , iface(NetworkManagerPrivate::DBUS_SERVICE, path, QDBusConnection::systemBus()) |
| 20 | #endif |
| 21 | , q_ptr(q) |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | NetworkManager::VpnConnection::State NetworkManager::VpnConnectionPrivate::convertVpnConnectionState(uint state) |
| 26 | { |
| 27 | return static_cast<NetworkManager::VpnConnection::State>(state); |
| 28 | } |
| 29 | |
| 30 | NetworkManager::VpnConnection::StateChangeReason NetworkManager::VpnConnectionPrivate::convertVpnConnectionStateReason(uint reason) |
| 31 | { |
| 32 | return static_cast<NetworkManager::VpnConnection::StateChangeReason>(reason); |
| 33 | } |
| 34 | |
| 35 | NetworkManager::VpnConnection::VpnConnection(const QString &path, QObject *parent) |
| 36 | : ActiveConnection(*new VpnConnectionPrivate(path, this), parent) |
| 37 | { |
| 38 | Q_D(VpnConnection); |
| 39 | |
| 40 | QDBusConnection::systemBus().connect(service: NetworkManagerPrivate::DBUS_SERVICE, |
| 41 | path: d->path, |
| 42 | interface: NetworkManagerPrivate::FDO_DBUS_PROPERTIES, |
| 43 | name: QLatin1String("PropertiesChanged" ), |
| 44 | receiver: d, |
| 45 | SLOT(dbusPropertiesChanged(QString, QVariantMap, QStringList))); |
| 46 | connect(sender: &d->iface, signal: &OrgFreedesktopNetworkManagerVPNConnectionInterface::VpnStateChanged, context: d, slot: &VpnConnectionPrivate::vpnStateChanged); |
| 47 | |
| 48 | // We need to get ActiveConnection's properties, because by default every ActiveConnection |
| 49 | // is basically a VpnConnection |
| 50 | QVariantMap initialProperties = |
| 51 | NetworkManagerPrivate::retrieveInitialProperties(interfaceName: OrgFreedesktopNetworkManagerConnectionActiveInterface::staticInterfaceName(), path); |
| 52 | if (!initialProperties.isEmpty()) { |
| 53 | d->propertiesChanged(properties: initialProperties); |
| 54 | } |
| 55 | |
| 56 | // Try to retrieve VPN specific properties if this is a VPN connection |
| 57 | if (vpn()) { |
| 58 | // Get all VpnConnection's properties at once |
| 59 | QVariantMap initialProperties = NetworkManagerPrivate::retrieveInitialProperties(interfaceName: d->iface.staticInterfaceName(), path); |
| 60 | if (!initialProperties.isEmpty()) { |
| 61 | d->propertiesChanged(properties: initialProperties); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | NetworkManager::VpnConnection::~VpnConnection() |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | QString NetworkManager::VpnConnection::banner() const |
| 71 | { |
| 72 | Q_D(const VpnConnection); |
| 73 | // return d->banner; // FIXME NM doesn't Q_EMIT the Banner property change |
| 74 | return d->iface.banner(); |
| 75 | } |
| 76 | |
| 77 | NetworkManager::VpnConnection::State NetworkManager::VpnConnection::state() const |
| 78 | { |
| 79 | Q_D(const VpnConnection); |
| 80 | return d->state; |
| 81 | } |
| 82 | |
| 83 | void NetworkManager::VpnConnectionPrivate::dbusPropertiesChanged(const QString &interfaceName, |
| 84 | const QVariantMap &properties, |
| 85 | const QStringList &invalidatedProperties) |
| 86 | { |
| 87 | Q_UNUSED(invalidatedProperties); |
| 88 | |
| 89 | if (interfaceName == QLatin1String("org.freedesktop.NetworkManager.VPN.Connection" )) { |
| 90 | propertiesChanged(properties); |
| 91 | } else { |
| 92 | ActiveConnectionPrivate::propertiesChanged(properties); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | void NetworkManager::VpnConnectionPrivate::propertyChanged(const QString &property, const QVariant &value) |
| 97 | { |
| 98 | Q_Q(VpnConnection); |
| 99 | |
| 100 | if (property == QLatin1String("Banner" )) { |
| 101 | banner = value.toString(); |
| 102 | Q_EMIT q->bannerChanged(banner); |
| 103 | } else if (property == QLatin1String("VpnState" )) { |
| 104 | // Do not notify about changed VpnState twice, because there is also signal VpnStateChanged() from NetworkManager |
| 105 | state = NetworkManager::VpnConnectionPrivate::convertVpnConnectionState(state: value.toUInt()); |
| 106 | // NetworkManager::VpnConnection::StateChangeReason reason = |
| 107 | // NetworkManager::VpnConnectionPrivate::convertVpnConnectionStateReason(properties.key("Reason").toUInt()); Q_EMIT stateChanged(d->state, reason); |
| 108 | } else { |
| 109 | ActiveConnectionPrivate::propertyChanged(property, value); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void NetworkManager::VpnConnectionPrivate::vpnStateChanged(uint newState, uint reason) |
| 114 | { |
| 115 | Q_Q(VpnConnection); |
| 116 | Q_UNUSED(reason); |
| 117 | |
| 118 | state = NetworkManager::VpnConnectionPrivate::convertVpnConnectionState(state: newState); |
| 119 | NetworkManager::VpnConnection::StateChangeReason stateChangeReason = NetworkManager::VpnConnectionPrivate::convertVpnConnectionStateReason(reason); |
| 120 | Q_EMIT q->stateChanged(state, reason: stateChangeReason); |
| 121 | } |
| 122 | |
| 123 | NetworkManager::VpnConnection::operator VpnConnection *() |
| 124 | { |
| 125 | Q_D(VpnConnection); |
| 126 | if (d->vpn) { |
| 127 | return this; |
| 128 | } else { |
| 129 | return nullptr; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | #include "moc_vpnconnection.cpp" |
| 134 | #include "moc_vpnconnection_p.cpp" |
| 135 | |