* @brief Reorders user-defined workspaces (id >= 5000) by the given id sequence. */
| 1387 | * @brief Reorders user-defined workspaces (id >= 5000) by the given id sequence. |
| 1388 | */ |
| 1389 | API::CommandResponse API::Handlers::WorkspacesHandler::reorder(const QString& id, |
| 1390 | const QJsonObject& params) |
| 1391 | { |
| 1392 | if (!inProjectFileMode()) |
| 1393 | return CommandResponse::makeError( |
| 1394 | id, ErrorCode::InvalidParam, QStringLiteral("Workspace mutations require ProjectFile mode")); |
| 1395 | |
| 1396 | if (!params.contains(QStringLiteral("workspaceIds"))) |
| 1397 | return CommandResponse::makeError( |
| 1398 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: workspaceIds")); |
| 1399 | |
| 1400 | if (!params.value(QStringLiteral("workspaceIds")).isArray()) |
| 1401 | return CommandResponse::makeError( |
| 1402 | id, ErrorCode::InvalidParam, QStringLiteral("workspaceIds must be an array of integers")); |
| 1403 | |
| 1404 | const auto arr = params.value(QStringLiteral("workspaceIds")).toArray(); |
| 1405 | |
| 1406 | QList<int> orderedIds; |
| 1407 | QStringList rejected; |
| 1408 | for (const auto& v : arr) { |
| 1409 | const int wid = v.toInt(-1); |
| 1410 | if (wid < ::WorkspaceIds::UserStart) { |
| 1411 | rejected.append(QString::number(wid)); |
| 1412 | continue; |
| 1413 | } |
| 1414 | orderedIds.append(wid); |
| 1415 | } |
| 1416 | |
| 1417 | if (!rejected.isEmpty()) |
| 1418 | return CommandResponse::makeError( |
| 1419 | id, |
| 1420 | ErrorCode::InvalidParam, |
| 1421 | QStringLiteral("Only user-defined workspaces (id >= 5000) can be reordered. " |
| 1422 | "Rejected ids: [%1]") |
| 1423 | .arg(rejected.join(QStringLiteral(", ")))); |
| 1424 | |
| 1425 | auto& pm = DataModel::ProjectModel::instance(); |
| 1426 | const auto& wsList = pm.editorWorkspaces(); |
| 1427 | QSet<int> currentUserIds; |
| 1428 | for (const auto& ws : wsList) |
| 1429 | if (ws.workspaceId >= ::WorkspaceIds::UserStart) |
| 1430 | currentUserIds.insert(ws.workspaceId); |
| 1431 | |
| 1432 | const auto orderedSet = QSet<int>(orderedIds.begin(), orderedIds.end()); |
| 1433 | if (orderedSet != currentUserIds) |
| 1434 | return CommandResponse::makeError( |
| 1435 | id, |
| 1436 | ErrorCode::InvalidParam, |
| 1437 | QStringLiteral("workspaceIds must include every user workspace exactly once. " |
| 1438 | "Current user workspaces: %1; got %2.") |
| 1439 | .arg(currentUserIds.size()) |
| 1440 | .arg(orderedIds.size())); |
| 1441 | |
| 1442 | pm.reorderWorkspaces(orderedIds); |
| 1443 | |
| 1444 | QJsonArray newOrder; |
| 1445 | for (const auto& ws : pm.editorWorkspaces()) |
| 1446 | if (ws.workspaceId >= ::WorkspaceIds::UserStart) |
nothing calls this directly
no test coverage detected