feat: add autostart support for Mac App Store builds on macOS
Fixes: QA-8
This commit is contained in:
parent
94ee3a89a4
commit
3bd25656fb
2 changed files with 83 additions and 4 deletions
|
|
@ -58,7 +58,80 @@ QString Autostart::appPath() {
|
|||
return QCoreApplication::applicationFilePath() + " --autostart";
|
||||
}
|
||||
|
||||
#elif defined Q_OS_MACX
|
||||
#elif defined(Q_OS_MACOS)
|
||||
|
||||
#if defined(MACOS_NE)
|
||||
#include <QFile>
|
||||
|
||||
static QString bundleIdentifier()
|
||||
{
|
||||
return QCoreApplication::applicationName();
|
||||
}
|
||||
|
||||
QString Autostart::launchAgentPlistPath()
|
||||
{
|
||||
// ~/Library/LaunchAgents
|
||||
const QString agentsDir = QDir::homePath() + "/Library/LaunchAgents";
|
||||
return agentsDir + "/" + bundleIdentifier() + ".plist";
|
||||
}
|
||||
|
||||
static QByteArray buildPlist()
|
||||
{
|
||||
QString plist;
|
||||
QTextStream ts(&plist);
|
||||
ts << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
||||
ts << "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n";
|
||||
ts << "<plist version=\"1.0\">\n";
|
||||
ts << "<dict>\n";
|
||||
ts << " <key>Label</key>\n";
|
||||
ts << " <string>" << bundleIdentifier() << "</string>\n";
|
||||
ts << " <key>ProgramArguments</key>\n";
|
||||
ts << " <array>\n";
|
||||
ts << " <string>" << Autostart::appPath() << "</string>\n";
|
||||
ts << " </array>\n";
|
||||
ts << " <key>RunAtLoad</key>\n";
|
||||
ts << " <true/>\n";
|
||||
ts << "</dict>\n";
|
||||
ts << "</plist>\n";
|
||||
|
||||
return plist.toUtf8();
|
||||
}
|
||||
|
||||
bool Autostart::isAutostart()
|
||||
{
|
||||
return QFile::exists(launchAgentPlistPath());
|
||||
}
|
||||
|
||||
void Autostart::setAutostart(bool autostart)
|
||||
{
|
||||
const QString plistPath = launchAgentPlistPath();
|
||||
|
||||
if (!autostart)
|
||||
{
|
||||
QFile::remove(plistPath);
|
||||
return;
|
||||
}
|
||||
|
||||
QDir().mkpath(QFileInfo(plistPath).absolutePath());
|
||||
|
||||
QFile f(plistPath);
|
||||
if (f.open(QIODevice::WriteOnly | QIODevice::Truncate))
|
||||
{
|
||||
f.write(buildPlist());
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
QString Autostart::appPath()
|
||||
{
|
||||
// launchd expects the path to the .app bundle, not the executable.
|
||||
QDir appDir = QDir(QCoreApplication::applicationDirPath());
|
||||
appDir.cdUp();
|
||||
appDir.cdUp();
|
||||
return appDir.absolutePath();
|
||||
}
|
||||
|
||||
#else // !MACOS_NE
|
||||
|
||||
bool Autostart::isAutostart() {
|
||||
QProcess process;
|
||||
|
|
@ -97,6 +170,8 @@ QString Autostart::appPath() {
|
|||
return absolutePath;
|
||||
}
|
||||
|
||||
#endif // MACOS_NE
|
||||
|
||||
#elif defined (Q_OS_LINUX)
|
||||
bool Autostart::isAutostart() {
|
||||
QFileInfo check_file(QDir::homePath() + "/.config/autostart/" + appName() +".desktop");
|
||||
|
|
|
|||
|
|
@ -30,10 +30,14 @@ class Autostart
|
|||
public:
|
||||
static bool isAutostart();
|
||||
static void setAutostart(bool autostart);
|
||||
|
||||
protected:
|
||||
static QString appPath();
|
||||
static QString appName();
|
||||
|
||||
#if defined(Q_OS_MACOS) && defined(MACOS_NE)
|
||||
// Full path to the per-user LaunchAgent plist for sandboxed App Store builds
|
||||
// (~ /Library/LaunchAgents/<bundleIdentifier>.plist)
|
||||
static QString launchAgentPlistPath();
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // AUTOSTART_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue