* @brief Detaches a widget ref matching either {widgetId} or the legacy tuple. */
| 1164 | * @brief Detaches a widget ref matching either {widgetId} or the legacy tuple. |
| 1165 | */ |
| 1166 | API::CommandResponse API::Handlers::WorkspacesHandler::widgetRemove(const QString& id, |
| 1167 | const QJsonObject& params) |
| 1168 | { |
| 1169 | if (!inProjectFileMode()) |
| 1170 | return CommandResponse::makeError( |
| 1171 | id, ErrorCode::InvalidParam, QStringLiteral("Workspace mutations require ProjectFile mode")); |
| 1172 | |
| 1173 | auto& pm = DataModel::ProjectModel::instance(); |
| 1174 | if (!pm.customizeWorkspaces()) |
| 1175 | return CommandResponse::makeError( |
| 1176 | id, |
| 1177 | ErrorCode::InvalidParam, |
| 1178 | QStringLiteral("customizeWorkspaces is off; call project.workspace.setCustomizeMode first")); |
| 1179 | |
| 1180 | int wid = -1; |
| 1181 | int wtype = -1; |
| 1182 | int gid = -1; |
| 1183 | int relIndex = -1; |
| 1184 | |
| 1185 | if (params.contains(QStringLiteral("widgetId"))) { |
| 1186 | const auto parsed = parseWidgetId(params.value(QStringLiteral("widgetId")).toString()); |
| 1187 | if (!parsed.valid) |
| 1188 | return CommandResponse::makeError(id, |
| 1189 | ErrorCode::InvalidParam, |
| 1190 | QStringLiteral("Malformed widgetId. Expected " |
| 1191 | "'ws<workspaceId>:<slug>:g<groupId>:" |
| 1192 | "<relativeIndex>'.")); |
| 1193 | |
| 1194 | wid = parsed.workspaceId; |
| 1195 | wtype = parsed.widgetType; |
| 1196 | gid = parsed.groupId; |
| 1197 | relIndex = parsed.relativeIndex; |
| 1198 | } else { |
| 1199 | const QStringList required{ |
| 1200 | QStringLiteral("workspaceId"), |
| 1201 | QStringLiteral("widgetType"), |
| 1202 | QStringLiteral("groupId"), |
| 1203 | QStringLiteral("relativeIndex"), |
| 1204 | }; |
| 1205 | |
| 1206 | for (const auto& key : required) |
| 1207 | if (!params.contains(key)) |
| 1208 | return CommandResponse::makeError( |
| 1209 | id, |
| 1210 | ErrorCode::MissingParam, |
| 1211 | QStringLiteral("Missing parameter: %1 (or provide widgetId)").arg(key)); |
| 1212 | |
| 1213 | wid = params.value(QStringLiteral("workspaceId")).toInt(); |
| 1214 | gid = params.value(QStringLiteral("groupId")).toInt(); |
| 1215 | relIndex = params.value(QStringLiteral("relativeIndex")).toInt(); |
| 1216 | |
| 1217 | const QJsonValue wtypeJson = params.value(QStringLiteral("widgetType")); |
| 1218 | if (wtypeJson.isString()) { |
| 1219 | wtype = API::EnumLabels::dashboardWidgetFromSlug(wtypeJson.toString()); |
| 1220 | if (wtype < 0) |
| 1221 | return CommandResponse::makeError( |
| 1222 | id, |
| 1223 | ErrorCode::InvalidParam, |
nothing calls this directly
no test coverage detected