1
+ #include " config.hpp"
2
+
3
+ #include " helpers/parse-version.hpp"
4
+
5
+ #include < array>
6
+ #include < windows.h>
7
+
8
+ namespace gelly {
9
+ namespace {
10
+ auto REGISTRY_PARENT = std::string(" SOFTWARE\\ " ) + Config::APP_NAME;
11
+ auto KEY = HKEY_CURRENT_USER;
12
+
13
+ std::optional<std::string> FetchFromGellyRegistry (const std::string &subkey) {
14
+ std::array<char , MAX_PATH> value = {};
15
+ DWORD size = sizeof (value);
16
+
17
+ const auto path = REGISTRY_PARENT;
18
+ if (RegGetValue (KEY, path.c_str (), subkey.c_str (), RRF_RT_REG_SZ, nullptr ,
19
+ value.data (), &size) != ERROR_SUCCESS) {
20
+ return std::nullopt ;
21
+ }
22
+
23
+ std::string valueStr (value.begin (), value.begin () + (size - 1 ));
24
+ return valueStr;
25
+ }
26
+
27
+ void WriteToGellyRegistry (const std::string &subkey, const std::string &value) {
28
+ if (RegSetKeyValue (KEY, REGISTRY_PARENT.c_str (), subkey.c_str (),
29
+ RRF_RT_REG_SZ, value.data (),
30
+ value.size ()) != ERROR_SUCCESS) {
31
+ throw std::runtime_error (" Failed to write to registry" );
32
+ }
33
+ }
34
+ } // namespace
35
+
36
+ optional<std::filesystem::path> Config::GetAppInstallPath () {
37
+ return FetchFromGellyRegistry (" InstallPath" );
38
+ }
39
+
40
+ void Config::SetAppInstallPath (const std::filesystem::path &path) {
41
+ WriteToGellyRegistry (" InstallPath" , path.string ());
42
+ }
43
+
44
+ optional<std::string> Config::GetAppVersion () {
45
+ return FetchFromGellyRegistry (" Version" );
46
+ }
47
+
48
+ void Config::SetAppVersion (const std::string &version) {
49
+ WriteToGellyRegistry (" Version" , version);
50
+ }
51
+
52
+ bool Config::IsAppInstalled () {
53
+ return GetAppInstallPath ().has_value () &&
54
+ std::filesystem::exists (*GetAppInstallPath ());
55
+ }
56
+
57
+ bool Config::IsAppUpToDate () {
58
+ return IsAppInstalled () && (GetAppVersion ().has_value () &&
59
+ *helpers::ParseVersion (*GetAppVersion ()) >=
60
+ *helpers::ParseVersion (APP_VERSION));
61
+ }
62
+ } // namespace gelly
0 commit comments