diff --git a/src/plugin/settings/engine.cpp b/src/plugin/settings/engine.cpp index f3212d9..0a4ca41 100644 --- a/src/plugin/settings/engine.cpp +++ b/src/plugin/settings/engine.cpp @@ -136,42 +136,41 @@ void enableLogging(std::string iniDir) NX_PRINT << "cloudfuse Engine::enableLogging - plugin stderr logging file: " + stderrFilename; } -bool Engine::processActiveSettings(Json::object *model, std::map *values, - const std::vector &settingIdsToUpdate) const +bool Engine::updateModel(Json::object *model, bool mountSuccessful) const { - NX_PRINT << "cloudfuse Engine::processActiveSettings"; - Json::array items = (*model)[kItems].array_items(); + NX_PRINT << "cloudfuse Engine::updateModel"; - auto activeSettingsGroupBoxIt = std::find_if(items.begin(), items.end(), [](Json &item) { - return item[kCaption].string_value() == kAdvancedSettingsGroupBoxCaption; - }); - - if (activeSettingsGroupBoxIt == items.cend()) + // prepare the new status item + auto statusJson = mountSuccessful ? kStatusSuccess : kStatusFailure; + std::string error; + auto newStatus = Json::parse(statusJson, error); + if (error != "") + { + NX_PRINT << "Failed to parse status JSON with error: " << error; return false; + } - Json activeSettingsItems = (*activeSettingsGroupBoxIt)[kItems]; - - std::vector activeSettingNames = settingIdsToUpdate; - if (activeSettingNames.empty()) + // find where to put it + auto items = (*model)[kItems]; + auto itemsArray = items.array_items(); + // find the status banner, if it's already present + auto statusBannerIt = std::find_if(itemsArray.begin(), itemsArray.end(), + [](Json &item) { return item[kName].string_value() == kStatusBannerId; }); + // if the banner is not there, add it + if (statusBannerIt == itemsArray.end()) { - for (const auto &item : activeSettingsItems.array_items()) - { - if (item["type"].string_value() == "Button") - continue; - - std::string name = item[kName].string_value(); - activeSettingNames.push_back(name); - } + // add the status banner to the beginning of the list of items + itemsArray.insert(itemsArray.begin(), newStatus); + } + else + { + // update the status + *statusBannerIt = newStatus; } - for (const auto &settingId : activeSettingNames) - m_activeSettingsBuilder.updateSettings(settingId, &activeSettingsItems, values); - - Json::array updatedActiveSettingsItems = activeSettingsItems.array_items(); - Json::object updatedActiveGroupBox = activeSettingsGroupBoxIt->object_items(); - updatedActiveGroupBox[kItems] = updatedActiveSettingsItems; - *activeSettingsGroupBoxIt = updatedActiveGroupBox; - (*model)[kItems] = items; + // write the updated array back into the model Json + // TODO: why do we have to do this? + (*model)[kItems] = itemsArray; return true; } @@ -244,24 +243,23 @@ Result Engine::settingsReceived() if (mountRequired) { NX_PRINT << "Settings changed."; - if (!mount()) + bool mountSuccessful = mount(); + if (!mountSuccessful) { NX_PRINT << "Mount failed."; } + // update the model so user can see mount status + if (!updateModel(&model, mountSuccessful)) + { + // on failure, no changes will be written to the model + NX_PRINT << "Status message update failed!"; + } } else { NX_PRINT << "Settings have not changed."; } - // TODO: use this to update the model when validation succeeds (or fails) - // if (!processActiveSettings(&model, &values)) - // { - // std::string errorMessage = "Unable to process the active settings section"; - // NX_PRINT << errorMessage; - // return error(ErrorCode::internalError, errorMessage); - // } - // returning invalid JSON to the VMS will crash the server // validate JSON before sending. NX_PRINT << "Returning settingsResponse..."; @@ -527,15 +525,6 @@ void Engine::doGetSettingsOnActiveSettingChange(Result values = toStdMap(shareToPtr(activeSettingChangedAction->settingsValues())); - if (!processActiveSettings(&model, &values, {settingId})) - { - std::string errorMessage = "Unable to process the active settings section"; - NX_PRINT << errorMessage; - *outResult = error(ErrorCode::internalError, errorMessage); - - return; - } - const auto settingsResponse = makePtr(); settingsResponse->setValues(makePtr(values)); settingsResponse->setModel(makePtr(Json(model).dump())); diff --git a/src/plugin/settings/engine.h b/src/plugin/settings/engine.h index 2562247..6d1b1a0 100644 --- a/src/plugin/settings/engine.h +++ b/src/plugin/settings/engine.h @@ -44,8 +44,7 @@ class Engine : public nx::sdk::analytics::Engine const nx::sdk::IActiveSettingChangedAction *activeSettingChangedAction) override; private: - bool processActiveSettings(nx::kit::Json::object *model, std::map *values, - const std::vector &settingIdsToUpdate = {}) const; + bool updateModel(nx::kit::Json::object *model, bool mountSuccessful) const; private: nx::sdk::analytics::Plugin *const m_plugin; diff --git a/src/plugin/settings/settings_model.h b/src/plugin/settings/settings_model.h index 17fe346..4595272 100644 --- a/src/plugin/settings/settings_model.h +++ b/src/plugin/settings/settings_model.h @@ -51,6 +51,29 @@ static const std::string kGermanCitiesSettingsModelPart = /*suppress newline*/ 1 static const std::string kRegularSettingsModelPart2 = /*suppress newline*/ 1 + R"json(")json"; // ------------------------------------------------------------------------------------------------ +// status +static const std::string kStatusBannerId = "connectionStatus"; +static const std::string kStatusSuccess = R"json( + { + "type": "Banner", + "name": ")json" + kStatusBannerId + + R"json(", + "icon": "info", + "text": "Cloud storage connected successfully!" + } +)json"; + +static const std::string kStatusFailure = R"json( + { + "type": "Banner", + "name": ")json" + kStatusBannerId + + R"json(", + "icon": "warning", + "text": "Cloud storage connection failed!" + } +)json"; + +// credentials static const std::string kKeyIdTextFieldId = "keyId"; static const std::string kSecretKeyPasswordFieldId = "secretKey"; static const std::string kCheckCredentialsButtonId = "checkCredentialsButton"; @@ -58,9 +81,8 @@ static const std::string kCheckCredentialsButtonId = "checkCredentialsButton"; static const std::string kEndpointUrlTextFieldId = "endpointUrl"; static const std::string kDefaultEndpoint = "https://s3.us-east-1.lyvecloud.seagate.com"; static const std::string kBucketNameTextFieldId = "bucketName"; - -// ------------------------------------------------------------------------------------------------ -static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json(" +// top-level settings model +static const std::string kEngineSettingsModel = /*suppress newline*/ 1 + R"json( { "type": "Settings", "items":