From 6ec1aa597402e2cef32610820fd3f4d6da06b8c2 Mon Sep 17 00:00:00 2001 From: "Christopher A. Mosher" Date: Sun, 11 Dec 2022 15:27:17 -0500 Subject: [PATCH] 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 --- src/PreferencesDialog.cpp | 38 ++++++++++++++++++++++++-------------- src/configep2.cpp | 16 ++++++++++++++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index 966f85b..ed0db28 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -14,8 +14,7 @@ #include #include #include -#include -#include +#include #include #include @@ -226,20 +225,30 @@ const std::filesystem::path BuildNewConfFilePath() { } void PreferencesDialog::OnActive(wxCommandEvent& evt) { + CTRL(wxTreeCtrl, treItems); + const TreeItemData *data = (TreeItemData*)treItems->GetItemData(treItems->GetSelection()); + if (!data->isFile()) { + return; + } + + if (data->isEditable()) { + Save(data->path()); + } + + const std::filesystem::path p = data->path(); if (evt.IsChecked()) { - CTRL(wxTreeCtrl, treItems); - const TreeItemData *data = (TreeItemData*)treItems->GetItemData(treItems->GetSelection()); - if (data->isFile()) { - const std::filesystem::path p = data->path(); - wxString name = wxFileName::FileName(p.c_str()).GetName(); - this->active = name; - std::cout << "setting current active config file to: " << this->active << std::endl; - wxConfigBase::Get()->Write("/ActivePreferences/name", this->active); - BuildItemTree(); // invalidates "data" pointer variable - PreSelectUserConfigItemName(p); - } + wxString name = wxFileName::FileName(p.c_str()).GetName(); + this->active = name; + BOOST_LOG_TRIVIAL(info) << "setting current active config file to: " << this->active; + wxConfigBase::Get()->Write("/ActivePreferences/name", this->active); + BuildItemTree(); // invalidates "data" pointer variable + PreSelectUserConfigItemName(p); } 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()); fn.SetName(newname); // 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()) { wxMessageBox("That name is already being used.", "File exists", wxOK|wxCENTER, this); } else { diff --git a/src/configep2.cpp b/src/configep2.cpp index d5dfc31..d446085 100644 --- a/src/configep2.cpp +++ b/src/configep2.cpp @@ -123,12 +123,12 @@ std::ifstream *Config::openFilePref(const wxString& s_name) { BOOST_LOG_TRIVIAL(info) << "config file name was specified as: " << s_name; std::filesystem::path path_name = path_from_string(s_name); 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; } 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; } @@ -191,11 +191,13 @@ static const std::array rs_path_legacy{ std::ifstream *Config::openFileLegacy() { std::ifstream *ret = nullptr; + BOOST_LOG_TRIVIAL(warning) << "Searching for config file in legacy locations..."; for (const auto &s_path_legacy : rs_path_legacy) { if ((ret = openFileExternal(std::filesystem::path{s_path_legacy})) != nullptr) { return ret; } } + BOOST_LOG_TRIVIAL(warning) << "Could not file config file in any legacy locations."; return ret; } @@ -209,6 +211,8 @@ std::ifstream *Config::openFile() { std::ifstream *ret = nullptr; if (this->file_path.empty()) { + BOOST_LOG_TRIVIAL(info) << "No config file specified on command line."; + wxString cname{}; 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) { return ret; } + BOOST_LOG_TRIVIAL(warning) << "Could not find the previously chosen active config file: " << cname.wc_str(); if (!this->prefs_only) { ret = openFileLegacy(); @@ -226,6 +231,7 @@ std::ifstream *Config::openFile() { } } else { if (!this->prefs_only) { + BOOST_LOG_TRIVIAL(warning) << "User has not specified any active config file."; ret = openFileLegacy(); if (ret != nullptr) { return ret; @@ -237,18 +243,24 @@ std::ifstream *Config::openFile() { } } } else { + BOOST_LOG_TRIVIAL(info) << "Config file specified on command line as: " << this->file_path.c_str(); + if (!this->prefs_only) { + BOOST_LOG_TRIVIAL(info) << "Searching for external configuration file."; ret = openFileExternal(this->file_path); if (ret != nullptr) { return ret; } } + ret = openFilePref(this->file_path.c_str()); if (ret != nullptr) { return ret; } } + BOOST_LOG_TRIVIAL(warning) << "Could not find any config file."; + return ret; }