#include "WedgeConfig.h"
#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QJsonDocument>
#include <QJsonObject>
#include <QStandardPaths>
#include <QTextStream>
QString ConfigManager::formatOtc(qint64 msecs) {
QDateTime dt = QDateTime::fromMSecsSinceEpoch(msecs);
QDate date = dt.date();
int otcYear = date.year();
QDate otcStart(otcYear, 3, 20);
if (date < otcStart) {
otcStart = otcStart.addYears(-1);
otcYear -= 1;
}
qint64 otcDay = otcStart.daysTo(date) + 1;
return QString("%1 %2 0%3 (%4)")
.arg(dt.toString("hh:mm:ss"))
.arg(otcDay)
.arg(otcYear % 100, 2, 10, QChar('0'))
.arg(dt.toString("dd MMM"));
}
QString ConfigManager::getConfigPath() {
QString path = QDir::homePath() + "/.config/wedge";
QDir dir(path);
if (!dir.exists()) dir.mkpath(".");
return path;
}
void ConfigManager::backupNote() {
const QString src = getConfigPath() + "/wedge.conf";
const QString dst = getConfigPath() + "/wedge.conf.bk";
if (!QFile::exists(src)) return;
QFile::remove(dst);
QFile::copy(src, dst);
}
qint64 ConfigManager::lastBackupTime() {
QFileInfo fi(getConfigPath() + "/wedge.conf.bk");
return fi.exists() ? fi.lastModified().toMSecsSinceEpoch() : 0;
}
void ConfigManager::saveNote(const NoteData &data) {
QJsonObject json;
json["autoStart"] = data.autoStart;
json["bgColor"] = data.bgColor.name();
json["content"] = data.content;
json["focusColor"] = data.focusColor.name();
json["fontFamily"] = data.fontFamily;
json["fontSize"] = data.fontSize;
json["headerFormat"] = data.headerFormat;
json["height"] = data.height;
json["hideTaskbar"] = data.hideTaskbar;
json["highlightColor"] = data.highlightColor.name();
json["ipAddress"] = data.ipAddress;
json["lastModified"] = data.lastModified;
json["lastSynced"] = data.lastSynced;
json["networkRole"] = data.networkRole;
json["networkRoute"] = data.networkRoute;
json["passKey"] = data.passKey;
json["textColor"] = data.textColor.name();
json["width"] = data.width;
json["x"] = data.x;
json["y"] = data.y;
QFile file(getConfigPath() + "/wedge.conf");
if (file.open(QIODevice::WriteOnly)) {
file.write(QJsonDocument(json).toJson());
}
updateAutoStart(data.autoStart);
}
NoteData ConfigManager::loadNote() {
NoteData data;
data.autoStart = false;
data.bgColor = QColor("#486860");
data.content = "";
data.focusColor = QColor("#f6f5f4");
data.fontFamily = "Sans-Serif";
data.fontSize = 12;
data.headerFormat = "otcDay EEE dd MMM*";
data.height = 450;
data.hideTaskbar = false;
data.highlightColor = QColor("#66837C");
data.ipAddress = "192.168.1.10";
data.lastModified = QDateTime::currentMSecsSinceEpoch();
data.lastSynced = 0;
data.networkRole = 0;
data.networkRoute = 0;
data.textColor = QColor("#deddda");
data.width = 350;
data.x = 100;
data.y = 100;
QFile file(getConfigPath() + "/wedge.conf");
if (file.open(QIODevice::ReadOnly)) {
QJsonObject json = QJsonDocument::fromJson(file.readAll()).object();
if (!json.isEmpty()) {
data.autoStart = json["autoStart"].toBool();
data.bgColor = QColor(json["bgColor"].toString());
data.content = json["content"].toString();
data.focusColor = QColor(json["focusColor"].toString());
data.fontFamily = json["fontFamily"].toString();
data.fontSize = json["fontSize"].toInt();
if (json.contains("headerFormat") && !json["headerFormat"].toString().isEmpty())
data.headerFormat = json["headerFormat"].toString();
data.height = json["height"].toInt();
data.hideTaskbar = json["hideTaskbar"].toBool();
data.highlightColor = QColor(json["highlightColor"].toString());
data.ipAddress = json["ipAddress"].toString();
data.lastModified = json["lastModified"].toVariant().toLongLong();
data.lastSynced = json["lastSynced"].toVariant().toLongLong();
data.networkRole = json["networkRole"].toInt();
data.networkRoute = json["networkRoute"].toInt();
data.passKey = json["passKey"].toString();
data.textColor = QColor(json["textColor"].toString());
data.width = json["width"].toInt();
data.x = json["x"].toInt();
data.y = json["y"].toInt();
}
}
return data;
}
void ConfigManager::updateAutoStart(bool enable) {
QString autostartPath = QDir::homePath() + "/.config/autostart";
QDir().mkpath(autostartPath);
QString desktopFile = autostartPath + "/wedge.desktop";
if (enable) {
QFile file(desktopFile);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out << "[Desktop Entry]\nType=Application\nName=Wedge\n";
out << "Exec=" << QCoreApplication::applicationFilePath() << "\n";
out << "Icon=wedge\nComment=Wedge Notes\nTerminal=false\n";
}
} else {
QFile::remove(desktopFile);
}
}