diff options
Diffstat (limited to 'Swiften/EventLoop/Qt/QtEventLoop.h')
-rw-r--r-- | Swiften/EventLoop/Qt/QtEventLoop.h | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/Swiften/EventLoop/Qt/QtEventLoop.h b/Swiften/EventLoop/Qt/QtEventLoop.h index 0097cf9..cf374ab 100644 --- a/Swiften/EventLoop/Qt/QtEventLoop.h +++ b/Swiften/EventLoop/Qt/QtEventLoop.h @@ -1,47 +1,59 @@ /* - * Copyright (c) 2010 Remko Tronçon - * Licensed under the GNU General Public License v3. - * See Documentation/Licenses/GPLv3.txt for more information. + * Copyright (c) 2010-2019 Isode Limited. + * All rights reserved. + * See the COPYING file for more information. */ #pragma once -#include <QObject> -#include <QEvent> +#include <mutex> + #include <QCoreApplication> +#include <QEvent> +#include <QObject> #include <Swiften/EventLoop/EventLoop.h> namespace Swift { - class QtEventLoop : public QObject, public EventLoop { - public: - QtEventLoop() {} - ~QtEventLoop() { - QCoreApplication::removePostedEvents(this); - } - - virtual void post(const Swift::Event& event) { - QCoreApplication::postEvent(this, new Event(event)); - } - - virtual bool event(QEvent* qevent) { - Event* event = dynamic_cast<Event*>(qevent); - if (event) { - handleEvent(event->event_); - //event->deleteLater(); FIXME: Leak? - return true; - } - - return false; - } - - private: - struct Event : public QEvent { - Event(const Swift::Event& event) : - QEvent(QEvent::User), event_(event) { - } - - Swift::Event event_; - }; - }; + class QtEventLoop : public QObject, public EventLoop { + public: + QtEventLoop() : isEventInQtEventLoop_(false) {} + virtual ~QtEventLoop() { + QCoreApplication::removePostedEvents(this); + } + + protected: + virtual void eventPosted() { + std::unique_lock<std::recursive_mutex> lock(isEventInQtEventLoopMutex_); + if (!isEventInQtEventLoop_) { + isEventInQtEventLoop_ = true; + QCoreApplication::postEvent(this, new Event()); + } + } + + virtual bool event(QEvent* qevent) { + Event* event = dynamic_cast<Event*>(qevent); + if (event) { + { + std::unique_lock<std::recursive_mutex> lock(isEventInQtEventLoopMutex_); + isEventInQtEventLoop_ = false; + } + handleNextEvent(); + //event->deleteLater(); FIXME: Leak? + return true; + } + + return false; + } + + private: + struct Event : public QEvent { + Event() : + QEvent(QEvent::User) { + } + }; + + bool isEventInQtEventLoop_; + std::recursive_mutex isEventInQtEventLoopMutex_; + }; } |