Files
DualFloatingClock/autostart_manager.cpp
T
sebastian 45fc172fc8
Build and Release DualFloatingClock / build-linux (push) Failing after 32s
CI Build / build (macos-latest) (push) Canceled after 0s
CI Build / build (windows-latest) (push) Canceled after 0s
Build and Release DualFloatingClock / build-macos (push) Canceled after 0s
Build and Release DualFloatingClock / build-windows (push) Canceled after 0s
Build and Release DualFloatingClock / generate-changelog (push) Canceled after 0s
Build and Release DualFloatingClock / create-release (push) Canceled after 0s
feat: rename project to DualFloatingClock, add README, public release
- Full rename: CMake target, bundle, LaunchAgent label
  (com.dualfloatingclock.clock), QSettings org/app, tray tooltip,
  icns resource, build scripts, CI workflows, sample config
- Config XML: new root DualFloatingClockConfig; reader accepts legacy
  ClouckConfig root for painless migration of existing settings
- README rewritten: fork provenance (ivaquero/clouck), features, build,
  tests, usage
- Drop stale README-CN.md
2026-08-27 22:36:05 +03:00

113 lines
2.7 KiB
C++

#include "autostart_manager.h"
#include <QCoreApplication>
#include <QDir>
#include <QFileInfo>
#include <QSaveFile>
#include <QStandardPaths>
#include <QXmlStreamWriter>
namespace
{
const QString launchAgentLabel = QStringLiteral("com.dualfloatingclock.clock");
}
bool AutoStartManager::enableForCurrentApplication()
{
#ifdef Q_OS_MAC
return enableForPath(QCoreApplication::applicationFilePath());
#else
return false;
#endif
}
bool AutoStartManager::enableForPath(const QString &executablePath)
{
#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(executablePath).toUtf8());
return agentFile.commit();
#else
Q_UNUSED(executablePath);
return false;
#endif
}
bool AutoStartManager::disable()
{
#ifdef Q_OS_MAC
const QString agentPath = launchAgentPath();
if (agentPath.isEmpty() || !QFile::exists(agentPath))
{
return true; // already gone — desired state reached
}
return QFile::remove(agentPath);
#else
return false;
#endif
}
bool AutoStartManager::isEnabled()
{
#ifdef Q_OS_MAC
return QFile::exists(launchAgentPath());
#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");
}