Skip to content

Commit b08468e

Browse files
committed
Optimize: use const char* where possible
Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 1e6e8cc commit b08468e

File tree

2 files changed

+98
-85
lines changed

2 files changed

+98
-85
lines changed

src/ui/webeiddialog.cpp

+96-83
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,8 @@ void WebEidDialog::showAboutPage()
241241
d->ui->aboutAlert->hide();
242242
auto* app = qApp;
243243
if (app->isSafariExtensionContainingApp()) {
244-
d->setupOK([app] { app->showSafariSettings(); },
245-
[] { return tr("Open Safari settings..."); }, true);
244+
d->setupOK([app] { app->showSafariSettings(); }, QT_TR_NOOP("Open Safari settings..."),
245+
true);
246246
connect(app, &Application::safariExtensionEnabled, d, [d](bool value) {
247247
d->ui->aboutAlert->setHidden(value);
248248
d->resizeHeight();
@@ -260,7 +260,7 @@ void WebEidDialog::showAboutPage()
260260
void WebEidDialog::showFatalErrorPage()
261261
{
262262
auto* d = new WebEidDialog();
263-
d->setTrText(d->ui->messagePageTitleLabel, []() -> QString { return tr("Operation failed"); });
263+
d->setTrText(d->ui->messagePageTitleLabel, QT_TR_NOOP("Operation failed"));
264264
d->ui->fatalError->show();
265265
d->ui->fatalHelp->show();
266266
d->ui->connectCardLabel->hide();
@@ -294,12 +294,8 @@ void WebEidDialog::onSmartCardStatusUpdate(const RetriableError status)
294294
{
295295
currentCommand = CommandType::INSERT_CARD;
296296

297-
setTrText(ui->connectCardLabel, [status]() -> QString {
298-
return std::get<0>(retriableErrorToTextTitleAndIcon(status));
299-
});
300-
setTrText(ui->messagePageTitleLabel, [status]() -> QString {
301-
return std::get<1>(retriableErrorToTextTitleAndIcon(status));
302-
});
297+
setTrText(ui->connectCardLabel, std::get<0>(retriableErrorToTextTitleAndIcon(status)));
298+
setTrText(ui->messagePageTitleLabel, std::get<1>(retriableErrorToTextTitleAndIcon(status)));
303299
ui->cardChipIcon->setPixmap(std::get<2>(retriableErrorToTextTitleAndIcon(status)));
304300

305301
// In case the insert card page is not shown, switch back to it.
@@ -382,29 +378,29 @@ void WebEidDialog::onSingleCertificateReady(const QUrl& origin,
382378
return;
383379
case CommandType::AUTHENTICATE:
384380
ui->pinInputCertificateInfo->setCertificateInfo(certAndPin);
385-
setTrText(ui->pinInputPageTitleLabel, []() -> QString { return tr("Authenticate"); });
386-
setTrText(ui->pinInputDescriptionLabel, []() -> QString {
387-
return tr("By authenticating, I agree to the transfer of my name and personal "
388-
"identification code to the service provider.");
389-
});
390-
setTrText(ui->pinTitleLabel, [useExternalPinDialog]() -> QString {
391-
return useExternalPinDialog
392-
? tr("Please enter PIN for authentication in the PIN dialog window that opens.")
393-
: tr("Enter PIN1 for authentication");
394-
});
381+
setTrText(ui->pinInputPageTitleLabel, QT_TR_NOOP("Authenticate"));
382+
setTrText(ui->pinInputDescriptionLabel,
383+
QT_TR_NOOP("By authenticating, I agree to the transfer of my name and personal "
384+
"identification code to the service provider."));
385+
setTrText(
386+
ui->pinTitleLabel,
387+
useExternalPinDialog
388+
? QT_TR_NOOP(
389+
"Please enter PIN for authentication in the PIN dialog window that opens.")
390+
: QT_TR_NOOP("Enter PIN1 for authentication"));
395391
break;
396392
case CommandType::SIGN:
397393
ui->pinInputCertificateInfo->setCertificateInfo(certAndPin);
398-
setTrText(ui->pinInputPageTitleLabel, []() -> QString { return tr("Signing"); });
399-
setTrText(ui->pinInputDescriptionLabel, []() -> QString {
400-
return tr("By signing, I agree to the transfer of my name and personal identification "
401-
"code to the service provider.");
402-
});
403-
setTrText(ui->pinTitleLabel, [useExternalPinDialog]() -> QString {
404-
return useExternalPinDialog
405-
? tr("Please enter PIN for signing in the PIN dialog window that opens.")
406-
: tr("Enter PIN2 for signing");
407-
});
394+
setTrText(ui->pinInputPageTitleLabel, QT_TR_NOOP("Signing"));
395+
setTrText(
396+
ui->pinInputDescriptionLabel,
397+
QT_TR_NOOP("By signing, I agree to the transfer of my name and personal identification "
398+
"code to the service provider."));
399+
setTrText(
400+
ui->pinTitleLabel,
401+
useExternalPinDialog
402+
? QT_TR_NOOP("Please enter PIN for signing in the PIN dialog window that opens.")
403+
: QT_TR_NOOP("Enter PIN2 for signing"));
408404
break;
409405
default:
410406
emit failure(QStringLiteral("Only SELECT_CERTIFICATE, AUTHENTICATE or SIGN allowed"));
@@ -430,15 +426,14 @@ void WebEidDialog::onSingleCertificateReady(const QUrl& origin,
430426

431427
void WebEidDialog::onRetry(const RetriableError error)
432428
{
433-
onRetryImpl([error] { return std::get<0>(retriableErrorToTextTitleAndIcon(error)); });
429+
onRetryImpl(std::get<0>(retriableErrorToTextTitleAndIcon(error)));
434430
}
435431

436432
void WebEidDialog::onSigningCertificateMismatch()
437433
{
438-
onRetryImpl([] {
439-
return tr("The certificate of the ID card in the reader does not match the originally "
440-
"submitted certificate. Please insert the original ID card.");
441-
});
434+
onRetryImpl(
435+
QT_TR_NOOP("The certificate of the ID card in the reader does not match the originally "
436+
"submitted certificate. Please insert the original ID card."));
442437
}
443438

444439
void WebEidDialog::onVerifyPinFailed(const VerifyPinFailed::Status status, const qint8 retriesLeft)
@@ -523,19 +518,25 @@ template <typename Text>
523518
void WebEidDialog::onRetryImpl(Text text)
524519
{
525520
setTrText(ui->connectCardLabel, std::forward<Text>(text));
526-
setTrText(ui->messagePageTitleLabel, []() -> QString { return tr("Operation failed"); });
521+
setTrText(ui->messagePageTitleLabel, QT_TR_NOOP("Operation failed"));
527522
ui->cardChipIcon->setPixmap(pixmap("no-id-card"_L1));
528-
setupOK([this] { emit retry(); }, []() -> QString { return tr("Try again"); }, true);
523+
setupOK([this] { emit retry(); }, QT_TR_NOOP("Try again"), true);
529524
ui->pageStack->setCurrentIndex(int(Page::ALERT));
530525
}
531526

532527
template <typename Text>
533528
void WebEidDialog::setTrText(QWidget* label, Text text) const
534529
{
535530
disconnect(this, &WebEidDialog::languageChange, label, nullptr);
536-
label->setProperty("text", text());
537-
connect(this, &WebEidDialog::languageChange, label,
538-
[label, text = std::forward<Text>(text)] { label->setProperty("text", text()); });
531+
if constexpr (std::is_same_v<Text, const char*>) {
532+
label->setProperty("text", tr(text));
533+
connect(this, &WebEidDialog::languageChange, label,
534+
[label, text] { label->setProperty("text", tr(text)); });
535+
} else {
536+
label->setProperty("text", text());
537+
connect(this, &WebEidDialog::languageChange, label,
538+
[label, text = std::forward<Text>(text)] { label->setProperty("text", text()); });
539+
}
539540
}
540541

541542
void WebEidDialog::connectOkToCachePinAndEmitSelectedCertificate(
@@ -650,13 +651,13 @@ void WebEidDialog::setupPinInput(const CardCertificateAndPinInfo& certAndPin)
650651
}
651652

652653
template <typename Func>
653-
void WebEidDialog::setupOK(Func&& func, const std::function<QString()>& text, bool enabled)
654+
void WebEidDialog::setupOK(Func&& func, const char* text, bool enabled)
654655
{
655656
ui->okButton->disconnect();
656657
connect(ui->okButton, &QPushButton::clicked, this, std::forward<Func>(func));
657658
ui->okButton->show();
658659
ui->okButton->setEnabled(enabled);
659-
setTrText(ui->okButton, text ? text : []() -> QString { return tr("Confirm"); });
660+
setTrText(ui->okButton, text ? text : QT_TR_NOOP("Confirm"));
660661
ui->cancelButton->show();
661662
ui->cancelButton->setEnabled(true);
662663
ui->helpButton->hide();
@@ -669,8 +670,7 @@ void WebEidDialog::displayPinBlockedError()
669670
ui->pinTimeoutTimer->stop();
670671
ui->pinTimeRemaining->hide();
671672
ui->pinEntryTimeoutProgressBar->hide();
672-
setTrText(ui->pinErrorLabel,
673-
[]() -> QString { return tr("PIN is locked. Unblock and try again."); });
673+
setTrText(ui->pinErrorLabel, QT_TR_NOOP("PIN is locked. Unblock and try again."));
674674
ui->pinErrorLabel->show();
675675
ui->okButton->hide();
676676
ui->cancelButton->setEnabled(true);
@@ -697,77 +697,90 @@ QPixmap WebEidDialog::pixmap(QLatin1String name)
697697
.arg(name, Application::isDarkTheme() ? "_dark"_L1 : QLatin1String())};
698698
}
699699

700-
std::tuple<QString, QString, QPixmap>
700+
std::tuple<const char*, const char*, QPixmap>
701701
WebEidDialog::retriableErrorToTextTitleAndIcon(const RetriableError error)
702702
{
703703
switch (error) {
704704
case RetriableError::SMART_CARD_SERVICE_IS_NOT_RUNNING:
705-
return {tr("The smart card service required to use the ID-card is not running. Please "
706-
"start the smart card service and try again."),
707-
tr("Launch the Smart Card service"), pixmap("cardreader"_L1)};
705+
return {
706+
QT_TR_NOOP("The smart card service required to use the ID-card is not running. Please "
707+
"start the smart card service and try again."),
708+
QT_TR_NOOP("Launch the Smart Card service"), pixmap("cardreader"_L1)};
708709
case RetriableError::NO_SMART_CARD_READERS_FOUND:
709-
return {tr("<b>Card reader not connected.</b> Please connect the card reader to "
710-
"the computer."),
711-
tr("Connect the card reader"), pixmap("cardreader"_L1)};
710+
return {QT_TR_NOOP("<b>Card reader not connected.</b> Please connect the card reader to "
711+
"the computer."),
712+
QT_TR_NOOP("Connect the card reader"), pixmap("cardreader"_L1)};
712713

713714
case RetriableError::NO_SMART_CARDS_FOUND:
714715
case RetriableError::PKCS11_TOKEN_NOT_PRESENT:
715-
return {tr("<b>ID-card not found.</b> Please insert the ID-card into the reader."),
716-
tr("Insert the ID-card"), pixmap("no-id-card"_L1)};
716+
return {QT_TR_NOOP("<b>ID-card not found.</b> Please insert the ID-card into the reader."),
717+
QT_TR_NOOP("Insert the ID-card"), pixmap("no-id-card"_L1)};
717718
case RetriableError::SMART_CARD_WAS_REMOVED:
718719
case RetriableError::PKCS11_TOKEN_REMOVED:
719-
return {tr("The ID-card was removed from the reader. Please insert the ID-card into the "
720-
"reader."),
721-
tr("Insert the ID-card"), pixmap("no-id-card"_L1)};
720+
return {QT_TR_NOOP(
721+
"The ID-card was removed from the reader. Please insert the ID-card into the "
722+
"reader."),
723+
QT_TR_NOOP("Insert the ID-card"), pixmap("no-id-card"_L1)};
722724

723725
case RetriableError::SMART_CARD_TRANSACTION_FAILED:
724-
return {tr("Operation failed. Make sure that the ID-card and the card reader are connected "
725-
"correctly."),
726-
tr("Check the ID-card and the reader connection"), pixmap("no-id-card"_L1)};
726+
return {
727+
QT_TR_NOOP(
728+
"Operation failed. Make sure that the ID-card and the card reader are connected "
729+
"correctly."),
730+
QT_TR_NOOP("Check the ID-card and the reader connection"), pixmap("no-id-card"_L1)};
727731
case RetriableError::FAILED_TO_COMMUNICATE_WITH_CARD_OR_READER:
728-
return {tr("Connection to the ID-card or reader failed. Make sure that the ID-card and the "
729-
"card reader are connected correctly."),
730-
tr("Check the ID-card and the reader connection"), pixmap("no-id-card"_L1)};
732+
return {
733+
QT_TR_NOOP(
734+
"Connection to the ID-card or reader failed. Make sure that the ID-card and the "
735+
"card reader are connected correctly."),
736+
QT_TR_NOOP("Check the ID-card and the reader connection"), pixmap("no-id-card"_L1)};
731737

732738
case RetriableError::SMART_CARD_CHANGE_REQUIRED:
733-
return {tr("The desired operation cannot be performed with the inserted ID-card. Make sure "
734-
"that the ID-card is supported by the Web eID application."),
735-
tr("Operation not supported"), pixmap("no-id-card"_L1)};
739+
return {
740+
QT_TR_NOOP(
741+
"The desired operation cannot be performed with the inserted ID-card. Make sure "
742+
"that the ID-card is supported by the Web eID application."),
743+
QT_TR_NOOP("Operation not supported"), pixmap("no-id-card"_L1)};
736744

737745
case RetriableError::SMART_CARD_COMMAND_ERROR:
738-
return {tr("Error communicating with the card."), tr("Operation failed"),
746+
return {QT_TR_NOOP("Error communicating with the card."), QT_TR_NOOP("Operation failed"),
739747
pixmap("no-id-card"_L1)};
740748
// TODO: what action should the user take? Should this be fatal?
741749
case RetriableError::PKCS11_ERROR:
742-
return {tr("Card driver error. Please try again."), tr("Card driver error"),
750+
return {QT_TR_NOOP("Card driver error. Please try again."), QT_TR_NOOP("Card driver error"),
743751
pixmap("no-id-card"_L1)};
744752
// TODO: what action should the user take? Should this be fatal?
745753
case RetriableError::SCARD_ERROR:
746-
return {tr("An error occurred in the Smart Card service required to use the ID-card. Make "
747-
"sure that the ID-card and the card reader are connected correctly or relaunch "
748-
"the Smart Card service."),
749-
tr("Operation failed"), pixmap("no-id-card"_L1)};
754+
return {QT_TR_NOOP(
755+
"An error occurred in the Smart Card service required to use the ID-card. Make "
756+
"sure that the ID-card and the card reader are connected correctly or relaunch "
757+
"the Smart Card service."),
758+
QT_TR_NOOP("Operation failed"), pixmap("no-id-card"_L1)};
750759

751760
case RetriableError::UNSUPPORTED_CARD:
752-
return {tr("The card in the reader is not supported. Make sure that the entered ID-card is "
753-
"supported by the Web eID application."),
754-
tr("Operation not supported"), pixmap("no-id-card"_L1)};
761+
return {
762+
QT_TR_NOOP(
763+
"The card in the reader is not supported. Make sure that the entered ID-card is "
764+
"supported by the Web eID application."),
765+
QT_TR_NOOP("Operation not supported"), pixmap("no-id-card"_L1)};
755766

756767
case RetriableError::NO_VALID_CERTIFICATE_AVAILABLE:
757-
return {tr("The inserted ID-card does not contain a certificate for the requested "
758-
"operation. Please insert an ID-card that supports the requested operation."),
759-
tr("Operation not supported"), pixmap("no-id-card"_L1)};
768+
return {QT_TR_NOOP(
769+
"The inserted ID-card does not contain a certificate for the requested "
770+
"operation. Please insert an ID-card that supports the requested operation."),
771+
QT_TR_NOOP("Operation not supported"), pixmap("no-id-card"_L1)};
760772

761773
case RetriableError::PIN_VERIFY_DISABLED:
762774
return {
763-
tr("Operation failed. Make sure that the driver of the corresponding card reader is "
764-
"used. Read more <a "
765-
"href=\"https://www.id.ee/en/article/using-pinpad-card-reader-drivers/\">here</"
766-
"a>."),
767-
tr("Card driver error"), QStringLiteral(":/images/cardreader.svg")};
775+
QT_TR_NOOP(
776+
"Operation failed. Make sure that the driver of the corresponding card reader is "
777+
"used. Read more <a "
778+
"href=\"https://www.id.ee/en/article/using-pinpad-card-reader-drivers/\">here</"
779+
"a>."),
780+
QT_TR_NOOP("Card driver error"), QStringLiteral(":/images/cardreader.svg")};
768781

769782
case RetriableError::UNKNOWN_ERROR:
770-
return {tr("Unknown error"), tr("Unknown error"), pixmap("no-id-card"_L1)};
783+
return {QT_TR_NOOP("Unknown error"), QT_TR_NOOP("Unknown error"), pixmap("no-id-card"_L1)};
771784
}
772-
return {tr("Unknown error"), tr("Unknown error"), pixmap("no-id-card"_L1)};
785+
return {QT_TR_NOOP("Unknown error"), QT_TR_NOOP("Unknown error"), pixmap("no-id-card"_L1)};
773786
}

src/ui/webeiddialog.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ class WebEidDialog final : public WebEidUI
102102
void setupPinPadProgressBarAndEmitWait(const CardCertificateAndPinInfo& certAndPin);
103103
void setupPinInput(const CardCertificateAndPinInfo& certAndPin);
104104
template <typename Func>
105-
void setupOK(Func&& func, const std::function<QString()>& text = {}, bool enabled = false);
105+
void setupOK(Func&& func, const char* text = {}, bool enabled = false);
106106
void displayPinBlockedError();
107107

108108
void showPinInputWarning(bool show);
109109
void resizeHeight();
110110

111111
static QPixmap pixmap(QLatin1String name);
112-
static std::tuple<QString, QString, QPixmap>
112+
static std::tuple<const char*, const char*, QPixmap>
113113
retriableErrorToTextTitleAndIcon(RetriableError error);
114114

115115
class Private;

0 commit comments

Comments
 (0)