ReactOS 0.4.15-dev-7674-gc0b4db1
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
13{
14 public:
16 {
17 ;
18 }
19 virtual BOOL
21 virtual BOOL
23};
24
26{
27 public:
29 {
30 }
31
32 virtual BOOL
33 Save(CRegKey &key) override
34 {
35 return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == ERROR_SUCCESS;
36 }
37 virtual BOOL
38 Load(CRegKey &key) override
39 {
40 DWORD dwField;
41 LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
42 if (lResult != ERROR_SUCCESS)
43 {
44 return FALSE;
45 }
46 *m_pValueStore = (BOOL)dwField;
47 return TRUE;
48 }
49
50 private:
51 BOOL *m_pValueStore; // where to read/store the value
52 LPCWSTR m_RegName; // key name in registery
53};
54
56{
57 public:
59 {
60 }
61
62 virtual BOOL
63 Save(CRegKey &key) override
64 {
65 return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == ERROR_SUCCESS;
66 }
67 virtual BOOL
68 Load(CRegKey &key) override
69 {
70 DWORD dwField;
71 LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
72 if (lResult != ERROR_SUCCESS)
73 {
74 return FALSE;
75 }
76 *m_pValueStore = (INT)dwField;
77 return TRUE;
78 }
79
80 private:
81 INT *m_pValueStore; // where to read/store the value
82 LPCWSTR m_RegName; // key name in registery
83};
84
86{
87 public:
89 : m_pStringStore(pString), m_StringLen(cchLen), m_RegName(szRegName)
90 {
91 }
92
93 virtual BOOL
94 Save(CRegKey &key) override
95 {
96 return key.SetStringValue(m_RegName, m_pStringStore) == ERROR_SUCCESS;
97 }
98 virtual BOOL
99 Load(CRegKey &key) override
100 {
101 ULONG nChar = m_StringLen - 1; // make sure the terminating L'\0'
102 LONG lResult = key.QueryStringValue(m_RegName, m_pStringStore, &nChar);
103 return lResult == ERROR_SUCCESS;
104 }
105
106 private:
107 WCHAR *m_pStringStore; // where to read/store the value
108 ULONG m_StringLen; // string length, in chars
109 LPCWSTR m_RegName; // key name in registery
110};
111
112void
114{
115 infoFields.AddTail(new SettingsFieldBool(&(settings.bSaveWndPos), L"bSaveWndPos"));
116 infoFields.AddTail(new SettingsFieldBool(&(settings.bUpdateAtStart), L"bUpdateAtStart"));
117 infoFields.AddTail(new SettingsFieldBool(&(settings.bLogEnabled), L"bLogEnabled"));
118 infoFields.AddTail(new SettingsFieldString(settings.szDownloadDir, MAX_PATH, L"szDownloadDir"));
119 infoFields.AddTail(new SettingsFieldBool(&(settings.bDelInstaller), L"bDelInstaller"));
120 infoFields.AddTail(new SettingsFieldBool(&(settings.Maximized), L"WindowPosMaximized"));
121 infoFields.AddTail(new SettingsFieldInt(&(settings.Left), L"WindowPosLeft"));
122 infoFields.AddTail(new SettingsFieldInt(&(settings.Top), L"WindowPosTop"));
123 infoFields.AddTail(new SettingsFieldInt(&(settings.Width), L"WindowPosWidth"));
124 infoFields.AddTail(new SettingsFieldInt(&(settings.Height), L"WindowPosHeight"));
125 infoFields.AddTail(new SettingsFieldInt(&(settings.Proxy), L"ProxyMode"));
126 infoFields.AddTail(new SettingsFieldString((settings.szProxyServer), MAX_PATH, L"ProxyServer"));
127 infoFields.AddTail(new SettingsFieldString((settings.szNoProxyFor), MAX_PATH, L"NoProxyFor"));
128 infoFields.AddTail(new SettingsFieldBool(&(settings.bUseSource), L"bUseSource"));
129 infoFields.AddTail(new SettingsFieldString((settings.szSourceURL), INTERNET_MAX_URL_LENGTH, L"SourceURL"));
130
131 return;
132}
133
134BOOL
136{
137 BOOL bAllSuccess = TRUE;
139
140 AddInfoFields(infoFields, settings);
141
142 POSITION InfoListPosition = infoFields.GetHeadPosition();
143 while (InfoListPosition)
144 {
145 SettingsField *Info = infoFields.GetNext(InfoListPosition);
146 if (!Info->Save(key))
147 {
148 bAllSuccess = FALSE;
149 // TODO: error log
150 }
151 delete Info;
152 }
153 return bAllSuccess;
154}
155
156BOOL
158{
159 BOOL bAllSuccess = TRUE;
161
162 AddInfoFields(infoFields, settings);
163
164 POSITION InfoListPosition = infoFields.GetHeadPosition();
165 while (InfoListPosition)
166 {
167 SettingsField *Info = infoFields.GetNext(InfoListPosition);
168 if (!Info->Load(key))
169 {
170 bAllSuccess = FALSE;
171 // TODO: error log
172 }
173 delete Info;
174 }
175 return bAllSuccess;
176}
177
178VOID
180{
181 CStringW szDownloadDir;
182 ZeroMemory(pSettingsInfo, sizeof(SETTINGS_INFO));
183
184 pSettingsInfo->bSaveWndPos = TRUE;
185 pSettingsInfo->bUpdateAtStart = FALSE;
186 pSettingsInfo->bLogEnabled = TRUE;
187 pSettingsInfo->bUseSource = FALSE;
188
190 {
191 szDownloadDir.ReleaseBuffer();
192 if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
193 {
194 szDownloadDir = L"C:";
195 }
196 }
197 else
198 {
199 szDownloadDir.ReleaseBuffer();
200 }
201
202 PathAppendW(szDownloadDir.GetBuffer(MAX_PATH), L"\\RAPPS Downloads");
203 szDownloadDir.ReleaseBuffer();
204
206 pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir), szDownloadDir.GetString(),
207 szDownloadDir.GetLength() + 1);
208
209 pSettingsInfo->bDelInstaller = FALSE;
210 pSettingsInfo->Maximized = FALSE;
211 pSettingsInfo->Left = CW_USEDEFAULT;
212 pSettingsInfo->Top = CW_USEDEFAULT;
213 pSettingsInfo->Width = 680;
214 pSettingsInfo->Height = 450;
215}
216
217BOOL
219{
220 ATL::CRegKey RegKey;
221 if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\" RAPPS_NAME, KEY_READ) != ERROR_SUCCESS)
222 {
223 return FALSE;
224 }
225
226 return LoadAllSettings(RegKey, *pSettingsInfo);
227}
228
229BOOL
231{
233 ATL::CRegKey RegKey;
234
235 if (pSettingsInfo->bSaveWndPos)
236 {
237 wp.length = sizeof(wp);
239
240 pSettingsInfo->Left = wp.rcNormalPosition.left;
241 pSettingsInfo->Top = wp.rcNormalPosition.top;
242 pSettingsInfo->Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
243 pSettingsInfo->Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
244 pSettingsInfo->Maximized =
246 }
247
248 if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\" RAPPS_NAME, NULL,
250 {
251 return FALSE;
252 }
253
254 return SaveAllSettings(RegKey, *pSettingsInfo);
255}
struct mke2fs_defaults settings[]
void SaveSettings(void)
Definition: settings.c:115
void LoadSettings(void)
Definition: settings.c:53
#define RAPPS_NAME
Definition: defines.h:34
BOOL SaveAllSettings(CRegKey &key, SETTINGS_INFO &settings)
Definition: settings.cpp:135
void AddInfoFields(ATL::CAtlList< SettingsField * > &infoFields, SETTINGS_INFO &settings)
Definition: settings.cpp:113
BOOL LoadAllSettings(CRegKey &key, SETTINGS_INFO &settings)
Definition: settings.cpp:157
VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
Definition: settings.cpp:179
POSITION AddTail(INARGTYPE element)
Definition: atlcoll.h:626
POSITION GetHeadPosition() const
Definition: atlcoll.h:551
E & GetNext(_Inout_ POSITION &pos)
Definition: atlcoll.h:563
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:433
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:382
PXSTR GetString() noexcept
Definition: atlsimpstr.h:367
int GetLength() const noexcept
Definition: atlsimpstr.h:362
SettingsFieldBool(BOOL *pValue, LPCWSTR szRegName)
Definition: settings.cpp:28
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:38
LPCWSTR m_RegName
Definition: settings.cpp:52
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:33
BOOL * m_pValueStore
Definition: settings.cpp:51
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:68
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:63
INT * m_pValueStore
Definition: settings.cpp:81
SettingsFieldInt(INT *pValue, LPCWSTR szRegName)
Definition: settings.cpp:58
LPCWSTR m_RegName
Definition: settings.cpp:82
SettingsFieldString(WCHAR *pString, ULONG cchLen, LPCWSTR szRegName)
Definition: settings.cpp:88
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:99
WCHAR * m_pStringStore
Definition: settings.cpp:107
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:94
virtual ~SettingsField()
Definition: settings.cpp:15
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:2558
#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:2067
#define CSIDL_PERSONAL
Definition: shlobj.h:2096
#define _countof(array)
Definition: sndvol32.h:68
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:3294
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:770
#define CW_USEDEFAULT
Definition: winuser.h:225
#define WPF_RESTORETOMAXIMIZED
Definition: winuser.h:2531
#define SW_MAXIMIZE
Definition: winuser.h:771
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185