-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathautostartmanager_linux.cc
More file actions
70 lines (58 loc) · 1.87 KB
/
autostartmanager_linux.cc
File metadata and controls
70 lines (58 loc) · 1.87 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
#include "autostartmanager.hpp"
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
namespace Utils {
bool isAutoRunStart()
{
QString appName = qApp->applicationName();
QString desktopFilePath = QDir::homePath()
+ QString("/.config/autostart/%1.desktop").arg(appName);
return QFile::exists(desktopFilePath);
}
void setAutoRunStart(bool run)
{
QString appName = qApp->applicationName();
QDir autostartDir(QDir::homePath() + "/.config/autostart");
if (!autostartDir.exists() && !autostartDir.mkpath(".")) {
qWarning() << "Failed to create autostart directory";
return;
}
QString desktopFilePath = autostartDir.filePath(appName + ".desktop");
if (run) {
// 创建桌面文件
QString desktopContent = QString(R"(
[Desktop Entry]
Type=Application
Name=%1
Exec=%2
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
)")
.arg(appName, QCoreApplication::applicationFilePath());
QFile desktopFile(desktopFilePath);
if (!desktopFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning() << "Failed to create desktop file:" << desktopFile.errorString();
return;
}
if (desktopFile.write(desktopContent.toUtf8()) == -1) {
qWarning() << "Failed to write desktop file:" << desktopFile.errorString();
desktopFile.close();
return;
}
desktopFile.close();
qInfo() << "Auto-run enabled using desktop file";
} else {
// 删除桌面文件
if (QFile::exists(desktopFilePath)) {
if (!QFile::remove(desktopFilePath)) {
qWarning() << "Failed to remove desktop file";
} else {
qInfo() << "Auto-run disabled";
}
}
}
}
} // namespace Utils