[RAPPS] Replaced my @gmail.com email with @reactos,org one & Removed my copyright...
[reactos.git] / reactos / base / applications / rapps / available.cpp
index f484260..69817da 100644 (file)
@@ -1,11 +1,11 @@
 /*
- * PROJECT:         ReactOS Applications Manager
- * LICENSE:         GPL - See COPYING in the top level directory
- * FILE:            base/applications/rapps/available.cpp
- * PURPOSE:         Classes for working with available applications
- * PROGRAMMERS:     Dmitry Chapyshev           (dmitry@reactos.org)
- *                  Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
- *                  Alexander Shaposhnikov     (chaez.san@gmail.com)
+ * PROJECT:     ReactOS Applications Manager
+ * LICENSE:     GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
+ * FILE:        base/applications/rapps/available.cpp
+ * PURPOSE:     Classes for working with available applications
+ * COPYRIGHT:   Copyright 2009 Dmitry Chapyshev           (dmitry@reactos.org)
+ *              Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
+ *              Copyright 2017 Alexander Shaposhnikov     (sanchaez@reactos.org)
  */
 #include "defines.h"
 
 CAvailableApplicationInfo::CAvailableApplicationInfo(const ATL::CStringW& sFileNameParam)
     : m_Parser(sFileNameParam)
 {
-    LicenseType = LICENSE_TYPE::None;
-    sFileName = sFileNameParam;
+    m_LicenseType = LicenseType::LICENSE_NONE;
+
+    m_sFileName = sFileNameParam;
 
     RetrieveGeneralInfo();
 }
 
 VOID CAvailableApplicationInfo::RefreshAppInfo()
 {
-    if (szUrlDownload.IsEmpty())
+    if (m_szUrlDownload.IsEmpty())
     {
         RetrieveGeneralInfo();
     }
@@ -38,23 +39,23 @@ VOID CAvailableApplicationInfo::RefreshAppInfo()
 // Lazily load general info from the file
 VOID CAvailableApplicationInfo::RetrieveGeneralInfo()
 {
-    Category = m_Parser.GetInt(L"Category");
+    m_Category = m_Parser.GetInt(L"Category");
 
-    if (!GetString(L"Name", szName)
-        || !GetString(L"URLDownload", szUrlDownload))
+    if (!GetString(L"Name", m_szName)
+        || !GetString(L"URLDownload", m_szUrlDownload))
     {
         return;
     }
 
-    GetString(L"RegName", szRegName);
-    GetString(L"Version", szVersion);
-    GetString(L"License", szLicense);
-    GetString(L"Description", szDesc);
-    GetString(L"Size", szSize);
-    GetString(L"URLSite", szUrlSite);
-    GetString(L"CDPath", szCDPath);
-    GetString(L"Language", szRegName);
-    GetString(L"SHA1", szSHA1);
+    GetString(L"RegName", m_szRegName);
+    GetString(L"Version", m_szVersion);
+    GetString(L"License", m_szLicense);
+    GetString(L"Description", m_szDesc);
+    GetString(L"Size", m_szSize);
+    GetString(L"URLSite", m_szUrlSite);
+    GetString(L"CDPath", m_szCDPath);
+    GetString(L"Language", m_szRegName);
+    GetString(L"SHA1", m_szSHA1);
 
     RetrieveLicenseType();
     RetrieveLanguages();
@@ -67,14 +68,16 @@ VOID CAvailableApplicationInfo::RetrieveGeneralInfo()
 
 VOID CAvailableApplicationInfo::RetrieveInstalledStatus()
 {
-    m_IsInstalled = ::GetInstalledVersion(NULL, szRegName)
-        || ::GetInstalledVersion(NULL, szName);
+    m_IsInstalled = ::GetInstalledVersion(NULL, m_szRegName)
+        || ::GetInstalledVersion(NULL, m_szName);
 }
 
 VOID CAvailableApplicationInfo::RetrieveInstalledVersion()
 {
-    m_HasInstalledVersion = ::GetInstalledVersion(&szInstalledVersion, szRegName)
-        || ::GetInstalledVersion(&szInstalledVersion, szName);
+    ATL::CStringW szNameVersion = m_szName + L" " + m_szVersion;
+    m_HasInstalledVersion = ::GetInstalledVersion(&m_szInstalledVersion, m_szRegName)
+        || ::GetInstalledVersion(&m_szInstalledVersion, m_szName)
+        || ::GetInstalledVersion(&m_szInstalledVersion, szNameVersion);
 }
 
 VOID CAvailableApplicationInfo::RetrieveLanguages()
@@ -102,7 +105,7 @@ VOID CAvailableApplicationInfo::RetrieveLanguages()
         {
             if (StrToIntExW(m_szLocale.GetString(), STIF_DEFAULT, &iLCID))
             {
-                Languages.Add(static_cast<LCID>(iLCID));
+                m_LanguageLCIDs.Add(static_cast<LCID>(iLCID));
                 m_szLocale.Empty();
             }
         }
@@ -113,7 +116,7 @@ VOID CAvailableApplicationInfo::RetrieveLanguages()
     {
         if (StrToIntExW(m_szLocale.GetString(), STIF_DEFAULT, &iLCID))
         {
-            Languages.Add(static_cast<LCID>(iLCID));
+            m_LanguageLCIDs.Add(static_cast<LCID>(iLCID));
         }
     }
 
@@ -124,13 +127,13 @@ VOID CAvailableApplicationInfo::RetrieveLicenseType()
 {
     INT IntBuffer = m_Parser.GetInt(L"LicenseType");
 
-    if (IntBuffer < 0 || IntBuffer > LICENSE_TYPE::Max)
+    if (IsLicenseType(IntBuffer))
     {
-        LicenseType = LICENSE_TYPE::None;
+        m_LicenseType = static_cast<LicenseType>(IntBuffer);
     }
     else
     {
-        LicenseType = (LICENSE_TYPE) IntBuffer;
+        m_LicenseType = LicenseType::LICENSE_NONE;
     }
 }
 
@@ -142,10 +145,10 @@ BOOL CAvailableApplicationInfo::FindInLanguages(LCID what) const
     }
 
     //Find locale code in the list
-    const INT nLanguagesSize = Languages.GetSize();
+    const INT nLanguagesSize = m_LanguageLCIDs.GetSize();
     for (INT i = 0; i < nLanguagesSize; ++i)
     {
-        if (Languages[i] == what)
+        if (m_LanguageLCIDs[i] == what)
         {
             return TRUE;
         }
@@ -181,12 +184,12 @@ BOOL CAvailableApplicationInfo::HasInstalledVersion() const
 
 BOOL CAvailableApplicationInfo::HasUpdate() const
 {
-    return (szInstalledVersion.Compare(szVersion) < 0) ? TRUE : FALSE;
+    return (m_szInstalledVersion.Compare(m_szVersion) < 0) ? TRUE : FALSE;
 }
 
 VOID CAvailableApplicationInfo::SetLastWriteTime(FILETIME* ftTime)
 {
-    RtlCopyMemory(&ftCacheStamp, ftTime, sizeof(FILETIME));
+    RtlCopyMemory(&m_ftCacheStamp, ftTime, sizeof(FILETIME));
 }
 
 inline BOOL CAvailableApplicationInfo::GetString(LPCWSTR lpKeyName, ATL::CStringW& ReturnedString)
@@ -201,15 +204,36 @@ inline BOOL CAvailableApplicationInfo::GetString(LPCWSTR lpKeyName, ATL::CString
 // CAvailableApplicationInfo 
 
 // CAvailableApps
-CAvailableApps::CAvailableApps()
+ATL::CStringW CAvailableApps::m_szPath;
+ATL::CStringW CAvailableApps::m_szCabPath;
+ATL::CStringW CAvailableApps::m_szAppsPath;
+ATL::CStringW CAvailableApps::m_szSearchPath;
+
+BOOL CAvailableApps::InitializeStaticStrings()
 {
-    //set all paths
+
+    if (!m_szPath.IsEmpty())
+    {
+        // strings are filled
+        return TRUE;
+    }
+
+    //FIXME: maybe provide a fallback?
     if (GetStorageDirectory(m_szPath))
     {
         m_szAppsPath = m_szPath + L"\\rapps\\";
         m_szCabPath = m_szPath + L"\\rappmgr.cab";
         m_szSearchPath = m_szAppsPath + L"*.txt";
+        return TRUE;
     }
+
+    return FALSE;
+}
+
+CAvailableApps::CAvailableApps()
+{
+    //set all paths
+    InitializeStaticStrings();
 }
 
 VOID CAvailableApps::FreeCachedEntries()
@@ -219,12 +243,11 @@ VOID CAvailableApps::FreeCachedEntries()
     /* loop and deallocate all the cached app infos in the list */
     while (InfoListPosition)
     {
-        CAvailableApplicationInfo* Info = m_InfoList.GetAt(InfoListPosition);
-        m_InfoList.RemoveHead();
+        CAvailableApplicationInfo* Info = m_InfoList.GetNext(InfoListPosition);
         delete Info;
-
-        InfoListPosition = m_InfoList.GetHeadPosition();
     }
+
+    m_InfoList.RemoveAll();
 }
 
 VOID CAvailableApps::DeleteCurrentAppsDB()
@@ -232,10 +255,11 @@ VOID CAvailableApps::DeleteCurrentAppsDB()
     HANDLE hFind = INVALID_HANDLE_VALUE;
     WIN32_FIND_DATAW FindFileData;
 
-    if (m_szPath.IsEmpty())
+    if (!InitializeStaticStrings())
+    {
         return;
+    }
 
-    DeleteFileW(m_szCabPath.GetString());
     hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
 
     if (hFind != INVALID_HANDLE_VALUE)
@@ -243,55 +267,68 @@ VOID CAvailableApps::DeleteCurrentAppsDB()
         ATL::CStringW szTmp;
         do
         {
-            szTmp = m_szPath + FindFileData.cFileName;
+            szTmp = m_szAppsPath + FindFileData.cFileName;
             DeleteFileW(szTmp.GetString());
         } while (FindNextFileW(hFind, &FindFileData) != 0);
         FindClose(hFind);
     }
+
+    RemoveDirectoryW(m_szAppsPath);
+    RemoveDirectoryW(m_szPath);
 }
 
 BOOL CAvailableApps::UpdateAppsDB()
 {
-    DeleteCurrentAppsDB();
+    HANDLE hFind = INVALID_HANDLE_VALUE;
+    WIN32_FIND_DATAW FindFileData;
 
-    CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL);
+    if (!InitializeStaticStrings())
+    {
+        return FALSE;
+    }
 
