[RAPPS] listview refactor (#2970)

This makes it easier to maintain the listview, and better separates the application list and listview.

* [RAPPS] fix memory leak when cleanup. some renaming are also done
* [RAPPS] move the code adding apps info inside class CAppsListView
* [RAPPS] add table view, create listview and AppInfoDisplay inside it
* [RAPPS] rename INSTALLED_INFO as CInstalledApplicationInfo
now it corresponds with CAvailableApplicationInfo
* [RAPPS] add CInstalledApps
* [RAPPS] optimize the speed when refreshing listview
* [RAPPS] correctly handle Enum for InstalledApps
* [RAPPS] make check all working properly (this also fixes some bugs)
the old version has some bugs when check all items after switching tags in tree-view
* [RAPPS] add handling for wow64
* [RAPPS] use an inline function to replace INSERT_TEXT macro
* [RAPPS] fix the bug that StatusBar won't update when switching tags
* [RAPPS] now TableView always reset bIsAscending in SetDisplayMode
* [RAPPS] rename TableView to ApplicationView
* [RAPPS] now bIsAscending would be reset when switching column in listview
This commit is contained in:
He Yang 2020-07-21 22:13:39 +08:00 committed by Mark Jansen
parent 4482d0f455
commit 10c0ff7416
No known key found for this signature in database
GPG key ID: B39240EE84BEAE8B
8 changed files with 1147 additions and 653 deletions

View file

@ -12,149 +12,253 @@
#include "misc.h"
BOOL INSTALLED_INFO::GetApplicationString(LPCWSTR lpKeyName, ATL::CStringW& String)
CInstalledApplicationInfo::CInstalledApplicationInfo(BOOL bIsUserKey, REGSAM RegWowKey, HKEY hKey)
: IsUserKey(bIsUserKey), WowKey(RegWowKey), hSubKey(hKey)
{
BOOL result = ::GetApplicationString(hSubKey, lpKeyName, String.GetBuffer(MAX_PATH));
String.ReleaseBuffer();
return result;
}
// if Initialize failed, hSubKey will be closed automatically and set to zero
BOOL GetApplicationString(HKEY hKey, LPCWSTR lpKeyName, LPWSTR szString)
{
DWORD dwSize = MAX_PATH * sizeof(WCHAR);
DWORD dwSize = MAX_PATH, dwType, dwValue;
BOOL bIsSystemComponent;
ATL::CStringW szParentKeyName;
if (RegQueryValueExW(hKey,
lpKeyName,
NULL,
NULL,
(LPBYTE) szString,
&dwSize) == ERROR_SUCCESS)
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
if (RegQueryValueExW(hSubKey,
L"SystemComponent",
NULL,
&dwType,
(LPBYTE)&dwValue,
&dwSize) == ERROR_SUCCESS)
{
return TRUE;
bIsSystemComponent = (dwValue == 0x1);
}
else
{
bIsSystemComponent = FALSE;
}
StringCchCopyW(szString, MAX_PATH, L"---");
return FALSE;
}
BOOL UninstallApplication(PINSTALLED_INFO ItemInfo, BOOL bModify)
{
LPCWSTR szModify = L"ModifyPath";
LPCWSTR szUninstall = L"UninstallString";
DWORD dwType, dwSize;
WCHAR szPath[MAX_PATH];
dwType = REG_SZ;
dwSize = MAX_PATH * sizeof(WCHAR);
if (RegQueryValueExW(ItemInfo->hSubKey,
bModify ? szModify : szUninstall,
NULL,
&dwType,
(LPBYTE) szPath,
&dwSize) != ERROR_SUCCESS)
bIsUpdate = (RegQueryValueExW(hSubKey,
L"ParentKeyName",
NULL,
&dwType,
(LPBYTE)szParentKeyName.GetBuffer(MAX_PATH),
&dwSize) == ERROR_SUCCESS);
szParentKeyName.ReleaseBuffer();
if (bIsSystemComponent)
{
return FALSE;
CloseHandle(hSubKey);
hSubKey = NULL;
}
return StartProcess(szPath, TRUE);
}
BOOL EnumInstalledApplications(INT EnumType, BOOL IsUserKey, APPENUMPROC lpEnumProc, PVOID param)
CInstalledApplicationInfo::~CInstalledApplicationInfo()
{
DWORD dwSize = MAX_PATH, dwType, dwValue;
BOOL bIsSystemComponent, bIsUpdate;
ATL::CStringW szParentKeyName;
ATL::CStringW szDisplayName;
INSTALLED_INFO Info;
HKEY hKey;
LONG ItemIndex = 0;
if (hSubKey)
{
CloseHandle(hSubKey);
hSubKey = NULL;
}
}
Info.hRootKey = IsUserKey ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
BOOL CInstalledApplicationInfo::GetApplicationString(LPCWSTR lpKeyName, ATL::CStringW& String)
{
DWORD dwSize = 0;
String.Empty();
DWORD dwType;
if (RegOpenKeyW(Info.hRootKey,
L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
&hKey) != ERROR_SUCCESS)
// retrieve the size of value first.
if (RegQueryValueExW(hSubKey,
lpKeyName,
NULL,
&dwType,
NULL,
&dwSize) != ERROR_SUCCESS)
{
return FALSE;
}
while (RegEnumKeyExW(hKey, ItemIndex, Info.szKeyName.GetBuffer(MAX_PATH), &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
// TODO: I assume the type as REG_SZ. but I think REG_EXPAND_SZ should be handled correctly too.
if (dwType != REG_SZ)
{
Info.szKeyName.ReleaseBuffer();
if (RegOpenKeyW(hKey, Info.szKeyName.GetString(), &Info.hSubKey) == ERROR_SUCCESS)
return FALSE;
}
// allocate buffer.
// attention: dwSize is size in bytes, and RegQueryValueExW does not guarantee the terminating null character.
String.GetBuffer(dwSize + sizeof(WCHAR));
// query the value
if (RegQueryValueExW(hSubKey,
lpKeyName,
NULL,
NULL,
(LPBYTE)String.GetBuffer(),
&dwSize) != ERROR_SUCCESS)
{
String.ReleaseBuffer();
String.Empty();
return FALSE;
}
String.GetBuffer()[dwSize / sizeof(WCHAR)] = L'\0'; // ensure zero terminated
String.ReleaseBuffer();
return TRUE;
}
BOOL CInstalledApplicationInfo::UninstallApplication(BOOL bModify)
{
return StartProcess(bModify ? szModifyPath : szUninstallString, TRUE);
}
LSTATUS CInstalledApplicationInfo::RemoveFromRegistry()
{
ATL::CStringW szFullName = L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + szKeyName;
// TODO: if there are subkeys inside, simply RegDeleteKeyExW will fail
// we don't have RegDeleteTree for ReactOS now. (It's a WinVista API)
// write a function to delete all subkeys recursively to solve this
// or consider letting ReactOS having this API
return RegDeleteKeyExW(IsUserKey ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE, szFullName, WowKey, 0);
}
BOOL CInstalledApps::Enum(INT EnumType, APPENUMPROC lpEnumProc, PVOID param)
{
FreeCachedEntries();
HKEY RootKeyEnum[3] = { HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_LOCAL_MACHINE };
REGSAM RegSamEnum[3] = { KEY_WOW64_32KEY, KEY_WOW64_32KEY, KEY_WOW64_64KEY };
int LoopTime;
// test if the OS is 64 bit.
if (IsSystem64Bit())
{
// loop for all 3 combination.
// note that HKEY_CURRENT_USER\Software don't have a redirect
// https://docs.microsoft.com/en-us/windows/win32/winprog64/shared-registry-keys#redirected-shared-and-reflected-keys-under-wow64
LoopTime = 3;
}
else
{
// loop for 2 combination for KEY_WOW64_32KEY only
LoopTime = 2;
}
// loop for all combination
for (int i = 0; i < LoopTime; i++)
{
DWORD dwSize = MAX_PATH;
HKEY hKey, hSubKey;
LONG ItemIndex = 0;
ATL::CStringW szKeyName;
if (RegOpenKeyExW(RootKeyEnum[i],
L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
NULL,
KEY_READ | RegSamEnum[i],
&hKey) != ERROR_SUCCESS)
{
dwType = REG_DWORD;
dwSize = sizeof(DWORD);
return FALSE;
}
if (RegQueryValueExW(Info.hSubKey,
L"SystemComponent",
NULL,
&dwType,
(LPBYTE) &dwValue,
&dwSize) == ERROR_SUCCESS)
while (1)
{
dwSize = MAX_PATH;
if (RegEnumKeyExW(hKey, ItemIndex, szKeyName.GetBuffer(MAX_PATH), &dwSize, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
{
bIsSystemComponent = (dwValue == 0x1);
}
else
{
bIsSystemComponent = FALSE;
break;
}
dwType = REG_SZ;
dwSize = MAX_PATH * sizeof(WCHAR);
bIsUpdate = (RegQueryValueExW(Info.hSubKey,
L"ParentKeyName",
NULL,
&dwType,
(LPBYTE) szParentKeyName.GetBuffer(MAX_PATH),
&dwSize) == ERROR_SUCCESS);
szParentKeyName.ReleaseBuffer();
ItemIndex++;
dwType = REG_SZ;
dwSize = MAX_PATH * sizeof(WCHAR);
if (RegQueryValueExW(Info.hSubKey,
L"DisplayName",
NULL,
&dwType,
(LPBYTE) szDisplayName.GetBuffer(MAX_PATH),
&dwSize) == ERROR_SUCCESS)
szKeyName.ReleaseBuffer();
if (RegOpenKeyW(hKey, szKeyName.GetString(), &hSubKey) == ERROR_SUCCESS)
{
szDisplayName.ReleaseBuffer();
if (EnumType < ENUM_ALL_INSTALLED || EnumType > ENUM_UPDATES)
EnumType = ENUM_ALL_INSTALLED;
BOOL bSuccess = FALSE;
CInstalledApplicationInfo *Info = new CInstalledApplicationInfo(RootKeyEnum[i] == HKEY_CURRENT_USER, RegSamEnum[i], hSubKey);
Info->szKeyName = szKeyName;
if (!bIsSystemComponent)
// check for failure. if failed to init, Info->hSubKey will be set to NULL
if (Info->hSubKey)
{
if ((EnumType == ENUM_ALL_INSTALLED) || /* All components */
((EnumType == ENUM_INSTALLED_APPLICATIONS) && (!bIsUpdate)) || /* Applications only */
((EnumType == ENUM_UPDATES) && (bIsUpdate))) /* Updates only */
// those items without display name are ignored
if (Info->GetApplicationString(L"DisplayName", Info->szDisplayName))
{
if (!lpEnumProc(ItemIndex, szDisplayName, &Info, param))
break;
Info->GetApplicationString(L"DisplayVersion", Info->szDisplayVersion);
Info->GetApplicationString(L"Publisher", Info->szPublisher);
Info->GetApplicationString(L"RegOwner", Info->szRegOwner);
Info->GetApplicationString(L"ProductID", Info->szProductID);
Info->GetApplicationString(L"HelpLink", Info->szHelpLink);
Info->GetApplicationString(L"HelpTelephone", Info->szHelpTelephone);
Info->GetApplicationString(L"Readme", Info->szReadme);
Info->GetApplicationString(L"Contact", Info->szContact);
Info->GetApplicationString(L"URLUpdateInfo", Info->szURLUpdateInfo);
Info->GetApplicationString(L"URLInfoAbout", Info->szURLInfoAbout);
Info->GetApplicationString(L"Comments", Info->szComments);
Info->GetApplicationString(L"InstallDate", Info->szInstallDate);
Info->GetApplicationString(L"InstallLocation", Info->szInstallLocation);
Info->GetApplicationString(L"InstallSource", Info->szInstallSource);
Info->GetApplicationString(L"UninstallString", Info->szUninstallString);
Info->GetApplicationString(L"ModifyPath", Info->szModifyPath);
bSuccess = TRUE;
}
else
}
// close handle
if (Info->hSubKey)
{
CloseHandle(Info->hSubKey);
Info->hSubKey = NULL;
}
if (bSuccess)
{
// add to InfoList.
m_InfoList.AddTail(Info);
// invoke callback
if (lpEnumProc)
{
RegCloseKey(Info.hSubKey);
if ((EnumType == ENUM_ALL_INSTALLED) || /* All components */
((EnumType == ENUM_INSTALLED_APPLICATIONS) && (!Info->bIsUpdate)) || /* Applications only */
((EnumType == ENUM_UPDATES) && (Info->bIsUpdate))) /* Updates only */
{
lpEnumProc(Info, param);
}
}
}
else
{
RegCloseKey(Info.hSubKey);
// destory object
delete Info;
}
}
else
{
szDisplayName.ReleaseBuffer();
RegCloseKey(Info.hSubKey);
}
}
dwSize = MAX_PATH;
ItemIndex++;
szKeyName.ReleaseBuffer();
RegCloseKey(hKey);
}
Info.szKeyName.ReleaseBuffer();
RegCloseKey(hKey);
return TRUE;
}
VOID CInstalledApps::FreeCachedEntries()
{
POSITION InfoListPosition = m_InfoList.GetHeadPosition();
/* loop and deallocate all the cached app infos in the list */
while (InfoListPosition)
{
CInstalledApplicationInfo *Info = m_InfoList.GetNext(InfoListPosition);
delete Info;
}
m_InfoList.RemoveAll();
}