mirror of
https://github.com/ciphervance/supercell-wx.git
synced 2025-10-30 21:20:06 +00:00
Make SettingsManager an object instead of a namespace
This commit is contained in:
parent
1f964c49f8
commit
67881d31d5
5 changed files with 76 additions and 42 deletions
|
|
@ -69,7 +69,7 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
// Initialize application
|
// Initialize application
|
||||||
scwx::qt::config::RadarSite::Initialize();
|
scwx::qt::config::RadarSite::Initialize();
|
||||||
scwx::qt::manager::SettingsManager::Initialize();
|
scwx::qt::manager::SettingsManager::Instance().Initialize();
|
||||||
scwx::qt::manager::ResourceManager::Initialize();
|
scwx::qt::manager::ResourceManager::Initialize();
|
||||||
|
|
||||||
// Run Qt main loop
|
// Run Qt main loop
|
||||||
|
|
@ -89,7 +89,7 @@ int main(int argc, char* argv[])
|
||||||
|
|
||||||
// Shutdown application
|
// Shutdown application
|
||||||
scwx::qt::manager::ResourceManager::Shutdown();
|
scwx::qt::manager::ResourceManager::Shutdown();
|
||||||
scwx::qt::manager::SettingsManager::Shutdown();
|
scwx::qt::manager::SettingsManager::Instance().Shutdown();
|
||||||
|
|
||||||
// Shutdown AWS SDK
|
// Shutdown AWS SDK
|
||||||
Aws::ShutdownAPI(awsSdkOptions);
|
Aws::ShutdownAPI(awsSdkOptions);
|
||||||
|
|
|
||||||
|
|
@ -21,21 +21,33 @@ namespace qt
|
||||||
{
|
{
|
||||||
namespace manager
|
namespace manager
|
||||||
{
|
{
|
||||||
namespace SettingsManager
|
|
||||||
{
|
|
||||||
|
|
||||||
static const std::string logPrefix_ = "scwx::qt::manager::settings_manager";
|
static const std::string logPrefix_ = "scwx::qt::manager::settings_manager";
|
||||||
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
|
static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
|
||||||
|
|
||||||
static boost::json::value ConvertSettingsToJson();
|
class SettingsManager::Impl
|
||||||
static void GenerateDefaultSettings();
|
{
|
||||||
static bool LoadSettings(const boost::json::object& settingsJson);
|
public:
|
||||||
static void ValidateSettings();
|
explicit Impl(SettingsManager* self) : self_ {self} {}
|
||||||
|
~Impl() = default;
|
||||||
|
|
||||||
static bool initialized_ {false};
|
void ValidateSettings();
|
||||||
static std::string settingsPath_ {};
|
|
||||||
|
|
||||||
void Initialize()
|
static boost::json::value ConvertSettingsToJson();
|
||||||
|
static void GenerateDefaultSettings();
|
||||||
|
static bool LoadSettings(const boost::json::object& settingsJson);
|
||||||
|
|
||||||
|
SettingsManager* self_;
|
||||||
|
|
||||||
|
bool initialized_ {false};
|
||||||
|
std::string settingsPath_ {};
|
||||||
|
};
|
||||||
|
|
||||||
|
SettingsManager::SettingsManager() : p(std::make_unique<Impl>(this)) {}
|
||||||
|
|
||||||
|
SettingsManager::~SettingsManager() {};
|
||||||
|
|
||||||
|
void SettingsManager::Initialize()
|
||||||
{
|
{
|
||||||
std::string appDataPath {
|
std::string appDataPath {
|
||||||
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
|
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)
|
||||||
|
|
@ -50,14 +62,14 @@ void Initialize()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
settingsPath_ = appDataPath + "/settings.json";
|
p->settingsPath_ = appDataPath + "/settings.json";
|
||||||
initialized_ = true;
|
p->initialized_ = true;
|
||||||
|
|
||||||
ReadSettings(settingsPath_);
|
ReadSettings(p->settingsPath_);
|
||||||
ValidateSettings();
|
p->ValidateSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReadSettings(const std::string& settingsPath)
|
void SettingsManager::ReadSettings(const std::string& settingsPath)
|
||||||
{
|
{
|
||||||
boost::json::value settingsJson = nullptr;
|
boost::json::value settingsJson = nullptr;
|
||||||
|
|
||||||
|
|
@ -68,34 +80,34 @@ void ReadSettings(const std::string& settingsPath)
|
||||||
|
|
||||||
if (settingsJson == nullptr || !settingsJson.is_object())
|
if (settingsJson == nullptr || !settingsJson.is_object())
|
||||||
{
|
{
|
||||||
GenerateDefaultSettings();
|
Impl::GenerateDefaultSettings();
|
||||||
settingsJson = ConvertSettingsToJson();
|
settingsJson = Impl::ConvertSettingsToJson();
|
||||||
util::json::WriteJsonFile(settingsPath, settingsJson);
|
util::json::WriteJsonFile(settingsPath, settingsJson);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bool jsonDirty = LoadSettings(settingsJson.as_object());
|
bool jsonDirty = Impl::LoadSettings(settingsJson.as_object());
|
||||||
|
|
||||||
if (jsonDirty)
|
if (jsonDirty)
|
||||||
{
|
{
|
||||||
settingsJson = ConvertSettingsToJson();
|
settingsJson = Impl::ConvertSettingsToJson();
|
||||||
util::json::WriteJsonFile(settingsPath, settingsJson);
|
util::json::WriteJsonFile(settingsPath, settingsJson);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void SaveSettings()
|
void SettingsManager::SaveSettings()
|
||||||
{
|
{
|
||||||
if (initialized_)
|
if (p->initialized_)
|
||||||
{
|
{
|
||||||
logger_->info("Saving settings");
|
logger_->info("Saving settings");
|
||||||
|
|
||||||
boost::json::value settingsJson = ConvertSettingsToJson();
|
boost::json::value settingsJson = Impl::ConvertSettingsToJson();
|
||||||
util::json::WriteJsonFile(settingsPath_, settingsJson);
|
util::json::WriteJsonFile(p->settingsPath_, settingsJson);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shutdown()
|
void SettingsManager::Shutdown()
|
||||||
{
|
{
|
||||||
bool dataChanged = false;
|
bool dataChanged = false;
|
||||||
|
|
||||||
|
|
@ -109,7 +121,7 @@ void Shutdown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static boost::json::value ConvertSettingsToJson()
|
boost::json::value SettingsManager::Impl::ConvertSettingsToJson()
|
||||||
{
|
{
|
||||||
boost::json::object settingsJson;
|
boost::json::object settingsJson;
|
||||||
|
|
||||||
|
|
@ -122,7 +134,7 @@ static boost::json::value ConvertSettingsToJson()
|
||||||
return settingsJson;
|
return settingsJson;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GenerateDefaultSettings()
|
void SettingsManager::Impl::GenerateDefaultSettings()
|
||||||
{
|
{
|
||||||
logger_->info("Generating default settings");
|
logger_->info("Generating default settings");
|
||||||
|
|
||||||
|
|
@ -133,7 +145,8 @@ static void GenerateDefaultSettings()
|
||||||
settings::UiSettings::Instance().SetDefaults();
|
settings::UiSettings::Instance().SetDefaults();
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool LoadSettings(const boost::json::object& settingsJson)
|
bool SettingsManager::Impl::LoadSettings(
|
||||||
|
const boost::json::object& settingsJson)
|
||||||
{
|
{
|
||||||
logger_->info("Loading settings");
|
logger_->info("Loading settings");
|
||||||
|
|
||||||
|
|
@ -148,7 +161,7 @@ static bool LoadSettings(const boost::json::object& settingsJson)
|
||||||
return jsonDirty;
|
return jsonDirty;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ValidateSettings()
|
void SettingsManager::Impl::ValidateSettings()
|
||||||
{
|
{
|
||||||
logger_->debug("Validating settings");
|
logger_->debug("Validating settings");
|
||||||
|
|
||||||
|
|
@ -185,11 +198,16 @@ static void ValidateSettings()
|
||||||
|
|
||||||
if (settingsChanged)
|
if (settingsChanged)
|
||||||
{
|
{
|
||||||
SaveSettings();
|
self_->SaveSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace SettingsManager
|
SettingsManager& SettingsManager::Instance()
|
||||||
|
{
|
||||||
|
static SettingsManager instance_ {};
|
||||||
|
return instance_;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace manager
|
} // namespace manager
|
||||||
} // namespace qt
|
} // namespace qt
|
||||||
} // namespace scwx
|
} // namespace scwx
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
namespace scwx
|
namespace scwx
|
||||||
{
|
{
|
||||||
|
|
@ -8,15 +11,28 @@ namespace qt
|
||||||
{
|
{
|
||||||
namespace manager
|
namespace manager
|
||||||
{
|
{
|
||||||
namespace SettingsManager
|
|
||||||
|
class SettingsManager : public QObject
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
Q_DISABLE_COPY_MOVE(SettingsManager)
|
||||||
|
|
||||||
void Initialize();
|
public:
|
||||||
void ReadSettings(const std::string& settingsPath);
|
explicit SettingsManager();
|
||||||
void SaveSettings();
|
~SettingsManager();
|
||||||
void Shutdown();
|
|
||||||
|
void Initialize();
|
||||||
|
void ReadSettings(const std::string& settingsPath);
|
||||||
|
void SaveSettings();
|
||||||
|
void Shutdown();
|
||||||
|
|
||||||
|
static SettingsManager& Instance();
|
||||||
|
|
||||||
|
private:
|
||||||
|
class Impl;
|
||||||
|
std::unique_ptr<Impl> p;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace SettingsManager
|
|
||||||
} // namespace manager
|
} // namespace manager
|
||||||
} // namespace qt
|
} // namespace qt
|
||||||
} // namespace scwx
|
} // namespace scwx
|
||||||
|
|
|
||||||
|
|
@ -989,7 +989,7 @@ void SettingsDialogImpl::ApplyChanges()
|
||||||
|
|
||||||
if (committed)
|
if (committed)
|
||||||
{
|
{
|
||||||
manager::SettingsManager::SaveSettings();
|
manager::SettingsManager::Instance().SaveSettings();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -76,7 +76,7 @@ TEST_F(SettingsManagerTest, CreateJson)
|
||||||
// Verify file doesn't exist prior to test start
|
// Verify file doesn't exist prior to test start
|
||||||
EXPECT_EQ(std::filesystem::exists(filename), false);
|
EXPECT_EQ(std::filesystem::exists(filename), false);
|
||||||
|
|
||||||
SettingsManager::ReadSettings(filename);
|
SettingsManager::Instance().ReadSettings(filename);
|
||||||
|
|
||||||
EXPECT_EQ(std::filesystem::exists(filename), true);
|
EXPECT_EQ(std::filesystem::exists(filename), true);
|
||||||
|
|
||||||
|
|
@ -92,7 +92,7 @@ TEST_F(SettingsManagerTest, SettingsKeax)
|
||||||
std::string filename(std::string(SCWX_TEST_DATA_DIR) +
|
std::string filename(std::string(SCWX_TEST_DATA_DIR) +
|
||||||
"/json/settings/settings-keax.json");
|
"/json/settings/settings-keax.json");
|
||||||
|
|
||||||
SettingsManager::ReadSettings(filename);
|
SettingsManager::Instance().ReadSettings(filename);
|
||||||
|
|
||||||
EXPECT_EQ(
|
EXPECT_EQ(
|
||||||
settings::GeneralSettings::Instance().default_radar_site().GetValue(),
|
settings::GeneralSettings::Instance().default_radar_site().GetValue(),
|
||||||
|
|
@ -112,7 +112,7 @@ TEST_P(DefaultSettingsTest, DefaultSettings)
|
||||||
|
|
||||||
std::filesystem::copy_file(sourceFile, filename);
|
std::filesystem::copy_file(sourceFile, filename);
|
||||||
|
|
||||||
SettingsManager::ReadSettings(filename);
|
SettingsManager::Instance().ReadSettings(filename);
|
||||||
|
|
||||||
VerifyDefaults();
|
VerifyDefaults();
|
||||||
CompareFiles(filename, DEFAULT_SETTINGS_FILE);
|
CompareFiles(filename, DEFAULT_SETTINGS_FILE);
|
||||||
|
|
@ -140,7 +140,7 @@ TEST_P(BadSettingsTest, BadSettings)
|
||||||
|
|
||||||
std::filesystem::copy_file(sourceFile, filename);
|
std::filesystem::copy_file(sourceFile, filename);
|
||||||
|
|
||||||
SettingsManager::ReadSettings(filename);
|
SettingsManager::Instance().ReadSettings(filename);
|
||||||
|
|
||||||
CompareFiles(filename, goodFile);
|
CompareFiles(filename, goodFile);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue