-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsingleapplication.cpp
More file actions
167 lines (137 loc) · 4.44 KB
/
singleapplication.cpp
File metadata and controls
167 lines (137 loc) · 4.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// 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)
{
qInfo() << "Attempting to connect to existing instance with key:" << key;
QLocalSocket *localSocket = new QLocalSocket;
localSocket->connectToServer(userServerName(key));
if (localSocket->waitForConnected(1000)) {
if (localSocket->state() == QLocalSocket::ConnectedState) {
if (localSocket->isValid()) {
qDebug() << "Connected to existing instance, sending message";
localSocket->write(message);
localSocket->flush();
localSocket->waitForBytesWritten(1000);
}
}
} else {
qWarning() << "Failed to connect to existing instance:" << localSocket->errorString();
}
localSocket->deleteLater();
}
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() << "Generated server name:" << userKey << "for user:" << userID();
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.isEmpty()) {
qDebug() << "No JSON data to process";
return;
}
if (w && cmdManager.enableBypassWindowManagerHint()) {
w->deleteLater();
w = Q_NULLPTR;
}
if (!w) {
qDebug() << "Creating new main window";
w = new MainWidget();
}
// 先隐藏窗口,设置数据后触发布局计算
w->hide();
w->setJsonData(jsonData);
if (cmdManager.enableBypassWindowManagerHint()) {
qDebug() << "Setting bypass window manager hint";
w->setWindowFlags(w->windowFlags() | Qt::BypassWindowManagerHint);
}
// 强制处理事件并调整大小
QApplication::processEvents();
w->adjustSize();
// 计算居中位置并移动到正确位置后显示
pos -= QPoint(w->width() / 2, w->height() / 2);
w->move(pos);
w->show();
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) {
qInfo() << "Another instance is already running";
return false;
}
m_localServer->removeServer(userKey);
bool f = m_localServer->listen(userKey);
return f;
}
void SingleApplication::handleConnection()
{
qDebug() << "New connection received";
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')) {
QString str = QString::fromLocal8Bit(data);
if (!str.isEmpty()) {
list << str;
}
}
processArgs(list);
}