2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/available.cpp
5 * PURPOSE: Classes for working with available applications
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 * Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
8 * Alexander Shaposhnikov (chaez.san@gmail.com)
12 #include "available.h"
17 #include <atlsimpcoll.h>
20 // CAvailableApplicationInfo
21 CAvailableApplicationInfo::CAvailableApplicationInfo(const ATL::CStringW
& sFileNameParam
)
22 : m_Parser(sFileNameParam
)
24 LicenseType
= LICENSE_TYPE::None
;
25 sFileName
= sFileNameParam
;
27 RetrieveGeneralInfo();
30 VOID
CAvailableApplicationInfo::RefreshAppInfo()
32 if (szUrlDownload
.IsEmpty())
34 RetrieveGeneralInfo();
38 // Lazily load general info from the file
39 VOID
CAvailableApplicationInfo::RetrieveGeneralInfo()
41 Category
= m_Parser
.GetInt(L
"Category");
43 if (!GetString(L
"Name", szName
)
44 || !GetString(L
"URLDownload", szUrlDownload
))
49 GetString(L
"RegName", szRegName
);
50 GetString(L
"Version", szVersion
);
51 GetString(L
"License", szLicense
);
52 GetString(L
"Description", szDesc
);
53 GetString(L
"Size", szSize
);
54 GetString(L
"URLSite", szUrlSite
);
55 GetString(L
"CDPath", szCDPath
);
56 GetString(L
"Language", szRegName
);
57 GetString(L
"SHA1", szSHA1
);
59 RetrieveLicenseType();
61 RetrieveInstalledStatus();
64 RetrieveInstalledVersion();
68 VOID
CAvailableApplicationInfo::RetrieveInstalledStatus()
70 m_IsInstalled
= ::GetInstalledVersion(NULL
, szRegName
)
71 || ::GetInstalledVersion(NULL
, szName
);
74 VOID
CAvailableApplicationInfo::RetrieveInstalledVersion()
76 m_HasInstalledVersion
= ::GetInstalledVersion(&szInstalledVersion
, szRegName
)
77 || ::GetInstalledVersion(&szInstalledVersion
, szName
);
80 VOID
CAvailableApplicationInfo::RetrieveLanguages()
82 const WCHAR cDelimiter
= L
'|';
83 ATL::CStringW szBuffer
;
85 // TODO: Get multiline parameter
86 if (!m_Parser
.GetString(L
"Languages", szBuffer
))
88 m_HasLanguageInfo
= FALSE
;
92 // Parse parameter string
93 ATL::CStringW m_szLocale
;
95 for (INT i
= 0; szBuffer
[i
] != UNICODE_NULL
; ++i
)
97 if (szBuffer
[i
] != cDelimiter
&& szBuffer
[i
] != L
'\n')
99 m_szLocale
+= szBuffer
[i
];
103 if (StrToIntExW(m_szLocale
.GetString(), STIF_DEFAULT
, &iLCID
))
105 Languages
.Add(static_cast<LCID
>(iLCID
));
111 // For the text after delimiter
112 if (!m_szLocale
.IsEmpty())
114 if (StrToIntExW(m_szLocale
.GetString(), STIF_DEFAULT
, &iLCID
))
116 Languages
.Add(static_cast<LCID
>(iLCID
));
120 m_HasLanguageInfo
= TRUE
;
123 VOID
CAvailableApplicationInfo::RetrieveLicenseType()
125 INT IntBuffer
= m_Parser
.GetInt(L
"LicenseType");
127 if (IntBuffer
< 0 || IntBuffer
> LICENSE_TYPE::Max
)
129 LicenseType
= LICENSE_TYPE::None
;
133 LicenseType
= (LICENSE_TYPE
) IntBuffer
;
137 BOOL
CAvailableApplicationInfo::FindInLanguages(LCID what
) const
139 if (!m_HasLanguageInfo
)
144 //Find locale code in the list
145 const INT nLanguagesSize
= Languages
.GetSize();
146 for (INT i
= 0; i
< nLanguagesSize
; ++i
)
148 if (Languages
[i
] == what
)
157 BOOL
CAvailableApplicationInfo::HasLanguageInfo() const
159 return m_HasLanguageInfo
;
162 BOOL
CAvailableApplicationInfo::HasNativeLanguage() const
164 return FindInLanguages(GetUserDefaultLCID());
167 BOOL
CAvailableApplicationInfo::HasEnglishLanguage() const
169 return FindInLanguages(MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
), SORT_DEFAULT
));
172 BOOL
CAvailableApplicationInfo::IsInstalled() const
174 return m_IsInstalled
;
177 BOOL
CAvailableApplicationInfo::HasInstalledVersion() const
179 return m_HasInstalledVersion
;
182 BOOL
CAvailableApplicationInfo::HasUpdate() const
184 return (szInstalledVersion
.Compare(szVersion
) < 0) ? TRUE
: FALSE
;
187 VOID
CAvailableApplicationInfo::SetLastWriteTime(FILETIME
* ftTime
)
189 RtlCopyMemory(&ftCacheStamp
, ftTime
, sizeof(FILETIME
));
192 inline BOOL
CAvailableApplicationInfo::GetString(LPCWSTR lpKeyName
, ATL::CStringW
& ReturnedString
)
194 if (!m_Parser
.GetString(lpKeyName
, ReturnedString
))
196 ReturnedString
.Empty();
201 // CAvailableApplicationInfo
204 CAvailableApps::CAvailableApps()
207 if (GetStorageDirectory(m_szPath
))
209 m_szAppsPath
= m_szPath
+ L
"\\rapps\\";
210 m_szCabPath
= m_szPath
+ L
"\\rappmgr.cab";
211 m_szSearchPath
= m_szAppsPath
+ L
"*.txt";
215 VOID
CAvailableApps::FreeCachedEntries()
217 POSITION InfoListPosition
= m_InfoList
.GetHeadPosition();
219 /* loop and deallocate all the cached app infos in the list */
220 while (InfoListPosition
)
222 CAvailableApplicationInfo
* Info
= m_InfoList
.GetAt(InfoListPosition
);
223 m_InfoList
.RemoveHead();
226 InfoListPosition
= m_InfoList
.GetHeadPosition();
230 BOOL
CAvailableApps::DeleteCurrentAppsDB()
232 HANDLE hFind
= INVALID_HANDLE_VALUE
;
233 WIN32_FIND_DATAW FindFileData
;
236 if (m_szPath
.IsEmpty())
239 result
= result
&& DeleteFileW(m_szCabPath
.GetString());
240 hFind
= FindFirstFileW(m_szSearchPath
.GetString(), &FindFileData
);
242 if (hFind
== INVALID_HANDLE_VALUE
)
248 szTmp
= m_szPath
+ FindFileData
.cFileName
;
249 result
= result
&& DeleteFileW(szTmp
.GetString());
250 } while (FindNextFileW(hFind
, &FindFileData
) != 0);
257 BOOL
CAvailableApps::UpdateAppsDB()
259 if (!DeleteCurrentAppsDB())
262 CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL
);
264 if (m_szPath
.IsEmpty())
267 if (!ExtractFilesFromCab(m_szCabPath
, m_szAppsPath
))
275 BOOL
CAvailableApps::EnumAvailableApplications(INT EnumType
, AVAILENUMPROC lpEnumProc
)
277 HANDLE hFind
= INVALID_HANDLE_VALUE
;
278 WIN32_FIND_DATAW FindFileData
;
280 if (!CreateDirectoryW(m_szPath
.GetString(), NULL
) &&
281 GetLastError() != ERROR_ALREADY_EXISTS
)
286 hFind
= FindFirstFileW(m_szSearchPath
.GetString(), &FindFileData
);
288 if (hFind
== INVALID_HANDLE_VALUE
)
290 if (GetFileAttributesW(m_szCabPath
) == INVALID_FILE_ATTRIBUTES
)
292 CDownloadManager::DownloadApplicationsDB(APPLICATION_DATABASE_URL
);
296 ExtractFilesFromCab(m_szCabPath
, m_szAppsPath
);
297 hFind
= FindFirstFileW(m_szSearchPath
.GetString(), &FindFileData
);
299 if (hFind
== INVALID_HANDLE_VALUE
)
305 // loop for all the cached entries
306 POSITION CurrentListPosition
= m_InfoList
.GetHeadPosition();
307 CAvailableApplicationInfo
* Info
= NULL
;
309 while (CurrentListPosition
!= NULL
)
311 POSITION LastListPosition
= CurrentListPosition
;
312 Info
= m_InfoList
.GetNext(CurrentListPosition
);
314 // do we already have this entry in cache?
315 if (Info
->sFileName
== FindFileData
.cFileName
)
317 // is it current enough, or the file has been modified since our last time here?
318 if (CompareFileTime(&FindFileData
.ftLastWriteTime
, &Info
->ftCacheStamp
) == 1)
320 // recreate our cache, this is the slow path
321 m_InfoList
.RemoveAt(LastListPosition
);
329 // speedy path, compare directly, we already have the data
335 // create a new entry
336 Info
= new CAvailableApplicationInfo(FindFileData
.cFileName
);
338 // set a timestamp for the next time
339 Info
->SetLastWriteTime(&FindFileData
.ftLastWriteTime
);
340 m_InfoList
.AddTail(Info
);
343 if (Info
->Category
== FALSE
)
346 if (EnumType
!= Info
->Category
&& EnumType
!= ENUM_ALL_AVAILABLE
)
349 Info
->RefreshAppInfo();
351 if (!lpEnumProc(static_cast<PAPPLICATION_INFO
>(Info
), m_szAppsPath
.GetString()))
354 } while (FindNextFileW(hFind
, &FindFileData
) != 0);
360 const ATL::CStringW
& CAvailableApps::GetFolderPath()
365 const ATL::CStringW
& CAvailableApps::GetAppPath()
370 const ATL::CStringW
& CAvailableApps::GetCabPath()
375 const LPCWSTR
CAvailableApps::GetFolderPathString()
377 return m_szPath
.GetString();
380 const LPCWSTR
CAvailableApps::GetAppPathString()
382 return m_szPath
.GetString();
385 const LPCWSTR
CAvailableApps::GetCabPathString()
387 return m_szPath
.GetString();
392 ATL::CStringW
CConfigParser::m_szLocaleID
;
393 ATL::CStringW
CConfigParser::m_szCachedINISectionLocale
;
394 ATL::CStringW
CConfigParser::m_szCachedINISectionLocaleNeutral
;
396 CConfigParser::CConfigParser(const ATL::CStringW
& FileName
) : szConfigPath(GetINIFullPath(FileName
))
398 // we don't have cached section strings for the current system language, create them, lazy
399 CacheINILocaleLazy();
402 ATL::CStringW
CConfigParser::GetINIFullPath(const ATL::CStringW
& FileName
)
405 ATL::CStringW szBuffer
;
407 GetStorageDirectory(szDir
);
408 szBuffer
.Format(L
"%ls\\rapps\\%ls", szDir
, FileName
);
413 VOID
CConfigParser::CacheINILocaleLazy()
415 if (m_szLocaleID
.IsEmpty())
417 // TODO: Set default locale if call fails
418 // find out what is the current system lang code (e.g. "0a") and append it to SectionLocale
419 GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE
,
420 m_szLocaleID
.GetBuffer(m_cchLocaleSize
), m_cchLocaleSize
);
422 m_szLocaleID
.ReleaseBuffer();
423 m_szCachedINISectionLocale
= L
"Section." + m_szLocaleID
;
425 // turn "Section.0c0a" into "Section.0a", keeping just the neutral lang part
426 m_szCachedINISectionLocaleNeutral
= m_szCachedINISectionLocale
+ m_szLocaleID
.Right(2);
430 const ATL::CStringW
& CConfigParser::GetLocale()
432 CacheINILocaleLazy();
436 INT
CConfigParser::GetLocaleSize()
438 return m_cchLocaleSize
;
441 UINT
CConfigParser::GetString(const ATL::CStringW
& KeyName
, ATL::CStringW
& ResultString
)
445 LPWSTR ResultStringBuffer
= ResultString
.GetBuffer(MAX_PATH
);
446 // 1st - find localized strings (e.g. "Section.0c0a")
447 dwResult
= GetPrivateProfileStringW(m_szCachedINISectionLocale
.GetString(),
452 szConfigPath
.GetString());
456 // 2nd - if they weren't present check for neutral sub-langs/ generic translations (e.g. "Section.0a")
457 dwResult
= GetPrivateProfileStringW(m_szCachedINISectionLocaleNeutral
.GetString(),
462 szConfigPath
.GetString());
465 // 3rd - if they weren't present fallback to standard english strings (just "Section")
466 dwResult
= GetPrivateProfileStringW(L
"Section",
471 szConfigPath
.GetString());
475 ResultString
.ReleaseBuffer();
476 return (dwResult
!= 0 ? TRUE
: FALSE
);
479 UINT
CConfigParser::GetInt(const ATL::CStringW
& KeyName
)
481 ATL::CStringW Buffer
;
483 // grab the text version of our entry
484 if (!GetString(KeyName
, Buffer
))
487 if (Buffer
.IsEmpty())
490 // convert it to an actual integer
491 int result
= StrToIntW(Buffer
.GetString());
493 return (UINT
) (result
<= 0) ? 0 : result
;