Settings work

This commit is contained in:
Ivan Stošić 2022-09-20 12:20:49 +02:00
parent c8a987a2a3
commit 24fca9df02
8 changed files with 168 additions and 8 deletions

View File

@ -22,10 +22,16 @@ namespace constants::nonlocalizable
constexpr WCHAR FileNameUIExe[] = L"PowerToys.FileLocksmithGUI.exe";
// String key used by PowerToys
constexpr WCHAR PowerToyKey[] = L"FileLocksmith";
constexpr WCHAR PowerToyKey[] = L"File Locksmith";
// Nonlocalized name of this PowerToy, for logs, etc
constexpr WCHAR PowerToyName[] = L"File Locksmith";
// JSON key used to store whether the module is enabled
constexpr WCHAR JsonKeyEnabled[] = L"Enabled";
// Path of the JSON file used to store settings
constexpr WCHAR DataFilePath[] = L"\\file-locksmith-settings.json";
}
// Macros, non-localizable

View File

@ -2,6 +2,7 @@
#include "ExplorerCommand.h"
#include "Constants.h"
#include "Settings.h"
#include "dllmain.h"
// Implementations of inherited IUnknown methods
@ -101,6 +102,12 @@ IFACEMETHODIMP ExplorerCommand::Initialize(PCIDLIST_ABSOLUTE pidlFolder, IDataOb
IFACEMETHODIMP ExplorerCommand::QueryContextMenu(HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags)
{
// Skip if disabled
if (!FileLocksmithSettingsInstance().GetEnabled())
{
return S_OK;
}
HRESULT hr = E_UNEXPECTED;
if (m_data_obj && !(uFlags & (CMF_DEFAULTONLY | CMF_VERBSONLY | CMF_OPTIMIZEFORINVOKE)))
{

View File

@ -166,6 +166,7 @@
<ClInclude Include="ExplorerCommand.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Registry.h" />
<ClInclude Include="Settings.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="ClassFactory.cpp" />
@ -179,6 +180,7 @@
</ClCompile>
<ClCompile Include="PowerToysModule.cpp" />
<ClCompile Include="Registry.cpp" />
<ClCompile Include="Settings.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="dll.def" />

View File

@ -33,6 +33,9 @@
<ClInclude Include="dllmain.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Settings.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
@ -53,6 +56,9 @@
<ClCompile Include="PowerToysModule.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Settings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="dll.def">

View File

@ -8,18 +8,17 @@
#include "Constants.h"
#include "dllmain.h"
#include "Settings.h"
class FileLocksmithModule : public PowertoyModuleIface
{
public:
FileLocksmithModule()
{
m_enabled = true;
LoggerHelpers::init_logger(constants::nonlocalizable::PowerToyName, L"ModuleInterface", LogSettings::fileLocksmithLoggerName);
init_settings();
}
virtual const wchar_t* get_name() override
{
return constants::localizable::PowerToyName;
@ -70,14 +69,14 @@ public:
{
Logger::info(L"File Locksmith enabled");
m_enabled = true;
globals::enabled = true;
save_settings();
}
virtual void disable() override
{
Logger::info(L"File Locksmith disabled");
m_enabled = false;
globals::enabled = false;
save_settings();
}
virtual bool is_enabled() override
@ -90,10 +89,24 @@ public:
delete this;
}
// This should be enough to create an instance
private:
bool m_enabled;
void init_settings()
{
m_enabled = FileLocksmithSettingsInstance().GetEnabled();
// TODO trace
// Trace::EnablePowerRename(m_enabled);
}
void save_settings()
{
auto& settings = FileLocksmithSettingsInstance();
settings.SetEnabled(m_enabled);
settings.Save();
// TODO trace
// Trace::EnablePowerRename(m_enabled);
}
};
extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()

View File

@ -0,0 +1,85 @@
#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()
{
std::wstring savePath = PTSettingsHelper::get_module_save_folder_location(constants::nonlocalizable::PowerToyKey);
std::error_code ec;
jsonFilePath = savePath + constants::nonlocalizable::DataFilePath;
Load();
}
void FileLocksmithSettings::Save()
{
json::JsonObject jsonData;
jsonData.SetNamedValue(constants::nonlocalizable::JsonKeyEnabled, json::value(settings.enabled));
json::to_file(jsonFilePath, jsonData);
GetSystemTimeAsFileTime(&lastLoadedTime);
}
void FileLocksmithSettings::Load()
{
if (!std::filesystem::exists(jsonFilePath))
{
Save();
}
else
{
ParseJson();
}
}
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::JsonKeyEnabled, json::JsonValueType::Boolean))
{
settings.enabled = jsonSettings.GetNamedBoolean(constants::nonlocalizable::JsonKeyEnabled);
}
}
catch (const winrt::hresult_error&)
{
}
}
GetSystemTimeAsFileTime(&lastLoadedTime);
}
FileLocksmithSettings& FileLocksmithSettingsInstance()
{
static FileLocksmithSettings instance;
return instance;
}

View File

@ -0,0 +1,39 @@
#pragma once
#include "pch.h"
class FileLocksmithSettings
{
public:
FileLocksmithSettings();
inline bool GetEnabled()
{
Reload();
return settings.enabled;
}
inline void SetEnabled(bool enabled)
{
settings.enabled = enabled;
Save();
}
void Save();
void Load();
private:
struct Settings
{
bool enabled{ true };
};
void Reload();
void ParseJson();
Settings settings;
std::wstring jsonFilePath;
FILETIME lastLoadedTime;
};
FileLocksmithSettings& FileLocksmithSettingsInstance();

View File

@ -7,7 +7,9 @@
#include <Shlwapi.h>
#include <ShlObj_core.h>
#include <atlbase.h>
#include <commctrl.h>
// C++ Standard library
#include <atomic>
#include <string>
#include <filesystem>