~ubuntu-branches/ubuntu/saucy/plasma-nm/saucy-proposed

« back to all changes in this revision

Viewing changes to vpn/vpnc/vpncwidget.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-08-16 19:07:09 UTC
  • Revision ID: package-import@ubuntu.com-20130816190709-ef9ydm9skigmg15l
Tags: upstream-0.0~git20130816
ImportĀ upstreamĀ versionĀ 0.0~git20130816

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2013 Lukas Tinkl <ltinkl@redhat.com>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Lesser General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2.1 of the License, or (at your option) version 3, or any
 
8
    later version accepted by the membership of KDE e.V. (or its
 
9
    successor approved by the membership of KDE e.V.), which shall
 
10
    act as a proxy defined in Section 6 of version 3 of the license.
 
11
 
 
12
    This library 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 GNU
 
15
    Lesser General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU Lesser General Public
 
18
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
19
*/
 
20
 
 
21
#include "vpncwidget.h"
 
22
#include "vpncadvancedwidget.h"
 
23
#include "ui_vpnc.h"
 
24
#include "nm-vpnc-service.h"
 
25
 
 
26
#include <QDBusMetaType>
 
27
#include <QDebug>
 
28
 
 
29
VpncWidget::VpncWidget(const NetworkManager::VpnSetting::Ptr &setting, QWidget* parent, Qt::WindowFlags f):
 
30
    SettingWidget(setting, parent, f),
 
31
    m_ui(new Ui::VpncWidget),
 
32
    m_setting(setting)
 
33
{
 
34
    qDBusRegisterMetaType<NMStringMap>();
 
35
 
 
36
    m_ui->setupUi(this);
 
37
 
 
38
    connect(m_ui->cboUserPasswordType, SIGNAL(currentIndexChanged(int)), SLOT(userPasswordTypeChanged(int)));
 
39
    connect(m_ui->cboGroupPasswordType, SIGNAL(currentIndexChanged(int)), SLOT(groupPasswordTypeChanged(int)));
 
40
    connect(m_ui->cbShowPasswords, SIGNAL(toggled(bool)), SLOT(showPasswords(bool)));
 
41
    connect(m_ui->btnAdvanced, SIGNAL(clicked()), SLOT(showAdvanced()));
 
42
 
 
43
    connect(m_ui->gateway, SIGNAL(textChanged(QString)), SLOT(slotWidgetChanged()));
 
44
 
 
45
    KAcceleratorManager::manage(this);
 
46
 
 
47
    if (m_setting)
 
48
        loadConfig(setting);
 
49
}
 
50
 
 
51
VpncWidget::~VpncWidget()
 
52
{
 
53
    m_tmpSetting.clear();
 
54
    delete m_ui;
 
55
}
 
56
 
 
57
void VpncWidget::loadConfig(const NetworkManager::Setting::Ptr &setting)
 
