ReactOS 0.4.16-dev-122-g325d74c
settings.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Functions to load / save settings from reg.
5 * COPYRIGHT: Copyright 2020 He Yang (1160386205@qq.com)
6 */
7
8#include "rapps.h"
9#include "settings.h"
10
11#define SETTINGSSUBKEY L"Software\\ReactOS\\" RAPPS_NAME
12
14{
15 public:
17 {
18 ;
19 }
20 virtual BOOL
22 virtual BOOL
24};
25
27{
28 public:
30 {
31 }
32
33 virtual BOOL
34 Save(CRegKey &key) override
35 {
36 return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == ERROR_SUCCESS;
37 }
38 virtual BOOL
39 Load(CRegKey &key) override
40 {
41 DWORD dwField;
42 LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
43 if (lResult != ERROR_SUCCESS)
44 {
45 return FALSE;
46 }
47 *m_pValueStore = (BOOL)dwField;
48 return TRUE;
49 }
50
51 private:
52 BOOL *m_pValueStore; // where to read/store the value
53 LPCWSTR m_RegName; // key name in registery
54};
55
57{
58 public:
60 {
61 }
62
63 virtual BOOL
64 Save(CRegKey &key) override
65 {
66 return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == ERROR_SUCCESS;
67 }
68 virtual BOOL
69 Load(CRegKey &key) override
70 {
71 DWORD dwField;
72 LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
73 if (lResult != ERROR_SUCCESS)
74 {
75 return FALSE;
76 }
77 *m_pValueStore = (INT)dwField;
78 return TRUE;
79 }
80
81 private:
82 INT *m_pValueStore; // where to read/store the value
83 LPCWSTR m_RegName; // key name in registery
84};
85
87{
88 public:
90 : m_pStringStore(pString), m_StringLen(cchLen), m_RegName(szRegName)
91 {
92 }
93
94 virtual BOOL
95 Save(CRegKey &key) override
96 {
97 return key.SetStringValue(m_RegName, m_pStringStore) == ERROR_SUCCESS;
98 }
99 virtual BOOL
100 Load(CRegKey &key) override
101 {
102 ULONG nChar = m_StringLen - 1; // make sure the terminating L'\0'
103 LONG lResult = key.QueryStringValue(m_RegName, m_pStringStore, &nChar);
104 return lResult == ERROR_SUCCESS;
105 }
106
107 private:
108 WCHAR *m_pStringStore; // where to read/store the value
109 ULONG m_StringLen; // string length, in chars
110 LPCWSTR m_RegName; // key name in registery
111};
112
113static void
115{
116 infoFields.AddTail(new SettingsFieldBool(&(settings.bSaveWndPos), L"bSaveWndPos"));
117 infoFields.AddTail(new SettingsFieldBool(&(settings.bUpdateAtStart), L"bUpdateAtStart"));
118 infoFields.AddTail(new SettingsFieldBool(&(settings.bLogEnabled), L"bLogEnabled"));
119 infoFields.AddTail(new SettingsFieldString(settings.szDownloadDir, MAX_PATH, L"szDownloadDir"));
120 infoFields.AddTail(new SettingsFieldBool(&(settings.bDelInstaller), L"bDelInstaller"));
121 infoFields.AddTail(new SettingsFieldBool(&(settings.Maximized), L"WindowPosMaximized"));
122 infoFields.AddTail(new SettingsFieldInt(&(settings.Left), L"WindowPosLeft"));
123 infoFields.AddTail(new SettingsFieldInt(&(settings.Top), L"WindowPosTop"));
124 infoFields.AddTail(new SettingsFieldInt(&(settings.Width), L"WindowPosWidth"));
125 infoFields.AddTail(new SettingsFieldInt(&(settings.Height), L"WindowPosHeight"));
126 infoFields.AddTail(new SettingsFieldInt(&(settings.Proxy), L"ProxyMode"));
127 infoFields.AddTail(new SettingsFieldString((settings.szProxyServer), MAX_PATH, L"ProxyServer"));
128 infoFields.AddTail(new SettingsFieldString((settings.szNoProxyFor), MAX_PATH, L"NoProxyFor"));
129 infoFields.AddTail(new SettingsFieldBool(&(settings.bUseSource), L"bUseSource"));
130 infoFields.AddTail(new SettingsFieldString((settings.szSourceURL), INTERNET_MAX_URL_LENGTH, L"SourceURL"));
131}
132
133static BOOL
135{
136 BOOL bAllSuccess = TRUE;
138
139 AddInfoFields(infoFields, settings);
140
141 POSITION InfoListPosition = infoFields.GetHeadPosition();
142 while (InfoListPosition)
143 {
144 SettingsField *Info = infoFields.GetNext(InfoListPosition);
145 if (!Info->Save(key))
146 {
147 bAllSuccess = FALSE;
148 // TODO: error log
149 }
150 delete Info;
151 }
152 return bAllSuccess;
153}
154
155static BOOL
157{
158 BOOL bLoadedAny = FALSE;
160
161 AddInfoFields(infoFields, settings);
162
163 POSITION InfoListPosition = infoFields.GetHeadPosition();
164 while (InfoListPosition)
165 {
166 SettingsField *Info = infoFields.GetNext(InfoListPosition);
167 if (Info->Load(key))
168 bLoadedAny = TRUE;
169 //else
170 // TODO: error log
171 delete Info;
172 }
173 return bLoadedAny;
174}
175
176static void
178{
180 {
181 szDownloadDir.ReleaseBuffer();
182 if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
183 {
184 szDownloadDir = L"C:";
185 }
186 }
187 else
188 {
189 szDownloadDir.ReleaseBuffer();
190 }
191
192 PathAppendW(szDownloadDir.GetBuffer(MAX_PATH), L"\\RAPPS Downloads");
193 szDownloadDir.ReleaseBuffer();
194}
195
196static VOID
198{
199 if (!pSettingsInfo->szDownloadDir[0])
200 {
201 CStringW szDownloadDir;
202 GetDefaultDownloadDirectory(szDownloadDir);
203
204 CStringW::CopyChars(pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir),
205 szDownloadDir.GetString(), szDownloadDir.GetLength() + 1);
206 }
207}
208
209VOID
211{
212 ZeroMemory(pSettingsInfo, sizeof(*pSettingsInfo));
213
214 pSettingsInfo->bSaveWndPos = TRUE;
215 pSettingsInfo->bUpdateAtStart = FALSE;
216 pSettingsInfo->bLogEnabled = TRUE;
217 pSettingsInfo->bUseSource = FALSE;
218 pSettingsInfo->bDelInstaller = FALSE;
219 pSettingsInfo->Maximized = FALSE;
220 pSettingsInfo->Left = CW_USEDEFAULT;
221 pSettingsInfo->Top = CW_USEDEFAULT;
222 pSettingsInfo->Width = 680;
223 pSettingsInfo->Height = 450;
224
225 ValidateStringSettings(pSettingsInfo);
226}
227
228BOOL
230{
231 BOOL bLoadedAny = FALSE;
232
233 FillDefaultSettings(pSettingsInfo);
234
235 ATL::CRegKey RegKey;
237 {
238 bLoadedAny = LoadAllSettings(RegKey, *pSettingsInfo);
239 }
240
241 ValidateStringSettings(pSettingsInfo); // Handles the case where a REG_SZ is present but empty
242
243 if (!bLoadedAny)
244 {
245 // This the first launch, write at least one item so ParseCmdAndExecute() does not
246 // trigger another DB update in another process instance between now and SaveSettings().
247 ATL::CRegKey RegKey;
250 {
251 SettingsFieldBool field(&(pSettingsInfo->bUpdateAtStart), L"bUpdateAtStart");
252 field.Save(RegKey);
253 }
254 }
255 return bLoadedAny;
256}
257
258BOOL
260{
262 ATL::CRegKey RegKey;
263
264 if (pSettingsInfo->bSaveWndPos)
265 {
266 wp.length = sizeof(wp);
268
269 pSettingsInfo->Left = wp.rcNormalPosition.left;
270 pSettingsInfo->Top = wp.rcNormalPosition.top;
271 pSettingsInfo->Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
272 pSettingsInfo->Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
273 pSettingsInfo->Maximized =
275 }
276
279 {
280 return FALSE;
281 }
282
283 return SaveAllSettings(RegKey, *pSettingsInfo);
284}
struct mke2fs_defaults settings[]
void SaveSettings(void)
Definition: settings.c:115
void LoadSettings(void)
Definition: settings.c:53
static void AddInfoFields(ATL::CAtlList< SettingsField * > &infoFields, SETTINGS_INFO &settings)
Definition: settings.cpp:114
static void GetDefaultDownloadDirectory(CStringW &szDownloadDir)
Definition: settings.cpp:177
static VOID ValidateStringSettings(PSETTINGS_INFO pSettingsInfo)
Definition: settings.cpp:197
static BOOL LoadAllSettings(CRegKey &key, SETTINGS_INFO &settings)
Definition: settings.cpp:156
#define SETTINGSSUBKEY
Definition: settings.cpp:11
VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
Definition: settings.cpp:210
static BOOL SaveAllSettings(CRegKey &key, SETTINGS_INFO &settings)
Definition: settings.cpp:134
POSITION AddTail(INARGTYPE element)
Definition: atlcoll.h:629
POSITION GetHeadPosition() const
Definition: atlcoll.h:554
E & GetNext(_Inout_ POSITION &pos)
Definition: atlcoll.h:566
LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired=KEY_READ|KEY_WRITE) noexcept
Definition: atlbase.h:1173
LONG Create(HKEY hKeyParent, LPCTSTR lpszKeyName, LPTSTR lpszClass=REG_NONE, DWORD dwOptions=REG_OPTION_NON_VOLATILE, REGSAM samDesired=KEY_READ|KEY_WRITE, LPSECURITY_ATTRIBUTES lpSecAttr=NULL, LPDWORD lpdwDisposition=NULL) noexcept
Definition: atlbase.h:1189
static void __cdecl CopyChars(_Out_writes_to_(nDestLen, nChars) XCHAR *pchDest, _In_ size_t nDestLen, _In_reads_opt_(nChars) const XCHAR *pchSrc, _In_ int nChars) noexcept
Definition: atlsimpstr.h:438
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:387
PXSTR GetString() noexcept
Definition: atlsimpstr.h:367
int GetLength() const noexcept
Definition: atlsimpstr.h:362
SettingsFieldBool(BOOL *pValue, LPCWSTR szRegName)
Definition: settings.cpp:29
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:39
LPCWSTR m_RegName
Definition: settings.cpp:53
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:34
BOOL * m_pValueStore
Definition: settings.cpp:52
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:69
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:64
INT * m_pValueStore
Definition: settings.cpp:82
SettingsFieldInt(INT *pValue, LPCWSTR szRegName)
Definition: settings.cpp:59
LPCWSTR m_RegName
Definition: settings.cpp:83
SettingsFieldString(WCHAR *pString, ULONG cchLen, LPCWSTR szRegName)
Definition: settings.cpp:89
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:100
WCHAR * m_pStringStore
Definition: settings.cpp:108
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:95
virtual ~SettingsField()
Definition: settings.cpp:16
virtual BOOL Load(CRegKey &key)=0
virtual BOOL Save(CRegKey &key)=0
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define MAX_PATH
Definition: compat.h:34
HRESULT WINAPI SHGetFolderPathW(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath)
Definition: shellpath.c:2589
#define INTERNET_MAX_URL_LENGTH
Definition: session.c:1418
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
FxString * pString
PWCHAR pValue
#define FAILED(hr)
Definition: intsafe.h:51
#define BOOL
Definition: nt_native.h:43
#define KEY_READ
Definition: nt_native.h:1023
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_WRITE
Definition: nt_native.h:1031
#define L(x)
Definition: ntvdm.h:50
#define PathAppendW
Definition: pathcch.h:309
long LONG
Definition: pedump.c:60
#define INT
Definition: polytest.cpp:20
@ SHGFP_TYPE_CURRENT
Definition: shlobj.h:2149
#define CSIDL_PERSONAL
Definition: shlobj.h:2178
#define _countof(array)
Definition: sndvol32.h:70
WCHAR szDownloadDir[MAX_PATH]
Definition: settings.h:10
BOOL bUseSource
Definition: settings.h:23
BOOL bLogEnabled
Definition: settings.h:9
BOOL bDelInstaller
Definition: settings.h:11
BOOL Maximized
Definition: settings.h:13
BOOL bUpdateAtStart
Definition: settings.h:8
BOOL bSaveWndPos
Definition: settings.h:7
RECT rcNormalPosition
Definition: winuser.h:3298
Definition: parser.c:44
Definition: copy.c:22
LONG right
Definition: windef.h:308
LONG bottom
Definition: windef.h:309
LONG top
Definition: windef.h:307
LONG left
Definition: windef.h:306
int32_t INT
Definition: typedefs.h:58
uint32_t ULONG
Definition: typedefs.h:59
_Must_inspect_result_ _In_ WDFCHILDLIST _In_ PWDF_CHILD_LIST_ITERATOR _Out_ WDFDEVICE _Inout_opt_ PWDF_CHILD_RETRIEVE_INFO Info
Definition: wdfchildlist.h:690
#define ZeroMemory
Definition: winbase.h:1712
_In_ LONG _In_ HWND hwnd
Definition: winddi.h:4023
#define HKEY_CURRENT_USER
Definition: winreg.h:11
BOOL WINAPI GetWindowPlacement(_In_ HWND, _Inout_ WINDOWPLACEMENT *)
#define SW_SHOWMINIMIZED
Definition: winuser.h:774
#define CW_USEDEFAULT
Definition: winuser.h:225
#define WPF_RESTORETOMAXIMIZED
Definition: winuser.h:2535
#define SW_MAXIMIZE
Definition: winuser.h:775
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185