Read the given file into component containers
| 183 | |
| 184 | // Read the given file into component containers |
| 185 | static bool loadPackProfile(PackProfile * parent, const QString & filename, const QString & componentJsonPattern, ComponentContainer & container) |
| 186 | { |
| 187 | QFile componentsFile(filename); |
| 188 | if (!componentsFile.exists()) |
| 189 | { |
| 190 | qWarning() << "Components file doesn't exist. This should never happen."; |
| 191 | return false; |
| 192 | } |
| 193 | if (!componentsFile.open(QFile::ReadOnly)) |
| 194 | { |
| 195 | qCritical() << "Couldn't open" << componentsFile.fileName() |
| 196 | << " for reading:" << componentsFile.errorString(); |
| 197 | qWarning() << "Ignoring overriden order"; |
| 198 | return false; |
| 199 | } |
| 200 | |
| 201 | // and it's valid JSON |
| 202 | QJsonParseError error; |
| 203 | QJsonDocument doc = QJsonDocument::fromJson(componentsFile.readAll(), &error); |
| 204 | if (error.error != QJsonParseError::NoError) |
| 205 | { |
| 206 | qCritical() << "Couldn't parse" << componentsFile.fileName() << ":" << error.errorString(); |
| 207 | qWarning() << "Ignoring overriden order"; |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // and then read it and process it if all above is true. |
| 212 | try |
| 213 | { |
| 214 | auto obj = Json::requireObject(doc); |
| 215 | // check order file version. |
| 216 | auto version = Json::requireInteger(obj.value("formatVersion")); |
| 217 | if (version != currentComponentsFileVersion) |
| 218 | { |
| 219 | throw JSONValidationError(QObject::tr("Invalid component file version, expected %1") |
| 220 | .arg(currentComponentsFileVersion)); |
| 221 | } |
| 222 | auto orderArray = Json::requireArray(obj.value("components")); |
| 223 | for(auto item: orderArray) |
| 224 | { |
| 225 | auto obj = Json::requireObject(item, "Component must be an object."); |
| 226 | container.append(componentFromJsonV1(parent, componentJsonPattern, obj)); |
| 227 | } |
| 228 | } |
| 229 | catch (const JSONValidationError &err) |
| 230 | { |
| 231 | qCritical() << "Couldn't parse" << componentsFile.fileName() << ": bad file format"; |
| 232 | container.clear(); |
| 233 | return false; |
| 234 | } |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | // END: component file format |
| 239 |
no test coverage detected