2012-12-23 06:12:38 +08:00
|
|
|
#include "singleapplication.h"
|
2015-02-27 03:00:11 +08:00
|
|
|
#include <cstdlib>
|
2012-12-23 06:12:38 +08:00
|
|
|
|
|
|
|
/**
|
2015-02-27 03:00:11 +08:00
|
|
|
* @brief Constructor. Checks and fires up LocalServer or closes the
|
|
|
|
program
|
|
|
|
* if another instance already exists
|
2012-12-23 06:12:38 +08:00
|
|
|
* @param argc
|
|
|
|
* @param argv
|
|
|
|
*/
|
2015-02-27 03:00:11 +08:00
|
|
|
SingleApplication::SingleApplication(int argc, char *argv[])
|
|
|
|
: QApplication(argc, argv)
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2015-02-27 03:00:11 +08:00
|
|
|
QString serverName = QApplication::organizationName() +
|
|
|
|
QApplication::applicationName();
|
|
|
|
serverName.replace(QRegExp("\\s"), "");
|
2012-12-23 06:12:38 +08:00
|
|
|
|
2015-02-27 03:00:11 +08:00
|
|
|
// Attempt to connect to the LocalServer
|
|
|
|
socket = new QLocalSocket();
|
|
|
|
socket->connectToServer(serverName);
|
|
|
|
if(socket->waitForConnected(1000)){
|
|
|
|
socket->close();
|
|
|
|
::exit(EXIT_SUCCESS); // Terminate the program using STDLib's
|
|
|
|
exit function
|
|
|
|
} else {
|
|
|
|
// If the connection is insuccessful, this is the main process
|
|
|
|
// So we create a Local Server
|
|
|
|
server = new QLocalServer();
|
|
|
|
server->removeServer(serverName);
|
|
|
|
server->listen(serverName);
|
|
|
|
QObject::connect(server, SIGNAL(newConnection()), this,
|
|
|
|
SLOT(slotConnectionEstablished()));
|
|
|
|
}
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-27 03:00:11 +08:00
|
|
|
* @brief Destructor
|
2012-12-23 06:12:38 +08:00
|
|
|
*/
|
2015-02-27 03:00:11 +08:00
|
|
|
SingleApplication::~SingleApplication()
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2015-02-27 03:00:11 +08:00
|
|
|
server->close();
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-27 03:00:11 +08:00
|
|
|
* @brief Executed when the showUp command is sent to LocalServer
|
2012-12-23 06:12:38 +08:00
|
|
|
*/
|
2015-02-27 03:00:11 +08:00
|
|
|
void SingleApplication::slotConnectionEstablished()
|
2012-12-23 06:12:38 +08:00
|
|
|
{
|
2015-02-27 03:00:11 +08:00
|
|
|
server->nextPendingConnection();
|
|
|
|
emit showUp();
|
2012-12-23 06:12:38 +08:00
|
|
|
}
|
|
|
|
|