settings_helpers.cpp
1 #include "pch.h" 2 #include "settings_helpers.h" 3 4 namespace PTSettingsHelper 5 { 6 constexpr inline const wchar_t* settings_filename = L"\\settings.json"; 7 constexpr inline const wchar_t* oobe_filename = L"oobe_settings.json"; 8 constexpr inline const wchar_t* last_version_run_filename = L"last_version_run.json"; 9 constexpr inline const wchar_t* opened_at_first_launch_json_field_name = L"openedAtFirstLaunch"; 10 constexpr inline const wchar_t* last_version_json_field_name = L"last_version"; 11 constexpr inline const wchar_t* DataDiagnosticsRegKey = L"Software\\Classes\\PowerToys"; 12 constexpr inline const wchar_t* DataDiagnosticsRegValueName = L"AllowDataDiagnostics"; 13 14 std::wstring get_root_save_folder_location() 15 { 16 PWSTR local_app_path; 17 winrt::check_hresult(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &local_app_path)); 18 std::wstring result{ local_app_path }; 19 CoTaskMemFree(local_app_path); 20 21 result += L"\\Microsoft\\PowerToys"; 22 std::filesystem::path save_path(result); 23 if (!std::filesystem::exists(save_path)) 24 { 25 std::filesystem::create_directories(save_path); 26 } 27 return result; 28 } 29 30 std::wstring get_local_low_folder_location() 31 { 32 PWSTR local_app_path; 33 winrt::check_hresult(SHGetKnownFolderPath(FOLDERID_LocalAppDataLow, 0, NULL, &local_app_path)); 34 std::wstring result{ local_app_path }; 35 CoTaskMemFree(local_app_path); 36 37 result += L"\\Microsoft\\PowerToys"; 38 std::filesystem::path save_path(result); 39 if (!std::filesystem::exists(save_path)) 40 { 41 std::filesystem::create_directories(save_path); 42 } 43 return result; 44 } 45 46 std::wstring get_module_save_folder_location(std::wstring_view powertoy_key) 47 { 48 std::wstring result = get_root_save_folder_location(); 49 result += L"\\"; 50 result += powertoy_key; 51 std::filesystem::path save_path(result); 52 if (!std::filesystem::exists(save_path)) 53 { 54 std::filesystem::create_directories(save_path); 55 } 56 return result; 57 } 58 59 std::wstring get_module_save_file_location(std::wstring_view powertoy_key) 60 { 61 return get_module_save_folder_location(powertoy_key) + settings_filename; 62 } 63 64 std::wstring get_powertoys_general_save_file_location() 65 { 66 return get_root_save_folder_location() + settings_filename; 67 } 68 69 void save_module_settings(std::wstring_view powertoy_key, json::JsonObject& settings) 70 { 71 const std::wstring save_file_location = get_module_save_file_location(powertoy_key); 72 json::to_file(save_file_location, settings); 73 } 74 75 json::JsonObject load_module_settings(std::wstring_view powertoy_key) 76 { 77 const std::wstring save_file_location = get_module_save_file_location(powertoy_key); 78 auto saved_settings = json::from_file(save_file_location); 79 return saved_settings.has_value() ? std::move(*saved_settings) : json::JsonObject{}; 80 } 81 82 void save_general_settings(const json::JsonObject& settings) 83 { 84 const std::wstring save_file_location = get_powertoys_general_save_file_location(); 85 json::to_file(save_file_location, settings); 86 } 87 88 json::JsonObject load_general_settings() 89 { 90 const std::wstring save_file_location = get_powertoys_general_save_file_location(); 91 auto saved_settings = json::from_file(save_file_location); 92 return saved_settings.has_value() ? std::move(*saved_settings) : json::JsonObject{}; 93 } 94 95 std::wstring get_log_settings_file_location() 96 { 97 std::filesystem::path result(PTSettingsHelper::get_root_save_folder_location()); 98 result = result.append(log_settings_filename); 99 return result.wstring(); 100 } 101 102 bool get_oobe_opened_state() 103 { 104 std::filesystem::path oobePath(PTSettingsHelper::get_root_save_folder_location()); 105 oobePath = oobePath.append(oobe_filename); 106 if (std::filesystem::exists(oobePath)) 107 { 108 auto saved_settings = json::from_file(oobePath.c_str()); 109 if (!saved_settings.has_value()) 110 { 111 return false; 112 } 113 114 bool opened = saved_settings->GetNamedBoolean(opened_at_first_launch_json_field_name, false); 115 return opened; 116 } 117 118 return false; 119 } 120 121 void save_oobe_opened_state() 122 { 123 std::filesystem::path oobePath(PTSettingsHelper::get_root_save_folder_location()); 124 oobePath = oobePath.append(oobe_filename); 125 126 json::JsonObject obj; 127 obj.SetNamedValue(opened_at_first_launch_json_field_name, json::value(true)); 128 129 json::to_file(oobePath.c_str(), obj); 130 } 131 132 std::wstring get_last_version_run() 133 { 134 std::filesystem::path lastVersionRunPath(PTSettingsHelper::get_root_save_folder_location()); 135 lastVersionRunPath = lastVersionRunPath.append(last_version_run_filename); 136 if (std::filesystem::exists(lastVersionRunPath)) 137 { 138 auto saved_settings = json::from_file(lastVersionRunPath.c_str()); 139 if (!saved_settings.has_value()) 140 { 141 return L""; 142 } 143 144 std::wstring last_version = saved_settings->GetNamedString(last_version_json_field_name, L"").c_str(); 145 return last_version; 146 } 147 return L""; 148 } 149 150 void save_last_version_run(const std::wstring& version) 151 { 152 std::filesystem::path lastVersionRunPath(PTSettingsHelper::get_root_save_folder_location()); 153 lastVersionRunPath = lastVersionRunPath.append(last_version_run_filename); 154 155 json::JsonObject obj; 156 obj.SetNamedValue(last_version_json_field_name, json::value(version)); 157 158 json::to_file(lastVersionRunPath.c_str(), obj); 159 } 160 161 void save_data_diagnostics(bool enabled) 162 { 163 HKEY key{}; 164 if (RegCreateKeyExW(HKEY_CURRENT_USER, 165 DataDiagnosticsRegKey, 166 0, 167 nullptr, 168 REG_OPTION_NON_VOLATILE, 169 KEY_ALL_ACCESS, 170 nullptr, 171 &key, 172 nullptr) != ERROR_SUCCESS) 173 { 174 return; 175 } 176 177 const DWORD value = enabled ? 1 : 0; 178 if (RegSetValueExW(key, DataDiagnosticsRegValueName, 0, REG_DWORD, reinterpret_cast<const BYTE*>(&value), sizeof(value)) != ERROR_SUCCESS) 179 { 180 RegCloseKey(key); 181 return; 182 } 183 RegCloseKey(key); 184 } 185 }