🐞 fix(应用单例): 添加自定义单例,防止sdk单例错误

程序退出时退出逻辑修改
This commit is contained in:
huheng@kylinos.cn 2023-05-26 15:49:57 +08:00
parent 374b428f17
commit 64a03503d8
3 changed files with 44 additions and 26 deletions

View File

@ -148,17 +148,19 @@ void ConnectivityManager::killServerPortProcess()
void ConnectivityManager::killConnectivity()
{
outLog("start killConnecicvity");
if (m_timer == nullptr) {
m_timer = new QTimer(this);
}
m_timer->start(3000);
connect(m_timer, &QTimer::timeout, this, [=]() {
outLog("killConnecicvity timeout");
m_timer->stop();
m_timer->deleteLater();
m_timer = nullptr;
killProcess("ps -aux | grep kylin-connectivity");
QCoreApplication::quit();
});
outLog("Connecicvity quit!");
QCoreApplication::quit();
// outLog("start killConnecicvity");
// if (m_timer == nullptr) {
// m_timer = new QTimer(this);
// }
// m_timer->start(3000);
// connect(m_timer, &QTimer::timeout, this, [=]() {
// outLog("killConnecicvity timeout");
// m_timer->stop();
// m_timer->deleteLater();
// m_timer = nullptr;
// killProcess("ps -aux | grep kylin-connectivity");
// QCoreApplication::quit();
// });
}

View File

@ -4,6 +4,11 @@
#include <QMessageBox>
#include <QScreen>
#include <QDebug>
#include <QDir>
#include <QStandardPaths>
#include <QLocale>
#include <fcntl.h>
#include <syslog.h>
#include <singleapplication.h>
#include "log.hpp"
#include "windowmanage.hpp"
@ -43,11 +48,24 @@ int main(int argc, char *argv[])
app.setWindowIcon(QIcon::fromTheme("kylin-connectivity"));
if (app.isRunning()) {
qDebug() << "is running";
qDebug() << "kylin-connectivity is already running!";
app.sendMessage("running , 4000");
return 0;
}
// 添加单例判断
QStringList homePath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation);
int singleInstanceFd =
open(QString(homePath.at(0) + "/.config/kylin-connecitvity%1.lock").arg(getenv("DISPLAY")).toUtf8().data(),
O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (singleInstanceFd < 0) {
exit(1);
}
if (lockf(singleInstanceFd, F_TLOCK, 0)) {
syslog(LOG_ERR, "Can't lock single file, kylin-connectivity is already running!");
exit(0);
}
// 加载sdk控件翻译
QTranslator trans;
QString locale = QLocale::system().name();
@ -93,9 +111,8 @@ int main(int argc, char *argv[])
/* wayland 下最小化拉起 */
if (QGuiApplication::platformName().startsWith(QLatin1String("wayland"), Qt::CaseInsensitive)) {
QObject::connect(&app, &kdk::QtSingleApplication::messageReceived, [=]() {
kabase::WindowManage::activateWindow(MainWindow::getInstance()->getWinId());
});
QObject::connect(&app, &kdk::QtSingleApplication::messageReceived,
[=]() { kabase::WindowManage::activateWindow(MainWindow::getInstance()->getWinId()); });
}
MainWindow::getInstance()->show();

View File

@ -206,8 +206,7 @@ void MainWindow::initDbus()
// 右键发送
QDBusConnection sessionBus = QDBusConnection::sessionBus();
if (!sessionBus.registerService(KYLIN_CONNECTIVITY_SEND_SERVICE)) {
qInfo() << "init dbus fail...";
return;
qWarning() << "init send file dbus fail!";
}
sessionBus.registerObject(KYLIN_CONNECTIVITY_SEND_PATH, this, QDBusConnection::ExportAllSlots);
@ -305,13 +304,6 @@ void MainWindow::deleterService()
m_wifiManager->deleteLater();
m_wifiManager = nullptr;
}
if (m_wifiManagerThread != nullptr) {
qInfo() << "close wifi manage thread!";
m_wifiManagerThread->quit();
m_wifiManagerThread->wait();
m_wifiManagerThread->deleteLater();
m_wifiManagerThread = nullptr;
}
if (m_ftpServer != nullptr) {
qInfo() << "close ftpserver!";
m_ftpServer->stop();
@ -344,6 +336,13 @@ void MainWindow::deleterService()
m_dbusInterface->deleteLater();
m_dbusInterface = nullptr;
}
if (m_wifiManagerThread != nullptr) {
qInfo() << "close wifi manage thread!";
m_wifiManagerThread->quit();
m_wifiManagerThread->wait();
m_wifiManagerThread->deleteLater();
m_wifiManagerThread = nullptr;
}
}
bool MainWindow::mountFtp(QString url, QString userName, QString pwd, QString path)