ReactOS  0.4.15-dev-1150-g593bcce
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:
15  virtual ~SettingsField() { ; }
16  virtual BOOL Save(CRegKey &key) = 0;
17  virtual BOOL Load(CRegKey &key) = 0;
18 };
19 
21 {
22 public:
23  SettingsFieldBool(BOOL *pValue, LPCWSTR szRegName)
24  : m_pValueStore(pValue), m_RegName(szRegName)
25  {
26  }
27 
28  virtual BOOL Save(CRegKey &key) override
29  {
30  return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == ERROR_SUCCESS;
31  }
32  virtual BOOL Load(CRegKey &key) override
33  {
34  DWORD dwField;
35  LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
36  if (lResult != ERROR_SUCCESS)
37  {
38  return FALSE;
39  }
40  *m_pValueStore = (BOOL)dwField;
41  return TRUE;
42  }
43 
44 private:
45  BOOL *m_pValueStore; // where to read/store the value
46  LPCWSTR m_RegName; // key name in registery
47 };
48 
50 {
51 public:
52  SettingsFieldInt(INT *pValue, LPCWSTR szRegName)
53  : m_pValueStore(pValue), m_RegName(szRegName)
54  {
55  }
56 
57  virtual BOOL Save(CRegKey &key) override
58  {
59  return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == ERROR_SUCCESS;
60  }
61  virtual BOOL Load(CRegKey &key) override
62  {
63  DWORD dwField;
64  LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
65  if (lResult != ERROR_SUCCESS)
66  {
67  return FALSE;
68  }
69  *m_pValueStore = (INT)dwField;
70  return TRUE;
71  }
72 
73 private:
74  INT *m_pValueStore; // where to read/store the value
75  LPCWSTR m_RegName; // key name in registery
76 };
77 
79 {
80 public:
81  SettingsFieldString(WCHAR *pString, ULONG cchLen, LPCWSTR szRegName)
82  : m_pStringStore(pString), m_StringLen(cchLen), m_RegName(szRegName)
83  {
84  }
85 
86  virtual BOOL Save(CRegKey &key) override
87  {
88  return key.SetStringValue(m_RegName, m_pStringStore) == ERROR_SUCCESS;
89  }
90  virtual BOOL Load(CRegKey &key) override
91  {
92  ULONG nChar = m_StringLen - 1; // make sure the terminating L'\0'
93  LONG lResult = key.QueryStringValue(m_RegName, m_pStringStore, &nChar);
94  return lResult == ERROR_SUCCESS;
95  }
96 
97 private:
98  WCHAR *m_pStringStore; // where to read/store the value
99  ULONG m_StringLen; // string length, in chars
100  LPCWSTR m_RegName; // key name in registery
101 };
102 
103 
105 {
106  infoFields.AddTail(new SettingsFieldBool(&(settings.bSaveWndPos), L"bSaveWndPos"));
107  infoFields.AddTail(new SettingsFieldBool(&(settings.bUpdateAtStart), L"bUpdateAtStart"));
108  infoFields.AddTail(new SettingsFieldBool(&(settings.bLogEnabled), L"bLogEnabled"));
109  infoFields.AddTail(new SettingsFieldString(settings.szDownloadDir, MAX_PATH, L"szDownloadDir"));
110  infoFields.AddTail(new SettingsFieldBool(&(settings.bDelInstaller), L"bDelInstaller"));
111  infoFields.AddTail(new SettingsFieldBool(&(settings.Maximized), L"WindowPosMaximized"));
112  infoFields.AddTail(new SettingsFieldInt(&(settings.Left), L"WindowPosLeft"));
113  infoFields.AddTail(new SettingsFieldInt(&(settings.Top), L"WindowPosTop"));
114  infoFields.AddTail(new SettingsFieldInt(&(settings.Width), L"WindowPosWidth"));
115  infoFields.AddTail(new SettingsFieldInt(&(settings.Height), L"WindowPosHeight"));
116  infoFields.AddTail(new SettingsFieldInt(&(settings.Proxy), L"ProxyMode"));
117  infoFields.AddTail(new SettingsFieldString((settings.szProxyServer), MAX_PATH, L"ProxyServer"));
118  infoFields.AddTail(new SettingsFieldString((settings.szNoProxyFor), MAX_PATH, L"NoProxyFor"));
119  infoFields.AddTail(new SettingsFieldBool(&(settings.bUseSource), L"bUseSource"));
120  infoFields.AddTail(new SettingsFieldString((settings.szSourceURL), INTERNET_MAX_URL_LENGTH, L"SourceURL"));
121 
122  return;
123 }
124 
126 {
127  BOOL bAllSuccess = TRUE;
129 
130  AddInfoFields(infoFields, settings);
131 
132  POSITION InfoListPosition = infoFields.GetHeadPosition();
133  while (InfoListPosition)
134  {
135  SettingsField *Info = infoFields.GetNext(InfoListPosition);
136  if (!Info->Save(key))
137  {
138  bAllSuccess = FALSE;
139  // TODO: error log
140  }
141  delete Info;
142  }
143  return bAllSuccess;
144 }
145 
147 {
148  BOOL bAllSuccess = TRUE;
150 
151  AddInfoFields(infoFields, settings);
152 
153  POSITION InfoListPosition = infoFields.GetHeadPosition();
154  while (InfoListPosition)
155  {
156  SettingsField *Info = infoFields.GetNext(InfoListPosition);
157  if (!Info->Load(key))
158  {
159  bAllSuccess = FALSE;
160  // TODO: error log
161  }
162  delete Info;
163  }
164  return bAllSuccess;
165 }
166 
168 {
169  ATL::CStringW szDownloadDir;
170  ZeroMemory(pSettingsInfo, sizeof(SETTINGS_INFO));
171 
172  pSettingsInfo->bSaveWndPos = TRUE;
173  pSettingsInfo->bUpdateAtStart = FALSE;
174  pSettingsInfo->bLogEnabled = TRUE;
175  pSettingsInfo->bUseSource = FALSE;
176 
178  {
179  szDownloadDir.ReleaseBuffer();
180  if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
181  {
182  szDownloadDir = L"C:";
183  }
184  }
185  else
186  {
187  szDownloadDir.ReleaseBuffer();
188  }
189 
190  PathAppendW(szDownloadDir.GetBuffer(MAX_PATH), L"\\RAPPS Downloads");
191  szDownloadDir.ReleaseBuffer();
192 
194  _countof(pSettingsInfo->szDownloadDir),
195  szDownloadDir.GetString(),
196  szDownloadDir.GetLength() + 1);
197 
198  pSettingsInfo->bDelInstaller = FALSE;
199  pSettingsInfo->Maximized = FALSE;
200  pSettingsInfo->Left = CW_USEDEFAULT;
201  pSettingsInfo->Top = CW_USEDEFAULT;
202  pSettingsInfo->Width = 680;
203  pSettingsInfo->Height = 450;
204 }
205 
207 {
208  ATL::CRegKey RegKey;
209  if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", KEY_READ) != ERROR_SUCCESS)
210  {
211  return FALSE;
212  }
213 
214  return LoadAllSettings(RegKey, *pSettingsInfo);
215 }
216 
218 {
219  WINDOWPLACEMENT wp;
220  ATL::CRegKey RegKey;
221 
222  if (pSettingsInfo->bSaveWndPos)
223  {
224  wp.length = sizeof(wp);
225  GetWindowPlacement(hwnd, &wp);
226 
227  pSettingsInfo->Left = wp.rcNormalPosition.left;
228  pSettingsInfo->Top = wp.rcNormalPosition.top;
229  pSettingsInfo->Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
230  pSettingsInfo->Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
231  pSettingsInfo->Maximized = (wp.showCmd == SW_MAXIMIZE
232  || (wp.showCmd == SW_SHOWMINIMIZED
233  && (wp.flags & WPF_RESTORETOMAXIMIZED)));
234  }
235 
236  if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", NULL,
238  {
239  return FALSE;
240  }
241 
242  return SaveAllSettings(RegKey, *pSettingsInfo);
243 }
int GetLength() const
Definition: atlsimpstr.h:356
struct mke2fs_defaults settings[]
VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
Definition: settings.cpp:167
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:57
BOOL bUpdateAtStart
Definition: settings.h:8
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
POSITION GetHeadPosition() const
Definition: atlcoll.h:549
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:61
BOOL LoadSettings(PSETTINGS_INFO pSettingsInfo)
Definition: settings.cpp:206
virtual BOOL Load(CRegKey &key)=0
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define KEY_READ
Definition: nt_native.h:1023
#define TRUE
Definition: types.h:120
void AddInfoFields(ATL::CAtlList< SettingsField * > &infoFields, SETTINGS_INFO &settings)
Definition: settings.cpp:104
virtual ~SettingsField()
Definition: settings.cpp:15
#define HKEY_CURRENT_USER
Definition: winreg.h:11
BOOL SaveAllSettings(CRegKey &key, SETTINGS_INFO &settings)
Definition: settings.cpp:125
#define INT
Definition: polytest.cpp:20
SettingsFieldInt(INT *pValue, LPCWSTR szRegName)
Definition: settings.cpp:52
LONG top
Definition: windef.h:307
#define ZeroMemory
Definition: winbase.h:1648
#define WPF_RESTORETOMAXIMIZED
Definition: winuser.h:2507
LPCWSTR m_RegName
Definition: settings.cpp:46
LONG left
Definition: windef.h:306
#define SW_SHOWMINIMIZED
Definition: winuser.h:765
LONG right
Definition: windef.h:308
BOOL Maximized
Definition: settings.h:13
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:32
#define BOOL
Definition: nt_native.h:43
int32_t INT
Definition: typedefs.h:58
struct TraceInfo Info
BOOL WINAPI PathAppendW(LPWSTR lpszPath, LPCWSTR lpszAppend)
Definition: path.c:121
WCHAR szDownloadDir[MAX_PATH]
Definition: settings.h:10
BOOL bUseSource
Definition: settings.h:23
LPCWSTR m_RegName
Definition: settings.cpp:75
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:372
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
long LONG
Definition: pedump.c:60
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:86
#define CW_USEDEFAULT
Definition: winuser.h:225
smooth NULL
Definition: ftsmooth.c:416
#define INTERNET_MAX_URL_LENGTH
Definition: session.c:1418
virtual BOOL Save(CRegKey &key)=0
BOOL WINAPI GetWindowPlacement(_In_ HWND, _Inout_ WINDOWPLACEMENT *)
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
#define KEY_WRITE
Definition: nt_native.h:1031
E & GetNext(_Inout_ POSITION &pos)
Definition: atlcoll.h:561
BOOL SaveSettings(HWND hwnd, PSETTINGS_INFO pSettingsInfo)
Definition: settings.cpp:217
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define _countof(array)
Definition: sndvol32.h:68
#define MAX_PATH
Definition: compat.h:34
PCXSTR GetString() const
Definition: atlsimpstr.h:361
LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired=KEY_READ|KEY_WRITE)
Definition: atlbase.h:1070
unsigned long DWORD
Definition: ntddk_ex.h:95
virtual BOOL Save(CRegKey &key) override
Definition: settings.cpp:28
BOOL LoadAllSettings(CRegKey &key, SETTINGS_INFO &settings)
Definition: settings.cpp:146
static const WCHAR L[]
Definition: oid.c:1250
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)
Definition: atlbase.h:1091
SettingsFieldString(WCHAR *pString, ULONG cchLen, LPCWSTR szRegName)
Definition: settings.cpp:81
SettingsFieldBool(BOOL *pValue, LPCWSTR szRegName)
Definition: settings.cpp:23
virtual BOOL Load(CRegKey &key) override
Definition: settings.cpp:90
POSITION AddTail(INARGTYPE element)
Definition: atlcoll.h:624
BOOL bLogEnabled
Definition: settings.h:9
INT * m_pValueStore
Definition: settings.cpp:74
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)
Definition: atlsimpstr.h:423
BOOL bDelInstaller
Definition: settings.h:11
RECT rcNormalPosition
Definition: winuser.h:3270
#define CSIDL_PERSONAL
Definition: shlobj.h:2017
HRESULT WINAPI SHGetFolderPathW(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath)
Definition: shellpath.c:2208
BOOL bSaveWndPos
Definition: settings.h:7
WCHAR * m_pStringStore
Definition: settings.cpp:98
unsigned int ULONG
Definition: retypes.h:1
LONG bottom
Definition: windef.h:309
#define SW_MAXIMIZE
Definition: winuser.h:766
Definition: path.c:41
BOOL * m_pValueStore
Definition: settings.cpp:45