Skip to content

Commit

Permalink
Add xml validation when saving file
Browse files Browse the repository at this point in the history
Signed-off-by: romanodanilo <[email protected]>
  • Loading branch information
romanodanilo committed May 30, 2024
1 parent 2fe522c commit 67eb344
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/runtime/src/ui/c_runtime_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,27 @@ const QString cRuntimeWindow::GetWorkingDir()
return QString::fromStdString(fs::current_path().string());
}


bool cRuntimeWindow::ValidateAndWrite(cConfiguration& configuration, const std::string& filePath){
std::string message = "";
if (!cConfigurationValidator::ValidateConfiguration(&configuration,
message)) {
std::stringstream ssDetails;

ssDetails << "Configuration is invalid." << std::endl << std::endl;
ssDetails << "Message: " << std::endl;
ssDetails << message;
ssDetails << std::endl << std::endl;
ssDetails << "Please fix it before saving configuration file";

QMessageBox::warning(this, tr("Error"), tr(ssDetails.str().c_str()),
QMessageBox::Ok);
return false;
}
configuration.WriteConfigurationToFile(filePath);
return true;
}

void cRuntimeWindow::OpenConfigurationFile()
{
QString filePath =
Expand Down Expand Up @@ -191,8 +212,11 @@ bool cRuntimeWindow::SaveConfigurationFile()
if (reply == QMessageBox::Yes)
{
UpdateConfiguration();
_currentConfiguration.WriteConfigurationToFile(_currentConfigurationPath.toLocal8Bit().data());

bool validation_result = false;
validation_result = ValidateAndWrite(_currentConfiguration, _currentConfigurationPath.toLocal8Bit().data());
if (!validation_result){
return false;
}
_configurationChanged = false;
SetupWindowTitle();
return true;
Expand All @@ -211,6 +235,7 @@ bool cRuntimeWindow::SaveConfigurationFile()
return SaveAsConfigurationFile();
}


bool cRuntimeWindow::SaveAsConfigurationFile()
{
QString filePath =
Expand All @@ -236,7 +261,11 @@ bool cRuntimeWindow::SaveAsConfigurationFile()
UpdateConfiguration();

_currentConfigurationPath = filePath;
_currentConfiguration.WriteConfigurationToFile(filePath.toLocal8Bit().data());
bool validation_result = false;
validation_result = ValidateAndWrite(_currentConfiguration, filePath.toLocal8Bit().data());
if (!validation_result){
return false;
}

_configurationChanged = false;
SetupWindowTitle();
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/src/ui/c_runtime_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class cRuntimeWindow : public QMainWindow
// SaveAs configuration file
bool SaveAsConfigurationFile();

bool ValidateAndWrite(cConfiguration& , const std::string&);

// Save configuration file
bool SaveConfigurationFile();

Expand Down

0 comments on commit 67eb344

Please sign in to comment.