[RAPPS] Extensive conversion to ATL and general improvements
authorAlexander Shaposhnikov <sanchaez@reactos.org>
Mon, 10 Jul 2017 21:02:24 +0000 (21:02 +0000)
committerAlexander Shaposhnikov <sanchaez@reactos.org>
Mon, 10 Jul 2017 21:02:24 +0000 (21:02 +0000)
* Converted most of the WCHAR buffers to ATL::CStringW
* available.cpp: replaced C heap list with ATL::CAtlList
* Improved style consistency
* Added myself to the headers
* Other improvements

svn path=/branches/GSoC_2017/rapps/; revision=75315

12 files changed:
reactos/base/applications/rapps/available.cpp
reactos/base/applications/rapps/crichedit.h
reactos/base/applications/rapps/gui.cpp
reactos/base/applications/rapps/installed.cpp
reactos/base/applications/rapps/integrity.cpp
reactos/base/applications/rapps/loaddlg.cpp
reactos/base/applications/rapps/misc.cpp
reactos/base/applications/rapps/rapps.h
reactos/base/applications/rapps/resource.h
reactos/base/applications/rapps/rosui.h
reactos/base/applications/rapps/settingsdlg.cpp
reactos/base/applications/rapps/winmain.cpp

index 66277d2..ac1ca30 100644 (file)
@@ -5,79 +5,79 @@
  * PURPOSE:         Functions for working with available applications
  * PROGRAMMERS:     Dmitry Chapyshev           (dmitry@reactos.org)
  *                  Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
+ *                  Alexander Shaposhnikov     (chaez.san@gmail.com)
  */
 
 #include "rapps.h"
 
-inline void _AddText(UINT a, LPCWSTR b, DWORD c, DWORD d)
+ATL::CAtlList<PAPPLICATION_INFO> InfoList;
+
+inline void InsertTextAfterLoaded_RichEdit(UINT uStringID, const ATL::CStringW& szText, DWORD StringFlags, DWORD TextFlags)
 {
-    if (b[0] != '\0')
+    ATL::CStringW szLoadedText;
+    if (!szText.IsEmpty() && szLoadedText.LoadStringW(hInst, uStringID))
     {
-        WCHAR szText[MAX_STR_LEN];
-        LoadStringW(hInst, a, szText, _countof(szText));
-        InsertRichEditText(szText, c);
-        InsertRichEditText(b, d);
+        InsertRichEditText(szLoadedText, StringFlags);
+        InsertRichEditText(szText, TextFlags);
     }
 }
 
-inline void _AddTextNewl(UINT a, DWORD b)
-{
-    WCHAR szText[MAX_STR_LEN];
-    LoadStringW(hInst, a, szText, _countof(szText));
-    InsertRichEditText(L"\n", 0);
-    InsertRichEditText(szText, b);
-    InsertRichEditText(L"\n", 0);
-}
-
-template<typename T, size_t N, size_t N2>
-inline BOOL _GetString(LPCWSTR a, T(&b)[N], T(&cFileName)[N2])
+inline void InsertLoadedTextNewl_RichEdit(UINT uStringID, DWORD StringFlags)
 {
-    return ParserGetString(a, b, N, cFileName);
+    ATL::CStringW szLoadedText;
+    if (szLoadedText.LoadStringW(hInst, uStringID))
+    {
+        InsertRichEditText(L"\n", 0);
+        InsertRichEditText(szLoadedText, StringFlags);
+        InsertRichEditText(L"\n", 0);
+    }
 }
 
-template<typename T, size_t N, size_t N2>
-inline void _GetStringNullFailure(LPCWSTR a, T(&b)[N], T(&cFileName)[N2])
+inline BOOL GetString(LPCWSTR lpKeyName, ATL::CStringW& ReturnedString, const ATL::CStringW& cFileName)
 {
-    if (!_GetString(a, b, cFileName))
+    if (!ParserGetString(lpKeyName, cFileName, ReturnedString))
     {
-        b[0] = '\0';
+        ReturnedString.Empty();
+        return FALSE;
     }
+    return TRUE;
 }
 
 //App is "installed" if the RegName or Name is in the registry
-inline BOOL _AppInstallCheckWithKey(PAPPLICATION_INFO Info, REGSAM key)
+inline BOOL IsAppInstalledKey(PAPPLICATION_INFO Info, REGSAM key)
 {
-    return (*Info->szRegName
-        && (IsInstalledApplication(Info->szRegName, TRUE, key)
-        || IsInstalledApplication(Info->szRegName, FALSE, key)))
-        || (*Info->szName && (IsInstalledApplication(Info->szName, TRUE, key)
-            || IsInstalledApplication(Info->szName, FALSE, key)));
+    return (!Info->szRegName.IsEmpty()
+            && (IsInstalledApplication(Info->szRegName, TRUE, key)
+                || IsInstalledApplication(Info->szRegName, FALSE, key)))
+        || (!Info->szName.IsEmpty()
+            && (IsInstalledApplication(Info->szName, TRUE, key)
+                || IsInstalledApplication(Info->szName, FALSE, key)));
 }
 
 
 //Check both registry keys in 64bit system
 //TODO: check system type beforehand to avoid double checks?
-inline BOOL _AppInstallCheck(PAPPLICATION_INFO Info)
+inline BOOL IsAppInstalled(PAPPLICATION_INFO Info)
 {
-    return  _AppInstallCheckWithKey(Info, KEY_WOW64_32KEY)
-        || _AppInstallCheckWithKey(Info, KEY_WOW64_64KEY);
+    return  IsAppInstalledKey(Info, KEY_WOW64_32KEY)
+        || IsAppInstalledKey(Info, KEY_WOW64_64KEY);
 }
 
 //App is "installed" if the RegName or Name is in the registry
-inline BOOL _GetInstalledVersionWithKey(PAPPLICATION_INFO Info, LPWSTR szVersion, UINT iVersionSize, REGSAM key)
+inline BOOL GetInstalledVersionWithKey(PAPPLICATION_INFO Info, ATL::CStringW& szVersion, REGSAM key)
 {
-    return (*Info->szRegName
-        && (InstalledVersion(szVersion, iVersionSize, Info->szRegName, TRUE, key)
-        || InstalledVersion(szVersion, iVersionSize, Info->szRegName, FALSE, key)))
-        || (*Info->szName && (InstalledVersion(szVersion, iVersionSize, Info->szName, TRUE, key)
-            || InstalledVersion(szVersion, iVersionSize, Info->szName, FALSE, key)));
+    return (!Info->szRegName.IsEmpty()
+            && (InstalledVersion(szVersion, Info->szRegName, TRUE, key)
+                || InstalledVersion(szVersion, Info->szRegName, FALSE, key)))
+        || (!Info->szName.IsEmpty()
+            && (InstalledVersion(szVersion, Info->szName, TRUE, key)
+                || InstalledVersion(szVersion, Info->szName, FALSE, key)));
 }
 
-//App is "installed" if the RegName or Name is in the registry
-inline BOOL _GetInstalledVersion(PAPPLICATION_INFO Info, LPWSTR szVersion, UINT iVersionSize)
+inline BOOL GetInstalledVersion(PAPPLICATION_INFO Info, ATL::CStringW& szVersion)
 {
-    return  _GetInstalledVersionWithKey(Info, szVersion, iVersionSize, KEY_WOW64_32KEY)
-        || _GetInstalledVersionWithKey(Info, szVersion, iVersionSize, KEY_WOW64_64KEY);
+    return  GetInstalledVersionWithKey(Info, szVersion, KEY_WOW64_32KEY)
+        || GetInstalledVersionWithKey(Info, szVersion, KEY_WOW64_64KEY);
 }
 
 LIST_ENTRY CachedEntriesHead = {&CachedEntriesHead, &CachedEntriesHead};
@@ -87,10 +87,10 @@ BOOL
 ShowAvailableAppInfo(INT Index)
 {
     PAPPLICATION_INFO Info = (PAPPLICATION_INFO) ListViewGetlParam(Index);
-    WCHAR szVersion[MAX_PATH] = L"\0";
-    WCHAR szLicense[MAX_PATH] = L"\0";
-    BOOL bIsInstalled = _AppInstallCheck(Info), 
-        bHasVersion = _GetInstalledVersion(Info, szVersion, _countof(szVersion));
+    ATL::CStringW szVersion;
+    ATL::CStringW szLicense;
+    BOOL bIsInstalled = IsAppInstalled(Info),
+        bHasVersion = GetInstalledVersion(Info, szVersion);
 
     if (!Info) return FALSE;
 
@@ -100,50 +100,51 @@ ShowAvailableAppInfo(INT Index)
     {
         if (bHasVersion)
         {
-            if (CompareVersionsStrings(Info->szVersion, szVersion))
-                _AddTextNewl(IDS_STATUS_UPDATE_AVAILABLE, CFE_ITALIC);
+            if (Info->szVersion.Compare(szVersion) > 0)
+                InsertLoadedTextNewl_RichEdit(IDS_STATUS_UPDATE_AVAILABLE, CFE_ITALIC);
             else
-                _AddTextNewl(IDS_STATUS_INSTALLED, CFE_ITALIC);
+                InsertLoadedTextNewl_RichEdit(IDS_STATUS_INSTALLED, CFE_ITALIC);
 
-            _AddText(IDS_AINFO_VERSION, szVersion, CFE_BOLD, 0);
-        } 
+            InsertTextAfterLoaded_RichEdit(IDS_AINFO_VERSION, szVersion, CFE_BOLD, 0);
+        }
         else
-            _AddTextNewl(IDS_STATUS_INSTALLED, CFE_ITALIC);
+            InsertLoadedTextNewl_RichEdit(IDS_STATUS_INSTALLED, CFE_ITALIC);
 
     }
     else
-        _AddTextNewl(IDS_STATUS_NOTINSTALLED, CFE_ITALIC);
-
+        InsertLoadedTextNewl_RichEdit(IDS_STATUS_NOTINSTALLED, CFE_ITALIC);
 
-    _AddText(IDS_AINFO_AVAILABLEVERSION, Info->szVersion, CFE_BOLD, 0);
+    InsertTextAfterLoaded_RichEdit(IDS_AINFO_AVAILABLEVERSION, Info->szVersion, CFE_BOLD, 0);
     //license
     switch (Info->LicenseType)
     {
-        case LICENSE_TYPE::OpenSource:
-            LoadStringW(hInst, IDS_LICENSE_OPENSOURCE, szLicense, _countof(szLicense));
-            break;
-        case LICENSE_TYPE::Freeware:
-            LoadStringW(hInst, IDS_LICENSE_FREEWARE, szLicense, _countof(szLicense));
-            break;
-        case LICENSE_TYPE::Trial:
-            LoadStringW(hInst, IDS_LICENSE_TRIAL, szLicense, _countof(szLicense));
-            break;
-        default:
-            break;
+    case LICENSE_TYPE::OpenSource:
+        szLicense.LoadStringW(hInst, IDS_LICENSE_OPENSOURCE);
+        break;
+    case LICENSE_TYPE::Freeware:
+        szLicense.LoadStringW(hInst, IDS_LICENSE_FREEWARE);
+        break;
+    case LICENSE_TYPE::Trial:
+        szLicense.LoadStringW(hInst, IDS_LICENSE_TRIAL);
+        break;
+    default:
+        break;
     }
-    if (szLicense[0] != '\0')
+
+    if (szLicense.IsEmpty())
     {
-        StringCbPrintfW(szLicense, _countof(szLicense), L"%ls (%ls)", szLicense, Info->szLicense);
-        _AddText(IDS_AINFO_LICENSE, szLicense, CFE_BOLD, 0);
+        InsertTextAfterLoaded_RichEdit(IDS_AINFO_LICENSE, Info->szLicense, CFE_BOLD, 0);
     }
     else
     {
-        _AddText(IDS_AINFO_LICENSE, Info->szLicense, CFE_BOLD, 0);
+        szLicense.Format(L"(%ls)", Info->szLicense);
+        InsertTextAfterLoaded_RichEdit(IDS_AINFO_LICENSE, szLicense, CFE_BOLD, 0);
     }
-    _AddText(IDS_AINFO_SIZE, Info->szSize, CFE_BOLD, 0);
-    _AddText(IDS_AINFO_URLSITE, Info->szUrlSite, CFE_BOLD, CFE_LINK);
-    _AddText(IDS_AINFO_DESCRIPTION, Info->szDesc, CFE_BOLD, 0);
-    _AddText(IDS_AINFO_URLDOWNLOAD, Info->szUrlDownload, CFE_BOLD, CFE_LINK);
+
+    InsertTextAfterLoaded_RichEdit(IDS_AINFO_SIZE, Info->szSize, CFE_BOLD, 0);
+    InsertTextAfterLoaded_RichEdit(IDS_AINFO_URLSITE, Info->szUrlSite, CFE_BOLD, CFE_LINK);
+    InsertTextAfterLoaded_RichEdit(IDS_AINFO_DESCRIPTION, Info->szDesc, CFE_BOLD, 0);
+    InsertTextAfterLoaded_RichEdit(IDS_AINFO_URLDOWNLOAD, Info->szUrlDownload, CFE_BOLD, CFE_LINK);
 
     return TRUE;
 }
@@ -153,89 +154,60 @@ DeleteCurrentAppsDB(VOID)
 {
     HANDLE hFind = INVALID_HANDLE_VALUE;
     WIN32_FIND_DATAW FindFileData;
-    WCHAR szCabPath[MAX_PATH];
-    WCHAR szSearchPath[MAX_PATH];
-    WCHAR szPath[MAX_PATH];
-    WCHAR szTmp[MAX_PATH];
-    HRESULT hr;
+    ATL::CStringW szCabPath;
+    ATL::CStringW szSearchPath;
+    ATL::CStringW szPath;
     BOOL result = TRUE;
 
-    if (!GetStorageDirectory(szPath, _countof(szPath)))
+    if (!GetStorageDirectory(szPath))
         return FALSE;
 
-    hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
-        L"%ls\\rappmgr.cab",
-        szPath);
-    if (FAILED(hr))
-        return FALSE;
+    szCabPath = szPath + L"\\rappmgr.cab";
+    result = result && DeleteFileW(szCabPath.GetString());
 
-    result = result && DeleteFileW(szCabPath);
-
-    hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
-
-    if (FAILED(hr))
-        return FALSE;
-
-    hr = StringCbPrintfW(szSearchPath, sizeof(szSearchPath),
-        L"%ls*.txt",
-        szPath);
-    if (FAILED(hr))
-        return FALSE;
+    szPath += L"\\rapps\\";
+    szSearchPath = szPath + L"*.txt";
 
-    hFind = FindFirstFileW(szSearchPath, &FindFileData);
+    hFind = FindFirstFileW(szSearchPath.GetString(), &FindFileData);
 
     if (hFind == INVALID_HANDLE_VALUE)
         return result;
 
+    ATL::CStringW szTmp;
     do
     {
-        hr = StringCbPrintfW(szTmp, sizeof(szTmp),
-            L"%ls%ls",
-            szPath, FindFileData.cFileName);
-        if (FAILED(hr))
-            continue;
-        result = result && DeleteFileW(szTmp);
-
-    }
-    while (FindNextFileW(hFind, &FindFileData) != 0);
+        szTmp = szPath + FindFileData.cFileName;
+        result = result && DeleteFileW(szTmp.GetString());
+    } while (FindNextFileW(hFind, &FindFileData) != 0);
 
     FindClose(hFind);
 
     return result;
 }
 
-
 BOOL
 UpdateAppsDB(VOID)
 {
-    WCHAR szPath[MAX_PATH];
-    WCHAR szAppsPath[MAX_PATH];
-    WCHAR szCabPath[MAX_PATH];
+    ATL::CStringW szPath;
+    ATL::CStringW szAppsPath;
+    ATL::CStringW szCabPath;
 
     if (!DeleteCurrentAppsDB())
         return FALSE;
 
     DownloadApplicationsDB(APPLICATION_DATABASE_URL);
 
-    if (!GetStorageDirectory(szPath, _countof(szPath)))
+    if (!GetStorageDirectory(szPath))
         return FALSE;
 
-    if (FAILED(StringCbPrintfW(szCabPath, sizeof(szCabPath),
-        L"%ls\\rappmgr.cab",
-        szPath)))
-    {
-        return FALSE;
-    }
+    szCabPath = szPath + L"\\rappmgr.cab";
+    szAppsPath = szPath + L"\\rapps\\";
 
-    if (FAILED(StringCbPrintfW(szAppsPath, sizeof(szAppsPath),
-        L"%ls\\rapps\\",
-        szPath)))
+    if (!ExtractFilesFromCab(szCabPath, szAppsPath))
     {
         return FALSE;
     }
 
-    ExtractFilesFromCab(szCabPath, szAppsPath);
-
     return TRUE;
 }
 
@@ -245,43 +217,27 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
 {
     HANDLE hFind = INVALID_HANDLE_VALUE;
     WIN32_FIND_DATAW FindFileData;
-    WCHAR szPath[MAX_PATH];
-    WCHAR szAppsPath[MAX_PATH];
-    WCHAR szCabPath[MAX_PATH];
+    ATL::CStringW szPath;
+    ATL::CStringW szAppsPath;
+    ATL::CStringW szCabPath;
     PAPPLICATION_INFO Info;
-    HRESULT hr;
-
-    if (!GetStorageDirectory(szPath, _countof(szPath)))
-        return FALSE;
-
-    hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
-        L"%ls\\rappmgr.cab",
-        szPath);
-    if (FAILED(hr))
-        return FALSE;
 
-    hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
 
-    if (FAILED(hr))
+    if (!GetStorageDirectory(szPath))
         return FALSE;
 
-    hr = StringCbCopyW(szAppsPath, sizeof(szAppsPath), szPath);
+    szCabPath = szPath + L"\\rappmgr.cab";
+    szPath += L"\\rapps\\";
+    szAppsPath = szPath;
 
