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:
@ -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() {}
|
||||
|
||||
@ -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();
|
||||
|
||||
Reference in New Issue
Block a user