Skip to content

Commit d927543

Browse files
authored
Improve progress hiding (#1137)
IB-7449 Signed-off-by: Raul Metsma <raul@metsma.ee> Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 7611dd8 commit d927543

File tree

9 files changed

+41
-38
lines changed

9 files changed

+41
-38
lines changed

client/DigiDoc.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ void DigiDoc::removeSignature( unsigned int num )
602602
if(isError(num >= b->signatures().size(), tr("Missing signature")))
603603
return;
604604
try {
605-
modified = waitFor([this, num] {
605+
modified = waitFor([&] {
606606
b->removeSignature(num);
607607
m_signatures.removeAt(num);
608608
return true;
@@ -627,7 +627,7 @@ bool DigiDoc::saveAs(const QString &filename)
627627
{
628628
try
629629
{
630-
return waitFor([&]{
630+
return waitFor([&] {
631631
parentContainer ? parentContainer->save(to(filename)) : b->save(to(filename));
632632
return true;
633633
});
@@ -694,9 +694,10 @@ bool DigiDoc::sign(const QString &city, const QString &state, const QString &zip
694694
signer->setProfile("time-stamp");
695695
qApp->waitForTSL( fileName() );
696696
digidoc::Signature *s = b->sign(signer);
697-
m_signatures.append(DigiDocSignature(s, this, false));
698-
modified = true;
699-
return true;
697+
return modified = waitFor([&] {
698+
m_signatures.append(DigiDocSignature(s, this, false));
699+
return true;
700+
});
700701
}
701702
catch( const Exception &e )
702703
{

client/MainWindow.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -915,8 +915,7 @@ void MainWindow::sign(F &&sign)
915915
return;
916916
}
917917

918-
AccessCert access(this);
919-
if(!access.validate())
918+
if(!AccessCert(this).validate())
920919
return;
921920

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

943-
access.increment();
942+
AccessCert::increment();
944943
if(!save())
945944
return;
946945

client/QSigner.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ QByteArray QSigner::decrypt(std::function<QByteArray (QCryptoBackend *)> &&func)
213213
return {};
214214
}
215215
} while(status != QCryptoBackend::PinOK);
216-
QByteArray result;
217-
result = waitFor([&]{ return func(d->backend); });
216+
QByteArray result = waitFor(func, d->backend);
218217
QCardLock::instance().exclusiveUnlock();
219218
d->backend->logout();
220219
d->smartcard->reload(); // QSmartCard should also know that PIN1 is blocked.
@@ -421,9 +420,8 @@ std::vector<unsigned char> QSigner::sign(const std::string &method, const std::v
421420
throwException((tr("Failed to login token") + " " + QCryptoBackend::errorString(status)), Exception::PINFailed)
422421
}
423422
} while(status != QCryptoBackend::PinOK);
424-
QByteArray sig = waitFor([&]{
425-
return d->backend->sign(methodToNID(method), QByteArray::fromRawData((const char*)digest.data(), int(digest.size())));
426-
});
423+
QByteArray sig = waitFor(&QCryptoBackend::sign, d->backend,
424+
methodToNID(method), QByteArray::fromRawData((const char*)digest.data(), int(digest.size())));
427425
QCardLock::instance().exclusiveUnlock();
428426
d->backend->logout();
429427
d->smartcard->reload(); // QSmartCard should also know that PIN2 info is updated

client/QSmartCard.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ QPCSCReader::Result Card::transfer(QPCSCReader *reader, bool verify, const QByte
112112
if(Common::language() == QLatin1String("en")) language = 0x0409;
113113
else if(Common::language() == QLatin1String("et")) language = 0x0425;
114114
else if(Common::language() == QLatin1String("ru")) language = 0x0419;
115-
QPCSCReader::Result result;
116-
return waitFor([&]{
117-
return reader->transferCTL(apdu, verify, language, QSmartCardData::minPinLen(type), newPINOffset, requestCurrentPIN);
118-
});
115+
return waitFor(&QPCSCReader::transferCTL, reader,
116+
apdu, verify, language, QSmartCardData::minPinLen(type), newPINOffset, requestCurrentPIN);
119117
}
120118

121119

client/Utils.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@
2525
#include <thread>
2626

2727
namespace {
28-
template <typename F>
29-
inline auto waitFor(F&& function) {
28+
template <typename F, class... Args>
29+
inline auto waitFor(F&& function, Args&& ...args) {
3030
std::exception_ptr exception;
31-
typename std::invoke_result<F>::type result{};
31+
std::invoke_result_t<F,Args...> result{};
3232
QEventLoop l;
33-
std::thread([&]{
33+
// c++20 ... args == std::forward<Args>(args)
34+
std::thread([&, function = std::forward<F>(function)]{
3435
try {
35-
result = function();
36+
result = std::invoke(function, args...);
3637
} catch(...) {
3738
exception = std::current_exception();
3839
}
@@ -44,15 +45,15 @@ namespace {
4445
return result;
4546
}
4647

47-
template <typename F>
48-
inline auto dispatchToMain(F&& function) {
49-
typename std::invoke_result<F>::type result{};
48+
template <typename F, class... Args>
49+
inline auto dispatchToMain(F&& function, Args&& ...args) {
50+
std::invoke_result_t<F,Args...> result{};
5051
QEventLoop l;
5152
QTimer* timer = new QTimer();
5253
timer->moveToThread(qApp->thread());
5354
timer->setSingleShot(true);
54-
QObject::connect(timer, &QTimer::timeout, timer, [&] {
55-
result = function();
55+
QObject::connect(timer, &QTimer::timeout, timer, [&, function = std::forward<F>(function)] {
56+
result = std::invoke(function, args...);
5657
l.exit();
5758
timer->deleteLater();
5859
});

client/dialogs/AccessCert.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ QSslCertificate AccessCert::cert()
8585
Application::confValue( Application::PKCS12Pass ).toString() ).certificate();
8686
}
8787

88-
unsigned int AccessCert::count(const QString &date) const
88+
unsigned int AccessCert::count(const QString &date)
8989
{
9090
return QByteArray::fromBase64(QSettings().value(date).toByteArray()).toUInt();
9191
}
@@ -99,7 +99,7 @@ void AccessCert::increment()
9999
}
100100
}
101101

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

118-
SecItemImportExportKeyParameters params = {};
119-
params.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
120-
params.flags = kSecKeyImportOnlyOne|kSecKeyNoAccessControl;
118+
SecItemImportExportKeyParameters params{
119+
SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION,
120+
kSecKeyImportOnlyOne|kSecKeyNoAccessControl,
121+
password.toCFString(),
122+
};
121123
CFTypeRef keyAttributes[] = { kSecAttrIsPermanent, kSecAttrIsExtractable };
122124
params.keyAttributes = CFArrayCreate(nullptr,
123125
(const void **)keyAttributes, sizeof(keyAttributes) / sizeof(keyAttributes[0]), nullptr);
124126
CFTypeRef keyUsage[] = { kSecAttrCanDecrypt, kSecAttrCanUnwrap, kSecAttrCanDerive };
125127
params.keyUsage = CFArrayCreate(nullptr,
126128
(const void **)keyUsage, sizeof(keyUsage) / sizeof(keyUsage[0]), nullptr);
127-
params.passphrase = password.toCFString();
128129

129130
SecKeychainRef keychain;
130131
SecKeychainCopyDefault( &keychain );

client/dialogs/AccessCert.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class AccessCert final: public WarningDialog
3333
bool validate();
3434

3535
static QSslCertificate cert();
36-
void increment();
36+
static void increment();
3737
bool installCert( const QByteArray &data, const QString &password );
3838
void remove();
3939

4040
private:
41-
unsigned int count( const QString &date ) const;
42-
bool isDefaultCert( const QSslCertificate &cert ) const;
41+
static unsigned int count(const QString &date);
42+
static bool isDefaultCert(const QSslCertificate &cert);
4343
void showWarning( const QString &msg );
4444

4545
class Private;

client/dialogs/MobileProgress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ class MobileProgress::Private final: public QDialog, public Ui::MobileProgress
7878
#ifdef QT_WIN_EXTRAS
7979
QWinTaskbarButton *taskbar = nullptr;
8080
#endif
81-
WaitDialogHider hider;
8281
};
8382

8483
MobileProgress::MobileProgress(QWidget *parent)
@@ -368,6 +367,7 @@ std::vector<unsigned char> MobileProgress::sign(const std::string &method, const
368367
d->manager->post(d->req, data);
369368
d->statusTimer->start();
370369
d->adjustSize();
370+
WaitDialogHider hider;
371371
d->show();
372372
switch(d->l.exec())
373373
{

client/dialogs/SmartIDProgress.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class SmartIDProgress::Private final: public QDialog, public Ui::MobileProgress
5959
QString URL() { return !UUID.isNull() && useCustomUUID ? SKURL : PROXYURL; }
6060
using QDialog::QDialog;
6161
void reject() final { l.exit(QDialog::Rejected); }
62+
void setVisible(bool visible) final {
63+
if(visible && !hider) hider = std::make_unique<WaitDialogHider>();
64+
QDialog::setVisible(visible);
65+
if(!visible && hider) hider.reset();
66+
}
6267
QTimeLine *statusTimer = nullptr;
6368
QNetworkAccessManager *manager = nullptr;
6469
QNetworkRequest req;
@@ -80,7 +85,7 @@ class SmartIDProgress::Private final: public QDialog, public Ui::MobileProgress
8085
#ifdef QT_WIN_EXTRAS
8186
QWinTaskbarButton *taskbar = nullptr;
8287
#endif
83-
WaitDialogHider hider;
88+
std::unique_ptr<WaitDialogHider> hider;
8489
};
8590

8691

0 commit comments

Comments
 (0)