-    if (m_szPath.IsEmpty())
+    if (!CreateDirectoryW(m_szPath.GetString(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS)
+    {
         return FALSE;
+    }
+
+    //if there are some files in the db folder - we're good
+    hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
+    if (hFind != INVALID_HANDLE_VALUE)
+    {
+        return TRUE;
+    }
+
+    CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL);
 
     if (!ExtractFilesFromCab(m_szCabPath, m_szAppsPath))
     {
         return FALSE;
     }
 
+    DeleteFileW(m_szCabPath.GetString());
+
     return TRUE;
 }
 
-BOOL CAvailableApps::EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
+BOOL CAvailableApps::ForceUpdateAppsDB()
 {
+    DeleteCurrentAppsDB();
+    return UpdateAppsDB();
+}
+
+BOOL CAvailableApps::Enum(INT EnumType, AVAILENUMPROC lpEnumProc)
+{
+
     HANDLE hFind = INVALID_HANDLE_VALUE;
     WIN32_FIND_DATAW FindFileData;
 
-    if (!CreateDirectoryW(m_szPath.GetString(), NULL) &&
-        GetLastError() != ERROR_ALREADY_EXISTS)
-    {
-        return FALSE;
-    }
-
     hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
 
     if (hFind == INVALID_HANDLE_VALUE)
     {
-        if(!UpdateAppsDB()) {
-            return FALSE;
-        }
-
-        hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
-
-        if (hFind == INVALID_HANDLE_VALUE)
-        {
-            return FALSE;
-        }
+        //no db yet
+        return FALSE;
     }
 
     do
@@ -306,10 +343,10 @@ BOOL CAvailableApps::EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnu
             Info = m_InfoList.GetNext(CurrentListPosition);
 
             // do we already have this entry in cache?
-            if (Info->sFileName == FindFileData.cFileName)
+            if (Info->m_sFileName == FindFileData.cFileName)
             {
                 // is it current enough, or the file has been modified since our last time here?
-                if (CompareFileTime(&FindFileData.ftLastWriteTime, &Info->ftCacheStamp) == 1)
+                if (CompareFileTime(&FindFileData.ftLastWriteTime, &Info->m_ftCacheStamp) == 1)
                 {
                     // recreate our cache, this is the slow path
                     m_InfoList.RemoveAt(LastListPosition);
@@ -334,16 +371,16 @@ BOOL CAvailableApps::EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnu
         m_InfoList.AddTail(Info);
 
 skip_if_cached:
-        if (Info->Category == FALSE)
+        if (Info->m_Category == FALSE)
             continue;
 
-        if (EnumType != Info->Category && EnumType != ENUM_ALL_AVAILABLE)
+        if (EnumType != Info->m_Category && EnumType != ENUM_ALL_AVAILABLE)
             continue;
 
         Info->RefreshAppInfo();
 
         if (lpEnumProc)
-            lpEnumProc(static_cast<PAPPLICATION_INFO>(Info), m_szAppsPath.GetString());
+            lpEnumProc(static_cast<CAvailableApplicationInfo*>(Info), m_szAppsPath.GetString());
 
     } while (FindNextFileW(hFind, &FindFileData) != 0);
 
@@ -351,7 +388,7 @@ skip_if_cached:
     return TRUE;
 }
 
-const PAPPLICATION_INFO CAvailableApps::FindInfo(const ATL::CStringW& szAppName)
+CAvailableApplicationInfo* CAvailableApps::FindInfo(const ATL::CStringW& szAppName) const
 {
     if (m_InfoList.IsEmpty())
     {
@@ -360,11 +397,11 @@ const PAPPLICATION_INFO CAvailableApps::FindInfo(const ATL::CStringW& szAppName)
 
     // linear search
     POSITION CurrentListPosition = m_InfoList.GetHeadPosition();
-    PAPPLICATION_INFO info;
+    CAvailableApplicationInfo* info;
     while (CurrentListPosition != NULL)
     {
         info = m_InfoList.GetNext(CurrentListPosition);
-        if (info->szName == szAppName)
+        if (info->m_szName == szAppName)
         {
             return info;
         }
@@ -372,12 +409,12 @@ const PAPPLICATION_INFO CAvailableApps::FindInfo(const ATL::CStringW& szAppName)
     return NULL;
 }
 
-ATL::CSimpleArray<PAPPLICATION_INFO> CAvailableApps::FindInfoList(const ATL::CSimpleArray<ATL::CStringW> &arrAppsNames)
+ATL::CSimpleArray<CAvailableApplicationInfo*> CAvailableApps::FindInfoList(const ATL::CSimpleArray<ATL::CStringW> &arrAppsNames) const
 {
-    ATL::CSimpleArray<PAPPLICATION_INFO> result;
+    ATL::CSimpleArray<CAvailableApplicationInfo*> result;
     for (INT i = 0; i < arrAppsNames.GetSize(); ++i)
     {
-        PAPPLICATION_INFO Info = FindInfo(arrAppsNames[i]);
+        CAvailableApplicationInfo* Info = FindInfo(arrAppsNames[i]);
         if (Info)
         {
             result.Add(Info);
@@ -386,32 +423,32 @@ ATL::CSimpleArray<PAPPLICATION_INFO> CAvailableApps::FindInfoList(const ATL::CSi
     return result;
 }
 
-const ATL::CStringW& CAvailableApps::GetFolderPath()
+const ATL::CStringW& CAvailableApps::GetFolderPath() const
 {
     return m_szPath;
 }
 
-const ATL::CStringW& CAvailableApps::GetAppPath()
+const ATL::CStringW& CAvailableApps::GetAppPath() const
 {
     return m_szAppsPath;
 }
 
-const ATL::CStringW& CAvailableApps::GetCabPath()
+const ATL::CStringW& CAvailableApps::GetCabPath() const
 {
     return m_szCabPath;
 }
 
-const LPCWSTR CAvailableApps::GetFolderPathString()
+LPCWSTR CAvailableApps::GetFolderPathString() const
 {
     return m_szPath.GetString();
 }
 
-const LPCWSTR CAvailableApps::GetAppPathString()
+LPCWSTR CAvailableApps::GetAppPathString() const
 {
     return m_szPath.GetString();
 }
 
-const LPCWSTR CAvailableApps::GetCabPathString()
+LPCWSTR CAvailableApps::GetCabPathString() const
 {
     return m_szPath.GetString();
 }