mirror of
https://invent.kde.org/marcoa/a-la-karte.git
synced 2026-03-26 17:03:08 +00:00
inputd: save profiles atomically
This commit is contained in:
parent
9160076aa2
commit
a032bec8c4
1 changed files with 12 additions and 12 deletions
|
|
@ -15,6 +15,7 @@
|
|||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSaveFile>
|
||||
#include <QStandardPaths>
|
||||
|
||||
#include <memory>
|
||||
|
|
@ -87,14 +88,6 @@ static QString gamepadTypeToString(SDL_GamepadType t)
|
|||
return QStringLiteral("unknown");
|
||||
}
|
||||
}
|
||||
|
||||
static QVariantMap unwrapVariantMap(QVariant v)
|
||||
{
|
||||
if (v.canConvert<QVariantMap>()) {
|
||||
return v.toMap();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
QVariantMap InputDaemon::Profile::toVariantMap() const
|
||||
|
|
@ -273,7 +266,10 @@ void InputDaemon::loadProfiles()
|
|||
bool InputDaemon::saveProfiles() const
|
||||
{
|
||||
const QString path = profilesPath();
|
||||
QDir().mkpath(QFileInfo(path).absolutePath());
|
||||
const QString dirPath = QFileInfo(path).absolutePath();
|
||||
if (!QDir().mkpath(dirPath)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QJsonArray profiles;
|
||||
for (const Profile &p : m_profiles) {
|
||||
|
|
@ -289,13 +285,17 @@ bool InputDaemon::saveProfiles() const
|
|||
root.insert(QStringLiteral("profiles"), profiles);
|
||||
root.insert(QStringLiteral("assignments"), assignments);
|
||||
|
||||
QFile f(path);
|
||||
QSaveFile f(path);
|
||||
if (!f.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
f.write(QJsonDocument(root).toJson());
|
||||
return true;
|
||||
const QByteArray payload = QJsonDocument(root).toJson();
|
||||
if (f.write(payload) != payload.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return f.commit();
|
||||
}
|
||||
|
||||
InputDaemon::Profile InputDaemon::profileById(const QString &profileId) const
|
||||
|
|
|
|||
Loading…
Reference in a new issue