Skip to content

Commit

Permalink
Dispatch confirmation dialog to main thread (open-eid#1122)
Browse files Browse the repository at this point in the history
IB-7533

Signed-off-by: Raul Metsma <[email protected]>

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma authored Sep 22, 2022
1 parent 9bedece commit dee4599
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 70 deletions.
21 changes: 9 additions & 12 deletions client/DigiDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ QDateTime DigiDocSignature::claimedTime() const
return toTime(s->claimedSigningTime());
}

QString DigiDocSignature::id() const
bool DigiDocSignature::isInvalid() const
{
return from(s->id());
return m_status >= Invalid;
}

QString DigiDocSignature::lastError() const { return m_lastError; }
Expand Down Expand Up @@ -521,12 +521,14 @@ bool DigiDoc::open( const QString &file )
WaitDialogHolder waitDialog(parent, tr("Opening"), false);
return waitFor([&] {
b = Container::openPtr(to(file));
if(b && b->mediaType() == "application/vnd.etsi.asic-s+zip" && b->dataFiles().size() == 1)
if(b && b->mediaType() == "application/vnd.etsi.asic-s+zip" &&
b->dataFiles().size() == 1 &&
b->signatures().size() == 1)
{
const DataFile *f = b->dataFiles().at(0);
if(from(f->fileName()).endsWith(QStringLiteral(".ddoc"), Qt::CaseInsensitive) &&
CheckConnection().check(QStringLiteral("https://id.eesti.ee/config.json")) &&
serviceConfirmation())
dispatchToMain(serviceConfirmation))
{
const QString tmppath = FileDialog::tempPath(FileDialog::safeName(from(f->fileName())));
f->saveAs(to(tmppath));
Expand All @@ -539,16 +541,11 @@ bool DigiDoc::open( const QString &file )
}
}
}
m_fileName = file;
bool isTimeStamped = false;
if(parentContainer &&
parentContainer->dataFiles().size() == 1 &&
parentContainer->signatures().size() == 1 &&
from(parentContainer->dataFiles()[0]->fileName()).endsWith(QStringLiteral(".ddoc"), Qt::CaseInsensitive))
isTimeStamped = parentContainer->signatures()[0]->trustedSigningTime().compare("2018-07-01T00:00:00Z") < 0;
bool isTimeStamped = parentContainer && parentContainer->signatures().at(0)->trustedSigningTime().compare("2018-07-01T00:00:00Z") < 0;
for(const Signature *signature: b->signatures())
m_signatures.append(DigiDocSignature(signature, this, isTimeStamped));
qApp->addRecent( file );
qApp->addRecent(file);
m_fileName = file;
containerState = signatures().isEmpty() ? ContainerState::UnsignedSavedContainer : ContainerState::SignedContainer;
return true;
});
Expand Down
2 changes: 1 addition & 1 deletion client/DigiDoc.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DigiDocSignature

