-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "singleapplication.h" | ||
|
||
#include <QLocalSocket> | ||
|
||
|
||
SingleApplication::SingleApplication(int &argc, char *argv[], const QString uniqueKey) : QApplication(argc, argv), _uniqueKey(uniqueKey) | ||
{ | ||
sharedMemory.setKey(_uniqueKey); | ||
if (sharedMemory.attach()) | ||
_isRunning = true; | ||
else | ||
{ | ||
_isRunning = false; | ||
// create shared memory. | ||
if (!sharedMemory.create(1)) | ||
{ | ||
qDebug("Unable to create single instance."); | ||
return; | ||
} | ||
// create local server and listen to incomming messages from other instances. | ||
localServer = new QLocalServer(this); | ||
connect(localServer, SIGNAL(newConnection()), this, SLOT(receiveMessage())); | ||
localServer->listen(_uniqueKey); | ||
} | ||
} | ||
|
||
// public slots. | ||
|
||
void SingleApplication::receiveMessage() | ||
{ | ||
QLocalSocket *localSocket = localServer->nextPendingConnection(); | ||
if (!localSocket->waitForReadyRead(timeout)) | ||
{ | ||
qDebug(localSocket->errorString().toLatin1()); | ||
return; | ||
} | ||
QByteArray byteArray = localSocket->readAll(); | ||
QString message = QString::fromUtf8(byteArray.constData()); | ||
emit messageAvailable(message); | ||
localSocket->disconnectFromServer(); | ||
} | ||
|
||
// public functions. | ||
|
||
bool SingleApplication::isRunning() | ||
{ | ||
return _isRunning; | ||
} | ||
|
||
bool SingleApplication::sendMessage(const QString &message) | ||
{ | ||
if (!_isRunning) | ||
return false; | ||
QLocalSocket localSocket(this); | ||
localSocket.connectToServer(_uniqueKey, QIODevice::WriteOnly); | ||
if (!localSocket.waitForConnected(timeout)) | ||
{ | ||
qDebug(localSocket.errorString().toLatin1()); | ||
return false; | ||
} | ||
localSocket.write(message.toUtf8()); | ||
if (!localSocket.waitForBytesWritten(timeout)) | ||
{ | ||
qDebug(localSocket.errorString().toLatin1()); | ||
return false; | ||
} | ||
localSocket.disconnectFromServer(); | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// "single_application.h" | ||
|
||
#ifndef SINGLE_APPLICATION_H | ||
#define SINGLE_APPLICATION_H | ||
|
||
#include <QApplication> | ||
#include <QSharedMemory> | ||
#include <QLocalServer> | ||
|
||
class SingleApplication : public QApplication | ||
{ | ||
Q_OBJECT | ||
public: | ||
SingleApplication(int &argc, char *argv[], const QString uniqueKey); | ||
|
||
bool isRunning(); | ||
bool sendMessage(const QString &message); | ||
|
||
public slots: | ||
void receiveMessage(); | ||
|
||
signals: | ||
void messageAvailable(QString message); | ||
|
||
private: | ||
bool _isRunning; | ||
QString _uniqueKey; | ||
QSharedMemory sharedMemory; | ||
QLocalServer *localServer; | ||
|
||
static const int timeout = 1000; | ||
}; | ||
#endif // SINGLE_APPLICATION_H |