mirror of
https://github.com/Aurora1949/AHNU-Portal-Authenticator.git
synced 2026-03-04 09:33:08 +08:00
first commit
This commit is contained in:
56
main.cpp
Normal file
56
main.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "mainwindow.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QLocalSocket>
|
||||
#include <QLocalServer>
|
||||
|
||||
bool isAlreadyRunning() {
|
||||
QLocalSocket socket;
|
||||
socket.connectToServer(APPNAME);
|
||||
|
||||
// 已有实例存在
|
||||
if (socket.waitForConnected(100)) {
|
||||
socket.write("raise");
|
||||
socket.flush();
|
||||
socket.waitForBytesWritten(100);
|
||||
socket.disconnectFromServer();
|
||||
return true;
|
||||
}
|
||||
|
||||
// 尝试清除之前未正常退出的 socket 文件
|
||||
QLocalServer::removeServer(APPNAME);
|
||||
return false;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
QApplication a(argc, argv);
|
||||
|
||||
if (isAlreadyRunning()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
MainWindow w;
|
||||
w.show();
|
||||
|
||||
// 建立本地 server
|
||||
QLocalServer server;
|
||||
if (!server.listen(APPNAME)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
QObject::connect(&server, &QLocalServer::newConnection, [&]() {
|
||||
QLocalSocket *client = server.nextPendingConnection();
|
||||
if (!client) return;
|
||||
|
||||
QObject::connect(client, &QLocalSocket::readyRead, [&]() {
|
||||
QByteArray msg = client->readAll();
|
||||
if (msg == "raise") {
|
||||
w.showFromTray();
|
||||
}
|
||||
});
|
||||
|
||||
QObject::connect(client, &QLocalSocket::disconnected, client, &QLocalSocket::deleteLater);
|
||||
});
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user