Split general module settings and UI flags to separate files. (#2613)

This commit is contained in:
vldmr11080
2020-05-05 12:39:15 +02:00
committed by GitHub
parent 9206d1f5f1
commit 41935aa9aa
2 changed files with 30 additions and 7 deletions

View File

@@ -6,10 +6,12 @@
#include <filesystem> #include <filesystem>
#include <commctrl.h> #include <commctrl.h>
#include <algorithm> #include <algorithm>
#include <fstream>
namespace namespace
{ {
const wchar_t c_powerRenameDataFilePath[] = L"\\power-rename-settings.json"; const wchar_t c_powerRenameDataFilePath[] = L"\\power-rename-settings.json";
const wchar_t c_powerRenameUIFlagsFilePath[] = L"\\power-rename-ui-flags";
const wchar_t c_searchMRUListFilePath[] = L"\\search-mru.json"; const wchar_t c_searchMRUListFilePath[] = L"\\search-mru.json";
const wchar_t c_replaceMRUListFilePath[] = L"\\replace-mru.json"; const wchar_t c_replaceMRUListFilePath[] = L"\\replace-mru.json";
@@ -395,6 +397,7 @@ CSettings::CSettings()
{ {
std::wstring result = PTSettingsHelper::get_module_save_folder_location(L"PowerRename"); std::wstring result = PTSettingsHelper::get_module_save_folder_location(L"PowerRename");
jsonFilePath = result + std::wstring(c_powerRenameDataFilePath); jsonFilePath = result + std::wstring(c_powerRenameDataFilePath);
UIFlagsFilePath = result + std::wstring(c_powerRenameUIFlagsFilePath);
Load(); Load();
} }
@@ -408,11 +411,11 @@ void CSettings::Save()
jsonData.SetNamedValue(c_persistState, json::value(settings.persistState)); jsonData.SetNamedValue(c_persistState, json::value(settings.persistState));
jsonData.SetNamedValue(c_mruEnabled, json::value(settings.MRUEnabled)); jsonData.SetNamedValue(c_mruEnabled, json::value(settings.MRUEnabled));
jsonData.SetNamedValue(c_maxMRUSize, json::value(settings.maxMRUSize)); jsonData.SetNamedValue(c_maxMRUSize, json::value(settings.maxMRUSize));
jsonData.SetNamedValue(c_flags, json::value(settings.flags));
jsonData.SetNamedValue(c_searchText, json::value(settings.searchText)); jsonData.SetNamedValue(c_searchText, json::value(settings.searchText));
jsonData.SetNamedValue(c_replaceText, json::value(settings.replaceText)); jsonData.SetNamedValue(c_replaceText, json::value(settings.replaceText));
json::to_file(jsonFilePath, jsonData); json::to_file(jsonFilePath, jsonData);
GetSystemTimeAsFileTime(&lastLoadedTime);
} }
void CSettings::Load() void CSettings::Load()
@@ -422,12 +425,13 @@ void CSettings::Load()
MigrateFromRegistry(); MigrateFromRegistry();
Save(); Save();
WriteFlags();
} }
else else
{ {
ParseJson(); ParseJson();
ReadFlags();
} }
GetSystemTimeAsFileTime(&lastLoadedTime);
} }
void CSettings::Reload() void CSettings::Reload()
@@ -486,10 +490,6 @@ void CSettings::ParseJson()
{ {
settings.maxMRUSize = (unsigned int)jsonSettings.GetNamedNumber(c_maxMRUSize); settings.maxMRUSize = (unsigned int)jsonSettings.GetNamedNumber(c_maxMRUSize);
} }
if (json::has(jsonSettings, c_flags, json::JsonValueType::Number))
{
settings.flags = (unsigned int)jsonSettings.GetNamedNumber(c_flags);
}
if (json::has(jsonSettings, c_searchText, json::JsonValueType::String)) if (json::has(jsonSettings, c_searchText, json::JsonValueType::String))
{ {
settings.searchText = jsonSettings.GetNamedString(c_searchText); settings.searchText = jsonSettings.GetNamedString(c_searchText);
@@ -501,6 +501,25 @@ void CSettings::ParseJson()
} }
catch (const winrt::hresult_error&) { } catch (const winrt::hresult_error&) { }
} }
GetSystemTimeAsFileTime(&lastLoadedTime);
}
void CSettings::ReadFlags()
{
std::ifstream file(UIFlagsFilePath, std::ios::binary);
if (file.is_open())
{
file >> settings.flags;
}
}
void CSettings::WriteFlags()
{
std::ofstream file(UIFlagsFilePath, std::ios::binary);
if (file.is_open())
{
file << settings.flags;
}
} }
CSettings& CSettingsInstance() CSettings& CSettingsInstance()

View File

@@ -79,7 +79,7 @@ public:
inline void SetFlags(unsigned int flags) inline void SetFlags(unsigned int flags)
{ {
settings.flags = flags; settings.flags = flags;
Save(); WriteFlags();
} }
inline const std::wstring& GetSearchText() const inline const std::wstring& GetSearchText() const
@@ -125,8 +125,12 @@ private:
void MigrateFromRegistry(); void MigrateFromRegistry();
void ParseJson(); void ParseJson();
void ReadFlags();
void WriteFlags();
Settings settings; Settings settings;
std::wstring jsonFilePath; std::wstring jsonFilePath;
std::wstring UIFlagsFilePath;
FILETIME lastLoadedTime; FILETIME lastLoadedTime;
}; };