forked from librepods-org/librepods
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautostartmanager.hpp
More file actions
98 lines (83 loc) · 2.72 KB
/
Copy pathautostartmanager.hpp
File metadata and controls
98 lines (83 loc) · 2.72 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
#ifndef AUTOSTARTMANAGER_HPP
#define AUTOSTARTMANAGER_HPP
#include <QObject>
#include <QSettings>
#include <QStandardPaths>
#include <QFile>
#include <QDir>
#include <QCoreApplication>
class AutoStartManager : public QObject
{
Q_OBJECT
Q_PROPERTY(bool autoStartEnabled READ autoStartEnabled WRITE setAutoStartEnabled NOTIFY autoStartEnabledChanged)
public:
explicit AutoStartManager(QObject *parent = nullptr) : QObject(parent)
{
QString autostartDir = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/autostart";
QDir().mkpath(autostartDir);
m_autostartFilePath = autostartDir + "/" + QCoreApplication::applicationName() + ".desktop";
}
bool autoStartEnabled() const
{
return QFile::exists(m_autostartFilePath);
}
void setAutoStartEnabled(bool enabled)
{
if (autoStartEnabled() == enabled)
{
return;
}
if (enabled)
{
createAutoStartEntry();
}
else
{
removeAutoStartEntry();
}
emit autoStartEnabledChanged(enabled);
}
private:
void createAutoStartEntry()
{
QFile desktopFile(m_autostartFilePath);
if (!desktopFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
qWarning() << "Failed to create autostart file:" << desktopFile.errorString();
return;
}
QString appPath = QCoreApplication::applicationFilePath();
// Handle cases where the path might contain spaces
if (appPath.contains(' '))
{
appPath = "\"" + appPath + "\"";
}
QString content = QStringLiteral(
"[Desktop Entry]\n"
"Type=Application\n"
"Name=%1\n"
"Exec=%2 --hide\n"
"Icon=%3\n"
"Comment=%4\n"
"X-GNOME-Autostart-enabled=true\n"
"Terminal=false\n")
.arg(
QCoreApplication::applicationName(),
appPath,
QCoreApplication::applicationName().toLower(),
QCoreApplication::applicationName() + " autostart");
desktopFile.write(content.toUtf8());
desktopFile.close();
}
void removeAutoStartEntry()
{
if (QFile::exists(m_autostartFilePath))
{
QFile::remove(m_autostartFilePath);
}
}
QString m_autostartFilePath;
signals:
void autoStartEnabledChanged(bool enabled);
};
#endif // AUTOSTARTMANAGER_HPP