Changed group list loading to use Qt JSON instead of boost.
This commit is contained in:
parent
7ac25bf2da
commit
dcc21b7e64
@ -16,15 +16,18 @@
|
|||||||
#include "instancemodel.h"
|
#include "instancemodel.h"
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QFile>
|
||||||
#include <QDirIterator>
|
#include <QDirIterator>
|
||||||
#include "stdinstance.h"
|
#include <QTextStream>
|
||||||
|
|
||||||
#include "../util/pathutils.h"
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonObject>
|
||||||
#include <boost/property_tree/json_parser.hpp>
|
#include <QJsonArray>
|
||||||
#include <boost/foreach.hpp>
|
|
||||||
|
|
||||||
|
#include "data/stdinstance.h"
|
||||||
|
#include "util/pathutils.h"
|
||||||
|
|
||||||
#define GROUP_FILE_FORMAT_VERSION 1
|
#define GROUP_FILE_FORMAT_VERSION 1
|
||||||
|
|
||||||
@ -65,47 +68,98 @@ void InstanceModel::initialLoad(QString dir)
|
|||||||
// temporary map from instance ID to group name
|
// temporary map from instance ID to group name
|
||||||
QMap<QString, QString> groupMap;
|
QMap<QString, QString> groupMap;
|
||||||
|
|
||||||
using namespace boost::property_tree;
|
if (QFileInfo(groupFile).exists())
|
||||||
ptree pt;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
read_json(groupFile.toStdString(), pt);
|
QFile groupFile(groupFile);
|
||||||
|
|
||||||
if (pt.get_optional<int>("formatVersion") != GROUP_FILE_FORMAT_VERSION)
|
if (!groupFile.open(QIODevice::ReadOnly))
|
||||||
{
|
{
|
||||||
// TODO: Discard old formats.
|
// An error occurred. Ignore it.
|
||||||
|
qDebug("Failed to read instance group file.");
|
||||||
|
goto groupParseFail;
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_FOREACH(const ptree::value_type& vp, pt.get_child("groups"))
|
QTextStream in(&groupFile);
|
||||||
{
|
QString jsonStr = in.readAll();
|
||||||
ptree gPt = vp.second;
|
groupFile.close();
|
||||||
QString groupName = QString::fromUtf8(vp.first.c_str());
|
|
||||||
|
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8(), &error);
|
||||||
|
|
||||||
|
if (error.error != QJsonParseError::NoError)
|
||||||
|
{
|
||||||
|
qWarning(QString("Failed to parse instance group file: %1 at offset %2").
|
||||||
|
arg(error.errorString(), QString::number(error.offset)).toUtf8());
|
||||||
|
goto groupParseFail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!jsonDoc.isObject())
|
||||||
|
{
|
||||||
|
qWarning("Invalid group file. Root entry should be an object.");
|
||||||
|
goto groupParseFail;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject rootObj = jsonDoc.object();
|
||||||
|
|
||||||
|
// Make sure the format version matches.
|
||||||
|
if (rootObj.value("formatVersion").toVariant().toInt() == GROUP_FILE_FORMAT_VERSION)
|
||||||
|
{
|
||||||
|
// Get the group list.
|
||||||
|
if (!rootObj.value("groups").isObject())
|
||||||
|
{
|
||||||
|
qWarning("Invalid group list JSON: 'groups' should be an object.");
|
||||||
|
goto groupParseFail;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Iterate through the list.
|
||||||
|
QJsonObject groupList = rootObj.value("groups").toObject();
|
||||||
|
|
||||||
|
for (QJsonObject::iterator iter = groupList.begin();
|
||||||
|
iter != groupList.end(); iter++)
|
||||||
|
{
|
||||||
|
QString groupName = iter.key();
|
||||||
|
|
||||||
|
// If not an object, complain and skip to the next one.
|
||||||
|
if (!iter.value().isObject())
|
||||||
|
{
|
||||||
|
qWarning(QString("Group '%1' in the group list should "
|
||||||
|
"be an object.").arg(groupName).toUtf8());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject groupObj = iter.value().toObject();
|
||||||
|
|
||||||
|
// Create the group object.
|
||||||
InstanceGroup *group = new InstanceGroup(groupName, this);
|
InstanceGroup *group = new InstanceGroup(groupName, this);
|
||||||
groups.push_back(group);
|
groups.push_back(group);
|
||||||
|
|
||||||
if (gPt.get_child_optional("hidden"))
|
// If 'hidden' isn't a bool value, just assume it's false.
|
||||||
group->setHidden(gPt.get<bool>("hidden"));
|
if (groupObj.value("hidden").isBool() && groupObj.value("hidden").toBool())
|
||||||
|
{
|
||||||
|
group->setHidden(groupObj.value("hidden").toBool());
|
||||||
|
}
|
||||||
|
|
||||||
QVector<QString> groupInstances;
|
if (!groupObj.value("instances").isArray())
|
||||||
BOOST_FOREACH(const ptree::value_type& v, gPt.get_child("instances"))
|
|
||||||
{
|
{
|
||||||
QString key = QString::fromUtf8(v.second.data().c_str());
|
qWarning(QString("Group '%1' in the group list is invalid. "
|
||||||
groupMap[key] = groupName;
|
"It should contain an array "
|
||||||
|
"called 'instances'.").arg(groupName).toUtf8());
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
// Iterate through the list of instances in the group.
|
||||||
catch (json_parser_error e)
|
QJsonArray instancesArray = groupObj.value("instances").toArray();
|
||||||
|
|
||||||
|
for (QJsonArray::iterator iter2 = instancesArray.begin();
|
||||||
|
iter2 != instancesArray.end(); iter2++)
|
||||||
{
|
{
|
||||||
qDebug("Failed to read group list. JSON parser error.");
|
groupMap[(*iter2).toString()] = groupName;
|
||||||
// wxLogError(_(),
|
|
||||||
// e.line(), wxStr(e.message()).c_str());
|
|
||||||
}
|
}
|
||||||
catch (ptree_error e)
|
|
||||||
{
|
|
||||||
qDebug("Failed to read group list. Unknown ptree error.");
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
groupParseFail:
|
||||||
|
|
||||||
qDebug("Loading instances");
|
qDebug("Loading instances");
|
||||||
QDir instDir(dir);
|
QDir instDir(dir);
|
||||||
|
Loading…
Reference in New Issue
Block a user