795f9730f8
- Notify at schedule start and optionally N minutes before\n- Add reminder lead setting and TOML persistence\n- Use Linux DBus native notifications with action handling\n- Focus the matching block when a notification is clicked\n- Keep overdue reminders and update validation docs/screenshots
495 lines
13 KiB
C++
495 lines
13 KiB
C++
#include "AppSettings.h"
|
|
|
|
#include <QDebug>
|
|
#include <QDir>
|
|
#include <QFile>
|
|
#include <QFileInfo>
|
|
#include <QProcessEnvironment>
|
|
#include <QSaveFile>
|
|
#include <QSettings>
|
|
#include <QStandardPaths>
|
|
#include <QUuid>
|
|
|
|
#include <sstream>
|
|
#include <string>
|
|
|
|
#include "toml.hpp"
|
|
|
|
namespace
|
|
{
|
|
constexpr const char* kLegacyDeviceIdKey = "sync/deviceId";
|
|
constexpr const char* kLegacyThemeKey = "ui/theme";
|
|
constexpr const char* kLegacyMicaKey = "ui/mica";
|
|
constexpr const char* kLegacySnapKey = "ui/snapMinutes";
|
|
constexpr const char* kLegacyTrayHintKey = "ui/trayHintShown";
|
|
constexpr const char* kLegacyCompactGeometryKey = "ui/compactGeometry";
|
|
constexpr const char* kLegacyScheduleGeometryKey = "ui/scheduleGeometry";
|
|
constexpr const char* kLegacyScheduleSplitterKey = "ui/scheduleSplitter";
|
|
constexpr const char* kLegacyDatabasePathKey = "storage/databasePath";
|
|
|
|
QString normalizedTheme(const QString& value)
|
|
{
|
|
if (value == QStringLiteral("light") || value == QStringLiteral("dark"))
|
|
{
|
|
return value;
|
|
}
|
|
return QStringLiteral("system");
|
|
}
|
|
|
|
int normalizedSnap(int value)
|
|
{
|
|
return (value == 30 || value == 60) ? value : 15;
|
|
}
|
|
|
|
QString newDeviceId()
|
|
{
|
|
return QUuid::createUuid().toString(QUuid::WithoutBraces);
|
|
}
|
|
|
|
const toml::table* subTable(const toml::table& root, const char* key)
|
|
{
|
|
return root[key].as_table();
|
|
}
|
|
|
|
QString stringValue(const toml::table* table, const char* key, const QString& fallback = {})
|
|
{
|
|
if (!table)
|
|
{
|
|
return fallback;
|
|
}
|
|
if (auto value = (*table)[key].value<std::string>())
|
|
{
|
|
return QString::fromStdString(*value);
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
bool boolValue(const toml::table* table, const char* key, bool fallback)
|
|
{
|
|
if (!table)
|
|
{
|
|
return fallback;
|
|
}
|
|
if (auto value = (*table)[key].value<bool>())
|
|
{
|
|
return *value;
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
int intValue(const toml::table* table, const char* key, int fallback)
|
|
{
|
|
if (!table)
|
|
{
|
|
return fallback;
|
|
}
|
|
if (auto value = (*table)[key].value<int64_t>())
|
|
{
|
|
return static_cast<int>(*value);
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
double doubleValue(const toml::table* table, const char* key, double fallback)
|
|
{
|
|
if (!table)
|
|
{
|
|
return fallback;
|
|
}
|
|
if (auto value = (*table)[key].value<double>())
|
|
{
|
|
return *value;
|
|
}
|
|
return fallback;
|
|
}
|
|
} // namespace
|
|
|
|
AppSettings::AppSettings(QString configPath)
|
|
{
|
|
const QString envPath =
|
|
QProcessEnvironment::systemEnvironment().value(QStringLiteral("SCHEDULE_CONFIG_PATH"));
|
|
if (!configPath.trimmed().isEmpty())
|
|
{
|
|
m_configPath = configPath;
|
|
m_useDefaultPath = false;
|
|
}
|
|
else if (!envPath.trimmed().isEmpty())
|
|
{
|
|
m_configPath = envPath;
|
|
m_useDefaultPath = false;
|
|
}
|
|
else
|
|
{
|
|
QString configDir = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation);
|
|
if (configDir.isEmpty())
|
|
{
|
|
configDir = QDir::homePath() + QStringLiteral("/.config/Misaki/Schedule");
|
|
}
|
|
m_configPath = configDir + QStringLiteral("/config.toml");
|
|
m_useDefaultPath = true;
|
|
}
|
|
|
|
applyDefaults();
|
|
load();
|
|
}
|
|
|
|
QString AppSettings::configPath() const
|
|
{
|
|
return m_configPath;
|
|
}
|
|
|
|
void AppSettings::applyDefaults()
|
|
{
|
|
m_databasePath.clear();
|
|
m_deviceId.clear();
|
|
m_themePreference = QStringLiteral("system");
|
|
m_micaEnabled = false;
|
|
m_snapMinutes = 15;
|
|
m_trayHintShown = false;
|
|
m_compactGeometry.clear();
|
|
m_scheduleGeometry.clear();
|
|
m_scheduleSplitter.clear();
|
|
m_aiConfig = AiConfig{};
|
|
m_syncConfig = SyncConfig{};
|
|
}
|
|
|
|
bool AppSettings::isUsingDefaultPath() const
|
|
{
|
|
return m_useDefaultPath;
|
|
}
|
|
|
|
void AppSettings::load()
|
|
{
|
|
const QFileInfo info(m_configPath);
|
|
if (!info.exists())
|
|
{
|
|
if (isUsingDefaultPath())
|
|
{
|
|
loadLegacyQSettings();
|
|
}
|
|
if (m_deviceId.isEmpty())
|
|
{
|
|
m_deviceId = newDeviceId();
|
|
}
|
|
save();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
const std::string path = m_configPath.toStdString();
|
|
const toml::table root = toml::parse_file(path);
|
|
|
|
if (const toml::table* app = subTable(root, "app"))
|
|
{
|
|
m_deviceId = stringValue(app, "device_id");
|
|
}
|
|
if (const toml::table* storage = subTable(root, "storage"))
|
|
{
|
|
m_databasePath = stringValue(storage, "database_path");
|
|
}
|
|
if (const toml::table* ui = subTable(root, "ui"))
|
|
{
|
|
m_themePreference = normalizedTheme(stringValue(ui, "theme", QStringLiteral("system")));
|
|
m_micaEnabled = boolValue(ui, "mica", false);
|
|
m_snapMinutes = normalizedSnap(intValue(ui, "snap_minutes", 15));
|
|
m_trayHintShown = boolValue(ui, "tray_hint_shown", false);
|
|
}
|
|
if (const toml::table* reminders = subTable(root, "reminders"))
|
|
{
|
|
m_reminderLeadMinutes = qBound(0, intValue(reminders, "lead_minutes", 10), 24 * 60);
|
|
}
|
|
if (const toml::table* window = subTable(root, "window"))
|
|
{
|
|
m_compactGeometry = QByteArray::fromBase64(stringValue(window, "compact_geometry").toLatin1());
|
|
m_scheduleGeometry = QByteArray::fromBase64(stringValue(window, "schedule_geometry").toLatin1());
|
|
m_scheduleSplitter = QByteArray::fromBase64(stringValue(window, "schedule_splitter").toLatin1());
|
|
}
|
|
if (const toml::table* sync = subTable(root, "sync"))
|
|
{
|
|
m_syncConfig.enabled = boolValue(sync, "enabled", false);
|
|
m_syncConfig.serverUrl = stringValue(sync, "server_url");
|
|
m_syncConfig.token = stringValue(sync, "token");
|
|
}
|
|
if (const toml::table* ai = subTable(root, "ai"))
|
|
{
|
|
m_aiConfig.baseUrl = stringValue(ai, "base_url", m_aiConfig.baseUrl);
|
|
m_aiConfig.apiKey = stringValue(ai, "api_key");
|
|
m_aiConfig.model = stringValue(ai, "model", m_aiConfig.model);
|
|
m_aiConfig.temperature = doubleValue(ai, "temperature", m_aiConfig.temperature);
|
|
if (m_aiConfig.baseUrl.trimmed().isEmpty())
|
|
{
|
|
m_aiConfig.baseUrl = AiConfig{}.baseUrl;
|
|
}
|
|
if (m_aiConfig.model.trimmed().isEmpty())
|
|
{
|
|
m_aiConfig.model = AiConfig{}.model;
|
|
}
|
|
}
|
|
}
|
|
catch (const toml::parse_error& error)
|
|
{
|
|
qWarning() << "Failed to parse config file" << m_configPath
|
|
<< QString::fromStdString(std::string(error.description()));
|
|
QFile::copy(m_configPath, m_configPath + QStringLiteral(".bak"));
|
|
applyDefaults();
|
|
}
|
|
|
|
if (m_deviceId.isEmpty())
|
|
{
|
|
m_deviceId = newDeviceId();
|
|
save();
|
|
}
|
|
}
|
|
|
|
void AppSettings::loadLegacyQSettings()
|
|
{
|
|
QSettings legacy;
|
|
m_deviceId = legacy.value(QString::fromLatin1(kLegacyDeviceIdKey)).toString();
|
|
m_themePreference =
|
|
normalizedTheme(legacy.value(QString::fromLatin1(kLegacyThemeKey), QStringLiteral("system")).toString());
|
|
m_micaEnabled = legacy.value(QString::fromLatin1(kLegacyMicaKey), false).toBool();
|
|
m_snapMinutes = normalizedSnap(legacy.value(QString::fromLatin1(kLegacySnapKey), 15).toInt());
|
|
m_trayHintShown = legacy.value(QString::fromLatin1(kLegacyTrayHintKey), false).toBool();
|
|
m_compactGeometry =
|
|
legacy.value(QString::fromLatin1(kLegacyCompactGeometryKey)).toByteArray();
|
|
m_scheduleGeometry =
|
|
legacy.value(QString::fromLatin1(kLegacyScheduleGeometryKey)).toByteArray();
|
|
m_scheduleSplitter =
|
|
legacy.value(QString::fromLatin1(kLegacyScheduleSplitterKey)).toByteArray();
|
|
m_databasePath = legacy.value(QString::fromLatin1(kLegacyDatabasePathKey)).toString();
|
|
}
|
|
|
|
void AppSettings::save()
|
|
{
|
|
if (m_configPath.isEmpty())
|
|
{
|
|
return;
|
|
}
|
|
|
|
const QFileInfo info(m_configPath);
|
|
if (!QDir().mkpath(info.absolutePath()))
|
|
{
|
|
qWarning() << "Failed to create config directory" << info.absolutePath();
|
|
return;
|
|
}
|
|
|
|
toml::table root;
|
|
root.insert("schema_version", 1);
|
|
|
|
toml::table app;
|
|
app.insert("device_id", m_deviceId.toStdString());
|
|
root.insert("app", std::move(app));
|
|
|
|
toml::table storage;
|
|
storage.insert("database_path", m_databasePath.toStdString());
|
|
root.insert("storage", std::move(storage));
|
|
|
|
toml::table ui;
|
|
ui.insert("theme", m_themePreference.toStdString());
|
|
ui.insert("mica", m_micaEnabled);
|
|
ui.insert("snap_minutes", m_snapMinutes);
|
|
ui.insert("tray_hint_shown", m_trayHintShown);
|
|
root.insert("ui", std::move(ui));
|
|
|
|
toml::table reminders;
|
|
reminders.insert("lead_minutes", m_reminderLeadMinutes);
|
|
root.insert("reminders", std::move(reminders));
|
|
|
|
toml::table window;
|
|
window.insert("compact_geometry", m_compactGeometry.toBase64().toStdString());
|
|
window.insert("schedule_geometry", m_scheduleGeometry.toBase64().toStdString());
|
|
window.insert("schedule_splitter", m_scheduleSplitter.toBase64().toStdString());
|
|
root.insert("window", std::move(window));
|
|
|
|
toml::table sync;
|
|
sync.insert("enabled", m_syncConfig.enabled);
|
|
sync.insert("server_url", m_syncConfig.serverUrl.toStdString());
|
|
sync.insert("token", m_syncConfig.token.toStdString());
|
|
root.insert("sync", std::move(sync));
|
|
|
|
toml::table ai;
|
|
ai.insert("base_url", m_aiConfig.baseUrl.toStdString());
|
|
ai.insert("api_key", m_aiConfig.apiKey.toStdString());
|
|
ai.insert("model", m_aiConfig.model.toStdString());
|
|
ai.insert("temperature", m_aiConfig.temperature);
|
|
root.insert("ai", std::move(ai));
|
|
|
|
std::ostringstream stream;
|
|
stream << root;
|
|
const QString content = QStringLiteral(
|
|
"# AI Schedule configuration. API key is stored locally; keep this file private.\n")
|
|
+ QString::fromStdString(stream.str());
|
|
|
|
QSaveFile file(m_configPath);
|
|
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
|
|
{
|
|
qWarning() << "Failed to open config for writing" << m_configPath << file.errorString();
|
|
return;
|
|
}
|
|
file.write(content.toUtf8());
|
|
if (!file.commit())
|
|
{
|
|
qWarning() << "Failed to write config" << m_configPath << file.errorString();
|
|
return;
|
|
}
|
|
|
|
#ifndef Q_OS_WIN
|
|
QFile::setPermissions(m_configPath, QFileDevice::ReadOwner | QFileDevice::WriteOwner);
|
|
#endif
|
|
}
|
|
|
|
QString AppSettings::databasePath() const
|
|
{
|
|
const QString envPath =
|
|
QProcessEnvironment::systemEnvironment().value(QStringLiteral("SCHEDULE_DB_PATH"));
|
|
if (!envPath.trimmed().isEmpty())
|
|
{
|
|
return envPath;
|
|
}
|
|
if (!m_databasePath.trimmed().isEmpty())
|
|
{
|
|
return m_databasePath;
|
|
}
|
|
|
|
QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
if (dataDir.isEmpty())
|
|
{
|
|
dataDir = QDir::homePath() + QStringLiteral("/.local/share/Misaki/Schedule");
|
|
}
|
|
return dataDir + QStringLiteral("/schedule.db");
|
|
}
|
|
|
|
QString AppSettings::deviceId()
|
|
{
|
|
if (m_deviceId.isEmpty())
|
|
{
|
|
m_deviceId = newDeviceId();
|
|
save();
|
|
}
|
|
return m_deviceId;
|
|
}
|
|
|
|
QString AppSettings::themePreference() const
|
|
{
|
|
return m_themePreference;
|
|
}
|
|
|
|
void AppSettings::setThemePreference(const QString& preference)
|
|
{
|
|
m_themePreference = normalizedTheme(preference);
|
|
save();
|
|
}
|
|
|
|
bool AppSettings::micaEnabled() const
|
|
{
|
|
return m_micaEnabled;
|
|
}
|
|
|
|
void AppSettings::setMicaEnabled(bool enabled)
|
|
{
|
|
m_micaEnabled = enabled;
|
|
save();
|
|
}
|
|
|
|
int AppSettings::snapMinutes() const
|
|
{
|
|
return m_snapMinutes;
|
|
}
|
|
|
|
void AppSettings::setSnapMinutes(int minutes)
|
|
{
|
|
m_snapMinutes = normalizedSnap(minutes);
|
|
save();
|
|
}
|
|
|
|
int AppSettings::reminderLeadMinutes() const
|
|
{
|
|
return m_reminderLeadMinutes;
|
|
}
|
|
|
|
void AppSettings::setReminderLeadMinutes(int minutes)
|
|
{
|
|
m_reminderLeadMinutes = qBound(0, minutes, 24 * 60);
|
|
save();
|
|
}
|
|
|
|
bool AppSettings::trayHintShown() const
|
|
{
|
|
return m_trayHintShown;
|
|
}
|
|
|
|
void AppSettings::setTrayHintShown(bool shown)
|
|
{
|
|
m_trayHintShown = shown;
|
|
save();
|
|
}
|
|
|
|
QByteArray AppSettings::compactGeometry() const
|
|
{
|
|
return m_compactGeometry;
|
|
}
|
|
|
|
void AppSettings::setCompactGeometry(const QByteArray& geometry)
|
|
{
|
|
m_compactGeometry = geometry;
|
|
save();
|
|
}
|
|
|
|
QByteArray AppSettings::scheduleGeometry() const
|
|
{
|
|
return m_scheduleGeometry;
|
|
}
|
|
|
|
void AppSettings::setScheduleGeometry(const QByteArray& geometry)
|
|
{
|
|
m_scheduleGeometry = geometry;
|
|
save();
|
|
}
|
|
|
|
QByteArray AppSettings::scheduleSplitter() const
|
|
{
|
|
return m_scheduleSplitter;
|
|
}
|
|
|
|
void AppSettings::setScheduleSplitter(const QByteArray& state)
|
|
{
|
|
m_scheduleSplitter = state;
|
|
save();
|
|
}
|
|
|
|
AiConfig AppSettings::aiConfig() const
|
|
{
|
|
return m_aiConfig;
|
|
}
|
|
|
|
SyncConfig AppSettings::syncConfig() const
|
|
{
|
|
return m_syncConfig;
|
|
}
|
|
|
|
void AppSettings::setSyncConfig(const SyncConfig& config)
|
|
{
|
|
m_syncConfig = config;
|
|
if (!m_syncConfig.serverUrl.trimmed().isEmpty())
|
|
{
|
|
m_syncConfig.serverUrl = m_syncConfig.serverUrl.trimmed();
|
|
}
|
|
m_syncConfig.token = m_syncConfig.token.trimmed();
|
|
save();
|
|
}
|
|
|
|
void AppSettings::setAiConfig(const AiConfig& config)
|
|
{
|
|
m_aiConfig = config;
|
|
if (m_aiConfig.baseUrl.trimmed().isEmpty())
|
|
{
|
|
m_aiConfig.baseUrl = AiConfig{}.baseUrl;
|
|
}
|
|
if (m_aiConfig.model.trimmed().isEmpty())
|
|
{
|
|
m_aiConfig.model = AiConfig{}.model;
|
|
}
|
|
save();
|
|
}
|