Revert "feat: add autostart support for Mac App Store builds on macOS"
This reverts commit 3bd25656fb
.
This commit is contained in:
parent
aa023c2919
commit
dd0e2a6cb8
2 changed files with 10 additions and 99 deletions
|
@ -30,8 +30,6 @@
|
|||
#include <QString>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
#if defined (Q_OS_WIN)
|
||||
#define REG_KEY "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run"
|
||||
|
@ -60,86 +58,7 @@ QString Autostart::appPath() {
|
|||
return QCoreApplication::applicationFilePath() + " --autostart";
|
||||
}
|
||||
|
||||
#elif defined(Q_OS_MACX) || 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()
|
||||
{
|
||||
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
|
||||
#elif defined Q_OS_MACX
|
||||
|
||||
bool Autostart::isAutostart() {
|
||||
QProcess process;
|
||||
|
@ -148,23 +67,21 @@ bool Autostart::isAutostart() {
|
|||
});
|
||||
process.waitForFinished(3000);
|
||||
const auto output = QString::fromLocal8Bit(process.readAllStandardOutput());
|
||||
const bool contains = output.contains(appPath());
|
||||
qDebug() << "AppleScript isAutostart?" << contains;
|
||||
return contains;
|
||||
return output.contains(appPath());
|
||||
}
|
||||
|
||||
void Autostart::setAutostart(bool autostart) {
|
||||
// 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.
|
||||
qDebug() << "Removing existing login items (non-sandbox)";
|
||||
QProcess::execute("osascript", {
|
||||
"-e tell application \"System Events\" to delete every login item whose name is \"" + appName() + "\""
|
||||
});
|
||||
{
|
||||
QProcess::execute("osascript", {
|
||||
"-e tell application \"System Events\" to delete every login item whose name is \"" + appName() + "\""
|
||||
});
|
||||
}
|
||||
|
||||
// Now install the login item, if needed.
|
||||
if ( autostart )
|
||||
{
|
||||
qDebug() << "Creating login item (non-sandbox)";
|
||||
QProcess::execute("osascript", {
|
||||
"-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;
|
||||
}
|
||||
|
||||
#endif // MACOS_NE
|
||||
|
||||
#elif defined (Q_OS_LINUX)
|
||||
bool Autostart::isAutostart() {
|
||||
QFileInfo check_file(QDir::homePath() + "/.config/autostart/" + appName() +".desktop");
|
||||
|
|
|
@ -30,14 +30,10 @@ 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