ReactOS 0.4.16-dev-338-g34e76ad
appdb.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4 * PURPOSE: Classes for working with available applications
5 * COPYRIGHT: Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
6 * Copyright 2020 He Yang (1160386205@qq.com)
7 * Copyright 2021-2023 Mark Jansen <mark.jansen@reactos.org>
8 */
9
10#include "rapps.h"
11#include "appdb.h"
12#include "configparser.h"
13#include "settings.h"
14
15
18#define UNINSTALL_SUBKEY L"Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
19
20static VOID
22{
23 POSITION InfoListPosition = list.GetHeadPosition();
24 while (InfoListPosition)
25 {
26 CAppInfo *Info = list.GetNext(InfoListPosition);
27 delete Info;
28 }
29 list.RemoveAll();
30}
31
32CAppDB::CAppDB(const CStringW &path) : m_BasePath(path)
33{
35}
36
39{
40 POSITION CurrentListPosition = m_Available.GetHeadPosition();
41 while (CurrentListPosition)
42 {
43 CAppInfo *Info = m_Available.GetNext(CurrentListPosition);
44 if (Info->szIdentifier == name)
45 {
46 return static_cast<CAvailableApplicationInfo *>(Info);
47 }
48 }
49 return NULL;
50}
51
52void
54{
55 const BOOL UseInstalled = IsInstalledEnum(Type);
56 const CAtlList<CAppInfo *> &list = UseInstalled ? m_Installed : m_Available;
57 const BOOL IncludeAll = UseInstalled ? (Type == ENUM_ALL_INSTALLED) : (Type == ENUM_ALL_AVAILABLE);
58
59 POSITION CurrentListPosition = list.GetHeadPosition();
60 while (CurrentListPosition)
61 {
62 CAppInfo *Info = list.GetNext(CurrentListPosition);
63
64 if (IncludeAll || Type == Info->iCategory)
65 {
66 List.AddTail(Info);
67 }
68 }
69}
70
71BOOL
73{
75
76 CPathW AppsPath = m_BasePath;
77 AppsPath += RAPPS_DATABASE_SUBDIR;
78 CPathW WildcardPath = AppsPath;
79 WildcardPath += L"*.txt";
80
81 WIN32_FIND_DATAW FindFileData;
82 HANDLE hFind = FindFirstFileW(WildcardPath, &FindFileData);
83 if (hFind == INVALID_HANDLE_VALUE)
84 {
85 return FALSE;
86 }
87
88 do
89 {
90 CStringW szPkgName = FindFileData.cFileName;
92 szPkgName.ReleaseBuffer();
93
94 CAppInfo *Info = FindByPackageName(szPkgName);
96 if (!Info)
97 {
98 CConfigParser *Parser = new CConfigParser(CPathW(AppsPath) += FindFileData.cFileName);
99 int Cat;
100 if (!Parser->GetInt(DB_CATEGORY, Cat))
101 Cat = ENUM_INVALID;
102
103 Info = new CAvailableApplicationInfo(Parser, szPkgName, static_cast<AppsCategories>(Cat), AppsPath);
104 if (Info->Valid())
105 {
107 }
108 else
109 {
110 delete Info;
111 }
112 }
113
114 } while (FindNextFileW(hFind, &FindFileData));
115
116 FindClose(hFind);
117 return TRUE;
118}
119
120VOID
122{
124 return;
125
126 if (EnumerateFiles())
127 return;
128
131
132 CPathW AppsPath = m_BasePath;
133 AppsPath += RAPPS_DATABASE_SUBDIR;
135 return;
136
137 CPathW CabFile = m_BasePath;
138 CabFile += APPLICATION_DATABASE_NAME;
139 DeleteFileW(CabFile);
140
142}
143
144static inline HKEY
146{
149 {
150 RegSam = g_RegSamEnum[Index];
151 return g_RootKeyEnum[Index];
152 }
153 return NULL;
154}
155
156HKEY
158{
159 // Loop for through all combinations.
160 // Note that HKEY_CURRENT_USER\Software does not have a redirect
161 // https://docs.microsoft.com/en-us/windows/win32/winprog64/shared-registry-keys#redirected-shared-and-reflected-keys-under-wow64
162 if (Index < (IsSystem64Bit() ? 3 : 2))
163 return GetRootKeyInfo(Index, RegSam);
164 else
165 return NULL;
166}
167
170{
171 CRegKey hSubKey;
172 if (hSubKey.Open(hKeyParent, KeyName, KEY_READ) != ERROR_SUCCESS)
173 return NULL;
175
176 size = sizeof(DWORD);
177 if (!RegQueryValueExW(hSubKey, L"SystemComponent", NULL, NULL, (LPBYTE)&value, &size) && value == 1)
178 {
179 // Ignore system components
180 return NULL;
181 }
182
183 size = 0;
184 BOOL bIsUpdate = !RegQueryValueExW(hSubKey, L"ParentKeyName", NULL, NULL, NULL, &size);
185
188 pInfo = new CInstalledApplicationInfo(hSubKey.Detach(), KeyName, cat, KeyIndex);
189 if (pInfo && pInfo->Valid())
190 {
191 return pInfo;
192 }
193 delete pInfo;
194 return NULL;
195}
196
199{
200 ATLASSERT(List || SearchOnly);
201 REGSAM wowsam;
202 HKEY hRootKey;
203 for (UINT rki = 0; (hRootKey = EnumInstalledRootKey(rki, wowsam)); ++rki)
204 {
206 if (hKey.Open(hRootKey, UNINSTALL_SUBKEY, KEY_READ | wowsam) != ERROR_SUCCESS)
207 {
208 continue;
209 }
210 for (DWORD Index = 0;; ++Index)
211 {
212 WCHAR szKeyName[MAX_PATH];
213 DWORD dwSize = _countof(szKeyName);
214 if (hKey.EnumKey(Index, szKeyName, &dwSize) != ERROR_SUCCESS)
215 {
216 break;
217 }
218 if (List || !StrCmpIW(SearchOnly, szKeyName))
219 {
221 Info = CreateInstalledAppByRegistryKey(szKeyName, hKey, rki);
222 if (Info)
223 {
224 if (List)
225 List->AddTail(Info);
226 else
227 return Info;
228 }
229 }
230 }
231 }
232 return NULL;
233}
234
235VOID
237{
238 // Remove all old entries
240
242}
243
246{
247 return EnumerateRegistry(NULL, Name);
248}
249
252{
253 HKEY hRootKey = User ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
254 UINT KeyIndex = User ? (0) : ((WowSam & KEY_WOW64_64KEY) ? 2 : 1);
256 if (hKey.Open(hRootKey, UNINSTALL_SUBKEY, KEY_READ | WowSam) == ERROR_SUCCESS)
257 {
259 }
260 return NULL;
261}
262
263static void
265{
267 WIN32_FIND_DATAW FindFileData;
268
269 CPathW DirWithFilter = Dir;
270 DirWithFilter += Filter;
271
272 hFind = FindFirstFileW(DirWithFilter, &FindFileData);
273
274 if (hFind == INVALID_HANDLE_VALUE)
275 return;
276
277 do
278 {
279 CPathW szTmp = Dir;
280 szTmp += FindFileData.cFileName;
281
282 if (!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
283 {
285 }
286 } while (FindNextFileW(hFind, &FindFileData) != 0);
287 FindClose(hFind);
288}
289
290VOID
292{
293 // Delete icons
294 CPathW AppsPath = m_BasePath;
295 AppsPath += RAPPS_DATABASE_SUBDIR;
296 CPathW IconPath = AppsPath;
297 IconPath += L"icons";
299
300 // Delete leftover screenshots
301 CPathW ScrnshotFolder = AppsPath;
302 ScrnshotFolder += L"screenshots";
303 DeleteWithWildcard(ScrnshotFolder, L"*.tmp");
304
305 // Delete data base files (*.txt)
306 DeleteWithWildcard(AppsPath, L"*.txt");
307
309 RemoveDirectoryW(ScrnshotFolder);
310 RemoveDirectoryW(AppsPath);
312}
313
314DWORD
316{
317 // Validate that this is actually an installed app / update
318 ATLASSERT(Info->iCategory == ENUM_INSTALLED_APPLICATIONS || Info->iCategory == ENUM_UPDATES);
319 if (Info->iCategory != ENUM_INSTALLED_APPLICATIONS && Info->iCategory != ENUM_UPDATES)
321
322 const CInstalledApplicationInfo *InstalledInfo = static_cast<const CInstalledApplicationInfo *>(Info);
323
324 CStringW Name = InstalledInfo->szIdentifier;
325 REGSAM wowsam;
326 HKEY hRoot = GetRootKeyInfo(InstalledInfo->m_KeyInfo, wowsam);
328 if (!hRoot)
329 return ERROR_OPEN_FAILED;
330
331 CRegKey Uninstall;
332 LSTATUS err = Uninstall.Open(hRoot, UNINSTALL_SUBKEY, KEY_READ | KEY_WRITE | wowsam);
333 if (err == ERROR_SUCCESS)
334 {
335 err = Uninstall.RecurseDeleteKey(Name);
336 }
337 return err;
338}
#define ATLASSERT(x)
Definition: CComVariant.cpp:10
char szTmp[518]
Type
Definition: Type.h:7
static VOID ClearList(CAtlList< CAppInfo * > &list)
Definition: appdb.cpp:21
#define UNINSTALL_SUBKEY
Definition: appdb.cpp:18
static void DeleteWithWildcard(const CPathW &Dir, const CStringW &Filter)
Definition: appdb.cpp:264
static HKEY g_RootKeyEnum[3]
Definition: appdb.cpp:16
static REGSAM g_RegSamEnum[3]
Definition: appdb.cpp:17
static HKEY GetRootKeyInfo(UINT Index, REGSAM &RegSam)
Definition: appdb.cpp:145
BOOL IsInstalledEnum(INT x)
Definition: appinfo.h:61
#define DB_CATEGORY
Definition: appinfo.h:80
AppsCategories
Definition: appinfo.h:25
@ ENUM_UPDATES
Definition: appinfo.h:46
@ ENUM_ALL_AVAILABLE
Definition: appinfo.h:26
@ ENUM_INSTALLED_APPLICATIONS
Definition: appinfo.h:45
@ ENUM_INVALID
Definition: appinfo.h:47
@ ENUM_ALL_INSTALLED
Definition: appinfo.h:44
#define RAPPS_DATABASE_SUBDIR
Definition: defines.h:37
#define APPLICATION_DATABASE_URL
Definition: defines.h:40
#define APPLICATION_DATABASE_NAME
Definition: defines.h:41
BOOL IsSystem64Bit()
Definition: misc.cpp:315
SETTINGS_INFO SettingsInfo
Definition: winmain.cpp:21
BOOL ExtractFilesFromCab(const CStringW &szCabName, const CStringW &szCabDir, const CStringW &szOutputDir, EXTRACTCALLBACK Callback, void *Cookie)
Definition: cabinet.cpp:250
POSITION AddTail(INARGTYPE element)
Definition: atlcoll.h:629
POSITION GetHeadPosition() const
Definition: atlcoll.h:554
E & GetNext(_Inout_ POSITION &pos)
Definition: atlcoll.h:566
void Canonicalize()
Definition: atlpath.h:149
LONG Open(HKEY hKeyParent, LPCTSTR lpszKeyName, REGSAM samDesired=KEY_READ|KEY_WRITE) noexcept
Definition: atlbase.h:1173
HKEY Detach() noexcept
Definition: atlbase.h:1166
LONG RecurseDeleteKey(LPCTSTR lpszKey) noexcept
Definition: atlbase.h:1414
void ReleaseBuffer(_In_ int nNewLength=-1)
Definition: atlsimpstr.h:387
static DWORD RemoveInstalledAppFromRegistry(const CAppInfo *Info)
Definition: appdb.cpp:315
VOID UpdateAvailable()
Definition: appdb.cpp:121
static CInstalledApplicationInfo * CreateInstalledAppByRegistryKey(LPCWSTR KeyName, HKEY hKeyParent, UINT KeyIndex)
Definition: appdb.cpp:169
CAvailableApplicationInfo * FindAvailableByPackageName(const CStringW &name)
Definition: appdb.cpp:38
CAppInfo * FindByPackageName(const CStringW &name)
Definition: appdb.h:31
CAtlList< CAppInfo * > m_Available
Definition: appdb.h:12
CAppDB(const CStringW &path)
Definition: appdb.cpp:32
BOOL EnumerateFiles()
Definition: appdb.cpp:72
CPathW m_BasePath
Definition: appdb.h:11
static CInstalledApplicationInfo * EnumerateRegistry(CAtlList< CAppInfo * > *List, LPCWSTR Name)
Definition: appdb.cpp:198
VOID UpdateInstalled()
Definition: appdb.cpp:236
VOID GetApps(CAtlList< CAppInfo * > &List, AppsCategories Type) const
Definition: appdb.cpp:53
static HKEY EnumInstalledRootKey(UINT Index, REGSAM &RegSam)
Definition: appdb.cpp:157
CAtlList< CAppInfo * > m_Installed
Definition: appdb.h:13
VOID RemoveCached()
Definition: appdb.cpp:291
static CInstalledApplicationInfo * CreateInstalledAppInstance(LPCWSTR KeyName, BOOL User, REGSAM WowSam)
Definition: appdb.cpp:251
const CStringW szIdentifier
Definition: appinfo.h:98
BOOL GetInt(const CStringW &KeyName, INT &iResult)
virtual BOOL Valid() const override
Definition: appinfo.cpp:536
Definition: list.h:37
#define ERROR_SUCCESS
Definition: deptool.c:10
static LSTATUS(WINAPI *pRegDeleteTreeW)(HKEY
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4103
#define ERROR_INVALID_PARAMETER
Definition: compat.h:101
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define MAX_PATH
Definition: compat.h:34
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39
BOOL WINAPI CreateDirectoryW(IN LPCWSTR lpPathName, IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
Definition: dir.c:90
BOOL WINAPI RemoveDirectoryW(IN LPCWSTR lpPathName)
Definition: dir.c:732
HANDLE WINAPI FindFirstFileW(IN LPCWSTR lpFileName, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:320
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
BOOL WINAPI FindNextFileW(IN HANDLE hFindFile, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:382
static const WCHAR IconPath[]
Definition: install.c:51
void WINAPI PathRemoveExtensionW(LPWSTR lpszPath)
Definition: path.c:823
int WINAPI StrCmpIW(LPCWSTR lpszStr, LPCWSTR lpszComp)
Definition: string.c:353
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
_Must_inspect_result_ _In_opt_ PFLT_FILTER Filter
Definition: fltkernel.h:1801
FxAutoRegKey hKey
GLsizeiptr size
Definition: glext.h:5919
#define C_ASSERT(e)
Definition: intsafe.h:73
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
static HTREEITEM hRoot
Definition: treeview.c:383
CPathT< CStringW > CPathW
Definition: atlpath.h:395
unsigned int UINT
Definition: ndis.h:50
#define KEY_READ
Definition: nt_native.h:1023
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
#define KEY_WRITE
Definition: nt_native.h:1031
#define DWORD
Definition: nt_native.h:44
#define L(x)
Definition: ntvdm.h:50
VOID DownloadApplicationsDB(LPCWSTR lpUrl, BOOL IsOfficial)
Definition: loaddlg.cpp:1145
#define err(...)
#define _countof(array)
Definition: sndvol32.h:70
BOOL bUseSource
Definition: settings.h:23
WCHAR szSourceURL[INTERNET_MAX_URL_LENGTH]
Definition: settings.h:24
Definition: name.c:39
unsigned char * LPBYTE
Definition: typedefs.h:53
Definition: pdh_main.c:94
_Must_inspect_result_ _In_ WDFCHILDLIST _In_ PWDF_CHILD_LIST_ITERATOR _Out_ WDFDEVICE _Inout_opt_ PWDF_CHILD_RETRIEVE_INFO Info
Definition: wdfchildlist.h:690
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PCUNICODE_STRING KeyName
Definition: wdfdevice.h:2699
_Must_inspect_result_ _In_ WDFCMRESLIST List
Definition: wdfresource.h:550
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define ERROR_OPEN_FAILED
Definition: winerror.h:184
#define HKEY_LOCAL_MACHINE
Definition: winreg.h:12
#define HKEY_CURRENT_USER
Definition: winreg.h:11
ACCESS_MASK REGSAM
Definition: winreg.h:69
#define KEY_WOW64_32KEY
Definition: cmtypes.h:45
#define KEY_WOW64_64KEY
Definition: cmtypes.h:46
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185