From 439c6b43a3432e604afbcef74c36ae5fb73de12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 5 Oct 2015 23:48:38 +0200 Subject: [PATCH] GH-1268 simplify and fix instance group file IO --- logic/InstanceList.cpp | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/logic/InstanceList.cpp b/logic/InstanceList.cpp index c3a6a9c4b..3644e8280 100644 --- a/logic/InstanceList.cpp +++ b/logic/InstanceList.cpp @@ -135,16 +135,6 @@ QStringList InstanceList::getGroups() void InstanceList::saveGroupList() { QString groupFileName = m_instDir + "/instgroups.json"; - QFile groupFile(groupFileName); - - // if you can't open the file, fail - if (!groupFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) - { - // An error occurred. Ignore it. - qCritical() << "Failed to save instance group file."; - return; - } - QTextStream out(&groupFile); QMap> groupMap; for (auto instance : m_instances) { @@ -187,8 +177,7 @@ void InstanceList::saveGroupList() } toplevel.insert("groups", groupsArr); QJsonDocument doc(toplevel); - groupFile.write(doc.toJson()); - groupFile.close(); + FS::write(groupFileName, doc.toJson()); } void InstanceList::loadGroupList(QMap &groupMap) @@ -199,22 +188,19 @@ void InstanceList::loadGroupList(QMap &groupMap) if (!QFileInfo(groupFileName).exists()) return; - QFile groupFile(groupFileName); - - // if you can't open the file, fail - if (!groupFile.open(QIODevice::ReadOnly)) + QByteArray jsonData; + try { - // An error occurred. Ignore it. - qCritical() << "Failed to read instance group file."; + jsonData = FS::read(groupFileName); + } + catch (FS::FileSystemException & e) + { + qCritical() << "Failed to read instance group file :" << e.cause(); return; } - QTextStream in(&groupFile); - QString jsonStr = in.readAll(); - groupFile.close(); - QJsonParseError error; - QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8(), &error); + QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error); // if the json was bad, fail if (error.error != QJsonParseError::NoError)