Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dialog read attribute: don't create a file #450

Merged
merged 3 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ Deprecated code produces compile-time warnings. These warning serve as
notification to users that their code should be upgraded. The next major
release will remove the deprecated code.

## Ignition GUI 3.10 to 3.11

* `Dialog::ReadConfigAttribute` doesn't create a missing file anymore.

## Ignition GUI 3.6 to 3.7

* The `Application::PluginAdded` signal used to send empty strings. Now it
Expand Down
6 changes: 3 additions & 3 deletions include/ignition/gui/Dialog.hh
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ namespace ignition
const std::string &_path, const std::string &_attribute,
const bool _value) const;

/// \brief Gets a config attribute value, if not found in config
/// write the default in the config and get it.
/// creates config file if it doesn't exist.
/// \brief Gets a config attribute value.
/// It will return an empty string if the config file or the attribute
/// don't exist.
/// \param[in] _path config path
/// \param[in] _attribute attribute name
/// \return attribute value as string
Expand Down
88 changes: 19 additions & 69 deletions src/Dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,83 +138,33 @@ void Dialog::SetDefaultConfig(const std::string &_config)
std::string Dialog::ReadConfigAttribute(const std::string &_path,
const std::string &_attribute) const
{
tinyxml2::XMLDocument doc;
std::string value {""};
std::string config = "<?xml version=\"1.0\"?>\n\n";
tinyxml2::XMLPrinter defaultPrinter;
bool configExists{true};
std::string dialogName = this->objectName().toStdString();

auto Value = [&_attribute, &dialogName](const tinyxml2::XMLDocument &_doc)
{
// Process each dialog
// If multiple attributes share the same name, return the first one
for (auto dialogElem = _doc.FirstChildElement("dialog");
dialogElem != nullptr;
dialogElem = dialogElem->NextSiblingElement("dialog"))
{
if (dialogElem->Attribute("name") == dialogName)
{
if (dialogElem->Attribute(_attribute.c_str()))
return dialogElem->Attribute(_attribute.c_str());
}
}
return "";
};

// Check if the passed in config file exists.
// (If the default config path doesn't exist yet, it's expected behavior.
// It will be created the first time now.)
if (!common::exists(_path))
{
configExists = false;
doc.Parse(this->dataPtr->config.c_str());
value = Value(doc);
return std::string();
}
else
{
auto success = !doc.LoadFile(_path.c_str());
if (!success)
{
ignerr << "Failed to load file [" << _path << "]: XMLError"
<< std::endl;
return "";
}
value = Value(doc);

// config exists but attribute not there read from default config
if (value.empty())
{
tinyxml2::XMLDocument missingDoc;
missingDoc.Parse(this->dataPtr->config.c_str());
value = Value(missingDoc);
missingDoc.Print(&defaultPrinter);
}
}

// Write config file
tinyxml2::XMLPrinter printer;
doc.Print(&printer);

// Don't write the xml version decleration if file exists
if (configExists)
tinyxml2::XMLDocument doc;
auto success = !doc.LoadFile(_path.c_str());
if (!success)
{
config = "";
ignerr << "Failed to load file [" << _path << "]: XMLError"
<< std::endl;
return std::string();
}

igndbg << "Setting dialog " << this->objectName().toStdString()
<< " default config." << std::endl;
config += printer.CStr();
config += defaultPrinter.CStr();
std::ofstream out(_path.c_str(), std::ios::out);
if (!out)
// Process each dialog
// If multiple attributes share the same name, return the first one
std::string dialogName = this->objectName().toStdString();
for (auto dialogElem = doc.FirstChildElement("dialog");
dialogElem != nullptr;
dialogElem = dialogElem->NextSiblingElement("dialog"))
{
ignerr << "Unable to open file: " << _path
<< ".\nCheck file permissions.\n";
return "";
if (dialogElem->Attribute("name") == dialogName &&
dialogElem->Attribute(_attribute.c_str()))
{
return dialogElem->Attribute(_attribute.c_str());
}
}
else
out << config;

return value;
return std::string();
}
93 changes: 48 additions & 45 deletions src/Dialog_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,61 +33,63 @@ char* g_argv[] =

using namespace ignition;
using namespace gui;
using namespace std::chrono_literals;

