Files
DualFloatingClock/autostart_manager.cpp
T
sebastian 6b9cb32534 feat: dual time zones, autostart, tray icon, config relocation, tests
- Dual clock (primary/secondary time zone) with IANA picker, persisted in XML
- AutoStartManager: macOS LaunchAgent (com.clouck.clock, RunAtLoad)
- Tray icon: template image, light/dark adaptation, toggle + quit menu
- ConfigManager: default path moved to QStandardPaths::AppConfigLocation
- Qt Test suite (ConfigManager, AutoStartManager) wired into CTest
- GitHub origin renamed to upstream; private Gitea repo as origin
2026-08-19 21:09:06 +03:00

80 lines
2.1 KiB
C++

#include "autostart_manager.h"
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QSaveFile>
#include <QStandardPaths>
#include <QXmlStreamWriter>
namespace
{
const QString launchAgentLabel = QStringLiteral("com.clouck.clock");
}
bool AutoStartManager::enableForCurrentApplication()
{
#ifdef Q_OS_MAC
const QString agentPath = launchAgentPath();
if (agentPath.isEmpty())
{
return false;
}
const QFileInfo agentFileInfo(agentPath);
if (!QDir().mkpath(agentFileInfo.absolutePath()))
{
return false;
}
QSaveFile agentFile(agentPath);
if (!agentFile.open(QIODevice::WriteOnly | QIODevice::Text))
{
return false;
}
agentFile.write(launchAgentContents(QCoreApplication::applicationFilePath()).toUtf8());
return agentFile.commit();
#else
return false;
#endif
}
QString AutoStartManager::launchAgentContents(const QString &executablePath)
{
QString contents;
QXmlStreamWriter writer(&contents);
writer.setAutoFormatting(true);
writer.writeStartDocument();
writer.writeDTD("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
writer.writeStartElement("plist");
writer.writeAttribute("version", "1.0");
writer.writeStartElement("dict");
writer.writeTextElement("key", "Label");
writer.writeTextElement("string", launchAgentLabel);
writer.writeTextElement("key", "ProgramArguments");
writer.writeStartElement("array");
writer.writeTextElement("string", executablePath);
writer.writeEndElement();
writer.writeTextElement("key", "RunAtLoad");
writer.writeEmptyElement("true");
writer.writeEndElement();
writer.writeEndElement();
writer.writeEndDocument();
return contents;
}
QString AutoStartManager::launchAgentPath()
{
const QString homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
if (homePath.isEmpty())
{
return QString();
}
return QDir(homePath).filePath("Library/LaunchAgents/" + launchAgentLabel + ".plist");
}