-    if (FAILED(hr))
-        return FALSE;
-
-    if (!CreateDirectory(szPath, NULL) &&
+    if (!CreateDirectoryW(szPath.GetString(), NULL) &&
         GetLastError() != ERROR_ALREADY_EXISTS)
     {
         return FALSE;
     }
 
-    hr = StringCbCatW(szPath, sizeof(szPath), L"*.txt");
-
-    if (FAILED(hr))
-        return FALSE;
-
-    hFind = FindFirstFileW(szPath, &FindFileData);
+    szPath += L"*.txt";
+    hFind = FindFirstFileW(szPath.GetString(), &FindFileData);
 
     if (hFind == INVALID_HANDLE_VALUE)
     {
@@ -298,32 +254,34 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
     do
     {
         /* loop for all the cached entries */
-        for (pCachedEntry = CachedEntriesHead.Flink; pCachedEntry != &CachedEntriesHead; pCachedEntry = pCachedEntry->Flink)
+        POSITION CurrentListPosition = InfoList.GetHeadPosition();
+
+        while (CurrentListPosition != NULL)
         {
-            Info = CONTAINING_RECORD(pCachedEntry, APPLICATION_INFO, List);
+            POSITION LastListPosition = CurrentListPosition;
+            Info = InfoList.GetNext(CurrentListPosition);
 
             /* do we already have this entry in cache? */
-            if (_wcsicmp(FindFileData.cFileName, Info->cFileName) == 0)
+            if (!Info->cFileName.Compare(FindFileData.cFileName))
             {
                 /* is it current enough, or the file has been modified since our last time here? */
                 if (CompareFileTime(&FindFileData.ftLastWriteTime, &Info->ftCacheStamp) == 1)
                 {
                     /* recreate our cache, this is the slow path */
-                    RemoveEntryList(&Info->List);
-                    HeapFree(GetProcessHeap(), 0, Info);
+                    InfoList.RemoveAt(LastListPosition);
+                    delete Info;
+                    break;
                 }
                 else
                 {
                     /* speedy path, compare directly, we already have the data */
                     goto skip_if_cached;
                 }
-
-                break;
             }
         }
 
         /* create a new entry */
-        Info = (PAPPLICATION_INFO) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(APPLICATION_INFO));
+        Info = new APPLICATION_INFO();
 
         if (!Info)
             break;
@@ -336,13 +294,13 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
         }
 
         /* copy the cache-related fields for the next time */
-        RtlCopyMemory(&Info->cFileName, &FindFileData.cFileName, MAX_PATH);
+        Info->cFileName = FindFileData.cFileName;
         RtlCopyMemory(&Info->ftCacheStamp, &FindFileData.ftLastWriteTime, sizeof(FILETIME));
 
         /* add our cached entry to the cached list */
-        InsertTailList(&CachedEntriesHead, &Info->List);
+        InfoList.AddTail(Info);
 
-    skip_if_cached:
+skip_if_cached:
 
         if (Info->Category == FALSE)
             continue;
@@ -353,30 +311,29 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
         /* if our cache hit was only partial, we need to parse
            and lazily fill the rest of fields only when needed */
 
-        if (Info->szUrlDownload[0] == 0)
+        if (Info->szUrlDownload.IsEmpty())
         {
-            if (!_GetString(L"Name", Info->szName, FindFileData.cFileName)
-                || !_GetString(L"URLDownload", Info->szUrlDownload, FindFileData.cFileName))
+            if (!GetString(L"Name", Info->szName, FindFileData.cFileName)
+                || !GetString(L"URLDownload", Info->szUrlDownload, FindFileData.cFileName))
             {
                 continue;
             }
 
-            _GetStringNullFailure(L"RegName", Info->szRegName, FindFileData.cFileName);
-            _GetStringNullFailure(L"Version", Info->szVersion, FindFileData.cFileName);
-            _GetStringNullFailure(L"License", Info->szLicense, FindFileData.cFileName);
-            _GetStringNullFailure(L"Description", Info->szDesc, FindFileData.cFileName);
-            _GetStringNullFailure(L"Size", Info->szSize, FindFileData.cFileName);
-            _GetStringNullFailure(L"URLSite", Info->szUrlSite, FindFileData.cFileName);
-            _GetStringNullFailure(L"CDPath", Info->szCDPath, FindFileData.cFileName);
-            _GetStringNullFailure(L"Language", Info->szRegName, FindFileData.cFileName);
-            _GetStringNullFailure(L"SHA1", Info->szSHA1, FindFileData.cFileName);
+            GetString(L"RegName", Info->szRegName, FindFileData.cFileName);
+            GetString(L"Version", Info->szVersion, FindFileData.cFileName);
+            GetString(L"License", Info->szLicense, FindFileData.cFileName);
+            GetString(L"Description", Info->szDesc, FindFileData.cFileName);
+            GetString(L"Size", Info->szSize, FindFileData.cFileName);
+            GetString(L"URLSite", Info->szUrlSite, FindFileData.cFileName);
+            GetString(L"CDPath", Info->szCDPath, FindFileData.cFileName);
+            GetString(L"Language", Info->szRegName, FindFileData.cFileName);
+            GetString(L"SHA1", Info->szSHA1, FindFileData.cFileName);
         }
 
         if (!lpEnumProc(Info))
             break;
 
-    }
-    while (FindNextFileW(hFind, &FindFileData) != 0);
+    } while (FindNextFileW(hFind, &FindFileData) != 0);
 
     FindClose(hFind);
 
@@ -386,17 +343,16 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
 VOID FreeCachedAvailableEntries(VOID)
 {
     PAPPLICATION_INFO Info;
-
+    POSITION InfoListPosition = InfoList.GetHeadPosition();
     /* loop and deallocate all the cached app infos in the list */
-    for (pCachedEntry = CachedEntriesHead.Flink; pCachedEntry != &CachedEntriesHead;)
+    while (InfoListPosition)
     {
-        Info = CONTAINING_RECORD(pCachedEntry, APPLICATION_INFO, List);
-
-        /* grab a reference to the next linked entry before getting rid of the current one */
-        pCachedEntry = pCachedEntry->Flink;
+        Info = InfoList.GetAt(InfoListPosition);
+        InfoList.RemoveHead();
 
         /* flush them down the toilet :D */
-        RemoveEntryList(&Info->List);
-        HeapFree(GetProcessHeap(), 0, Info);
+        delete Info;
+
+        InfoListPosition = InfoList.GetHeadPosition();
     }
 }
\ No newline at end of file
index 63c5b62..2961dfb 100644 (file)
@@ -3,6 +3,22 @@
 class CRichEdit :
     public CWindow
 {
+    HMODULE LoadedLibrary;
+    inline VOID GenericInsertText(LPCWSTR lpszText, LONG InsertedTextLen, DWORD dwEffects)
+    {
+        SETTEXTEX SetText;
+        LONG Len = GetTextLen();
+
+        /* Insert new text */
+        SetText.flags = ST_SELECTION;
+        SetText.codepage = 1200;
+
+        SendMessageW(EM_SETTEXTEX, (WPARAM) &SetText, (LPARAM) lpszText);
+
+        SetRangeFormatting(Len, Len + InsertedTextLen,
+            (dwEffects == CFM_LINK) ? (PathIsURLW(lpszText) ? dwEffects : 0) : dwEffects);
+    }
+
 public:
     VOID SetRangeFormatting(LONG Start, LONG End, DWORD dwEffects)
     {
@@ -41,19 +57,13 @@ public:
     */
     VOID InsertText(LPCWSTR lpszText, DWORD dwEffects)
     {
-        SETTEXTEX SetText;
-        LONG Len = GetTextLen();
-
-        /* Insert new text */
-        SetText.flags = ST_SELECTION;
-        SetText.codepage = 1200;
-
-        SendMessageW(EM_SETTEXTEX, (WPARAM) &SetText, (LPARAM) lpszText);
-
-        SetRangeFormatting(Len, Len + wcslen(lpszText),
-            (dwEffects == CFM_LINK) ? (PathIsURLW(lpszText) ? dwEffects : 0) : dwEffects);
+        GenericInsertText(lpszText, wcslen(lpszText), dwEffects);
     }
 
+    VOID InsertText(const ATL::CStringW& szText, DWORD dwEffects)
+    {
+        GenericInsertText(szText.GetString(), szText.GetLength(), dwEffects);
+    }
     /*
     * Clear old text and add new
     */
@@ -63,10 +73,15 @@ public:
         InsertText(lpszText, dwEffects);
     }
 
+    VOID SetText(const ATL::CStringW& szText, DWORD dwEffects)
+    {
+        SetText(szText.GetString(), dwEffects);
+    }
+
     HWND Create(HWND hwndParent)
     {
         // TODO: FreeLibrary when the window is destroyed
-        LoadLibraryW(L"riched20.dll");
+        LoadedLibrary = LoadLibraryW(L"riched20.dll");
 
         m_hWnd = CreateWindowExW(0,
             L"RichEdit20W",
@@ -94,4 +109,9 @@ public:
     {
     }
 
+    ~CRichEdit()
+    {
+        FreeLibrary(LoadedLibrary);
+    }
+
 };
index 80c2295..71de7c3 100644 (file)
@@ -1,6 +1,7 @@
 /* PROJECT:     ReactOS CE Applications Manager
  * LICENSE:     GPL - See COPYING in the top level directory
- * AUTHORS:     David Quintana <gigaherz@gmail.com>
+ * AUTHORS:     David Quintana             <gigaherz@gmail.com>
+ *              Alexander Shaposhnikov     <chaez.san@gmail.com>
  */
 
 #include "rapps.h"
@@ -37,11 +38,11 @@ class CMainToolbar :
         HICON hImage;
 
         if (!(hImage = (HICON) LoadImage(hInst,
-            MAKEINTRESOURCE(ImageIndex),
-            IMAGE_ICON,
-            TOOLBAR_HEIGHT,
-            TOOLBAR_HEIGHT,
-            0)))
+                                         MAKEINTRESOURCE(ImageIndex),
+                                         IMAGE_ICON,
+                                         TOOLBAR_HEIGHT,
+                                         TOOLBAR_HEIGHT,
+                                         0)))
         {
             /* TODO: Error message */
         }
@@ -56,10 +57,10 @@ class CMainToolbar :
 
         /* Create the toolbar icon image list */
         hImageList = ImageList_Create(TOOLBAR_HEIGHT,//GetSystemMetrics(SM_CXSMICON),
-            TOOLBAR_HEIGHT,//GetSystemMetrics(SM_CYSMICON),
-            ILC_MASK | GetSystemColorDepth(),
-            1,
-            1);
+                                      TOOLBAR_HEIGHT,//GetSystemMetrics(SM_CYSMICON),
+                                      ILC_MASK | GetSystemColorDepth(),
+                                      1,
+                                      1);
         if (!hImageList)
         {
             /* TODO: Error message */
@@ -107,7 +108,7 @@ public:
         case ID_REFRESH:
             lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_REFRESH);
             break;
-           
+
         case ID_RESETDB:
             lpttt->lpszText = MAKEINTRESOURCE(IDS_TOOLTIP_UPDATE_DB);
             break;
@@ -116,7 +117,10 @@ public:
 
     HWND Create(HWND hwndParent)
     {
-        static TBBUTTON Buttons [] =
+        HIMAGELIST hImageList;
+
+        // buttons
+        static TBBUTTON Buttons[] =
         {   /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
             { 0, ID_INSTALL, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, (INT_PTR) szInstallBtn },
             { 1, ID_UNINSTALL, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, (INT_PTR) szUninstallBtn },
@@ -129,22 +133,15 @@ public:
             { 6, ID_EXIT, TBSTATE_ENABLED, BTNS_BUTTON | BTNS_AUTOSIZE, { 0 }, 0, 0 }
         };
 
-        INT NumButtons = _countof(Buttons);
-        HIMAGELIST hImageList;
-
         LoadStringW(hInst, IDS_INSTALL, szInstallBtn, _countof(szInstallBtn));
         LoadStringW(hInst, IDS_UNINSTALL, szUninstallBtn, _countof(szUninstallBtn));
         LoadStringW(hInst, IDS_MODIFY, szModifyBtn, _countof(szModifyBtn));
 
-        m_hWnd = CreateWindowExW(0,
-            TOOLBARCLASSNAMEW,
-            NULL,
-            WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | TBSTYLE_LIST,
-            0, 0, 0, 0,
-            hwndParent,
-            0,
-            hInst,
-            NULL);
+        m_hWnd = CreateWindowExW(0, TOOLBARCLASSNAMEW, NULL,
+                                 WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | TBSTYLE_LIST,
+                                 0, 0, 0, 0,
+                                 hwndParent,
+                                 0, hInst, NULL);
 
         if (!m_hWnd)
         {
@@ -165,7 +162,7 @@ public:
 
         ImageList_Destroy((HIMAGELIST) SetImageList(hImageList));
 
-        AddButtons(NumButtons, Buttons);
+        AddButtons(_countof(Buttons), Buttons);
 
         return m_hWnd;
     }
@@ -190,7 +187,7 @@ public:
 
     VOID ColumnClick(LPNMLISTVIEW pnmv)
     {
-        SortContext ctx = { this, pnmv->iSubItem };
+        SortContext ctx = {this, pnmv->iSubItem};
 
         SortItems(s_CompareFunc, &ctx);
 
@@ -223,6 +220,13 @@ public:
         return (PVOID) Item.lParam;
     }
 
+    BOOL AddColumn(INT Index, ATL::CStringW& Text, INT Width, INT Format)
+    {
+        BOOL result = AddColumn(Index, Text.GetBuffer(), Width, Format);
+
+        return result;
+    }
+
     BOOL AddColumn(INT Index, LPWSTR lpText, INT Width, INT Format)
     {
         LV_COLUMN Column;
@@ -258,10 +262,10 @@ public:
         SortContext * ctx = ((SortContext*) lParamSort);
         return ctx->lvw->CompareFunc(lParam1, lParam2, ctx->iSubItem);
     }
-    
+
     INT CompareFunc(LPARAM lParam1, LPARAM lParam2, INT iSubItem)
     {
-        WCHAR Item1[MAX_STR_LEN], Item2[MAX_STR_LEN];
+        ATL::CStringW Item1, Item2;
         LVFINDINFO IndexInfo;
         INT Index;
 
@@ -269,26 +273,28 @@ public:
 
         IndexInfo.lParam = lParam1;
         Index = FindItem(-1, &IndexInfo);
-        GetItemText(Index, iSubItem, Item1, _countof(Item1));
+        GetItemText(Index, iSubItem, Item1.GetBuffer(MAX_STR_LEN), MAX_STR_LEN);
+        Item1.ReleaseBuffer();
 
         IndexInfo.lParam = lParam2;
         Index = FindItem(-1, &IndexInfo);
-        GetItemText(Index, iSubItem, Item2, _countof(Item2));
+        GetItemText(Index, iSubItem, Item2.GetBuffer(MAX_STR_LEN), MAX_STR_LEN);
+        Item2.ReleaseBuffer();
 
         if (bAscending)
-            return wcscmp(Item2, Item1);
+            return Item2 == Item1;
         else
-            return wcscmp(Item1, Item2);
+            return Item1 == Item2;
 
         return 0;
     }
 
     HWND Create(HWND hwndParent)
     {
-        RECT r = { 205, 28, 465, 250 };
+        RECT r = {205, 28, 465, 250};
         DWORD style = WS_CHILD | WS_VISIBLE | LVS_SORTASCENDING | LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS;
         HMENU menu = GetSubMenu(LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_APPLICATIONMENU)), 0);
-        
+
         HWND hwnd = CListView::Create(hwndParent, r, NULL, style, WS_EX_CLIENTEDGE, menu);
 
         if (hwnd)
@@ -299,6 +305,87 @@ public:
 
 };
 
