Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add single application solution

  • Loading branch information...
commit cd583ab6d3d47caebf27a512885cfd45caafddeb 1 parent 2e48e09
@iptton iptton authored
View
6 Rythem.pro
@@ -37,7 +37,8 @@ SOURCES += main.cpp\
quazip/JlCompress.cpp \
rytablesortfilterproxymodel.cpp \
widget/rywebview.cpp \
- widget/rytabwidget.cpp
+ widget/rytabwidget.cpp \
+ singleapplication.cpp
HEADERS += mainwindow.h \
@@ -70,7 +71,8 @@ HEADERS += mainwindow.h \
rymimedata.h \
rytablesortfilterproxymodel.h \
widget/rywebview.h \
- widget/rytabwidget.h
+ widget/rytabwidget.h \
+ singleapplication.h
win32:HEADERS += zlib/zutil.h \
zlib/zlib.h \
zlib/zconf.h \
View
8 main.cpp
@@ -16,6 +16,7 @@
#include <QThread>
#include <QTranslator>
+#include "singleapplication.h"
using namespace rule;
@@ -51,7 +52,12 @@ void myMessageHandler(QtMsgType type, const char *msg)
int main(int argc, char *argv[])
{
- QApplication a(argc, argv);
+ SingleApplication a(argc, argv, "a");
+ if(a.isRunning()){
+ a.sendMessage("from other instance");
+ return 0;
+ }
+
a.setQuitOnLastWindowClosed(false);
appPath = qApp->applicationDirPath();
View
69 singleapplication.cpp
@@ -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;
+}
View
33 singleapplication.h
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.