From 613f3651ea21ff654e6bd6b075352044f2621311 Mon Sep 17 00:00:00 2001 From: sithlord48 Date: Fri, 14 Mar 2025 17:18:54 -0400 Subject: [PATCH] refactor: move daemon settings to Settings fixes #8353 --- src/apps/deskflow-daemon/deskflow-daemon.cpp | 8 ++-- src/lib/common/Settings.cpp | 7 +++ src/lib/common/Settings.h | 10 ++++ src/lib/deskflow/DaemonApp.cpp | 50 +++++--------------- src/lib/deskflow/DaemonApp.h | 2 +- 5 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/apps/deskflow-daemon/deskflow-daemon.cpp b/src/apps/deskflow-daemon/deskflow-daemon.cpp index 6a1f710ae..04589465f 100644 --- a/src/apps/deskflow-daemon/deskflow-daemon.cpp +++ b/src/apps/deskflow-daemon/deskflow-daemon.cpp @@ -8,7 +8,7 @@ #include "arch/Arch.h" #include "base/EventQueue.h" #include "base/Log.h" -#include "common/constants.h" +#include "common/Settings.h" #include "deskflow/DaemonApp.h" #include "deskflow/ipc/DaemonIpcServer.h" @@ -80,7 +80,8 @@ int main(int argc, char **argv) LOG_PRINT("%s v%s", QCoreApplication::applicationName().toStdString().c_str(), kDisplayVersion); // Default log level to system setting (found in Registry). - if (std::string logLevel = ARCH->setting("LogLevel"); logLevel != "") { + auto logLevel = Settings::value(Settings::Daemon::LogLevel).toString().toStdString(); + if (logLevel != "") { CLOG->setFilter(logLevel.c_str()); LOG_DEBUG("log level: %s", logLevel.c_str()); } @@ -102,7 +103,8 @@ int main(int argc, char **argv) return kExitSuccess; } - const auto ipcServer = new ipc::DaemonIpcServer(&app, DaemonApp::logFilename().c_str()); // NOSONAR - Qt managed + const auto ipcServer = + new ipc::DaemonIpcServer(&app, DaemonApp::logFilename().toStdString().c_str()); // NOSONAR - Qt managed ipcServer->listen(); daemon.connectIpcServer(ipcServer); diff --git a/src/lib/common/Settings.cpp b/src/lib/common/Settings.cpp index f4dc52d60..3d2e620db 100644 --- a/src/lib/common/Settings.cpp +++ b/src/lib/common/Settings.cpp @@ -119,6 +119,13 @@ QVariant Settings::defaultValue(const QString &key) if (key == Core::ProcessMode) return defaultProcessMode; + if (key == Daemon::LogFile) { + return QStringLiteral("%1/%2").arg(instance()->settingsPath(), kDaemonLogFilename); + } + + if (key == Daemon::Elevate) + return true; + return QVariant(); } diff --git a/src/lib/common/Settings.h b/src/lib/common/Settings.h index 32e98fa1a..465176fd7 100644 --- a/src/lib/common/Settings.h +++ b/src/lib/common/Settings.h @@ -50,6 +50,13 @@ public: inline static const auto ScreenName = QStringLiteral("core/screenName"); inline static const auto StartedBefore = QStringLiteral("core/startedBefore"); }; + struct Daemon + { + inline static const auto Command = QStringLiteral("daemon/command"); + inline static const auto Elevate = QStringLiteral("daemon/elevate"); + inline static const auto LogFile = QStringLiteral("daemon/logFile"); + inline static const auto LogLevel = QStringLiteral("daemon/logLevel"); + }; struct Gui { inline static const auto Autohide = QStringLiteral("gui/autoHide"); @@ -178,6 +185,9 @@ private: , Settings::Core::ProcessMode , Settings::Core::ScreenName , Settings::Core::StartedBefore + , Settings::Daemon::Command + , Settings::Daemon::Elevate + , Settings::Daemon::LogFile , Settings::Log::File , Settings::Log::Level , Settings::Log::ToFile diff --git a/src/lib/deskflow/DaemonApp.cpp b/src/lib/deskflow/DaemonApp.cpp index cd7cfb95c..f609878dc 100644 --- a/src/lib/deskflow/DaemonApp.cpp +++ b/src/lib/deskflow/DaemonApp.cpp @@ -6,11 +6,10 @@ #include "deskflow/DaemonApp.h" -#include "arch/XArch.h" #include "base/IEventQueue.h" #include "base/Log.h" #include "base/log_outputters.h" -#include "common/constants.h" +#include "common/Settings.h" #include "deskflow/App.h" #include "deskflow/ipc/DaemonIpcServer.h" @@ -50,39 +49,21 @@ void DaemonApp::saveLogLevel(const QString &logLevel) const { LOG_DEBUG("log level changed: %s", logLevel.toUtf8().constData()); CLOG->setFilter(logLevel.toUtf8().constData()); - - try { - // saves setting for next time the daemon starts. - ARCH->setting("LogLevel", logLevel.toStdString()); - } catch (XArch &e) { - LOG_ERR("failed to save log level setting: %s", e.what()); - } + Settings::setValue(Settings::Daemon::LogLevel, logLevel); } void DaemonApp::setElevate(bool elevate) { LOG_DEBUG("elevate value changed: %s", elevate ? "yes" : "no"); m_elevate = elevate; - - try { - // saves setting for next time the daemon starts. - ARCH->setting("Elevate", std::string(elevate ? "1" : "0")); - } catch (XArch &e) { - LOG_ERR("failed to save elevate setting: %s", e.what()); - } + Settings::setValue(Settings::Daemon::Elevate, m_elevate); } void DaemonApp::setCommand(const QString &command) { LOG_DEBUG("service command updated"); + Settings::setValue(Settings::Daemon::Command, command); m_command = command.toStdString(); - - try { - // saves setting for next time the daemon starts. - ARCH->setting("Command", command.toStdString()); - } catch (XArch &e) { - LOG_ERR("failed to save command setting: %s", e.what()); - } } void DaemonApp::applyWatchdogCommand() const @@ -113,7 +94,10 @@ void DaemonApp::clearWatchdogCommand() void DaemonApp::clearSettings() const { LOG_INFO("clearing daemon settings"); - ARCH->clearSettings(); + Settings::setValue(Settings::Daemon::Command, QVariant()); + Settings::setValue(Settings::Daemon::Elevate, QVariant()); + Settings::setValue(Settings::Daemon::LogFile, QVariant()); + Settings::setValue(Settings::Daemon::LogLevel, QVariant()); } void DaemonApp::connectIpcServer(const ipc::DaemonIpcServer *ipcServer) const @@ -184,8 +168,8 @@ void DaemonApp::run(QThread &daemonThread) #if SYSAPI_WIN32 m_pWatchdog = std::make_unique(m_foreground, *m_pFileLogOutputter); - std::string command = ARCH->setting("Command"); - bool elevate = ARCH->setting("Elevate") == "1"; + auto command = Settings::value(Settings::Daemon::Command).toString().toStdString(); + bool elevate = Settings::value(Settings::Daemon::Elevate).toBool(); if (!command.empty()) { LOG_DEBUG("using last known command: %s", command.c_str()); m_pWatchdog->setProcessConfig(command, elevate); @@ -253,17 +237,9 @@ int DaemonApp::mainLoop() return kExitSuccess; } -std::string DaemonApp::logFilename() +QString DaemonApp::logFilename() { - string logFilename; - logFilename = ARCH->setting("LogFilename"); - if (logFilename.empty()) { - logFilename = ARCH->getLogDirectory(); - logFilename.append("/"); - logFilename.append(kDaemonLogFilename); - } - - return logFilename; + return Settings::value(Settings::Daemon::LogFile).toString(); } void DaemonApp::setForeground() @@ -282,7 +258,7 @@ void DaemonApp::initLogging() } #endif - m_pFileLogOutputter = new FileLogOutputter(logFilename().c_str()); // NOSONAR - Adopted by `Log` + m_pFileLogOutputter = new FileLogOutputter(logFilename().toStdString().c_str()); // NOSONAR - Adopted by `Log` CLOG->insert(m_pFileLogOutputter); } diff --git a/src/lib/deskflow/DaemonApp.h b/src/lib/deskflow/DaemonApp.h index 3490d7be9..1e74abfa9 100644 --- a/src/lib/deskflow/DaemonApp.h +++ b/src/lib/deskflow/DaemonApp.h @@ -55,7 +55,7 @@ public: void initLogging(); void connectIpcServer(const deskflow::core::ipc::DaemonIpcServer *ipcServer) const; - static std::string logFilename(); + static QString logFilename(); private: void daemonize();