forked from MrBesen/lolautoaccept
117 lines
2.7 KiB
C++
117 lines
2.7 KiB
C++
#include "config.h"
|
|
|
|
#include "files.h"
|
|
#include <QFile>
|
|
#include <QJsonDocument>
|
|
#include <QJsonArray>
|
|
|
|
#include <Log.h>
|
|
|
|
#include "json.h"
|
|
|
|
Config::StageConfig::StageConfig() : enabled(false) {}
|
|
|
|
Config::StageConfig::StageConfig(const QJsonObject& j) {
|
|
if(j["champ"].isString()) {
|
|
champs.push_back(getValue<std::string>(j, "champ", ""));
|
|
}
|
|
if(j["champs"].isArray()) {
|
|
QJsonArray jchamps = j["champs"].toArray();
|
|
for(auto jchamp : jchamps) {
|
|
if(jchamp.isString()) {
|
|
QString jchampstr = jchamp.toString();
|
|
if(!jchampstr.isEmpty()) {
|
|
champs.push_back(jchampstr.toStdString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
enabled = getValue(j, "enabled", false);
|
|
}
|
|
|
|
Config::StageConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
QJsonArray jchamps;
|
|
for(const std::string& champ : champs) {
|
|
if(!champ.empty()) {
|
|
jchamps.push_back(QString::fromStdString(champ));
|
|
}
|
|
}
|
|
|
|
out.insert("champs", jchamps);
|
|
out.insert("enabled", enabled);
|
|
|
|
return out;
|
|
}
|
|
|
|
Config::RootConfig::RootConfig() {}
|
|
|
|
Config::RootConfig::RootConfig(const QJsonObject& j) {
|
|
prepick = getValue<Config::StageConfig>(j, "prepick");
|
|
ban = getValue<Config::StageConfig>(j, "ban");
|
|
pick = getValue<Config::StageConfig>(j, "pick");
|
|
enabledAutoAccept = getValue(j, "enabledAutoAccept", false);
|
|
}
|
|
|
|
Config::RootConfig::operator QJsonObject() const {
|
|
QJsonObject out;
|
|
|
|
out.insert("prepick", (QJsonObject) prepick);
|
|
out.insert("ban", (QJsonObject) ban);
|
|
out.insert("pick", (QJsonObject) pick);
|
|
out.insert("enabledAutoAccept", enabledAutoAccept);
|
|
|
|
return out;
|
|
}
|
|
|
|
Config::Config() {
|
|
configFolderPath = getHome() + ".config/lolautoaccept/";
|
|
configFilePath = configFolderPath + "config.json";
|
|
}
|
|
|
|
Config::~Config() {}
|
|
|
|
bool Config::load() {
|
|
QFile conffile(QString::fromStdString(configFilePath));
|
|
if(!conffile.open(QIODevice::ReadOnly)) {
|
|
Log::error << "could not open configfile: " << configFilePath;
|
|
return false;
|
|
}
|
|
|
|
QJsonParseError err;
|
|
QJsonDocument doc = QJsonDocument::fromJson(conffile.readAll(), &err);
|
|
if(err.error != QJsonParseError::NoError) {
|
|
Log::error << "config parse error: " << err.errorString().toStdString() << " position: " << err.offset;
|
|
return false;
|
|
}
|
|
if(doc.isObject()) {
|
|
root = doc.object();
|
|
Log::info << "config loaded";
|
|
return true;
|
|
}
|
|
|
|
Log::error << "config is not a json object!";
|
|
return false;
|
|
}
|
|
|
|
void Config::save() {
|
|
Log::note << "Config::save()";
|
|
mkdirs(configFolderPath);
|
|
|
|
QFile conffile(QString::fromStdString(configFilePath));
|
|
if(!conffile.open(QIODevice::WriteOnly)) {
|
|
Log::error << "could not open configfile: " << configFilePath;
|
|
return;
|
|
}
|
|
|
|
QJsonDocument doc;
|
|
doc.setObject(root);
|
|
conffile.write(doc.toJson());
|
|
Log::info << "config saved";
|
|
}
|
|
|
|
Config::RootConfig& Config::getConfig() {
|
|
return root;
|
|
}
|