summaryrefslogtreecommitdiffstats
blob: 275ba4b64856b1bd61e9b180c3634638a580e881 (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
/*
 * Copyright (c) 2010 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#pragma once

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

#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_;
			};
	};
}