From 3ddc7f0130c7f56df5560add727e89dbd1aeea90 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 25 Nov 2017 01:25:47 +0100 Subject: Lookup keys in threads --- framework/src/domain/composercontroller.cpp | 30 ++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/framework/src/domain/composercontroller.cpp b/framework/src/domain/composercontroller.cpp index 59c18cb6..b6079f77 100644 --- a/framework/src/domain/composercontroller.cpp +++ b/framework/src/domain/composercontroller.cpp @@ -30,6 +30,9 @@ #include #include #include +#include +#include +#include #include #include @@ -128,14 +131,22 @@ public: mb.fromUnicodeString(addressee); SinkLog() << "Searching key for: " << mb.address(); - auto keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, false, MailCrypto::OPENPGP); - if (keys.empty()) { - //Search for key on remote server if it's missing and import - //TODO: this is blocking and thus blocks the UI - keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, true, MailCrypto::OPENPGP); - MailCrypto::importKeys(keys); - } - if (item) { + + auto future = QtConcurrent::run([mb] { + auto keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, false, MailCrypto::OPENPGP); + if (keys.empty()) { + //Search for key on remote server if it's missing and import + //TODO: this is blocking and thus blocks the UI + keys = MailCrypto::findKeys(QStringList{} << mb.address(), false, true, MailCrypto::OPENPGP); + MailCrypto::importKeys(keys); + } + return keys; + }); + auto watcher = new QFutureWatcher>; + //FIXME holding on to the item pointer like this is not safe + QObject::connect(watcher, &QFutureWatcher>::finished, watcher, [this, watcher, item]() { + auto keys = watcher->future().result(); + delete watcher; if (!keys.empty()) { if (keys.size() > 1 ) { SinkWarning() << "Found more than one key, picking first one."; @@ -146,7 +157,8 @@ public: } else { SinkWarning() << "Failed to find key for recipient."; } - } + }); + watcher->setFuture(future); } void remove(const QString &addressee) -- cgit v1.2.3