* @brief Open project file */
| 1542 | * @brief Open project file |
| 1543 | */ |
| 1544 | API::CommandResponse API::Handlers::ProjectHandler::fileOpen(const QString& id, |
| 1545 | const QJsonObject& params) |
| 1546 | { |
| 1547 | if (!params.contains(QStringLiteral("filePath"))) { |
| 1548 | return CommandResponse::makeError( |
| 1549 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: filePath")); |
| 1550 | } |
| 1551 | |
| 1552 | const QString file_path = params.value(QStringLiteral("filePath")).toString(); |
| 1553 | if (file_path.isEmpty()) { |
| 1554 | return CommandResponse::makeError( |
| 1555 | id, ErrorCode::InvalidParam, QStringLiteral("filePath cannot be empty")); |
| 1556 | } |
| 1557 | |
| 1558 | if (!API::isPathAllowed(file_path)) { |
| 1559 | return CommandResponse::makeError( |
| 1560 | id, ErrorCode::InvalidParam, QStringLiteral("filePath is not allowed")); |
| 1561 | } |
| 1562 | |
| 1563 | const bool isDryRun = params.value(QStringLiteral("dryRun")).toBool(false); |
| 1564 | |
| 1565 | if (isDryRun) { |
| 1566 | QFile f(file_path); |
| 1567 | if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 1568 | return CommandResponse::makeError( |
| 1569 | id, |
| 1570 | ErrorCode::OperationFailed, |
| 1571 | QStringLiteral("DRY RUN: cannot read filePath '%1'").arg(file_path)); |
| 1572 | |
| 1573 | QJsonParseError err{}; |
| 1574 | const auto doc = QJsonDocument::fromJson(f.readAll(), &err); |
| 1575 | f.close(); |
| 1576 | if (err.error != QJsonParseError::NoError || !doc.isObject()) |
| 1577 | return CommandResponse::makeError( |
| 1578 | id, |
| 1579 | ErrorCode::InvalidParam, |
| 1580 | QStringLiteral("DRY RUN: filePath does not contain a valid project JSON object.")); |
| 1581 | |
| 1582 | QJsonObject result; |
| 1583 | result[QStringLiteral("dryRun")] = true; |
| 1584 | result[QStringLiteral("filePath")] = file_path; |
| 1585 | result[QStringLiteral("wouldDiscard")] = summarizeCurrentProject(); |
| 1586 | result[QStringLiteral("wouldApply")] = summarizeProjectJson(doc.object()); |
| 1587 | result[QStringLiteral("warning")] = |
| 1588 | QStringLiteral("DRY RUN: no project was loaded. wouldDiscard shows what would be lost; " |
| 1589 | "wouldApply shows what would replace it. Confirm before re-issuing " |
| 1590 | "without dryRun:true."); |
| 1591 | return CommandResponse::makeSuccess(id, result); |
| 1592 | } |
| 1593 | |
| 1594 | DataModel::ProjectModel::instance().setSuppressMessageBoxes(true); |
| 1595 | const bool ok = DataModel::ProjectModel::instance().openJsonFile(file_path); |
| 1596 | DataModel::ProjectModel::instance().setSuppressMessageBoxes(false); |
| 1597 | |
| 1598 | if (!ok) { |
| 1599 | return CommandResponse::makeError( |
| 1600 | id, |
| 1601 | ErrorCode::OperationFailed, |
nothing calls this directly
no test coverage detected