This repository was archived by the owner on Jun 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppSettings.cpp
More file actions
172 lines (146 loc) · 7.51 KB
/
Copy pathAppSettings.cpp
File metadata and controls
172 lines (146 loc) · 7.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "pch.h"
#include "AppSettings.h"
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Data.Json.h>
#include <shlobj.h>
#include <fstream>
#include <filesystem>
using namespace winrt::Windows::Data::Json;
namespace Aegis::Settings
{
namespace
{
std::wstring GetLocalAppDataPath()
{
wchar_t path[MAX_PATH]{};
if (SUCCEEDED(SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, path)))
return std::wstring(path) + L"\\Aegis";
return L"";
}
std::wstring GetStr(JsonObject const& obj, std::wstring_view key, std::wstring_view def)
{
winrt::hstring k{ key };
if (obj.HasKey(k) && obj.GetNamedValue(k).ValueType() == JsonValueType::String)
return std::wstring(obj.GetNamedString(k));
return std::wstring(def);
}
int GetInt(JsonObject const& obj, std::wstring_view key, int def)
{
winrt::hstring k{ key };
if (obj.HasKey(k) && obj.GetNamedValue(k).ValueType() == JsonValueType::Number)
return static_cast<int>(obj.GetNamedNumber(k));
return def;
}
bool GetBool(JsonObject const& obj, std::wstring_view key, bool def)
{
winrt::hstring k{ key };
if (obj.HasKey(k) && obj.GetNamedValue(k).ValueType() == JsonValueType::Boolean)
return obj.GetNamedBoolean(k);
return def;
}
}
SettingsService& SettingsService::GetInstance()
{
static SettingsService instance;
return instance;
}
std::wstring SettingsService::GetSettingsPath() const
{
if (!m_testSettingsPath.empty()) return m_testSettingsPath;
return GetLocalAppDataPath() + L"\\settings.json";
}
void SettingsService::SetSettingsPathForTesting(std::wstring path)
{
m_testSettingsPath = std::move(path);
}
void SettingsService::ClearSettingsPathForTesting()
{
m_testSettingsPath.clear();
}
AppSettings SettingsService::Load()
{
AppSettings s;
std::wstring path = GetSettingsPath();
std::ifstream file(path);
if (!file.is_open()) return s;
std::string raw((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
if (raw.empty()) return s;
std::wstring wide(raw.begin(), raw.end());
JsonObject obj;
if (!JsonObject::TryParse(wide, obj)) return s;
s.SessionLockTimeoutMinutes = GetInt(obj, L"SessionLockTimeoutMinutes", 15);
s.SettingsSchemaVersion = GetInt(obj, L"SettingsSchemaVersion", 1);
s.LockOnMinimize = GetBool(obj, L"LockOnMinimize", false);
s.RequireConfirmationForPrivateKeyExport = GetBool(obj, L"RequireConfirmationForPrivateKeyExport", true);
s.ClearClipboardAfterDelay = GetBool(obj, L"ClearClipboardAfterDelay", true);
s.ClipboardClearDelaySeconds = GetInt(obj, L"ClipboardClearDelaySeconds", 30);
s.WarnOnRetiredKeyUse = GetBool(obj, L"WarnOnRetiredKeyUse", true);
s.WarnOnExperimentalKeyUse = GetBool(obj, L"WarnOnExperimentalKeyUse", true);
s.DefaultPQAlgorithm = GetStr(obj, L"DefaultPQAlgorithm", L"ML-KEM-768");
s.DefaultTraditionalAlgorithm = GetStr(obj, L"DefaultTraditionalAlgorithm", L"Ed25519");
s.RememberedBackupFolder = GetStr(obj, L"RememberedBackupFolder", L"");
s.BackendPaths.GnuPg = L"";
s.BackendPaths.OpenSsl = L"";
s.BackendPaths.OqsProvider = L"";
winrt::hstring pathsKey{ L"BackendPaths" };
if (obj.HasKey(pathsKey) && obj.GetNamedValue(pathsKey).ValueType() == JsonValueType::Object)
{
JsonObject pathsObj = obj.GetNamedObject(pathsKey);
s.BackendPaths.GnuPg = GetStr(pathsObj, L"GnuPg", L"");
s.BackendPaths.OpenSsl = GetStr(pathsObj, L"OpenSsl", L"");
s.BackendPaths.OqsProvider = GetStr(pathsObj, L"OqsProvider", L"");
}
s.BlockedPrivateExportFormats.clear();
winrt::hstring blockedKey{ L"BlockedPrivateExportFormats" };
if (obj.HasKey(blockedKey) && obj.GetNamedValue(blockedKey).ValueType() == JsonValueType::Array)
{
auto arr = obj.GetNamedArray(blockedKey);
for (auto const& item : arr)
{
if (item.ValueType() == JsonValueType::String)
s.BlockedPrivateExportFormats.push_back(std::wstring(item.GetString()));
}
}
return s;
}
bool SettingsService::Save(AppSettings const& s)
{
std::wstring settingsPath = GetSettingsPath();
std::wstring dir = std::filesystem::path(settingsPath).parent_path().wstring();
if (dir.empty()) dir = GetLocalAppDataPath();
std::error_code ec;
std::filesystem::create_directories(dir, ec);
if (ec) return false;
JsonObject obj;
obj.SetNamedValue(L"SettingsSchemaVersion", JsonValue::CreateNumberValue(s.SettingsSchemaVersion));
obj.SetNamedValue(L"SessionLockTimeoutMinutes", JsonValue::CreateNumberValue(s.SessionLockTimeoutMinutes));
obj.SetNamedValue(L"LockOnMinimize", JsonValue::CreateBooleanValue(s.LockOnMinimize));
obj.SetNamedValue(L"RequireConfirmationForPrivateKeyExport", JsonValue::CreateBooleanValue(s.RequireConfirmationForPrivateKeyExport));
obj.SetNamedValue(L"ClearClipboardAfterDelay", JsonValue::CreateBooleanValue(s.ClearClipboardAfterDelay));
obj.SetNamedValue(L"ClipboardClearDelaySeconds", JsonValue::CreateNumberValue(s.ClipboardClearDelaySeconds));
obj.SetNamedValue(L"WarnOnRetiredKeyUse", JsonValue::CreateBooleanValue(s.WarnOnRetiredKeyUse));
obj.SetNamedValue(L"WarnOnExperimentalKeyUse", JsonValue::CreateBooleanValue(s.WarnOnExperimentalKeyUse));
obj.SetNamedValue(L"DefaultPQAlgorithm", JsonValue::CreateStringValue(s.DefaultPQAlgorithm));
obj.SetNamedValue(L"DefaultTraditionalAlgorithm", JsonValue::CreateStringValue(s.DefaultTraditionalAlgorithm));
obj.SetNamedValue(L"RememberedBackupFolder", JsonValue::CreateStringValue(s.RememberedBackupFolder));
JsonObject pathsObj;
pathsObj.SetNamedValue(L"GnuPg", JsonValue::CreateStringValue(s.BackendPaths.GnuPg));
pathsObj.SetNamedValue(L"OpenSsl", JsonValue::CreateStringValue(s.BackendPaths.OpenSsl));
pathsObj.SetNamedValue(L"OqsProvider", JsonValue::CreateStringValue(s.BackendPaths.OqsProvider));
obj.SetNamedValue(L"BackendPaths", pathsObj);
JsonArray blockedArr;
for (auto const& fmt : s.BlockedPrivateExportFormats)
blockedArr.Append(JsonValue::CreateStringValue(fmt));
obj.SetNamedValue(L"BlockedPrivateExportFormats", blockedArr);
std::wstring json = obj.Stringify().c_str();
std::string narrow(json.begin(), json.end());
std::wstring tmp = settingsPath + L".tmp";
{
std::ofstream out(tmp, std::ios::binary | std::ios::trunc);
if (!out.is_open()) return false;
out.write(narrow.data(), narrow.size());
}
return MoveFileExW(tmp.c_str(), settingsPath.c_str(), MOVEFILE_REPLACE_EXISTING) != 0;
}
}