[RAPPS] unuglify icons. CORE-10642
[reactos.git] / reactos / base / applications / rapps / available.c
index e7ca270..421eacd 100644 (file)
@@ -3,11 +3,31 @@
  * LICENSE:         GPL - See COPYING in the top level directory
  * FILE:            base/applications/rapps/available.c
  * PURPOSE:         Functions for working with availabled applications
- * PROGRAMMERS:     Dmitry Chapyshev (dmitry@reactos.org)
+ * PROGRAMMERS:     Dmitry Chapyshev           (dmitry@reactos.org)
+ *                  Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
  */
 
 #include "rapps.h"
 
+#define ADD_TEXT(a, b, c, d) \
+    if (b[0] != '\0') \
+    { \
+        LoadStringW(hInst, a, szText, _countof(szText)); \
+        InsertRichEditText(szText, c); \
+        InsertRichEditText(b, d); \
+    } \
+
+#define GET_STRING1(a, b)  \
+    if (!ParserGetString(a, b, MAX_PATH, FindFileData.cFileName)) \
+        continue;
+
+#define GET_STRING2(a, b)  \
+    if (!ParserGetString(a, b, MAX_PATH, FindFileData.cFileName)) \
+        b[0] = '\0';
+
+LIST_ENTRY CachedEntriesHead = { &CachedEntriesHead, &CachedEntriesHead };
+PLIST_ENTRY pCachedEntry = &CachedEntriesHead;
+
 BOOL
 ShowAvailableAppInfo(INT Index)
 {
@@ -20,19 +40,11 @@ ShowAvailableAppInfo(INT Index)
 
     InsertRichEditText(L"\n", 0);
 
-#define ADD_TEXT(a, b, c, d) \
-    if (b[0] != '\0') \
-    { \
-        LoadStringW(hInst, a, szText, sizeof(szText) / sizeof(WCHAR)); \
-        InsertRichEditText(szText, c); \
-        InsertRichEditText(b, d); \
-    } \
-
-    ADD_TEXT(IDS_AINFO_VERSION, Info->szVersion, CFE_BOLD, 0);
-    ADD_TEXT(IDS_AINFO_LICENSE, Info->szLicense, CFE_BOLD, 0);
-    ADD_TEXT(IDS_AINFO_SIZE, Info->szSize, CFE_BOLD, 0);
-    ADD_TEXT(IDS_AINFO_URLSITE, Info->szUrlSite, CFE_BOLD, CFE_LINK);
-    ADD_TEXT(IDS_AINFO_DESCRIPTION, Info->szDesc, CFE_BOLD, 0);
+    ADD_TEXT(IDS_AINFO_VERSION,     Info->szVersion, CFE_BOLD, 0);
+    ADD_TEXT(IDS_AINFO_LICENSE,     Info->szLicense, CFE_BOLD, 0);
+    ADD_TEXT(IDS_AINFO_SIZE,        Info->szSize,    CFE_BOLD, 0);
+    ADD_TEXT(IDS_AINFO_URLSITE,     Info->szUrlSite, CFE_BOLD, CFE_LINK);
+    ADD_TEXT(IDS_AINFO_DESCRIPTION, Info->szDesc,    CFE_BOLD, 0);
 
     return TRUE;
 }
@@ -49,7 +61,7 @@ DeleteCurrentAppsDB(VOID)
     HRESULT hr;
     BOOL result = TRUE;
 
-    if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
+    if (!GetStorageDirectory(szPath, _countof(szPath)))
         return FALSE;
 
     hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
@@ -61,6 +73,7 @@ DeleteCurrentAppsDB(VOID)
     result = result && DeleteFileW(szCabPath);
 
     hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
+
     if (FAILED(hr))
         return FALSE;
 
@@ -71,6 +84,7 @@ DeleteCurrentAppsDB(VOID)
         return FALSE;
 
     hFind = FindFirstFileW(szSearchPath, &FindFileData);
+
     if (hFind == INVALID_HANDLE_VALUE)
         return result;
 
@@ -82,6 +96,7 @@ DeleteCurrentAppsDB(VOID)
         if (FAILED(hr))
             continue;
         result = result && DeleteFileW(szTmp);
+
     } while (FindNextFileW(hFind, &FindFileData) != 0);
 
     FindClose(hFind);
@@ -100,9 +115,9 @@ UpdateAppsDB(VOID)
     if (!DeleteCurrentAppsDB())
         return FALSE;
 
-    DownloadApplicationsDB(APPLICATION_DATEBASE_URL);
+    DownloadApplicationsDB(APPLICATION_DATABASE_URL);
 
