fix bug where config file wasn't saved when clicking 'Active'; add more logging to config file search; make unchecking active delete the user pref

This commit is contained in:
Christopher A. Mosher 2022-12-11 15:27:17 -05:00
parent c04ab77f43
commit 6ec1aa5974
2 changed files with 38 additions and 16 deletions

View File

@ -14,8 +14,7 @@
#include <wx/filename.h> #include <wx/filename.h>
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#include <boost/date_time/posix_time/posix_time.hpp> #include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream> #include <filesystem>
#include <ostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
@ -226,20 +225,30 @@ const std::filesystem::path BuildNewConfFilePath() {
} }
void PreferencesDialog::OnActive(wxCommandEvent& evt) { void PreferencesDialog::OnActive(wxCommandEvent& evt) {
if (evt.IsChecked()) {
CTRL(wxTreeCtrl, treItems); CTRL(wxTreeCtrl, treItems);
const TreeItemData *data = (TreeItemData*)treItems->GetItemData(treItems->GetSelection()); const TreeItemData *data = (TreeItemData*)treItems->GetItemData(treItems->GetSelection());
if (data->isFile()) { if (!data->isFile()) {
return;
}
if (data->isEditable()) {
Save(data->path());
}
const std::filesystem::path p = data->path(); const std::filesystem::path p = data->path();
if (evt.IsChecked()) {
wxString name = wxFileName::FileName(p.c_str()).GetName(); wxString name = wxFileName::FileName(p.c_str()).GetName();
this->active = name; this->active = name;
std::cout << "setting current active config file to: " << this->active << std::endl; BOOST_LOG_TRIVIAL(info) << "setting current active config file to: " << this->active;
wxConfigBase::Get()->Write("/ActivePreferences/name", this->active); wxConfigBase::Get()->Write("/ActivePreferences/name", this->active);
BuildItemTree(); // invalidates "data" pointer variable BuildItemTree(); // invalidates "data" pointer variable
PreSelectUserConfigItemName(p); PreSelectUserConfigItemName(p);
}
} else { } else {
// TODO what if they uncheck the active checkbox? this->active = "";
BOOST_LOG_TRIVIAL(info) << "removing active config file from settings";
wxConfigBase::Get()->DeleteEntry("/ActivePreferences/name");
BuildItemTree(); // invalidates "data" pointer variable
PreSelectUserConfigItemName(p);
} }
} }
@ -298,6 +307,7 @@ void PreferencesDialog::OnRename(wxCommandEvent& evt) {
wxFileName fn(data->path().c_str()); wxFileName fn(data->path().c_str());
fn.SetName(newname); fn.SetName(newname);
// TODO should we check for existence of name in built-in (to prevent override)? // TODO should we check for existence of name in built-in (to prevent override)?
// TODO handle case when renaming the currently active file
if (fn.Exists()) { if (fn.Exists()) {
wxMessageBox("That name is already being used.", "File exists", wxOK|wxCENTER, this); wxMessageBox("That name is already being used.", "File exists", wxOK|wxCENTER, this);
} else { } else {

View File

@ -123,12 +123,12 @@ std::ifstream *Config::openFilePref(const wxString& s_name) {
BOOST_LOG_TRIVIAL(info) << "config file name was specified as: " << s_name; BOOST_LOG_TRIVIAL(info) << "config file name was specified as: " << s_name;
std::filesystem::path path_name = path_from_string(s_name); std::filesystem::path path_name = path_from_string(s_name);
if (path_name.has_parent_path()) { if (path_name.has_parent_path()) {
BOOST_LOG_TRIVIAL(error) << "invalid name for config file (paths are not allowed): " << path_name.c_str(); BOOST_LOG_TRIVIAL(warning) << "invalid name for config file (paths are not allowed): " << path_name.c_str();
return ret; return ret;
} }
if (path_name.empty()) { if (path_name.empty()) {
BOOST_LOG_TRIVIAL(error) << "invalid: empty name for config file"; BOOST_LOG_TRIVIAL(warning) << "invalid: empty name for config file";
return ret; return ret;
} }
@ -191,11 +191,13 @@ static const std::array rs_path_legacy{
std::ifstream *Config::openFileLegacy() { std::ifstream *Config::openFileLegacy() {
std::ifstream *ret = nullptr; std::ifstream *ret = nullptr;
BOOST_LOG_TRIVIAL(warning) << "Searching for config file in legacy locations...";
for (const auto &s_path_legacy : rs_path_legacy) { for (const auto &s_path_legacy : rs_path_legacy) {
if ((ret = openFileExternal(std::filesystem::path{s_path_legacy})) != nullptr) { if ((ret = openFileExternal(std::filesystem::path{s_path_legacy})) != nullptr) {
return ret; return ret;
} }
} }
BOOST_LOG_TRIVIAL(warning) << "Could not file config file in any legacy locations.";
return ret; return ret;
} }
@ -209,6 +211,8 @@ std::ifstream *Config::openFile() {
std::ifstream *ret = nullptr; std::ifstream *ret = nullptr;
if (this->file_path.empty()) { if (this->file_path.empty()) {
BOOST_LOG_TRIVIAL(info) << "No config file specified on command line.";
wxString cname{}; wxString cname{};
const bool stored_prefs_found = wxConfigBase::Get()->Read("/ActivePreferences/name", &cname, DEFAULT_CONFIG_NAME); const bool stored_prefs_found = wxConfigBase::Get()->Read("/ActivePreferences/name", &cname, DEFAULT_CONFIG_NAME);
@ -217,6 +221,7 @@ std::ifstream *Config::openFile() {
if (ret != nullptr) { if (ret != nullptr) {
return ret; return ret;
} }
BOOST_LOG_TRIVIAL(warning) << "Could not find the previously chosen active config file: " << cname.wc_str();
if (!this->prefs_only) { if (!this->prefs_only) {
ret = openFileLegacy(); ret = openFileLegacy();
@ -226,6 +231,7 @@ std::ifstream *Config::openFile() {
} }
} else { } else {
if (!this->prefs_only) { if (!this->prefs_only) {
BOOST_LOG_TRIVIAL(warning) << "User has not specified any active config file.";
ret = openFileLegacy(); ret = openFileLegacy();
if (ret != nullptr) { if (ret != nullptr) {
return ret; return ret;
@ -237,18 +243,24 @@ std::ifstream *Config::openFile() {
} }
} }
} else { } else {
BOOST_LOG_TRIVIAL(info) << "Config file specified on command line as: " << this->file_path.c_str();
if (!this->prefs_only) { if (!this->prefs_only) {
BOOST_LOG_TRIVIAL(info) << "Searching for external configuration file.";
ret = openFileExternal(this->file_path); ret = openFileExternal(this->file_path);
if (ret != nullptr) { if (ret != nullptr) {
return ret; return ret;
} }
} }
ret = openFilePref(this->file_path.c_str()); ret = openFilePref(this->file_path.c_str());
if (ret != nullptr) { if (ret != nullptr) {
return ret; return ret;
} }
} }
BOOST_LOG_TRIVIAL(warning) << "Could not find any config file.";
return ret; return ret;
} }