lolautoaccept/src/champcache.cpp

55 lines
1.3 KiB
C++
Raw Normal View History

2022-07-05 23:45:28 +02:00
#include "champcache.h"
#include "files.h"
#include <QFile>
#include <QFileInfo>
#include <QDateTime>
#include <Log.h>
ChampCache::ChampCache() {
basefolder = getCache();
}
// the age of f in seconds
static qint64 ageOfFile(QFile& f) {
QFileInfo info(f);
return info.lastModified().secsTo(QDateTime::currentDateTime());
}
2023-05-31 22:22:23 +02:00
QString ChampCache::getVersion() {
QFile versionfile(basefolder + "version");
2022-07-05 23:45:28 +02:00
if(ageOfFile(versionfile) < (qint64) maxage) {
versionfile.open(QFile::ReadOnly);
2023-05-31 22:22:23 +02:00
return versionfile.readAll();
2022-07-05 23:45:28 +02:00
}
return {}; // empty string
}
QJsonDocument ChampCache::getChamps() {
2023-05-31 22:22:23 +02:00
QFile champsfile(basefolder + "champs.json");
2022-07-05 23:45:28 +02:00
if(ageOfFile(champsfile) < (qint64) maxage) {
champsfile.open(QFile::ReadOnly);
QByteArray bytes = champsfile.readAll();
QJsonDocument doc = QJsonDocument::fromJson(bytes);
return doc;
}
return {}; // empty document
}
2023-05-31 22:22:23 +02:00
void ChampCache::saveChamps(QJsonDocument doc, const QString& version) {
2022-07-05 23:45:28 +02:00
QByteArray arr = doc.toJson();
2023-05-31 22:22:23 +02:00
QFile champsfile(basefolder + "champs.json");
2022-07-05 23:45:28 +02:00
champsfile.open(QFile::WriteOnly | QFile::Truncate);
champsfile.write(arr);
2023-05-31 22:22:23 +02:00
QFile versionfile(basefolder + "version");
2022-07-05 23:45:28 +02:00
versionfile.open(QFile::WriteOnly | QFile::Truncate);
2023-05-31 22:22:23 +02:00
versionfile.write(version.toLocal8Bit());
2022-07-05 23:45:28 +02:00
versionfile.close();
2023-05-31 22:22:23 +02:00
qInfo() << "saved Champs and version Cache";
2022-07-05 23:45:28 +02:00
}