https://git.reactos.org/?p=reactos.git;a=commitdiff;h=e1974d21dc591615968c995b054e96d80d6229f8

commit e1974d21dc591615968c995b054e96d80d6229f8
Author:     He Yang <[email protected]>
AuthorDate: Sat Aug 29 20:32:59 2020 +0800
Commit:     Mark Jansen <[email protected]>
CommitDate: Sun Sep 6 17:10:18 2020 +0200

    [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
---
 base/applications/rapps/CMakeLists.txt             |   2 +
 base/applications/rapps/gui.cpp                    |   2 +-
 .../rapps/include/{winmain.h => settings.h}        |  13 +-
 base/applications/rapps/include/winmain.h          |  30 +--
 base/applications/rapps/settings.cpp               | 243 +++++++++++++++++++++
 base/applications/rapps/settingsdlg.cpp            |   2 +-
 base/applications/rapps/winmain.cpp                |  86 +-------
 7 files changed, 254 insertions(+), 124 deletions(-)

diff --git a/base/applications/rapps/CMakeLists.txt 
b/base/applications/rapps/CMakeLists.txt
index 4a0dfabfd7f..62a61d24b95 100644
--- a/base/applications/rapps/CMakeLists.txt
+++ b/base/applications/rapps/CMakeLists.txt
@@ -17,6 +17,7 @@ list(APPEND SOURCE
     integrity.cpp
     loaddlg.cpp
     misc.cpp
+    settings.cpp
     settingsdlg.cpp
     winmain.cpp
     unattended.cpp
@@ -30,6 +31,7 @@ list(APPEND SOURCE
     include/crichedit.h
     include/defines.h
     include/misc.h
+    include/settings.h
     include/resource.h
     include/rosui.h
     include/winmain.h
diff --git a/base/applications/rapps/gui.cpp b/base/applications/rapps/gui.cpp
index 50f4b14f810..fb414504fc7 100644
--- a/base/applications/rapps/gui.cpp
+++ b/base/applications/rapps/gui.cpp
@@ -316,7 +316,7 @@ BOOL CMainWindow::ProcessWindowMessage(HWND hwnd, UINT Msg, 
WPARAM wParam, LPARA
     case WM_DESTROY:
     {
         ShowWindow(SW_HIDE);
-        SaveSettings(hwnd);
+        SaveSettings(hwnd, &SettingsInfo);
 
         FreeLogs();
         m_AvailableApps.FreeCachedEntries();
diff --git a/base/applications/rapps/include/winmain.h 
b/base/applications/rapps/include/settings.h
similarity index 73%
copy from base/applications/rapps/include/winmain.h
copy to base/applications/rapps/include/settings.h
index 3b9397a0eda..a3b4b867ffd 100644
--- a/base/applications/rapps/include/winmain.h
+++ b/base/applications/rapps/include/settings.h
@@ -2,9 +2,6 @@
 #include <windef.h>
 #include <wininet.h>
 
-extern LPCWSTR szWindowClass;
-
-//TODO: Separate main and settings related definitions
 struct SETTINGS_INFO
 {
     BOOL bSaveWndPos;
@@ -29,12 +26,8 @@ struct SETTINGS_INFO
 
 typedef SETTINGS_INFO *PSETTINGS_INFO;
 
-extern HWND hMainWnd;
-extern HINSTANCE hInst;
-extern SETTINGS_INFO SettingsInfo;
-
-VOID SaveSettings(HWND hwnd);
+BOOL LoadSettings(PSETTINGS_INFO pSettingsInfo);
+BOOL SaveSettings(HWND hwnd, PSETTINGS_INFO pSettingsInfo);
 VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo);
 
-// integrity.cpp
-BOOL VerifyInteg(LPCWSTR lpSHA1Hash, LPCWSTR lpFileName);
+extern SETTINGS_INFO SettingsInfo;
diff --git a/base/applications/rapps/include/winmain.h 
b/base/applications/rapps/include/winmain.h
index 3b9397a0eda..d6a214298a6 100644
--- a/base/applications/rapps/include/winmain.h
+++ b/base/applications/rapps/include/winmain.h
@@ -1,40 +1,12 @@
 #pragma once
 #include <windef.h>
 #include <wininet.h>
+#include "settings.h"
 
 extern LPCWSTR szWindowClass;
 
-//TODO: Separate main and settings related definitions
-struct SETTINGS_INFO
-{
-    BOOL bSaveWndPos;
-    BOOL bUpdateAtStart;
-    BOOL bLogEnabled;
-    WCHAR szDownloadDir[MAX_PATH];
-    BOOL bDelInstaller;
-    /* Window Pos */
-    BOOL Maximized;
-    INT Left;
-    INT Top;
-    INT Width;
-    INT Height;
-    /* Proxy settings */
-    INT Proxy;
-    WCHAR szProxyServer[MAX_PATH];
-    WCHAR szNoProxyFor[MAX_PATH];
-    /* Software source settings */
-    BOOL bUseSource;
-    WCHAR szSourceURL[INTERNET_MAX_URL_LENGTH];
-};
-
-typedef SETTINGS_INFO *PSETTINGS_INFO;
-
 extern HWND hMainWnd;
 extern HINSTANCE hInst;
-extern SETTINGS_INFO SettingsInfo;
-
-VOID SaveSettings(HWND hwnd);
-VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo);
 
 // integrity.cpp
 BOOL VerifyInteg(LPCWSTR lpSHA1Hash, LPCWSTR lpFileName);
diff --git a/base/applications/rapps/settings.cpp 
b/base/applications/rapps/settings.cpp
new file mode 100644
index 00000000000..57c7b55ba06
--- /dev/null
+++ b/base/applications/rapps/settings.cpp
@@ -0,0 +1,243 @@
+/*
+ * PROJECT:     ReactOS Applications Manager
+ * LICENSE:     GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
+ * PURPOSE:     Functions to load / save settings from reg.
+ * COPYRIGHT:   Copyright 2020 He Yang           ([email protected])
+ */
+
+#include "rapps.h"
+#include "settings.h"
+
+
+class SettingsField
+{
+public:
+    virtual ~SettingsField() { ; }
+    virtual BOOL Save(CRegKey &key) = 0;
+    virtual BOOL Load(CRegKey &key) = 0;
+};
+
+class SettingsFieldBool : public SettingsField
+{
+public:
+    SettingsFieldBool(BOOL *pValue, LPCWSTR szRegName)
+        : m_pValueStore(pValue), m_RegName(szRegName)
+    {
+    }
+
+    virtual BOOL Save(CRegKey &key) override
+    {
+        return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == 
ERROR_SUCCESS;
+    }
+    virtual BOOL Load(CRegKey &key) override
+    {
+        DWORD dwField;
+        LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
+        if (lResult != ERROR_SUCCESS)
+        {
+            return FALSE;
+        }
+        *m_pValueStore = (BOOL)dwField;
+        return TRUE;
+    }
+
+private:
+    BOOL *m_pValueStore;     // where to read/store the value
+    LPCWSTR m_RegName;       // key name in registery
+};
+
+class SettingsFieldInt : public SettingsField
+{
+public:
+    SettingsFieldInt(INT *pValue, LPCWSTR szRegName)
+        : m_pValueStore(pValue), m_RegName(szRegName)
+    {
+    }
+
+    virtual BOOL Save(CRegKey &key) override
+    {
+        return key.SetDWORDValue(m_RegName, (DWORD)(*m_pValueStore)) == 
ERROR_SUCCESS;
+    }
+    virtual BOOL Load(CRegKey &key) override
+    {
+        DWORD dwField;
+        LONG lResult = key.QueryDWORDValue(m_RegName, dwField);
+        if (lResult != ERROR_SUCCESS)
+        {
+            return FALSE;
+        }
+        *m_pValueStore = (INT)dwField;
+        return TRUE;
+    }
+
+private:
+    INT *m_pValueStore;      // where to read/store the value
+    LPCWSTR m_RegName;       // key name in registery
+};
+
+class SettingsFieldString : public SettingsField
+{
+public:
+    SettingsFieldString(WCHAR *pString, ULONG cchLen, LPCWSTR szRegName)
+        : m_pStringStore(pString), m_StringLen(cchLen), m_RegName(szRegName)
+    {
+    }
+
+    virtual BOOL Save(CRegKey &key) override
+    {
+        return key.SetStringValue(m_RegName, m_pStringStore) == ERROR_SUCCESS;
+    }
+    virtual BOOL Load(CRegKey &key) override
+    {
+        ULONG nChar = m_StringLen - 1; // make sure the terminating L'\0'
+        LONG lResult = key.QueryStringValue(m_RegName, m_pStringStore, &nChar);
+        return lResult == ERROR_SUCCESS;
+    }
+
+private:
+    WCHAR *m_pStringStore;        // where to read/store the value
+    ULONG m_StringLen;            // string length, in chars
+    LPCWSTR m_RegName;            // key name in registery
+};
+
+
+void AddInfoFields(ATL::CAtlList<SettingsField *> &infoFields, SETTINGS_INFO 
&settings)
+{
+    infoFields.AddTail(new SettingsFieldBool(&(settings.bSaveWndPos), 
L"bSaveWndPos"));
+    infoFields.AddTail(new SettingsFieldBool(&(settings.bUpdateAtStart), 
L"bUpdateAtStart"));
+    infoFields.AddTail(new SettingsFieldBool(&(settings.bLogEnabled), 
L"bLogEnabled"));
+    infoFields.AddTail(new SettingsFieldString(settings.szDownloadDir, 
MAX_PATH, L"szDownloadDir"));
+    infoFields.AddTail(new SettingsFieldBool(&(settings.bDelInstaller), 
L"bDelInstaller"));
+    infoFields.AddTail(new SettingsFieldBool(&(settings.Maximized), 
L"WindowPosMaximized"));
+    infoFields.AddTail(new SettingsFieldInt(&(settings.Left), 
L"WindowPosLeft"));
+    infoFields.AddTail(new SettingsFieldInt(&(settings.Top), L"WindowPosTop"));
+    infoFields.AddTail(new SettingsFieldInt(&(settings.Width), 
L"WindowPosWidth"));
+    infoFields.AddTail(new SettingsFieldInt(&(settings.Height), 
L"WindowPosHeight"));
+    infoFields.AddTail(new SettingsFieldInt(&(settings.Proxy), L"ProxyMode"));
+    infoFields.AddTail(new SettingsFieldString((settings.szProxyServer), 
MAX_PATH, L"ProxyServer"));
+    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 SaveAllSettings(CRegKey &key, SETTINGS_INFO &settings)
+{
+    BOOL bAllSuccess = TRUE;
+    ATL::CAtlList<SettingsField *> infoFields;
+
+    AddInfoFields(infoFields, settings);
+
+    POSITION InfoListPosition = infoFields.GetHeadPosition();
+    while (InfoListPosition)
+    {
+        SettingsField *Info = infoFields.GetNext(InfoListPosition);
+        if (!Info->Save(key))
+        {
+            bAllSuccess = FALSE;
+            // TODO: error log
+        }
+        delete Info;
+    }
+    return bAllSuccess;
+}
+
+BOOL LoadAllSettings(CRegKey &key, SETTINGS_INFO &settings)
+{
+    BOOL bAllSuccess = TRUE;
+    ATL::CAtlList<SettingsField *> infoFields;
+
+    AddInfoFields(infoFields, settings);
+
+    POSITION InfoListPosition = infoFields.GetHeadPosition();
+    while (InfoListPosition)
+    {
+        SettingsField *Info = infoFields.GetNext(InfoListPosition);
+        if (!Info->Load(key))
+        {
+            bAllSuccess = FALSE;
+            // TODO: error log
+        }
+        delete Info;
+    }
+    return bAllSuccess;
+}
+
+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;
+}
+
+BOOL LoadSettings(PSETTINGS_INFO pSettingsInfo)
+{
+    ATL::CRegKey RegKey;
+    if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", KEY_READ) 
!= ERROR_SUCCESS)
+    {
+        return FALSE;
+    }
+
+    return LoadAllSettings(RegKey, *pSettingsInfo);
+}
+
+BOOL SaveSettings(HWND hwnd, PSETTINGS_INFO pSettingsInfo)
+{
+    WINDOWPLACEMENT wp;
+    ATL::CRegKey RegKey;
+
+    if (pSettingsInfo->bSaveWndPos)
+    {
+        wp.length = sizeof(wp);
+        GetWindowPlacement(hwnd, &wp);
+
+        pSettingsInfo->Left = wp.rcNormalPosition.left;
+        pSettingsInfo->Top = wp.rcNormalPosition.top;
+        pSettingsInfo->Width = wp.rcNormalPosition.right - 
wp.rcNormalPosition.left;
+        pSettingsInfo->Height = wp.rcNormalPosition.bottom - 
wp.rcNormalPosition.top;
+        pSettingsInfo->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)
+    {
+        return FALSE;
+    }
+
+    return SaveAllSettings(RegKey, *pSettingsInfo);
+}
diff --git a/base/applications/rapps/settingsdlg.cpp 
b/base/applications/rapps/settingsdlg.cpp
index 8943c1ac98e..d1f36519d9a 100644
--- a/base/applications/rapps/settingsdlg.cpp
+++ b/base/applications/rapps/settingsdlg.cpp
@@ -260,7 +260,7 @@ namespace
                 }
 
                 SettingsInfo = NewSettingsInfo;
-                SaveSettings(GetParent(hDlg));
+                SaveSettings(GetParent(hDlg), &SettingsInfo);
                 EndDialog(hDlg, LOWORD(wParam));
             }
             break;
diff --git a/base/applications/rapps/winmain.cpp 
b/base/applications/rapps/winmain.cpp
index 253c802ad61..9d36df598eb 100644
--- a/base/applications/rapps/winmain.cpp
+++ b/base/applications/rapps/winmain.cpp
@@ -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);

Reply via email to