mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 04:50:06 +00:00 
			
		
		
		
	Initial loading of JSON-based settings
This commit is contained in:
		
							parent
							
								
									28ea12cbfe
								
							
						
					
					
						commit
						1c0140fc98
					
				
					 8 changed files with 561 additions and 38 deletions
				
			
		
							
								
								
									
										116
									
								
								scwx-qt/source/scwx/qt/manager/settings_manager.cpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										116
									
								
								scwx-qt/source/scwx/qt/manager/settings_manager.cpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,116 @@ | |||
| #include <scwx/qt/manager/settings_manager.hpp> | ||||
| #include <scwx/qt/util/json.hpp> | ||||
| 
 | ||||
| #include <filesystem> | ||||
| #include <fstream> | ||||
| 
 | ||||
| #include <QDir> | ||||
| #include <QStandardPaths> | ||||
| 
 | ||||
| #include <boost/log/trivial.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace manager | ||||
| { | ||||
| namespace SettingsManager | ||||
| { | ||||
| 
 | ||||
| static const std::string logPrefix_ = "[scwx::qt::manager::settings_manager] "; | ||||
| 
 | ||||
| static std::shared_ptr<settings::GeneralSettings> generalSettings_ = nullptr; | ||||
| 
 | ||||
| static boost::json::value ConvertSettingsToJson(); | ||||
| static void               GenerateDefaultSettings(); | ||||
| static bool               LoadSettings(const boost::json::object& settingsJson); | ||||
| 
 | ||||
| bool Initialize() | ||||
| { | ||||
|    std::string appDataPath { | ||||
|       QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) | ||||
|          .toStdString()}; | ||||
| 
 | ||||
|    if (!std::filesystem::is_directory(appDataPath)) | ||||
|    { | ||||
|       if (!std::filesystem::create_directories(appDataPath)) | ||||
|       { | ||||
|          BOOST_LOG_TRIVIAL(error) | ||||
|             << logPrefix_ << "Unable to create application data directory: \"" | ||||
|             << appDataPath << "\""; | ||||
|          return false; | ||||
|       } | ||||
|    } | ||||
| 
 | ||||
|    std::string settingsPath {appDataPath + "/settings.json"}; | ||||
| 
 | ||||
|    ReadSettings(settingsPath); | ||||
| 
 | ||||
|    return true; | ||||
| } | ||||
| 
 | ||||
| void ReadSettings(const std::string& settingsPath) | ||||
| { | ||||
|    boost::json::value settingsJson = nullptr; | ||||
| 
 | ||||
|    if (std::filesystem::exists(settingsPath)) | ||||
|    { | ||||
|       settingsJson = util::json::ReadJsonFile(settingsPath); | ||||
|    } | ||||
| 
 | ||||
|    if (settingsJson == nullptr || !settingsJson.is_object()) | ||||
|    { | ||||
|       GenerateDefaultSettings(); | ||||
|       settingsJson = ConvertSettingsToJson(); | ||||
|       util::json::WriteJsonFile(settingsPath, settingsJson); | ||||
|    } | ||||
|    else | ||||
|    { | ||||
|       bool jsonDirty = LoadSettings(settingsJson.as_object()); | ||||
| 
 | ||||
|       if (jsonDirty) | ||||
|       { | ||||
|          settingsJson = ConvertSettingsToJson(); | ||||
|          util::json::WriteJsonFile(settingsPath, settingsJson); | ||||
|       } | ||||
|    }; | ||||
| } | ||||
| 
 | ||||
| std::shared_ptr<settings::GeneralSettings> general_settings() | ||||
| { | ||||
|    return generalSettings_; | ||||
| } | ||||
| 
 | ||||
| static boost::json::value ConvertSettingsToJson() | ||||
| { | ||||
|    boost::json::object settingsJson; | ||||
| 
 | ||||
|    settingsJson["general"] = generalSettings_->ToJson(); | ||||
| 
 | ||||
|    return settingsJson; | ||||
| } | ||||
| 
 | ||||
| static void GenerateDefaultSettings() | ||||
| { | ||||
|    BOOST_LOG_TRIVIAL(info) << logPrefix_ << "Generating default settings"; | ||||
| 
 | ||||
|    generalSettings_ = settings::GeneralSettings::Create(); | ||||
| } | ||||
| 
 | ||||
| static bool LoadSettings(const boost::json::object& settingsJson) | ||||
| { | ||||
|    BOOST_LOG_TRIVIAL(info) << logPrefix_ << "Loading settings"; | ||||
| 
 | ||||
|    bool jsonDirty = false; | ||||
| 
 | ||||
|    generalSettings_ = settings::GeneralSettings::Load( | ||||
|       settingsJson.if_contains("general"), jsonDirty); | ||||
| 
 | ||||
|    return jsonDirty; | ||||
| } | ||||
| 
 | ||||
| } // namespace SettingsManager
 | ||||
| } // namespace manager
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
							
								
								
									
										22
									
								
								scwx-qt/source/scwx/qt/manager/settings_manager.hpp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								scwx-qt/source/scwx/qt/manager/settings_manager.hpp
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,22 @@ | |||
| #pragma once | ||||
| 
 | ||||
| #include <scwx/qt/settings/general_settings.hpp> | ||||
| 
 | ||||
| namespace scwx | ||||
| { | ||||
| namespace qt | ||||
| { | ||||
| namespace manager | ||||
| { | ||||
| namespace SettingsManager | ||||
| { | ||||
| 
 | ||||
| bool Initialize(); | ||||
| void ReadSettings(const std::string& settingsPath); | ||||
| 
 | ||||
| std::shared_ptr<settings::GeneralSettings> general_settings(); | ||||
| 
 | ||||
| } // namespace SettingsManager
 | ||||
| } // namespace manager
 | ||||
| } // namespace qt
 | ||||
| } // namespace scwx
 | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat