diff --git a/client/ui/qautostart.cpp b/client/ui/qautostart.cpp index b0165dc4..a9f84e30 100644 --- a/client/ui/qautostart.cpp +++ b/client/ui/qautostart.cpp @@ -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 + +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 << "\n"; + ts << "\n"; + ts << "\n"; + ts << "\n"; + ts << " Label\n"; + ts << " " << bundleIdentifier() << "\n"; + ts << " ProgramArguments\n"; + ts << " \n"; + ts << " " << Autostart::appPath() << "\n"; + ts << " \n"; + ts << " RunAtLoad\n"; + ts << " \n"; + ts << "\n"; + ts << "\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"); diff --git a/client/ui/qautostart.h b/client/ui/qautostart.h index eef7f846..1e95c933 100644 --- a/client/ui/qautostart.h +++ b/client/ui/qautostart.h @@ -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/.plist) + static QString launchAgentPlistPath(); +#endif +}; #endif // AUTOSTART_H