Files
Andrey Nekrasov f23fa3f592 [DSC] Implement Microsoft.PowerToys.Configure DSCResource & winget support (#30918)
* [DSC] Microsoft.PowerToys.Configure module + winget configuration file support

* f: fix for an incorrect directory id reference

* f: update comment

* f: address review comments

* f: file locksmith bug fix

* f: add explorer preview switches in samples

* f: remove debug

* Sign DSC files

* f: implement docs/samples generator

* [ci]Sign FancyZonesEditorCommon.dll

* Sign DSC files in the Generated folder

* f: address review comments

* f: update usable options

* f: add autogenerated sample

* [Installer] Don't use same GUID for different components

* [Installer]Don't remove folders shared by other modules

* Allow configuring PTRun MaximumNumberOfResults

* Remove all settings DSC sample. Just random data

* Allow configuring Hosts Run as Administrator

* Revert "[Installer]Don't remove folders shared by other modules"

This reverts commit 6da3d6cfd5.

* Add all PTRun plugins and Global and keyboard to DSC sample

* Fix issues with context menu modules not disabling

* Fix default enabled values when setting with DSC

* Fix tests regarding default modules in Settings

* Fix merge error

* Restart PowerToys process if we stopped it

---------

Co-authored-by: Andrey Nekrasov <1828123+yuyoyuppe@users.noreply.github.com>
Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-04-02 00:09:47 +01:00

114 lines
3.1 KiB
C++

#include "pch.h"
#include "Settings.h"
#include "Constants.h"
#include <common/utils/json.h>
#include <common/SettingsAPI/settings_helpers.h>
static 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;
}
FileLocksmithSettings::FileLocksmithSettings()
{
generalJsonFilePath = PTSettingsHelper::get_powertoys_general_save_file_location();
std::wstring savePath = PTSettingsHelper::get_module_save_folder_location(constants::nonlocalizable::PowerToyKey);
std::error_code ec;
jsonFilePath = savePath + constants::nonlocalizable::DataFilePath;
RefreshEnabledState();
Load();
}
void FileLocksmithSettings::Save()
{
json::JsonObject jsonData;
jsonData.SetNamedValue(constants::nonlocalizable::JsonKeyShowInExtendedContextMenu, json::value(settings.showInExtendedContextMenu));
json::to_file(jsonFilePath, jsonData);
GetSystemTimeAsFileTime(&lastLoadedTime);
}
void FileLocksmithSettings::Load()
{
if (!std::filesystem::exists(jsonFilePath))
{
Save();
}
else
{
ParseJson();
}
}
void FileLocksmithSettings::RefreshEnabledState()
{
// Load json settings from data file if it is modified in the meantime.
FILETIME lastModifiedTime{};
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"File Locksmith", settings.enabled, true);
}
catch (const winrt::hresult_error&)
{
}
}
void FileLocksmithSettings::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 FileLocksmithSettings::ParseJson()
{
auto json = json::from_file(jsonFilePath);
if (json)
{
const json::JsonObject& jsonSettings = json.value();
try
{
if (json::has(jsonSettings, constants::nonlocalizable::JsonKeyShowInExtendedContextMenu, json::JsonValueType::Boolean))
{
settings.showInExtendedContextMenu = jsonSettings.GetNamedBoolean(constants::nonlocalizable::JsonKeyShowInExtendedContextMenu);
}
}
catch (const winrt::hresult_error&)
{
}
}
GetSystemTimeAsFileTime(&lastLoadedTime);
}
FileLocksmithSettings& FileLocksmithSettingsInstance()
{
static FileLocksmithSettings instance;
return instance;
}