| 1 | // vi: ts=8 sts=4 sw=4 |
| 2 | /* |
| 3 | This file is part of the KDE libraries |
| 4 | SPDX-FileCopyrightText: 1998 Pietro Iglio <iglio@fub.it> |
| 5 | SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <jansen@kde.org> |
| 6 | SPDX-FileCopyrightText: 2004, 2005 Andrew Coles <andrew_coles@yahoo.co.uk> |
| 7 | SPDX-FileCopyrightText: 2007 Michaƫl Larouche <larouche@kde.org> |
| 8 | |
| 9 | SPDX-License-Identifier: LGPL-2.0-only |
| 10 | */ |
| 11 | #include "knewpassworddialog.h" |
| 12 | |
| 13 | #include <QMessageBox> |
| 14 | #include <QPushButton> |
| 15 | #include <QString> |
| 16 | #include <QStyleOption> |
| 17 | |
| 18 | #include <ktitlewidget.h> |
| 19 | |
| 20 | #include "ui_knewpassworddialog.h" |
| 21 | |
| 22 | class KNewPasswordDialogPrivate |
| 23 | { |
| 24 | Q_DECLARE_TR_FUNCTIONS(KNewPasswordDialog) |
| 25 | |
| 26 | public: |
| 27 | KNewPasswordDialogPrivate(KNewPasswordDialog *parent) |
| 28 | : q(parent) |
| 29 | { |
| 30 | } |
| 31 | |
| 32 | void init(); |
| 33 | void passwordStatusChanged(); |
| 34 | void showMessageWidget(const QString &message, KMessageWidget::MessageType type); |
| 35 | |
| 36 | KNewPasswordDialog *const q; |
| 37 | |
| 38 | QString pass; |
| 39 | QIcon icon; |
| 40 | |
| 41 | Ui::KNewPasswordDialog ui; |
| 42 | }; |
| 43 | |
| 44 | void KNewPasswordDialogPrivate::init() |
| 45 | { |
| 46 | ui.setupUi(q); |
| 47 | q->setIcon(QIcon::fromTheme(QStringLiteral("dialog-password" ))); |
| 48 | |
| 49 | ui.statusMsgWidget->hide(); |
| 50 | |
| 51 | QObject::connect(sender: ui.pwdWidget, signal: &KNewPasswordWidget::passwordStatusChanged, context: q, slot: [this]() { |
| 52 | passwordStatusChanged(); |
| 53 | }); |
| 54 | } |
| 55 | |
| 56 | void KNewPasswordDialogPrivate::passwordStatusChanged() |
| 57 | { |
| 58 | switch (ui.pwdWidget->passwordStatus()) { |
| 59 | case KNewPasswordWidget::PasswordTooShort: |
| 60 | ui.buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(false); |
| 61 | //~ singular Password must be at least %n character long |
| 62 | //~ plural Password must be at least %n characters long |
| 63 | showMessageWidget(message: tr(sourceText: "Password must be at least %n character(s) long." , disambiguation: "@info" , n: ui.pwdWidget->minimumPasswordLength()), type: KMessageWidget::Error); |
| 64 | break; |
| 65 | case KNewPasswordWidget::EmptyPasswordNotAllowed: |
| 66 | ui.buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(false); |
| 67 | showMessageWidget(message: tr(sourceText: "Password is empty." , disambiguation: "@info" ), type: KMessageWidget::Warning); |
| 68 | break; |
| 69 | case KNewPasswordWidget::PasswordNotVerified: |
| 70 | ui.buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(false); |
| 71 | showMessageWidget(message: tr(sourceText: "Passwords do not match." , disambiguation: "@info" ), type: KMessageWidget::Warning); |
| 72 | break; |
| 73 | case KNewPasswordWidget::WeakPassword: |
| 74 | case KNewPasswordWidget::StrongPassword: |
| 75 | ui.buttonBox->button(which: QDialogButtonBox::Ok)->setEnabled(true); |
| 76 | showMessageWidget(message: tr(sourceText: "Passwords match." , disambiguation: "@info" ), type: KMessageWidget::Positive); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void KNewPasswordDialogPrivate::showMessageWidget(const QString &message, KMessageWidget::MessageType type) |
| 82 | { |
| 83 | ui.statusMsgWidget->setText(message); |
| 84 | ui.statusMsgWidget->setMessageType(type); |
| 85 | ui.statusMsgWidget->animatedShow(); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Password dialog. |
| 90 | */ |
| 91 | |
| 92 | KNewPasswordDialog::KNewPasswordDialog(QWidget *parent) |
| 93 | : QDialog(parent) |
| 94 | , d(new KNewPasswordDialogPrivate(this)) |
| 95 | { |
| 96 | d->init(); |
| 97 | } |
| 98 | |
| 99 | KNewPasswordDialog::~KNewPasswordDialog() = default; |
| 100 | |
| 101 | void KNewPasswordDialog::setPrompt(const QString &prompt) |
| 102 | { |
| 103 | d->ui.labelPrompt->setText(prompt); |
| 104 | d->ui.labelPrompt->setMinimumSize(d->ui.labelPrompt->sizeHint()); |
| 105 | } |
| 106 | |
| 107 | QString KNewPasswordDialog::prompt() const |
| 108 | { |
| 109 | return d->ui.labelPrompt->text(); |
| 110 | } |
| 111 | |
| 112 | void KNewPasswordDialog::setIcon(const QIcon &icon) |
| 113 | { |
| 114 | d->icon = icon; |
| 115 | QStyleOption option; |
| 116 | option.initFrom(w: this); |
| 117 | const int iconSize = style()->pixelMetric(metric: QStyle::PM_MessageBoxIconSize, option: &option, widget: this); |
| 118 | d->ui.labelIcon->setPixmap(icon.pixmap(w: iconSize, h: iconSize)); |
| 119 | d->ui.labelIcon->setFixedSize(d->ui.labelIcon->sizeHint()); |
| 120 | } |
| 121 | |
| 122 | QIcon KNewPasswordDialog::icon() const |
| 123 | { |
| 124 | return d->icon; |
| 125 | } |
| 126 | |
| 127 | bool KNewPasswordDialog::checkAndGetPassword(QString *pwd) |
| 128 | { |
| 129 | pwd->clear(); |
| 130 | |
| 131 | if (d->ui.pwdWidget->passwordStatus() == KNewPasswordWidget::WeakPassword) { |
| 132 | QMessageBox::StandardButton selectedButton = QMessageBox::warning(parent: this, |
| 133 | title: tr(s: "Low Password Strength" , c: "@title:window" ), |
| 134 | text: tr(s: "The password you have entered has a low strength. " |
| 135 | "To improve the strength of " |
| 136 | "the password, try:\n" |
| 137 | " - using a longer password;\n" |
| 138 | " - using a mixture of upper- and lower-case letters;\n" |
| 139 | " - using numbers or symbols as well as letters.\n" |
| 140 | "\n" |
| 141 | "Would you like to use this password anyway?" ), |
| 142 | buttons: QMessageBox::Yes | QMessageBox::No); |
| 143 | |
| 144 | if (selectedButton == QMessageBox::No) { |
| 145 | return false; |
| 146 | } |
| 147 | } |
| 148 | if (!checkPassword(d->ui.pwdWidget->password())) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | *pwd = d->ui.pwdWidget->password(); |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | void KNewPasswordDialog::accept() |
| 157 | { |
| 158 | QString pwd; |
| 159 | if (!checkAndGetPassword(pwd: &pwd)) { |
| 160 | return; |
| 161 | } |
| 162 | d->pass = pwd; |
| 163 | Q_EMIT newPassword(password: d->pass); |
| 164 | QDialog::accept(); |
| 165 | } |
| 166 | |
| 167 | void KNewPasswordDialog::setAllowEmptyPasswords(bool allowed) |
| 168 | { |
| 169 | d->ui.pwdWidget->setAllowEmptyPasswords(allowed); |
| 170 | } |
| 171 | |
| 172 | bool KNewPasswordDialog::allowEmptyPasswords() const |
| 173 | { |
| 174 | return d->ui.pwdWidget->allowEmptyPasswords(); |
| 175 | } |
| 176 | |
| 177 | void KNewPasswordDialog::setMinimumPasswordLength(int minLength) |
| 178 | { |
| 179 | d->ui.pwdWidget->setMinimumPasswordLength(minLength); |
| 180 | } |
| 181 | |
| 182 | int KNewPasswordDialog::minimumPasswordLength() const |
| 183 | { |
| 184 | return d->ui.pwdWidget->minimumPasswordLength(); |
| 185 | } |
| 186 | |
| 187 | void KNewPasswordDialog::setMaximumPasswordLength(int maxLength) |
| 188 | { |
| 189 | d->ui.pwdWidget->setMaximumPasswordLength(maxLength); |
| 190 | } |
| 191 | |
| 192 | int KNewPasswordDialog::maximumPasswordLength() const |
| 193 | { |
| 194 | return d->ui.pwdWidget->maximumPasswordLength(); |
| 195 | } |
| 196 | |
| 197 | // reasonable password length code contributed by Steffen Mthing |
| 198 | |
| 199 | void KNewPasswordDialog::setReasonablePasswordLength(int reasonableLength) |
| 200 | { |
| 201 | d->ui.pwdWidget->setReasonablePasswordLength(reasonableLength); |
| 202 | } |
| 203 | |
| 204 | int KNewPasswordDialog::reasonablePasswordLength() const |
| 205 | { |
| 206 | return d->ui.pwdWidget->reasonablePasswordLength(); |
| 207 | } |
| 208 | |
| 209 | void KNewPasswordDialog::setPasswordStrengthWarningLevel(int warningLevel) |
| 210 | { |
| 211 | d->ui.pwdWidget->setPasswordStrengthWarningLevel(warningLevel); |
| 212 | } |
| 213 | |
| 214 | int KNewPasswordDialog::passwordStrengthWarningLevel() const |
| 215 | { |
| 216 | return d->ui.pwdWidget->passwordStrengthWarningLevel(); |
| 217 | } |
| 218 | |
| 219 | void KNewPasswordDialog::setBackgroundWarningColor(const QColor &color) |
| 220 | { |
| 221 | d->ui.pwdWidget->setBackgroundWarningColor(color); |
| 222 | } |
| 223 | |
| 224 | QColor KNewPasswordDialog::backgroundWarningColor() const |
| 225 | { |
| 226 | return d->ui.pwdWidget->backgroundWarningColor(); |
| 227 | } |
| 228 | |
| 229 | QString KNewPasswordDialog::password() const |
| 230 | { |
| 231 | return d->pass; |
| 232 | } |
| 233 | |
| 234 | #if KWIDGETSADDONS_ENABLE_DEPRECATED_SINCE(6, 0) |
| 235 | #pragma GCC diagnostic push |
| 236 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 237 | void KNewPasswordDialog::setRevealPasswordAvailable(bool reveal) |
| 238 | { |
| 239 | d->ui.pwdWidget->setRevealPasswordAvailable(reveal); |
| 240 | } |
| 241 | |
| 242 | bool KNewPasswordDialog::isRevealPasswordAvailable() const |
| 243 | { |
| 244 | return d->ui.pwdWidget->isRevealPasswordAvailable(); |
| 245 | } |
| 246 | #pragma GCC diagnostic pop |
| 247 | #endif |
| 248 | |
| 249 | KPassword::RevealMode KNewPasswordDialog::revealPasswordMode() const |
| 250 | { |
| 251 | return d->ui.pwdWidget->revealPasswordMode(); |
| 252 | } |
| 253 | |
| 254 | void KNewPasswordDialog::setRevealPasswordMode(KPassword::RevealMode revealPasswordMode) |
| 255 | { |
| 256 | d->ui.pwdWidget->setRevealPasswordMode(revealPasswordMode); |
| 257 | } |
| 258 | |
| 259 | bool KNewPasswordDialog::checkPassword(const QString &) |
| 260 | { |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | #include "moc_knewpassworddialog.cpp" |
| 265 | |