Skip to content

Commit

Permalink
add single application solution
Browse files Browse the repository at this point in the history
  • Loading branch information
iptton committed May 29, 2012
1 parent 2e48e09 commit cd583ab
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 3 deletions.
6 changes: 4 additions & 2 deletions Rythem.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
8 changes: 7 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QThread>

#include <QTranslator>
#include "singleapplication.h"

using namespace rule;

Expand Down Expand Up @@ -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();

Expand Down
69 changes: 69 additions & 0 deletions singleapplication.cpp
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;
}
33 changes: 33 additions & 0 deletions singleapplication.h
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

0 comments on commit cd583ab

Please sign in to comment.