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

This reverts commit 3bd25656fb.
This commit is contained in:
Yaroslav Yashin 2025-05-13 23:28:15 +03:00
parent aa023c2919
commit dd0e2a6cb8
2 changed files with 10 additions and 99 deletions

View file

@ -30,8 +30,6 @@
#include <QString> #include <QString>
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QDebug>
#if defined (Q_OS_WIN) #if defined (Q_OS_WIN)
#define REG_KEY "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run" #define REG_KEY "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
@ -60,86 +58,7 @@ QString Autostart::appPath() {
return QCoreApplication::applicationFilePath() + " --autostart"; return QCoreApplication::applicationFilePath() + " --autostart";
} }
#elif defined(Q_OS_MACX) || defined(Q_OS_MACOS) #elif defined Q_OS_MACX
#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()
{
const bool exists = QFile::exists(launchAgentPlistPath());
qDebug() << "isAutostart?" << exists << "plistPath=" << launchAgentPlistPath();
return exists;
}
void Autostart::setAutostart(bool autostart)
{
const QString plistPath = launchAgentPlistPath();
if (!autostart)
{
qDebug() << "Removing LaunchAgent plist" << plistPath;
QFile::remove(plistPath);
return;
}
qDebug() << "Creating LaunchAgent plist" << plistPath;
QDir().mkpath(QFileInfo(plistPath).absolutePath());
QFile f(plistPath);
if (f.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QByteArray data = buildPlist();
f.write(data);
qDebug() << "Written plist bytes=" << data.size();
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;
@ -148,23 +67,21 @@ bool Autostart::isAutostart() {
}); });
process.waitForFinished(3000); process.waitForFinished(3000);
const auto output = QString::fromLocal8Bit(process.readAllStandardOutput()); const auto output = QString::fromLocal8Bit(process.readAllStandardOutput());
const bool contains = output.contains(appPath()); return output.contains(appPath());
qDebug() << "AppleScript isAutostart?" << contains;
return contains;
} }
void Autostart::setAutostart(bool autostart) { void Autostart::setAutostart(bool autostart) {
// Remove any existing login entry for this app first, in case there was one // Remove any existing login entry for this app first, in case there was one
// from a previous installation, that may be under a different launch path. // from a previous installation, that may be under a different launch path.
qDebug() << "Removing existing login items (non-sandbox)"; {
QProcess::execute("osascript", { QProcess::execute("osascript", {
"-e tell application \"System Events\" to delete every login item whose name is \"" + appName() + "\"" "-e tell application \"System Events\" to delete every login item whose name is \"" + appName() + "\""
}); });
}
// Now install the login item, if needed. // Now install the login item, if needed.
if ( autostart ) if ( autostart )
{ {
qDebug() << "Creating login item (non-sandbox)";
QProcess::execute("osascript", { QProcess::execute("osascript", {
"-e tell application \"System Events\" to make login item at end with properties {path:\"" + appPath() + "\", hidden:true, name: \"" + appName() + "\"}" "-e tell application \"System Events\" to make login item at end with properties {path:\"" + appPath() + "\", hidden:true, name: \"" + appName() + "\"}"
}); });
@ -180,8 +97,6 @@ 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,14 +30,10 @@ 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