Skip to content

Commit

Permalink
More verbose error messages (open-eid#1103)
Browse files Browse the repository at this point in the history
IB-7308

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma authored Feb 9, 2023
1 parent 5ea92a9 commit c2582fa
Show file tree
Hide file tree
Showing 21 changed files with 667 additions and 935 deletions.
24 changes: 5 additions & 19 deletions client/IKValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
#include <QtCore/QDate>
#include <QtCore/QStringList>

IKValidator::IKValidator(QObject *parent)
: QValidator(parent)
{}

QDate IKValidator::birthDate(const QString &ik)
{
if(ik.size() != 11) return {};
Expand All @@ -41,9 +37,9 @@ QDate IKValidator::birthDate(const QString &ik)
}

QDate date(
ik.mid(1, 2).toUInt() + year,
ik.mid(3, 2).toUInt(),
ik.mid(5, 2).toUInt() );
ik.mid(1, 2).toInt() + year,
ik.mid(3, 2).toInt(),
ik.mid(5, 2).toInt());
return date.isValid() ? date : QDate();
}

Expand Down Expand Up @@ -76,8 +72,8 @@ bool IKValidator::isValid(const QString &ik)
int sum1 = 0, sum2 = 0;
for(int i = 0, pos1 = 1, pos2 = 3; i < 10; ++i)
{
sum1 += ik.mid(i, 1).toUInt() * pos1;
sum2 += ik.mid(i, 1).toUInt() * pos2;
sum1 += ik.mid(i, 1).toInt() * pos1;
sum2 += ik.mid(i, 1).toInt() * pos2;
pos1 = pos1 == 9 ? 1 : pos1 + 1;
pos2 = pos2 == 9 ? 1 : pos2 + 1;
}
Expand All @@ -90,16 +86,6 @@ bool IKValidator::isValid(const QString &ik)
return ik.right( 1 ).toInt() == result;
}

QValidator::State IKValidator::validate(QString &input, int &/*pos*/) const
{
input = input.trimmed();
if(input.size() > 11 || !QRegularExpression(QStringLiteral("\\d{0,11}")).match(input).hasMatch())
return Invalid;
if(input.size() == 11)
return isValid(input) ? Acceptable : Invalid;
return Intermediate;
}



NumberValidator::NumberValidator(QObject *parent)
Expand Down
6 changes: 1 addition & 5 deletions client/IKValidator.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@

#include <QtGui/QValidator>

class IKValidator: public QValidator
class IKValidator
{
Q_OBJECT
public:
explicit IKValidator(QObject *parent);

static QDate birthDate(const QString &ik);
static bool isValid(const QString &ik);
State validate(QString &input, int &pos) const;
};


Expand Down
2 changes: 1 addition & 1 deletion client/dialogs/AddRecipients.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ AddRecipients::AddRecipients(ItemList* itemList, QWidget *parent)
ui->actionLayout->setDirection(QBoxLayout::RightToLeft);
#endif
setWindowFlags( Qt::Dialog | Qt::CustomizeWindowHint );
new Overlay(this, parent->topLevelWidget());
new Overlay(this, parent);

ui->leftPane->init(ria::qdigidoc4::ToAddAdresses, QT_TRANSLATE_NOOP("ItemList", "Add recipients"));
ui->leftPane->setFont(Styles::font(Styles::Regular, 20));
Expand Down
4 changes: 1 addition & 3 deletions client/dialogs/CertificateDetails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ CertificateDetails::CertificateDetails(const SslCertificate &cert, QWidget *pare
#else
setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint);
#endif
Overlay *overlay = new Overlay(parent->topLevelWidget());
overlay->show();
connect(this, &CertificateDetails::destroyed, overlay, &Overlay::deleteLater);
new Overlay(this, parent);

QFont headerFont = Styles::font( Styles::Regular, 18 );
QFont regularFont = Styles::font( Styles::Regular, 14 );
Expand Down
2 changes: 1 addition & 1 deletion client/dialogs/KeyDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ KeyDialog::KeyDialog( const CKey &k, QWidget *parent )
d->buttonLayout->setDirection(QBoxLayout::RightToLeft);
#endif
setWindowFlag(Qt::CustomizeWindowHint);
new Overlay(this, parent->topLevelWidget());
new Overlay(this, parent);

QFont condensed = Styles::font(Styles::Condensed, 12);
QFont regular = Styles::font(Styles::Regular, 14);
Expand Down
89 changes: 50 additions & 39 deletions client/dialogs/MobileDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "MobileDialog.h"
#include "ui_MobileDialog.h"

