1/*
2 SPDX-FileCopyrightText: 2018 Aleksander Morgado <aleksander@aleksander.es>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "ipconfig.h"
8
9#include <arpa/inet.h>
10
11#include "dnsconfiguration.h"
12
13#include <QVariant>
14
15namespace NetworkManager
16{
17class NetworkManager::DnsConfiguration::Private
18{
19public:
20 Private(const QStringList &theSearches, const QStringList &theOptions, const QList<DnsDomain> theDomains)
21 : searches(theSearches)
22 , options(theOptions)
23 , domains(theDomains)
24 {
25 }
26 Private()
27 {
28 }
29 QStringList searches;
30 QStringList options;
31 QList<DnsDomain> domains;
32};
33
34}
35
36NetworkManager::DnsConfiguration::DnsConfiguration(const QStringList &searches, const QStringList &options, const QList<DnsDomain> domains)
37 : d(new Private(searches, options, domains))
38{
39}
40
41NetworkManager::DnsConfiguration::DnsConfiguration()
42 : d(new Private())
43{
44}
45
46NetworkManager::DnsConfiguration::DnsConfiguration(const DnsConfiguration &other)
47 : d(new Private)
48{
49 *this = other;
50}
51
52NetworkManager::DnsConfiguration::~DnsConfiguration()
53{
54 delete d;
55}
56
57QStringList NetworkManager::DnsConfiguration::searches() const
58{
59 return d->searches;
60}
61
62void NetworkManager::DnsConfiguration::setSearches(const QStringList &searches)
63{
64 d->searches = searches;
65}
66
67QStringList NetworkManager::DnsConfiguration::options() const
68{
69 return d->options;
70}
71
72void NetworkManager::DnsConfiguration::setOptions(const QStringList &options)
73{
74 d->options = options;
75}
76
77QList<NetworkManager::DnsDomain> NetworkManager::DnsConfiguration::domains() const
78{
79 return d->domains;
80}
81
82void NetworkManager::DnsConfiguration::setDomains(const QList<NetworkManager::DnsDomain> &domains)
83{
84 d->domains = domains;
85}
86
87QVariantMap NetworkManager::DnsConfiguration::toMap() const
88{
89 QVariantMap map;
90
91 map["searches"] = d->searches;
92 map["options"] = d->options;
93
94 QVariantMap domains;
95 for (const NetworkManager::DnsDomain &domain : std::as_const(t&: d->domains)) {
96 QVariantMap contents;
97 QStringList serversList;
98 const QList<QHostAddress> servers = domain.servers();
99 for (const QHostAddress &address : servers) {
100 serversList.append(t: address.toString());
101 }
102 contents["servers"] = serversList;
103 contents["options"] = domain.options();
104 domains[domain.name()] = contents;
105 }
106 map["domains"] = domains;
107
108 return map;
109}
110
111void NetworkManager::DnsConfiguration::fromMap(const QVariantMap &map)
112{
113 d->searches = map["searches"].toStringList();
114 d->options = map["options"].toStringList();
115 d->domains = QList<NetworkManager::DnsDomain>();
116
117 QVariantMap domains = map["domains"].toMap();
118 QVariantMap::const_iterator i = domains.constBegin();
119 while (i != domains.constEnd()) {
120 const QVariantMap contents = i.value().toMap();
121 QList<QHostAddress> addressList;
122 const QStringList serversList = contents["servers"].toStringList();
123 for (const QString &server : serversList) {
124 addressList.append(t: QHostAddress(server));
125 }
126 NetworkManager::DnsDomain domain(i.key(), addressList, contents["options"].toStringList());
127 d->domains.append(t: domain);
128 ++i;
129 }
130}
131
132NetworkManager::DnsConfiguration &NetworkManager::DnsConfiguration::operator=(const DnsConfiguration &other)
133{
134 if (this == &other) {
135 return *this;
136 }
137
138 *d = *other.d;
139 return *this;
140}
141

source code of networkmanager-qt/src/dnsconfiguration.cpp