Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F103514
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
7 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/singleapplication.cpp b/singleapplication.cpp
index 0bcbc12..62f3fbd 100644
--- a/singleapplication.cpp
+++ b/singleapplication.cpp
@@ -1,133 +1,176 @@
#include "singleapplication.h"
#include <QSharedMemory>
#include <QLocalSocket>
#include <QLocalServer>
-#include <QMutex>
-#include <cstdlib>
+#include <QDataStream>
#ifdef Q_OS_UNIX
+ #include <QMutex>
+ #include <cstdlib>
+
#include <signal.h>
#include <unistd.h>
#endif
-class SingleApplicationPrivate {
+class SingleApplicationPrivate
+{
public:
SingleApplicationPrivate(SingleApplication *q_ptr) : q_ptr(q_ptr) { }
void startServer(QString &serverName)
{
// Start a QLocalServer to listen for connections
server = new QLocalServer();
server->removeServer(serverName);
server->listen(serverName);
QObject::connect(server, SIGNAL(newConnection()), q_ptr, SLOT(slotConnectionEstablished()));
}
#ifdef Q_OS_UNIX
void crashHandler()
{
// This guarantees the program will work even with multiple
// instances of SingleApplication in different threads
// Which in my opinion is idiotic, but lets handle that too
{
sharedMemMutex.lock();
sharedMem.append(memory);
sharedMemMutex.unlock();
}
// Handle any further termination signals to ensure the
// QSharedMemory block is deleted even if the process crashes
signal(SIGSEGV, SingleApplicationPrivate::terminate);
signal(SIGABRT, SingleApplicationPrivate::terminate);
signal(SIGFPE, SingleApplicationPrivate::terminate);
signal(SIGILL, SingleApplicationPrivate::terminate);
signal(SIGINT, SingleApplicationPrivate::terminate);
signal(SIGTERM, SingleApplicationPrivate::terminate);
}
static void terminate(int signum)
{
- while( ! sharedMem.empty() ) {
+ while (!sharedMem.empty()) {
delete sharedMem.back();
sharedMem.pop_back();
}
::exit(128 + signum);
}
- static QList<QSharedMemory*> sharedMem;
+ static QList<QSharedMemory *> sharedMem;
static QMutex sharedMemMutex;
#endif
QSharedMemory *memory;
SingleApplication *q_ptr;
QLocalServer *server;
QLocalSocket *socket;
};
#ifdef Q_OS_UNIX
- QList<QSharedMemory*> SingleApplicationPrivate::sharedMem;
+ QList<QSharedMemory *> SingleApplicationPrivate::sharedMem;
QMutex SingleApplicationPrivate::sharedMemMutex;
#endif
/**
* @brief Constructor. Checks and fires up LocalServer or closes the program
* if another instance already exists
* @param argc
* @param argv
*/
SingleApplication::SingleApplication(int &argc, char *argv[])
: QAPPLICATION_CLASS(argc, argv), d_ptr(new SingleApplicationPrivate(this))
{
QString serverName = QAPPLICATION_CLASS::organizationName() + QAPPLICATION_CLASS::applicationName();
serverName.replace(QRegExp("[^\\w\\-. ]"), "");
// Garantee thread safe behaviour with a shared memory block
d_ptr->memory = new QSharedMemory(serverName);
// Create a shared memory block with a minimum size of 1 byte
- if( d_ptr->memory->create(1, QSharedMemory::ReadOnly) )
- {
+ if (d_ptr->memory->create(1, QSharedMemory::ReadOnly)) {
#ifdef Q_OS_UNIX
// Handle any further termination signals to ensure the
// QSharedMemory block is deleted even if the process crashes
d_ptr->crashHandler();
#endif
// Successful creation means that no main process exists
// So we start a Local Server to listen for connections
d_ptr->startServer(serverName);
} else {
- // Connect to the Local Server of the main process to notify it
- // that a new process had been started
+ // Connect to the Local Server of the main process
+ // and send the current arguments
d_ptr->socket = new QLocalSocket();
d_ptr->socket->connectToServer(serverName);
// Even though a shared memory block exists, the original application might have crashed
// So only after a successful connection is the second instance terminated
- if( d_ptr->socket->waitForConnected(100) )
- {
+ if (d_ptr->socket->waitForConnected(100)) {
+ // Before closing, we send the arguments that this application was called with
+ // to the old instance
+ QByteArray argumentData;
+
+ // Serialize the application arguments
+ QDataStream ds(&argumentData, QIODevice::WriteOnly);
+ ds << arguments();
+
+ d_ptr->socket->write(argumentData);
+ d_ptr->socket->waitForBytesWritten(200); // Make sure our data is written
+
::exit(EXIT_SUCCESS); // Terminate the program using STDLib's exit function
} else {
delete d_ptr->memory;
::exit(EXIT_SUCCESS);
}
}
+
+#ifdef Q_OS_WIN
+ // Creating a Windows Mutex, mostly so that other apps (like Inno Installer) can also know about the application's single instance
+ QString mutexName = "Global\\" + serverName.replace(" ", "");
+ HANDLE mutexResult = CreateMutexA(NULL, FALSE, mutexName.toStdString().c_str());
+
+ if (mutexResult == NULL) {
+ // Quit if we couldn't create the mutext.
+ delete d_ptr->memory;
+ ::exit(EXIT_SUCCESS);
+ }
+#endif
}
/**
* @brief Destructor
*/
SingleApplication::~SingleApplication()
{
delete d_ptr->memory;
d_ptr->server->close();
}
/**
- * @brief Executed when the showUp command is sent to LocalServer
+ * @brief Executed when the new instance connects with the LocalServer
*/
void SingleApplication::slotConnectionEstablished()
{
QLocalSocket *socket = d_ptr->server->nextPendingConnection();
- socket->close();
- delete socket;
+
+ // Emit showUp as soon as the connection is established
emit showUp();
+
+ // Connect the socket's readyRead signal to a lambda that is in charge of
+ // grabbing the arguments and emitting the signal that they arrived.
+ connect(socket, &QLocalSocket::readyRead, [&, socket] {
+ // Grab all the data from the socket
+ QByteArray argumentData = socket->readAll();
+
+ // Deserialize it
+ QStringList arguments;
+ QDataStream ds(argumentData);
+ ds >> arguments;
+
+ emit instanceArguments(arguments);
+
+ socket->close();
+ });
+
+ // Makes sure we delete the socket object even if we receive no data
+ connect(socket, &QLocalSocket::aboutToClose, socket, &QLocalSocket::deleteLater);
}
diff --git a/singleapplication.h b/singleapplication.h
index 1fe5c12..0f3c4e6 100644
--- a/singleapplication.h
+++ b/singleapplication.h
@@ -1,36 +1,37 @@
#ifndef SINGLE_APPLICATION_H
#define SINGLE_APPLICATION_H
// Change this to inherit from QGuiApplication or QCoreApplication
#ifndef QAPPLICATION_CLASS
- #define QAPPLICATION_CLASS QCoreApplication
+ #define QAPPLICATION_CLASS QCoreApplication
#endif
#define QUOTE(C) #C
#define INCLUDE_FILE(C) QUOTE(C)
#include INCLUDE_FILE(QAPPLICATION_CLASS)
class SingleApplicationPrivate;
/**
* @brief The SingleApplication class handles multipe instances of the same Application
* @see QApplication
*/
class SingleApplication : public QAPPLICATION_CLASS
{
- Q_OBJECT
+ Q_OBJECT
public:
- explicit SingleApplication(int&, char *[]);
- ~SingleApplication();
+ explicit SingleApplication(int &, char *[]);
+ ~SingleApplication();
signals:
- void showUp();
+ void showUp();
+ void instanceArguments(QStringList arguments);
private slots:
- void slotConnectionEstablished();
+ void slotConnectionEstablished();
private:
- SingleApplicationPrivate *d_ptr;
+ SingleApplicationPrivate *d_ptr;
};
#endif // SINGLE_APPLICATION_H
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Feb 7, 10:02 AM (17 h, 21 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55956
Default Alt Text
(7 KB)
Attached To
Mode
R64 cKaiser's QSingleApplicaiton
Attached
Detach File
Event Timeline
Log In to Comment