-
Notifications
You must be signed in to change notification settings - Fork 14
/
singleapplication.cpp
149 lines (119 loc) · 3.74 KB
/
singleapplication.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "singleapplication.h"
#include "commandlinemanager.h"
#include "view/mainwidget.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QProcess>
#include <QDir>
QString SingleApplication::UserID = "1000";
SingleApplication::SingleApplication(int &argc, char **argv, int)
: DApplication(argc, argv)
{
m_localServer = new QLocalServer;
initConnect();
}
SingleApplication::~SingleApplication()
{
if (m_localServer) {
m_localServer->removeServer(m_localServer->serverName());
m_localServer->close();
}
}
void SingleApplication::initConnect()
{
connect(m_localServer, &QLocalServer::newConnection, this, &SingleApplication::handleConnection);
}
void SingleApplication::newClientProcess(const QString &key, const QByteArray &message)
{
qDebug() << "The deepin-shortcut-viewer is running!";
QLocalSocket *localSocket = new QLocalSocket;
localSocket->connectToServer(userServerName(key));
if (localSocket->waitForConnected(1000)) {
if (localSocket->state() == QLocalSocket::ConnectedState) {
if (localSocket->isValid()) {
qDebug() << "start write";
localSocket->write(message);
localSocket->flush();
}
}
} else {
qDebug() << localSocket->errorString();
}
qDebug() << "The deepin-shortcut-viewer is running end!";
}
QString SingleApplication::userServerName(const QString &key)
{
QString userKey;
if (userID() == "0") {
userKey = QString("%1/%2").arg("/tmp", key);
} else {
userKey = QString("%1/%2/%3").arg("/var/run/user", userID(), key);
}
qDebug() << userKey;
return userKey;
}
QString SingleApplication::userID()
{
QProcess userID;
userID.start("id", QStringList() << "-u");
userID.waitForFinished();
QByteArray id = userID.readAll();
UserID = QString(id).trimmed();
return UserID;
}
void SingleApplication::processArgs(const QStringList &list)
{
//Command manager
CommandLineManager cmdManager;
cmdManager.process(list);
QString jsonData = cmdManager.jsonData();
QPoint pos = cmdManager.pos();
static MainWidget *w = Q_NULLPTR;
if (jsonData == "")
return;
if (w && cmdManager.enableBypassWindowManagerHint()) {
w->deleteLater();
w = Q_NULLPTR;
}
if (!w)
w = new MainWidget();
w->setJsonData(jsonData);
pos -= QPoint(w->width() / 2, w->height() / 2);
if (cmdManager.enableBypassWindowManagerHint())
w->setWindowFlags(w->windowFlags() | Qt::BypassWindowManagerHint);
w->show();
w->move(pos);
//w->activateWindow();
w->setFocus();
}
bool SingleApplication::setSingleInstance(const QString &key)
{
QString userKey = userServerName(key);
QLocalSocket *localSocket = new QLocalSocket;
localSocket->connectToServer(userKey);
// if connect success, another instance is running.
bool result = localSocket->waitForConnected(1000);
localSocket->deleteLater();
if (result)
return false;
m_localServer->removeServer(userKey);
bool f = m_localServer->listen(userKey);
return f;
}
void SingleApplication::handleConnection()
{
qDebug() << "new connection is coming";
QLocalSocket *nextPendingConnection = m_localServer->nextPendingConnection();
connect(nextPendingConnection, SIGNAL(readyRead()), this, SLOT(readData()));
}
void SingleApplication::readData()
{
const QByteArray &message = qobject_cast<QLocalSocket *>(sender())->readAll();
QStringList list;
for (const QByteArray &data : message.split('\0'))
list << QString::fromLocal8Bit(data);
processArgs(list);
}