aboutsummaryrefslogtreecommitdiff
path: root/singleapplication.cpp
diff options
context:
space:
mode:
authorItay Grudev <itay@mail.bg>2012-12-23 00:12:38 +0200
committerItay Grudev <itay@mail.bg>2012-12-23 00:12:38 +0200
commit230b4ebe94400ac7af47731e5c2b965b0c5fb2da (patch)
treead0b511cdff2023e0cabe0268986707efcc7b6ff /singleapplication.cpp
downloadsingleapplication-230b4ebe94400ac7af47731e5c2b965b0c5fb2da.tar.xz
Add base files
Diffstat (limited to 'singleapplication.cpp')
-rw-r--r--singleapplication.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/singleapplication.cpp b/singleapplication.cpp
new file mode 100644
index 0000000..7e63371
--- /dev/null
+++ b/singleapplication.cpp
@@ -0,0 +1,61 @@
+#include "singleapplication.h"
+
+/**
+ * @brief SingleApplication::SingleApplication
+ * 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)
+{
+ _shouldContinue = false; // By default this is not the main process
+
+ 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()));
+ }
+}
+
+/**
+ * @brief SingleApplication::~SingleApplication
+ * Destructor
+ */
+Application::~SingleApplication()
+{
+ if(_shouldContinue){
+ server->terminate();
+ }
+}
+
+/**
+ * @brief SingleApplication::shouldContinue
+ * Weather the program should be terminated
+ * @return bool
+ */
+bool SingleApplication::shouldContinue()
+{
+ return _shouldContinue;
+}
+
+/**
+ * @brief SingleApplication::slotShowUp
+ * Executed when the showUp command is sent to LocalServer
+ */
+void SingleApplication::slotShowUp()
+{
+ emit showUp();
+}