HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
user_settings_win32_impl.hpp
1
2#pragma once
3
4#include "user_settings_intf.hpp"
5#include "../win32/win32.hpp"
6#include "../path/path.hpp"
7#include "../metadata/metadata.hpp"
8#include "../macros.hpp"
9#include <format>
10#include <string>
11#include <expected>
12#include <system_error>
13#include <Windows.h>
14#include <winreg.h>
15
16hi_export_module(hikogui.settings.user_settings : impl);
17
18hi_export namespace hi { inline namespace v1 {
19
20[[nodiscard]] inline std::string user_setting_registry_path()
21{
22 return std::format("Software\\{}\\{}", get_application_vendor(), get_application_name());
23}
24
29[[nodiscard]] inline std::expected<std::string, std::error_code> get_user_setting_string(std::string_view name) noexcept
30{
31 // First check the registry of the current-user.
32 if (auto const value = win32_RegGetValue<std::string>(HKEY_CURRENT_USER, user_setting_registry_path(), name)) {
33 return *value;
34 } else if (value.error() != win32_error::file_not_found) {
35 return std::unexpected{std::error_code{value.error()}};
36 }
37
38 // Now check the registry for the local-machine.
39 // These are settings that where made by the Administrator of the machine.
40 if (auto const value = win32_RegGetValue<std::string>(HKEY_LOCAL_MACHINE, user_setting_registry_path(), name)) {
41 return *value;
42 } else {
43 return std::unexpected{std::error_code{value.error()}};
44 }
45}
46
47[[nodiscard]] inline std::expected<long long, std::error_code> get_user_setting_integral(std::string_view name) noexcept
48{
49 // First check the registry of the current-user.
50 if (auto const value = win32_RegGetValue<long long>(HKEY_CURRENT_USER, user_setting_registry_path(), name)) {
51 return *value;
52 } else if (value.error() != win32_error::file_not_found) {
53 return std::unexpected{std::error_code{value.error()}};
54 }
55
56 // Now check the registry for the local-machine.
57 // These are settings that where made by the Administrator of the machine.
58 if (auto const value = win32_RegGetValue<long long>(HKEY_LOCAL_MACHINE, user_setting_registry_path(), name)) {
59 return *value;
60 } else {
61 return std::unexpected{std::error_code{value.error()}};
62 }
63}
64
65inline std::error_code set_user_setting(std::string_view name, std::string_view value) noexcept
66{
67 return win32_RegSetKeyValue(HKEY_CURRENT_USER, user_setting_registry_path(), name, value);
68}
69
70inline std::error_code set_user_setting(std::string_view name, long long value) noexcept
71{
72 return win32_RegSetKeyValue(HKEY_CURRENT_USER, user_setting_registry_path(), name, narrow_cast<uint32_t>(value));
73}
74
75inline std::error_code delete_user_setting(std::string_view name) noexcept
76{
77 return win32_RegDeleteKeyValue(HKEY_CURRENT_USER, user_setting_registry_path(), name);
78}
79
81{
82 return win32_RegDeleteKey(HKEY_CURRENT_USER, user_setting_registry_path());
83}
84
85}}
86
The HikoGUI namespace.
Definition array_generic.hpp:20
win32_error win32_RegSetKeyValue(HKEY key, std::string_view path, std::string_view name, uint32_t value)
Write a DWORD registry value.
Definition winreg.hpp:66
win32_error win32_RegDeleteKeyValue(HKEY key, std::string_view path, std::string_view name) noexcept
Delete a registry value.
Definition winreg.hpp:27
win32_error win32_RegDeleteKey(HKEY key, std::string_view path) noexcept
Delete all registry values and the last part of the subkey.
Definition winreg.hpp:47
std::error_code delete_user_settings() noexcept
Delete all user-setting for the application.
Definition user_settings_win32_impl.hpp:80
std::expected< std::string, std::error_code > get_user_setting_string(std::string_view key) noexcept
Definition user_settings_win32_impl.hpp:29
std::error_code delete_user_setting(std::string_view key) noexcept
Delete a user-setting for the application.
Definition user_settings_win32_impl.hpp:75
std::error_code set_user_setting(std::string_view key, std::string_view value) noexcept
Set a user-setting for the application.
Definition user_settings_win32_impl.hpp:65
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T unexpected(T... args)