#include "IKValidator.h"
#include "Styles.h"
#include "dialogs/SettingsDialog.h"
Expand All @@ -33,76 +34,86 @@ MobileDialog::MobileDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::MobileDialog)
{
new Overlay(this, parent->topLevelWidget());
new Overlay(this, parent);
ui->setupUi(this);
setWindowFlags( Qt::Dialog | Qt::FramelessWindowHint );
setFixedSize( size() );
#if defined (Q_OS_WIN)
setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);
setFixedSize(size());
#ifdef Q_OS_WIN
ui->buttonLayout->setDirection(QBoxLayout::RightToLeft);
#endif

connect( ui->sign, &QPushButton::clicked, this, &MobileDialog::accept );
connect( ui->cancel, &QPushButton::clicked, this, &MobileDialog::reject );
connect( this, &MobileDialog::finished, this, &MobileDialog::close );

QFont condensed = Styles::font(Styles::Condensed, 14);
QFont header = Styles::font(Styles::Regular, 16, QFont::DemiBold);
QFont regularFont = Styles::font(Styles::Regular, 14);
ui->labelNameId->setFont(header);
ui->labelNameId->setFont(Styles::font(Styles::Regular, 16, QFont::DemiBold));
ui->labelPhone->setFont(regularFont);
ui->labelCode->setFont(regularFont);
ui->errorCode->setFont(regularFont);
ui->errorPhone->setFont(regularFont);
ui->phoneNo->setFont(regularFont);
ui->idCode->setFont(regularFont);
ui->cbRemember->setFont(regularFont);
ui->sign->setFont(condensed);
ui->cancel->setFont(condensed);

