Read the given file into component containers
| 155 | |
| 156 | // Read the given file into component containers |
| 157 | static bool loadPackProfile(PackProfile * parent, const QString & filename, const QString & componentJsonPattern, ComponentContainer & container) |
| 158 | { |
| 159 | QFile componentsFile(filename); |
| 160 | if (!componentsFile.exists()) |
| 161 | { |
| 162 | qWarning() << "Components file doesn't exist. This should never happen."; |
| 163 | return false; |
| 164 | } |
| 165 | if (!componentsFile.open(QFile::ReadOnly)) |
| 166 | { |
| 167 | qCritical() << "Couldn't open" << componentsFile.fileName() |
| 168 | << " for reading:" << componentsFile.errorString(); |
| 169 | qWarning() << "Ignoring overriden order"; |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | // and it's valid JSON |
| 174 | QJsonParseError error; |
| 175 | QJsonDocument doc = QJsonDocument::fromJson(componentsFile.readAll(), &error); |
| 176 | if (error.error != QJsonParseError::NoError) |
| 177 | { |
| 178 | qCritical() << "Couldn't parse" << componentsFile.fileName() << ":" << error.errorString(); |
| 179 | qWarning() << "Ignoring overriden order"; |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | // and then read it and process it if all above is true. |
| 184 | try |
| 185 | { |
| 186 | auto obj = Json::requireObject(doc); |
| 187 | // check order file version. |
| 188 | auto version = Json::requireValueInteger(obj.value("formatVersion")); |
| 189 | if (version != currentComponentsFileVersion) |
| 190 | { |
| 191 | throw JSONValidationError(QObject::tr("Invalid component file version, expected %1") |
| 192 | .arg(currentComponentsFileVersion)); |
| 193 | } |
| 194 | auto orderArray = Json::requireValueArray(obj.value("components")); |
| 195 | for(auto item: orderArray) |
| 196 | { |
| 197 | auto obj = Json::requireValueObject(item, "Component must be an object."); |
| 198 | container.append(componentFromJsonV1(parent, componentJsonPattern, obj)); |
| 199 | } |
| 200 | } |
| 201 | catch (const JSONValidationError &err) |
| 202 | { |
| 203 | qCritical() << "Couldn't parse" << componentsFile.fileName() << ": bad file format"; |
| 204 | container.clear(); |
| 205 | return false; |
| 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | // END: component file format |
| 211 |
no test coverage detected