1/*
2 SPDX-FileCopyrightText: 2012-2013 Jan Grulich <jgrulich@redhat.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "vpnsetting.h"
8#include "nmdebug.h"
9#include "vpnsetting_p.h"
10
11NetworkManager::VpnSettingPrivate::VpnSettingPrivate()
12 : name(NM_SETTING_VPN_SETTING_NAME)
13 , persistent(false)
14 , timeout(0)
15{
16}
17
18NetworkManager::VpnSetting::VpnSetting()
19 : Setting(Setting::Vpn)
20 , d_ptr(new VpnSettingPrivate())
21{
22}
23
24NetworkManager::VpnSetting::VpnSetting(const Ptr &other)
25 : Setting(other)
26 , d_ptr(new VpnSettingPrivate())
27{
28 setServiceType(other->serviceType());
29 setUsername(other->username());
30 setData(other->data());
31 setSecrets(other->secrets());
32 setPersistent(other->persistent());
33 setTimeout(other->timeout());
34}
35
36NetworkManager::VpnSetting::~VpnSetting()
37{
38 delete d_ptr;
39}
40
41QString NetworkManager::VpnSetting::name() const
42{
43 Q_D(const VpnSetting);
44
45 return d->name;
46}
47
48bool NetworkManager::VpnSetting::persistent() const
49{
50 Q_D(const VpnSetting);
51
52 return d->persistent;
53}
54
55void NetworkManager::VpnSetting::setPersistent(bool persistent)
56{
57 Q_D(VpnSetting);
58
59 d->persistent = persistent;
60}
61
62uint NetworkManager::VpnSetting::timeout() const
63{
64 Q_D(const VpnSetting);
65
66 return d->timeout;
67}
68
69void NetworkManager::VpnSetting::setTimeout(uint timeout)
70{
71 Q_D(VpnSetting);
72
73 d->timeout = timeout;
74}
75
76void NetworkManager::VpnSetting::setServiceType(const QString &type)
77{
78 Q_D(VpnSetting);
79
80 d->serviceType = type;
81}
82
83QString NetworkManager::VpnSetting::serviceType() const
84{
85 Q_D(const VpnSetting);
86
87 return d->serviceType;
88}
89
90void NetworkManager::VpnSetting::setUsername(const QString &username)
91{
92 Q_D(VpnSetting);
93
94 d->username = username;
95}
96
97QString NetworkManager::VpnSetting::username() const
98{
99 Q_D(const VpnSetting);
100
101 return d->username;
102}
103
104void NetworkManager::VpnSetting::setData(const NMStringMap &data)
105{
106 Q_D(VpnSetting);
107
108 d->data = data;
109}
110
111NMStringMap NetworkManager::VpnSetting::data() const
112{
113 Q_D(const VpnSetting);
114
115 return d->data;
116}
117
118void NetworkManager::VpnSetting::setSecrets(const NMStringMap &secrets)
119{
120 Q_D(VpnSetting);
121
122 d->secrets = secrets;
123}
124
125NMStringMap NetworkManager::VpnSetting::secrets() const
126{
127 Q_D(const VpnSetting);
128
129 return d->secrets;
130}
131
132void NetworkManager::VpnSetting::secretsFromMap(const QVariantMap &secrets)
133{
134 if (secrets.contains(key: QLatin1String(NM_SETTING_VPN_SECRETS))) {
135 setSecrets(qdbus_cast<NMStringMap>(v: secrets.value(key: QLatin1String(NM_SETTING_VPN_SECRETS))));
136 }
137}
138
139QVariantMap NetworkManager::VpnSetting::secretsToMap() const
140{
141 QVariantMap secretsMap;
142 if (!secrets().isEmpty()) {
143 secretsMap.insert(key: QLatin1String(NM_SETTING_VPN_SECRETS), value: QVariant::fromValue<NMStringMap>(value: secrets()));
144 }
145
146 // qCDebug(NMQT) << secretsMap;
147 return secretsMap;
148}
149
150void NetworkManager::VpnSetting::fromMap(const QVariantMap &setting)
151{
152 if (setting.contains(key: QLatin1String(NM_SETTING_VPN_SERVICE_TYPE))) {
153 setServiceType(setting.value(key: QLatin1String(NM_SETTING_VPN_SERVICE_TYPE)).toString());
154 }
155
156 if (setting.contains(key: QLatin1String(NM_SETTING_VPN_USER_NAME))) {
157 setUsername(setting.value(key: QLatin1String(NM_SETTING_VPN_USER_NAME)).toString());
158 }
159
160 if (setting.contains(key: QLatin1String(NM_SETTING_VPN_DATA))) {
161 setData(qdbus_cast<NMStringMap>(v: setting.value(key: QLatin1String(NM_SETTING_VPN_DATA))));
162 }
163
164 if (setting.contains(key: QLatin1String(NM_SETTING_VPN_SECRETS))) {
165 setSecrets(qdbus_cast<NMStringMap>(v: setting.value(key: QLatin1String(NM_SETTING_VPN_SECRETS))));
166 }
167
168 if (setting.contains(key: QLatin1String(NM_SETTING_VPN_PERSISTENT))) {
169 setPersistent(setting.value(key: QLatin1String(NM_SETTING_VPN_PERSISTENT)).toBool());
170 }
171
172 if (setting.contains(key: QLatin1String(NM_SETTING_VPN_TIMEOUT))) {
173 setTimeout(setting.value(key: QLatin1String(NM_SETTING_VPN_TIMEOUT)).toUInt());
174 }
175}
176
177QVariantMap NetworkManager::VpnSetting::toMap() const
178{
179 QVariantMap setting;
180
181 if (!serviceType().isEmpty()) {
182 setting.insert(key: QLatin1String(NM_SETTING_VPN_SERVICE_TYPE), value: serviceType());
183 }
184
185 if (!username().isEmpty()) {
186 setting.insert(key: QLatin1String(NM_SETTING_VPN_USER_NAME), value: username());
187 }
188
189 if (!data().isEmpty()) {
190 setting.insert(key: QLatin1String(NM_SETTING_VPN_DATA), value: QVariant::fromValue<NMStringMap>(value: data()));
191 }
192
193 if (!secrets().isEmpty()) {
194 setting.insert(key: QLatin1String(NM_SETTING_VPN_SECRETS), value: QVariant::fromValue<NMStringMap>(value: secrets()));
195 }
196
197 setting.insert(key: QLatin1String(NM_SETTING_VPN_PERSISTENT), value: persistent());
198 setting.insert(key: QLatin1String(NM_SETTING_VPN_TIMEOUT), value: timeout());
199
200 return setting;
201}
202
203void NetworkManager::VpnSetting::secretsFromStringMap(const NMStringMap &map)
204{
205 Q_D(VpnSetting);
206
207 if (map.contains(key: QLatin1String("VpnSecrets"))) {
208 const QStringList list = map.value(key: QLatin1String("VpnSecrets")).split(sep: "%SEP%");
209 if (list.count() % 2 == 0) {
210 for (int i = 0; i < list.count(); i += 2) {
211 if (d->secrets.contains(key: list[i])) {
212 d->secrets.remove(key: list[i]);
213 }
214 d->secrets.insert(key: list[i], value: list[i + 1]);
215 }
216 }
217 }
218}
219
220NMStringMap NetworkManager::VpnSetting::secretsToStringMap() const
221{
222 NMStringMap ret;
223 QStringList list;
224 QMap<QString, QString>::ConstIterator i = secrets().constBegin();
225 while (i != secrets().constEnd()) {
226 list << i.key() << i.value();
227 ++i;
228 }
229
230 ret.insert(key: QLatin1String("VpnSecrets"), value: list.join(sep: "%SEP%"));
231 return ret;
232}
233
234QDebug NetworkManager::operator<<(QDebug dbg, const NetworkManager::VpnSetting &setting)
235{
236 dbg.nospace() << "type: " << setting.typeAsString(type: setting.type()) << '\n';
237 dbg.nospace() << "initialized: " << !setting.isNull() << '\n';
238
239 dbg.nospace() << NM_SETTING_VPN_SERVICE_TYPE << ": " << setting.serviceType() << '\n';
240 dbg.nospace() << NM_SETTING_VPN_USER_NAME << ": " << setting.username() << '\n';
241 dbg.nospace() << NM_SETTING_VPN_DATA << ": " << setting.data() << '\n';
242 dbg.nospace() << NM_SETTING_VPN_PERSISTENT << ": " << setting.persistent() << '\n';
243 dbg.nospace() << NM_SETTING_VPN_TIMEOUT << ": " << setting.timeout() << '\n';
244
245 return dbg.maybeSpace();
246}
247

source code of networkmanager-qt/src/settings/vpnsetting.cpp