Items are loaded from default JSON file in standard location at startup. If no items are found in file, example items are generated.

This commit is contained in:
2025-12-23 13:40:33 +01:00
parent 0e1a0d4959
commit 1fc1b1715d
7 changed files with 100 additions and 31 deletions

View File

@ -28,4 +28,26 @@ bool FileHandler::saveToFile(const QJsonDocument& doc, const QString& fileName)
return true;
}
QByteArray FileHandler::loadJSONDataFromFile(const QString fileName) {
QByteArray jsonData;
QFile file;
QString path = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).at(0);
file.setFileName(path + "/" + fileName);
if (file.exists()) {
qDebug() << "File found, reading content...";
const bool successfulOpened = file.open(QIODevice::ReadOnly | QIODevice::Text);
if (successfulOpened) {
// TODO learn and decide on the differences between "readAll" and using
// streams
jsonData = file.readAll();
file.close();
} else {
qWarning() << "File could not be opened!";
}
} else {
qInfo() << "File not found. Returning empty result...";
}
return jsonData;
}
FileHandler::FileHandler() {}

View File

@ -3,10 +3,12 @@
class QJsonDocument;
class QString;
class QByteArray;
class FileHandler {
public:
static bool saveToFile(const QJsonDocument& doc, const QString& fileName);
static QByteArray loadJSONDataFromFile(const QString fileName);
private:
explicit FileHandler();