+class CSideTreeView :
+    public CUiWindow<CTreeView>
+{
+    HIMAGELIST hImageTreeView = ImageList_Create(TREEVIEW_ICON_SIZE, TREEVIEW_ICON_SIZE,
+                                                 GetSystemColorDepth() | ILC_MASK,
+                                                 0, 1);
+
+public:
+    HTREEITEM AddItem(HTREEITEM hParent, ATL::CStringW &Text, INT Image, INT SelectedImage, LPARAM lParam)
+    {
+        HTREEITEM result = CUiWindow<CTreeView>::AddItem(hParent, Text.GetBuffer(), Image, SelectedImage, lParam);
+        Text.ReleaseBuffer();
+        return result;
+    }
+
+    HTREEITEM AddCategory(HTREEITEM hRootItem, UINT TextIndex, UINT IconIndex)
+    {
+        ATL::CStringW szText;
+        INT Index;
+        HICON hIcon;
+
+        hIcon = (HICON) LoadImage(hInst,
+                                  MAKEINTRESOURCE(IconIndex),
+                                  IMAGE_ICON,
+                                  TREEVIEW_ICON_SIZE,
+                                  TREEVIEW_ICON_SIZE,
+                                  LR_CREATEDIBSECTION);
+        if (hIcon)
+        {
+            Index = ImageList_AddIcon(hImageTreeView, hIcon);
+            DestroyIcon(hIcon);
+        }
+
+        szText.LoadStringW(hInst, TextIndex);
+        return AddItem(hRootItem, szText, Index, Index, TextIndex);
+    }
+
+    HIMAGELIST SetImageList()
+    {
+        return CUiWindow<CTreeView>::SetImageList(hImageTreeView, TVSIL_NORMAL);
+    }
+
+    VOID DestroyImageList()
+    {
+        if (hImageTreeView)
+            ImageList_Destroy(hImageTreeView);
+    }
+
+    ~CSideTreeView()
+    {
+        DestroyImageList();
+        CUiWindow<CTreeView>::~CUiWindow();
+    }
+};
+
+class CSearchBar :
+    public CWindow
+{
+public:
+    VOID SetText(LPCWSTR lpszText)
+    {
+        SendMessage(SB_SETTEXT, SBT_NOBORDERS, (LPARAM) lpszText);
+    }
+
+    HWND Create(HWND hwndParent)
+    {
+        ATL::CStringW szBuf;
+        m_hWnd = CreateWindowExW(WS_EX_CLIENTEDGE, L"Edit", NULL,
+                                 WS_CHILD | WS_VISIBLE | ES_LEFT | ES_AUTOHSCROLL,
+                                 0, 0, 200, 22,
+                                 hwndParent, (HMENU) NULL,
+                                 hInst, 0);
+
+        SendMessageW(WM_SETFONT, (WPARAM) GetStockObject(DEFAULT_GUI_FONT), 0);
+        szBuf.LoadStringW(hInst, IDS_SEARCH_TEXT);
+        SetWindowTextW(szBuf);
+        return m_hWnd;
+    }
+
+};
+
 class CMainWindow :
     public CWindowImpl<CMainWindow, CWindow, CFrameWinTraits>
 {
@@ -309,22 +396,19 @@ class CMainWindow :
     CMainToolbar * m_Toolbar;
     CAppsListView * m_ListView;
 
-    CUiWindow<CTreeView> * m_TreeView;
+    CSideTreeView * m_TreeView;
     CUiWindow<CStatusBar> * m_StatusBar;
     CUiWindow<CRichEdit> * m_RichEdit;
 
-    CUiWindow<> * m_SearchBar;
-
-    HIMAGELIST hImageTreeView;
+    CUiWindow<CSearchBar> * m_SearchBar;
 
-    PWSTR pLink;
+    LPWSTR pLink;
 
     BOOL SearchEnabled;
 
 public:
     CMainWindow() :
         m_ClientPanel(NULL),
-        hImageTreeView(NULL),
         pLink(NULL),
         SearchEnabled(TRUE)
     {
@@ -334,16 +418,16 @@ private:
 
     VOID InitApplicationsList(VOID)
     {
-        WCHAR szText[MAX_STR_LEN];
+        ATL::CStringW szText;
 
         /* Add columns to ListView */
-        LoadStringW(hInst, IDS_APP_NAME, szText, _countof(szText));
+        szText.LoadStringW(hInst, IDS_APP_NAME);
         m_ListView->AddColumn(0, szText, 200, LVCFMT_LEFT);
 
-        LoadStringW(hInst, IDS_APP_INST_VERSION, szText, _countof(szText));
+        szText.LoadStringW(hInst, IDS_APP_INST_VERSION);
         m_ListView->AddColumn(1, szText, 90, LVCFMT_RIGHT);
 
-        LoadStringW(hInst, IDS_APP_DESCRIPTION, szText, _countof(szText));
+        szText.LoadStringW(hInst, IDS_APP_DESCRIPTION);
         m_ListView->AddColumn(3, szText, 250, LVCFMT_LEFT);
 
         UpdateApplicationsList(ENUM_ALL_COMPONENTS);
@@ -351,23 +435,7 @@ private:
 
     HTREEITEM AddCategory(HTREEITEM hRootItem, UINT TextIndex, UINT IconIndex)
     {
-        WCHAR szText[MAX_STR_LEN];
-        INT Index;
-        HICON hIcon;
-
-        hIcon = (HICON) LoadImage(hInst,
-            MAKEINTRESOURCE(IconIndex),
-            IMAGE_ICON,
-            TREEVIEW_ICON_SIZE,
-            TREEVIEW_ICON_SIZE,
-            LR_CREATEDIBSECTION);
-
-        Index = ImageList_AddIcon(hImageTreeView, hIcon);
-        DestroyIcon(hIcon);
-
-        LoadStringW(hInst, TextIndex, szText, _countof(szText));
-
-        return m_TreeView->AddItem(hRootItem, szText, Index, Index, TextIndex);
+        return m_TreeView->AddCategory(hRootItem, TextIndex, IconIndex);
     }
 
     VOID InitCategoriesList(VOID)
@@ -391,7 +459,7 @@ private:
         AddCategory(hRootItem, IDS_CAT_LIBS, IDI_CAT_LIBS);
         AddCategory(hRootItem, IDS_CAT_OTHER, IDI_CAT_OTHER);
 
-        m_TreeView->SetImageList(hImageTreeView, TVSIL_NORMAL);
+        m_TreeView->SetImageList();
         m_TreeView->Expand(hRootItem, TVE_EXPAND);
         m_TreeView->SelectItem(hRootItem);
     }
@@ -403,7 +471,7 @@ private:
         m_StatusBar->m_HorizontalAlignment = UiAlign_Stretch;
         m_ClientPanel->Children().Append(m_StatusBar);
 
-        return m_StatusBar->Create(m_hWnd, (HMENU)IDC_STATUSBAR) != NULL;
+        return m_StatusBar->Create(m_hWnd, (HMENU) IDC_STATUSBAR) != NULL;
     }
 
     BOOL CreateToolbar()
@@ -412,17 +480,17 @@ private:
         m_Toolbar->m_VerticalAlignment = UiAlign_LeftTop;
         m_Toolbar->m_HorizontalAlignment = UiAlign_Stretch;
         m_ClientPanel->Children().Append(m_Toolbar);
-        
+
         return m_Toolbar->Create(m_hWnd) != NULL;
     }
 
     BOOL CreateTreeView()
     {
-        m_TreeView = new CUiWindow<CTreeView>();
+        m_TreeView = new CSideTreeView();
         m_TreeView->m_VerticalAlignment = UiAlign_Stretch;
         m_TreeView->m_HorizontalAlignment = UiAlign_Stretch;
         m_VSplitter->First().Append(m_TreeView);
-        
+
         return m_TreeView->Create(m_hWnd) != NULL;
     }
 
@@ -478,37 +546,13 @@ private:
 
     BOOL CreateSearchBar(VOID)
     {
-        WCHAR szBuf[MAX_STR_LEN];
-
-        // TODO: WRAPPER
-        m_SearchBar = new CUiWindow<>();
+        m_SearchBar = new CUiWindow<CSearchBar>();
         m_SearchBar->m_VerticalAlignment = UiAlign_LeftTop;
         m_SearchBar->m_HorizontalAlignment = UiAlign_RightBtm;
         m_SearchBar->m_Margin.top = 6;
         m_SearchBar->m_Margin.right = 6;
-        //m_ClientPanel->Children().Append(m_SearchBar);
-
-        HWND hwnd = CreateWindowExW(WS_EX_CLIENTEDGE,
-            L"Edit",
-            NULL,
-            WS_CHILD | WS_VISIBLE | ES_LEFT | ES_AUTOHSCROLL,
-            0,
-            0,
-            200,
-            22,
-            m_Toolbar->m_hWnd,
-            (HMENU) 0,
-            hInst,
-            0);
-
-        m_SearchBar->m_hWnd = hwnd;
 
-        m_SearchBar->SendMessageW(WM_SETFONT, (WPARAM) GetStockObject(DEFAULT_GUI_FONT), 0);
-
-        LoadStringW(hInst, IDS_SEARCH_TEXT, szBuf, _countof(szBuf));
-        m_SearchBar->SetWindowTextW(szBuf);
-
-        return hwnd != NULL;
+        return m_SearchBar->Create(m_Toolbar->m_hWnd) != NULL;
     }
 
     BOOL CreateLayout()
@@ -548,7 +592,7 @@ private:
             ::GetWindowRect(m_StatusBar->m_hWnd, &rBottom);
 
             m_VSplitter->m_Margin.top = rTop.bottom - rTop.top;
-            m_VSplitter->m_Margin.bottom = rBottom.bottom-rBottom.top;
+            m_VSplitter->m_Margin.bottom = rBottom.bottom - rBottom.top;
         }
 
         return b;
