Changeset View
Changeset View
Standalone View
Standalone View
kded/passworddialog.cpp
Show All 16 Lines | 1 | /* | |||
---|---|---|---|---|---|
17 | 17 | | |||
18 | You should have received a copy of the GNU Lesser General Public | 18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "debug.h" | 22 | #include "debug.h" | ||
23 | #include "passworddialog.h" | 23 | #include "passworddialog.h" | ||
24 | #include "ui_passworddialog.h" | 24 | #include "ui_passworddialog.h" | ||
25 | #include "uiutils.h" | ||||
25 | 26 | | |||
26 | #include <vpnuiplugin.h> | 27 | #include <vpnuiplugin.h> | ||
27 | 28 | | |||
28 | #include <NetworkManagerQt/WirelessSetting> | 29 | #include <NetworkManagerQt/WirelessSetting> | ||
29 | #include <NetworkManagerQt/VpnSetting> | 30 | #include <NetworkManagerQt/VpnSetting> | ||
30 | 31 | | |||
31 | #include <KServiceTypeTrader> | 32 | #include <KServiceTypeTrader> | ||
32 | #include <KLocalizedString> | 33 | #include <KLocalizedString> | ||
33 | #include <KIconLoader> | 34 | #include <KIconLoader> | ||
34 | 35 | | |||
35 | #include <QIcon> | 36 | #include <QIcon> | ||
37 | #include <QPushButton> | ||||
36 | 38 | | |||
37 | using namespace NetworkManager; | 39 | using namespace NetworkManager; | ||
38 | 40 | | |||
39 | PasswordDialog::PasswordDialog(const NMVariantMapMap &connection, SecretAgent::GetSecretsFlags flags, | 41 | PasswordDialog::PasswordDialog(const NetworkManager::ConnectionSettings::Ptr &connectionSettings, SecretAgent::GetSecretsFlags flags, | ||
40 | const QString &setting_name, QWidget *parent) : | 42 | const QString &setting_name, QWidget *parent) : | ||
41 | QDialog(parent), | 43 | QDialog(parent), | ||
42 | ui(0), | 44 | m_ui(0), | ||
43 | vpnWidget(0), | | |||
44 | m_connection(connection), | | |||
45 | m_flags(flags), | | |||
46 | m_settingName(setting_name), | | |||
47 | m_hasError(false), | 45 | m_hasError(false), | ||
48 | m_error(SecretAgent::NoSecrets) | 46 | m_settingName(setting_name), | ||
47 | m_connectionSettings(connectionSettings), | ||||
48 | m_error(SecretAgent::NoSecrets), | ||||
49 | m_flags(flags), | ||||
50 | m_vpnWidget(0) | ||||
49 | { | 51 | { | ||
50 | setWindowIcon(QIcon::fromTheme(QStringLiteral("dialog-password"))); | 52 | setWindowIcon(QIcon::fromTheme(QStringLiteral("dialog-password"))); | ||
53 | | ||||
54 | initializeUi(); | ||||
51 | } | 55 | } | ||
52 | 56 | | |||
53 | PasswordDialog::~PasswordDialog() | 57 | PasswordDialog::~PasswordDialog() | ||
54 | { | 58 | { | ||
55 | delete ui; | 59 | delete m_ui; | ||
56 | } | 60 | } | ||
57 | 61 | | |||
58 | void PasswordDialog::setupGenericUi(const ConnectionSettings &connectionSettings) | 62 | void PasswordDialog::initializeUi() | ||
59 | { | 63 | { | ||
60 | Setting::Ptr setting = connectionSettings.setting(m_settingName); | 64 | m_ui = new Ui::PasswordDialog; | ||
61 | 65 | m_ui->setupUi(this); | |||
62 | ui = new Ui::PasswordDialog; | | |||
63 | ui->setupUi(this); | | |||
64 | // TODO fix this for high DPI | 66 | // TODO fix this for high DPI | ||
65 | ui->labelIcon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-password")).pixmap(KIconLoader::SizeMedium)); | 67 | m_ui->labelIcon->setPixmap(QIcon::fromTheme(QStringLiteral("dialog-password")).pixmap(KIconLoader::SizeHuge)); | ||
68 | m_ui->labelHeadline->setText(i18n("Authenticate %1", m_connectionSettings->id())); | ||||
69 | | ||||
70 | connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &PasswordDialog::accept); | ||||
71 | connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &PasswordDialog::reject); | ||||
66 | 72 | | |||
73 | if (m_connectionSettings->connectionType() != NetworkManager::ConnectionSettings::Vpn) { | ||||
74 | NetworkManager::Setting::Ptr setting = m_connectionSettings->setting(m_settingName); | ||||
67 | m_neededSecrets = setting->needSecrets(m_flags & SecretAgent::RequestNew); | 75 | m_neededSecrets = setting->needSecrets(m_flags & SecretAgent::RequestNew); | ||
76 | | ||||
68 | if (m_neededSecrets.isEmpty()) { | 77 | if (m_neededSecrets.isEmpty()) { | ||
69 | qCWarning(PLASMA_NM) << "list of secrets is empty!!!"; | 78 | qCWarning(PLASMA_NM) << "list of secrets is empty!!!"; | ||
70 | m_hasError = true; | 79 | m_hasError = true; | ||
71 | m_error = SecretAgent::InternalError; | 80 | m_error = SecretAgent::InternalError; | ||
72 | m_errorMessage = QLatin1String("No secrets were requested"); | 81 | m_errorMessage = QLatin1String("No secrets were requested"); | ||
73 | return; | 82 | return; | ||
74 | } | 83 | } | ||
75 | 84 | | |||
76 | WirelessSetting::Ptr wifi = connectionSettings.setting(Setting::Wireless).dynamicCast<WirelessSetting>(); | 85 | WirelessSetting::Ptr wifi = m_connectionSettings->setting(Setting::Wireless).dynamicCast<WirelessSetting>(); | ||
77 | | ||||
78 | Setting::SettingType connectionType = setting->type(); | 86 | Setting::SettingType connectionType = setting->type(); | ||
79 | if (wifi && (connectionType == Setting::WirelessSecurity || connectionType == Setting::Security8021x)) { | 87 | if (wifi && (connectionType == Setting::WirelessSecurity || connectionType == Setting::Security8021x)) { | ||
80 | const QString ssid = QString::fromUtf8(wifi->ssid()); | 88 | const QString ssid = QString::fromUtf8(wifi->ssid()); | ||
81 | ui->labelText->setText(i18n("For accessing the wireless network '%1' you need to provide a password below", ssid)); | 89 | m_ui->labelText->setText(i18n("For accessing the wireless network <b>%1</b> you need to provide a password below:", ssid)); | ||
82 | } else { | 90 | } else { | ||
83 | ui->labelText->setText(i18n("Please provide the password for activating connection '%1'", connectionSettings.id())); | 91 | m_ui->labelText->setText(i18n("Please provide the password for activating connection <b>%1</b>:", m_connectionSettings->id())); | ||
84 | } | 92 | } | ||
85 | 93 | | |||
86 | ui->password->setFocus(); | 94 | QString connectionLabel; | ||
87 | connect(ui->showPassword, &QCheckBox::toggled, this, &PasswordDialog::showPassword); | 95 | UiUtils::iconAndTitleForConnectionSettingsType(m_connectionSettings->connectionType(), connectionLabel); | ||
88 | connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &PasswordDialog::accept); | 96 | m_ui->password->setFocus(); | ||
89 | connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &PasswordDialog::reject); | 97 | setWindowTitle(i18n("%1 password dialog", connectionLabel)); | ||
90 | } | 98 | } else { | ||
91 | 99 | NetworkManager::VpnSetting::Ptr vpnSetting = m_connectionSettings->setting(Setting::Vpn).dynamicCast<VpnSetting>(); | |||
92 | void PasswordDialog::setupVpnUi(const ConnectionSettings &connectionSettings) | 100 | qWarning() << "VPN Setting " << *vpnSetting; | ||
93 | { | | |||
94 | NetworkManager::VpnSetting::Ptr vpnSetting = connectionSettings.setting(Setting::Vpn).dynamicCast<VpnSetting>(); | | |||
95 | if (!vpnSetting) { | 101 | if (!vpnSetting) { | ||
96 | qCWarning(PLASMA_NM) << "Missing VPN setting!"; | 102 | qCWarning(PLASMA_NM) << "Missing VPN setting!"; | ||
97 | m_hasError = true; | 103 | m_hasError = true; | ||
98 | m_error = SecretAgent::InternalError; | 104 | m_error = SecretAgent::InternalError; | ||
99 | m_errorMessage = QLatin1String("VPN settings are missing"); | 105 | m_errorMessage = QLatin1String("VPN settings are missing"); | ||
100 | } else { | 106 | } else { | ||
101 | VpnUiPlugin *vpnUiPlugin; | 107 | VpnUiPlugin *vpnUiPlugin; | ||
102 | QString error; | 108 | QString error; | ||
103 | const QString serviceType = vpnSetting->serviceType(); | 109 | const QString serviceType = vpnSetting->serviceType(); | ||
104 | // qCDebug(PLASMA_NM) << "Agent loading VPN plugin" << serviceType << "from DBUS" << calledFromDBus(); | | |||
105 | // vpnSetting->printSetting(); | | |||
106 | vpnUiPlugin = KServiceTypeTrader::createInstanceFromQuery<VpnUiPlugin>(QLatin1String("PlasmaNetworkManagement/VpnUiPlugin"), | 110 | vpnUiPlugin = KServiceTypeTrader::createInstanceFromQuery<VpnUiPlugin>(QLatin1String("PlasmaNetworkManagement/VpnUiPlugin"), | ||
107 | QString::fromLatin1("[X-NetworkManager-Services]=='%1'").arg(serviceType), | 111 | QString::fromLatin1("[X-NetworkManager-Services]=='%1'").arg(serviceType), | ||
108 | this, QVariantList(), &error); | 112 | this, QVariantList(), &error); | ||
109 | if (vpnUiPlugin && error.isEmpty()) { | 113 | if (vpnUiPlugin && error.isEmpty()) { | ||
110 | const QString shortName = serviceType.section('.', -1); | 114 | const QString shortName = serviceType.section('.', -1); | ||
111 | setWindowTitle(i18n("VPN secrets (%1)", shortName)); | 115 | m_vpnWidget = vpnUiPlugin->askUser(vpnSetting, this); | ||
112 | vpnWidget = vpnUiPlugin->askUser(vpnSetting, this); | 116 | QVBoxLayout *layout = new QVBoxLayout(); | ||
113 | QDialogButtonBox * box; | 117 | layout->addWidget(m_vpnWidget); | ||
114 | if (shortName != QLatin1String("openconnect")) { | 118 | m_ui->vpnWidget->setLayout(layout); | ||
115 | box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this); | 119 | m_ui->labelText->setText(i18n("For accessing the vpn connection <b>%1</b> you need to provide secrets below:", m_connectionSettings->id())); | ||
116 | connect(box, &QDialogButtonBox::accepted, this, &PasswordDialog::accept); | 120 | setWindowTitle(i18n("VPN secrets (%1) dialog", shortName)); | ||
117 | } else { | 121 | | ||
118 | box = new QDialogButtonBox(QDialogButtonBox::Cancel, Qt::Horizontal, this); | 122 | // Hide generic password field and OK button in case of openconnect dialog | ||
123 | m_ui->labelPass->setVisible(false); | ||||
124 | m_ui->password->setVisible(false); | ||||
125 | if (shortName == QLatin1String("openconnect")) { | ||||
126 | QAbstractButton *button = m_ui->buttonBox->button(QDialogButtonBox::Ok); | ||||
127 | m_ui->buttonBox->removeButton(button); | ||||
119 | } | 128 | } | ||
120 | connect(box, &QDialogButtonBox::rejected, this, &PasswordDialog::reject); | | |||
121 | QVBoxLayout * layout = new QVBoxLayout(this); | | |||
122 | layout->addWidget(vpnWidget); | | |||
123 | layout->addWidget(box); | | |||
124 | setLayout(layout); | | |||
125 | } else { | 129 | } else { | ||
126 | qCWarning(PLASMA_NM) << error << ", serviceType == " << serviceType; | 130 | qCWarning(PLASMA_NM) << error << ", serviceType == " << serviceType; | ||
127 | m_hasError = true; | 131 | m_hasError = true; | ||
128 | m_error = SecretAgent::InternalError; | 132 | m_error = SecretAgent::InternalError; | ||
129 | m_errorMessage = error; | 133 | m_errorMessage = error; | ||
130 | } | 134 | } | ||
131 | } | 135 | } | ||
132 | } | 136 | } | ||
137 | } | ||||
133 | 138 | | |||
134 | bool PasswordDialog::hasError() const | 139 | bool PasswordDialog::hasError() const | ||
135 | { | 140 | { | ||
136 | return m_hasError; | 141 | return m_hasError; | ||
137 | } | 142 | } | ||
138 | 143 | | |||
139 | SecretAgent::Error PasswordDialog::error() const | 144 | SecretAgent::Error PasswordDialog::error() const | ||
140 | { | 145 | { | ||
141 | return m_error; | 146 | return m_error; | ||
142 | } | 147 | } | ||
143 | 148 | | |||
144 | QString PasswordDialog::errorMessage() const | 149 | QString PasswordDialog::errorMessage() const | ||
145 | { | 150 | { | ||
146 | return m_errorMessage; | 151 | return m_errorMessage; | ||
147 | } | 152 | } | ||
148 | 153 | | |||
149 | NMVariantMapMap PasswordDialog::secrets() const | 154 | NMVariantMapMap PasswordDialog::secrets() const | ||
150 | { | 155 | { | ||
151 | NMVariantMapMap ret = m_connection; | 156 | NMVariantMapMap ret = m_connectionSettings->toMap(); | ||
152 | QVariantMap result; | 157 | QVariantMap result; | ||
153 | if (vpnWidget) { | 158 | if (m_vpnWidget) { | ||
154 | result = vpnWidget->setting(); | 159 | result = m_vpnWidget->setting(); | ||
155 | } else if (!ui->password->text().isEmpty() && !m_neededSecrets.isEmpty()) { | 160 | } else if (!m_ui->password->text().isEmpty() && !m_neededSecrets.isEmpty()) { | ||
156 | result.insert(m_neededSecrets.first(), ui->password->text()); | 161 | result.insert(m_neededSecrets.first(), m_ui->password->text()); | ||
157 | } | 162 | } | ||
158 | 163 | | |||
159 | ret.insert(m_settingName, result); | 164 | ret.insert(m_settingName, result); | ||
160 | 165 | | |||
161 | return ret; | 166 | return ret; | ||
162 | } | 167 | } | ||
163 | | ||||
164 | void PasswordDialog::showPassword(bool show) | | |||
165 | { | | |||
166 | if (show) | | |||
167 | ui->password->setEchoMode(QLineEdit::Normal); | | |||
168 | else | | |||
169 | ui->password->setEchoMode(QLineEdit::Password); | | |||
170 | } | |