[RAPPS] settings read/write refactor (#3101)

* [RAPPS] move settings-related stuff to a separate file
* [RAPPS] refactor reg read/write
* [RAPPS] add params for Load/SaveSettings, no longer use global vars
This commit is contained in:
He Yang 2020-08-29 20:32:59 +08:00 committed by Mark Jansen
parent ce0110195b
commit e1974d21dc
No known key found for this signature in database
GPG key ID: B39240EE84BEAE8B
7 changed files with 284 additions and 114 deletions

View file

@ -10,6 +10,8 @@
#include "unattended.h"
#include "winmain.h"
#include <atlcom.h>
#include <gdiplus.h>
@ -61,88 +63,6 @@ VOID InitializeGDIPlus(BOOL bInitialize)
}
}
VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
{
ATL::CStringW szDownloadDir;
ZeroMemory(pSettingsInfo, sizeof(SETTINGS_INFO));
pSettingsInfo->bSaveWndPos = TRUE;
pSettingsInfo->bUpdateAtStart = FALSE;
pSettingsInfo->bLogEnabled = TRUE;
pSettingsInfo->bUseSource = FALSE;
if (FAILED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szDownloadDir.GetBuffer(MAX_PATH))))
{
szDownloadDir.ReleaseBuffer();
if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
{
szDownloadDir = L"C:";
}
}
else
{
szDownloadDir.ReleaseBuffer();
}
PathAppendW(szDownloadDir.GetBuffer(MAX_PATH), L"\\RAPPS Downloads");
szDownloadDir.ReleaseBuffer();
ATL::CStringW::CopyChars(pSettingsInfo->szDownloadDir,
_countof(pSettingsInfo->szDownloadDir),
szDownloadDir.GetString(),
szDownloadDir.GetLength() + 1);
pSettingsInfo->bDelInstaller = FALSE;
pSettingsInfo->Maximized = FALSE;
pSettingsInfo->Left = CW_USEDEFAULT;
pSettingsInfo->Top = CW_USEDEFAULT;
pSettingsInfo->Width = 680;
pSettingsInfo->Height = 450;
}
static BOOL LoadSettings()
{
ATL::CRegKey RegKey;
DWORD dwSize;
BOOL bResult = FALSE;
if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", KEY_READ) == ERROR_SUCCESS)
{
dwSize = sizeof(SettingsInfo);
bResult = (RegKey.QueryBinaryValue(L"Settings", (PVOID) &SettingsInfo, &dwSize) == ERROR_SUCCESS);
RegKey.Close();
}
return bResult;
}
VOID SaveSettings(HWND hwnd)
{
WINDOWPLACEMENT wp;
ATL::CRegKey RegKey;
if (SettingsInfo.bSaveWndPos)
{
wp.length = sizeof(wp);
GetWindowPlacement(hwnd, &wp);
SettingsInfo.Left = wp.rcNormalPosition.left;
SettingsInfo.Top = wp.rcNormalPosition.top;
SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE
|| (wp.showCmd == SW_SHOWMINIMIZED
&& (wp.flags & WPF_RESTORETOMAXIMIZED)));
}
if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL) == ERROR_SUCCESS)
{
RegKey.SetBinaryValue(L"Settings", (const PVOID) &SettingsInfo, sizeof(SettingsInfo));
RegKey.Close();
}
}
int wmain(int argc, wchar_t *argv[])
{
BOOL bIsFirstLaunch;
@ -157,7 +77,7 @@ int wmain(int argc, wchar_t *argv[])
hInst = GetModuleHandle(NULL);
bIsFirstLaunch = !LoadSettings();
bIsFirstLaunch = !LoadSettings(&SettingsInfo);
if (bIsFirstLaunch)
{
FillDefaultSettings(&SettingsInfo);