@@ -556,23 +600,17 @@ private:
 
     BOOL InitControls()
     {
-        /* Create image list */
-        hImageTreeView = ImageList_Create(TREEVIEW_ICON_SIZE, TREEVIEW_ICON_SIZE,
-            GetSystemColorDepth() | ILC_MASK,
-            0, 1);
-
         if (CreateLayout())
         {
-            WCHAR szBuffer1[MAX_STR_LEN], szBuffer2[MAX_STR_LEN];
+            ATL::CStringW szBuffer1, szBuffer2;
 
             InitApplicationsList();
 
             InitCategoriesList();
 
-            LoadStringW(hInst, IDS_APPS_COUNT, szBuffer2, _countof(szBuffer2));
-            StringCbPrintfW(szBuffer1, sizeof(szBuffer1),
-                szBuffer2,
-                m_ListView->GetItemCount());
+            szBuffer2.LoadStringW(hInst, IDS_APPS_COUNT);
+            szBuffer1.Format(szBuffer2, m_ListView->GetItemCount());
+
             m_StatusBar->SetText(szBuffer1);
             return TRUE;
         }
@@ -589,7 +627,7 @@ private:
         m_Toolbar->AutoSize();
 
 
-        RECT r = { 0, 0, LOWORD(lParam), HIWORD(lParam) };
+        RECT r = {0, 0, LOWORD(lParam), HIWORD(lParam)};
 
         HDWP hdwp = NULL;
 
@@ -627,9 +665,6 @@ private:
             if (IS_INSTALLED_ENUM(SelectedEnumType))
                 FreeInstalledAppList();
 
-            if (hImageTreeView)
-                ImageList_Destroy(hImageTreeView);
-
             delete m_ClientPanel;
 
             PostQuitMessage(0);
@@ -898,9 +933,9 @@ private:
         {
             if (pLink) HeapFree(GetProcessHeap(), 0, pLink);
 
-            pLink = (PWSTR) HeapAlloc(GetProcessHeap(), 0,
+            pLink = (LPWSTR) HeapAlloc(GetProcessHeap(), 0,
                 (max(Link->chrg.cpMin, Link->chrg.cpMax) -
-                min(Link->chrg.cpMin, Link->chrg.cpMax) + 1) * sizeof(WCHAR));
+                 min(Link->chrg.cpMin, Link->chrg.cpMax) + 1) * sizeof(WCHAR));
             if (!pLink)
             {
                 /* TODO: Error message */
@@ -941,17 +976,17 @@ private:
 
         if (lParam == (LPARAM) m_SearchBar->m_hWnd)
         {
-            WCHAR szBuf[MAX_STR_LEN];
+            ATL::CStringW szBuf;
 
             switch (HIWORD(wParam))
             {
             case EN_SETFOCUS:
             {
-                WCHAR szWndText[MAX_STR_LEN];
+                ATL::CStringW szWndText;
 
-                LoadStringW(hInst, IDS_SEARCH_TEXT, szBuf, _countof(szBuf));
-                m_SearchBar->GetWindowTextW(szWndText, MAX_STR_LEN);
-                if (wcscmp(szBuf, szWndText) == 0)
+                szBuf.LoadStringW(hInst, IDS_SEARCH_TEXT);
+                m_SearchBar->GetWindowTextW(szWndText);
+                if (szBuf == szWndText)
                 {
                     SearchEnabled = FALSE;
                     m_SearchBar->SetWindowTextW(L"");
@@ -961,19 +996,19 @@ private:
 
             case EN_KILLFOCUS:
             {
-                m_SearchBar->GetWindowTextW(szBuf, MAX_STR_LEN);
-                if (wcslen(szBuf) < 1)
+                m_SearchBar->GetWindowTextW(szBuf);
+                if (szBuf.IsEmpty())
                 {
-                    LoadStringW(hInst, IDS_SEARCH_TEXT, szBuf, _countof(szBuf));
+                    szBuf.LoadStringW(hInst, IDS_SEARCH_TEXT);
                     SearchEnabled = FALSE;
-                    m_SearchBar->SetWindowTextW(szBuf);
+                    m_SearchBar->SetWindowTextW(szBuf.GetString());
                 }
             }
             break;
 
             case EN_CHANGE:
             {
-                WCHAR szWndText[MAX_STR_LEN];
+                ATL::CStringW szWndText;
 
                 if (!SearchEnabled)
                 {
@@ -981,16 +1016,15 @@ private:
                     break;
                 }
 
-                LoadStringW(hInst, IDS_SEARCH_TEXT, szBuf, _countof(szBuf));
-                m_SearchBar->GetWindowTextW(szWndText, MAX_STR_LEN);
-                if (wcscmp(szBuf, szWndText) != 0)
+                szBuf.LoadStringW(hInst, IDS_SEARCH_TEXT);
+                m_SearchBar->GetWindowTextW(szWndText);
+                if (szBuf == szWndText)
                 {
-                    StringCbCopy(szSearchPattern, sizeof(szSearchPattern),
-                        szWndText);
+                    szSearchPattern.Empty();
                 }
                 else
                 {
-                    szSearchPattern[0] = UNICODE_NULL;
+                    szSearchPattern = szWndText;
                 }
 
                 DWORD dwDelay;
@@ -1075,7 +1109,7 @@ private:
             if (Info)
             {
                 RegCloseKey(Info->hSubKey);
-                HeapFree(GetProcessHeap(), 0, Info);
+                delete Info;
             }
             Count--;
         }
@@ -1092,7 +1126,7 @@ private:
     static BOOL CALLBACK s_EnumInstalledAppProc(INT ItemIndex, LPWSTR lpName, PINSTALLED_INFO Info)
     {
         PINSTALLED_INFO ItemInfo;
-        WCHAR szText[MAX_PATH];
+        ATL::CStringW szText;
         INT Index;
 
         if (!SearchPatternMatch(lpName, szSearchPattern))
@@ -1114,12 +1148,13 @@ private:
 
         /* Get version info */
         GetApplicationString(ItemInfo->hSubKey, L"DisplayVersion", szText);
-        ListView_SetItemText(hListView, Index, 1, szText);
+        ListView_SetItemText(hListView, Index, 1, szText.GetBuffer(MAX_PATH));
+        szText.ReleaseBuffer();
 
         /* Get comments */
         GetApplicationString(ItemInfo->hSubKey, L"Comments", szText);
-        ListView_SetItemText(hListView, Index, 2, szText);
-
+        ListView_SetItemText(hListView, Index, 2, szText.GetBuffer(MAX_PATH));
+        szText.ReleaseBuffer();
         return TRUE;
     }
 
@@ -1127,8 +1162,7 @@ private:
     {
         INT Index;
         HICON hIcon = NULL;
-        bool failed = false;
-        WCHAR szIconPath[MAX_PATH] = L"";
+        ATL::CStringW szIconPath;
         HIMAGELIST hImageListView = NULL;
         hImageListView = ListView_GetImageList(hListView, LVSIL_SMALL);
 
@@ -1137,21 +1171,23 @@ private:
         {
             return TRUE;
         }
-        //TODO: Define another field for icon name in the DB
-        failed = !GetStorageDirectory(szIconPath, _countof(szIconPath)) 
-          || FAILED(StringCbPrintfW(szIconPath, sizeof(szIconPath), L"%ls\\rapps\\icons\\%ls.ico", szIconPath, Info->szName));
-        if (!failed) {
-          //Load icon from file
-          hIcon = (HICON)LoadImage(NULL,
-            szIconPath,
-            IMAGE_ICON,
-            LISTVIEW_ICON_SIZE,
-            LISTVIEW_ICON_SIZE,
-            LR_LOADFROMFILE);
+
+        if (GetStorageDirectory(szIconPath))
+        {
+            /* Load icon from file */
+            szIconPath += L"\\rapps\\icons\\" + Info->szName + L".ico";
+            hIcon = (HICON) LoadImageW(NULL,
+                                       szIconPath.GetString(),
+                                       IMAGE_ICON,
+                                       LISTVIEW_ICON_SIZE,
+                                       LISTVIEW_ICON_SIZE,
+                                       LR_LOADFROMFILE);
         }
-        if (GetLastError() != ERROR_SUCCESS) {
-          //Load default icon
-          hIcon = (HICON)LoadIcon(hInst, MAKEINTRESOURCE(IDI_MAIN));
+
+        if (!hIcon)
+        {
+            /* Load default icon */
+            hIcon = (HICON) LoadIcon(hInst, MAKEINTRESOURCEW(IDI_MAIN));
         }
         Index = ImageList_AddIcon(hImageListView, hIcon);
         DestroyIcon(hIcon);
@@ -1159,15 +1195,18 @@ private:
         Index = ListViewAddItem(Info->Category, Index, Info->szName, (LPARAM) Info);
         hImageListView = ListView_SetImageList(hListView, hImageListView, LVSIL_SMALL);
 
-        ListView_SetItemText(hListView, Index, 1, Info->szVersion);
-        ListView_SetItemText(hListView, Index, 2, Info->szDesc);
+        ListView_SetItemText(hListView, Index, 1, Info->szVersion.GetBuffer(MAX_PATH));
+        Info->szVersion.ReleaseBuffer();
+
+        ListView_SetItemText(hListView, Index, 2, Info->szDesc.GetBuffer(MAX_PATH));
+        Info->szDesc.ReleaseBuffer();
         return TRUE;
     }
 
 
     VOID UpdateApplicationsList(INT EnumType)
     {
-        WCHAR szBuffer1[MAX_STR_LEN], szBuffer2[MAX_STR_LEN];
+        ATL::CStringW szBuffer1, szBuffer2;
         HIMAGELIST hImageListView = NULL;
 
         m_ListView->SendMessage(WM_SETREDRAW, FALSE, 0);
@@ -1178,27 +1217,26 @@ private:
             FreeInstalledAppList();
 
         (VOID) ListView_DeleteAllItems(hListView);
-        //Create new ImageList
+        /* Create new ImageList */
         hImageListView = ImageList_Create(LISTVIEW_ICON_SIZE,
-          LISTVIEW_ICON_SIZE,
-          GetSystemColorDepth() | ILC_MASK,
-          0, 1);
+                                          LISTVIEW_ICON_SIZE,
+                                          GetSystemColorDepth() | ILC_MASK,
+                                          0, 1);
         hImageListView = ListView_SetImageList(hListView, hImageListView, LVSIL_SMALL);
 
         if (hImageListView)
-          ImageList_Destroy(hImageListView);
+            ImageList_Destroy(hImageListView);
 
-        if (IS_AVAILABLE_ENUM(EnumType)) {
-          /* Enum available applications */
-          EnumAvailableApplications(EnumType, s_EnumAvailableAppProc);
+        if (IS_AVAILABLE_ENUM(EnumType))
+        {
+            /* Enum available applications */
+            EnumAvailableApplications(EnumType, s_EnumAvailableAppProc);
         }
 
         SelectedEnumType = EnumType;
 
-        LoadStringW(hInst, IDS_APPS_COUNT, szBuffer2, _countof(szBuffer2));
-        StringCbPrintfW(szBuffer1, sizeof(szBuffer1),
-            szBuffer2,
-            ListView_GetItemCount(hListView));
+        szBuffer2.LoadStringW(hInst, IDS_APPS_COUNT);
+        szBuffer1.Format(szBuffer2, ListView_GetItemCount(hListView));
         SetStatusBarText(szBuffer1);
 
         SetWelcomeText();
@@ -1217,7 +1255,7 @@ public:
         static ATL::CWndClassInfo wc =
         {
             { sizeof(WNDCLASSEX), csStyle, StartWindowProc,
-            0, 0, NULL, 
+            0, 0, NULL,
             LoadIcon(_AtlBaseModule.GetModuleInstance(), MAKEINTRESOURCE(IDI_MAIN)),
             LoadCursor(NULL, IDC_ARROW),
             (HBRUSH) (COLOR_BTNFACE + 1), MAKEINTRESOURCE(IDR_MAINMENU),
@@ -1229,9 +1267,8 @@ public:
 
     HWND Create()
     {
-        WCHAR szWindowName[MAX_STR_LEN];
-
-        LoadStringW(hInst, IDS_APPTITLE, szWindowName, _countof(szWindowName));
+        ATL::CStringW szWindowName;
+        szWindowName.LoadStringW(hInst, IDS_APPTITLE);
 
         RECT r = {
             (SettingsInfo.bSaveWndPos ? SettingsInfo.Left : CW_USEDEFAULT),
@@ -1242,7 +1279,7 @@ public:
         r.right += r.left;
         r.bottom += r.top;
 
-        return CWindowImpl::Create(NULL, r, szWindowName, WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
+        return CWindowImpl::Create(NULL, r, szWindowName.GetString(), WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_WINDOWEDGE);
     }
 
     CStatusBar * GetStatusBar()
@@ -1278,22 +1315,46 @@ DWORD_PTR ListViewGetlParam(INT item)
     return g_MainWindow->GetListView()->GetItemData(item);
 }
 
-VOID SetStatusBarText(PCWSTR szText)
+VOID SetStatusBarText(LPCWSTR szText)
 {
     g_MainWindow->GetStatusBar()->SetText(szText);
 }
 
-INT ListViewAddItem(INT ItemIndex, INT IconIndex, PWSTR lpName, LPARAM lParam)
+INT ListViewAddItem(INT ItemIndex, INT IconIndex, LPWSTR lpName, LPARAM lParam)
 {
     return g_MainWindow->GetListView()->AddItem(ItemIndex, IconIndex, lpName, lParam);
 }
 
-VOID NewRichEditText(PCWSTR szText, DWORD flags)
+VOID NewRichEditText(LPCWSTR szText, DWORD flags)
 {
     g_MainWindow->GetRichEdit()->SetText(szText, flags);
 }
 
-VOID InsertRichEditText(PCWSTR szText, DWORD flags)
+VOID InsertRichEditText(LPCWSTR szText, DWORD flags)
 {
     g_MainWindow->GetRichEdit()->InsertText(szText, flags);
-}
\ No newline at end of file
+}
+
+/* ATL version of functions */
+VOID SetStatusBarText(const ATL::CStringW& szText)
+{
+    SetStatusBarText(szText.GetString());
+}
+
+INT ListViewAddItem(INT ItemIndex, INT IconIndex, ATL::CStringW & Name, LPARAM lParam)
+{
+    INT result = ListViewAddItem(ItemIndex, IconIndex, Name.GetBuffer(), lParam);
+    Name.ReleaseBuffer();
+    return result;
+}
+
+VOID NewRichEditText(const ATL::CStringW& szText, DWORD flags)
+{
+    NewRichEditText(szText.GetString(), flags);
+}
+
+VOID InsertRichEditText(const ATL::CStringW& szText, DWORD flags)
+{
+    InsertRichEditText(szText.GetString(), flags);
+}
+
index a5d7888..12f729c 100644 (file)
@@ -3,13 +3,22 @@
  * LICENSE:         GPL - See COPYING in the top level directory
  * FILE:            base/applications/rapps/installed.cpp
  * PURPOSE:         Functions for working with installed applications
- * PROGRAMMERS:     Dmitry Chapyshev (dmitry@reactos.org)
+ * PROGRAMMERS:     Dmitry Chapyshev            (dmitry@reactos.org)
+ *                  Alexander Shaposhnikov      (chaez.san@gmail.com)
  */
 
 #include "rapps.h"
 
 BOOL
-GetApplicationString(HKEY hKey, LPCWSTR lpKeyName, LPWSTR lpString)
+GetApplicationString(HKEY hKey, LPCWSTR lpKeyName, ATL::CStringW& String)
+{
+    BOOL result = GetApplicationString(hKey, lpKeyName, String.GetBuffer(MAX_PATH));
+    String.ReleaseBuffer();
+    return result;
+}
+
+BOOL
+GetApplicationString(HKEY hKey, LPCWSTR lpKeyName, LPWSTR szString)
 {
     DWORD dwSize = MAX_PATH * sizeof(WCHAR);
 
@@ -17,26 +26,22 @@ GetApplicationString(HKEY hKey, LPCWSTR lpKeyName, LPWSTR lpString)
                          lpKeyName,
                          NULL,
                          NULL,
-                         (LPBYTE)lpString,
+                         (LPBYTE) szString,
                          &dwSize) == ERROR_SUCCESS)
     {
         return TRUE;
     }
 
-    (VOID)StringCchCopyW(lpString, MAX_PATH, L"---");
-
+    szString = L"---";
     return FALSE;
 }
 
-
-
 BOOL
-IsInstalledApplication(LPCWSTR lpRegName, BOOL IsUserKey, REGSAM keyWow)
+IsInstalledApplication(const ATL::CStringW &RegName, BOOL IsUserKey, REGSAM keyWow)
 {
     HKEY hKey = NULL;
     BOOL IsInstalled = FALSE;
-    WCHAR szPath[MAX_PATH] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
-    StringCbPrintfW(szPath, _countof(szPath), L"%ls\\%ls", szPath, lpRegName);
+    ATL::CStringW szPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + RegName;
 
     if (RegOpenKeyExW(IsUserKey ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
         szPath, 0, keyWow | KEY_READ,
@@ -49,33 +54,36 @@ IsInstalledApplication(LPCWSTR lpRegName, BOOL IsUserKey, REGSAM keyWow)
 }
 
 BOOL
-InstalledVersion(LPWSTR szVersionResult, UINT iVersionResultSize, LPCWSTR lpRegName, BOOL IsUserKey, REGSAM keyWow)
+InstalledVersion(ATL::CStringW& szVersionResult, const ATL::CStringW& RegName, BOOL IsUserKey, REGSAM keyWow)
 {
-    DWORD dwSize = MAX_PATH;
-    DWORD dwType = REG_SZ;
-    WCHAR szVersion[MAX_PATH] = L"";
     HKEY hKey;
     BOOL bHasVersion = FALSE;
-    iVersionResultSize = 0;
-    WCHAR szPath[MAX_PATH] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
-    StringCbPrintfW(szPath, _countof(szPath), L"%ls\\%ls", szPath, lpRegName);
+    ATL::CStringW szVersion;
+    ATL::CStringW szPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + RegName;
 
     if (RegOpenKeyExW(IsUserKey ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE,
-        szPath, 0, keyWow | KEY_READ,
+        szPath.GetString(), 0, keyWow | KEY_READ,
         &hKey) == ERROR_SUCCESS)
     {
-        dwSize = MAX_PATH;
+        DWORD dwSize = MAX_PATH;
+        DWORD dwType = REG_SZ;
         if (RegQueryValueExW(hKey,
             L"DisplayVersion",
             NULL,
             &dwType,
-            (LPBYTE) szVersion,
+            (LPBYTE) szVersion.GetBuffer(dwSize),
             &dwSize) == ERROR_SUCCESS)
         {
-            StringCbCopyW(szVersionResult, dwSize, szVersion);
+            szVersion.ReleaseBuffer();
+            szVersionResult = szVersion;
             bHasVersion = TRUE;
         }
+        else
+        {
+            szVersion.ReleaseBuffer();
+        }
     }
+
     RegCloseKey(hKey);
     return bHasVersion;
 }
@@ -84,8 +92,8 @@ InstalledVersion(LPWSTR szVersionResult, UINT iVersionResultSize, LPCWSTR lpRegN
 BOOL
 UninstallApplication(INT Index, BOOL bModify)
 {
-    WCHAR szModify[] = L"ModifyPath";
-    WCHAR szUninstall[] = L"UninstallString";
+    LPCWSTR szModify = L"ModifyPath";
+    LPCWSTR szUninstall = L"UninstallString";
     WCHAR szPath[MAX_PATH];
     WCHAR szAppName[MAX_STR_LEN];
     DWORD dwType, dwSize;
@@ -127,7 +135,7 @@ UninstallApplication(INT Index, BOOL bModify)
                          bModify ? szModify : szUninstall,
                          NULL,
                          &dwType,
-                         (LPBYTE)szPath,
+                         (LPBYTE) szPath,
                          &dwSize) != ERROR_SUCCESS)
     {
         return FALSE;
@@ -140,20 +148,20 @@ UninstallApplication(INT Index, BOOL bModify)
 BOOL
 ShowInstalledAppInfo(INT Index)
 {
-    WCHAR szText[MAX_PATH], szInfo[MAX_PATH];
+    ATL::CStringW szText;
+    ATL::CStringW szInfo;
     PINSTALLED_INFO Info = (PINSTALLED_INFO) ListViewGetlParam(Index);
 
     if (!Info || !Info->hSubKey) return FALSE;
 
     GetApplicationString(Info->hSubKey, L"DisplayName", szText);
     NewRichEditText(szText, CFE_BOLD);
-
     InsertRichEditText(L"\n", 0);
 
 #define GET_INFO(a, b, c, d) \
     if (GetApplicationString(Info->hSubKey, a, szInfo)) \
     { \
-        LoadStringW(hInst, b, szText, _countof(szText)); \
+        szText.LoadStringW(hInst, b); \
         InsertRichEditText(szText, c); \
         InsertRichEditText(szInfo, d); \
     } \
@@ -185,7 +193,7 @@ RemoveAppFromRegistry(INT Index)
 {
     PINSTALLED_INFO Info;
     WCHAR szFullName[MAX_PATH] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
-    WCHAR szMsgText[MAX_STR_LEN], szMsgTitle[MAX_STR_LEN];
+    ATL::CStringW szMsgText, szMsgTitle;
     INT ItemIndex = SendMessage(hListView, LVM_GETNEXTITEM, -1, LVNI_FOCUSED);
 
     if (!IS_INSTALLED_ENUM(SelectedEnumType))
@@ -194,13 +202,13 @@ RemoveAppFromRegistry(INT Index)
     Info = (PINSTALLED_INFO) ListViewGetlParam(Index);
     if (!Info || !Info->hSubKey || (ItemIndex == -1)) return;
 
-    if (!LoadStringW(hInst, IDS_APP_REG_REMOVE, szMsgText, _countof(szMsgText)) ||
-        !LoadStringW(hInst, IDS_INFORMATION, szMsgTitle, _countof(szMsgTitle)))
+    if (!szMsgText.LoadStringW(hInst, IDS_APP_REG_REMOVE) ||
+        !szMsgTitle.LoadStringW(hInst, IDS_INFORMATION))
         return;
 
     if (MessageBoxW(hMainWnd, szMsgText, szMsgTitle, MB_YESNO | MB_ICONQUESTION) == IDYES)
     {
-        wcsncat(szFullName, Info->szKeyName, MAX_PATH - wcslen(szFullName));
+        wcsncat(szFullName, Info->szKeyName.GetString(), MAX_PATH - wcslen(szFullName));
 
         if (RegDeleteKeyW(Info->hRootKey, szFullName) == ERROR_SUCCESS)
         {
@@ -208,10 +216,10 @@ RemoveAppFromRegistry(INT Index)
             return;
         }
 
-        if (!LoadStringW(hInst, IDS_UNABLE_TO_REMOVE, szMsgText, _countof(szMsgText)))
+        if (!szMsgText.LoadStringW(hInst, IDS_UNABLE_TO_REMOVE))
             return;
 
-        MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR);
+        MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
     }
 }
 
@@ -221,8 +229,8 @@ EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc)
 {
     DWORD dwSize = MAX_PATH, dwType, dwValue;
     BOOL bIsSystemComponent, bIsUpdate;
-    WCHAR pszParentKeyName[MAX_PATH];
-    WCHAR pszDisplayName[MAX_PATH];
+    ATL::CStringW szParentKeyName;
+    ATL::CStringW szDisplayName;
     INSTALLED_INFO Info;
     HKEY hKey;
     LONG ItemIndex = 0;
@@ -236,9 +244,10 @@ EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc)
         return FALSE;
     }
 
-    while (RegEnumKeyExW(hKey, ItemIndex, Info.szKeyName, &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
+    while (RegEnumKeyExW(hKey, ItemIndex, Info.szKeyName.GetBuffer(MAX_PATH), &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
     {
-        if (RegOpenKeyW(hKey, Info.szKeyName, &Info.hSubKey) == ERROR_SUCCESS)
+        Info.szKeyName.ReleaseBuffer();
+        if (RegOpenKeyW(hKey, Info.szKeyName.GetString(), &Info.hSubKey) == ERROR_SUCCESS)
         {
             dwType = REG_DWORD;
             dwSize = sizeof(DWORD);
@@ -247,7 +256,7 @@ EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc)
                                  L"SystemComponent",
                                  NULL,
                                  &dwType,
-                                 (LPBYTE)&dwValue,
+                                 (LPBYTE) &dwValue,
                                  &dwSize) == ERROR_SUCCESS)
             {
                 bIsSystemComponent = (dwValue == 0x1);
@@ -258,22 +267,24 @@ EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc)
             }
 
             dwType = REG_SZ;
-            dwSize = sizeof(pszParentKeyName);
+            dwSize = MAX_PATH;
             bIsUpdate = (RegQueryValueExW(Info.hSubKey,
                                           L"ParentKeyName",
                                           NULL,
                                           &dwType,
-                                          (LPBYTE)pszParentKeyName,
+                                          (LPBYTE) szParentKeyName.GetBuffer(dwSize),
                                           &dwSize) == ERROR_SUCCESS);
+            szParentKeyName.ReleaseBuffer();
 
-            dwSize = sizeof(pszDisplayName);
+            dwSize = sizeof(szDisplayName);
             if (RegQueryValueExW(Info.hSubKey,
                                  L"DisplayName",
                                  NULL,
                                  &dwType,
-                                 (LPBYTE)pszDisplayName,
+                                 (LPBYTE) szDisplayName.GetBuffer(dwSize),
                                  &dwSize) == ERROR_SUCCESS)
             {
+                szDisplayName.ReleaseBuffer();
                 if (EnumType < ENUM_ALL_COMPONENTS || EnumType > ENUM_UPDATES)
                     EnumType = ENUM_ALL_COMPONENTS;
 
@@ -283,7 +294,7 @@ EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc)
                         ((EnumType == ENUM_APPLICATIONS) && (!bIsUpdate)) || /* Applications only */
                         ((EnumType == ENUM_UPDATES) && (bIsUpdate))) /* Updates only */
                     {
-                        if (!lpEnumProc(ItemIndex, pszDisplayName, &Info))
+                        if (!lpEnumProc(ItemIndex, szDisplayName, &Info))
                             break;
                     }
                     else
@@ -298,6 +309,7 @@ EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc)
             }
             else
             {
+                szDisplayName.ReleaseBuffer();
                 RegCloseKey(Info.hSubKey);
             }
         }
@@ -306,6 +318,7 @@ EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc)
         ItemIndex++;
     }
 
+    Info.szKeyName.ReleaseBuffer();
     RegCloseKey(hKey);
 
     return TRUE;
index a1a2cfb..72c91fc 100644 (file)
 #include "rapps.h"
 #include <sha1.h>
 
+BOOL VerifyInteg(const ATL::CStringW &SHA1Hash, const ATL::CStringW &FileName)
+{
+    return VerifyInteg(SHA1Hash.GetString(), FileName.GetString());
+}
 
 BOOL VerifyInteg(LPCWSTR lpSHA1Hash, LPCWSTR lpFileName)
 {
index 36f4932..6ce1a17 100644 (file)
@@ -2,12 +2,14 @@
  * LICENSE:     GPL - See COPYING in the top level directory
  * FILE:        base/applications/rapps/loaddlg.cpp
  * PURPOSE:     Displaying a download dialog
- * COPYRIGHT:   Copyright 2001 John R. Sheets (for CodeWeavers)
- *              Copyright 2004 Mike McCormack (for CodeWeavers)
- *              Copyright 2005 Ge van Geldorp (gvg@reactos.org)
- *              Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
+ * COPYRIGHT:   Copyright 2001 John R. Sheets             (for CodeWeavers)
+ *              Copyright 2004 Mike McCormack             (for CodeWeavers)
+ *              Copyright 2005 Ge van Geldorp             (gvg@reactos.org)
+ *              Copyright 2009 Dmitry Chapyshev           (dmitry@reactos.org)
  *              Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
+ *              Copyright 2017 Alexander Shaposhnikov     (chaez.san@gmail.com)
  */
+
 /*
  * Based on Wine dlls/shdocvw/shdocvw_main.c
  *
@@ -46,15 +48,13 @@ class CDownloadDialog :
     HWND m_hDialog;
     PBOOL m_pbCancelled;
     BOOL m_UrlHasBeenCopied;
-    WCHAR m_ProgressText[MAX_PATH];
-
 
 public:
     ~CDownloadDialog()
     {
         DestroyWindow(m_hDialog);
     }
-    
+
     HRESULT Initialize(HWND Dlg, BOOL *pbCancelled)
     {
         m_hDialog = Dlg;
@@ -96,7 +96,7 @@ public:
         {
             WCHAR szProgress[100];
             WCHAR szProgressMax[100];
-            UINT uiPercentage = ((ULONGLONG)ulProgress * 100) / ulProgressMax;
+            UINT uiPercentage = ((ULONGLONG) ulProgress * 100) / ulProgressMax;
 
             /* send the current progress to the progress bar */
             SendMessageW(Item, PBM_SETPOS, uiPercentage, 0);
@@ -106,40 +106,37 @@ public:
             StrFormatByteSizeW(ulProgressMax, szProgressMax, _countof(szProgressMax));
 
             /* ...and post all of it to our subclassed progress bar text subroutine */
-            StringCbPrintfW(m_ProgressText,
-                            sizeof(m_ProgressText),
-                            L"%u%% \x2014 %ls / %ls",
-                            uiPercentage,
-                            szProgress,
-                            szProgressMax);
-            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM)m_ProgressText);
+            ATL::CStringW m_ProgressText;
+            m_ProgressText.Format(L"%u%% \x2014 %ls / %ls",
+                                  uiPercentage,
+                                  szProgress,
+                                  szProgressMax);
+            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) m_ProgressText.GetBuffer());
+            m_ProgressText.ReleaseBuffer();
         }
 
         Item = GetDlgItem(m_hDialog, IDC_DOWNLOAD_STATUS);
         if (Item && szStatusText && wcslen(szStatusText) > 0 && m_UrlHasBeenCopied == FALSE)
         {
             DWORD len = wcslen(szStatusText) + 1;
-            PWSTR buf = (PWSTR)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR));
+            ATL::CStringW buf;
 
-            if (buf)
+            /* beautify our url for display purposes */
+            if (!InternetCanonicalizeUrlW(szStatusText, buf.GetBuffer(len), &len, ICU_DECODE | ICU_NO_ENCODE))
             {
-                /* beautify our url for display purposes */
-                InternetCanonicalizeUrl(szStatusText, buf, &len, ICU_DECODE | ICU_NO_ENCODE);
+                /* just use the original */
+                buf.ReleaseBuffer();
+                buf = szStatusText;
             }
             else
             {
-                /* just use the original */
-                buf = (PWSTR)szStatusText;
+                buf.ReleaseBuffer();
             }
 
             /* paste it into our dialog and don't do it again in this instance */
-            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM)buf);
+            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) buf.GetBuffer());
+            buf.ReleaseBuffer();
             m_UrlHasBeenCopied = TRUE;
