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 | |
15 | namespace NetworkManager |
16 | { |
17 | class NetworkManager::DnsConfiguration::Private |
18 | { |
19 | public: |
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 | |
36 | NetworkManager::DnsConfiguration::DnsConfiguration(const QStringList &searches, const QStringList &options, const QList<DnsDomain> domains) |
37 | : d(new Private(searches, options, domains)) |
38 | { |
39 | } |
40 | |
41 | NetworkManager::DnsConfiguration::DnsConfiguration() |
42 | : d(new Private()) |
43 | { |
44 | } |
45 | |
46 | NetworkManager::DnsConfiguration::DnsConfiguration(const DnsConfiguration &other) |
47 | : d(new Private) |
48 | { |
49 | *this = other; |
50 | } |
51 | |
52 | NetworkManager::DnsConfiguration::~DnsConfiguration() |
53 | { |
54 | delete d; |
55 | } |
56 | |
57 | QStringList NetworkManager::DnsConfiguration::searches() const |
58 | { |
59 | return d->searches; |
60 | } |
61 | |
62 | void NetworkManager::DnsConfiguration::setSearches(const QStringList &searches) |
63 | { |
64 | d->searches = searches; |
65 | } |
66 | |
67 | QStringList NetworkManager::DnsConfiguration::options() const |
68 | { |
69 | return d->options; |
70 | } |
71 | |
72 | void NetworkManager::DnsConfiguration::setOptions(const QStringList &options) |
73 | { |
74 | d->options = options; |
75 | } |
76 | |
77 | QList<NetworkManager::DnsDomain> NetworkManager::DnsConfiguration::domains() const |
78 | { |
79 | return d->domains; |
80 | } |
81 | |
82 | void NetworkManager::DnsConfiguration::setDomains(const QList<NetworkManager::DnsDomain> &domains) |
83 | { |
84 | d->domains = domains; |
85 | } |
86 | |
87 | QVariantMap 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 | |
111 | void 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 | |
132 | NetworkManager::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 | |