mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-21 22:24:07 +01:00
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:
parent
ad559f9d97
commit
34f820be18
2 changed files with 15 additions and 7 deletions
|
@ -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
|
||||
|
|
|
@ -54,6 +54,7 @@ public:
|
|||
virtual void initialize(Settings &settings, Paths &paths) override;
|
||||
virtual void save() override;
|
||||
void closeAll();
|
||||
QJsonArray loadWindowArray(const QString &settingsPath);
|
||||
|
||||
int getGeneration() const;
|
||||
void incGeneration();
|
||||
|
|
Loading…
Reference in a new issue