58
{
 
59
    Q_UNUSED(setting);
 
60
 
 
61
    const NMStringMap data = m_setting->data();
 
62
    const NMStringMap secrets = m_setting->secrets();
 
63
 
 
64
    const QString gateway = data.value(NM_VPNC_KEY_GATEWAY);
 
65
    if (!gateway.isEmpty())
 
66
        m_ui->gateway->setText(gateway);
 
67
 
 
68
    const QString user = data.value(NM_VPNC_KEY_XAUTH_USER);
 
69
    if (!user.isEmpty())
 
70
        m_ui->user->setText(user);
 
71
 
 
72
    const QString userPassword = secrets.value(NM_VPNC_KEY_XAUTH_PASSWORD);
 
73
    if (!userPassword.isEmpty())
 
74
        m_ui->userPassword->setText(userPassword);
 
75
 
 
76
    const NetworkManager::Setting::SecretFlags userPassType =
 
77
            static_cast<NetworkManager::Setting::SecretFlags>(data.value(NM_VPNC_KEY_XAUTH_PASSWORD"-flags").toInt());
 
78
    if (userPassType.testFlag(NetworkManager::Setting::NotSaved))
 
79
        m_ui->cboUserPasswordType->setCurrentIndex(SettingWidget::EnumPasswordStorageType::AlwaysAsk);
 
80
    else if (userPassType.testFlag(NetworkManager::Setting::NotRequired))
 
81
        m_ui->cboUserPasswordType->setCurrentIndex(SettingWidget::EnumPasswordStorageType::NotRequired);
 
82
    else
 
83
        m_ui->cboUserPasswordType->setCurrentIndex(SettingWidget::EnumPasswordStorageType::Store);
 
84
 
 
85
    const QString groupName = data.value(NM_VPNC_KEY_ID);
 
86
    if (!groupName.isEmpty())
 
87
        m_ui->group->setText(groupName);
 
88
 
 
89
    const QString groupPassword = secrets.value(NM_VPNC_KEY_SECRET);
 
90
    if (!groupPassword.isEmpty())
 
91
        m_ui->groupPassword->setText(groupPassword);
 
92
 
 
93
    const NetworkManager::Setting::SecretFlags groupPassType =
 
94
            static_cast<NetworkManager::Setting::SecretFlags>(data.value(NM_VPNC_KEY_SECRET"-flags").toInt());
 
95
    if (groupPassType.testFlag(NetworkManager::Setting::NotSaved))
 
96
        m_ui->cboGroupPasswordType->setCurrentIndex(SettingWidget::EnumPasswordStorageType::AlwaysAsk);
 
97
    else if (groupPassType.testFlag(NetworkManager::Setting::NotRequired))
 
98
        m_ui->cboGroupPasswordType->setCurrentIndex(SettingWidget::EnumPasswordStorageType::NotRequired);
 
99
    else
 
100
        m_ui->cboGroupPasswordType->setCurrentIndex(SettingWidget::EnumPasswordStorageType::Store);
 
101
 
 
102
    if (data.value(NM_VPNC_KEY_AUTHMODE) == QLatin1String("hybrid")) {
 
103
        m_ui->useHybridAuth->setChecked(true);
 
104
        m_ui->caFile->setUrl(KUrl(data.value(NM_VPNC_KEY_CA_FILE)));
 
105
    }
 
106
}
 
107
 
 
108
QVariantMap VpncWidget::setting(bool agentOwned) const
 
