aboutsummaryrefslogtreecommitdiff
path: root/singleapplication.cpp
diff options
context:
space:
mode:
authorItay Grudev <itay@grudev.com>2015-02-26 19:00:11 +0000
committerItay Grudev <itay@grudev.com>2015-02-26 19:00:11 +0000
commit5c89ab17cf5c516cb2232dffc0b5ddae6c7d2be0 (patch)
treeb6e72002f23fce5d976a85e6beefe89cbcb94999 /singleapplication.cpp
parentUpdate README.md (diff)
downloadsingleapplication-5c89ab17cf5c516cb2232dffc0b5ddae6c7d2be0.tar.xz
Completly rewritten and simplified source
Diffstat (limited to 'singleapplication.cpp')
-rw-r--r--singleapplication.cpp73
1 files changed, 32 insertions, 41 deletions
diff --git a/singleapplication.cpp b/singleapplication.cpp
index 7e63371..5a4d37a 100644
--- a/singleapplication.cpp
+++ b/singleapplication.cpp
@@ -1,61 +1,52 @@
#include "singleapplication.h"
+#include <cstdlib>
/**
- * @brief SingleApplication::SingleApplication
- * Constructor. Checks and fires up LocalServer or closes the program
- * if another instance already exists
+ * @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[]) :
- QGuiApplication(argc, argv)
+SingleApplication::SingleApplication(int argc, char *argv[])
+ : QApplication(argc, argv)
{
- _shouldContinue = false; // By default this is not the main process
+ QString serverName = QApplication::organizationName() +
+QApplication::applicationName();
+ serverName.replace(QRegExp("\\s"), "");
- socket = new QLocalSocket();
-
- // Attempt to connect to the LocalServer
- socket->connectToServer(LOCAL_SERVER_NAME);
- if(socket->waitForConnected(100)){
- socket->write("CMD:showUp");
- socket->flush();
- QThread::msleep(100);
- socket->close();
- } else {
- // The attempt was insuccessful, so we continue the program
- _shouldContinue = true;
- server = new LocalServer();
- server->start();
- QObject::connect(server, SIGNAL(showUp()), this, SLOT(slotShowUp()));
- }
+ // 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()));
+ }
}
/**
- * @brief SingleApplication::~SingleApplication
- * Destructor
+ * @brief Destructor
*/
-Application::~SingleApplication()
+SingleApplication::~SingleApplication()
{
- if(_shouldContinue){
- server->terminate();
- }
+ server->close();
}
/**
- * @brief SingleApplication::shouldContinue
- * Weather the program should be terminated
- * @return bool
+ * @brief Executed when the showUp command is sent to LocalServer
*/
-bool SingleApplication::shouldContinue()
+void SingleApplication::slotConnectionEstablished()
{
- return _shouldContinue;
+ server->nextPendingConnection();
+ emit showUp();
}
-/**
- * @brief SingleApplication::slotShowUp
- * Executed when the showUp command is sent to LocalServer
- */
-void SingleApplication::slotShowUp()
-{
- emit showUp();
-}