Repository navigation
Expand file tree
/
Copy pathsingleapplication.cpp
More file actions
65 lines (56 loc) · 1.44 KB
/
Copy pathsingleapplication.cpp
File metadata and controls
65 lines (56 loc) · 1.44 KB
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
62
63
64
65
#include "singleapplication.h"
#include <QLocalSocket>
#include <QTimer>
SingleApplication::SingleApplication(int &argc, char **argv)
:QApplication(argc, argv)
{
if (argc > 1)
{
filePath = QString::fromUtf8(argv[1]);
QTimer::singleShot(10, this, [=]() {
emit importFile(filePath);
});
}
initConnect();
}
bool SingleApplication::isRunning()
{
return m_IsRunning;
}
void SingleApplication::initConnect()
{
m_IsRunning = false;
QLocalSocket socket;
socket.connectToServer(serverName);
bool ret = socket.waitForConnected(500);
if (ret)
{
m_IsRunning = true;
socket.write(filePath.toUtf8());
socket.waitForBytesWritten();
socket.disconnectFromServer();
return;
}
newLocalServer();
}
void SingleApplication::newLocalServer()
{
m_server = new QLocalServer(this);
connect(m_server, &QLocalServer::newConnection, this, &SingleApplication::newLocalConnect);
if (!m_server->listen(serverName)) {
if (m_server->serverError() == QAbstractSocket::AddressInUseError) {
QLocalServer::removeServer(serverName);
m_server->listen(serverName);
}
}
}
void SingleApplication::newLocalConnect()
{
QLocalSocket *socket = m_server->nextPendingConnection();
if (socket)
{
socket->waitForReadyRead(1000);
emit importFile(QString(socket->readAll()));
delete socket;
}
}