forked from Skycoder42/QAutoStart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqautostart_mac.cpp
80 lines (69 loc) · 2.79 KB
/
qautostart_mac.cpp
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
#include "qautostart_p.h"
#include <QXmlStreamWriter>
#include <QFile>
#include <QDir>
bool QAutoStartPrivate::isAutoStartEnabled()
{
return QFile::exists(launchAgentFilePath());
}
bool QAutoStartPrivate::setAutoStartEnabled(bool autoStartEnabled)
{
const auto autoStartPath = launchAgentFilePath();
if(autoStartEnabled) {
if(QFile::exists(autoStartPath))
return true;
else {
QFile launchFile{autoStartPath};
if(!launchFile.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
QXmlStreamWriter writer{&launchFile};
writer.setAutoFormatting(true);
writer.setAutoFormattingIndent(-1);
writer.setCodec("UTF-8");
writer.writeStartDocument(QStringLiteral("1.0"));
writer.writeDTD(QStringLiteral("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"));
writer.writeStartElement(QStringLiteral("plist"));
writer.writeAttribute(QStringLiteral("version"), QStringLiteral("1.0"));
writer.writeStartElement(QStringLiteral("dict"));
writer.writeTextElement(QStringLiteral("key"), QStringLiteral("Label"));
writer.writeTextElement(QStringLiteral("string"), startId);
writer.writeTextElement(QStringLiteral("key"), QStringLiteral("ProgramArguments"));
writer.writeStartElement(QStringLiteral("array"));
writer.writeTextElement(QStringLiteral("string"), QDir::toNativeSeparators(program));
for(const auto &arg : qAsConst(arguments))
writer.writeTextElement(QStringLiteral("string"), arg);
writer.writeEndElement();
writer.writeTextElement(QStringLiteral("key"), QStringLiteral("RunAtLoad"));
writer.writeEmptyElement(QStringLiteral("true"));
writer.writeTextElement(QStringLiteral("key"), QStringLiteral("ProcessType"));
if(!extraProperties.contains(QAutoStart::Interactive))
writer.writeTextElement(QStringLiteral("string"), QStringLiteral("Standard"));
else if(extraProperties.value(QAutoStart::Interactive).toBool())
writer.writeTextElement(QStringLiteral("string"), QStringLiteral("Interactive"));
else
writer.writeTextElement(QStringLiteral("string"), QStringLiteral("Background"));
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndDocument();
launchFile.close();
}
} else {
if(!QFile::exists(autoStartPath) || QFile::remove(autoStartPath))
return true;
}
return false;
}
QString QAutoStartPrivate::launchAgentFilePath() const
{
QDir launchDir;
if(extraProperties.contains(QAutoStart::CustomLocation)) {
launchDir = extraProperties.value(QAutoStart::CustomLocation).toString();
launchDir.mkpath(QStringLiteral("."));
} else {
launchDir = QDir::home();
const auto subDir = QStringLiteral("Library/LaunchAgents");
launchDir.mkpath(subDir);
launchDir.cd(subDir);
}
return launchDir.absoluteFilePath(startId + QStringLiteral(".plist"));
}