mirror of
https://invent.kde.org/marcoa/a-la-karte.git
synced 2026-03-26 17:03:08 +00:00
app: save library.json atomically
This commit is contained in:
parent
05ca3de5f5
commit
c6f3e645f4
1 changed files with 17 additions and 3 deletions
20
src/app.cpp
20
src/app.cpp
|
|
@ -18,6 +18,7 @@
|
|||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonParseError>
|
||||
#include <QSaveFile>
|
||||
#include <QStandardPaths>
|
||||
#include <QtConcurrent>
|
||||
|
||||
|
|
@ -936,10 +937,14 @@ void App::saveLibrary()
|
|||
QString dataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
QDir dir(dataPath);
|
||||
if (!dir.exists()) {
|
||||
dir.mkpath(dataPath);
|
||||
if (!dir.mkpath(dataPath)) {
|
||||
qWarning() << "Failed to create data directory:" << dataPath;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
QFile file(dataPath + QStringLiteral("/library.json"));
|
||||
const QString libraryPath = dataPath + QStringLiteral("/library.json");
|
||||
QSaveFile file(libraryPath);
|
||||
if (!file.open(QIODevice::WriteOnly)) {
|
||||
qWarning() << "Failed to save library:" << file.errorString();
|
||||
return;
|
||||
|
|
@ -954,7 +959,16 @@ void App::saveLibrary()
|
|||
}
|
||||
|
||||
QJsonDocument doc(gamesArray);
|
||||
file.write(doc.toJson());
|
||||
const QByteArray payload = doc.toJson();
|
||||
if (file.write(payload) != payload.size()) {
|
||||
qWarning() << "Failed to save library:" << file.errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!file.commit()) {
|
||||
qWarning() << "Failed to save library:" << file.errorString();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void App::loadLibrary()
|
||||
|
|
|
|||
Loading…
Reference in a new issue