From a860b011d7b12ae17d278d36a30eaa7754b7a2ce Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 8 Dec 2017 22:46:00 +0100 Subject: Always guard async callbacks --- framework/src/async.h | 40 +++++++++++++++++++++++++++++ framework/src/domain/composercontroller.cpp | 22 +--------------- framework/src/domain/composercontroller.h | 4 +-- framework/src/domain/mime/messageparser.cpp | 32 ++++++++++------------- 4 files changed, 55 insertions(+), 43 deletions(-) create mode 100644 framework/src/async.h diff --git a/framework/src/async.h b/framework/src/async.h new file mode 100644 index 00000000..34233244 --- /dev/null +++ b/framework/src/async.h @@ -0,0 +1,40 @@ +/* + Copyright (c) 2017 Christian Mollekopf + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ +#pragma once + +#include +#include +#include +#include + +template +void asyncRun(QObject *object, std::function run, std::function continuation) +{ + auto guard = QPointer{object}; + auto future = QtConcurrent::run(run); + auto watcher = new QFutureWatcher; + QObject::connect(watcher, &QFutureWatcher::finished, watcher, [watcher, continuation, guard]() { + if (guard) { + continuation(watcher->future().result()); + } + delete watcher; + }); + watcher->setFuture(future); +} + diff --git a/framework/src/domain/composercontroller.cpp b/framework/src/domain/composercontroller.cpp index 471c30f8..e3218007 100644 --- a/framework/src/domain/composercontroller.cpp +++ b/framework/src/domain/composercontroller.cpp @@ -17,22 +17,16 @@ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - - #include "composercontroller.h" #include #include #include -#include #include #include #include #include #include #include -#include -#include -#include #include #include @@ -40,6 +34,7 @@ #include "recepientautocompletionmodel.h" #include "mime/mailtemplates.h" #include "mime/mailcrypto.h" +#include "async.h" std::vector &operator+=(std::vector &list, const std::vector &add) { @@ -97,21 +92,6 @@ public: } }; -template -void asyncRun(QObject *object, std::function run, std::function continuation) -{ - auto guard = QPointer{object}; - auto future = QtConcurrent::run(run); - auto watcher = new QFutureWatcher; - QObject::connect(watcher, &QFutureWatcher::finished, watcher, [watcher, continuation, guard]() { - if (guard) { - continuation(watcher->future().result()); - } - delete watcher; - }); - watcher->setFuture(future); -} - class AddresseeController : public Kube::ListPropertyController { public: diff --git a/framework/src/domain/composercontroller.h b/framework/src/domain/composercontroller.h index 70a88900..df3c7b1b 100644 --- a/framework/src/domain/composercontroller.h +++ b/framework/src/domain/composercontroller.h @@ -22,10 +22,8 @@ #include #include -#include -#include -#include #include +#include #include #include #include diff --git a/framework/src/domain/mime/messageparser.cpp b/framework/src/domain/mime/messageparser.cpp index 6dc880f3..bc7535ec 100644 --- a/framework/src/domain/mime/messageparser.cpp +++ b/framework/src/domain/mime/messageparser.cpp @@ -18,15 +18,12 @@ */ #include "messageparser.h" +#include + #include "partmodel.h" #include "attachmentmodel.h" #include "modeltest.h" -#include - -#include -#include -#include -#include +#include "async.h" class MessagePartPrivate { @@ -54,19 +51,16 @@ QVariant MessageParser::message() const void MessageParser::setMessage(const QVariant &message) { mRawContent = message.toString(); - auto future = QtConcurrent::run([message] { - auto parser = std::make_shared(); - parser->parseObjectTree(message.toByteArray()); - parser->decryptParts(); - return parser; - }); - auto watcher = new QFutureWatcher>; - QObject::connect(watcher, &QFutureWatcher>::finished, watcher, [this, watcher]() { - d->mParser = watcher->future().result(); - delete watcher; - emit htmlChanged(); - }); - watcher->setFuture(future); + asyncRun>(this, [=] { + auto parser = std::make_shared(); + parser->parseObjectTree(message.toByteArray()); + parser->decryptParts(); + return parser; + }, + [this](const std::shared_ptr &parser) { + d->mParser = parser; + emit htmlChanged(); + }); } QString MessageParser::rawContent() const -- cgit v1.2.3