Sync with trunk r63192.
[reactos.git] / base / applications / rapps / available.c
index 016e580..22ba9c6 100644 (file)
@@ -8,7 +8,6 @@
 
 #include "rapps.h"
 
-
 BOOL
 ShowAvailableAppInfo(INT Index)
 {
@@ -47,39 +46,47 @@ DeleteCurrentAppsDB(VOID)
     WCHAR szSearchPath[MAX_PATH];
     WCHAR szPath[MAX_PATH];
     WCHAR szTmp[MAX_PATH];
+    HRESULT hr;
+    BOOL result = TRUE;
 
-    if (!GetCurrentDirectoryW(MAX_PATH, szPath))
+    if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
         return FALSE;
 
-    swprintf(szCabPath, L"%s\\rappmgr.cab", szPath);
+    hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
+                         L"%ls\\rappmgr.cab",
+                         szPath);
+    if (FAILED(hr))
+        return FALSE;
 
-    if (GetFileAttributesW(szCabPath) != INVALID_FILE_ATTRIBUTES)
-    {
-        if (!DeleteFileW(szCabPath))
-            return FALSE;
-    }
+    result = result && DeleteFileW(szCabPath);
 
-    wcscat(szPath, L"\\rapps\\");
-    swprintf(szSearchPath, L"%s*.txt", szPath);
+    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;
 
     hFind = FindFirstFileW(szSearchPath, &FindFileData);
     if (hFind == INVALID_HANDLE_VALUE)
-        return TRUE;
+        return result;
 
     do
     {
-        swprintf(szTmp, L"%s%s", szPath, FindFileData.cFileName);
-        if (!DeleteFileW(szTmp))
-        {
-            FindClose(hFind);
-            return FALSE;
-        }
-    }
-    while (FindNextFileW(hFind, &FindFileData) != 0);
+        hr = StringCbPrintfW(szTmp, sizeof(szTmp),
+                             L"%ls%ls",
+                             szPath, FindFileData.cFileName);
+        if (FAILED(hr))
+            continue;
+        result = result && DeleteFileW(szTmp);
+    } while (FindNextFileW(hFind, &FindFileData) != 0);
 
     FindClose(hFind);
 
-    return TRUE;
+    return result;
 }
 
 
@@ -95,13 +102,22 @@ UpdateAppsDB(VOID)
 
     DownloadApplicationsDB(APPLICATION_DATEBASE_URL);
 
-    if (!GetCurrentDirectoryW(MAX_PATH, szPath))
+    if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
         return FALSE;
 
-    swprintf(szCabPath, L"%s\\rappmgr.cab", szPath);
+    if (FAILED(StringCbPrintfW(szCabPath, sizeof(szCabPath),
+                               L"%ls\\rappmgr.cab",
+                               szPath)))
+    {
+        return FALSE;
+    }
 
-    wcscat(szPath, L"\\rapps\\");
-    wcscpy(szAppsPath, szPath);
+    if (FAILED(StringCbPrintfW(szAppsPath, sizeof(szAppsPath),
+                               L"%ls\\rapps\\",
+                               szPath)))
+    {
+        return FALSE;
+    }
 
     ExtractFilesFromCab(szCabPath, szAppsPath);
 
@@ -120,16 +136,23 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
     WCHAR szCabPath[MAX_PATH];
     WCHAR szLocale[4 + 1];
     APPLICATION_INFO Info;
+    HRESULT hr;
 
-    if (!GetCurrentDirectoryW(MAX_PATH, szPath))
-    {
+    if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
         return FALSE;
-    }
 
-    swprintf(szCabPath, L"%s\\rappmgr.cab", szPath);
+    hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
+                         L"%ls\\rappmgr.cab",
+                         szPath);
+    if (FAILED(hr))
+        return FALSE;
 
-    wcscat(szPath, L"\\rapps\\");
-    wcscpy(szAppsPath, szPath);
+    hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
+    if (FAILED(hr))
+        return FALSE;
+    hr = StringCbCopyW(szAppsPath, sizeof(szAppsPath), szPath);
+    if (FAILED(hr))
+        return FALSE;
 
     if (!CreateDirectory(szPath, NULL) &&
         GetLastError() != ERROR_ALREADY_EXISTS)
@@ -137,10 +160,9 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
         return FALSE;
     }
 
-    GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE, szLocale, sizeof(szLocale) / sizeof(WCHAR));
-    wcscat(szSectionLocale, szLocale);
-
-    wcscat(szPath, L"*.txt");
+    hr = StringCbCatW(szPath, sizeof(szPath), L"*.txt");
+    if (FAILED(hr))
+        return FALSE;
 
     hFind = FindFirstFileW(szPath, &FindFileData);
     if (hFind == INVALID_HANDLE_VALUE)
@@ -154,6 +176,20 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
             return FALSE;
     }
 
+    if (!GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE,
+                        szLocale, sizeof(szLocale) / sizeof(WCHAR)))
+    {
+        FindClose(hFind);
+        return FALSE;
+    }
+
+    hr = StringCbCatW(szSectionLocale, sizeof(szSectionLocale), szLocale);
+    if (FAILED(hr))
+    {
+        FindClose(hFind);
+        return FALSE;
+    }
+
 #define GET_STRING1(a, b)  \
     if (!ParserGetString(szSectionLocale, a, b, MAX_PATH, FindFileData.cFileName)) \
         if (!ParserGetString(L"Section", a, b, MAX_PATH, FindFileData.cFileName)) \
@@ -187,9 +223,8 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
         GET_STRING2(L"URLSite", Info.szUrlSite);
         GET_STRING2(L"CDPath", Info.szCDPath);
 
-        if (!lpEnumProc(Info)) break;
-    }
-    while (FindNextFileW(hFind, &FindFileData) != 0);
+        if (!lpEnumProc(&Info)) break;
+    } while (FindNextFileW(hFind, &FindFileData) != 0);
 
     FindClose(hFind);