| 655 | } |
| 656 | |
| 657 | void InstanceList::loadGroupList() |
| 658 | { |
| 659 | qDebug() << "Will load group list now."; |
| 660 | |
| 661 | QString groupFileName = m_instDir + "/instgroups.json"; |
| 662 | |
| 663 | // if there's no group file, fail |
| 664 | if (!QFileInfo(groupFileName).exists()) |
| 665 | return; |
| 666 | |
| 667 | QByteArray jsonData; |
| 668 | try { |
| 669 | jsonData = FS::read(groupFileName); |
| 670 | } catch (const FS::FileSystemException& e) { |
| 671 | qCritical() << "Failed to read instance group file :" << e.cause(); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | QJsonParseError error; |
| 676 | QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonData, &error); |
| 677 | |
| 678 | // if the json was bad, fail |
| 679 | if (error.error != QJsonParseError::NoError) { |
| 680 | qCritical() << QString("Failed to parse instance group file: %1 at offset %2") |
| 681 | .arg(error.errorString(), QString::number(error.offset)) |
| 682 | .toUtf8(); |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | // if the root of the json wasn't an object, fail |
| 687 | if (!jsonDoc.isObject()) { |
| 688 | qWarning() << "Invalid group file. Root entry should be an object."; |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | QJsonObject rootObj = jsonDoc.object(); |
| 693 | |
| 694 | // Make sure the format version matches, otherwise fail. |
| 695 | if (rootObj.value("formatVersion").toVariant().toInt() != GROUP_FILE_FORMAT_VERSION) |
| 696 | return; |
| 697 | |
| 698 | // Get the groups. if it's not an object, fail |
| 699 | if (!rootObj.value("groups").isObject()) { |
| 700 | qWarning() << "Invalid group list JSON: 'groups' should be an object."; |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | QSet<QString> groupSet; |
| 705 | m_instanceGroupIndex.clear(); |
| 706 | |
| 707 | // Iterate through all the groups. |
| 708 | QJsonObject groupMapping = rootObj.value("groups").toObject(); |
| 709 | for (QJsonObject::iterator iter = groupMapping.begin(); iter != groupMapping.end(); iter++) { |
| 710 | QString groupName = iter.key(); |
| 711 | |
| 712 | // If not an object, complain and skip to the next one. |
| 713 | if (!iter.value().isObject()) { |
| 714 | qWarning() << QString("Group '%1' in the group list should be an object.").arg(groupName).toUtf8(); |