-
-            if (buf != szStatusText)
-            {
-                HeapFree(GetProcessHeap(), 0, buf);
-            }
         }
 
         SetLastError(0);
@@ -223,7 +220,7 @@ static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
                         LocalFree(certInfo.lpszEncryptionAlgName);
                     if (certInfo.lpszIssuerInfo)
                     {
-                        if (strcmp((LPSTR)certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
+                        if (strcmp((LPSTR) certInfo.lpszIssuerInfo, CERT_ISSUER_INFO) != 0)
                             Ret = FALSE;
                         LocalFree(certInfo.lpszIssuerInfo);
                     }
@@ -233,7 +230,7 @@ static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
                         LocalFree(certInfo.lpszSignatureAlgName);
                     if (certInfo.lpszSubjectInfo)
                     {
-                        if (strcmp((LPSTR)certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
+                        if (strcmp((LPSTR) certInfo.lpszSubjectInfo, CERT_SUBJECT_INFO) != 0)
                             Ret = FALSE;
                         LocalFree(certInfo.lpszSubjectInfo);
                     }
@@ -247,13 +244,20 @@ static BOOL CertIsValid(HINTERNET hInternet, LPWSTR lpszHostName)
 }
 #endif
 
+inline VOID
+MessageBox_LoadString(HWND hMainWnd, INT StringID)
+{
+    ATL::CString szMsgText;
+    if (szMsgText.LoadStringW(hInst, StringID))
+        MessageBoxW(hMainWnd, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
+}
 
 static
 DWORD WINAPI
 ThreadFunc(LPVOID Context)
 {
     CComPtr<IBindStatusCallback> dl;
-    WCHAR path[MAX_PATH];
+    ATL::CStringW Path;
     PWSTR p, q;
     HWND Dlg = (HWND) Context;
     ULONG dwContentLen, dwBytesWritten, dwBytesRead, dwStatus;
@@ -266,14 +270,14 @@ ThreadFunc(LPVOID Context)
     HINTERNET hFile = NULL;
     HANDLE hOut = INVALID_HANDLE_VALUE;
     unsigned char lpBuffer[4096];
-    PCWSTR lpszAgent = L"RApps/1.0";
+    LPCWSTR lpszAgent = L"RApps/1.0";
     URL_COMPONENTS urlComponents;
     size_t urlLength, filenameLength;
 
     /* build the path for the download */
     p = wcsrchr(AppInfo->szUrlDownload, L'/');
     q = wcsrchr(AppInfo->szUrlDownload, L'?');
+
     /* do we have a final slash separator? */
     if (!p)
         goto end;
@@ -287,35 +291,31 @@ ThreadFunc(LPVOID Context)
         filenameLength -= wcslen(q - 1) * sizeof(WCHAR);
 
     /* is this URL an update package for RAPPS? if so store it in a different place */
-    if (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0)
+    if (AppInfo->szUrlDownload == APPLICATION_DATABASE_URL)
     {
         bCab = TRUE;
-        if (!GetStorageDirectory(path, _countof(path)))
+        if (!GetStorageDirectory(Path))
             goto end;
     }
     else
     {
-        if (FAILED(StringCbCopyW(path, sizeof(path),  SettingsInfo.szDownloadDir)))
-            goto end;
+        Path = SettingsInfo.szDownloadDir;
     }
 
     /* is the path valid? can we access it? */
-    if (GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES)
+    if (GetFileAttributesW(Path.GetString()) == INVALID_FILE_ATTRIBUTES)
     {
-        if (!CreateDirectoryW(path, NULL))
+        if (!CreateDirectoryW(Path.GetString(), NULL))
             goto end;
     }
 
     /* append a \ to the provided file system path, and the filename portion from the URL after that */
-    if (FAILED(StringCbCatW(path, sizeof(path), L"\\")))
-        goto end;
-    if (FAILED(StringCbCatNW(path, sizeof(path), p + 1, filenameLength)))
-        goto end;
+    Path.Format(L"\\%ls", (p + 1));
 
-    if (!bCab && AppInfo->szSHA1[0] != 0 && GetFileAttributesW(path) != INVALID_FILE_ATTRIBUTES)
+    if (!bCab && AppInfo->szSHA1[0] && GetFileAttributesW(Path.GetString()) != INVALID_FILE_ATTRIBUTES)
     {
         /* only open it in case of total correctness */
-        if (VerifyInteg(AppInfo->szSHA1, path))
+        if (VerifyInteg(AppInfo->szSHA1, Path))
             goto run;
     }
 
@@ -327,48 +327,38 @@ ThreadFunc(LPVOID Context)
         goto end;
 
     /* FIXME: this should just be using the system-wide proxy settings */
-    switch(SettingsInfo.Proxy)
+    switch (SettingsInfo.Proxy)
     {
-        case 0: /* preconfig */
-            hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
-            break;
-        case 1: /* direct (no proxy) */
-            hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
-            break;
-        case 2: /* use proxy */
-            hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
-            break;
-        default: /* preconfig */
-            hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
-            break;
+    case 0: /* preconfig */
+        hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
+        break;
+    case 1: /* direct (no proxy) */
+        hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
+        break;
+    case 2: /* use proxy */
+        hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PROXY, SettingsInfo.szProxyServer, SettingsInfo.szNoProxyFor, 0);
+        break;
+    default: /* preconfig */
+        hOpen = InternetOpenW(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
+        break;
     }
 
     if (!hOpen)
         goto end;
 
-    hFile = InternetOpenUrlW(hOpen, AppInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE|INTERNET_FLAG_KEEP_CONNECTION, 0);
+    hFile = InternetOpenUrlW(hOpen, AppInfo->szUrlDownload, NULL, 0, INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_KEEP_CONNECTION, 0);
     if (!hFile)
     {
-        WCHAR szMsgText[MAX_STR_LEN];
-
-        if (!LoadStringW(hInst, IDS_UNABLE_TO_DOWNLOAD2, szMsgText, _countof(szMsgText)))
-            goto end;
-
-        MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR);
+        MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD2);
         goto end;
     }
 
     if (!HttpQueryInfoW(hFile, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwStatusLen, NULL))
         goto end;
 
-    if(dwStatus != HTTP_STATUS_OK)
+    if (dwStatus != HTTP_STATUS_OK)
     {
-        WCHAR szMsgText[MAX_STR_LEN];
-
-        if (!LoadStringW(hInst, IDS_UNABLE_TO_DOWNLOAD, szMsgText, _countof(szMsgText)))
-            goto end;
-
-        MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR);
+        MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_DOWNLOAD);
         goto end;
     }
 
@@ -377,22 +367,22 @@ ThreadFunc(LPVOID Context)
     memset(&urlComponents, 0, sizeof(urlComponents));
     urlComponents.dwStructSize = sizeof(urlComponents);
 
-    if(FAILED(StringCbLengthW(AppInfo->szUrlDownload, sizeof(AppInfo->szUrlDownload), &urlLength)))
+    if (FAILED(StringCbLengthW(AppInfo->szUrlDownload, sizeof(AppInfo->szUrlDownload), &urlLength)))
         goto end;
 
     urlLength /= sizeof(WCHAR);
     urlComponents.dwSchemeLength = urlLength + 1;
-    urlComponents.lpszScheme = (LPWSTR)malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
+    urlComponents.lpszScheme = (LPWSTR) malloc(urlComponents.dwSchemeLength * sizeof(WCHAR));
     urlComponents.dwHostNameLength = urlLength + 1;
-    urlComponents.lpszHostName = (LPWSTR)malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
+    urlComponents.lpszHostName = (LPWSTR) malloc(urlComponents.dwHostNameLength * sizeof(WCHAR));
 
-    if(!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength+1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
+    if (!InternetCrackUrlW(AppInfo->szUrlDownload, urlLength + 1, ICU_DECODE | ICU_ESCAPE, &urlComponents))
         goto end;
-    
-    if(urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
+
+    if (urlComponents.nScheme == INTERNET_SCHEME_HTTP || urlComponents.nScheme == INTERNET_SCHEME_HTTPS)
         HttpQueryInfo(hFile, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwContentLen, &dwStatus, 0);
 
-    if(urlComponents.nScheme == INTERNET_SCHEME_FTP)
+    if (urlComponents.nScheme == INTERNET_SCHEME_FTP)
         dwContentLen = FtpGetFileSize(hFile, &dwStatus);
 
 #ifdef USE_CERT_PINNING
@@ -401,12 +391,7 @@ ThreadFunc(LPVOID Context)
         (wcscmp(AppInfo->szUrlDownload, APPLICATION_DATABASE_URL) == 0) &&
         (!CertIsValid(hOpen, urlComponents.lpszHostName)))
     {
-        WCHAR szMsgText[MAX_STR_LEN];
-
-        if (!LoadStringW(hInst, IDS_CERT_DOES_NOT_MATCH, szMsgText, _countof(szMsgText)))
-            goto end;
-
-        MessageBoxW(Dlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
+        MessageBox_LoadString(hMainWnd, IDS_CERT_DOES_NOT_MATCH);
         goto end;
     }
 #endif
@@ -414,7 +399,7 @@ ThreadFunc(LPVOID Context)
     free(urlComponents.lpszScheme);
     free(urlComponents.lpszHostName);
 
-    hOut = CreateFileW(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
+    hOut = CreateFileW(Path.GetString(), GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
 
     if (hOut == INVALID_HANDLE_VALUE)
         goto end;
@@ -423,28 +408,19 @@ ThreadFunc(LPVOID Context)
     {
         if (!InternetReadFile(hFile, lpBuffer, _countof(lpBuffer), &dwBytesRead))
         {
-            WCHAR szMsgText[MAX_STR_LEN];
-
-            if (!LoadStringW(hInst, IDS_INTERRUPTED_DOWNLOAD, szMsgText, _countof(szMsgText)))
-                goto end;
-
-            MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR);
+            MessageBox_LoadString(hMainWnd, IDS_INTERRUPTED_DOWNLOAD);
             goto end;
         }
+
         if (!WriteFile(hOut, &lpBuffer[0], dwBytesRead, &dwBytesWritten, NULL))
         {
-            WCHAR szMsgText[MAX_STR_LEN];
-
-            if (!LoadStringW(hInst, IDS_UNABLE_TO_WRITE, szMsgText, _countof(szMsgText)))
-                goto end;
-
-            MessageBoxW(hMainWnd, szMsgText, NULL, MB_OK | MB_ICONERROR);
+            MessageBox_LoadString(hMainWnd, IDS_UNABLE_TO_WRITE);
             goto end;
         }
+
         dwCurrentBytesRead += dwBytesRead;
         dl->OnProgress(dwCurrentBytesRead, dwContentLen, 0, AppInfo->szUrlDownload);
-    }
-    while (dwBytesRead && !bCancelled);
+    } while (dwBytesRead && !bCancelled);
 
     CloseHandle(hOut);
     hOut = INVALID_HANDLE_VALUE;
@@ -456,21 +432,23 @@ ThreadFunc(LPVOID Context)
        verify its integrity by using the native advapi32.A_SHA1 functions */
     if (!bCab && AppInfo->szSHA1[0] != 0)
     {
-        WCHAR szMsgText[MAX_STR_LEN];
+        ATL::CStringW szMsgText;
 
         /* change a few strings in the download dialog to reflect the verification process */
-        LoadStringW(hInst, IDS_INTEG_CHECK_TITLE, szMsgText, _countof(szMsgText));
+        if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_TITLE))
+            goto end;
 
-        SetWindowText(Dlg, szMsgText);
-        SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM)path);
+        SetWindowText(Dlg, szMsgText.GetString());
+        SendMessageW(GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS), WM_SETTEXT, 0, (LPARAM) Path.GetBuffer());
+        Path.ReleaseBuffer();
 
         /* this may take a while, depending on the file size */
-        if (!VerifyInteg(AppInfo->szSHA1, path))
+        if (!VerifyInteg(AppInfo->szSHA1, Path.GetString()))
         {
-            if (!LoadStringW(hInst, IDS_INTEG_CHECK_FAIL, szMsgText, _countof(szMsgText)))
+            if (!szMsgText.LoadStringW(hInst, IDS_INTEG_CHECK_FAIL))
                 goto end;
 
-            MessageBoxW(Dlg, szMsgText, NULL, MB_OK | MB_ICONERROR);
+            MessageBoxW(Dlg, szMsgText.GetString(), NULL, MB_OK | MB_ICONERROR);
             goto end;
         }
     }
@@ -480,7 +458,7 @@ ThreadFunc(LPVOID Context)
 run:
     /* run it */
     if (!bCab)
-        ShellExecuteW( NULL, L"open", path, NULL, NULL, SW_SHOWNORMAL );
+        ShellExecuteW(NULL, L"open", Path.GetString(), NULL, NULL, SW_SHOWNORMAL);
 
 end:
     if (hOut != INVALID_HANDLE_VALUE)
@@ -492,7 +470,7 @@ end:
     if (bTempfile)
     {
         if (bCancelled || (SettingsInfo.bDelInstaller && !bCab))
-            DeleteFileW(path);
+            DeleteFileW(Path.GetString());
     }
 
     EndDialog(Dlg, 0);
