implemented fallback logic for corrupted window layout (#1039)
* fallback logic for corrupted window layout 1. before saving the window-layout a backup will created to avoid corruption due to crashes while saving 2. when starting chatterino and the window-layout file returns and empty window layout (due to corruptio) the backup will be read and the layout will be build from this data * Update WindowManager.hpp * used QSaveFile instead of crude custom implementation * implemented suggested feedback from review * proper method call was tired and slightly drunk Kapp
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSaveFile>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
@@ -258,11 +259,7 @@ void WindowManager::initialize(Settings &settings, Paths &paths)
|
||||
|
||||
// load file
|
||||
QString settingsPath = getPaths()->settingsDirectory + SETTINGS_FILENAME;
|
||||
QFile file(settingsPath);
|
||||
file.open(QIODevice::ReadOnly);
|
||||
QByteArray data = file.readAll();
|
||||
QJsonDocument document = QJsonDocument::fromJson(data);
|
||||
QJsonArray windows_arr = document.object().value("windows").toArray();
|
||||
QJsonArray windows_arr = this->loadWindowArray(settingsPath);
|
||||
|
||||
// "deserialize"
|
||||
for (QJsonValue window_val : windows_arr)
|
||||
@@ -477,7 +474,7 @@ void WindowManager::save()
|
||||
|
||||
// save file
|
||||
QString settingsPath = getPaths()->settingsDirectory + SETTINGS_FILENAME;
|
||||
QFile file(settingsPath);
|
||||
QSaveFile file(settingsPath);
|
||||
file.open(QIODevice::WriteOnly | QIODevice::Truncate);
|
||||
|
||||
QJsonDocument::JsonFormat format =
|
||||
@@ -489,7 +486,7 @@ void WindowManager::save()
|
||||
;
|
||||
|
||||
file.write(document.toJson(format));
|
||||
file.flush();
|
||||
file.commit();
|
||||
}
|
||||
|
||||
void WindowManager::sendAlert()
|
||||
@@ -622,4 +619,14 @@ void WindowManager::incGeneration()
|
||||
this->generation_++;
|
||||
}
|
||||
|
||||
QJsonArray WindowManager::loadWindowArray(const QString &settingsPath)
|
||||
{
|
||||
QFile file(settingsPath);
|
||||
file.open(QIODevice::ReadOnly);
|
||||
QByteArray data = file.readAll();
|
||||
QJsonDocument document = QJsonDocument::fromJson(data);
|
||||
QJsonArray windows_arr = document.object().value("windows").toArray();
|
||||
return windows_arr;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
Reference in New Issue
Block a user