| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QNETWORKMANAGERSERVICE_H |
| 5 | #define QNETWORKMANAGERSERVICE_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore/qvariant.h> |
| 19 | #include <QtCore/qmap.h> |
| 20 | #include <QtDBus/qdbusabstractinterface.h> |
| 21 | |
| 22 | #include <optional> |
| 23 | |
| 24 | // Matches 'NMDeviceState' from https://developer.gnome.org/NetworkManager/stable/nm-dbus-types.html |
| 25 | enum NMDeviceState { |
| 26 | NM_DEVICE_STATE_UNKNOWN = 0, |
| 27 | NM_DEVICE_STATE_UNMANAGED = 10, |
| 28 | NM_DEVICE_STATE_UNAVAILABLE = 20, |
| 29 | NM_DEVICE_STATE_DISCONNECTED = 30, |
| 30 | NM_DEVICE_STATE_PREPARE = 40, |
| 31 | NM_DEVICE_STATE_CONFIG = 50, |
| 32 | NM_DEVICE_STATE_NEED_AUTH = 60, |
| 33 | NM_DEVICE_STATE_IP_CONFIG = 70, |
| 34 | NM_DEVICE_STATE_ACTIVATED = 100, |
| 35 | NM_DEVICE_STATE_DEACTIVATING = 110, |
| 36 | NM_DEVICE_STATE_FAILED = 120 |
| 37 | }; |
| 38 | |
| 39 | QT_BEGIN_NAMESPACE |
| 40 | |
| 41 | class QDBusObjectPath; |
| 42 | class QNetworkManagerNetworkInformationBackend; |
| 43 | |
| 44 | // This tiny class exists for the purpose of seeing if NetworkManager is available without |
| 45 | // initializing everything the derived/full class needs. |
| 46 | class QNetworkManagerInterfaceBase : public QDBusAbstractInterface |
| 47 | { |
| 48 | Q_OBJECT |
| 49 | public: |
| 50 | explicit QNetworkManagerInterfaceBase(QObject *parent = nullptr); |
| 51 | ~QNetworkManagerInterfaceBase() = default; |
| 52 | |
| 53 | static bool networkManagerAvailable(); |
| 54 | |
| 55 | private: |
| 56 | Q_DISABLE_COPY_MOVE(QNetworkManagerInterfaceBase) |
| 57 | }; |
| 58 | |
| 59 | class QNetworkManagerInterface final : public QNetworkManagerInterfaceBase |
| 60 | { |
| 61 | Q_OBJECT |
| 62 | |
| 63 | public: |
| 64 | // Matches 'NMState' from https://developer.gnome.org/NetworkManager/stable/nm-dbus-types.html |
| 65 | enum NMState { |
| 66 | NM_STATE_UNKNOWN = 0, |
| 67 | NM_STATE_ASLEEP = 10, |
| 68 | NM_STATE_DISCONNECTED = 20, |
| 69 | NM_STATE_DISCONNECTING = 30, |
| 70 | NM_STATE_CONNECTING = 40, |
| 71 | NM_STATE_CONNECTED_LOCAL = 50, |
| 72 | NM_STATE_CONNECTED_SITE = 60, |
| 73 | NM_STATE_CONNECTED_GLOBAL = 70 |
| 74 | }; |
| 75 | Q_ENUM(NMState) |
| 76 | // Matches 'NMConnectivityState' from |
| 77 | // https://developer.gnome.org/NetworkManager/stable/nm-dbus-types.html#NMConnectivityState |
| 78 | enum NMConnectivityState { |
| 79 | NM_CONNECTIVITY_UNKNOWN = 0, |
| 80 | NM_CONNECTIVITY_NONE = 1, |
| 81 | NM_CONNECTIVITY_PORTAL = 2, |
| 82 | NM_CONNECTIVITY_LIMITED = 3, |
| 83 | NM_CONNECTIVITY_FULL = 4, |
| 84 | }; |
| 85 | Q_ENUM(NMConnectivityState) |
| 86 | // Matches 'NMDeviceType' from |
| 87 | // https://developer-old.gnome.org/NetworkManager/stable/nm-dbus-types.html#NMDeviceType |
| 88 | enum NMDeviceType { |
| 89 | NM_DEVICE_TYPE_UNKNOWN = 0, |
| 90 | NM_DEVICE_TYPE_GENERIC = 14, |
| 91 | NM_DEVICE_TYPE_ETHERNET = 1, |
| 92 | NM_DEVICE_TYPE_WIFI = 2, |
| 93 | NM_DEVICE_TYPE_UNUSED1 = 3, |
| 94 | NM_DEVICE_TYPE_UNUSED2 = 4, |
| 95 | NM_DEVICE_TYPE_BT = 5, |
| 96 | NM_DEVICE_TYPE_OLPC_MESH = 6, |
| 97 | NM_DEVICE_TYPE_WIMAX = 7, |
| 98 | NM_DEVICE_TYPE_MODEM = 8, |
| 99 | NM_DEVICE_TYPE_INFINIBAND = 9, |
| 100 | NM_DEVICE_TYPE_BOND = 10, |
| 101 | NM_DEVICE_TYPE_VLAN = 11, |
| 102 | NM_DEVICE_TYPE_ADSL = 12, |
| 103 | NM_DEVICE_TYPE_BRIDGE = 13, |
| 104 | NM_DEVICE_TYPE_TEAM = 15, |
| 105 | NM_DEVICE_TYPE_TUN = 16, |
| 106 | NM_DEVICE_TYPE_IP_TUNNEL = 17, |
| 107 | NM_DEVICE_TYPE_MACVLAN = 18, |
| 108 | NM_DEVICE_TYPE_VXLAN = 19, |
| 109 | NM_DEVICE_TYPE_VETH = 20, |
| 110 | NM_DEVICE_TYPE_MACSEC = 21, |
| 111 | NM_DEVICE_TYPE_DUMMY = 22, |
| 112 | NM_DEVICE_TYPE_PPP = 23, |
| 113 | NM_DEVICE_TYPE_OVS_INTERFACE = 24, |
| 114 | NM_DEVICE_TYPE_OVS_PORT = 25, |
| 115 | NM_DEVICE_TYPE_OVS_BRIDGE = 26, |
| 116 | NM_DEVICE_TYPE_WPAN = 27, |
| 117 | NM_DEVICE_TYPE_6LOWPAN = 28, |
| 118 | NM_DEVICE_TYPE_WIREGUARD = 29, |
| 119 | NM_DEVICE_TYPE_WIFI_P2P = 30, |
| 120 | NM_DEVICE_TYPE_VRF = 31, |
| 121 | }; |
| 122 | // Matches 'NMMetered' from |
| 123 | // https://developer-old.gnome.org/NetworkManager/stable/nm-dbus-types.html#NMMetered |
| 124 | enum NMMetered { |
| 125 | NM_METERED_UNKNOWN, |
| 126 | NM_METERED_YES, |
| 127 | NM_METERED_NO, |
| 128 | NM_METERED_GUESS_YES, |
| 129 | NM_METERED_GUESS_NO, |
| 130 | }; |
| 131 | |
| 132 | explicit QNetworkManagerInterface(QObject *parent = nullptr); |
| 133 | ~QNetworkManagerInterface(); |
| 134 | |
| 135 | void setBackend(QNetworkManagerNetworkInformationBackend *ourBackend); |
| 136 | |
| 137 | NMState state() const; |
| 138 | NMConnectivityState connectivityState() const; |
| 139 | NMDeviceType deviceType() const; |
| 140 | NMMetered meteredState() const; |
| 141 | |
| 142 | bool isValid() const { return QDBusAbstractInterface::isValid() && validDBusConnection; } |
| 143 | |
| 144 | private Q_SLOTS: |
| 145 | void setProperties(const QString &interfaceName, const QMap<QString, QVariant> &map, |
| 146 | const QStringList &invalidatedProperties); |
| 147 | |
| 148 | private: |
| 149 | Q_DISABLE_COPY_MOVE(QNetworkManagerInterface) |
| 150 | |
| 151 | NMDeviceType (const QDBusObjectPath &devicePath) const; |
| 152 | NMMetered (const QDBusObjectPath &devicePath) const; |
| 153 | |
| 154 | std::optional<QDBusObjectPath> primaryConnectionDevicePath() const; |
| 155 | |
| 156 | QVariantMap propertyMap; |
| 157 | QNetworkManagerNetworkInformationBackend *backend = nullptr; |
| 158 | bool validDBusConnection = true; |
| 159 | }; |
| 160 | |
| 161 | class PropertiesDBusInterface : public QDBusAbstractInterface |
| 162 | { |
| 163 | public: |
| 164 | PropertiesDBusInterface(const QString &service, const QString &path, const QString &interface, |
| 165 | const QDBusConnection &connection, QObject *parent = nullptr) |
| 166 | : QDBusAbstractInterface(service, path, interface.toLatin1().data(), connection, parent) |
| 167 | { |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | QT_END_NAMESPACE |
| 172 | |
| 173 | #endif |
| 174 | |