* @brief Deletes a workspace. No-op if id not found. */
| 798 | * @brief Deletes a workspace. No-op if id not found. |
| 799 | */ |
| 800 | API::CommandResponse API::Handlers::WorkspacesHandler::remove(const QString& id, |
| 801 | const QJsonObject& params) |
| 802 | { |
| 803 | if (!inProjectFileMode()) |
| 804 | return CommandResponse::makeError( |
| 805 | id, ErrorCode::InvalidParam, QStringLiteral("Workspace mutations require ProjectFile mode")); |
| 806 | |
| 807 | if (!params.contains(QStringLiteral("id"))) |
| 808 | return CommandResponse::makeError( |
| 809 | id, ErrorCode::MissingParam, QStringLiteral("Missing required parameter: id")); |
| 810 | |
| 811 | const bool isDryRun = params.value(QStringLiteral("dryRun")).toBool(false); |
| 812 | |
| 813 | auto& pm = DataModel::ProjectModel::instance(); |
| 814 | if (!pm.customizeWorkspaces()) |
| 815 | return CommandResponse::makeError( |
| 816 | id, |
| 817 | ErrorCode::InvalidParam, |
| 818 | QStringLiteral("customizeWorkspaces is off; call project.workspaces.customize.set first")); |
| 819 | |
| 820 | const int wid = params.value(QStringLiteral("id")).toInt(); |
| 821 | |
| 822 | const auto& workspaces = pm.activeWorkspaces(); |
| 823 | const auto it = findWorkspace(workspaces, wid); |
| 824 | if (it == workspaces.end()) |
| 825 | return CommandResponse::makeError( |
| 826 | id, ErrorCode::InvalidParam, QStringLiteral("Workspace not found: %1").arg(wid)); |
| 827 | |
| 828 | QJsonObject deleted; |
| 829 | deleted[QStringLiteral("id")] = it->workspaceId; |
| 830 | deleted[QStringLiteral("title")] = it->title; |
| 831 | deleted[QStringLiteral("widgetCount")] = static_cast<int>(it->widgetRefs.size()); |
| 832 | |
| 833 | if (!isDryRun) |
| 834 | pm.deleteWorkspace(wid); |
| 835 | |
| 836 | QJsonObject result; |
| 837 | if (isDryRun) { |
| 838 | result[QStringLiteral("dryRun")] = true; |
| 839 | result[QStringLiteral("warning")] = |
| 840 | QStringLiteral("DRY RUN: workspace not deleted. Re-call without dryRun:true to commit."); |
| 841 | } |
| 842 | |
| 843 | result[QStringLiteral("id")] = wid; |
| 844 | result[QStringLiteral("deleted")] = !isDryRun; |
| 845 | result[QStringLiteral("workspace")] = deleted; |
| 846 | return CommandResponse::makeSuccess(id, result); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * @brief Wipes every workspace in one shot. Leaves customize mode on with an empty list. |
no test coverage detected