Changed group list loading to use Qt JSON instead of boost.

This commit is contained in:
Andrew 2013-01-29 13:01:04 -06:00
parent 7ac25bf2da
commit dcc21b7e64

View File

@ -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();
groupFile.close();
QJsonParseError error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonStr.toUtf8(), &error);
if (error.error != QJsonParseError::NoError)
{ {
ptree gPt = vp.second; qWarning(QString("Failed to parse instance group file: %1 at offset %2").
QString groupName = QString::fromUtf8(vp.first.c_str()); arg(error.errorString(), QString::number(error.offset)).toUtf8());
goto groupParseFail;
}
InstanceGroup *group = new InstanceGroup(groupName, this); if (!jsonDoc.isObject())
groups.push_back(group); {
qWarning("Invalid group file. Root entry should be an object.");
goto groupParseFail;
}
if (gPt.get_child_optional("hidden")) QJsonObject rootObj = jsonDoc.object();
group->setHidden(gPt.get<bool>("hidden"));
QVector<QString> groupInstances; // Make sure the format version matches.
BOOST_FOREACH(const ptree::value_type& v, gPt.get_child("instances")) if (rootObj.value("formatVersion").toVariant().toInt() == GROUP_FILE_FORMAT_VERSION)
{
// Get the group list.
if (!rootObj.value("groups").isObject())
{ {
QString key = QString::fromUtf8(v.second.data().c_str()); qWarning("Invalid group list JSON: 'groups' should be an object.");
groupMap[key] = groupName; 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);
groups.push_back(group);
// If 'hidden' isn't a bool value, just assume it's false.
if (groupObj.value("hidden").isBool() && groupObj.value("hidden").toBool())
{
group->setHidden(groupObj.value("hidden").toBool());
}
if (!groupObj.value("instances").isArray())
{
qWarning(QString("Group '%1' in the group list is invalid. "
"It should contain an array "
"called 'instances'.").arg(groupName).toUtf8());
continue;
}
// Iterate through the list of instances in the group.
QJsonArray instancesArray = groupObj.value("instances").toArray();
for (QJsonArray::iterator iter2 = instancesArray.begin();
iter2 != instancesArray.end(); iter2++)
{
groupMap[(*iter2).toString()] = groupName;
}
} }
} }
} }
catch (json_parser_error e)
{ groupParseFail:
qDebug("Failed to read group list. JSON parser error.");
// wxLogError(_(),
// e.line(), wxStr(e.message()).c_str());
}
catch (ptree_error e)
{
qDebug("Failed to read group list. Unknown ptree error.");
}
qDebug("Loading instances"); qDebug("Loading instances");
QDir instDir(dir); QDir instDir(dir);