Files
Chronos/sources/configurationmanager.cpp
Aurélie Delhaie ffebb3c7d7 starting cloud save
2022-01-09 21:21:18 +01:00

52 lines
1.1 KiB
C++

#include "configurationmanager.h"
ConfigurationManager::ConfigurationManager()
{
if (QFile::exists(GetPath())) {
QFile* file = new QFile(GetPath());
file->open(QIODevice::ReadOnly);
auto json = QString(file->readAll());
file->close();
delete file;
QJsonObject obj = QJsonDocument::fromJson(json.toUtf8()).object();
this->s = Settings::FromJSON(obj);
} else {
this->s = new Settings();
Save();
}
}
ConfigurationManager::~ConfigurationManager()
{
delete s;
}
Settings *ConfigurationManager::GetConfiguration()
{
return s;
}
QString ConfigurationManager::GetPath() {
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (!QDir(path).exists()) {
QDir().mkpath(path);
}
path += "/";
if (!QDir(path).exists()) {
QDir().mkpath(path);
}
path += FILENAME;
return path;
}
void ConfigurationManager::Save() {
QJsonDocument doc(s->ToJSON());
QFile *f = new QFile(GetPath());
if (f->open(QIODevice::WriteOnly)) {
f->write(doc.toJson());
f->close();
}
delete f;
}