Skip to content

Commit

Permalink
Improve progress hiding (open-eid#1137)
Browse files Browse the repository at this point in the history
IB-7449

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

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma authored Nov 24, 2022
1 parent 7611dd8 commit d927543
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 38 deletions.
11 changes: 6 additions & 5 deletions client/DigiDoc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ void DigiDoc::removeSignature( unsigned int num )
if(isError(num >= b->signatures().size(), tr("Missing signature")))
return;
try {
modified = waitFor([this, num] {
modified = waitFor([&] {
b->removeSignature(num);
m_signatures.removeAt(num);
return true;
Expand All @@ -627,7 +627,7 @@ bool DigiDoc::saveAs(const QString &filename)
{
try
{
return waitFor([&]{
return waitFor([&] {
parentContainer ? parentContainer->save(to(filename)) : b->save(to(filename));
return true;
});
Expand Down Expand Up @@ -694,9 +694,10 @@ bool DigiDoc::sign(const QString &city, const QString &state, const QString &zip
signer->setProfile("time-stamp");
qApp->waitForTSL( fileName() );
digidoc::Signature *s = b->sign(signer);
m_signatures.append(DigiDocSignature(s, this, false));
modified = true;
return true;
return modified = waitFor([&] {
m_signatures.append(DigiDocSignature(s, this, false));
return true;
});
}
catch( const Exception &e )
{
Expand Down
5 changes: 2 additions & 3 deletions client/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,8 +915,7 @@ void MainWindow::sign(F &&sign)
return;
}

AccessCert access(this);
if(!access.validate())
if(!AccessCert(this).validate())
return;

QString role, city, state, country, zip;
Expand All @@ -940,7 +939,7 @@ void MainWindow::sign(F &&sign)
else if(!sign(city, state, zip, country, role))
return;

access.increment();
AccessCert::increment();
if(!save())
return;

Expand Down
8 changes: 3 additions & 5 deletions client/QSigner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,7 @@ QByteArray QSigner::decrypt(std::function<QByteArray (QCryptoBackend *)> &&func)
return {};
}
} while(status != QCryptoBackend::PinOK);
QByteArray result;
result = waitFor([&]{ return func(d->backend); });
QByteArray result = waitFor(func, d->backend);
QCardLock::instance().exclusiveUnlock();
d->backend->logout();
d->smartcard->reload(); // QSmartCard should also know that PIN1 is blocked.
Expand Down Expand Up @@ -421,9 +420,8 @@ std::vector<unsigned char> QSigner::sign(const std::string &method, const std::v
throwException((tr("Failed to login token") + " " + QCryptoBackend::errorString(status)), Exception::PINFailed)
}
} while(status != QCryptoBackend::PinOK);
QByteArray sig = waitFor([&]{
return d->backend->sign(methodToNID(method), QByteArray::fromRawData((const char*)digest.data(), int(digest.size())));
});
QByteArray sig = waitFor(&QCryptoBackend::sign, d->backend,
methodToNID(method), QByteArray::fromRawData((const char*)digest.data(), int(digest.size())));
QCardLock::instance().exclusiveUnlock();
d->backend->logout();
d->smartcard->reload(); // QSmartCard should also know that PIN2 info is updated
Expand Down
6 changes: 2 additions & 4 deletions client/QSmartCard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,8 @@ QPCSCReader::Result Card::transfer(QPCSCReader *reader, bool verify, const QByte
if(Common::language() == QLatin1String("en")) language = 0x0409;
else if(Common::language() == QLatin1String("et")) language = 0x0425;
else if(Common::language() == QLatin1String("ru")) language = 0x0419;
QPCSCReader::Result result;
return waitFor([&]{
return reader->transferCTL(apdu, verify, language, QSmartCardData::minPinLen(type), newPINOffset, requestCurrentPIN);
});
return waitFor(&QPCSCReader::transferCTL, reader,
apdu, verify, language, QSmartCardData::minPinLen(type), newPINOffset, requestCurrentPIN);
}


Expand Down
21 changes: 11 additions & 10 deletions client/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@
#include <thread>

namespace {
template <typename F>
inline auto waitFor(F&& function) {
template <typename F, class... Args>
inline auto waitFor(F&& function, Args&& ...args) {
std::exception_ptr exception;
typename std::invoke_result<F>::type result{};
std::invoke_result_t<F,Args...> result{};
QEventLoop l;
std::thread([&]{
// c++20 ... args == std::forward<Args>(args)
std::thread([&, function = std::forward<F>(function)]{
try {
result = function();
result = std::invoke(function, args...);
} catch(...) {
exception = std::current_exception();
}
Expand All @@ -44,15 +45,15 @@ namespace {
return result;
}

template <typename F>
inline auto dispatchToMain(F&& function) {
typename std::invoke_result<F>::type result{};
template <typename F, class... Args>
inline auto dispatchToMain(F&& function, Args&& ...args) {
std::invoke_result_t<F,Args...> result{};
QEventLoop l;
QTimer* timer = new QTimer();
timer->moveToThread(qApp->thread());
timer->setSingleShot(true);
QObject::connect(timer, &QTimer::timeout, timer, [&] {
result = function();
QObject::connect(timer, &QTimer::timeout, timer, [&, function = std::forward<F>(function)] {
result = std::invoke(function, args...);
l.exit();
timer->deleteLater();
});
Expand Down
13 changes: 7 additions & 6 deletions client/dialogs/AccessCert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ QSslCertificate AccessCert::cert()
Application::confValue( Application::PKCS12Pass ).toString() ).certificate();
}

unsigned int AccessCert::count(const QString &date) const
unsigned int AccessCert::count(const QString &date)
{
return QByteArray::fromBase64(QSettings().value(date).toByteArray()).toUInt();
}
Expand All @@ -99,7 +99,7 @@ void AccessCert::increment()
}
}

bool AccessCert::isDefaultCert(const QSslCertificate &cert) const
bool AccessCert::isDefaultCert(const QSslCertificate &cert)
{
static const QList<QByteArray> list {
// CN=Riigi Infos\xC3\xBCsteemi Amet, SN = da:98:09:46:6d:57:51:65:48:8b:b2:14:0d:9e:19:27
Expand All @@ -115,16 +115,17 @@ bool AccessCert::installCert( const QByteArray &data, const QString &password )
SecExternalFormat format = kSecFormatPKCS12;
SecExternalItemType type = kSecItemTypeAggregate;

SecItemImportExportKeyParameters params = {};
params.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
params.flags = kSecKeyImportOnlyOne|kSecKeyNoAccessControl;
SecItemImportExportKeyParameters params{
SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION,
kSecKeyImportOnlyOne|kSecKeyNoAccessControl,
password.toCFString(),
};
CFTypeRef keyAttributes[] = { kSecAttrIsPermanent, kSecAttrIsExtractable };
params.keyAttributes = CFArrayCreate(nullptr,
(const void **)keyAttributes, sizeof(keyAttributes) / sizeof(keyAttributes[0]), nullptr);
CFTypeRef keyUsage[] = { kSecAttrCanDecrypt, kSecAttrCanUnwrap, kSecAttrCanDerive };
params.keyUsage = CFArrayCreate(nullptr,
(const void **)keyUsage, sizeof(keyUsage) / sizeof(keyUsage[0]), nullptr);
params.passphrase = password.toCFString();

SecKeychainRef keychain;
SecKeychainCopyDefault( &keychain );
Expand Down
6 changes: 3 additions & 3 deletions client/dialogs/AccessCert.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class AccessCert final: public WarningDialog
bool validate();

static QSslCertificate cert();
void increment();
static void increment();
bool installCert( const QByteArray &data, const QString &password );
void remove();

private:
unsigned int count( const QString &date ) const;
bool isDefaultCert( const QSslCertificate &cert ) const;
static unsigned int count(const QString &date);
static bool isDefaultCert(const QSslCertificate &cert);
void showWarning( const QString &msg );

class Private;
Expand Down
2 changes: 1 addition & 1 deletion client/dialogs/MobileProgress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class MobileProgress::Private final: public QDialog, public Ui::MobileProgress
#ifdef QT_WIN_EXTRAS
QWinTaskbarButton *taskbar = nullptr;
#endif
WaitDialogHider hider;
};

MobileProgress::MobileProgress(QWidget *parent)
Expand Down Expand Up @@ -368,6 +367,7 @@ std::vector<unsigned char> MobileProgress::sign(const std::string &method, const
d->manager->post(d->req, data);
d->statusTimer->start();
d->adjustSize();
WaitDialogHider hider;
d->show();
switch(d->l.exec())
{
Expand Down
7 changes: 6 additions & 1 deletion client/dialogs/SmartIDProgress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class SmartIDProgress::Private final: public QDialog, public Ui::MobileProgress
QString URL() { return !UUID.isNull() && useCustomUUID ? SKURL : PROXYURL; }
using QDialog::QDialog;
void reject() final { l.exit(QDialog::Rejected); }
void setVisible(bool visible) final {
if(visible && !hider) hider = std::make_unique<WaitDialogHider>();
QDialog::setVisible(visible);
if(!visible && hider) hider.reset();
}
QTimeLine *statusTimer = nullptr;
QNetworkAccessManager *manager = nullptr;
QNetworkRequest req;
Expand All @@ -80,7 +85,7 @@ class SmartIDProgress::Private final: public QDialog, public Ui::MobileProgress
#ifdef QT_WIN_EXTRAS
QWinTaskbarButton *taskbar = nullptr;
#endif
WaitDialogHider hider;
std::unique_ptr<WaitDialogHider> hider;
};


Expand Down

0 comments on commit d927543

Please sign in to comment.