summaryrefslogtreecommitdiffstats
blob: 389b0a797af623e5f935a7c80b9b7822f1eaff0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
 * Copyright (c) 2010-2015 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

#include <boost/thread.hpp>

#include <QCoreApplication>
#include <QEvent>
#include <QObject>

#include <Swiften/EventLoop/EventLoop.h>

namespace Swift {
	class QtEventLoop : public QObject, public EventLoop {
		public:
			QtEventLoop() : isEventInQtEventLoop_(false) {}
			virtual ~QtEventLoop() {
				QCoreApplication::removePostedEvents(this);
			}

		protected:
			virtual void eventPosted() {
				boost::recursive_mutex::scoped_lock lock(isEventInQtEventLoopMutex_);	
				if (!isEventInQtEventLoop_) {
					isEventInQtEventLoop_ = true;
					QCoreApplication::postEvent(this, new Event());
				}
			}

			virtual bool event(QEvent* qevent) {
				Event* event = dynamic_cast<Event*>(qevent);
				if (event) {
					{
						boost::recursive_mutex::scoped_lock lock(isEventInQtEventLoopMutex_);	
						isEventInQtEventLoop_ = false;
					}
					handleNextEvent();
					//event->deleteLater(); FIXME: Leak?
					return true;
				}

				return false;
			}
		
		private:
			struct Event : public QEvent {
					Event() :
							QEvent(QEvent::User) {
					}
			};

			bool isEventInQtEventLoop_;
			boost::recursive_mutex isEventInQtEventLoopMutex_;
	};
}