@@ -504,77 +482,75 @@ end:
 LRESULT CALLBACK
 DownloadProgressProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData)
 {
-    static WCHAR szProgressText[MAX_STR_LEN] = {0};
+    static ATL::CStringW szProgressText;
 
     switch (uMsg)
     {
-        case WM_SETTEXT:
+    case WM_SETTEXT:
+    {
+        if (lParam)
         {
-            if (lParam)
-            {
-                StringCbCopyW(szProgressText,
-                              sizeof(szProgressText),
-                              (PCWSTR)lParam);
-            }
-            return TRUE;
+            szProgressText = (PCWSTR) lParam;
         }
+        return TRUE;
+    }
 
-        case WM_ERASEBKGND:
-        case WM_PAINT:
-        {
-            PAINTSTRUCT  ps;
-            HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
-            HBITMAP hbmMem;
-            HANDLE hOld;
-            RECT myRect;
-            UINT win_width, win_height;
-
-            GetClientRect(hWnd, &myRect);
-
-            /* grab the progress bar rect size */
-            win_width  = myRect.right - myRect.left;
-            win_height = myRect.bottom - myRect.top;
-
-            /* create an off-screen DC for double-buffering */
-            hdcMem = CreateCompatibleDC(hDC);
-            hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
-
-            hOld = SelectObject(hdcMem, hbmMem);
-
-            /* call the original draw code and redirect it to our memory buffer */
-            DefSubclassProc(hWnd, uMsg, (WPARAM)hdcMem, lParam);
-
-            /* draw our nifty progress text over it */
-            SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
-            DrawShadowText(hdcMem, szProgressText, wcslen(szProgressText),
-                           &myRect,
-                           DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
-                           GetSysColor(COLOR_CAPTIONTEXT),
-                           GetSysColor(COLOR_3DSHADOW),
-                           1, 1);
-
-            /* transfer the off-screen DC to the screen */
-            BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
-
-            /* free the off-screen DC */
-            SelectObject(hdcMem, hOld);
-            DeleteObject(hbmMem);
-            DeleteDC(hdcMem);
-
-            EndPaint(hWnd, &ps);
-            return 0;
-        }
+    case WM_ERASEBKGND:
+    case WM_PAINT:
+    {
+        PAINTSTRUCT  ps;
+        HDC hDC = BeginPaint(hWnd, &ps), hdcMem;
+        HBITMAP hbmMem;
+        HANDLE hOld;
+        RECT myRect;
+        UINT win_width, win_height;
+
+        GetClientRect(hWnd, &myRect);
+
+        /* grab the progress bar rect size */
+        win_width = myRect.right - myRect.left;
+        win_height = myRect.bottom - myRect.top;
+
+        /* create an off-screen DC for double-buffering */
+        hdcMem = CreateCompatibleDC(hDC);
+        hbmMem = CreateCompatibleBitmap(hDC, win_width, win_height);
+
+        hOld = SelectObject(hdcMem, hbmMem);
+
+        /* call the original draw code and redirect it to our memory buffer */
+        DefSubclassProc(hWnd, uMsg, (WPARAM) hdcMem, lParam);
+
+        /* draw our nifty progress text over it */
+        SelectFont(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
+        DrawShadowText(hdcMem, szProgressText.GetString(), szProgressText.GetLength(),
+                       &myRect,
+                       DT_CENTER | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE,
+                       GetSysColor(COLOR_CAPTIONTEXT),
+                       GetSysColor(COLOR_3DSHADOW),
+                       1, 1);
+
+        /* transfer the off-screen DC to the screen */
+        BitBlt(hDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY);
+
+        /* free the off-screen DC */
+        SelectObject(hdcMem, hOld);
+        DeleteObject(hbmMem);
+        DeleteDC(hdcMem);
+
+        EndPaint(hWnd, &ps);
+        return 0;
+    }
 
-        /* Raymond Chen says that we should safely unsubclass all the things!
-          (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
-        case WM_NCDESTROY:
-        {
-            ZeroMemory(szProgressText, sizeof(szProgressText));
-            RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
-        }
-        /* Fall-through */
-        default:
-            return DefSubclassProc(hWnd, uMsg, wParam, lParam);
+    /* Raymond Chen says that we should safely unsubclass all the things!
+      (http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx) */
+    case WM_NCDESTROY:
+    {
+        szProgressText.Empty();
+        RemoveWindowSubclass(hWnd, DownloadProgressProc, uIdSubclass);
+    }
+    /* Fall-through */
+    default:
+        return DefSubclassProc(hWnd, uMsg, wParam, lParam);
     }
 }
 
@@ -588,55 +564,55 @@ DownloadDlgProc(HWND Dlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
 
     switch (uMsg)
     {
-        case WM_INITDIALOG:
-        {
-            HICON hIconSm = NULL, hIconBg = NULL;
+    case WM_INITDIALOG:
+    {
+        HICON hIconSm = NULL, hIconBg = NULL;
 
-            hIconBg = (HICON)GetClassLongPtr(hMainWnd, GCLP_HICON);
-            hIconSm = (HICON)GetClassLongPtr(hMainWnd, GCLP_HICONSM);
+        hIconBg = (HICON) GetClassLongPtr(hMainWnd, GCLP_HICON);
+        hIconSm = (HICON) GetClassLongPtr(hMainWnd, GCLP_HICONSM);
 
-            if (hIconBg && hIconSm)
-            {
-                SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
-                SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
-            }
+        if (hIconBg && hIconSm)
+        {
+            SendMessageW(Dlg, WM_SETICON, ICON_BIG, (LPARAM) hIconBg);
+            SendMessageW(Dlg, WM_SETICON, ICON_SMALL, (LPARAM) hIconSm);
+        }
 
-            SetWindowLongPtrW(Dlg, GWLP_USERDATA, 0);
-            Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
-            if (Item)
-            {
-                /* initialize the default values for our nifty progress bar
-                   and subclass it so that it learns to print a status text */
-                SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
-                SendMessageW(Item, PBM_SETPOS, 0, 0);
+        SetWindowLongPtrW(Dlg, GWLP_USERDATA, 0);
+        Item = GetDlgItem(Dlg, IDC_DOWNLOAD_PROGRESS);
+        if (Item)
+        {
+            /* initialize the default values for our nifty progress bar
+               and subclass it so that it learns to print a status text */
+            SendMessageW(Item, PBM_SETRANGE, 0, MAKELPARAM(0, 100));
+            SendMessageW(Item, PBM_SETPOS, 0, 0);
 
-                SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
-            }
+            SetWindowSubclass(Item, DownloadProgressProc, 0, 0);
+        }
 
-            /* add a neat placeholder until the download URL is retrieved */
-            Item = GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS);
-            SendMessageW(Item, WM_SETTEXT, 0, (LPARAM)L"\x2022 \x2022 \x2022");
+        /* add a neat placeholder until the download URL is retrieved */
+        Item = GetDlgItem(Dlg, IDC_DOWNLOAD_STATUS);
+        SendMessageW(Item, WM_SETTEXT, 0, (LPARAM) L"\x2022 \x2022 \x2022");
 
-            Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
-            if (!Thread)
-                return FALSE;
-            CloseHandle(Thread);
-            return TRUE;
-        }
-        case WM_COMMAND:
-            if (wParam == IDCANCEL)
-            {
-                SetWindowLongPtrW(Dlg, GWLP_USERDATA, 1);
-                PostMessageW(Dlg, WM_CLOSE, 0, 0);
-            }
+        Thread = CreateThread(NULL, 0, ThreadFunc, Dlg, 0, &ThreadId);
+        if (!Thread)
             return FALSE;
+        CloseHandle(Thread);
+        return TRUE;
+    }
+    case WM_COMMAND:
+        if (wParam == IDCANCEL)
+        {
+            SetWindowLongPtrW(Dlg, GWLP_USERDATA, 1);
+            PostMessageW(Dlg, WM_CLOSE, 0, 0);
+        }
+        return FALSE;
 
-        case WM_CLOSE:
-            EndDialog(Dlg, 0);
-            return TRUE;
+    case WM_CLOSE:
+        EndDialog(Dlg, 0);
+        return TRUE;
 
-        default:
-            return FALSE;
+    default:
+        return FALSE;
     }
 }
 
@@ -647,9 +623,10 @@ DownloadApplication(INT Index)
         return FALSE;
 
     AppInfo = (PAPPLICATION_INFO) ListViewGetlParam(Index);
-    if (!AppInfo) return FALSE;
+    if (!AppInfo)
+        return FALSE;
 
-    WriteLogMessage(EVENTLOG_SUCCESS, MSG_SUCCESS_INSTALL, AppInfo->szName);
+    WriteLogMessage(EVENTLOG_SUCCESS, MSG_SUCCESS_INSTALL, AppInfo->szName.GetString());
 
     DialogBoxW(hInst,
                MAKEINTRESOURCEW(IDD_DOWNLOAD_DIALOG),
@@ -663,14 +640,7 @@ VOID
 DownloadApplicationsDB(LPCWSTR lpUrl)
 {
     APPLICATION_INFO IntInfo;
-
-    ZeroMemory(&IntInfo, sizeof(IntInfo));
-    if (FAILED(StringCbCopyW(IntInfo.szUrlDownload,
-                             sizeof(IntInfo.szUrlDownload),
-                             lpUrl)))
-    {
-        return;
-    }
+    IntInfo.szUrlDownload = lpUrl;
 
     AppInfo = &IntInfo;
 
@@ -679,4 +649,3 @@ DownloadApplicationsDB(LPCWSTR lpUrl)
                hMainWnd,
                DownloadDlgProc);
 }
-
index 159c739..e7cb872 100644 (file)
@@ -5,21 +5,21 @@
  * PURPOSE:         Misc functions
  * PROGRAMMERS:     Dmitry Chapyshev           (dmitry@reactos.org)
  *                  Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
+ *                  Alexander Shaposhnikov     (chaez.san@gmail.com)
  */
 
 #include "rapps.h"
-#include <atlsimpcoll.h>
-#include <atlstr.h> 
 
-/* SESSION Operation */
+ /* SESSION Operation */
 #define EXTRACT_FILLFILELIST  0x00000001
 #define EXTRACT_EXTRACTFILES  0x00000002
 
 static HANDLE hLog = NULL;
-WCHAR szCachedINISectionLocale[MAX_PATH] = L"Section.";
-WCHAR szCachedINISectionLocaleNeutral[MAX_PATH] = {0};
+ATL::CStringW szCachedINISectionLocale = L"Section.";
+ATL::CStringW szCachedINISectionLocaleNeutral;
 BYTE bCachedSectionStatus = FALSE;
 
+#define LOCALIZED_STRING_LEN MAX_PATH
 #define STR_VERSION_CURRENT L"CURRENT"
 
 typedef struct
@@ -70,12 +70,12 @@ GetSystemColorDepth(VOID)
 
     switch (pDevMode.dmBitsPerPel)
     {
-        case 32: ColorDepth = ILC_COLOR32; break;
-        case 24: ColorDepth = ILC_COLOR24; break;
-        case 16: ColorDepth = ILC_COLOR16; break;
-        case  8: ColorDepth = ILC_COLOR8;  break;
-        case  4: ColorDepth = ILC_COLOR4;  break;
-        default: ColorDepth = ILC_COLOR;   break;
+    case 32: ColorDepth = ILC_COLOR32; break;
+    case 24: ColorDepth = ILC_COLOR24; break;
+    case 16: ColorDepth = ILC_COLOR16; break;
+    case  8: ColorDepth = ILC_COLOR8;  break;
+    case  4: ColorDepth = ILC_COLOR4;  break;
+    default: ColorDepth = ILC_COLOR;   break;
     }
 
     return ColorDepth;
@@ -131,7 +131,7 @@ CopyTextToClipboard(LPCWSTR lpszText)
         EmptyClipboard();
         cchBuffer = wcslen(lpszText) + 1;
         ClipBuffer = GlobalAlloc(GMEM_DDESHARE, cchBuffer * sizeof(WCHAR));
-        Buffer = (PWCHAR)GlobalLock(ClipBuffer);
+        Buffer = (PWCHAR) GlobalLock(ClipBuffer);
         hr = StringCchCopyW(Buffer, cchBuffer, lpszText);
         GlobalUnlock(ClipBuffer);
 
@@ -145,15 +145,15 @@ CopyTextToClipboard(LPCWSTR lpszText)
 VOID
 SetWelcomeText(VOID)
 {
-    WCHAR szText[MAX_STR_LEN*3];
+    ATL::CStringW szText;
 
-    LoadStringW(hInst, IDS_WELCOME_TITLE, szText, _countof(szText));
+    szText.LoadStringW(hInst, IDS_WELCOME_TITLE);
     NewRichEditText(szText, CFE_BOLD);
 
-    LoadStringW(hInst, IDS_WELCOME_TEXT, szText, _countof(szText));
+    szText.LoadStringW(hInst, IDS_WELCOME_TEXT);
     InsertRichEditText(szText, 0);
 
-    LoadStringW(hInst, IDS_WELCOME_URL, szText, _countof(szText));
+    szText.LoadStringW(hInst, IDS_WELCOME_URL);
     InsertRichEditText(szText, CFM_LINK);
 }
 
@@ -190,6 +190,14 @@ ShowPopupMenu(HWND hwnd, UINT MenuID, UINT DefaultItem)
         DestroyMenu(hMenu);
 }
 
+BOOL
+StartProcess(ATL::CStringW &Path, BOOL Wait)
+{
+    BOOL result = StartProcess(Path.GetBuffer(), Wait);
+    Path.ReleaseBuffer();
+    return result;
+}
+
 BOOL
 StartProcess(LPWSTR lpPath, BOOL Wait)
 {
@@ -242,28 +250,28 @@ StartProcess(LPWSTR lpPath, BOOL Wait)
 }
 
 BOOL
-GetStorageDirectory(PWCHAR lpDirectory, DWORD cch)
+GetStorageDirectory(ATL::CStringW& Directory)
 {
-    if (cch < MAX_PATH)
-        return FALSE;
-
-    if (!SHGetSpecialFolderPathW(NULL, lpDirectory, CSIDL_LOCAL_APPDATA, TRUE))
-        return FALSE;
-
-    if (FAILED(StringCchCatW(lpDirectory, cch, L"\\rapps")))
-        return FALSE;
-
-    if (!CreateDirectoryW(lpDirectory, NULL) &&
-        GetLastError() != ERROR_ALREADY_EXISTS)
+    if (!SHGetSpecialFolderPathW(NULL, Directory.GetBuffer(MAX_PATH), CSIDL_LOCAL_APPDATA, TRUE))
     {
+        Directory.ReleaseBuffer();
         return FALSE;
     }
 
-    return TRUE;
+    Directory.ReleaseBuffer();
+    Directory += L"\\rapps";
+
+    return (CreateDirectoryW(Directory.GetString(), NULL) || GetLastError() == ERROR_ALREADY_EXISTS);
+}
+
+BOOL
+ExtractFilesFromCab(const ATL::CStringW &CabName, const ATL::CStringW &OutputPath)
+{
+    return ExtractFilesFromCab(CabName.GetString(), OutputPath.GetString());
 }
 
 BOOL
