~ubuntu-branches/ubuntu/precise/networkmanagement/precise

« back to all changes in this revision

Viewing changes to .pc/010_git-5e19d5bbd46ce19f32b8a038fa5121932e905732.patch/vpnplugins/vpnc/vpncwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-10-23 14:00:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20111023140013-e38hdzybcg6zndrk
Tags: 0.9~svngit.nm09.20111023.ff842e-0ubuntu1
* New upstream snapshot.
* Drop all patches, merged upstream.
* Add kubuntu_add_subdirectory_po.diff to build the translations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
Copyright 2008 Will Stephenson <wstephenson@kde.org>
3
 
 
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.
11
 
 
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.
16
 
 
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/>.
19
 
*/
20
 
 
21
 
#include "vpncwidget.h"
22
 
 
23
 
#include <nm-setting-vpn.h>
24
 
 
25
 
#include <QString>
26
 
#include "nm-vpnc-service.h"
27
 
 
28
 
#include "connection.h"
29
 
 
30
 
class VpncSettingWidgetPrivate
31
 
{
32
 
public:
33
 
    Ui_VpncProp ui;
34
 
    Knm::VpnSetting * setting;
35
 
    uint dpdTimeout;
36
 
};
37
 
 
38
 
VpncSettingWidget::VpncSettingWidget(Knm::Connection * connection, QWidget * parent)
39
 
: SettingWidget(connection, parent), d_ptr(new VpncSettingWidgetPrivate)
40
 
{
41
 
    Q_D(VpncSettingWidget);
42
 
    d->dpdTimeout = 0;
43
 
    d->ui.setupUi(this);
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)));
48
 
}
49
 
 
50
 
VpncSettingWidget::~VpncSettingWidget()
51
 
{
52
 
   delete d_ptr;
53
 
}
54
 
 
55
 
void VpncSettingWidget::userPasswordTypeChanged(int index)
56
 
{
57
 
    Q_D(VpncSettingWidget);
58
 
    d->ui.leUserPassword->setEnabled(index == 1);
59
 
}
60
 
 
61
 
void VpncSettingWidget::groupPasswordTypeChanged(int index)
62
 
{
63
 
    Q_D(VpncSettingWidget);
64
 
    d->ui.leGroupPassword->setEnabled(index == 1);
65
 
}
66
 
 
67
 
void VpncSettingWidget::readConfig()
68
 
{
69
 
    Q_D(VpncSettingWidget);
70
 
    // General settings
71
 
    QStringMap dataMap = d->setting->data();
72
 
    //   gateway
73
 
    QString gateway = dataMap[NM_VPNC_KEY_GATEWAY];
74
 
    if (!gateway.isEmpty()) {
75
 
        d->ui.leGateway->setText(gateway);
76
 
    }
77
 
    //   group name
78
 
    QString group = dataMap[NM_VPNC_KEY_ID];
79
 
    if (!group.isEmpty()) {
80
 
        d->ui.leGroupName->setText(group);
81
 
    }
82
 
    // password storage type is set in readSecrets
83
 
 
84
 
    // Optional settings
85
 
    //   username
86
 
    QString user = dataMap[NM_VPNC_KEY_XAUTH_USER];
87
 
    if (!user.isEmpty()) {
88
 
        d->ui.leUserName->setText(user);
89
 
    }
90
 
 
91
 
    //   domain
92
 
    QString domain = dataMap[NM_VPNC_KEY_DOMAIN];
93
 
    if (!domain.isEmpty()) {
94
 
        d->ui.leDomain->setText(domain);
95
 
    }
96
 
 
97
 
    //   encryption
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);
102
 
    }
103
 
 
104
 
    //   nat traversal
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);
109
 
    }
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;
116
 
        }
117
 
    }
118
 
    //  dh group
119
 
    if (dataMap.contains(NM_VPNC_KEY_DHGROUP)) {
120
 
        uint dhGroup = dataMap.value(NM_VPNC_KEY_DHGROUP).toUInt();
121
 
        switch (dhGroup) {
122
 
            case 1:     // DH Group 1
123
 
                d->ui.cboDHGroup->setCurrentIndex(0);
124
 
                break;
125
 
            case 2:     // DH Group 2
126
 
                d->ui.cboDHGroup->setCurrentIndex(1);
127
 
                break;
128
 
            case 5:     // DH Group 5
129
 
                d->ui.cboDHGroup->setCurrentIndex(2);
130
 
                break;
131
 
        }
132
 
    }
133
 
}
134
 
 
135
 
void VpncSettingWidget::fillOnePasswordCombo(QComboBox * combo, const QString & type, bool hasPassword)
136
 
{
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);
142
 
        }
143
 
    } else if (!hasPassword) {
144
 
        combo->setCurrentIndex(1);
145
 
    }
146
 
}
147
 
 
148
 
void VpncSettingWidget::writeConfig()
149
 
