mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 03:07:04 +02:00
* Added get_key to powertoysmodule interface * Replace get_name with get_key * Implement get_key function in modules * Make key global constant in each module * Update settings v1 to use key to load and save files * Fixed fancyzones and preview pane unit tests * Removed setings unit test as the case is not covered anymore * Add constant files for modules and use it to reference module key * Add constant string files to colorpicker, launcher and shortcut guide * correct sunction signature in settings helper * Fix powerpreview merge conflicts * nit fix with include statement location * add check for fields in from_json_string * Updated preview pane tests with correct from_json_string signature * Correct Image resizer naming * Roll back changes for adding check for property and version * Fix image resizer not working
117 lines
3.0 KiB
C++
117 lines
3.0 KiB
C++
#include "pch.h"
|
|
#include "Settings.h"
|
|
|
|
#include <common/json.h>
|
|
#include <common/settings_helpers.h>
|
|
#include <filesystem>
|
|
#include <commctrl.h>
|
|
#include <imageresizer\dll\ImageResizerConstants.h>
|
|
|
|
namespace
|
|
{
|
|
const wchar_t c_imageResizerDataFilePath[] = L"\\image-resizer-settings.json";
|
|
const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\ImageResizer";
|
|
const wchar_t c_enabled[] = L"Enabled";
|
|
|
|
unsigned int RegReadInteger(const std::wstring& valueName, unsigned int defaultValue)
|
|
{
|
|
DWORD type = REG_DWORD;
|
|
DWORD data = 0;
|
|
DWORD size = sizeof(DWORD);
|
|
if (SHGetValue(HKEY_CURRENT_USER, c_rootRegPath, valueName.c_str(), &type, &data, &size) == ERROR_SUCCESS)
|
|
{
|
|
return data;
|
|
}
|
|
return defaultValue;
|
|
}
|
|
|
|
bool RegReadBoolean(const std::wstring& valueName, bool defaultValue)
|
|
{
|
|
DWORD value = RegReadInteger(valueName.c_str(), defaultValue ? 1 : 0);
|
|
return (value == 0) ? false : true;
|
|
}
|
|
|
|
bool LastModifiedTime(const std::wstring& filePath, FILETIME* lpFileTime)
|
|
{
|
|
WIN32_FILE_ATTRIBUTE_DATA attr{};
|
|
if (GetFileAttributesExW(filePath.c_str(), GetFileExInfoStandard, &attr))
|
|
{
|
|
*lpFileTime = attr.ftLastWriteTime;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
CSettings::CSettings()
|
|
{
|
|
std::wstring result = PTSettingsHelper::get_module_save_folder_location(ImageResizerConstants::ModuleSaveFolderKey);
|
|
jsonFilePath = result + std::wstring(c_imageResizerDataFilePath);
|
|
Load();
|
|
}
|
|
|
|
void CSettings::Save()
|
|
{
|
|
json::JsonObject jsonData;
|
|
|
|
jsonData.SetNamedValue(c_enabled, json::value(settings.enabled));
|
|
|
|
json::to_file(jsonFilePath, jsonData);
|
|
GetSystemTimeAsFileTime(&lastLoadedTime);
|
|
}
|
|
|
|
void CSettings::Load()
|
|
{
|
|
if (!std::filesystem::exists(jsonFilePath))
|
|
{
|
|
MigrateFromRegistry();
|
|
|
|
Save();
|
|
}
|
|
else
|
|
{
|
|
ParseJson();
|
|
}
|
|
}
|
|
|
|
void CSettings::Reload()
|
|
{
|
|
// Load json settings from data file if it is modified in the meantime.
|
|
FILETIME lastModifiedTime{};
|
|
if (LastModifiedTime(jsonFilePath, &lastModifiedTime) &&
|
|
CompareFileTime(&lastModifiedTime, &lastLoadedTime) == 1)
|
|
{
|
|
Load();
|
|
}
|
|
}
|
|
|
|
void CSettings::MigrateFromRegistry()
|
|
{
|
|
settings.enabled = RegReadBoolean(c_enabled, true);
|
|
}
|
|
|
|
void CSettings::ParseJson()
|
|
{
|
|
auto json = json::from_file(jsonFilePath);
|
|
if (json)
|
|
{
|
|
const json::JsonObject& jsonSettings = json.value();
|
|
try
|
|
{
|
|
if (json::has(jsonSettings, c_enabled, json::JsonValueType::Boolean))
|
|
{
|
|
settings.enabled = jsonSettings.GetNamedBoolean(c_enabled);
|
|
}
|
|
}
|
|
catch (const winrt::hresult_error&)
|
|
{
|
|
}
|
|
}
|
|
GetSystemTimeAsFileTime(&lastLoadedTime);
|
|
}
|
|
|
|
CSettings& CSettingsInstance()
|
|
{
|
|
static CSettings instance;
|
|
return instance;
|
|
} |