2020-04-29 13:02:18 -07:00
|
|
|
#include "pch.h"
|
2019-11-11 11:00:42 -08:00
|
|
|
#include "Settings.h"
|
2019-12-17 11:21:46 +03:00
|
|
|
#include "PowerRenameInterfaces.h"
|
2021-10-25 15:40:19 +02:00
|
|
|
#include "Helpers.h"
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <common/SettingsAPI/settings_helpers.h>
|
2020-01-17 11:25:42 -08:00
|
|
|
|
2020-04-08 19:12:46 +02:00
|
|
|
#include <filesystem>
|
|
|
|
#include <commctrl.h>
|
2020-04-15 23:14:17 +02:00
|
|
|
#include <algorithm>
|
2020-05-05 12:39:15 +02:00
|
|
|
#include <fstream>
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <dll/PowerRenameConstants.h>
|
2019-11-11 11:00:42 -08:00
|
|
|
|
2020-04-08 19:12:46 +02:00
|
|
|
namespace
|
2019-11-11 11:00:42 -08:00
|
|
|
{
|
2020-04-15 23:14:17 +02:00
|
|
|
const wchar_t c_powerRenameDataFilePath[] = L"\\power-rename-settings.json";
|
2023-10-24 22:49:09 +02:00
|
|
|
const wchar_t c_powerRenameLastRunFilePath[] = L"\\power-rename-last-run-data.json";
|
2020-05-05 12:39:15 +02:00
|
|
|
const wchar_t c_powerRenameUIFlagsFilePath[] = L"\\power-rename-ui-flags";
|
2019-11-11 11:00:42 -08:00
|
|
|
|
2020-04-08 19:12:46 +02:00
|
|
|
const wchar_t c_enabled[] = L"Enabled";
|
|
|
|
const wchar_t c_showIconOnMenu[] = L"ShowIcon";
|
|
|
|
const wchar_t c_extendedContextMenuOnly[] = L"ExtendedContextMenuOnly";
|
|
|
|
const wchar_t c_persistState[] = L"PersistState";
|
|
|
|
const wchar_t c_maxMRUSize[] = L"MaxMRUSize";
|
|
|
|
const wchar_t c_flags[] = L"Flags";
|
|
|
|
const wchar_t c_searchText[] = L"SearchText";
|
|
|
|
const wchar_t c_replaceText[] = L"ReplaceText";
|
|
|
|
const wchar_t c_mruEnabled[] = L"MRUEnabled";
|
2020-11-09 19:13:43 +01:00
|
|
|
const wchar_t c_useBoostLib[] = L"UseBoostLib";
|
2023-08-14 21:43:31 +02:00
|
|
|
const wchar_t c_lastWindowWidth[] = L"LastWindowWidth";
|
|
|
|
const wchar_t c_lastWindowHeight[] = L"LastWindowHeight";
|
2019-11-11 11:00:42 -08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-08 19:12:46 +02:00
|
|
|
CSettings::CSettings()
|
|
|
|
{
|
2024-04-02 01:09:47 +02:00
|
|
|
generalJsonFilePath = PTSettingsHelper::get_powertoys_general_save_file_location();
|
2020-10-19 16:07:02 -07:00
|
|
|
std::wstring result = PTSettingsHelper::get_module_save_folder_location(PowerRenameConstants::ModuleKey);
|
2024-04-02 01:09:47 +02:00
|
|
|
moduleJsonFilePath = result + std::wstring(c_powerRenameDataFilePath);
|
2020-05-05 12:39:15 +02:00
|
|
|
UIFlagsFilePath = result + std::wstring(c_powerRenameUIFlagsFilePath);
|
2024-04-02 01:09:47 +02:00
|
|
|
RefreshEnabledState();
|
2020-04-15 23:14:17 +02:00
|
|
|
Load();
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 23:14:17 +02:00
|
|
|
void CSettings::Save()
|
2020-04-08 19:12:46 +02:00
|
|
|
{
|
2020-04-15 23:14:17 +02:00
|
|
|
json::JsonObject jsonData;
|
2020-04-08 19:12:46 +02:00
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
jsonData.SetNamedValue(c_showIconOnMenu, json::value(settings.showIconOnMenu));
|
2020-04-15 23:14:17 +02:00
|
|
|
jsonData.SetNamedValue(c_extendedContextMenuOnly, json::value(settings.extendedContextMenuOnly));
|
2020-12-15 15:16:09 +03:00
|
|
|
jsonData.SetNamedValue(c_persistState, json::value(settings.persistState));
|
|
|
|
jsonData.SetNamedValue(c_mruEnabled, json::value(settings.MRUEnabled));
|
|
|
|
jsonData.SetNamedValue(c_maxMRUSize, json::value(settings.maxMRUSize));
|
|
|
|
jsonData.SetNamedValue(c_useBoostLib, json::value(settings.useBoostLib));
|
2020-04-15 23:14:17 +02:00
|
|
|
|
2024-04-02 01:09:47 +02:00
|
|
|
json::to_file(moduleJsonFilePath, jsonData);
|
2020-05-05 12:39:15 +02:00
|
|
|
GetSystemTimeAsFileTime(&lastLoadedTime);
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 23:14:17 +02:00
|
|
|
void CSettings::Load()
|
2020-04-08 19:12:46 +02:00
|
|
|
{
|
2024-04-02 01:09:47 +02:00
|
|
|
if (!std::filesystem::exists(moduleJsonFilePath))
|
2020-04-08 19:12:46 +02:00
|
|
|
{
|
2020-04-15 23:14:17 +02:00
|
|
|
MigrateFromRegistry();
|
2020-04-08 19:12:46 +02:00
|
|
|
|
2020-04-15 23:14:17 +02:00
|
|
|
Save();
|
2020-05-05 12:39:15 +02:00
|
|
|
WriteFlags();
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-15 23:14:17 +02:00
|
|
|
ParseJson();
|
2020-05-05 12:39:15 +02:00
|
|
|
ReadFlags();
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-02 01:09:47 +02:00
|
|
|
void CSettings::RefreshEnabledState()
|
2020-04-08 19:12:46 +02:00
|
|
|
{
|
2020-04-15 23:14:17 +02:00
|
|
|
// Load json settings from data file if it is modified in the meantime.
|
|
|
|
FILETIME lastModifiedTime{};
|
2024-04-02 01:09:47 +02:00
|
|
|
if (!(LastModifiedTime(generalJsonFilePath, &lastModifiedTime) &&
|
|
|
|
CompareFileTime(&lastModifiedTime, &lastLoadedGeneralSettingsTime) == 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
lastLoadedGeneralSettingsTime = lastModifiedTime;
|
|
|
|
|
|
|
|
auto json = json::from_file(generalJsonFilePath);
|
|
|
|
if (!json)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const json::JsonObject& jsonSettings = json.value();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
json::JsonObject modulesEnabledState;
|
|
|
|
json::get(jsonSettings, L"enabled", modulesEnabledState, json::JsonObject{});
|
|
|
|
json::get(modulesEnabledState, L"PowerRename", settings.enabled, true);
|
|
|
|
}
|
|
|
|
catch (const winrt::hresult_error&)
|
2020-04-15 23:14:17 +02:00
|
|
|
{
|
|
|
|
}
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 23:14:17 +02:00
|
|
|
void CSettings::MigrateFromRegistry()
|
2020-04-08 19:12:46 +02:00
|
|
|
{
|
2024-04-02 01:09:47 +02:00
|
|
|
//settings.enabled = GetRegBoolean(c_enabled, true);
|
2020-12-15 15:16:09 +03:00
|
|
|
settings.showIconOnMenu = GetRegBoolean(c_showIconOnMenu, true);
|
2020-04-08 19:12:46 +02:00
|
|
|
settings.extendedContextMenuOnly = GetRegBoolean(c_extendedContextMenuOnly, false); // Disabled by default.
|
2020-12-15 15:16:09 +03:00
|
|
|
settings.persistState = GetRegBoolean(c_persistState, true);
|
|
|
|
settings.MRUEnabled = GetRegBoolean(c_mruEnabled, true);
|
|
|
|
settings.maxMRUSize = GetRegNumber(c_maxMRUSize, 10);
|
|
|
|
settings.flags = GetRegNumber(c_flags, 0);
|
2023-10-24 22:49:09 +02:00
|
|
|
|
|
|
|
LastRunSettingsInstance().SetSearchText(GetRegString(c_searchText, L""));
|
|
|
|
LastRunSettingsInstance().SetReplaceText(GetRegString(c_replaceText, L""));
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
settings.useBoostLib = false; // Never existed in registry, disabled by default.
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
|
|
|
|
2020-04-15 23:14:17 +02:00
|
|
|
void CSettings::ParseJson()
|
2020-04-08 19:12:46 +02:00
|
|
|
{
|
2024-04-02 01:09:47 +02:00
|
|
|
auto json = json::from_file(moduleJsonFilePath);
|
2020-04-08 19:12:46 +02:00
|
|
|
if (json)
|
|
|
|
{
|
|
|
|
const json::JsonObject& jsonSettings = json.value();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (json::has(jsonSettings, c_showIconOnMenu, json::JsonValueType::Boolean))
|
|
|
|
{
|
|
|
|
settings.showIconOnMenu = jsonSettings.GetNamedBoolean(c_showIconOnMenu);
|
|
|
|
}
|
|
|
|
if (json::has(jsonSettings, c_extendedContextMenuOnly, json::JsonValueType::Boolean))
|
|
|
|
{
|
|
|
|
settings.extendedContextMenuOnly = jsonSettings.GetNamedBoolean(c_extendedContextMenuOnly);
|
|
|
|
}
|
|
|
|
if (json::has(jsonSettings, c_persistState, json::JsonValueType::Boolean))
|
|
|
|
{
|
|
|
|
settings.persistState = jsonSettings.GetNamedBoolean(c_persistState);
|
|
|
|
}
|
|
|
|
if (json::has(jsonSettings, c_mruEnabled, json::JsonValueType::Boolean))
|
|
|
|
{
|
|
|
|
settings.MRUEnabled = jsonSettings.GetNamedBoolean(c_mruEnabled);
|
|
|
|
}
|
|
|
|
if (json::has(jsonSettings, c_maxMRUSize, json::JsonValueType::Number))
|
|
|
|
{
|
2023-02-08 13:10:28 +00:00
|
|
|
settings.maxMRUSize = static_cast<unsigned int>(jsonSettings.GetNamedNumber(c_maxMRUSize));
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
2020-11-09 19:13:43 +01:00
|
|
|
if (json::has(jsonSettings, c_useBoostLib, json::JsonValueType::Boolean))
|
|
|
|
{
|
|
|
|
settings.useBoostLib = jsonSettings.GetNamedBoolean(c_useBoostLib);
|
|
|
|
}
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
2020-12-15 15:16:09 +03:00
|
|
|
catch (const winrt::hresult_error&)
|
|
|
|
{
|
|
|
|
}
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
2020-05-05 12:39:15 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-04-08 19:12:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CSettings& CSettingsInstance()
|
|
|
|
{
|
|
|
|
static CSettings instance;
|
|
|
|
return instance;
|
|
|
|
}
|
2023-10-24 22:49:09 +02:00
|
|
|
|
|
|
|
LastRunSettings& LastRunSettingsInstance()
|
|
|
|
{
|
|
|
|
static LastRunSettings instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LastRunSettings::Load()
|
|
|
|
{
|
|
|
|
const auto lastRunSettingsFilePath = PTSettingsHelper::get_module_save_folder_location(PowerRenameConstants::ModuleKey) + c_powerRenameLastRunFilePath;
|
|
|
|
auto json = json::from_file(lastRunSettingsFilePath);
|
|
|
|
if (!json)
|
|
|
|
return;
|
|
|
|
|
|
|
|
json::get(*json, c_searchText, searchText, L"");
|
|
|
|
json::get(*json, c_replaceText, replaceText, L"");
|
|
|
|
json::get(*json, c_lastWindowWidth, lastWindowWidth, DEFAULT_WINDOW_WIDTH);
|
|
|
|
json::get(*json, c_lastWindowHeight, lastWindowHeight, DEFAULT_WINDOW_HEIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LastRunSettings::Save()
|
|
|
|
{
|
|
|
|
json::JsonObject json;
|
|
|
|
|
|
|
|
json.SetNamedValue(c_searchText, json::value(searchText));
|
|
|
|
json.SetNamedValue(c_replaceText, json::value(replaceText));
|
|
|
|
json.SetNamedValue(c_lastWindowWidth, json::value(lastWindowWidth));
|
|
|
|
json.SetNamedValue(c_lastWindowHeight, json::value(lastWindowHeight));
|
|
|
|
|
|
|
|
const auto lastRunSettingsFilePath = PTSettingsHelper::get_module_save_folder_location(PowerRenameConstants::ModuleKey) + c_powerRenameLastRunFilePath;
|
|
|
|
json::to_file(lastRunSettingsFilePath, json);
|
|
|
|
}
|