feat: add debug logging to Autostart functionality on macOS

This commit is contained in:
Yaroslav Yashin 2025-05-13 15:32:00 +03:00
parent 3bd25656fb
commit aa023c2919

View file

@ -30,6 +30,8 @@
#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"
@ -58,7 +60,7 @@ QString Autostart::appPath() {
return QCoreApplication::applicationFilePath() + " --autostart"; return QCoreApplication::applicationFilePath() + " --autostart";
} }
#elif defined(Q_OS_MACOS) #elif defined(Q_OS_MACX) || defined(Q_OS_MACOS)
#if defined(MACOS_NE) #if defined(MACOS_NE)
#include <QFile> #include <QFile>
@ -99,7 +101,9 @@ static QByteArray buildPlist()
bool Autostart::isAutostart() bool Autostart::isAutostart()
{ {
return QFile::exists(launchAgentPlistPath()); const bool exists = QFile::exists(launchAgentPlistPath());
qDebug() << "isAutostart?" << exists << "plistPath=" << launchAgentPlistPath();
return exists;
} }
void Autostart::setAutostart(bool autostart) void Autostart::setAutostart(bool autostart)
@ -108,16 +112,20 @@ void Autostart::setAutostart(bool autostart)
if (!autostart) if (!autostart)
{ {
qDebug() << "Removing LaunchAgent plist" << plistPath;
QFile::remove(plistPath); QFile::remove(plistPath);
return; return;
} }
qDebug() << "Creating LaunchAgent plist" << plistPath;
QDir().mkpath(QFileInfo(plistPath).absolutePath()); QDir().mkpath(QFileInfo(plistPath).absolutePath());
QFile f(plistPath); QFile f(plistPath);
if (f.open(QIODevice::WriteOnly | QIODevice::Truncate)) if (f.open(QIODevice::WriteOnly | QIODevice::Truncate))
{ {
f.write(buildPlist()); QByteArray data = buildPlist();
f.write(data);
qDebug() << "Written plist bytes=" << data.size();
f.close(); f.close();
} }
} }
@ -140,21 +148,23 @@ bool Autostart::isAutostart() {
}); });
process.waitForFinished(3000); process.waitForFinished(3000);
const auto output = QString::fromLocal8Bit(process.readAllStandardOutput()); const auto output = QString::fromLocal8Bit(process.readAllStandardOutput());
return output.contains(appPath()); const bool contains = 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() + "\"}"
}); });