* PROJECT: ReactOS Applications Manager
* LICENSE: GPL - See COPYING in the top level directory
* FILE: base/applications/rapps/available.cpp
- * PURPOSE: Functions for working with available applications
+ * 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)
*/
+#include "defines.h"
-#include "rapps.h"
+#include "available.h"
+#include "misc.h"
+#include "dialogs.h"
-#define ADD_TEXT(a, b, c, d) \
- if (b[0] != '\0') \
- { \
- LoadStringW(hInst, a, szText, _countof(szText)); \
- InsertRichEditText(szText, c); \
- InsertRichEditText(b, d); \
- } \
+#include <atlcoll.h>
+#include <atlsimpcoll.h>
+#include <atlstr.h>
-#define GET_STRING1(a, b) \
- if (!ParserGetString(a, b, _countof(b), FindFileData.cFileName)) \
- continue;
-
-#define GET_STRING2(a, b) \
- if (!ParserGetString(a, b, _countof(b), FindFileData.cFileName)) \
- b[0] = '\0';
+ // CAvailableApplicationInfo
+CAvailableApplicationInfo::CAvailableApplicationInfo(const ATL::CStringW& sFileNameParam)
+ : m_Parser(sFileNameParam)
+{
+ LicenseType = LICENSE_TYPE::None;
+ sFileName = sFileNameParam;
-LIST_ENTRY CachedEntriesHead = { &CachedEntriesHead, &CachedEntriesHead };
-PLIST_ENTRY pCachedEntry = &CachedEntriesHead;
+ RetrieveGeneralInfo();
+}
-BOOL
-ShowAvailableAppInfo(INT Index)
+VOID CAvailableApplicationInfo::RefreshAppInfo()
{
- PAPPLICATION_INFO Info = (PAPPLICATION_INFO) ListViewGetlParam(Index);
- WCHAR szText[MAX_STR_LEN];
+ if (szUrlDownload.IsEmpty())
+ {
+ RetrieveGeneralInfo();
+ }
+}
- if (!Info) return FALSE;
+// Lazily load general info from the file
+VOID CAvailableApplicationInfo::RetrieveGeneralInfo()
+{
+ Category = m_Parser.GetInt(L"Category");
- NewRichEditText(Info->szName, CFE_BOLD);
+ if (!GetString(L"Name", szName)
+ || !GetString(L"URLDownload", szUrlDownload))
+ {
+ return;
+ }
- InsertRichEditText(L"\n", 0);
+ 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);
+
+ RetrieveLicenseType();
+ RetrieveLanguages();
+ RetrieveInstalledStatus();
+ if (m_IsInstalled)
+ {
+ RetrieveInstalledVersion();
+ }
+}
- 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);
+VOID CAvailableApplicationInfo::RetrieveInstalledStatus()
+{
+ m_IsInstalled = ::GetInstalledVersion(NULL, szRegName)
+ || ::GetInstalledVersion(NULL, szName);
+}
- return TRUE;
+VOID CAvailableApplicationInfo::RetrieveInstalledVersion()
+{
+ m_HasInstalledVersion = ::GetInstalledVersion(&szInstalledVersion, szRegName)
+ || ::GetInstalledVersion(&szInstalledVersion, szName);
}
-static BOOL
-DeleteCurrentAppsDB(VOID)
+VOID CAvailableApplicationInfo::RetrieveLanguages()
{
- 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;
- BOOL result = TRUE;
-
- if (!GetStorageDirectory(szPath, _countof(szPath)))
- return FALSE;
+ const WCHAR cDelimiter = L'|';
+ ATL::CStringW szBuffer;
- hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
- L"%ls\\rappmgr.cab",
- szPath);
- if (FAILED(hr))
- return FALSE;
+ // TODO: Get multiline parameter
+ if (!m_Parser.GetString(L"Languages", szBuffer))
+ {
+ m_HasLanguageInfo = FALSE;
+ return;
+ }
- result = result && DeleteFileW(szCabPath);
+ // Parse parameter string
+ ATL::CStringW m_szLocale;
+ INT iLCID;
+ for (INT i = 0; szBuffer[i] != UNICODE_NULL; ++i)
+ {
+ if (szBuffer[i] != cDelimiter && szBuffer[i] != L'\n')
+ {
+ m_szLocale += szBuffer[i];
+ }
+ else
+ {
+ if (StrToIntExW(m_szLocale.GetString(), STIF_DEFAULT, &iLCID))
+ {
+ Languages.Add(static_cast<LCID>(iLCID));
+ m_szLocale.Empty();
+ }
+ }
+ }
- hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
+ // For the text after delimiter
+ if (!m_szLocale.IsEmpty())
+ {
+ if (StrToIntExW(m_szLocale.GetString(), STIF_DEFAULT, &iLCID))
+ {
+ Languages.Add(static_cast<LCID>(iLCID));
+ }
+ }
- if (FAILED(hr))
- return FALSE;
+ m_HasLanguageInfo = TRUE;
+}
- hr = StringCbPrintfW(szSearchPath, sizeof(szSearchPath),
- L"%ls*.txt",
- szPath);
- if (FAILED(hr))
- return FALSE;
+VOID CAvailableApplicationInfo::RetrieveLicenseType()
+{
+ INT IntBuffer = m_Parser.GetInt(L"LicenseType");
- hFind = FindFirstFileW(szSearchPath, &FindFileData);
+ if (IntBuffer < 0 || IntBuffer > LICENSE_TYPE::Max)
+ {
+ LicenseType = LICENSE_TYPE::None;
+ }
+ else
+ {
+ LicenseType = (LICENSE_TYPE) IntBuffer;
+ }
+}
- if (hFind == INVALID_HANDLE_VALUE)
- return result;
+BOOL CAvailableApplicationInfo::FindInLanguages(LCID what) const
+{
+ if (!m_HasLanguageInfo)
+ {
+ return FALSE;
+ }
- do
+ //Find locale code in the list
+ const INT nLanguagesSize = Languages.GetSize();
+ for (INT i = 0; i < nLanguagesSize; ++i)
{
- hr = StringCbPrintfW(szTmp, sizeof(szTmp),
- L"%ls%ls",
- szPath, FindFileData.cFileName);
- if (FAILED(hr))
- continue;
- result = result && DeleteFileW(szTmp);
+ if (Languages[i] == what)
+ {
+ return TRUE;
+ }
+ }
- } while (FindNextFileW(hFind, &FindFileData) != 0);
+ return FALSE;
+}
- FindClose(hFind);
+BOOL CAvailableApplicationInfo::HasLanguageInfo() const
+{
+ return m_HasLanguageInfo;
+}
- return result;
+BOOL CAvailableApplicationInfo::HasNativeLanguage() const
+{
+ return FindInLanguages(GetUserDefaultLCID());
}
+BOOL CAvailableApplicationInfo::HasEnglishLanguage() const
+{
+ return FindInLanguages(MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), SORT_DEFAULT));
+}
-BOOL
-UpdateAppsDB(VOID)
+BOOL CAvailableApplicationInfo::IsInstalled() const
{
- WCHAR szPath[MAX_PATH];
- WCHAR szAppsPath[MAX_PATH];
- WCHAR szCabPath[MAX_PATH];
+ return m_IsInstalled;
+}
- if (!DeleteCurrentAppsDB())
- return FALSE;
+BOOL CAvailableApplicationInfo::HasInstalledVersion() const
+{
+ return m_HasInstalledVersion;
+}
- DownloadApplicationsDB(APPLICATION_DATABASE_URL);
+BOOL CAvailableApplicationInfo::HasUpdate() const
+{
+ return (szInstalledVersion.Compare(szVersion) < 0) ? TRUE : FALSE;
+}
- if (!GetStorageDirectory(szPath, _countof(szPath)))
- return FALSE;
+VOID CAvailableApplicationInfo::SetLastWriteTime(FILETIME* ftTime)
+{
+ RtlCopyMemory(&ftCacheStamp, ftTime, sizeof(FILETIME));
+}
- if (FAILED(StringCbPrintfW(szCabPath, sizeof(szCabPath),
- L"%ls\\rappmgr.cab",
- szPath)))
+inline BOOL CAvailableApplicationInfo::GetString(LPCWSTR lpKeyName, ATL::CStringW& ReturnedString)
+{
+ if (!m_Parser.GetString(lpKeyName, ReturnedString))
{
+ ReturnedString.Empty();
return FALSE;
}
+ return TRUE;
+}
+// CAvailableApplicationInfo
- if (FAILED(StringCbPrintfW(szAppsPath, sizeof(szAppsPath),
- L"%ls\\rapps\\",
- szPath)))
+// CAvailableApps
+CAvailableApps::CAvailableApps()
+{
+ //set all paths
+ if (GetStorageDirectory(m_szPath))
{
- return FALSE;
+ m_szAppsPath = m_szPath + L"\\rapps\\";
+ m_szCabPath = m_szPath + L"\\rappmgr.cab";
+ m_szSearchPath = m_szAppsPath + L"*.txt";
}
+}
- ExtractFilesFromCab(szCabPath, szAppsPath);
+VOID CAvailableApps::FreeCachedEntries()
+{
+ POSITION InfoListPosition = m_InfoList.GetHeadPosition();
- return TRUE;
-}
+ /* loop and deallocate all the cached app infos in the list */
+ while (InfoListPosition)
+ {
+ CAvailableApplicationInfo* Info = m_InfoList.GetAt(InfoListPosition);
+ m_InfoList.RemoveHead();
+ delete Info;
+ InfoListPosition = m_InfoList.GetHeadPosition();
+ }
+}
-BOOL
-EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
+VOID CAvailableApps::DeleteCurrentAppsDB()
{
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATAW FindFileData;
- WCHAR szPath[MAX_PATH];
- WCHAR szAppsPath[MAX_PATH];
- WCHAR szCabPath[MAX_PATH];
- PAPPLICATION_INFO Info;
- HRESULT hr;
- if (!GetStorageDirectory(szPath, _countof(szPath)))
- return FALSE;
+ if (m_szPath.IsEmpty())
+ return;
- hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
- L"%ls\\rappmgr.cab",
- szPath);
- if (FAILED(hr))
- return FALSE;
+ DeleteFileW(m_szCabPath.GetString());
+ hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
- hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
+ if (hFind != INVALID_HANDLE_VALUE)
+ {
+ ATL::CStringW szTmp;
+ do
+ {
+ szTmp = m_szPath + FindFileData.cFileName;
+ DeleteFileW(szTmp.GetString());
+ } while (FindNextFileW(hFind, &FindFileData) != 0);
+ FindClose(hFind);
+ }
+}
- if (FAILED(hr))
- return FALSE;
+BOOL CAvailableApps::UpdateAppsDB()
+{
+ DeleteCurrentAppsDB();
- hr = StringCbCopyW(szAppsPath, sizeof(szAppsPath), szPath);
+ CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL);
- if (FAILED(hr))
+ if (m_szPath.IsEmpty())
return FALSE;
- if (!CreateDirectory(szPath, NULL) &&
- GetLastError() != ERROR_ALREADY_EXISTS)
+ if (!ExtractFilesFromCab(m_szCabPath, m_szAppsPath))
{
return FALSE;
}
- hr = StringCbCatW(szPath, sizeof(szPath), L"*.txt");
+ return TRUE;
+}
+
+BOOL CAvailableApps::EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
+{
+ HANDLE hFind = INVALID_HANDLE_VALUE;
+ WIN32_FIND_DATAW FindFileData;
- if (FAILED(hr))
+ if (!CreateDirectoryW(m_szPath.GetString(), NULL) &&
+ GetLastError() != ERROR_ALREADY_EXISTS)
+ {
return FALSE;
+ }
- hFind = FindFirstFileW(szPath, &FindFileData);
+ hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
- if (GetFileAttributesW(szCabPath) == INVALID_FILE_ATTRIBUTES)
- DownloadApplicationsDB(APPLICATION_DATABASE_URL);
+ if(!UpdateAppsDB()) {
+ return FALSE;
+ }
- ExtractFilesFromCab(szCabPath, szAppsPath);
- hFind = FindFirstFileW(szPath, &FindFileData);
+ hFind = FindFirstFileW(m_szSearchPath.GetString(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
+ {
return FALSE;
+ }
}
do
{
- /* loop for all the cached entries */
- for (pCachedEntry = CachedEntriesHead.Flink; pCachedEntry != &CachedEntriesHead; pCachedEntry = pCachedEntry->Flink)
+ // loop for all the cached entries
+ POSITION CurrentListPosition = m_InfoList.GetHeadPosition();
+ CAvailableApplicationInfo* Info = NULL;
+
+ while (CurrentListPosition != NULL)
{
- Info = CONTAINING_RECORD(pCachedEntry, APPLICATION_INFO, List);
+ POSITION LastListPosition = CurrentListPosition;
+ Info = m_InfoList.GetNext(CurrentListPosition);
- /* do we already have this entry in cache? */
- if(_wcsicmp(FindFileData.cFileName, Info->cFileName) == 0)
+ // do we already have this entry in cache?
+ if (Info->sFileName == FindFileData.cFileName)
{
- /* is it current enough, or the file has been modified since our last time here? */
+ // 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);
+ // recreate our cache, this is the slow path
+ m_InfoList.RemoveAt(LastListPosition);
+
+ delete Info;
+ Info = NULL;
+ break;
}
else
{
- /* speedy path, compare directly, we already have the data */
+ // 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));
+ // create a new entry
+ Info = new CAvailableApplicationInfo(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);
+ // set a timestamp for the next time
+ Info->SetLastWriteTime(&FindFileData.ftLastWriteTime);
+ m_InfoList.AddTail(Info);
skip_if_cached:
-
if (Info->Category == FALSE)
continue;
if (EnumType != Info->Category && EnumType != ENUM_ALL_AVAILABLE)
continue;
- /* if our cache hit was only partial, we need to parse
- and lazily fill the rest of fields only when needed */
+ Info->RefreshAppInfo();
- if (Info->szUrlDownload[0] == 0)
- {
- 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 (!lpEnumProc(Info))
- break;
+ if (lpEnumProc)
+ lpEnumProc(static_cast<PAPPLICATION_INFO>(Info), m_szAppsPath.GetString());
} while (FindNextFileW(hFind, &FindFileData) != 0);
FindClose(hFind);
-
return TRUE;
}
-VOID FreeCachedAvailableEntries(VOID)
+const PAPPLICATION_INFO CAvailableApps::FindInfo(const ATL::CStringW& szAppName)
{
- PAPPLICATION_INFO Info;
-
- /* loop and deallocate all the cached app infos in the list */
- for (pCachedEntry = CachedEntriesHead.Flink; pCachedEntry != &CachedEntriesHead;)
+ if (m_InfoList.IsEmpty())
{
- 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);
+ return NULL;
+ }
+
+ // linear search
+ POSITION CurrentListPosition = m_InfoList.GetHeadPosition();
+ PAPPLICATION_INFO info;
+ while (CurrentListPosition != NULL)
+ {
+ info = m_InfoList.GetNext(CurrentListPosition);
+ if (info->szName == szAppName)
+ {
+ return info;
+ }
}
-}
\ No newline at end of file
+ return NULL;
+}
+
+ATL::CSimpleArray<PAPPLICATION_INFO> CAvailableApps::FindInfoList(const ATL::CSimpleArray<ATL::CStringW> &arrAppsNames)
+{
+ ATL::CSimpleArray<PAPPLICATION_INFO> result;
+ for (INT i = 0; i < arrAppsNames.GetSize(); ++i)
+ {
+ PAPPLICATION_INFO Info = FindInfo(arrAppsNames[i]);
+ if (Info)
+ {
+ result.Add(Info);
+ }
+ }
+ return result;
+}
+
+const ATL::CStringW& CAvailableApps::GetFolderPath()
+{
+ return m_szPath;
+}
+
+const ATL::CStringW& CAvailableApps::GetAppPath()
+{
+ return m_szAppsPath;
+}
+
+const ATL::CStringW& CAvailableApps::GetCabPath()
+{
+ return m_szCabPath;
+}
+
+const LPCWSTR CAvailableApps::GetFolderPathString()
+{
+ return m_szPath.GetString();
+}
+
+const LPCWSTR CAvailableApps::GetAppPathString()
+{
+ return m_szPath.GetString();
+}
+
+const LPCWSTR CAvailableApps::GetCabPathString()
+{
+ return m_szPath.GetString();
+}
+// CAvailableApps