From 301675c112f65de2ae1f1cc0a1378ecb90df9613 Mon Sep 17 00:00:00 2001 From: Whindmar Saksit Date: Mon, 16 Sep 2024 00:51:53 +0200 Subject: [PATCH] [RAPPS] Respect partial settings configuration (#7247) Instead of defaulting all settings if any setting is missing, respect any setting that is already set and default the rest. --- base/applications/rapps/settings.cpp | 89 ++++++++++++++++++---------- base/applications/rapps/winmain.cpp | 5 -- 2 files changed, 59 insertions(+), 35 deletions(-) diff --git a/base/applications/rapps/settings.cpp b/base/applications/rapps/settings.cpp index b0a0c191ebd..b23c2dd7a49 100644 --- a/base/applications/rapps/settings.cpp +++ b/base/applications/rapps/settings.cpp @@ -8,6 +8,7 @@ #include "rapps.h" #include "settings.h" +#define SETTINGSSUBKEY L"Software\\ReactOS\\" RAPPS_NAME class SettingsField { @@ -109,7 +110,7 @@ class SettingsFieldString : public SettingsField LPCWSTR m_RegName; // key name in registery }; -void +static void AddInfoFields(ATL::CAtlList &infoFields, SETTINGS_INFO &settings) { infoFields.AddTail(new SettingsFieldBool(&(settings.bSaveWndPos), L"bSaveWndPos")); @@ -127,11 +128,9 @@ AddInfoFields(ATL::CAtlList &infoFields, SETTINGS_INFO &setting infoFields.AddTail(new SettingsFieldString((settings.szNoProxyFor), MAX_PATH, L"NoProxyFor")); infoFields.AddTail(new SettingsFieldBool(&(settings.bUseSource), L"bUseSource")); infoFields.AddTail(new SettingsFieldString((settings.szSourceURL), INTERNET_MAX_URL_LENGTH, L"SourceURL")); - - return; } -BOOL +static BOOL SaveAllSettings(CRegKey &key, SETTINGS_INFO &settings) { BOOL bAllSuccess = TRUE; @@ -153,10 +152,10 @@ SaveAllSettings(CRegKey &key, SETTINGS_INFO &settings) return bAllSuccess; } -BOOL +static BOOL LoadAllSettings(CRegKey &key, SETTINGS_INFO &settings) { - BOOL bAllSuccess = TRUE; + BOOL bLoadedAny = FALSE; ATL::CAtlList infoFields; AddInfoFields(infoFields, settings); @@ -165,27 +164,18 @@ LoadAllSettings(CRegKey &key, SETTINGS_INFO &settings) while (InfoListPosition) { SettingsField *Info = infoFields.GetNext(InfoListPosition); - if (!Info->Load(key)) - { - bAllSuccess = FALSE; - // TODO: error log - } + if (Info->Load(key)) + bLoadedAny = TRUE; + //else + // TODO: error log delete Info; } - return bAllSuccess; + return bLoadedAny; } -VOID -FillDefaultSettings(PSETTINGS_INFO pSettingsInfo) +static void +GetDefaultDownloadDirectory(CStringW &szDownloadDir) { - 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(); @@ -201,29 +191,68 @@ FillDefaultSettings(PSETTINGS_INFO pSettingsInfo) PathAppendW(szDownloadDir.GetBuffer(MAX_PATH), L"\\RAPPS Downloads"); szDownloadDir.ReleaseBuffer(); +} - CStringW::CopyChars( - pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir), szDownloadDir.GetString(), - szDownloadDir.GetLength() + 1); +static VOID +ValidateStringSettings(PSETTINGS_INFO pSettingsInfo) +{ + if (!pSettingsInfo->szDownloadDir[0]) + { + CStringW szDownloadDir; + GetDefaultDownloadDirectory(szDownloadDir); + CStringW::CopyChars(pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir), + szDownloadDir.GetString(), szDownloadDir.GetLength() + 1); + } +} + +VOID +FillDefaultSettings(PSETTINGS_INFO pSettingsInfo) +{ + ZeroMemory(pSettingsInfo, sizeof(*pSettingsInfo)); + + pSettingsInfo->bSaveWndPos = TRUE; + pSettingsInfo->bUpdateAtStart = FALSE; + pSettingsInfo->bLogEnabled = TRUE; + pSettingsInfo->bUseSource = FALSE; pSettingsInfo->bDelInstaller = FALSE; pSettingsInfo->Maximized = FALSE; pSettingsInfo->Left = CW_USEDEFAULT; pSettingsInfo->Top = CW_USEDEFAULT; pSettingsInfo->Width = 680; pSettingsInfo->Height = 450; + + ValidateStringSettings(pSettingsInfo); } BOOL LoadSettings(PSETTINGS_INFO pSettingsInfo) { + BOOL bLoadedAny = FALSE; + + FillDefaultSettings(pSettingsInfo); + ATL::CRegKey RegKey; - if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\" RAPPS_NAME, KEY_READ) != ERROR_SUCCESS) + if (RegKey.Open(HKEY_CURRENT_USER, SETTINGSSUBKEY, KEY_READ) == ERROR_SUCCESS) { - return FALSE; + bLoadedAny = LoadAllSettings(RegKey, *pSettingsInfo); } - return LoadAllSettings(RegKey, *pSettingsInfo); + ValidateStringSettings(pSettingsInfo); // Handles the case where a REG_SZ is present but empty + + if (!bLoadedAny) + { + // This the first launch, write at least one item so ParseCmdAndExecute() does not + // trigger another DB update in another process instance between now and SaveSettings(). + ATL::CRegKey RegKey; + if (RegKey.Create(HKEY_CURRENT_USER, SETTINGSSUBKEY, NULL, REG_OPTION_NON_VOLATILE, + KEY_WRITE, NULL, NULL) == ERROR_SUCCESS) + { + SettingsFieldBool field(&(pSettingsInfo->bUpdateAtStart), L"bUpdateAtStart"); + field.Save(RegKey); + } + } + return bLoadedAny; } BOOL @@ -245,8 +274,8 @@ SaveSettings(HWND hwnd, PSETTINGS_INFO pSettingsInfo) (wp.showCmd == SW_MAXIMIZE || (wp.showCmd == SW_SHOWMINIMIZED && (wp.flags & WPF_RESTORETOMAXIMIZED))); } - if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\" RAPPS_NAME, NULL, - REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL) != ERROR_SUCCESS) + if (RegKey.Create(HKEY_CURRENT_USER, SETTINGSSUBKEY, NULL, REG_OPTION_NON_VOLATILE, + KEY_WRITE, NULL, NULL) != ERROR_SUCCESS) { return FALSE; } diff --git a/base/applications/rapps/winmain.cpp b/base/applications/rapps/winmain.cpp index be2294c7504..9c32c4cd887 100644 --- a/base/applications/rapps/winmain.cpp +++ b/base/applications/rapps/winmain.cpp @@ -40,12 +40,7 @@ wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nSh } hInst = hInstance; - BOOL bIsFirstLaunch = !LoadSettings(&SettingsInfo); - if (bIsFirstLaunch) - { - FillDefaultSettings(&SettingsInfo); - } InitLogs(); InitCommonControls();