Read the given file into component containers
| 174 | |
| 175 | // Read the given file into component containers |
| 176 | static PackProfile::Result loadPackProfile(PackProfile* parent, |
| 177 | const QString& filename, |
| 178 | const QString& componentJsonPattern, |
| 179 | ComponentContainer& container) |
| 180 | { |
| 181 | QFile componentsFile(filename); |
| 182 | if (!componentsFile.exists()) { |
| 183 | auto message = QObject::tr("Components file %1 doesn't exist. This should never happen.").arg(filename); |
| 184 | qCWarning(instanceProfileC) << message; |
| 185 | return PackProfile::Result::Error(message); |
| 186 | } |
| 187 | if (!componentsFile.open(QFile::ReadOnly)) { |
| 188 | auto message = QObject::tr("Couldn't open %1 for reading: %2").arg(componentsFile.fileName(), componentsFile.errorString()); |
| 189 | qCCritical(instanceProfileC) << message; |
| 190 | qCWarning(instanceProfileC) << "Ignoring overridden order"; |
| 191 | return PackProfile::Result::Error(message); |
| 192 | } |
| 193 | |
| 194 | // and it's valid JSON |
| 195 | QJsonParseError error; |
| 196 | QJsonDocument doc = QJsonDocument::fromJson(componentsFile.readAll(), &error); |
| 197 | if (error.error != QJsonParseError::NoError) { |
| 198 | auto message = QObject::tr("Couldn't parse %1 as json: %2").arg(componentsFile.fileName(), error.errorString()); |
| 199 | qCCritical(instanceProfileC) << message; |
| 200 | qCWarning(instanceProfileC) << "Ignoring overridden order"; |
| 201 | return PackProfile::Result::Error(message); |
| 202 | } |
| 203 | |
| 204 | // and then read it and process it if all above is true. |
| 205 | try { |
| 206 | auto obj = Json::requireObject(doc); |
| 207 | // check order file version. |
| 208 | auto version = Json::requireInteger(obj.value("formatVersion")); |
| 209 | if (version != currentComponentsFileVersion) { |
| 210 | throw JSONValidationError(QObject::tr("Invalid component file version, expected %1").arg(currentComponentsFileVersion)); |
| 211 | } |
| 212 | auto orderArray = Json::requireArray(obj.value("components")); |
| 213 | for (auto item : orderArray) { |
| 214 | auto comp_obj = Json::requireObject(item, "Component must be an object."); |
| 215 | container.append(componentFromJsonV1(parent, componentJsonPattern, comp_obj)); |
| 216 | } |
| 217 | } catch ([[maybe_unused]] const JSONValidationError& err) { |
| 218 | auto message = QObject::tr("Couldn't parse %1 : bad file format").arg(componentsFile.fileName()); |
| 219 | qCCritical(instanceProfileC) << message; |
| 220 | qCWarning(instanceProfileC) << "error:" << err.what(); |
| 221 | container.clear(); |
| 222 | return PackProfile::Result::Error(message); |
| 223 | } |
| 224 | return PackProfile::Result::Success(); |
| 225 | } |
| 226 | |
| 227 | // END: component file format |
| 228 |
no test coverage detected