diff options
author | Anne Jan Brouwer <brouwer@annejan.com> | 2014-08-17 05:23:37 +0200 |
---|---|---|
committer | Anne Jan Brouwer <ajbrouwer@totalactivemedia.nl> | 2014-08-17 05:23:37 +0200 |
commit | 9c70b87e1e9736e2cf74162532665d9d7d0c5149 (patch) | |
tree | 7686531a2596bb67321e6bad99c4ff2f442a93ea | |
parent | a0b9842a969ed5454d3c502fa03b9a9c036bb862 (diff) |
Highlander code
-rw-r--r-- | main.cpp | 6 | ||||
-rw-r--r-- | qtpass.pro | 8 | ||||
-rw-r--r-- | singleapplication.cpp | 68 | ||||
-rw-r--r-- | singleapplication.h | 31 |
4 files changed, 109 insertions, 4 deletions
@@ -1,10 +1,14 @@ #include "mainwindow.h" #include <QApplication> #include <QTranslator> +#include "singleapplication.h" int main(int argc, char *argv[]) { - QApplication app(argc, argv); + SingleApplication app(argc, argv, "QtPass"); + if (app.isRunning()) { + return 0; + } QCoreApplication::setOrganizationName("IJHack"); QCoreApplication::setOrganizationDomain("ijhack.org"); @@ -4,7 +4,7 @@ # #------------------------------------------------- -QT += core gui +QT += core gui network greaterThan(QT_MAJOR_VERSION, 4): QT += widgets @@ -19,11 +19,13 @@ TEMPLATE = app SOURCES += main.cpp\ mainwindow.cpp \ dialog.cpp \ - storemodel.cpp + storemodel.cpp \ + singleapplication.cpp HEADERS += mainwindow.h \ dialog.h \ - storemodel.h + storemodel.h \ + singleapplication.h FORMS += mainwindow.ui \ dialog.ui diff --git a/singleapplication.cpp b/singleapplication.cpp new file mode 100644 index 00000000..fc668f97 --- /dev/null +++ b/singleapplication.cpp @@ -0,0 +1,68 @@ +#include <QLocalSocket> + +#include "singleapplication.h" + +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; +} diff --git a/singleapplication.h b/singleapplication.h new file mode 100644 index 00000000..85c5f609 --- /dev/null +++ b/singleapplication.h @@ -0,0 +1,31 @@ +#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 |