-    if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
+    if (!GetStorageDirectory(szPath, _countof(szPath)))
         return FALSE;
 
     if (FAILED(StringCbPrintfW(szCabPath, sizeof(szCabPath),
@@ -132,13 +147,11 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
     WIN32_FIND_DATAW FindFileData;
     WCHAR szPath[MAX_PATH];
     WCHAR szAppsPath[MAX_PATH];
-    WCHAR szSectionLocale[MAX_PATH] = L"Section.";
     WCHAR szCabPath[MAX_PATH];
-    WCHAR szLocale[4 + 1];
-    APPLICATION_INFO Info;
+    PAPPLICATION_INFO Info;
     HRESULT hr;
 
-    if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
+    if (!GetStorageDirectory(szPath, _countof(szPath)))
         return FALSE;
 
     hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
@@ -148,9 +161,12 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
         return FALSE;
 
     hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
+
     if (FAILED(hr))
         return FALSE;
+
     hr = StringCbCopyW(szAppsPath, sizeof(szAppsPath), szPath);
+
     if (FAILED(hr))
         return FALSE;
 
@@ -161,72 +177,116 @@ EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
     }
 
     hr = StringCbCatW(szPath, sizeof(szPath), L"*.txt");
+
     if (FAILED(hr))
         return FALSE;
 
     hFind = FindFirstFileW(szPath, &FindFileData);
+
     if (hFind == INVALID_HANDLE_VALUE)
     {
         if (GetFileAttributesW(szCabPath) == INVALID_FILE_ATTRIBUTES)
-            DownloadApplicationsDB(APPLICATION_DATEBASE_URL);
+            DownloadApplicationsDB(APPLICATION_DATABASE_URL);
 
         ExtractFilesFromCab(szCabPath, szAppsPath);
         hFind = FindFirstFileW(szPath, &FindFileData);
+
         if (hFind == INVALID_HANDLE_VALUE)
             return FALSE;
     }
 
-    if (!GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE,
-                        szLocale, sizeof(szLocale) / sizeof(WCHAR)))
+    do
     {
-        FindClose(hFind);
-        return FALSE;
-    }
+        /* loop for all the cached entries */
+        for (pCachedEntry = CachedEntriesHead.Flink; pCachedEntry != &CachedEntriesHead; pCachedEntry = pCachedEntry->Flink)
+        {
+            Info = CONTAINING_RECORD(pCachedEntry, APPLICATION_INFO, List);
+
+            /* do we already have this entry in cache? */
+            if(_wcsicmp(FindFileData.cFileName, Info->cFileName) == 0)
+            {
+                /* 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);
+                }
+                else
+                {
+                    /* speedy path, compare directly, we already have the data */
+                    goto skip_if_cached;
+                }
+
+                break;
+            }
+        }
 
-    hr = StringCbCatW(szSectionLocale, sizeof(szSectionLocale), szLocale);
-    if (FAILED(hr))
-    {
-        FindClose(hFind);
-        return FALSE;
-    }
+        /* create a new entry */
+        Info = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(APPLICATION_INFO));
 
-#define GET_STRING1(a, b)  \
-    if (!ParserGetString(szSectionLocale, a, b, MAX_PATH, FindFileData.cFileName)) \
-        if (!ParserGetString(L"Section", a, b, MAX_PATH, FindFileData.cFileName)) \
+        if(!Info)
+            break;
+
+        Info->Category = ParserGetInt(L"Category", FindFileData.cFileName);
+
+        /* copy the cache-related fields for the next time */
+        RtlCopyMemory(&Info->cFileName,    &FindFileData.cFileName, MAX_PATH);
+        RtlCopyMemory(&Info->ftCacheStamp, &FindFileData.ftLastWriteTime, sizeof(FILETIME));
+
+        /* add our cached entry to the cached list */
+        InsertTailList(&CachedEntriesHead, &Info->List);
+
+skip_if_cached:
+
+        if (Info->Category == FALSE)
             continue;
 
-#define GET_STRING2(a, b)  \
-    if (!ParserGetString(szSectionLocale, a, b, MAX_PATH, FindFileData.cFileName)) \
-        if (!ParserGetString(L"Section", a, b, MAX_PATH, FindFileData.cFileName)) \
-            b[0] = '\0';
+        if (EnumType != Info->Category && EnumType != ENUM_ALL_AVAILABLE)
+            continue;
 
-    do
-    {
-        Info.Category = ParserGetInt(szSectionLocale, L"Category", FindFileData.cFileName);
-        if (Info.Category == -1)
+        /* 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)
         {
-            Info.Category = ParserGetInt(L"Section", L"Category", FindFileData.cFileName);
-            if (Info.Category == -1)
-                continue;
+            GET_STRING1(L"Name",        Info->szName);
+            GET_STRING1(L"URLDownload", Info->szUrlDownload);
+
+            GET_STRING2(L"RegName",     Info->szRegName);
+            GET_STRING2(L"Version",     Info->szVersion);
+            GET_STRING2(L"License",     Info->szLicense);
+            GET_STRING2(L"Description", Info->szDesc);
+            GET_STRING2(L"Size",        Info->szSize);
+            GET_STRING2(L"URLSite",     Info->szUrlSite);
+            GET_STRING2(L"CDPath",      Info->szCDPath);
+            GET_STRING2(L"SHA1",        Info->szSHA1);
         }
 
-        if (EnumType != Info.Category && EnumType != ENUM_ALL_AVAILABLE) continue;
+        if (!lpEnumProc(Info))
+            break;
 
-        GET_STRING1(L"Name", Info.szName);
-        GET_STRING1(L"URLDownload", Info.szUrlDownload);
-
-        GET_STRING2(L"RegName", Info.szRegName);
-        GET_STRING2(L"Version", Info.szVersion);
-        GET_STRING2(L"License", Info.szLicense);
-        GET_STRING2(L"Description", Info.szDesc);
-        GET_STRING2(L"Size", Info.szSize);
-        GET_STRING2(L"URLSite", Info.szUrlSite);
-        GET_STRING2(L"CDPath", Info.szCDPath);
-
-        if (!lpEnumProc(&Info)) break;
     } while (FindNextFileW(hFind, &FindFileData) != 0);
 
     FindClose(hFind);
 
     return TRUE;
 }
+
+VOID FreeCachedAvailableEntries(VOID)
+{
+     PAPPLICATION_INFO Info;
+    /* loop and deallocate all the cached app infos in the list */
+    for (pCachedEntry = CachedEntriesHead.Flink; pCachedEntry != &CachedEntriesHead;)
+    {
+         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;
+        /* flush them down the toilet :D */
+        RemoveEntryList(&Info->List);
+        HeapFree(GetProcessHeap(), 0, Info);
+    }
+}
\ No newline at end of file