diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -18,6 +18,8 @@ dhcp4config.cpp dhcp6config.cpp devicestatistics.cpp + dnsconfiguration.cpp + dnsdomain.cpp infinibanddevice.cpp ipaddress.cpp iproute.cpp @@ -161,6 +163,8 @@ DeviceStatistics Dhcp4Config Dhcp6Config + DnsConfiguration + DnsDomain GenericDevice GenericTypes GreDevice diff --git a/src/dnsconfiguration.h b/src/dnsconfiguration.h new file mode 100644 --- /dev/null +++ b/src/dnsconfiguration.h @@ -0,0 +1,124 @@ +/* + Copyright 2018 Aleksander Morgado + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#ifndef NETWORKMANAGERQT_DNSCONFIGURATION_H +#define NETWORKMANAGERQT_DNSCONFIGURATION_H + +#include +#include "dnsdomain.h" + +// To prevent signals in glib2 be defined by QT +#undef signals +#include +#if NM_CHECK_VERSION(1, 0, 0) +#include +#else +#include +#endif +#define signals Q_SIGNALS + +#include +#include + +namespace NetworkManager +{ + +/** + * This class represents IP configuration + */ +class NETWORKMANAGERQT_EXPORT DnsConfiguration +{ +public: + + /** + * Constructs an initialized DnsConfiguration object + */ + DnsConfiguration(const QStringList &searches, + const QStringList &options, + const QList domains); + + /** + * Constructs an empty DnsConfiguration object + */ + DnsConfiguration(); + + /** + * Destroys this DnsConfiguration object. + */ + ~DnsConfiguration(); + + /** + * Constructs a DnsConfiguration object that is a copy of the object @p other. + */ + DnsConfiguration(const DnsConfiguration &other); + + /** + * Returns the list of search domains + */ + QStringList searches() const; + + /** + * Sets the list of search domains + */ + void setSearches(const QStringList &list); + + /** + * Returns the list of resolver options + */ + QStringList options() const; + + /** + * Sets the list of resolver options + */ + void setOptions(const QStringList &list); + + /** + * Returns the list of domains + */ + QList domains() const; + + /** + * Sets the list of domains + */ + void setDomains(const QList &domains); + + /** + * Marshall into a map + */ + QVariantMap toMap() const; + + /** + * De-marshall from a map + */ + void fromMap (const QVariantMap &map); + + /** + * Makes a copy of the DnsConfiguration object @p other. + */ + DnsConfiguration &operator=(const DnsConfiguration &other); + +private: + class Private; + Private *const d; +}; + +} // namespace NetworkManager + +#endif // NETWORKMANAGERQT_DNSCONFIGURATION_H diff --git a/src/dnsconfiguration.cpp b/src/dnsconfiguration.cpp new file mode 100644 --- /dev/null +++ b/src/dnsconfiguration.cpp @@ -0,0 +1,153 @@ +/* + Copyright 2018 Aleksander Morgado + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include "ipconfig.h" + +#include + +#include "dnsconfiguration.h" + +namespace NetworkManager +{ + +class NetworkManager::DnsConfiguration::Private +{ +public: + Private(const QStringList &theSearches, + const QStringList &theOptions, + const QList theDomains) + : searches(theSearches) + , options(theOptions) + , domains(theDomains) + {} + Private() + {} + QStringList searches; + QStringList options; + QList domains; +}; + +} + +NetworkManager::DnsConfiguration::DnsConfiguration(const QStringList &searches, + const QStringList &options, + const QList domains) + : d(new Private(searches, options, domains)) +{ +} + +NetworkManager::DnsConfiguration::DnsConfiguration() + : d(new Private()) +{ +} + +NetworkManager::DnsConfiguration::DnsConfiguration(const DnsConfiguration &other) + : d(new Private) +{ + *this = other; +} + +NetworkManager::DnsConfiguration::~DnsConfiguration() +{ + delete d; +} + +QStringList NetworkManager::DnsConfiguration::searches() const +{ + return d->searches; +} + +void NetworkManager::DnsConfiguration::setSearches(const QStringList &searches) +{ + d->searches = searches; +} + +QStringList NetworkManager::DnsConfiguration::options() const +{ + return d->options; +} + +void NetworkManager::DnsConfiguration::setOptions(const QStringList &options) +{ + d->options = options; +} + +QList NetworkManager::DnsConfiguration::domains() const +{ + return d->domains; +} + +void NetworkManager::DnsConfiguration::setDomains(const QList &domains) +{ + d->domains = domains; +} + +QVariantMap NetworkManager::DnsConfiguration::toMap() const +{ + QVariantMap map; + + map["searches"] = d->searches; + map["options"] = d->options; + + QVariantMap domains; + Q_FOREACH (const NetworkManager::DnsDomain &domain, d->domains) { + QVariantMap contents; + QStringList serversList; + Q_FOREACH (const QHostAddress &address, domain.servers()) + serversList.append(address.toString()); + contents["servers"] = serversList; + contents["options"] = domain.options(); + domains[domain.name()] = contents; + } + map["domains"] = domains; + + return map; +} + +void NetworkManager::DnsConfiguration::fromMap (const QVariantMap &map) +{ + d->searches = map["searches"].toStringList(); + d->options = map["options"].toStringList(); + d->domains = QList(); + + QVariantMap domains = map["domains"].toMap(); + QVariantMap::const_iterator i = domains.constBegin(); + while (i != domains.constEnd()) { + QVariantMap contents = i.value().toMap(); + QList addressList; + Q_FOREACH (const QString &server, contents["servers"].toStringList()) + addressList.append(QHostAddress(server)); + NetworkManager::DnsDomain domain(i.key(), + addressList, + contents["options"].toStringList()); + d->domains.append(domain); + ++i; + } +} + +NetworkManager::DnsConfiguration &NetworkManager::DnsConfiguration::operator=(const DnsConfiguration &other) +{ + if (this == &other) { + return *this; + } + + *d = *other.d; + return *this; +} diff --git a/src/dnsdomain.h b/src/dnsdomain.h new file mode 100644 --- /dev/null +++ b/src/dnsdomain.h @@ -0,0 +1,115 @@ +/* + Copyright 2018 Aleksander Morgado + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#ifndef NETWORKMANAGERQT_DNSDOMAIN_H +#define NETWORKMANAGERQT_DNSDOMAIN_H + +#include +#include "ipaddress.h" +#include "iproute.h" + +// To prevent signals in glib2 be defined by QT +#undef signals +#include +#if NM_CHECK_VERSION(1, 0, 0) +#include +#else +#include +#endif +#define signals Q_SIGNALS + +#include +#include + +namespace NetworkManager +{ + +/** + * This class represents the configuration for a DNS domain + */ +class NETWORKMANAGERQT_EXPORT DnsDomain +{ +public: + + /** + * Constructs a DnsDomain object with a list of + */ + DnsDomain(const QString &name, + const QList &servers, + const QStringList &options); + + /** + * Constructs a DnsDomain object + */ + DnsDomain(); + + /** + * Destroys this DnsDomain object. + */ + ~DnsDomain(); + + /** + * Constructs a DnsDomain object that is a copy of the object @p other. + */ + DnsDomain(const DnsDomain &other); + + /** + * Returns the domain name + */ + QString name() const; + + /** + * Sets the domain name + */ + void setName(const QString &name); + + /** + * Returns the list of servers + */ + QList servers() const; + + /** + * Sets the list of servers + */ + void setServers(const QList &list); + + /** + * Returns the list of resolver options + */ + QStringList options() const; + + /** + * Sets the list of resolver options + */ + void setOptions(const QStringList &list); + + /** + * Makes a copy of the DnsDomain object @p other. + */ + DnsDomain &operator=(const DnsDomain &other); + +private: + class Private; + Private *const d; +}; + +} // namespace NetworkManager + +#endif // NETWORKMANAGERQT_DNSDOMAIN_H diff --git a/src/dnsdomain.cpp b/src/dnsdomain.cpp new file mode 100644 --- /dev/null +++ b/src/dnsdomain.cpp @@ -0,0 +1,110 @@ +/* + Copyright 2018 Aleksander Morgado + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) version 3, or any + later version accepted by the membership of KDE e.V. (or its + successor approved by the membership of KDE e.V.), which shall + act as a proxy defined in Section 6 of version 3 of the license. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library. If not, see . +*/ + +#include "ipconfig.h" + +#include + +#include "dnsdomain.h" + +namespace NetworkManager +{ + +class NetworkManager::DnsDomain::Private +{ +public: + Private(const QString &theName, + const QList &theServers, + const QStringList &theOptions) + : name(theName) + , servers(theServers) + , options(theOptions) + {} + Private() + {} + QString name; + QList servers; + QStringList options; +}; + +} + +NetworkManager::DnsDomain::DnsDomain(const QString &name, + const QList &servers, + const QStringList &options) + : d(new Private(name, servers, options)) +{ +} + +NetworkManager::DnsDomain::DnsDomain() + : d(new Private()) +{ +} + +NetworkManager::DnsDomain::DnsDomain(const DnsDomain &other) + : d(new Private) +{ + *this = other; +} + +NetworkManager::DnsDomain::~DnsDomain() +{ + delete d; +} + +QString NetworkManager::DnsDomain::name() const +{ + return d->name; +} + +void NetworkManager::DnsDomain::setName(const QString &name) +{ + d->name = name; +} + +QList NetworkManager::DnsDomain::servers() const +{ + return d->servers; +} + +void NetworkManager::DnsDomain::setServers(const QList &servers) +{ + d->servers = servers; +} + +QStringList NetworkManager::DnsDomain::options() const +{ + return d->options; +} + +void NetworkManager::DnsDomain::setOptions(const QStringList &options) +{ + d->options = options; +} + +NetworkManager::DnsDomain &NetworkManager::DnsDomain::operator=(const DnsDomain &other) +{ + if (this == &other) { + return *this; + } + + *d = *other.d; + return *this; +} diff --git a/src/manager.h b/src/manager.h --- a/src/manager.h +++ b/src/manager.h @@ -30,6 +30,7 @@ #include "device.h" #include "activeconnection.h" +#include "dnsconfiguration.h" /** * This class allows querying the underlying system to discover the available @@ -204,6 +205,14 @@ * @note never emitted in runtime NM < 1.0.6 */ void meteredChanged(NetworkManager::Device::MeteredStatus metered); + + /** + * Emitted when the global DNS configuration has changed + * @since 5.45.0 + * @see globalDnsConfiguration + * @note never emitted in runtime NM < 1.2.0 + */ + void globalDnsConfigurationChanged(const NetworkManager::DnsConfiguration &configuration); }; /** @@ -401,6 +410,19 @@ */ NETWORKMANAGERQT_EXPORT NetworkManager::Device::MeteredStatus metered(); +/** + * @return Gets the global DNS configuration. + * @since 5.45.0 + * @note always returns an empty configuration in runtime NM < 1.2.0 + */ +NETWORKMANAGERQT_EXPORT NetworkManager::DnsConfiguration globalDnsConfiguration(); + +/** + * @return Sets the global DNS configuration. + * @since 5.45.0 + */ +NETWORKMANAGERQT_EXPORT void setGlobalDnsConfiguration(const NetworkManager::DnsConfiguration &configuration); + /** * Find an ActiveConnection object for an active connection id * diff --git a/src/manager.cpp b/src/manager.cpp --- a/src/manager.cpp +++ b/src/manager.cpp @@ -86,6 +86,7 @@ , m_isWirelessHardwareEnabled(false) , m_isWwanEnabled(false) , m_isWwanHardwareEnabled(false) + , m_globalDnsConfiguration(NetworkManager::DnsConfiguration()) , m_supportedInterfaceTypes(NetworkManager::Device::UnknownType) { connect(&iface, &OrgFreedesktopNetworkManagerInterface::DeviceAdded, @@ -682,6 +683,17 @@ return checkVersion(1, 0, 6) ? m_metered : NetworkManager::Device::UnknownStatus; } +NetworkManager::DnsConfiguration NetworkManager::NetworkManagerPrivate::globalDnsConfiguration() const +{ + return m_globalDnsConfiguration; +} + +void NetworkManager::NetworkManagerPrivate::setGlobalDnsConfiguration(const NetworkManager::DnsConfiguration &configuration) +{ + m_globalDnsConfiguration = configuration; + iface.setGlobalDnsConfiguration(m_globalDnsConfiguration.toMap()); +} + void NetworkManager::NetworkManagerPrivate::onDeviceAdded(const QDBusObjectPath &objpath) { // qCDebug(NMQT); @@ -806,6 +818,9 @@ } else if (property == QLatin1String("Metered")) { m_metered = (NetworkManager::Device::MeteredStatus)it->toUInt(); Q_EMIT meteredChanged(m_metered); + } else if (property == QLatin1String("GlobalDnsConfiguration")) { + m_globalDnsConfiguration.fromMap(qdbus_cast(*it)); + Q_EMIT globalDnsConfigurationChanged(m_globalDnsConfiguration); } else { qCDebug(NMQT) << Q_FUNC_INFO << "Unhandled property" << property; } @@ -1108,6 +1123,16 @@ return globalNetworkManager->metered(); } +NetworkManager::DnsConfiguration NetworkManager::globalDnsConfiguration() +{ + return globalNetworkManager->globalDnsConfiguration(); +} + +void NetworkManager::setGlobalDnsConfiguration(const NetworkManager::DnsConfiguration &configuration) +{ + globalNetworkManager->setGlobalDnsConfiguration(configuration); +} + NetworkManager::Notifier *NetworkManager::notifier() { return globalNetworkManager; diff --git a/src/manager_p.h b/src/manager_p.h --- a/src/manager_p.h +++ b/src/manager_p.h @@ -67,6 +67,7 @@ QString m_primaryConnection; NetworkManager::ConnectionSettings::ConnectionType m_primaryConnectionType; NetworkManager::Device::MeteredStatus m_metered; + NetworkManager::DnsConfiguration m_globalDnsConfiguration; QString m_version; // to store NetworkManager's version. int m_x; @@ -120,6 +121,8 @@ NetworkManager::ConnectionSettings::ConnectionType primaryConnectionType(); bool isStartingUp() const; NetworkManager::Device::MeteredStatus metered() const; + NetworkManager::DnsConfiguration globalDnsConfiguration() const; + void setGlobalDnsConfiguration(const NetworkManager::DnsConfiguration &configuration); protected Q_SLOTS: void init(); void onDeviceAdded(const QDBusObjectPath &state);