1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
|
/* pinentrydialog.cpp - A (not yet) secure Qt 4 dialog for PIN entry.
* Copyright (C) 2002, 2008 Klarälvdalens Datakonsult AB (KDAB)
* Copyright 2007 Ingo Klöcker
* Copyright 2016 Intevation GmbH
* Copyright (C) 2021, 2022 g10 Code GmbH
*
* Written by Steffen Hansen <steffen@klaralvdalens-datakonsult.se>.
* Modified by Andre Heinecke <aheinecke@intevation.de>
* Software engineering by Ingo Klöcker <dev@ingo-kloecker.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
* SPDX-License-Identifier: GPL-2.0+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "pinentrydialog.h"
#include "accessibility.h"
#include "capslock.h"
#include "pinlineedit.h"
#include "util.h"
#include <QGridLayout>
#include <QProgressBar>
#include <QApplication>
#include <QFontMetrics>
#include <QStyle>
#include <QPainter>
#include <QPushButton>
#include <QDialogButtonBox>
#include <QKeyEvent>
#include <QLabel>
#include <QPalette>
#include <QLineEdit>
#include <QAction>
#include <QCheckBox>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QRegularExpression>
#include <QAccessible>
#include <QDebug>
#ifdef Q_OS_WIN
#include <windows.h>
#if QT_VERSION >= 0x050700
#include <QtPlatformHeaders/QWindowsWindowFunctions>
#endif
#endif
void raiseWindow(QWidget *w)
{
#ifdef Q_OS_WIN
#if QT_VERSION >= 0x050700
QWindowsWindowFunctions::setWindowActivationBehavior(
QWindowsWindowFunctions::AlwaysActivateWindow);
#endif
#endif
w->setWindowState((w->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
w->activateWindow();
w->raise();
}
QPixmap applicationIconPixmap(const QIcon &overlayIcon)
{
QPixmap pm = qApp->windowIcon().pixmap(48, 48);
if (!overlayIcon.isNull()) {
QPainter painter(&pm);
const int emblemSize = 22;
painter.drawPixmap(pm.width() - emblemSize, 0,
overlayIcon.pixmap(emblemSize, emblemSize));
}
return pm;
}
void PinEntryDialog::slotTimeout()
{
_timed_out = true;
reject();
}
PinEntryDialog::PinEntryDialog(pinentry_t pe, QWidget *parent, const char *name,
bool modal,
const QString &repeatString,
const QString &visibilityTT,
const QString &hideTT)
: QDialog{parent}
, _have_quality_bar{!!pe->quality_bar}
, _pinentry_info{pe}
, mVisibilityTT{visibilityTT}
, mHideTT{hideTT}
{
Q_UNUSED(name)
if (modal) {
setWindowModality(Qt::ApplicationModal);
}
// Check for dark scheme to determine the icons
// use relative luminance defined in Rec. 709 for HDTV as reasonable estimate of the perceived lightness of the background color
const QColor &color = palette().button().color();
const double luminance709 = (0.2126 * color.red() + 0.7152 * color.green() + 0.0722 * color.blue()) / 255.;
if (luminance709 < 0.5) {
mIconSuffix = QStringLiteral("_dark");
}
QPalette redTextPalette;
redTextPalette.setColor(QPalette::WindowText, Qt::red);
const QIcon visibilityIcon = QIcon(QLatin1String(":/icons/visibility") + mIconSuffix);
const QIcon hideIcon = QIcon(QLatin1String(":/icons/hint") + mIconSuffix);
auto *const mainLayout = new QVBoxLayout{this};
auto *const hbox = new QHBoxLayout;
_icon = new QLabel(this);
_icon->setPixmap(applicationIconPixmap());
hbox->addWidget(_icon, 0, Qt::AlignVCenter | Qt::AlignLeft);
auto *const grid = new QGridLayout;
int row = 1;
_error = new QLabel{this};
_error->setTextFormat(Qt::PlainText);
_error->setTextInteractionFlags(Qt::TextSelectableByMouse);
_error->setPalette(redTextPalette);
_error->hide();
grid->addWidget(_error, row, 1, 1, 2);
row++;
_desc = new QLabel{this};
_desc->setTextFormat(Qt::PlainText);
_desc->setTextInteractionFlags(Qt::TextSelectableByMouse);
_desc->hide();
grid->addWidget(_desc, row, 1, 1, 2);
row++;
mCapsLockHint = new QLabel{this};
mCapsLockHint->setTextFormat(Qt::PlainText);
mCapsLockHint->setTextInteractionFlags(Qt::TextSelectableByMouse);
mCapsLockHint->setPalette(redTextPalette);
mCapsLockHint->setAlignment(Qt::AlignCenter);
mCapsLockHint->setVisible(false);
grid->addWidget(mCapsLockHint, row, 1, 1, 2);
row++;
{
_prompt = new QLabel(this);
_prompt->setTextFormat(Qt::PlainText);
_prompt->setTextInteractionFlags(Qt::TextSelectableByMouse);
_prompt->hide();
grid->addWidget(_prompt, row, 1);
const auto l = new QHBoxLayout;
_edit = new PinLineEdit(this);
_edit->setMaxLength(256);
_edit->setMinimumWidth(_edit->fontMetrics().horizontalAdvance(QLatin1Char('x')) * 35);
_edit->setEchoMode(QLineEdit::Password);
_prompt->setBuddy(_edit);
l->addWidget(_edit, 1);
if (!visibilityIcon.isNull() && !hideIcon.isNull()) {
mShowHideButton = new QPushButton{this};
mShowHideButton->setIcon(visibilityIcon);
mShowHideButton->setToolTip(mVisibilityTT);
l->addWidget(mShowHideButton);
}
grid->addLayout(l, row, 2);
}
if (!mShowHideButton && !mVisibilityTT.isNull()) {
row++;
mVisiCB = new QCheckBox{mVisibilityTT, this};
grid->addWidget(mVisiCB, row, 1, 1, 2, Qt::AlignLeft);
}
row++;
mConstraintsHint = new QLabel{this};
mConstraintsHint->setTextFormat(Qt::PlainText);
mConstraintsHint->setTextInteractionFlags(Qt::TextSelectableByMouse);
mConstraintsHint->setVisible(false);
grid->addWidget(mConstraintsHint, row, 2);
row++;
mFormattedPassphraseHintSpacer = new QLabel{this};
mFormattedPassphraseHintSpacer->setVisible(false);
mFormattedPassphraseHint = new QLabel{this};
mFormattedPassphraseHint->setTextFormat(Qt::PlainText);
mFormattedPassphraseHint->setTextInteractionFlags(Qt::TextSelectableByMouse);
mFormattedPassphraseHint->setVisible(false);
grid->addWidget(mFormattedPassphraseHintSpacer, row, 1);
grid->addWidget(mFormattedPassphraseHint, row, 2);
if (!repeatString.isNull()) {
row++;
auto repeatLabel = new QLabel{this};
repeatLabel->setTextFormat(Qt::PlainText);
repeatLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);
repeatLabel->setText(repeatString);
grid->addWidget(repeatLabel, row, 1);
const auto l = new QHBoxLayout;
mRepeat = new PinLineEdit(this);
mRepeat->setMaxLength(256);
mRepeat->setEchoMode(QLineEdit::Password);
repeatLabel->setBuddy(mRepeat);
l->addWidget(mRepeat, 1);
if (!repeatString.isNull()) {
mGenerateButton = new QPushButton{this};
mGenerateButton->setIcon(QIcon(QLatin1String(":/icons/password-generate") + mIconSuffix));
mGenerateButton->setVisible(false);
l->addWidget(mGenerateButton);
}
grid->addLayout(l, row, 2);
row++;
mRepeatError = new QLabel{this};
mRepeatError->setTextFormat(Qt::PlainText);
mRepeatError->setTextInteractionFlags(Qt::TextSelectableByMouse);
mRepeatError->setPalette(redTextPalette);
mRepeatError->hide();
grid->addWidget(mRepeatError, row, 2);
}
if (_have_quality_bar) {
row++;
_quality_bar_label = new QLabel(this);
_quality_bar_label->setTextFormat(Qt::PlainText);
_quality_bar_label->setTextInteractionFlags(Qt::TextSelectableByMouse);
_quality_bar_label->setAlignment(Qt::AlignVCenter);
grid->addWidget(_quality_bar_label, row, 1);
_quality_bar = new QProgressBar(this);
_quality_bar->setAlignment(Qt::AlignCenter);
_quality_bar_label->setBuddy(_quality_bar);
grid->addWidget(_quality_bar, row, 2);
}
++row;
mSavePassphraseCB = new QCheckBox{this};
mSavePassphraseCB->setVisible(false);
mSavePassphraseCB->setCheckState(!!_pinentry_info->may_cache_password
? Qt::Checked
: Qt::Unchecked);
#ifdef HAVE_LIBSECRET
if (_pinentry_info->allow_external_password_cache && _pinentry_info->keyinfo) {
mSavePassphraseCB->setVisible(true);
}
#endif
grid->addWidget(mSavePassphraseCB, row, 1, 1, 2);
hbox->addLayout(grid, 1);
mainLayout->addLayout(hbox);
QDialogButtonBox *const buttons = new QDialogButtonBox(this);
buttons->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
_ok = buttons->button(QDialogButtonBox::Ok);
_cancel = buttons->button(QDialogButtonBox::Cancel);
if (style()->styleHint(QStyle::SH_DialogButtonBox_ButtonsHaveIcons)) {
_ok->setIcon(style()->standardIcon(QStyle::SP_DialogOkButton));
_cancel->setIcon(style()->standardIcon(QStyle::SP_DialogCancelButton));
}
mainLayout->addStretch(1);
mainLayout->addWidget(buttons);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
if (_pinentry_info->timeout > 0) {
_timer = new QTimer(this);
connect(_timer, &QTimer::timeout, this, &PinEntryDialog::slotTimeout);
_timer->start(_pinentry_info->timeout * 1000);
}
connect(buttons, &QDialogButtonBox::accepted,
this, &PinEntryDialog::onAccept);
connect(buttons, &QDialogButtonBox::rejected,
this, &QDialog::reject);
connect(_edit, &QLineEdit::textChanged,
this, &PinEntryDialog::updateQuality);
connect(_edit, &QLineEdit::textChanged,
this, &PinEntryDialog::textChanged);
connect(_edit, &PinLineEdit::backspacePressed,
this, &PinEntryDialog::onBackspace);
if (mGenerateButton) {
connect(mGenerateButton, &QPushButton::clicked,
this, &PinEntryDialog::generatePin);
}
if (mShowHideButton) {
connect(mShowHideButton, &QPushButton::clicked,
this, &PinEntryDialog::toggleVisibility);
}
if (mVisiCB) {
connect(mVisiCB, &QCheckBox::toggled,
this, &PinEntryDialog::toggleVisibility);
}
if (mRepeat) {
connect(mRepeat, &QLineEdit::textChanged,
this, &PinEntryDialog::textChanged);
}
connect(mSavePassphraseCB, &QCheckBox::toggled,
this, &PinEntryDialog::togglePasswordCaching);
auto capsLockWatcher = new CapsLockWatcher{this};
connect(capsLockWatcher, &CapsLockWatcher::stateChanged,
this, [this] (bool locked) {
mCapsLockHint->setVisible(locked);
});
connect(qApp, &QApplication::focusChanged,
this, &PinEntryDialog::focusChanged);
connect(qApp, &QApplication::applicationStateChanged,
this, &PinEntryDialog::checkCapsLock);
checkCapsLock();
#ifndef QT_NO_ACCESSIBILITY
QAccessible::installActivationObserver(this);
accessibilityActiveChanged(QAccessible::isActive());
#endif
#if QT_VERSION >= 0x050000
/* This is mostly an issue on Windows where this results
in the pinentry popping up nicely with an animation and
comes to front. It is not ifdefed for Windows only since
window managers on Linux like KWin can also have this
result in an animation when the pinentry is shown and
not just popping it up.
*/
if (qApp->platformName() != QLatin1String("wayland")) {
setWindowState(Qt::WindowMinimized);
QTimer::singleShot(0, this, [this] () {
raiseWindow(this);
});
}
#else
activateWindow();
raise();
#endif
}
PinEntryDialog::~PinEntryDialog()
{
#ifndef QT_NO_ACCESSIBILITY
QAccessible::removeActivationObserver(this);
#endif
}
void PinEntryDialog::keyPressEvent(QKeyEvent *e)
{
const auto returnPressed =
(!e->modifiers() && (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return))
|| (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter);
if (returnPressed && _edit->hasFocus() && mRepeat) {
// if the user pressed Return in the first input field, then move the
// focus to the repeat input field and prevent further event processing
// by QDialog (which would trigger the default button)
mRepeat->setFocus();
e->ignore();
return;
}
QDialog::keyPressEvent(e);
}
void PinEntryDialog::keyReleaseEvent(QKeyEvent *event)
{
QDialog::keyReleaseEvent(event);
checkCapsLock();
}
void PinEntryDialog::showEvent(QShowEvent *event)
{
QDialog::showEvent(event);
_edit->setFocus();
}
void PinEntryDialog::setDescription(const QString &txt)
{
_desc->setVisible(!txt.isEmpty());
_desc->setText(txt);
_icon->setPixmap(applicationIconPixmap());
setError(QString());
}
QString PinEntryDialog::description() const
{
return _desc->text();
}
void PinEntryDialog::setError(const QString &txt)
{
if (!txt.isNull()) {
_icon->setPixmap(applicationIconPixmap(QIcon{QStringLiteral(":/icons/data-error.svg")}));
}
_error->setText(txt);
_error->setVisible(!txt.isEmpty());
}
QString PinEntryDialog::error() const
{
return _error->text();
}
void PinEntryDialog::setPin(const QString &txt)
{
_edit->setPin(txt);
}
QString PinEntryDialog::pin() const
{
return _edit->pin();
}
void PinEntryDialog::setPrompt(const QString &txt)
{
_prompt->setText(txt);
_prompt->setVisible(!txt.isEmpty());
if (txt.contains("PIN"))
_disable_echo_allowed = false;
}
QString PinEntryDialog::prompt() const
{
return _prompt->text();
}
void PinEntryDialog::setOkText(const QString &txt)
{
_ok->setText(txt);
_ok->setVisible(!txt.isEmpty());
}
void PinEntryDialog::setCancelText(const QString &txt)
{
_cancel->setText(txt);
_cancel->setVisible(!txt.isEmpty());
}
void PinEntryDialog::setQualityBar(const QString &txt)
{
if (_have_quality_bar) {
_quality_bar_label->setText(txt);
}
}
void PinEntryDialog::setQualityBarTT(const QString &txt)
{
if (_have_quality_bar) {
_quality_bar->setToolTip(txt);
}
}
void PinEntryDialog::setGenpinLabel(const QString &txt)
{
if (!mGenerateButton) {
return;
}
mGenerateButton->setVisible(!txt.isEmpty());
if (!txt.isEmpty()) {
Accessibility::setName(mGenerateButton, txt);
}
}
void PinEntryDialog::setGenpinTT(const QString &txt)
{
if (mGenerateButton) {
mGenerateButton->setToolTip(txt);
}
}
void PinEntryDialog::setCapsLockHint(const QString &txt)
{
mCapsLockHint->setText(txt);
}
void PinEntryDialog::setFormattedPassphrase(const PinEntryDialog::FormattedPassphraseOptions &options)
{
mFormatPassphrase = options.formatPassphrase;
mFormattedPassphraseHint->setTextFormat(Qt::RichText);
mFormattedPassphraseHint->setText(QLatin1String("<html>") + options.hint.toHtmlEscaped() + QLatin1String("</html>"));
Accessibility::setName(mFormattedPassphraseHint, options.hint);
toggleFormattedPassphrase();
}
void PinEntryDialog::setConstraintsOptions(const ConstraintsOptions &options)
{
mEnforceConstraints = options.enforce;
mConstraintsHint->setText(options.shortHint);
if (!options.longHint.isEmpty()) {
mConstraintsHint->setToolTip(QLatin1String("<html>") +
options.longHint.toHtmlEscaped().replace(QLatin1String("\n\n"), QLatin1String("<br>")) +
QLatin1String("</html>"));
Accessibility::setDescription(mConstraintsHint, options.longHint);
}
mConstraintsErrorTitle = options.errorTitle;
mConstraintsHint->setVisible(mEnforceConstraints && !options.shortHint.isEmpty());
}
void PinEntryDialog::toggleFormattedPassphrase()
{
const bool enableFormatting = mFormatPassphrase && _edit->echoMode() == QLineEdit::Normal;
_edit->setFormattedPassphrase(enableFormatting);
if (mRepeat) {
mRepeat->setFormattedPassphrase(enableFormatting);
const bool hintAboutToBeHidden = mFormattedPassphraseHint->isVisible() && !enableFormatting;
if (hintAboutToBeHidden) {
// set hint spacer to current height of hint label before hiding the hint
mFormattedPassphraseHintSpacer->setMinimumHeight(mFormattedPassphraseHint->height());
mFormattedPassphraseHintSpacer->setVisible(true);
} else if (enableFormatting) {
mFormattedPassphraseHintSpacer->setVisible(false);
}
mFormattedPassphraseHint->setVisible(enableFormatting);
}
}
void PinEntryDialog::togglePasswordCaching(bool enabled)
{
_pinentry_info->may_cache_password = enabled;
}
void PinEntryDialog::onBackspace()
{
cancelTimeout();
if (_disable_echo_allowed) {
_edit->setEchoMode(QLineEdit::NoEcho);
if (mRepeat) {
mRepeat->setEchoMode(QLineEdit::NoEcho);
}
}
}
void PinEntryDialog::updateQuality(const QString &txt)
{
int length;
int percent;
QPalette pal;
_disable_echo_allowed = false;
if (!_have_quality_bar || !_pinentry_info) {
return;
}
const QByteArray utf8_pin = txt.toUtf8();
const char *pin = utf8_pin.constData();
length = strlen(pin);
percent = length ? pinentry_inq_quality(_pinentry_info, pin, length) : 0;
if (!length) {
_quality_bar->reset();
} else {
pal = _quality_bar->palette();
if (percent < 0) {
pal.setColor(QPalette::Highlight, QColor("red"));
percent = -percent;
} else {
pal.setColor(QPalette::Highlight, QColor("green"));
}
_quality_bar->setPalette(pal);
_quality_bar->setValue(percent);
}
}
void PinEntryDialog::setSavePassphraseCBText(const QString &text)
{
mSavePassphraseCB->setText(text);
}
void PinEntryDialog::focusChanged(QWidget *old, QWidget *now)
{
// Grab keyboard. It might be a little weird to do it here, but it works!
// Previously this code was in showEvent, but that did not work in Qt4.
if (!_pinentry_info || _pinentry_info->grab) {
if (_grabbed && old && (old == _edit || old == mRepeat)) {
old->releaseKeyboard();
_grabbed = false;
}
if (!_grabbed && now && (now == _edit || now == mRepeat)) {
now->grabKeyboard();
_grabbed = true;
}
}
}
void PinEntryDialog::textChanged(const QString &text)
{
Q_UNUSED(text);
cancelTimeout();
}
void PinEntryDialog::generatePin()
{
unique_malloced_ptr<char> pin{pinentry_inq_genpin(_pinentry_info)};
if (pin) {
if (_edit->echoMode() == QLineEdit::Password) {
if (mShowHideButton) {
mShowHideButton->click();
}
if (mVisiCB) {
mVisiCB->setChecked(true);
}
}
const auto pinStr = QString::fromUtf8(pin.get());
_edit->setPin(pinStr);
mRepeat->setPin(pinStr);
// explicitly focus the first input field and select the generated password
_edit->setFocus();
_edit->selectAll();
}
}
void PinEntryDialog::toggleVisibility()
{
if (sender() != mVisiCB) {
if (_edit->echoMode() == QLineEdit::Password) {
if (mShowHideButton) {
mShowHideButton->setIcon(QIcon(QLatin1String(":/icons/hint") + mIconSuffix));
mShowHideButton->setToolTip(mHideTT);
}
_edit->setEchoMode(QLineEdit::Normal);
if (mRepeat) {
mRepeat->setEchoMode(QLineEdit::Normal);
}
} else {
if (mShowHideButton) {
mShowHideButton->setIcon(QIcon(QLatin1String(":/icons/visibility") + mIconSuffix));
mShowHideButton->setToolTip(mVisibilityTT);
}
_edit->setEchoMode(QLineEdit::Password);
if (mRepeat) {
mRepeat->setEchoMode(QLineEdit::Password);
}
}
} else {
if (mVisiCB->isChecked()) {
if (mRepeat) {
mRepeat->setEchoMode(QLineEdit::Normal);
}
_edit->setEchoMode(QLineEdit::Normal);
} else {
if (mRepeat) {
mRepeat->setEchoMode(QLineEdit::Password);
}
_edit->setEchoMode(QLineEdit::Password);
}
}
toggleFormattedPassphrase();
}
QString PinEntryDialog::repeatedPin() const
{
if (mRepeat) {
return mRepeat->pin();
}
return QString();
}
bool PinEntryDialog::timedOut() const
{
return _timed_out;
}
void PinEntryDialog::setRepeatErrorText(const QString &err)
{
if (mRepeatError) {
mRepeatError->setText(err);
}
}
void PinEntryDialog::cancelTimeout()
{
if (_timer) {
_timer->stop();
}
}
void PinEntryDialog::checkCapsLock()
{
const auto state = capsLockState();
if (state != LockState::Unknown) {
mCapsLockHint->setVisible(state == LockState::On);
}
}
void PinEntryDialog::onAccept()
{
cancelTimeout();
if (mRepeat && mRepeat->pin() != _edit->pin()) {
#ifndef QT_NO_ACCESSIBILITY
if (QAccessible::isActive()) {
QMessageBox::information(this, mRepeatError->text(), mRepeatError->text());
} else
#endif
{
mRepeatError->setVisible(true);
}
return;
}
const auto result = checkConstraints();
if (result != PassphraseNotOk) {
accept();
}
}
#ifndef QT_NO_ACCESSIBILITY
void PinEntryDialog::accessibilityActiveChanged(bool active)
{
// Allow text labels to get focus if accessibility is active
const auto focusPolicy = active ? Qt::StrongFocus : Qt::ClickFocus;
_error->setFocusPolicy(focusPolicy);
_desc->setFocusPolicy(focusPolicy);
mCapsLockHint->setFocusPolicy(focusPolicy);
mConstraintsHint->setFocusPolicy(focusPolicy);
mFormattedPassphraseHint->setFocusPolicy(focusPolicy);
if (mRepeatError) {
mRepeatError->setFocusPolicy(focusPolicy);
}
}
#endif
PinEntryDialog::PassphraseCheckResult PinEntryDialog::checkConstraints()
{
if (!mEnforceConstraints) {
return PassphraseNotChecked;
}
const auto passphrase = _edit->pin().toUtf8();
unique_malloced_ptr<char> error{pinentry_inq_checkpin(
_pinentry_info, passphrase.constData(), passphrase.size())};
if (!error) {
return PassphraseOk;
}
const auto messageLines = QString::fromUtf8(QByteArray::fromPercentEncoding(error.get())).split(QChar{'\n'});
if (messageLines.isEmpty()) {
// shouldn't happen because pinentry_inq_checkpin() either returns NULL or a non-empty string
return PassphraseOk;
}
const auto firstLine = messageLines.first();
const auto indexOfFirstNonEmptyAdditionalLine = messageLines.indexOf(QRegularExpression{QStringLiteral(".*\\S.*")}, 1);
const auto additionalLines = indexOfFirstNonEmptyAdditionalLine > 0 ? messageLines.mid(indexOfFirstNonEmptyAdditionalLine).join(QChar{'\n'}) : QString{};
QMessageBox messageBox{this};
messageBox.setIcon(QMessageBox::Information);
messageBox.setWindowTitle(mConstraintsErrorTitle);
messageBox.setText(firstLine);
messageBox.setInformativeText(additionalLines);
messageBox.setStandardButtons(QMessageBox::Ok);
messageBox.exec();
return PassphraseNotOk;
}
#include "pinentrydialog.moc"
|