109
{
 
110
    NetworkManager::VpnSetting setting;
 
111
    setting.setServiceType(QLatin1String(NM_DBUS_SERVICE_VPNC));
 
112
    NMStringMap data;
 
113
    if (!m_tmpSetting.isNull()) {
 
114
        data = m_tmpSetting->data();
 
115
    }
 
116
    NMStringMap secrets;
 
117
 
 
118
    if (!m_ui->gateway->text().isEmpty())
 
119
        data.insert(NM_VPNC_KEY_GATEWAY, m_ui->gateway->text());
 
120
 
 
121
    if (!m_ui->user->text().isEmpty())
 
122
        data.insert(NM_VPNC_KEY_XAUTH_USER, m_ui->user->text());
 
123
 
 
124
    if (m_ui->userPassword->isEnabled() && !m_ui->userPassword->text().isEmpty())
 
125
        secrets.insert(NM_VPNC_KEY_XAUTH_PASSWORD, m_ui->userPassword->text());
 
126
 
 
127
    const int userPasswordTypeIndex =  m_ui->cboUserPasswordType->currentIndex();
 
128
    if (userPasswordTypeIndex == SettingWidget::EnumPasswordStorageType::AlwaysAsk) {
 
129
        data.insert(NM_VPNC_KEY_XAUTH_PASSWORD"-flags", QString::number(NetworkManager::Setting::NotSaved));
 
130
    } else if (userPasswordTypeIndex == SettingWidget::EnumPasswordStorageType::NotRequired) {
 
131
        data.insert(NM_VPNC_KEY_XAUTH_PASSWORD"-flags", QString::number(NetworkManager::Setting::NotRequired));
 
132
    } else { // none
 
133
        if (agentOwned) {
 
134
            data.insert(NM_VPNC_KEY_XAUTH_PASSWORD"-flags", QString::number(NetworkManager::Setting::AgentOwned));
 
135
        } else {
 
136
            data.insert(NM_VPNC_KEY_XAUTH_PASSWORD"-flags", QString::number(NetworkManager::Setting::None));
 
137
        }
 
138
    }
 
139
 
 
140
    if (!m_ui->group->text().isEmpty())
 
141
        data.insert(NM_VPNC_KEY_ID, m_ui->group->text());
 
142
 
 
143
    if (m_ui->groupPassword->isEnabled() && !m_ui->groupPassword->text().isEmpty())
 
144
        secrets.insert(NM_VPNC_KEY_SECRET, m_ui->groupPassword->text());
 
145
 
 
146
    const int groupPasswordTypeIndex =  m_ui->cboGroupPasswordType->currentIndex();
 
147
    if (groupPasswordTypeIndex == SettingWidget::EnumPasswordStorageType::AlwaysAsk) {
 
148
        data.insert(NM_VPNC_KEY_SECRET"-flags", QString::number(NetworkManager::Setting::NotSaved));
 
149
    } else if (groupPasswordTypeIndex == SettingWidget::EnumPasswordStorageType::NotRequired) {
 
150
        data.insert(NM_VPNC_KEY_SECRET"-flags", QString::number(NetworkManager::Setting::NotRequired));
 
151
    } else { // none
 
152
        if (agentOwned) {
 
153
            data.insert(NM_VPNC_KEY_SECRET"-flags", QString::number(NetworkManager::Setting::AgentOwned));
 
154
        } else {
 
155
            data.insert(NM_VPNC_KEY_SECRET"-flags", QString::number(NetworkManager::Setting::None));
 
156
        }
 
157
    }
 
158
 
 
159
    if (m_ui->useHybridAuth->isChecked() && !m_ui->caFile->url().isEmpty()) {
 
160
        data.insert(NM_VPNC_KEY_AUTHMODE, "hybrid");
 
161
        data.insert(NM_VPNC_KEY_CA_FILE, m_ui->caFile->url().url());
 
162
    }
 
163
 
 
164
    setting.setData(data);
 
165
    setting.setSecrets(secrets);
 
166
    return setting.toMap();
 
167
}
 
168
 
 
169
void VpncWidget::userPasswordTypeChanged(int index)
 
170
{
 
171
    m_ui->userPassword->setEnabled(index == SettingWidget::EnumPasswordStorageType::Store);
 
172
}
 
173
 
 
174
void VpncWidget::groupPasswordTypeChanged(int index)
 
175
{
 
176
    m_ui->groupPassword->setEnabled(index == SettingWidget::EnumPasswordStorageType::Store);
 
177
}
 
178
 
 
179
void VpncWidget::showPasswords(bool show)
 
180
{
 
181
    m_ui->userPassword->setPasswordMode(!show);
 
182
    m_ui->groupPassword->setPasswordMode(!show);
 
183
}
 
184
 
 
185
void VpncWidget::showAdvanced()
 
186
{
 
187
    QPointer<VpncAdvancedWidget> adv;
 
188
    if (m_tmpSetting.isNull()) {
 
189
        adv = new VpncAdvancedWidget(m_setting, this);
 
190
    } else {
 
191
        adv = new VpncAdvancedWidget(m_tmpSetting, this);
 
192
    }
 
193
    if (adv->exec() == QDialog::Accepted) {
 
194
        NMStringMap advData = adv->setting();
 
195
        if (!advData.isEmpty()) {
 
196
            if (m_tmpSetting.isNull()) {
 
197
                m_tmpSetting = NetworkManager::VpnSetting::Ptr(new NetworkManager::VpnSetting);
 
198
            }
 
199
            m_tmpSetting->setData(advData);
 
200
        }
 
201
    }
 
202
 
 
203
    if (adv) {
 
204
        adv->deleteLater();
 
205
    }
 
206
}
 
207
 
 
208
bool VpncWidget::isValid() const
 
209
{
 
210
    return !m_ui->gateway->text().isEmpty();
 
211
}