aboutsummaryrefslogtreecommitdiff
path: root/singleapplication.cpp
blob: 7e63371d774fb6695ab006f6a31c9d3e9ddc7fd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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();
}