Settings.cpp
1 #include "pch.h" 2 #include "Settings.h" 3 #include "Constants.h" 4 5 #include <filesystem> 6 #include <common/utils/json.h> 7 #include <common/SettingsAPI/settings_helpers.h> 8 9 static bool LastModifiedTime(const std::wstring& filePath, FILETIME* lpFileTime) 10 { 11 WIN32_FILE_ATTRIBUTE_DATA attr{}; 12 if (GetFileAttributesExW(filePath.c_str(), GetFileExInfoStandard, &attr)) 13 { 14 *lpFileTime = attr.ftLastWriteTime; 15 return true; 16 } 17 return false; 18 } 19 20 FileLocksmithSettings::FileLocksmithSettings() 21 { 22 generalJsonFilePath = PTSettingsHelper::get_powertoys_general_save_file_location(); 23 std::wstring savePath = PTSettingsHelper::get_module_save_folder_location(constants::nonlocalizable::PowerToyKey); 24 std::error_code ec; 25 26 jsonFilePath = savePath + constants::nonlocalizable::DataFilePath; 27 RefreshEnabledState(); 28 Load(); 29 } 30 31 void FileLocksmithSettings::Save() 32 { 33 json::JsonObject jsonData; 34 35 jsonData.SetNamedValue(constants::nonlocalizable::JsonKeyShowInExtendedContextMenu, json::value(settings.showInExtendedContextMenu)); 36 37 json::to_file(jsonFilePath, jsonData); 38 GetSystemTimeAsFileTime(&lastLoadedTime); 39 } 40 41 void FileLocksmithSettings::Load() 42 { 43 if (!std::filesystem::exists(jsonFilePath)) 44 { 45 Save(); 46 } 47 else 48 { 49 ParseJson(); 50 } 51 } 52 53 void FileLocksmithSettings::RefreshEnabledState() 54 { 55 // Load json settings from data file if it is modified in the meantime. 56 FILETIME lastModifiedTime{}; 57 if (!(LastModifiedTime(generalJsonFilePath, &lastModifiedTime) && 58 CompareFileTime(&lastModifiedTime, &lastLoadedGeneralSettingsTime) == 1)) 59 return; 60 61 lastLoadedGeneralSettingsTime = lastModifiedTime; 62 63 auto json = json::from_file(generalJsonFilePath); 64 if (!json) 65 return; 66 67 const json::JsonObject& jsonSettings = json.value(); 68 try 69 { 70 json::JsonObject modulesEnabledState; 71 json::get(jsonSettings, L"enabled", modulesEnabledState, json::JsonObject{}); 72 json::get(modulesEnabledState, L"File Locksmith", settings.enabled, true); 73 } 74 catch (const winrt::hresult_error&) 75 { 76 } 77 } 78 79 void FileLocksmithSettings::Reload() 80 { 81 // Load json settings from data file if it is modified in the meantime. 82 FILETIME lastModifiedTime{}; 83 if (LastModifiedTime(jsonFilePath, &lastModifiedTime) && 84 CompareFileTime(&lastModifiedTime, &lastLoadedTime) == 1) 85 { 86 Load(); 87 } 88 } 89 90 void FileLocksmithSettings::ParseJson() 91 { 92 auto json = json::from_file(jsonFilePath); 93 if (json) 94 { 95 const json::JsonObject& jsonSettings = json.value(); 96 try 97 { 98 if (json::has(jsonSettings, constants::nonlocalizable::JsonKeyShowInExtendedContextMenu, json::JsonValueType::Boolean)) 99 { 100 settings.showInExtendedContextMenu = jsonSettings.GetNamedBoolean(constants::nonlocalizable::JsonKeyShowInExtendedContextMenu); 101 } 102 } 103 catch (const winrt::hresult_error&) 104 { 105 } 106 } 107 GetSystemTimeAsFileTime(&lastLoadedTime); 108 } 109 110 FileLocksmithSettings& FileLocksmithSettingsInstance() 111 { 112 static FileLocksmithSettings instance; 113 return instance; 114 }