forked from benlau/quickios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqisystemmessenger.cpp
45 lines (36 loc) · 1.13 KB
/
qisystemmessenger.cpp
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
#include <QCoreApplication>
#include <QPointer>
#include <QtCore>
#include "qisystemmessenger.h"
typedef bool (*handler)(QVariantMap& data);
static QMap<QString,handler> handlers;
static QPointer<QISystemMessenger> m_instance;
QISystemMessenger *QISystemMessenger::instance()
{
if (!m_instance) {
QCoreApplication* app = QCoreApplication::instance();
m_instance = new QISystemMessenger(app);
}
return m_instance;
}
QISystemMessenger::QISystemMessenger(QObject *parent) : QObject(parent) {
}
bool QISystemMessenger::sendMessage(QString name , QVariantMap data) {
QMetaObject::invokeMethod(this,"received",Qt::QueuedConnection,
Q_ARG(QString , name),
Q_ARG(QVariantMap,data));
bool res = false;
if (handlers.contains(name)) {
res = handlers[name](data);
}
return res;
}
bool QISystemMessenger::registerMessageHandler(QString name, bool (*func)(QVariantMap&))
{
if (handlers.contains(name)) {
qWarning() << QString("%s is already registered").arg(name);
return false;
}
handlers[name] = func;
return true;
}