-ExtractFilesFromCab(LPWSTR lpCabName, LPWSTR lpOutputPath)
+ExtractFilesFromCab(LPCWSTR lpCabName, LPCWSTR lpOutputPath)
 {
     HINSTANCE hCabinetDll;
     CHAR szCabName[MAX_PATH];
@@ -326,21 +334,21 @@ InitLogs(VOID)
                        L"EventMessageFile",
                        0,
                        REG_EXPAND_SZ,
-                       (LPBYTE)szPath,
-                       (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
+                       (LPBYTE) szPath,
+                       (DWORD) (wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
     {
         RegCloseKey(hKey);
         return;
     }
 
     dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
-             EVENTLOG_INFORMATION_TYPE;
+        EVENTLOG_INFORMATION_TYPE;
 
     if (RegSetValueExW(hKey,
                        L"TypesSupported",
                        0,
                        REG_DWORD,
-                       (LPBYTE)&dwData,
+                       (LPBYTE) &dwData,
                        sizeof(DWORD)) != ERROR_SUCCESS)
     {
         RegCloseKey(hKey);
@@ -351,8 +359,8 @@ InitLogs(VOID)
                        L"CategoryMessageFile",
                        0,
                        REG_EXPAND_SZ,
-                       (LPBYTE)szPath,
-                       (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
+                       (LPBYTE) szPath,
+                       (DWORD) (wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
     {
         RegCloseKey(hKey);
         return;
@@ -362,7 +370,7 @@ InitLogs(VOID)
                        L"CategoryCount",
                        0,
                        REG_DWORD,
-                       (LPBYTE)&dwCategoryNum,
+                       (LPBYTE) &dwCategoryNum,
                        sizeof(DWORD)) != ERROR_SUCCESS)
     {
         RegCloseKey(hKey);
@@ -383,19 +391,12 @@ FreeLogs(VOID)
 
 
 BOOL
-WriteLogMessage(WORD wType, DWORD dwEventID, LPWSTR lpMsg)
+WriteLogMessage(WORD wType, DWORD dwEventID, LPCWSTR lpMsg)
 {
     if (!SettingsInfo.bLogEnabled) return TRUE;
 
-    if (!ReportEventW(hLog,
-                      wType,
-                      0,
-                      dwEventID,
-                      NULL,
-                      1,
-                      0,
-                      (LPCWSTR*)&lpMsg,
-                      NULL))
+    if (!ReportEventW(hLog, wType, 0, dwEventID,
+                      NULL, 1, 0, &lpMsg, NULL))
     {
         return FALSE;
     }
@@ -404,211 +405,98 @@ WriteLogMessage(WORD wType, DWORD dwEventID, LPWSTR lpMsg)
 }
 
 
-LPWSTR GetINIFullPath(LPCWSTR lpFileName)
+ATL::CStringW GetINIFullPath(const ATL::CStringW& FileName)
 {
-           WCHAR szDir[MAX_PATH];
-    static WCHAR szBuffer[MAX_PATH];
+    ATL::CStringW szDir;
+    static ATL::CStringW szBuffer;
 
-    GetStorageDirectory(szDir, _countof(szDir));
-    StringCbPrintfW(szBuffer, sizeof(szBuffer), L"%ls\\rapps\\%ls", szDir, lpFileName);
+    GetStorageDirectory(szDir);
+    szBuffer.Format(L"%ls\\rapps\\%ls", szDir, FileName);
 
     return szBuffer;
 }
 
-
-UINT ParserGetString(LPCWSTR lpKeyName, LPWSTR lpReturnedString, UINT nSize, LPCWSTR lpFileName)
+UINT ParserGetString(const ATL::CStringW& KeyName, const ATL::CStringW& FileName, ATL::CStringW& ResultString)
 {
-    PWSTR lpFullFileName = GetINIFullPath(lpFileName);
+    ATL::CStringW FullFileName = GetINIFullPath(FileName);
     DWORD dwResult;
 
     /* we don't have cached section strings for the current system language, create them */
-    if(bCachedSectionStatus == FALSE)
+    if (bCachedSectionStatus == FALSE)
     {
-        WCHAR szLocale[4 + 1];
-        DWORD len;
+        ATL::CStringW szLocale;
+        const INT LocaleSize = 5;
 
         /* find out what is the current system lang code (e.g. "0a") and append it to SectionLocale */
         GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE,
-                       szLocale, _countof(szLocale));
-
-        StringCbCatW(szCachedINISectionLocale, sizeof(szCachedINISectionLocale), szLocale);
-
-        /* copy the locale-dependent string into the buffer of the future neutral one */
-        StringCbCopyW(szCachedINISectionLocaleNeutral,
-                      sizeof(szCachedINISectionLocaleNeutral),
-                      szCachedINISectionLocale);
+                       szLocale.GetBuffer(LocaleSize), LocaleSize);
+        szLocale.ReleaseBuffer();
 
         /* turn "Section.0c0a" into "Section.0a", keeping just the neutral lang part */
-        len = wcslen(szCachedINISectionLocale);
-
-        memmove((szCachedINISectionLocaleNeutral + len) - 4,
-                (szCachedINISectionLocaleNeutral + len) - 2,
-                (2 * sizeof(WCHAR)) + sizeof(UNICODE_NULL));
+        szCachedINISectionLocaleNeutral = szCachedINISectionLocale + szLocale.Right(2);
+        szCachedINISectionLocale += szLocale;
 
         /* finally, mark us as cache-friendly for the next time */
         bCachedSectionStatus = TRUE;
     }
 
+    LPWSTR ResultStringBuffer = ResultString.GetBuffer(MAX_PATH);
     /* 1st - find localized strings (e.g. "Section.0c0a") */
-    dwResult = GetPrivateProfileStringW(szCachedINISectionLocale,
-                                        lpKeyName,
+    dwResult = GetPrivateProfileStringW(szCachedINISectionLocale.GetString(),
+                                        KeyName.GetString(),
                                         NULL,
-                                        lpReturnedString,
-                                        nSize,
-                                        lpFullFileName);
+                                        ResultStringBuffer,
+                                        LOCALIZED_STRING_LEN,
+                                        FullFileName.GetString());
 
-    if (dwResult != 0)
-        return TRUE;
-
-    /* 2nd - if they weren't present check for neutral sub-langs/ generic translations (e.g. "Section.0a") */
-    dwResult = GetPrivateProfileStringW(szCachedINISectionLocaleNeutral,
-                                        lpKeyName,
-                                        NULL,
-                                        lpReturnedString,
-                                        nSize,
-                                        lpFullFileName);
-
-    if (dwResult != 0)
-        return TRUE;
-
-    /* 3rd - if they weren't present fallback to standard english strings (just "Section") */
-    dwResult = GetPrivateProfileStringW(L"Section",
-                                        lpKeyName,
-                                        NULL,
-                                        lpReturnedString,
-                                        nSize,
-                                        lpFullFileName);
+    if (!dwResult)
+    {
+        /* 2nd - if they weren't present check for neutral sub-langs/ generic translations (e.g. "Section.0a") */
+        dwResult = GetPrivateProfileStringW(szCachedINISectionLocaleNeutral.GetString(),
+                                            KeyName.GetString(),
+                                            NULL,
+                                            ResultStringBuffer,
+                                            LOCALIZED_STRING_LEN,
+                                            FullFileName.GetString());
+        if (!dwResult)
+        {
+            /* 3rd - if they weren't present fallback to standard english strings (just "Section") */
+            dwResult = GetPrivateProfileStringW(L"Section",
+                                                KeyName.GetString(),
+                                                NULL,
+                                                ResultStringBuffer,
+                                                LOCALIZED_STRING_LEN,
+                                                FullFileName.GetString());
+        }
+    }
 
+    ResultString.ReleaseBuffer();
     return (dwResult != 0 ? TRUE : FALSE);
 }
 
-UINT ParserGetInt(LPCWSTR lpKeyName, LPCWSTR lpFileName)
+UINT ParserGetInt(const ATL::CStringW& KeyName, const ATL::CStringW& FileName)
 {
-    WCHAR Buffer[30];
+    ATL::CStringW Buffer;
     UNICODE_STRING BufferW;
     ULONG Result;
 
     /* grab the text version of our entry */
-    if (!ParserGetString(lpKeyName, Buffer, _countof(Buffer), lpFileName))
+    if (!ParserGetString(KeyName, FileName, Buffer))
         return FALSE;
 
-    if (!Buffer[0])
+    if (Buffer.IsEmpty())
         return FALSE;
 
     /* convert it to an actual integer */
-    RtlInitUnicodeString(&BufferW, Buffer);
+    RtlInitUnicodeString(&BufferW, Buffer.GetString());
     RtlUnicodeStringToInteger(&BufferW, 0, &Result);
 
-    return (UINT)Result;
-}
-
-template<typename XCHAR>
-inline BOOL IsCharNumeric(XCHAR ch)
-{
-    return IsCharAlphaNumeric(ch) && !IsCharAlpha(ch);
+    return (UINT) Result;
 }
 
-
-//Parses version string that can be formatted as 1.2.3-4b (or CURRENT)
-//Returns int buffer and it's size
-BOOL
-ParseVersion(_In_z_ LPCWSTR szVersion, _Outptr_ PVERSION_INFO Version)
+LPWSTR HeapBufferFromCStringW(const ATL::CStringW& String)
 {
-    ATL::CStringW szVersionSingleInt = L"";
-    BOOL bHasParsed = TRUE;
-    INT VersionCharCount = 0;
-    INT VersionLength = lstrlenW(szVersion);
-    StringCbCopyW(Version->szVersion, VersionLength * sizeof(szVersion), szVersion);
-    //CURRENT
-    if (!StrCmpW(szVersion, STR_VERSION_CURRENT))
-    {
-        Version->VersionSize = NULL;
-        return bHasParsed;
-    }
-
-    Version->VersionSize = 0;
-    //int expected every iteration, quit the loop if its not a number
-    while (Version->VersionSize < MAX_VERSION 
-        && IsCharNumeric(szVersion[VersionCharCount]) 
-        && VersionCharCount < VersionLength)
-    {
-        for (; IsCharNumeric(szVersion[VersionCharCount]) && VersionCharCount < VersionLength; ++VersionCharCount)
-        {
-            szVersionSingleInt += szVersion[VersionCharCount];
-        }
-        if (szVersionSingleInt.IsEmpty())
-        {
-            bHasParsed = FALSE;
-            continue;
-        }
-        INT IntResult = StrToIntW(szVersionSingleInt.GetBuffer());
-        Version->arrVersion[Version->VersionSize] = IntResult;
-        ++Version->VersionSize;
-        szVersionSingleInt.Empty();
-        ++VersionCharCount;
-    }
-
-    if (IsCharAlphaW(szVersion[VersionCharCount]))
-    {
-        Version->cVersionSuffix = szVersion[VersionCharCount];
-    }
-    else
-        Version->cVersionSuffix = NULL;
-    return bHasParsed;
-}
-
-//Compares versions 
-//In:   Zero terminated strings of versions
-//Out:  TRUE if first is bigger than second, FALSE if else
-BOOL
-CompareVersionsStrings(_In_z_ LPCWSTR sczVersionLeft, _In_z_ LPCWSTR sczVersionRight)
-{
-    VERSION_INFO LeftVersion, RightVersion;
-
-    if (!ParseVersion(sczVersionLeft, &LeftVersion)
-        || !ParseVersion(sczVersionRight, &RightVersion))
-    {
-        return FALSE;
-    }
-
-    return CompareVersions(&LeftVersion, &RightVersion);  
-}
-
-BOOL
-CompareVersions(_In_ PVERSION_INFO LeftVersion, _In_ PVERSION_INFO RightVersion)
-{
-    //CURRENT 
-    if (!LeftVersion->VersionSize || !RightVersion->VersionSize)
-    {
-        return FALSE;
-    }
-    //1.2.3 > 1.2
-    INT SizeDiff = LeftVersion->VersionSize - RightVersion->VersionSize;
-    if (SizeDiff > 0)
-    {
-        return TRUE;
-    }
-    if (SizeDiff < 0)
-    {
-        return FALSE;
-    }
-    //2.0.0 > 1.9.9
-    for (INT i = 0; i < LeftVersion->VersionSize && i < RightVersion->VersionSize && i < MAX_VERSION; ++i)
-    {
-        if (LeftVersion->arrVersion[i] > RightVersion->arrVersion[i])
-        {
-            return TRUE;
-        }
-        if (LeftVersion->arrVersion[i] < RightVersion->arrVersion[i])
-        {
-            return FALSE;
-        }
-    }
-    //1.2.3b > 1.2.3
-    if (LeftVersion->cVersionSuffix > RightVersion->cVersionSuffix)
-    {
-        return TRUE;
-    }
-
-    return FALSE;
+    LPWSTR szBuffer = (LPWSTR) HeapAlloc(GetProcessHeap(), 0, MAX_PATH * sizeof(WCHAR));
+    ATL::CString::CopyChars(szBuffer, MAX_PATH, String.GetString(), String.GetLength() + 1);
+    return szBuffer;
 }
\ No newline at end of file
index 837ef60..afa942e 100644 (file)
@@ -25,7 +25,8 @@
 #include <stdio.h>
 #include <strsafe.h>
 #include <ndk/rtlfuncs.h>
-
+#include <atlcoll.h>
+#include <atlstr.h> 
 #include <rappsmsg.h>
 
 #include "resource.h"
@@ -39,7 +40,6 @@
 
 #define SPLIT_WIDTH 4
 #define MAX_STR_LEN 256
-#define MAX_VERSION 10
 #define LISTVIEW_ICON_SIZE 24
 #define TREEVIEW_ICON_SIZE 24
 
@@ -91,32 +91,33 @@ typedef struct
 {
     INT Category;
     LICENSE_TYPE LicenseType;
-    WCHAR szName[MAX_PATH];
-    WCHAR szRegName[MAX_PATH];
-    WCHAR szVersion[MAX_PATH];
-    WCHAR szLicense[MAX_PATH];
-    WCHAR szDesc[MAX_PATH];
-    WCHAR szSize[MAX_PATH];
-    WCHAR szUrlSite[MAX_PATH];
-    WCHAR szUrlDownload[MAX_PATH];
-    WCHAR szCDPath[MAX_PATH];
-    WCHAR szLanguages[MAX_PATH];
+    ATL::CStringW szName;
+    ATL::CStringW szRegName;
+    ATL::CStringW szVersion;
+    ATL::CStringW szLicense;
+    ATL::CStringW szDesc;
+    ATL::CStringW szSize;
+    ATL::CStringW szUrlSite;
+    ATL::CStringW szUrlDownload;
+    ATL::CStringW szCDPath;
+    ATL::CStringW szLanguages;
 
     /* caching mechanism related entries */
-    WCHAR cFileName[MAX_PATH];
+    ATL::CStringW cFileName;
     FILETIME ftCacheStamp;
-    LIST_ENTRY List;
 
     /* optional integrity checks (SHA-1 digests are 160 bit = 40 characters in hex string form) */
-    WCHAR szSHA1[40 + 1];
+    ATL::CStringW szSHA1;
 
 } APPLICATION_INFO, *PAPPLICATION_INFO;
 
+extern ATL::CAtlList<PAPPLICATION_INFO> InfoList;
+
 typedef struct
 {
     HKEY hRootKey;
     HKEY hSubKey;
-    WCHAR szKeyName[MAX_PATH];
+    ATL::CStringW szKeyName;
 
 } INSTALLED_INFO, *PINSTALLED_INFO;
 
@@ -140,14 +141,6 @@ typedef struct
 
 } SETTINGS_INFO, *PSETTINGS_INFO;
 
-typedef struct
-{
-    INT arrVersion[MAX_VERSION];
-    UINT VersionSize;
-    WCHAR cVersionSuffix = (WCHAR) NULL;
-    WCHAR szVersion[MAX_PATH];
-} VERSION_INFO, *PVERSION_INFO;
-
 /* available.cpp */
 typedef BOOL (CALLBACK *AVAILENUMPROC)(PAPPLICATION_INFO Info);
 BOOL EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc);
@@ -155,20 +148,21 @@ BOOL ShowAvailableAppInfo(INT Index);
 BOOL UpdateAppsDB(VOID);
 VOID FreeCachedAvailableEntries(VOID);
 
-
 /* installdlg.cpp */
 BOOL InstallApplication(INT Index);
 
 /* installed.cpp */
-typedef BOOL (CALLBACK *APPENUMPROC)(INT ItemIndex, LPWSTR lpName, PINSTALLED_INFO Info);
+typedef BOOL (CALLBACK *APPENUMPROC)(INT ItemIndex, ATL::CStringW &lpName, PINSTALLED_INFO Info);
 BOOL EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc);
-BOOL GetApplicationString(HKEY hKey, LPCWSTR lpKeyName, LPWSTR lpString);
+BOOL GetApplicationString(HKEY hKey, LPCWSTR lpKeyName, LPWSTR szString);
+BOOL GetApplicationString(HKEY hKey, LPCWSTR RegName, ATL::CStringW& String);
+
 BOOL ShowInstalledAppInfo(INT Index);
 BOOL UninstallApplication(INT Index, BOOL bModify);
-BOOL IsInstalledApplication(LPCWSTR lpRegName, BOOL IsUserKey, REGSAM keyWow);
+BOOL IsInstalledApplication(const ATL::CStringW& RegName, BOOL IsUserKey, REGSAM keyWow);
 VOID RemoveAppFromRegistry(INT Index);
 
-BOOL InstalledVersion(LPWSTR szVersionResult, UINT iVersionResultSize, LPCWSTR lpRegName, BOOL IsUserKey, REGSAM keyWow);
+BOOL InstalledVersion(ATL::CStringW& szVersionResult, const ATL::CStringW& RegName, BOOL IsUserKey, REGSAM keyWow);
 
 /* winmain.cpp */
 extern HWND hMainWnd;
@@ -191,19 +185,16 @@ int GetClientWindowHeight(HWND hwnd);
 VOID CopyTextToClipboard(LPCWSTR lpszText);
 VOID SetWelcomeText(VOID);
 VOID ShowPopupMenu(HWND hwnd, UINT MenuID, UINT DefaultItem);
+BOOL StartProcess(ATL::CStringW & Path, BOOL Wait);
 BOOL StartProcess(LPWSTR lpPath, BOOL Wait);
-BOOL GetStorageDirectory(PWCHAR lpDirectory, DWORD cch);
-BOOL ExtractFilesFromCab(LPWSTR lpCabName, LPWSTR lpOutputPath);
+BOOL GetStorageDirectory(ATL::CStringW &lpDirectory);
+BOOL ExtractFilesFromCab(LPCWSTR lpCabName, LPCWSTR lpOutputPath);
 VOID InitLogs(VOID);
 VOID FreeLogs(VOID);
-BOOL WriteLogMessage(WORD wType, DWORD dwEventID, LPWSTR lpMsg);
+BOOL WriteLogMessage(WORD wType, DWORD dwEventID, LPCWSTR lpMsg);
 
-UINT ParserGetString(LPCWSTR lpKeyName, LPWSTR lpReturnedString, UINT nSize, LPCWSTR lpFileName);
-UINT ParserGetInt(LPCWSTR lpKeyName, LPCWSTR lpFileName);
-
-BOOL ParseVersion(_In_z_ LPCWSTR szVersion, _Outptr_ PVERSION_INFO parrVersion);
-BOOL CompareVersionsStrings(_In_z_ LPCWSTR sczVersionLeft, _In_z_ LPCWSTR sczVersionRight);
-BOOL CompareVersions(_In_ PVERSION_INFO LeftVersion, _In_ PVERSION_INFO RightVersion);
+UINT ParserGetString(const ATL::CStringW& KeyName, const ATL::CStringW& FileName, ATL::CStringW& ReturnedString);
+UINT ParserGetInt(const ATL::CStringW& KeyName, const ATL::CStringW& FileName);
 
 /* settingsdlg.cpp */
 VOID CreateSettingsDlg(HWND hwnd);
@@ -211,12 +202,17 @@ VOID CreateSettingsDlg(HWND hwnd);
 /* gui.cpp */
 HWND CreateMainWindow();
 DWORD_PTR ListViewGetlParam(INT item);
-INT ListViewAddItem(INT ItemIndex, INT IconIndex, PWSTR lpName, LPARAM lParam);
-VOID SetStatusBarText(PCWSTR szText);
-VOID NewRichEditText(PCWSTR szText, DWORD flags);
-VOID InsertRichEditText(PCWSTR szText, DWORD flags);
+INT ListViewAddItem(INT ItemIndex, INT IconIndex, LPWSTR lpName, LPARAM lParam);
+VOID SetStatusBarText(LPCWSTR szText);
+VOID NewRichEditText(LPCWSTR szText, DWORD flags);
+VOID InsertRichEditText(LPCWSTR szText, DWORD flags);
+
+VOID SetStatusBarText(const ATL::CStringW& szText);
+INT ListViewAddItem(INT ItemIndex, INT IconIndex, ATL::CStringW & Name, LPARAM lParam);
+VOID NewRichEditText(const ATL::CStringW& szText, DWORD flags);
+VOID InsertRichEditText(const ATL::CStringW& szText, DWORD flags);
 extern HWND hListView;
-extern WCHAR szSearchPattern[MAX_STR_LEN];
+extern ATL::CStringW szSearchPattern;
 
 /* integrity.cpp */
 BOOL VerifyInteg(LPCWSTR lpSHA1Hash, LPCWSTR lpFileName);
@@ -225,5 +221,4 @@ BOOL VerifyInteg(LPCWSTR lpSHA1Hash, LPCWSTR lpFileName);
 //BOOL CreateTreeView(HWND hwnd);
 //HTREEITEM TreeViewAddItem(HTREEITEM hParent, LPWSTR lpText, INT Image, INT SelectedImage, LPARAM lParam);
 
-
 #endif /* _RAPPS_H */
index a89b8cb..90d8193 100644 (file)
 #define IDS_STATUS_INSTALLING         805
 
 /* App license names */
-#define IDS_LICENSE_OPENSOURCE  900
-#define IDS_LICENSE_FREEWARE 901
-#define IDS_LICENSE_TRIAL 902
+#define IDS_LICENSE_OPENSOURCE      900
+#define IDS_LICENSE_FREEWARE        901
+#define IDS_LICENSE_TRIAL           902
 
 /* Accelerators */
 #define HOTKEYS                  715
index 2b58f83..9456379 100644 (file)
@@ -488,11 +488,16 @@ public:
         }
     };
 