QSslCertificate cert() const;
QDateTime claimedTime() const;
QString id() const;
bool isInvalid() const;
QString lastError() const;
QString location() const;
QStringList locations() const;
Expand Down
4 changes: 1 addition & 3 deletions client/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1085,9 +1085,7 @@ bool MainWindow::validateFiles(const QString &container, const QStringList &file

void MainWindow::warningClicked(const QString &link)
{
if(link.startsWith(QLatin1String("#invalid-signature-")))
emit ui->signContainerPage->details(link.mid(19));
else if(link == QLatin1String("#unblock-PIN1"))
if(link == QLatin1String("#unblock-PIN1"))
ui->accordion->changePin1Clicked (false, true);
else if(link == QLatin1String("#unblock-PIN2"))
ui->accordion->changePin2Clicked (false, true);
Expand Down
18 changes: 18 additions & 0 deletions client/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#pragma once

#include <QEventLoop>
#include <QTimer>
#include <exception>
#include <thread>

Expand All @@ -43,6 +44,23 @@ namespace {
return result;
}

template <typename F>
inline auto dispatchToMain(F&& function) {
typename std::invoke_result<F>::type result{};
QEventLoop l;
QTimer* timer = new QTimer();
timer->moveToThread(qApp->thread());
timer->setSingleShot(true);
QObject::connect(timer, &QTimer::timeout, timer, [&] {
result = function();
l.exit();
timer->deleteLater();
});
QMetaObject::invokeMethod(timer, "start", Qt::QueuedConnection, Q_ARG(int, 0));
l.exec();
return result;
}

inline QString escapeUnicode(const QString &str) {
QString escaped;
escaped.reserve(6 * str.size());
Expand Down
5 changes: 2 additions & 3 deletions client/widgets/ContainerPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ ContainerPage::ContainerPage(QWidget *parent)
mobileCode = QSettings().value(QStringLiteral("MobileCode")).toString();

connect(this, &ContainerPage::moved,this, &ContainerPage::setHeader);
connect(this, &ContainerPage::details, ui->rightPane, &ItemList::details);
connect(ui->changeLocation, &LabelButton::clicked, this, &ContainerPage::forward);
connect(ui->cancel, &LabelButton::clicked, this, &ContainerPage::forward);
connect(ui->save, &LabelButton::clicked, this, &ContainerPage::forward);
Expand Down Expand Up @@ -329,7 +328,7 @@ void ContainerPage::transition(DigiDoc* container)
for(const DigiDocSignature &c: container->timestamps())
{
SignatureItem *item = new SignatureItem(c, container->state(), ui->rightPane);
if(item->isInvalid())
if(c.isInvalid())
addError(item);
ui->rightPane->addHeaderWidget(item);
}
Expand All @@ -338,7 +337,7 @@ void ContainerPage::transition(DigiDoc* container)
for(const DigiDocSignature &c: container->signatures())
{
SignatureItem *item = new SignatureItem(c, container->state(), ui->rightPane);
if(item->isInvalid())
if(c.isInvalid())
addError(item);
ui->rightPane->addWidget(item);
}
Expand Down
1 change: 0 additions & 1 deletion client/widgets/ContainerPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class ContainerPage final : public QWidget
signals:
void action(int code, const QString &info1 = {}, const QString &info2 = {});
void addFiles(const QStringList &files);
void details(const QString &id);
void fileRemoved(int row);
void keysSelected(const QList<CKey> &keys);
void moved(const QString &to);
Expand Down
2 changes: 0 additions & 2 deletions client/widgets/Item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#include "Item.h"

QString Item::id() const { return QString(); }
void Item::idChanged(const SslCertificate & /* cert */) {}
void Item::initTabOrder(QWidget * /* item */) {}
void Item::details() {}
QWidget* Item::lastTabWidget() { return this; }
6 changes: 1 addition & 5 deletions client/widgets/Item.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,10 @@ class Item : public StyledWidget
public:
using StyledWidget::StyledWidget;

virtual QString id() const;
virtual void idChanged(const SslCertificate &cert);
virtual void initTabOrder(QWidget *item);
virtual QWidget* lastTabWidget();

public slots:
virtual void details();
virtual void idChanged(const SslCertificate &cert);

signals:
void add(Item* item);
void remove(Item* item);
Expand Down
14 changes: 0 additions & 14 deletions client/widgets/ItemList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,6 @@ void ItemList::clear()
(*it)->deleteLater();
}

void ItemList::details(const QString &id)
{
for(auto item: items)
{
if(item->id() == id)
emit item->details();
}
}

bool ItemList::eventFilter(QObject *o, QEvent *e)
{
if(o != ui->infoIcon)
Expand Down Expand Up @@ -272,11 +263,6 @@ void ItemList::setRecipientTooltip()
#endif
}

void ItemList::setTerm(const QString &term)
{
ui->txtFind->setText(term);
}

void ItemList::stateChange( ContainerState state )
{
this->state = state;
Expand Down
8 changes: 1 addition & 7 deletions client/widgets/ItemList.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ class ItemList : public QScrollArea
ria::qdigidoc4::ContainerState getState() const;
bool hasItem(const std::function<bool(Item* const)> &cb);
virtual void removeItem(int row);
void setTerm(const QString &term);
virtual void stateChange(ria::qdigidoc4::ContainerState state);

signals:
Expand All @@ -62,16 +61,11 @@ class ItemList : public QScrollArea
void removed(int row);
void search(const QString &term);

public slots:
void details(const QString &id);

protected slots:
virtual void remove(Item *item);

protected:
void changeEvent(QEvent* event) override;
bool eventFilter(QObject *o, QEvent *e) override;
int index(Item *item) const;
virtual void remove(Item *item);

Ui::ItemList* ui;
std::vector<Item*> items;
Expand Down
22 changes: 3 additions & 19 deletions client/widgets/SignatureItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class SignatureItem::Private: public Ui::SignatureItem
explicit Private(DigiDocSignature s): signature(std::move(s)) {}
DigiDocSignature signature;

bool invalid = true;
ria::qdigidoc4::WarningType error = ria::qdigidoc4::NoWarning;
QString nameText;
QString serial;
Expand Down Expand Up @@ -82,7 +81,6 @@ void SignatureItem::init()
ui->serial.clear();
ui->status.clear();
ui->error = ria::qdigidoc4::NoWarning;
ui->invalid = signatureValidity >= DigiDocSignature::Invalid;
if(!cert.isNull())
ui->nameText = cert.toString(cert.showCN() ? QStringLiteral("CN") : QStringLiteral("GN SN")).toHtmlEscaped();
else
Expand All @@ -102,7 +100,7 @@ void SignatureItem::init()
auto color = [](QLatin1String color, const QString &text) {
return QStringLiteral("<font color=\"%1\">%2</font>").arg(color, text);
};
switch( signatureValidity )
switch(ui->signature.status())
{
case DigiDocSignature::Valid:
ui->status = color(QLatin1String("green"), isValid);
Expand Down Expand Up @@ -156,13 +154,9 @@ bool SignatureItem::event(QEvent *event)
return Item::event(event);
}

void SignatureItem::details()
{
(new SignatureDialog(ui->signature, this))->open();
}

bool SignatureItem::eventFilter(QObject *o, QEvent *e)
{
auto details = [this]{ (new SignatureDialog(ui->signature, this))->open(); };
switch(e->type())
{
case QEvent::MouseButtonRelease:
Expand All @@ -185,22 +179,12 @@ ria::qdigidoc4::WarningType SignatureItem::getError() const
return ui->error;
}

QString SignatureItem::id() const
{
return ui->signature.id();
}

void SignatureItem::initTabOrder(QWidget *item)
{
setTabOrder(item, ui->name);
setTabOrder(ui->name, lastTabWidget());
}

bool SignatureItem::isInvalid() const
{
return ui->invalid;
}

bool SignatureItem::isSelfSigned(const QString& cardCode, const QString& mobileCode) const
{
return !ui->serial.isEmpty() && (ui->serial == cardCode || ui->serial == mobileCode);
Expand Down Expand Up @@ -231,7 +215,7 @@ void SignatureItem::updateNameField()
doc.setHtml(ui->status);
QString plain = doc.toPlainText();
auto red = [this](const QString &text) {
return ui->invalid ? QStringLiteral("<font color=\"red\">%1</font>").arg(text) : text;
return ui->signature.isInvalid() ? QStringLiteral("<font color=\"red\">%1</font>").arg(text) : text;
};
if(ui->name->fontMetrics().boundingRect(ui->nameText + " - " + plain).width() < ui->name->width())
ui->name->setText(red(ui->nameText + " - ") + ui->status);
Expand Down
3 changes: 0 additions & 3 deletions client/widgets/SignatureItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,8 @@ class SignatureItem final : public Item
explicit SignatureItem(DigiDocSignature s, ria::qdigidoc4::ContainerState state, QWidget *parent = nullptr);
~SignatureItem() final;

void details() final;
ria::qdigidoc4::WarningType getError() const;
QString id() const final;
void initTabOrder(QWidget *item) final;
bool isInvalid() const;
bool isSelfSigned(const QString& cardCode, const QString& mobileCode) const;
QWidget* lastTabWidget() final;

Expand Down

0 comments on commit dee4599

Please sign in to comment.