Skip to content

Commit

Permalink
Display banner with cloud connection result (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
foodprocessor authored Aug 27, 2024
1 parent 9debd6c commit 77bbe56
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
83 changes: 36 additions & 47 deletions src/plugin/settings/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> *values,
const std::vector<std::string> &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<std::string> 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;
}
Expand Down Expand Up @@ -244,24 +243,23 @@ Result<const ISettingsResponse *> 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...";
Expand Down Expand Up @@ -527,15 +525,6 @@ void Engine::doGetSettingsOnActiveSettingChange(Result<const IActiveSettingChang

std::map<std::string, std::string> 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>();
settingsResponse->setValues(makePtr<StringMap>(values));
settingsResponse->setModel(makePtr<String>(Json(model).dump()));
Expand Down
3 changes: 1 addition & 2 deletions src/plugin/settings/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> *values,
const std::vector<std::string> &settingIdsToUpdate = {}) const;
bool updateModel(nx::kit::Json::object *model, bool mountSuccessful) const;

private:
nx::sdk::analytics::Plugin *const m_plugin;
Expand Down
28 changes: 25 additions & 3 deletions src/plugin/settings/settings_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,38 @@ static const std::string kGermanCitiesSettingsModelPart = /*suppress newline*/ 1
static const std::string kRegularSettingsModelPart2 = /*suppress newline*/ 1 + R"json(")json";

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 51 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]
// ------------------------------------------------------------------------------------------------

// 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";
// advanced
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(

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-22.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]

Check warning on line 85 in src/plugin/settings/settings_model.h

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, Release, clang)

adding 'int' to a string does not append to the string [-Wstring-plus-int]
{
"type": "Settings",
"items":
Expand Down

0 comments on commit 77bbe56

Please sign in to comment.