-
Notifications
You must be signed in to change notification settings - Fork 44
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
Add a flexible mechanism to combine user and default plugins #631
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc1e997
Add a flexible mechanism to combine user and default plugins
azeey 4330ef9
Merge remote-tracking branch 'origin/main' into config_loading
azeey c2fa792
Address reviewer feedback
azeey 2ff6043
Merge remote-tracking branch 'origin/gz-gui9' into config_loading
azeey 33f7020
Fix doxygen warnings
azeey 9655930
Mark some member functions as const
azeey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,31 +325,7 @@ bool Application::LoadConfig(const std::string &_config) | |
return false; | ||
} | ||
|
||
std::string configFull = _config; | ||
|
||
// 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 the user presses "Save configuration".) | ||
if (!common::exists(configFull) && (configFull != this->DefaultConfigPath())) | ||
{ | ||
// If not, then check environment variable | ||
std::string configPathEnv; | ||
common::env("GZ_GUI_RESOURCE_PATH", configPathEnv); | ||
|
||
if (!configPathEnv.empty()) | ||
{ | ||
std::vector<std::string> parentPaths = common::Split(configPathEnv, ':'); | ||
for (const auto &parentPath : parentPaths) | ||
{ | ||
std::string tempPath = common::joinPaths(parentPath, configFull); | ||
if (common::exists(tempPath)) | ||
{ | ||
configFull = tempPath; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
const auto configFull = this->ResolveConfigFile(_config); | ||
|
||
// Use tinyxml to read config | ||
tinyxml2::XMLDocument doc; | ||
|
@@ -404,108 +380,114 @@ bool Application::LoadConfig(const std::string &_config) | |
// Process window properties | ||
if (auto *winElem = doc.FirstChildElement("window")) | ||
{ | ||
gzdbg << "Loading window config" << std::endl; | ||
this->LoadWindowConfig(*winElem); | ||
} | ||
|
||
this->ApplyConfig(); | ||
|
||
return true; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
bool Application::LoadWindowConfig(const tinyxml2::XMLElement &_winElem) | ||
{ | ||
gzdbg << "Loading window config" << std::endl; | ||
|
||
tinyxml2::XMLPrinter printer; | ||
if (!_winElem.Accept(&printer)) | ||
{ | ||
gzwarn << "There was an error parsing the <window> element" | ||
<< std::endl; | ||
return false; | ||
} | ||
this->dataPtr->windowConfig.MergeFromXML(std::string(printer.CStr())); | ||
|
||
tinyxml2::XMLPrinter printer; | ||
if (!winElem->Accept(&printer)) | ||
// Closing behavior. | ||
if (auto *defaultExitActionElem = | ||
_winElem.FirstChildElement("default_exit_action")) | ||
{ | ||
ExitAction action{ExitAction::CLOSE_GUI}; | ||
const auto value = common::lowercase(defaultExitActionElem->GetText()); | ||
if (value == "shutdown_server") | ||
{ | ||
gzwarn << "There was an error parsing the <window> element" | ||
<< std::endl; | ||
return false; | ||
action = ExitAction::SHUTDOWN_SERVER; | ||
} | ||
this->dataPtr->windowConfig.MergeFromXML(std::string(printer.CStr())); | ||
|
||
// Closing behavior. | ||
if (auto *defaultExitActionElem = | ||
winElem->FirstChildElement("default_exit_action")) | ||
else if (value != "close_gui" && !value.empty()) | ||
{ | ||
ExitAction action{ExitAction::CLOSE_GUI}; | ||
const auto value = common::lowercase(defaultExitActionElem->GetText()); | ||
if (value == "shutdown_server") | ||
{ | ||
action = ExitAction::SHUTDOWN_SERVER; | ||
} | ||
else if (value != "close_gui" && !value.empty()) | ||
{ | ||
gzwarn << "Value '" << value << "' of <default_exit_action> is " | ||
<< "invalid. Allowed values are CLOSE_GUI and SHUTDOWN_SERVER. " | ||
<< "Selecting CLOSE_GUI as fallback." << std::endl; | ||
} | ||
this->dataPtr->mainWin->SetDefaultExitAction(action); | ||
gzwarn << "Value '" << value << "' of <default_exit_action> is " | ||
<< "invalid. Allowed values are CLOSE_GUI and SHUTDOWN_SERVER. " | ||
<< "Selecting CLOSE_GUI as fallback." << std::endl; | ||
} | ||
this->dataPtr->mainWin->SetDefaultExitAction(action); | ||
} | ||
|
||
// Dialog on exit | ||
if (auto *dialogOnExitElem = _winElem.FirstChildElement("dialog_on_exit")) | ||
{ | ||
bool showDialogOnExit{false}; | ||
dialogOnExitElem->QueryBoolText(&showDialogOnExit); | ||
this->dataPtr->mainWin->SetShowDialogOnExit(showDialogOnExit); | ||
} | ||
|
||
// Dialog on exit | ||
if (auto *dialogOnExitElem = winElem->FirstChildElement("dialog_on_exit")) | ||
if (auto *dialogOnExitOptionsElem = | ||
_winElem.FirstChildElement("dialog_on_exit_options")) | ||
{ | ||
if (auto *promptElem = | ||
dialogOnExitOptionsElem->FirstChildElement("prompt_text")) | ||
{ | ||
bool showDialogOnExit{false}; | ||
dialogOnExitElem->QueryBoolText(&showDialogOnExit); | ||
this->dataPtr->mainWin->SetShowDialogOnExit(showDialogOnExit); | ||
this->dataPtr->mainWin->SetDialogOnExitText( | ||
QString::fromStdString(promptElem->GetText())); | ||
} | ||
|
||
if (auto *dialogOnExitOptionsElem = | ||
winElem->FirstChildElement("dialog_on_exit_options")) | ||
if (auto *showShutdownElem = | ||
dialogOnExitOptionsElem->FirstChildElement("show_shutdown_button")) | ||
{ | ||
if (auto *promptElem = | ||
dialogOnExitOptionsElem->FirstChildElement("prompt_text")) | ||
{ | ||
this->dataPtr->mainWin->SetDialogOnExitText( | ||
QString::fromStdString(promptElem->GetText())); | ||
} | ||
if (auto *showShutdownElem = | ||
dialogOnExitOptionsElem->FirstChildElement("show_shutdown_button")) | ||
{ | ||
bool showShutdownButton{false}; | ||
showShutdownElem->QueryBoolText(&showShutdownButton); | ||
this->dataPtr->mainWin->SetExitDialogShowShutdown(showShutdownButton); | ||
} | ||
if (auto *showCloseGuiElem = | ||
dialogOnExitOptionsElem->FirstChildElement("show_close_gui_button")) | ||
{ | ||
bool showCloseGuiButton{false}; | ||
showCloseGuiElem->QueryBoolText(&showCloseGuiButton); | ||
this->dataPtr->mainWin->SetExitDialogShowCloseGui(showCloseGuiButton); | ||
} | ||
if (auto *shutdownTextElem = | ||
dialogOnExitOptionsElem->FirstChildElement("shutdown_button_text")) | ||
{ | ||
this->dataPtr->mainWin->SetExitDialogShutdownText( | ||
QString::fromStdString(shutdownTextElem->GetText())); | ||
} | ||
if (auto *closeGuiTextElem = | ||
dialogOnExitOptionsElem->FirstChildElement("close_gui_button_text")) | ||
{ | ||
this->dataPtr->mainWin->SetExitDialogCloseGuiText( | ||
QString::fromStdString(closeGuiTextElem->GetText())); | ||
} | ||
bool showShutdownButton{false}; | ||
showShutdownElem->QueryBoolText(&showShutdownButton); | ||
this->dataPtr->mainWin->SetExitDialogShowShutdown(showShutdownButton); | ||
} | ||
|
||
// Server control service topic | ||
std::string serverControlService{"/server_control"}; | ||
auto *serverControlElem = | ||
winElem->FirstChildElement("server_control_service"); | ||
if (nullptr != serverControlElem && nullptr != serverControlElem->GetText()) | ||
if (auto *showCloseGuiElem = | ||
dialogOnExitOptionsElem->FirstChildElement("show_close_gui_button")) | ||
{ | ||
serverControlService = transport::TopicUtils::AsValidTopic( | ||
serverControlElem->GetText()); | ||
bool showCloseGuiButton{false}; | ||
showCloseGuiElem->QueryBoolText(&showCloseGuiButton); | ||
this->dataPtr->mainWin->SetExitDialogShowCloseGui(showCloseGuiButton); | ||
} | ||
|
||
if (serverControlService.empty()) | ||
if (auto *shutdownTextElem = | ||
dialogOnExitOptionsElem->FirstChildElement("shutdown_button_text")) | ||
{ | ||
gzerr << "Failed to create valid server control service" << std::endl; | ||
this->dataPtr->mainWin->SetExitDialogShutdownText( | ||
QString::fromStdString(shutdownTextElem->GetText())); | ||
} | ||
else | ||
if (auto *closeGuiTextElem = | ||
dialogOnExitOptionsElem->FirstChildElement("close_gui_button_text")) | ||
{ | ||
gzmsg << "Using server control service [" << serverControlService | ||
<< "]" << std::endl; | ||
this->dataPtr->mainWin->SetServerControlService(serverControlService); | ||
this->dataPtr->mainWin->SetExitDialogCloseGuiText( | ||
QString::fromStdString(closeGuiTextElem->GetText())); | ||
} | ||
} | ||
|
||
this->ApplyConfig(); | ||
// Server control service topic | ||
std::string serverControlService{"/server_control"}; | ||
auto *serverControlElem = | ||
_winElem.FirstChildElement("server_control_service"); | ||
if (nullptr != serverControlElem && nullptr != serverControlElem->GetText()) | ||
{ | ||
serverControlService = transport::TopicUtils::AsValidTopic( | ||
serverControlElem->GetText()); | ||
} | ||
|
||
if (serverControlService.empty()) | ||
{ | ||
gzerr << "Failed to create valid server control service" << std::endl; | ||
} | ||
else | ||
{ | ||
gzmsg << "Using server control service [" << serverControlService | ||
<< "]" << std::endl; | ||
this->dataPtr->mainWin->SetServerControlService(serverControlService); | ||
} | ||
return true; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
bool Application::LoadDefaultConfig() | ||
{ | ||
|
@@ -524,6 +506,37 @@ std::string Application::DefaultConfigPath() | |
return this->dataPtr->defaultConfigPath; | ||
} | ||
|
||
std::string Application::ResolveConfigFile(const std::string &_path) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add separation line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in c2fa792 |
||
{ | ||
std::string configFull = _path; | ||
|
||
// 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 the user presses "Save configuration".) | ||
if (!common::exists(configFull) && (configFull != this->DefaultConfigPath())) | ||
{ | ||
// If not, then check environment variable | ||
std::string configPathEnv; | ||
common::env("GZ_GUI_RESOURCE_PATH", configPathEnv); | ||
|
||
if (!configPathEnv.empty()) | ||
{ | ||
std::vector<std::string> parentPaths = common::Split(configPathEnv, ':'); | ||
for (const auto &parentPath : parentPaths) | ||
{ | ||
std::string tempPath = common::joinPaths(parentPath, configFull); | ||
if (common::exists(tempPath)) | ||
{ | ||
configFull = tempPath; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return configFull; | ||
} | ||
|
||
///////////////////////////////////////////////// | ||
bool Application::LoadPlugin(const std::string &_filename, | ||
const tinyxml2::XMLElement *_pluginElem) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add doxygen comments for the new functions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in c2fa792