From 3b052a030cb375ffc4e5f43f6ddc914bed169ee1 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Sat, 25 Nov 2017 01:32:30 +0100 Subject: Factor out how we run it into a separate method --- framework/src/domain/composercontroller.cpp | 62 ++++++++++++++++------------- 1 file changed, 35 insertions(+), 27 deletions(-) (limited to 'framework') diff --git a/framework/src/domain/composercontroller.cpp b/framework/src/domain/composercontroller.cpp index b6079f77..aa8dea5d 100644 --- a/framework/src/domain/composercontroller.cpp +++ b/framework/src/domain/composercontroller.cpp @@ -101,6 +101,19 @@ static void traverse(const QStandardItemModel *model, const std::function +void asyncRun(std::function run, std::function continuation) +{ + auto future = QtConcurrent::run(run); + auto watcher = new QFutureWatcher; + //FIXME holding on to the item pointer like this is not safe + QObject::connect(watcher, &QFutureWatcher::finished, watcher, [watcher, continuation]() { + continuation(watcher->future().result()); + delete watcher; + }); + watcher->setFuture(future); +} + class AddresseeModel : public QStandardItemModel { public: @@ -131,34 +144,29 @@ public: mb.fromUnicodeString(addressee); SinkLog() << "Searching key for: " << mb.address(); - - 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."; + asyncRun>([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); } - SinkLog() << "Found key: " << keys.front().primaryFingerprint(); - item->setData(true, ComposerController::KeyFoundRole); - item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole); - } else { - SinkWarning() << "Failed to find key for recipient."; - } - }); - watcher->setFuture(future); + return keys; + }, + //FIXME holding on to the item pointer like this is not safe + [this, item](const std::vector &keys) { + if (!keys.empty()) { + if (keys.size() > 1 ) { + SinkWarning() << "Found more than one key, picking first one."; + } + SinkLog() << "Found key: " << keys.front().primaryFingerprint(); + item->setData(true, ComposerController::KeyFoundRole); + item->setData(QVariant::fromValue(keys.front()), ComposerController::KeyRole); + } else { + SinkWarning() << "Failed to find key for recipient."; + } + }); } void remove(const QString &addressee) -- cgit v1.2.3