feat: add autostart support for Mac App Store builds on macOS

Fixes: QA-8
This commit is contained in:
Yaroslav Yashin 2025-05-12 15:18:52 +03:00
parent 94ee3a89a4
commit 3bd25656fb
2 changed files with 83 additions and 4 deletions

View file

@ -58,7 +58,80 @@ QString Autostart::appPath() {
return QCoreApplication::applicationFilePath() + " --autostart"; 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() { bool Autostart::isAutostart() {
QProcess process; QProcess process;
@ -97,6 +170,8 @@ QString Autostart::appPath() {
return absolutePath; return absolutePath;
} }
#endif // MACOS_NE
#elif defined (Q_OS_LINUX) #elif defined (Q_OS_LINUX)
bool Autostart::isAutostart() { bool Autostart::isAutostart() {
QFileInfo check_file(QDir::homePath() + "/.config/autostart/" + appName() +".desktop"); QFileInfo check_file(QDir::homePath() + "/.config/autostart/" + appName() +".desktop");

View file

@ -30,10 +30,14 @@ class Autostart
public: public:
static bool isAutostart(); static bool isAutostart();
static void setAutostart(bool autostart); static void setAutostart(bool autostart);
protected:
static QString appPath(); static QString appPath();
static QString appName(); 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 #endif // AUTOSTART_H