-public:
     virtual ~CUiWindow()
     {
         T::DestroyWindow();
     }
+
+    void GetWindowTextW(ATL::CStringW& szText)
+    {
+        CWindow::GetWindowText(szText.GetBuffer(MAX_STR_LEN), MAX_STR_LEN);
+        szText.ReleaseBuffer();
+    }
 };
 
 class CUiSplitPanel :
@@ -523,7 +528,6 @@ public:
     CUiMeasure m_Width;
     CUiMeasure m_Height;
 
-public:
     CUiSplitPanel()
     {
         m_Width = CUiMeasure::FitParent();
index 527227c..10d3c0b 100644 (file)
@@ -3,7 +3,8 @@
  * LICENSE:         GPL - See COPYING in the top level directory
  * FILE:            base/applications/rapps/settingsdlg.cpp
  * PURPOSE:         Settings Dialog
- * PROGRAMMERS:     Dmitry Chapyshev (dmitry@reactos.org)
+ * PROGRAMMERS:     Dmitry Chapyshev           (dmitry@reactos.org)
+ *                  Alexander Shaposhnikov     (chaez.san@gmail.com)
  */
 
 #include "rapps.h"
@@ -18,27 +19,31 @@ ChooseFolder(HWND hwnd)
 {
     BOOL bRet = FALSE;
     BROWSEINFO bi;
-    WCHAR szPath[MAX_PATH], szBuf[MAX_STR_LEN];
+    ATL::CStringW szBuf;
 
-    LoadStringW(hInst, IDS_CHOOSE_FOLDER_TEXT, szBuf, _countof(szBuf));
+    szBuf.LoadStringW(hInst, IDS_CHOOSE_FOLDER_TEXT);
 
     ZeroMemory(&bi, sizeof(bi));
     bi.hwndOwner = hwnd;
-    bi.pidlRoot  = NULL;
-    bi.lpszTitle = szBuf;
-    bi.ulFlags   = BIF_USENEWUI | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | /* BIF_BROWSEFILEJUNCTIONS | */ BIF_VALIDATE;
+    bi.pidlRoot = NULL;
+    bi.lpszTitle = szBuf.GetString();
+    bi.ulFlags = BIF_USENEWUI | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | /* BIF_BROWSEFILEJUNCTIONS | */ BIF_VALIDATE;
 
+    szBuf.Empty();
     if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
     {
         LPITEMIDLIST lpItemList = SHBrowseForFolder(&bi);
-        if (lpItemList && SHGetPathFromIDList(lpItemList, szPath))
+        if (lpItemList && SHGetPathFromIDList(lpItemList, szBuf.GetBuffer(MAX_PATH)))
         {
-            if (szPath[0] != 0)
+            szBuf.ReleaseBuffer();
+            if (!szBuf.IsEmpty())
             {
-                SetDlgItemTextW(hwnd, IDC_DOWNLOAD_DIR_EDIT, szPath);
+                SetDlgItemTextW(hwnd, IDC_DOWNLOAD_DIR_EDIT, szBuf.GetString());
                 bRet = TRUE;
             }
         }
+        else
+            szBuf.ReleaseBuffer();
 
         CoTaskMemFree(lpItemList);
         CoUninitialize();
@@ -58,9 +63,9 @@ InitSettingsControls(HWND hDlg, PSETTINGS_INFO Info)
     SetWindowTextW(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT),
                    Info->szDownloadDir);
 
-    CheckRadioButton(hDlg, IDC_PROXY_DEFAULT, IDC_USE_PROXY, IDC_PROXY_DEFAULT+Info->Proxy);
+    CheckRadioButton(hDlg, IDC_PROXY_DEFAULT, IDC_USE_PROXY, IDC_PROXY_DEFAULT + Info->Proxy);
 
-    if(IDC_PROXY_DEFAULT + Info->Proxy == IDC_USE_PROXY)
+    if (IDC_PROXY_DEFAULT + Info->Proxy == IDC_USE_PROXY)
     {
         EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), TRUE);
         EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), TRUE);
@@ -76,120 +81,127 @@ SettingsDlgProc(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
 {
     switch (Msg)
     {
-        case WM_INITDIALOG:
-        {
-            NewSettingsInfo = SettingsInfo;
-            InitSettingsControls(hDlg, &SettingsInfo);
-        }
-        break;
+    case WM_INITDIALOG:
+    {
+        NewSettingsInfo = SettingsInfo;
+        InitSettingsControls(hDlg, &SettingsInfo);
+    }
+    break;
 
-        case WM_COMMAND:
+    case WM_COMMAND:
+    {
+        switch (LOWORD(wParam))
+        {
+        case IDC_CHOOSE:
+            ChooseFolder(hDlg);
+            break;
+
+        case IDC_SAVE_WINDOW_POS:
+            IS_CHECKED(NewSettingsInfo.bSaveWndPos, IDC_SAVE_WINDOW_POS);
+            break;
+
+        case IDC_UPDATE_AVLIST:
+            IS_CHECKED(NewSettingsInfo.bUpdateAtStart, IDC_UPDATE_AVLIST);
+            break;
+
+        case IDC_LOG_ENABLED:
+            IS_CHECKED(NewSettingsInfo.bLogEnabled, IDC_LOG_ENABLED);
+            break;
+
+        case IDC_DEL_AFTER_INSTALL:
+            IS_CHECKED(NewSettingsInfo.bDelInstaller, IDC_DEL_AFTER_INSTALL);
+            break;
+
+        case IDC_PROXY_DEFAULT:
+            NewSettingsInfo.Proxy = 0;
+            EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), FALSE);
+            EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), FALSE);
+            break;
+
+        case IDC_NO_PROXY:
+            NewSettingsInfo.Proxy = 1;
+            EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), FALSE);
+            EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), FALSE);
+            break;
+
+        case IDC_USE_PROXY:
+            NewSettingsInfo.Proxy = 2;
+            EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), TRUE);
+            EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), TRUE);
+            break;
+
+        case IDC_DEFAULT_SETTINGS:
+            FillDefaultSettings(&NewSettingsInfo);
+            InitSettingsControls(hDlg, &NewSettingsInfo);
+            break;
+
+        case IDOK:
         {
-            switch (LOWORD(wParam))
+            ATL::CStringW szDir;
+            ATL::CStringW szProxy;
+            ATL::CStringW szNoProxy;
+            DWORD dwAttr;
+
+            GetWindowTextW(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT),
+                           szDir.GetBuffer(MAX_PATH), MAX_PATH);
+            szDir.ReleaseBuffer();
+
+            GetWindowTextW(GetDlgItem(hDlg, IDC_PROXY_SERVER),
+                           szProxy.GetBuffer(MAX_PATH), MAX_PATH);
+            szProxy.ReleaseBuffer();
+            ATL::CStringW::CopyChars(NewSettingsInfo.szProxyServer,
+                                     _countof(NewSettingsInfo.szProxyServer),
+                                     szProxy.GetString(),
+                                     szProxy.GetLength() + 1);
+
+            GetWindowTextW(GetDlgItem(hDlg, IDC_NO_PROXY_FOR),
+                           szNoProxy.GetBuffer(MAX_PATH), MAX_PATH);
+            szNoProxy.ReleaseBuffer();
+            ATL::CStringW::CopyChars(NewSettingsInfo.szNoProxyFor,
+                                     _countof(NewSettingsInfo.szNoProxyFor),
+                                     szNoProxy.GetString(),
+                                     szNoProxy.GetLength() + 1);
+
+            dwAttr = GetFileAttributesW(szDir.GetString());
+            if (dwAttr != INVALID_FILE_ATTRIBUTES &&
+                (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
             {
-                case IDC_CHOOSE:
-                    ChooseFolder(hDlg);
-                    break;
-
-                case IDC_SAVE_WINDOW_POS:
-                    IS_CHECKED(NewSettingsInfo.bSaveWndPos, IDC_SAVE_WINDOW_POS);
-                    break;
-
-                case IDC_UPDATE_AVLIST:
-                    IS_CHECKED(NewSettingsInfo.bUpdateAtStart, IDC_UPDATE_AVLIST);
-                    break;
-
-                case IDC_LOG_ENABLED:
-                    IS_CHECKED(NewSettingsInfo.bLogEnabled, IDC_LOG_ENABLED);
-                    break;
-
-                case IDC_DEL_AFTER_INSTALL:
-                    IS_CHECKED(NewSettingsInfo.bDelInstaller, IDC_DEL_AFTER_INSTALL);
-                    break;
-
-                case IDC_PROXY_DEFAULT:
-                    NewSettingsInfo.Proxy = 0;
-                    EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), FALSE);
-                    EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), FALSE);
-                    break;
-
-                case IDC_NO_PROXY:
-                    NewSettingsInfo.Proxy = 1;
-                    EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), FALSE);
-                    EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), FALSE);
-                    break;
-
-                case IDC_USE_PROXY:
-                    NewSettingsInfo.Proxy = 2;
-                    EnableWindow(GetDlgItem(hDlg, IDC_PROXY_SERVER), TRUE);
-                    EnableWindow(GetDlgItem(hDlg, IDC_NO_PROXY_FOR), TRUE);
-                    break;
-
-                case IDC_DEFAULT_SETTINGS:
-                    FillDefaultSettings(&NewSettingsInfo);
-                    InitSettingsControls(hDlg, &NewSettingsInfo);
-                    break;
+                ATL::CStringW::CopyChars(NewSettingsInfo.szDownloadDir,
+                                         _countof(NewSettingsInfo.szDownloadDir),
+                                         szDir.GetString(),
+                                         szDir.GetLength() + 1);
+            }
+            else
+            {
+                ATL::CStringW szMsgText;
+                szMsgText.LoadStringW(hInst, IDS_CHOOSE_FOLDER_ERROR);
 
-                case IDOK:
+                if (MessageBoxW(hDlg, szMsgText.GetString(), NULL, MB_YESNO) == IDYES)
                 {
-                    WCHAR szDir[MAX_PATH];
-                    WCHAR szProxy[MAX_PATH];
-                    WCHAR szNoProxy[MAX_PATH];
-                    DWORD dwAttr;
-
-                    GetWindowTextW(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT),
-                                   szDir, MAX_PATH);
-
-                    GetWindowTextW(GetDlgItem(hDlg, IDC_PROXY_SERVER),
-                                   szProxy, MAX_PATH);
-                    StringCbCopyW(NewSettingsInfo.szProxyServer, sizeof(NewSettingsInfo.szProxyServer), szProxy);
-
-                    GetWindowTextW(GetDlgItem(hDlg, IDC_NO_PROXY_FOR),
-                                   szNoProxy, MAX_PATH);
-                    StringCbCopyW(NewSettingsInfo.szNoProxyFor, sizeof(NewSettingsInfo.szNoProxyFor), szNoProxy);
-
-                    dwAttr = GetFileAttributesW(szDir);
-                    if (dwAttr != INVALID_FILE_ATTRIBUTES &&
-                        (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
+                    if (CreateDirectoryW(szDir.GetString(), NULL))
                     {
-                        StringCbCopyW(NewSettingsInfo.szDownloadDir,
-                                      sizeof(NewSettingsInfo.szDownloadDir),
-                                      szDir);
+                        EndDialog(hDlg, LOWORD(wParam));
                     }
-                    else
-                    {
-                        WCHAR szMsgText[MAX_STR_LEN];
-
-                        LoadStringW(hInst,
-                                    IDS_CHOOSE_FOLDER_ERROR,
-                                    szMsgText, _countof(szMsgText));
-
-                        if (MessageBoxW(hDlg, szMsgText, NULL, MB_YESNO) == IDYES)
-                        {
-                            if (CreateDirectoryW(szDir, NULL))
-                            {
-                                EndDialog(hDlg, LOWORD(wParam));
-                            }
-                        }
-                        else
-                        {
-                            SetFocus(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT));
-                            break;
-                        }
-                    }
-
-                    SettingsInfo = NewSettingsInfo;
-                    SaveSettings(GetParent(hDlg));
-                    EndDialog(hDlg, LOWORD(wParam));
                 }
-                break;
-
-                case IDCANCEL:
-                    EndDialog(hDlg, LOWORD(wParam));
+                else
+                {
+                    SetFocus(GetDlgItem(hDlg, IDC_DOWNLOAD_DIR_EDIT));
                     break;
+                }
             }
+
+            SettingsInfo = NewSettingsInfo;
+            SaveSettings(GetParent(hDlg));
+            EndDialog(hDlg, LOWORD(wParam));
         }
         break;
+
+        case IDCANCEL:
+            EndDialog(hDlg, LOWORD(wParam));
+            break;
+        }
+    }
+    break;
     }
 
     return FALSE;
index f065d8c..35fff2a 100644 (file)
@@ -5,6 +5,7 @@
  * PURPOSE:         Main program
  * PROGRAMMERS:     Dmitry Chapyshev           (dmitry@reactos.org)
  *                  Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
+ *                  Alexander Shaposhnikov     (chaez.san@gmail.com)
  */
 
 #include "rapps.h"
@@ -18,7 +19,7 @@ HINSTANCE hInst;
 INT SelectedEnumType = ENUM_ALL_COMPONENTS;
 SETTINGS_INFO SettingsInfo;
 
-WCHAR szSearchPattern[MAX_STR_LEN] = L"";
+ATL::CStringW szSearchPattern;
 
 class CRAppsModule : public CComModule
 {
@@ -28,13 +29,13 @@ public:
 BEGIN_OBJECT_MAP(ObjectMap)
 END_OBJECT_MAP()
 
-CRAppsModule                             gModule;
-CAtlWinModule                               gWinModule;
+CRAppsModule gModule;
+CAtlWinModule gWinModule;
 
-void *operator new (size_t, void *buf)
-{
-    return buf;
-}
+//void *operator new (size_t, void *buf)
+//{
+//    return buf;
+//}
 
 static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
 {
@@ -51,30 +52,35 @@ static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
 VOID
 FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
 {
+    ATL::CStringW szDownloadDir;
     pSettingsInfo->bSaveWndPos = TRUE;
     pSettingsInfo->bUpdateAtStart = FALSE;
     pSettingsInfo->bLogEnabled = TRUE;
-    if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pSettingsInfo->szDownloadDir)))
-    {
-        StringCchCatW(pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir), L"\\RAPPS Downloads");
-    } 
-    else
+
+    if (FAILED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szDownloadDir.GetBuffer(MAX_PATH))))
     {
-        ExpandEnvironmentStringsW(L"%SystemDrive%\\RAPPS Downloads", 
-            pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir));
+        szDownloadDir.ReleaseBuffer();
+        if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
+        {
+            szDownloadDir = L"C:";
+        }
     }
+    else
+        szDownloadDir.ReleaseBuffer();
 
-    pSettingsInfo->bDelInstaller = FALSE;
+    szDownloadDir += L"\\RAPPS Downloads";
+    StringCchCopyW(pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir), szDownloadDir.GetString());
 
+    pSettingsInfo->bDelInstaller = FALSE;
     pSettingsInfo->Maximized = FALSE;
     pSettingsInfo->Left = CW_USEDEFAULT;
     pSettingsInfo->Top = CW_USEDEFAULT;
     pSettingsInfo->Width = 680;
     pSettingsInfo->Height = 450;
-
     pSettingsInfo->Proxy = 0;
+
     StringCbCopyW(pSettingsInfo->szProxyServer, sizeof(pSettingsInfo->szProxyServer), L"");
-    StringCbCopyW(pSettingsInfo->szNoProxyFor,  sizeof(pSettingsInfo->szNoProxyFor),  L"");
+    StringCbCopyW(pSettingsInfo->szNoProxyFor, sizeof(pSettingsInfo->szNoProxyFor), L"");
 }
 
 static BOOL
@@ -86,7 +92,7 @@ LoadSettings(VOID)
     if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
     {
         dwSize = sizeof(SettingsInfo);
-        if (RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE)&SettingsInfo, &dwSize) == ERROR_SUCCESS)
+        if (RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE) &SettingsInfo, &dwSize) == ERROR_SUCCESS)
         {
             RegCloseKey(hKey);
             return TRUE;
@@ -110,16 +116,16 @@ SaveSettings(HWND hwnd)
         GetWindowPlacement(hwnd, &wp);
 
         SettingsInfo.Left = wp.rcNormalPosition.left;
-        SettingsInfo.Top  = wp.rcNormalPosition.top;
-        SettingsInfo.Width  = wp.rcNormalPosition.right - 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 (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, NULL,
-        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
+                        REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
     {
-        RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE)&SettingsInfo, sizeof(SettingsInfo));
+        RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE) &SettingsInfo, sizeof(SettingsInfo));
         RegCloseKey(hKey);
     }
 }
@@ -136,12 +142,12 @@ wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nSh
 
     switch (GetUserDefaultUILanguage())
     {
-        case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
-            SetProcessDefaultLayout(LAYOUT_RTL);
-            break;
+    case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
+        SetProcessDefaultLayout(LAYOUT_RTL);
+        break;
 
-        default:
-            break;
+    default:
+        break;
     }
 
     hInst = hInstance;