2
Copyright 2008 Will Stephenson <wstephenson@kde.org>
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License or (at your option) version 3 or any later version
8
accepted by the membership of KDE e.V. (or its successor approved
9
by the membership of KDE e.V.), which shall act as a proxy
10
defined in Section 14 of version 3 of the license.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "vpncwidget.h"
23
#include <nm-setting-vpn.h>
26
#include "nm-vpnc-service.h"
28
#include "connection.h"
30
class VpncSettingWidgetPrivate
34
Knm::VpnSetting * setting;
38
VpncSettingWidget::VpncSettingWidget(Knm::Connection * connection, QWidget * parent)
39
: SettingWidget(connection, parent), d_ptr(new VpncSettingWidgetPrivate)
41
Q_D(VpncSettingWidget);
44
d->ui.cboDHGroup->setCurrentIndex(1); // DH Group 2 default
45
d->setting = static_cast<Knm::VpnSetting *>(connection->setting(Knm::Setting::Vpn));
46
connect(d->ui.cboUserPassOptions, SIGNAL(currentIndexChanged(int)), this, SLOT(userPasswordTypeChanged(int)));
47
connect(d->ui.cboGroupPassOptions, SIGNAL(currentIndexChanged(int)), this, SLOT(groupPasswordTypeChanged(int)));
50
VpncSettingWidget::~VpncSettingWidget()
55
void VpncSettingWidget::userPasswordTypeChanged(int index)
57
Q_D(VpncSettingWidget);
58
d->ui.leUserPassword->setEnabled(index == 1);
61
void VpncSettingWidget::groupPasswordTypeChanged(int index)
63
Q_D(VpncSettingWidget);
64
d->ui.leGroupPassword->setEnabled(index == 1);
67
void VpncSettingWidget::readConfig()
69
Q_D(VpncSettingWidget);
71
QStringMap dataMap = d->setting->data();
73
QString gateway = dataMap[NM_VPNC_KEY_GATEWAY];
74
if (!gateway.isEmpty()) {
75
d->ui.leGateway->setText(gateway);
78
QString group = dataMap[NM_VPNC_KEY_ID];
79
if (!group.isEmpty()) {
80
d->ui.leGroupName->setText(group);
82
// password storage type is set in readSecrets
86
QString user = dataMap[NM_VPNC_KEY_XAUTH_USER];
87
if (!user.isEmpty()) {
88
d->ui.leUserName->setText(user);
92
QString domain = dataMap[NM_VPNC_KEY_DOMAIN];
93
if (!domain.isEmpty()) {
94
d->ui.leDomain->setText(domain);
98
if (dataMap[NM_VPNC_KEY_SINGLE_DES] == QLatin1String("yes")) {
99
d->ui.cboEncryptionMethod->setCurrentIndex(1);
100
} else if (dataMap[NM_VPNC_KEY_NO_ENCRYPTION] == QLatin1String("yes")) {
101
d->ui.cboEncryptionMethod->setCurrentIndex(2);
105
if (dataMap[NM_VPNC_KEY_NAT_TRAVERSAL_MODE] == NM_VPNC_NATT_MODE_NATT) {
106
d->ui.cboNatTraversal->setCurrentIndex(1);
107
} else if (dataMap[NM_VPNC_KEY_NAT_TRAVERSAL_MODE] == NM_VPNC_NATT_MODE_NONE) {
108
d->ui.cboNatTraversal->setCurrentIndex(2);
110
// dead peer detection
111
if (dataMap.contains(NM_VPNC_KEY_DPD_IDLE_TIMEOUT)) {
112
uint dpdTimeout = dataMap.value(NM_VPNC_KEY_DPD_IDLE_TIMEOUT).toUInt();
113
if (dpdTimeout == 0) {
114
d->ui.chkDeadPeerDetection->setChecked(false);
115
d->dpdTimeout = dpdTimeout;
119
if (dataMap.contains(NM_VPNC_KEY_DHGROUP)) {
120
uint dhGroup = dataMap.value(NM_VPNC_KEY_DHGROUP).toUInt();
122
case 1: // DH Group 1
123
d->ui.cboDHGroup->setCurrentIndex(0);
125
case 2: // DH Group 2
126
d->ui.cboDHGroup->setCurrentIndex(1);
128
case 5: // DH Group 5
129
d->ui.cboDHGroup->setCurrentIndex(2);
135
void VpncSettingWidget::fillOnePasswordCombo(QComboBox * combo, const QString & type, bool hasPassword)
137
if (!type.isNull()) {
138
if (type == QLatin1String(NM_VPN_PW_TYPE_SAVE)) {
139
combo->setCurrentIndex(1);
140
} else if (type == QLatin1String(NM_VPN_PW_TYPE_UNUSED)) {
141
combo->setCurrentIndex(2);
143
} else if (!hasPassword) {
144
combo->setCurrentIndex(1);
148
void VpncSettingWidget::writeConfig()
150
Q_D(VpncSettingWidget);
153
d->setting->setServiceType(QLatin1String(NM_DBUS_SERVICE_VPNC));
156
QStringMap secretData;
157
QStringMap secretsType;
161
if (!d->ui.leGateway->text().isEmpty()) {
162
data.insert(NM_VPNC_KEY_GATEWAY, d->ui.leGateway->text());
166
if (!d->ui.leGroupName->text().isEmpty()) {
167
data.insert(NM_VPNC_KEY_ID, d->ui.leGroupName->text());
171
if (!d->ui.leUserPassword->text().isEmpty() && d->ui.cboUserPassOptions->currentIndex() == 1) {
172
secretData.insert(NM_VPNC_KEY_XAUTH_PASSWORD, d->ui.leUserPassword->text());
175
if (!d->ui.leGroupPassword->text().isEmpty() && d->ui.cboGroupPassOptions->currentIndex() == 1) {
176
secretData.insert(NM_VPNC_KEY_SECRET, d->ui.leGroupPassword->text());
178
handleOnePasswordType(d->ui.cboUserPassOptions, NM_VPNC_KEY_XAUTH_PASSWORD, secretsType);
179
handleOnePasswordType(d->ui.cboGroupPassOptions, NM_VPNC_KEY_SECRET, secretsType);
183
if (!d->ui.leUserName->text().isEmpty()) {
184
data.insert(NM_VPNC_KEY_XAUTH_USER, d->ui.leUserName->text());
188
if (!d->ui.leDomain->text().isEmpty()) {
189
data.insert(NM_VPNC_KEY_DOMAIN, d->ui.leDomain->text());
193
switch (d->ui.cboEncryptionMethod->currentIndex()) {
195
data.insert(NM_VPNC_KEY_SINGLE_DES, QLatin1String("yes"));
198
data.insert(NM_VPNC_KEY_NO_ENCRYPTION, QLatin1String("yes"));
205
switch (d->ui.cboNatTraversal->currentIndex()) {
207
data.insert(NM_VPNC_KEY_NAT_TRAVERSAL_MODE, QLatin1String(NM_VPNC_NATT_MODE_NATT));
210
data.insert(NM_VPNC_KEY_NAT_TRAVERSAL_MODE, QLatin1String(NM_VPNC_NATT_MODE_NONE));
216
// dead peer detection
217
if (d->ui.chkDeadPeerDetection->isChecked()) {
218
if (d->dpdTimeout > 0) {
219
data.insert(NM_VPNC_KEY_DPD_IDLE_TIMEOUT, QString::number(d->dpdTimeout));
222
data.insert(NM_VPNC_KEY_DPD_IDLE_TIMEOUT, QString::number(0));
226
switch (d->ui.cboDHGroup->currentIndex()) {
227
case 0: // DH Group 1
228
data.insert(NM_VPNC_KEY_DHGROUP, "1");
230
case 1: // DH Group 2
231
data.insert(NM_VPNC_KEY_DHGROUP, "2");
233
case 2: // DH Group 5
234
data.insert(NM_VPNC_KEY_DHGROUP, "5");
238
d->setting->setData(data);
239
d->setting->setVpnSecrets(secretData);
240
d->setting->setSecretsStorageType(secretsType);
243
uint VpncSettingWidget::handleOnePasswordType(const QComboBox * combo, const QString & key, QStringMap & data)
245
uint type = combo->currentIndex();
248
data.insert(key, NM_VPN_PW_TYPE_ASK);
251
data.insert(key, NM_VPN_PW_TYPE_SAVE);
254
data.insert(key, NM_VPN_PW_TYPE_UNUSED);
260
void VpncSettingWidget::readSecrets()
262
Q_D(VpncSettingWidget);
263
QStringMap secrets = d->setting->vpnSecrets();
264
QStringMap secretsType = d->setting->secretsStorageType();
269
* First time "old" settings are loaded secretsType map is empty, so
270
* try to read from data as fallback
272
userType = secretsType.value(NM_VPNC_KEY_XAUTH_PASSWORD);
273
if (userType.isNull()) {
274
// TODO: change this to use the new Setting::secretsType instead of NM_VPNC_KEY_XAUTH_PASSWORD_TYPE.
275
userType = d->setting->data().value(NM_VPNC_KEY_XAUTH_PASSWORD_TYPE);
277
if (userType == QLatin1String(NM_VPN_PW_TYPE_SAVE)) {
278
d->ui.leUserPassword->setText(secrets.value(QLatin1String(NM_VPNC_KEY_XAUTH_PASSWORD)));
280
fillOnePasswordCombo(d->ui.cboUserPassOptions, userType, !d->ui.leUserName->text().isEmpty());
282
groupType = secretsType.value(NM_VPNC_KEY_SECRET);
283
if (groupType.isNull()) {
284
// TODO: change this to use the new Setting::secretsType instead of NM_VPNC_KEY_SECRET_TYPE.
285
groupType = d->setting->data().value(NM_VPNC_KEY_SECRET_TYPE);
287
if (groupType == QLatin1String(NM_VPN_PW_TYPE_SAVE)) {
288
d->ui.leGroupPassword->setText(secrets.value(QLatin1String(NM_VPNC_KEY_SECRET)));
290
fillOnePasswordCombo(d->ui.cboGroupPassOptions, groupType, !d->ui.leGroupPassword->text().isEmpty());
293
void VpncSettingWidget::validate()
297
// vim: sw=4 sts=4 et tw=100