mirror of
https://github.com/Aurora1949/AHNU-Portal-Authenticator.git
synced 2026-03-06 01:43:08 +08:00
feat: implement auto-run functionality for Windows and Linux
This commit is contained in:
@@ -35,7 +35,7 @@ void MainWindow::init() {
|
|||||||
setWindowTitle("AHNU上号器");
|
setWindowTitle("AHNU上号器");
|
||||||
setWindowIcon(QIcon(":/images/logo.png"));
|
setWindowIcon(QIcon(":/images/logo.png"));
|
||||||
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint & ~Qt::WindowMinimizeButtonHint);
|
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint & ~Qt::WindowMinimizeButtonHint);
|
||||||
setFixedSize(400, 178);
|
setFixedSize(400, 200);
|
||||||
|
|
||||||
manager.setProxy(QNetworkProxy::NoProxy);
|
manager.setProxy(QNetworkProxy::NoProxy);
|
||||||
|
|
||||||
@@ -190,6 +190,8 @@ void MainWindow::testOnline() {
|
|||||||
|
|
||||||
void MainWindow::setAutoRun(bool isStart) {
|
void MainWindow::setAutoRun(bool isStart) {
|
||||||
QString appName = QApplication::applicationName(); // 获取应用名称
|
QString appName = QApplication::applicationName(); // 获取应用名称
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
QSettings settings(RegKey, QSettings::NativeFormat); // 创建QSettings对象
|
QSettings settings(RegKey, QSettings::NativeFormat); // 创建QSettings对象
|
||||||
|
|
||||||
if (isStart) {
|
if (isStart) {
|
||||||
@@ -199,12 +201,43 @@ void MainWindow::setAutoRun(bool isStart) {
|
|||||||
} else {
|
} else {
|
||||||
settings.remove(appName); // 从注册表中删除
|
settings.remove(appName); // 从注册表中删除
|
||||||
}
|
}
|
||||||
|
#elifdef Q_OS_LINUX
|
||||||
|
QString autostartDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart";
|
||||||
|
QDir dir(autostartDir);
|
||||||
|
if (!dir.exists())
|
||||||
|
dir.mkpath(autostartDir);
|
||||||
|
|
||||||
|
QString desktopFilePath = autostartDir + "/" + appName + ".desktop";
|
||||||
|
|
||||||
|
if (isStart) {
|
||||||
|
QFile desktopFile(desktopFilePath);
|
||||||
|
if (desktopFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||||
|
QTextStream out(&desktopFile);
|
||||||
|
out << "[Desktop Entry]\n";
|
||||||
|
out << "Type=Application\n";
|
||||||
|
out << "Exec=" << QApplication::applicationFilePath() << "\n";
|
||||||
|
out << "Icon=ahnu\n";
|
||||||
|
out << "Hidden=false\n";
|
||||||
|
out << "NoDisplay=false\n";
|
||||||
|
out << "X-GNOME-Autostart-enabled=true\n";
|
||||||
|
out << "Name=" << appName << "\n";
|
||||||
|
desktopFile.close();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
QFile::remove(desktopFilePath);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MainWindow::isAutoRunEnabled() {
|
bool MainWindow::isAutoRunEnabled() {
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
QSettings settings(RegKey, QSettings::NativeFormat);
|
QSettings settings(RegKey, QSettings::NativeFormat);
|
||||||
QString appPath = QDir::toNativeSeparators(QApplication::applicationFilePath());
|
QString appPath = QDir::toNativeSeparators(QApplication::applicationFilePath());
|
||||||
return settings.value(QApplication::applicationName()).toString().split(" ")[0] == appPath;
|
return settings.value(QApplication::applicationName()).toString().split(" ")[0] == appPath;
|
||||||
|
#elifdef Q_OS_LINUX
|
||||||
|
QString desktopFilePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart/" + QApplication::applicationName() + ".desktop";
|
||||||
|
return QFile::exists(desktopFilePath);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::on_selfStartup_checkStateChanged() {
|
void MainWindow::on_selfStartup_checkStateChanged() {
|
||||||
|
|||||||
Reference in New Issue
Block a user