-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_application.cpp
More file actions
93 lines (82 loc) · 3.03 KB
/
single_application.cpp
File metadata and controls
93 lines (82 loc) · 3.03 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "single_application.h"
#include <typeinfo>
#include <qdebug.h>
SingleApplication::SingleApplication(int & argc, char * argv[], const QString uniqueKey) : QApplication(argc, argv) {
// fix for linux crashed app memory clear
{
QSharedMemory shmem(uniqueKey);
shmem.attach();
}
sharedMemory.setKey(uniqueKey);
if (sharedMemory.attach())
_isRunning = true;
else {
_isRunning = false;
// attach data to shared memory.
QByteArray byteArray("0"); // default value to note that no message is available.
if (!sharedMemory.create(byteArray.size())) {
qDebug("Unable to create single instance.");
return;
}
sharedMemory.lock();
char * to = (char *)sharedMemory.data();
const char * from = byteArray.data();
memcpy(to, from, qMin(sharedMemory.size(), byteArray.size()));
sharedMemory.unlock();
// start checking for messages of other instances.
QTimer * timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(checkForMessage()));
timer -> start(1000);
}
}
// public slots.
void SingleApplication::checkForMessage() {
sharedMemory.lock();
QByteArray byteArray = QByteArray((char *)sharedMemory.constData(), sharedMemory.size());
sharedMemory.unlock();
if (byteArray.left(1) == "0")
return;
byteArray.remove(0, 1);
QString message = QString::fromUtf8(byteArray.constData());
emit messageAvailable(message);
// remove message from shared memory.
byteArray = "0";
sharedMemory.lock();
char *to = (char*)sharedMemory.data();
const char *from = byteArray.data();
memcpy(to, from, qMin(sharedMemory.size(), byteArray.size()));
sharedMemory.unlock();
}
// public functions.
bool SingleApplication::isRunning() {
return _isRunning;
}
bool SingleApplication::sendMessage(const QString & message) {
if (!_isRunning)
return false;
QByteArray byteArray("1");
byteArray.append(message.toUtf8());
byteArray.append('\0'); // < should be as char here, not a string!
sharedMemory.lock();
char *to = (char*)sharedMemory.data();
const char *from = byteArray.data();
memcpy(to, from, qMin(sharedMemory.size(), byteArray.size()));
sharedMemory.unlock();
return true;
}
//bool SingleApplication::notify(QObject * receiver, QEvent * event) {
// try {
// return QApplication::notify(receiver, event);
// } catch (std::exception & e) {
// qFatal("Error %s sending event %s to object %s (%s)",
// e.what(), typeid(*event).name(), qPrintable(receiver -> objectName()),
// typeid(*receiver).name());
// } catch (...) {
// qFatal("Error <unknown> sending event %s to object %s (%s)",
// typeid(*event).name(), qPrintable(receiver -> objectName()),
// typeid(*receiver).name());
// }
// // qFatal aborts, so this isn't really necessary
// // but you might continue if you use a different logging lib
// return false;
//}