// Mobile
ui->idCode->setValidator( new IKValidator( ui->idCode ) );
ui->idCode->setValidator(new NumberValidator(ui->idCode));
ui->idCode->setText(QSettings().value(QStringLiteral("MobileCode")).toString());
ui->idCode->setAttribute(Qt::WA_MacShowFocusRect, false);
ui->phoneNo->setValidator( new NumberValidator( ui->phoneNo ) );
ui->phoneNo->setValidator(new NumberValidator(ui->phoneNo));
ui->phoneNo->setText(QSettings().value(QStringLiteral("MobileNumber"), COUNTRY_CODE_EST).toString());
ui->phoneNo->setAttribute(Qt::WA_MacShowFocusRect, false);
connect(ui->idCode, &QLineEdit::returnPressed, ui->sign, &QPushButton::click);
connect(ui->phoneNo, &QLineEdit::returnPressed, ui->sign, &QPushButton::click);
ui->cbRemember->setChecked(QSettings().value(QStringLiteral("MobileSettings"), true).toBool());
ui->cbRemember->setAttribute(Qt::WA_MacShowFocusRect, false);
connect(ui->idCode, &QLineEdit::textEdited, this, &MobileDialog::enableSign);
connect(ui->phoneNo, &QLineEdit::textEdited, this, &MobileDialog::enableSign);
connect(ui->cbRemember, &QCheckBox::clicked, this, [=](bool checked) {
auto saveSettings = [this] {
bool checked = ui->cbRemember->isChecked();
SettingsDialog::setValueEx(QStringLiteral("MobileSettings"), checked, true);
SettingsDialog::setValueEx(QStringLiteral("MobileCode"), checked ? ui->idCode->text() : QString());
SettingsDialog::setValueEx(QStringLiteral("MobileNumber"), checked ? ui->phoneNo->text() : QString());
SettingsDialog::setValueEx(QStringLiteral("MobileSettings"), checked, true);
};
connect(ui->idCode, &QLineEdit::returnPressed, ui->sign, &QPushButton::click);
connect(ui->idCode, &QLineEdit::textEdited, this, saveSettings);
connect(ui->phoneNo, &QLineEdit::returnPressed, ui->sign, &QPushButton::click);
connect(ui->phoneNo, &QLineEdit::textEdited, this, saveSettings);
connect(ui->cbRemember, &QCheckBox::clicked, this, saveSettings);
connect(ui->sign, &QPushButton::clicked, this, [this] {
static const QStringList countryCodes {COUNTRY_CODE_EST, COUNTRY_CODE_LTU};
if(!IKValidator::isValid(idCode()))
{
ui->idCode->setStyleSheet(QStringLiteral("border-color: #c53e3e"));
ui->errorCode->setText(tr("Personal code is not valid"));
}
else
{
ui->idCode->setStyleSheet({});
ui->errorCode->clear();
}
if(phoneNo().size() < 8 || countryCodes.contains(phoneNo()))
{
ui->phoneNo->setStyleSheet(QStringLiteral("border-color: #c53e3e"));
ui->errorPhone->setText(tr("Phone number is not entered"));
}
else if(!countryCodes.contains(phoneNo().left(3)))
{
ui->phoneNo->setStyleSheet(QStringLiteral("border-color: #c53e3e"));
ui->errorPhone->setText(tr("Invalid country code"));
}
else
{
ui->phoneNo->setStyleSheet({});
ui->errorPhone->clear();
}
if(ui->errorCode->text().isEmpty() && ui->errorPhone->text().isEmpty())
accept();
});

enableSign();
connect(ui->cancel, &QPushButton::clicked, this, &MobileDialog::reject);
connect(this, &MobileDialog::finished, this, &MobileDialog::close);
}

MobileDialog::~MobileDialog()
{
delete ui;
}

void MobileDialog::enableSign()
{
static const QStringList countryCodes {COUNTRY_CODE_EST, COUNTRY_CODE_LTU};
if( ui->cbRemember->isChecked() )
{
SettingsDialog::setValueEx(QStringLiteral("MobileCode"), ui->idCode->text());
SettingsDialog::setValueEx(QStringLiteral("MobileNumber"), ui->phoneNo->text(),
ui->phoneNo->text() == COUNTRY_CODE_EST ? COUNTRY_CODE_EST : QString());
}
ui->sign->setToolTip(QString());
if( !IKValidator::isValid( ui->idCode->text() ) )
ui->sign->setToolTip( tr("Personal code is not valid") );
if(ui->phoneNo->text().size() < 8 || countryCodes.contains(ui->phoneNo->text()))
ui->sign->setToolTip( tr("Phone number is not entered") );
if(!countryCodes.contains(ui->phoneNo->text().left(3)))
ui->sign->setToolTip(tr("Invalid country code"));
ui->sign->setEnabled( ui->sign->toolTip().isEmpty() );
}

QString MobileDialog::idCode()
{
return ui->idCode->text();
Expand Down
6 changes: 1 addition & 5 deletions client/dialogs/MobileDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@

#include <QDialog>

namespace Ui {
class MobileDialog;
}
namespace Ui { class MobileDialog; }

class MobileDialog final : public QDialog
{
Expand All @@ -37,8 +35,6 @@ class MobileDialog final : public QDialog
QString phoneNo();

private:
void enableSign();

Ui::MobileDialog *ui;
};

59 changes: 27 additions & 32 deletions client/dialogs/MobileDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
<ui version="4.0">
<class>MobileDialog</class>
<widget class="QDialog" name="MobileDialog">
<property name="windowModality">
<enum>Qt::WindowModal</enum>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>380</width>
<height>282</height>
<width>382</width>
<height>301</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -22,6 +19,12 @@ background-color: #FFFFFF;
color: #000000;
border-radius: 2px;
}
#labelCode, #labelPhone {
color: #353739
}
#errorCode, #errorPhone {
color: #c53e3e
}
QLineEdit {
padding: 0px 10px;
border: 1px solid #DEE4E9;
Expand Down Expand Up @@ -118,7 +121,7 @@ background-color: #BEDBED;
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>67</height>
<height>8</height>
</size>
</property>
</spacer>
Expand All @@ -131,9 +134,6 @@ background-color: #BEDBED;
<pointsize>12</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: #353739;</string>
</property>
<property name="text">
<string>Country code and phone number</string>
</property>
Expand All @@ -155,6 +155,15 @@ background-color: #BEDBED;
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="errorPhone">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="labelCode">
<property name="font">
Expand All @@ -163,10 +172,6 @@ background-color: #BEDBED;
<pointsize>12</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: #353739;
padding-top: 8px;</string>
</property>
<property name="text">
<string>Personal code</string>
</property>
Expand All @@ -189,20 +194,13 @@ padding-top: 8px;</string>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>8</height>
</size>
<widget class="QLabel" name="errorCode">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
</spacer>
</widget>
</item>
<item>
<widget class="CheckBox" name="cbRemember">
Expand All @@ -219,7 +217,7 @@ padding-top: 8px;</string>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>66</height>
<height>8</height>
</size>
</property>
</spacer>
Expand All @@ -233,7 +231,7 @@ padding-top: 8px;</string>
<widget class="QPushButton" name="cancel">
<property name="minimumSize">
<size>
<width>120</width>
<width>0</width>
<height>30</height>
</size>
</property>
Expand All @@ -258,12 +256,9 @@ padding-top: 8px;</string>
</item>
<item>
<widget class="QPushButton" name="sign">
<property name="enabled">
<bool>false</bool>
</property>
<property name="minimumSize">
<size>
<width>120</width>
<width>0</width>
<height>30</height>
</size>
</property>
Expand Down
Loading

0 comments on commit c2582fa

Please sign in to comment.