/////////////////////////////////////////////////
TEST(DialogTest, IGN_UTILS_TEST_DISABLED_ON_WIN32(UpdateDialogConfig))
{
ignition::common::Console::SetVerbosity(4);
Application app(g_argc, g_argv, ignition::gui::WindowType::kDialog);

// Change default config path
App()->SetDefaultConfigPath(kTestConfigFile);
common::Console::SetVerbosity(4);
Application app(g_argc, g_argv, WindowType::kDialog);

auto dialog = new Dialog;
ASSERT_NE(nullptr, dialog);
dialog->setObjectName("quick_menu");

// Start without a file
std::remove(kTestConfigFile.c_str());

// Read attribute value when the default the config is not set
// Read attribute value when the config doesn't exist
{
EXPECT_FALSE(common::exists(kTestConfigFile));
std::string allow = dialog->ReadConfigAttribute(app.DefaultConfigPath(),
std::string allow = dialog->ReadConfigAttribute(kTestConfigFile,
"allow");
EXPECT_EQ(allow, "");

// Config file is created when a read is attempted
EXPECT_TRUE(common::exists(kTestConfigFile));
EXPECT_TRUE(allow.empty());

// Delete file
std::remove(kTestConfigFile.c_str());
// Config file still doesn't exist
EXPECT_FALSE(common::exists(kTestConfigFile));
}

// Read a non existing attribute
{
EXPECT_FALSE(common::exists(kTestConfigFile));
dialog->setObjectName("quick_menu");
dialog->SetDefaultConfig(std::string(
"<dialog name=\"quick_menu\" show=\"true\"/>"));
std::string allow = dialog->ReadConfigAttribute(app.DefaultConfigPath(),
"allow");
EXPECT_EQ(allow, "");

// Config file is created when a read is attempted
// Create file
std::ofstream configFile(kTestConfigFile);
configFile << "<dialog name='quick_menu'/>";
configFile.close();
EXPECT_TRUE(common::exists(kTestConfigFile));

std::string allow = dialog->ReadConfigAttribute(kTestConfigFile,
"allow");
EXPECT_TRUE(allow.empty());

// Delete file
std::remove(kTestConfigFile.c_str());
}

// Read an existing attribute
{
EXPECT_FALSE(common::exists(kTestConfigFile));
std::string show = dialog->ReadConfigAttribute(app.DefaultConfigPath(),
"show");
EXPECT_EQ(show, "true");

// Config file is created when a read is attempted
// Create file
std::ofstream configFile(kTestConfigFile);
configFile << "<dialog name='quick_menu' show='true'/>";
configFile.close();
EXPECT_TRUE(common::exists(kTestConfigFile));

std::string show = dialog->ReadConfigAttribute(kTestConfigFile,
"show");
EXPECT_EQ(show, "true");

// Delete file
std::remove(kTestConfigFile.c_str());
}
Expand All @@ -96,19 +98,18 @@ TEST(DialogTest, IGN_UTILS_TEST_DISABLED_ON_WIN32(UpdateDialogConfig))
{
EXPECT_FALSE(common::exists(kTestConfigFile));

// Call a read to create config file
std::string allow = dialog->ReadConfigAttribute(app.DefaultConfigPath(),
"allow");
// Create file
std::ofstream configFile(kTestConfigFile);
configFile << "<dialog name='quick_menu'/>";
configFile.close();
EXPECT_TRUE(common::exists(kTestConfigFile));

// Empty string for a non existing attribute
EXPECT_EQ(allow, "");
dialog->UpdateConfigAttribute(app.DefaultConfigPath(), "allow", true);
allow = dialog->ReadConfigAttribute(app.DefaultConfigPath(),
"allow");
EXPECT_EQ(allow, "true");
// Update value
dialog->UpdateConfigAttribute(kTestConfigFile, "allow", true);

// Config file is created when a read is attempted
EXPECT_TRUE(common::exists(kTestConfigFile));
// Read value
auto allow = dialog->ReadConfigAttribute(kTestConfigFile, "allow");
EXPECT_EQ(allow, "true");

// Delete file
std::remove(kTestConfigFile.c_str());
Expand All @@ -118,17 +119,19 @@ TEST(DialogTest, IGN_UTILS_TEST_DISABLED_ON_WIN32(UpdateDialogConfig))
{
EXPECT_FALSE(common::exists(kTestConfigFile));

// Call a read to create config file
std::string allow = dialog->ReadConfigAttribute(app.DefaultConfigPath(),
"allow");
dialog->UpdateConfigAttribute(app.DefaultConfigPath(), "allow", false);
allow = dialog->ReadConfigAttribute(app.DefaultConfigPath(),
"allow");
EXPECT_EQ(allow, "false");

// Config file is created when a read is attempted
// Create file
std::ofstream configFile(kTestConfigFile);
configFile << "<dialog name='quick_menu' show='true'/>";
configFile.close();
EXPECT_TRUE(common::exists(kTestConfigFile));

// Update value
dialog->UpdateConfigAttribute(kTestConfigFile, "allow", false);

// Read value
auto allow = dialog->ReadConfigAttribute(kTestConfigFile, "allow");
EXPECT_EQ(allow, "false");

// Delete file
std::remove(kTestConfigFile.c_str());
}
Expand Down