{
150
 
    Q_D(VpncSettingWidget);
151
 
    kDebug();
152
 
 
153
 
    d->setting->setServiceType(QLatin1String(NM_DBUS_SERVICE_VPNC));
154
 
 
155
 
    QStringMap data;
156
 
    QStringMap secretData;
157
 
    QStringMap secretsType;
158
 
 
159
 
    // General settings
160
 
    //   gateway
161
 
    if (!d->ui.leGateway->text().isEmpty()) {
162
 
        data.insert(NM_VPNC_KEY_GATEWAY, d->ui.leGateway->text());
163
 
    }
164
 
 
165
 
    //   group name
166
 
    if (!d->ui.leGroupName->text().isEmpty()) {
167
 
        data.insert(NM_VPNC_KEY_ID, d->ui.leGroupName->text());
168
 
    }
169
 
 
170
 
    //   user password
171
 
    if (!d->ui.leUserPassword->text().isEmpty() && d->ui.cboUserPassOptions->currentIndex() == 1) {
172
 
        secretData.insert(NM_VPNC_KEY_XAUTH_PASSWORD, d->ui.leUserPassword->text());
173
 
    }
174
 
    //   group password
175
 
    if (!d->ui.leGroupPassword->text().isEmpty() && d->ui.cboGroupPassOptions->currentIndex() == 1) {
176
 
        secretData.insert(NM_VPNC_KEY_SECRET, d->ui.leGroupPassword->text());
177
 
    }
178
 
    handleOnePasswordType(d->ui.cboUserPassOptions, NM_VPNC_KEY_XAUTH_PASSWORD, secretsType);
179
 
    handleOnePasswordType(d->ui.cboGroupPassOptions, NM_VPNC_KEY_SECRET, secretsType);
180
 
 
181
 
    // Optional settings
182
 
    //   username
183
 
    if (!d->ui.leUserName->text().isEmpty()) {
184
 
        data.insert(NM_VPNC_KEY_XAUTH_USER, d->ui.leUserName->text());
185
 
    }
186
 
 
187
 
    //   domain
188
 
    if (!d->ui.leDomain->text().isEmpty()) {
189
 
        data.insert(NM_VPNC_KEY_DOMAIN, d->ui.leDomain->text());
190
 
    }
191
 
 
192
 
    //   encryption
193
 
    switch (d->ui.cboEncryptionMethod->currentIndex()) {
194
 
        case 1:
195
 
            data.insert(NM_VPNC_KEY_SINGLE_DES, QLatin1String("yes"));
196
 
            break;
197
 
        case 2:
198
 
            data.insert(NM_VPNC_KEY_NO_ENCRYPTION, QLatin1String("yes"));
199
 
            break;
200
 
        default:
201
 
            break;
202
 
    }
203
 
 
204
 
    // nat traversal
205
 
    switch (d->ui.cboNatTraversal->currentIndex()) {
206
 
        case 1:
207
 
            data.insert(NM_VPNC_KEY_NAT_TRAVERSAL_MODE, QLatin1String(NM_VPNC_NATT_MODE_NATT));
208
 
            break;
209
 
        case 2:
210
 
            data.insert(NM_VPNC_KEY_NAT_TRAVERSAL_MODE, QLatin1String(NM_VPNC_NATT_MODE_NONE));
211
 
            break;
212
 
        default:
213
 
            break;
214
 
    }
215
 
 
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));
220
 
        }
221
 
    } else {
222
 
        data.insert(NM_VPNC_KEY_DPD_IDLE_TIMEOUT, QString::number(0));
223
 
    }
224
 
 
225
 
    // dh group
226
 
    switch (d->ui.cboDHGroup->currentIndex()) {
227
 
    case 0:     // DH Group 1
228
 
        data.insert(NM_VPNC_KEY_DHGROUP, "1");
229
 
        break;
230
 
    case 1:     // DH Group 2
231
 
        data.insert(NM_VPNC_KEY_DHGROUP, "2");
232
 
        break;
233
 
    case 2:     // DH Group 5
234
 
        data.insert(NM_VPNC_KEY_DHGROUP, "5");
235
 
        break;
236
 
    }
237
 
 
238
 
    d->setting->setData(data);
239
 
    d->setting->setVpnSecrets(secretData);
240
 
    d->setting->setSecretsStorageType(secretsType);
241
 
}
242
 
 
243
 
uint VpncSettingWidget::handleOnePasswordType(const QComboBox * combo, const QString & key, QStringMap & data)
244
 
{
245
 
    uint type = combo->currentIndex();
246
 
    switch (type) {
247
 
        case 0:
248
 
            data.insert(key, NM_VPN_PW_TYPE_ASK);
249
 
            break;
250
 
        case 1:
251
 
            data.insert(key, NM_VPN_PW_TYPE_SAVE);
252
 
            break;
253
 
        case 2:
254
 
            data.insert(key, NM_VPN_PW_TYPE_UNUSED);
255
 
            break;
256
 
    }
257
 
    return type;
258
 
}
259
 
 
260
 
void VpncSettingWidget::readSecrets()
261
 
{
262
 
    Q_D(VpncSettingWidget);
263
 
    QStringMap secrets = d->setting->vpnSecrets();
264
 
    QStringMap secretsType = d->setting->secretsStorageType();
265
 
    QString userType;
266
 
    QString groupType;
267
 
 
268
 
    /*
269
 
     * First time "old" settings are loaded secretsType map is empty, so
270
 
     * try to read from data as fallback
271
 
     */
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);
276
 
    }
277
 
    if (userType == QLatin1String(NM_VPN_PW_TYPE_SAVE)) {
278
 
        d->ui.leUserPassword->setText(secrets.value(QLatin1String(NM_VPNC_KEY_XAUTH_PASSWORD)));
279
 
    }
280
 
    fillOnePasswordCombo(d->ui.cboUserPassOptions, userType, !d->ui.leUserName->text().isEmpty());
281
 
 
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);
286
 
    }
287
 
    if (groupType == QLatin1String(NM_VPN_PW_TYPE_SAVE)) {
288
 
        d->ui.leGroupPassword->setText(secrets.value(QLatin1String(NM_VPNC_KEY_SECRET)));
289
 
    }
290
 
    fillOnePasswordCombo(d->ui.cboGroupPassOptions, groupType, !d->ui.leGroupPassword->text().isEmpty());
291
 
}
292
 
 
293
 
void VpncSettingWidget::validate()
294
 
{
295
 
 
296
 
}
297
 
// vim: sw=4 sts=4 et tw=100