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)
13 // CAvailableApplicationInfo
15 CAvailableApplicationInfo::CAvailableApplicationInfo(const ATL::CStringW
& sFileNameParam
)
16 : m_Parser(sFileNameParam
)
18 LicenseType
= LICENSE_TYPE::None
;
19 sFileName
= sFileNameParam
;
21 RetrieveGeneralInfo();
24 VOID
CAvailableApplicationInfo::RefreshAppInfo()
26 if (szUrlDownload
.IsEmpty())
28 RetrieveGeneralInfo();
32 VOID
CAvailableApplicationInfo::RetrieveGeneralInfo()
34 Category
= m_Parser
.GetInt(L
"Category");
36 if (!GetString(L
"Name", szName
)
37 || !GetString(L
"URLDownload", szUrlDownload
))
42 GetString(L
"RegName", szRegName
);
43 GetString(L
"Version", szVersion
);
44 GetString(L
"License", szLicense
);
45 GetString(L
"Description", szDesc
);
46 GetString(L
"Size", szSize
);
47 GetString(L
"URLSite", szUrlSite
);
48 GetString(L
"CDPath", szCDPath
);
49 GetString(L
"Language", szRegName
);
50 GetString(L
"SHA1", szSHA1
);
52 RetrieveLicenseType();
54 RetrieveInstalledStatus();
57 RetrieveInstalledVersion();
61 VOID
CAvailableApplicationInfo::RetrieveInstalledStatus()
63 m_IsInstalled
= ::GetInstalledVersion(NULL
, szRegName
)
64 || ::GetInstalledVersion(NULL
, szName
);
67 VOID
CAvailableApplicationInfo::RetrieveInstalledVersion()
69 m_HasInstalledVersion
= ::GetInstalledVersion(&szInstalledVersion
, szRegName
)
70 || ::GetInstalledVersion(&szInstalledVersion
, szName
);
73 VOID
CAvailableApplicationInfo::RetrieveLanguages()
75 const WCHAR cDelimiter
= L
'|';
76 ATL::CStringW szBuffer
;
78 // TODO: Get multiline parameter
79 if (!m_Parser
.GetString(L
"Languages", szBuffer
))
81 m_HasLanguageInfo
= FALSE
;
85 // Parse parameter string
86 ATL::CStringW m_szLocale
;
88 for (INT i
= 0; szBuffer
[i
] != UNICODE_NULL
; ++i
)
90 if (szBuffer
[i
] != cDelimiter
&& szBuffer
[i
] != L
'\n')
92 m_szLocale
+= szBuffer
[i
];
96 if (StrToIntExW(m_szLocale
.GetString(), STIF_DEFAULT
, &iLCID
))
98 Languages
.Add(static_cast<LCID
>(iLCID
));
104 // For the text after delimiter
105 if (!m_szLocale
.IsEmpty())
107 if (StrToIntExW(m_szLocale
.GetString(), STIF_DEFAULT
, &iLCID
))
109 Languages
.Add(static_cast<LCID
>(iLCID
));
113 m_HasLanguageInfo
= TRUE
;
116 VOID
CAvailableApplicationInfo::RetrieveLicenseType()
118 INT IntBuffer
= m_Parser
.GetInt(L
"LicenseType");
120 if (IntBuffer
< 0 || IntBuffer
> LICENSE_TYPE::Max
)
122 LicenseType
= LICENSE_TYPE::None
;
126 LicenseType
= (LICENSE_TYPE
) IntBuffer
;
130 BOOL
CAvailableApplicationInfo::FindInLanguages(LCID what
) const
132 if (!m_HasLanguageInfo
)
137 //Find locale code in the list
138 const INT nLanguagesSize
= Languages
.GetSize();
139 for (INT i
= 0; i
< nLanguagesSize
; ++i
)
141 if (Languages
[i
] == what
)
150 BOOL
CAvailableApplicationInfo::HasLanguageInfo() const
152 return m_HasLanguageInfo
;
155 BOOL
CAvailableApplicationInfo::HasNativeLanguage() const
157 return FindInLanguages(GetUserDefaultLCID());
160 BOOL
CAvailableApplicationInfo::HasEnglishLanguage() const
162 return FindInLanguages(MAKELCID(MAKELANGID(LANG_ENGLISH
, SUBLANG_DEFAULT
), SORT_DEFAULT
));
165 BOOL
CAvailableApplicationInfo::IsInstalled() const
167 return m_IsInstalled
;
170 BOOL
CAvailableApplicationInfo::HasInstalledVersion() const
172 return m_HasInstalledVersion
;
175 BOOL
CAvailableApplicationInfo::HasUpdate() const
177 return (szInstalledVersion
.Compare(szVersion
) < 0) ? TRUE
: FALSE
;
180 VOID
CAvailableApplicationInfo::SetLastWriteTime(FILETIME
* ftTime
)
182 RtlCopyMemory(&ftCacheStamp
, ftTime
, sizeof(FILETIME
));
185 inline BOOL
CAvailableApplicationInfo::GetString(LPCWSTR lpKeyName
, ATL::CStringW
& ReturnedString
)
187 if (!m_Parser
.GetString(lpKeyName
, ReturnedString
))
189 ReturnedString
.Empty();
194 // CAvailableApplicationInfo
197 CAvailableApps::CAvailableApps()
200 if (GetStorageDirectory(m_szPath
))
202 m_szAppsPath
= m_szPath
+ L
"\\rapps\\";
203 m_szCabPath
= m_szPath
+ L
"\\rappmgr.cab";
204 m_szSearchPath
= m_szAppsPath
+ L
"*.txt";
208 VOID
CAvailableApps::FreeCachedEntries()
210 POSITION InfoListPosition
= m_InfoList
.GetHeadPosition();
212 /* loop and deallocate all the cached app infos in the list */
213 while (InfoListPosition
)
215 CAvailableApplicationInfo
* Info
= m_InfoList
.GetAt(InfoListPosition
);
216 m_InfoList
.RemoveHead();
219 InfoListPosition
= m_InfoList
.GetHeadPosition();
223 BOOL
CAvailableApps::DeleteCurrentAppsDB()
225 HANDLE hFind
= INVALID_HANDLE_VALUE
;
226 WIN32_FIND_DATAW FindFileData
;
229 if (m_szPath
.IsEmpty())
232 result
= result
&& DeleteFileW(m_szCabPath
.GetString());
233 hFind
= FindFirstFileW(m_szSearchPath
.GetString(), &FindFileData
);
235 if (hFind
== INVALID_HANDLE_VALUE
)
241 szTmp
= m_szPath
+ FindFileData
.cFileName
;
242 result
= result
&& DeleteFileW(szTmp
.GetString());
243 } while (FindNextFileW(hFind
, &FindFileData
) != 0);
250 BOOL
CAvailableApps::UpdateAppsDB()
252 if (!DeleteCurrentAppsDB())
255 DownloadApplicationsDB(APPLICATION_DATABASE_URL
);
257 if (m_szPath
.IsEmpty())
260 if (!ExtractFilesFromCab(m_szCabPath
, m_szAppsPath
))
268 BOOL
CAvailableApps::EnumAvailableApplications(INT EnumType
, AVAILENUMPROC lpEnumProc
)
270 HANDLE hFind
= INVALID_HANDLE_VALUE
;
271 WIN32_FIND_DATAW FindFileData
;
273 if (!CreateDirectoryW(m_szPath
.GetString(), NULL
) &&
274 GetLastError() != ERROR_ALREADY_EXISTS
)
279 hFind
= FindFirstFileW(m_szSearchPath
.GetString(), &FindFileData
);
281 if (hFind
== INVALID_HANDLE_VALUE
)
283 if (GetFileAttributesW(m_szCabPath
) == INVALID_FILE_ATTRIBUTES
)
284 DownloadApplicationsDB(APPLICATION_DATABASE_URL
);
286 ExtractFilesFromCab(m_szCabPath
, m_szAppsPath
);
287 hFind
= FindFirstFileW(m_szSearchPath
.GetString(), &FindFileData
);
289 if (hFind
== INVALID_HANDLE_VALUE
)
295 // loop for all the cached entries
296 POSITION CurrentListPosition
= m_InfoList
.GetHeadPosition();
297 CAvailableApplicationInfo
* Info
= NULL
;
299 while (CurrentListPosition
!= NULL
)
301 POSITION LastListPosition
= CurrentListPosition
;
302 Info
= m_InfoList
.GetNext(CurrentListPosition
);
304 // do we already have this entry in cache?
305 if (Info
->sFileName
== FindFileData
.cFileName
)
307 // is it current enough, or the file has been modified since our last time here?
308 if (CompareFileTime(&FindFileData
.ftLastWriteTime
, &Info
->ftCacheStamp
) == 1)
310 // recreate our cache, this is the slow path
311 m_InfoList
.RemoveAt(LastListPosition
);
319 // speedy path, compare directly, we already have the data
325 // create a new entry
326 Info
= new CAvailableApplicationInfo(FindFileData
.cFileName
);
328 // set a timestamp for the next time
329 Info
->SetLastWriteTime(&FindFileData
.ftLastWriteTime
);
330 m_InfoList
.AddTail(Info
);
333 if (Info
->Category
== FALSE
)
336 if (EnumType
!= Info
->Category
&& EnumType
!= ENUM_ALL_AVAILABLE
)
339 Info
->RefreshAppInfo();
341 if (!lpEnumProc(static_cast<PAPPLICATION_INFO
>(Info
), m_szAppsPath
.GetString()))
344 } while (FindNextFileW(hFind
, &FindFileData
) != 0);
350 const ATL::CStringW
& CAvailableApps::GetFolderPath()
355 const ATL::CStringW
& CAvailableApps::GetAppPath()
360 const ATL::CStringW
& CAvailableApps::GetCabPath()
365 const LPCWSTR
CAvailableApps::GetFolderPathString()
367 return m_szPath
.GetString();
370 const LPCWSTR
CAvailableApps::GetAppPathString()
372 return m_szPath
.GetString();
375 const LPCWSTR
CAvailableApps::GetCabPathString()
377 return m_szPath
.GetString();
382 ATL::CStringW
CConfigParser::m_szLocaleID
;
383 ATL::CStringW
CConfigParser::m_szCachedINISectionLocale
;
384 ATL::CStringW
CConfigParser::m_szCachedINISectionLocaleNeutral
;
386 CConfigParser::CConfigParser(const ATL::CStringW
& FileName
) : szConfigPath(GetINIFullPath(FileName
))
388 // we don't have cached section strings for the current system language, create them, lazy
389 CacheINILocaleLazy();
392 ATL::CStringW
CConfigParser::GetINIFullPath(const ATL::CStringW
& FileName
)
395 ATL::CStringW szBuffer
;
397 GetStorageDirectory(szDir
);
398 szBuffer
.Format(L
"%ls\\rapps\\%ls", szDir
, FileName
);
403 VOID
CConfigParser::CacheINILocaleLazy()
405 if (m_szLocaleID
.IsEmpty())
407 // TODO: Set default locale if call fails
408 // find out what is the current system lang code (e.g. "0a") and append it to SectionLocale
409 GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE
,
410 m_szLocaleID
.GetBuffer(m_cchLocaleSize
), m_cchLocaleSize
);
412 m_szLocaleID
.ReleaseBuffer();
413 m_szCachedINISectionLocale
= L
"Section." + m_szLocaleID
;
415 // turn "Section.0c0a" into "Section.0a", keeping just the neutral lang part
416 m_szCachedINISectionLocaleNeutral
= m_szCachedINISectionLocale
+ m_szLocaleID
.Right(2);
420 const ATL::CStringW
& CConfigParser::GetLocale()
422 CacheINILocaleLazy();
426 INT
CConfigParser::GetLocaleSize()
428 return m_cchLocaleSize
;
431 UINT
CConfigParser::GetString(const ATL::CStringW
& KeyName
, ATL::CStringW
& ResultString
)
435 LPWSTR ResultStringBuffer
= ResultString
.GetBuffer(MAX_PATH
);
436 // 1st - find localized strings (e.g. "Section.0c0a")
437 dwResult
= GetPrivateProfileStringW(m_szCachedINISectionLocale
.GetString(),
442 szConfigPath
.GetString());
446 // 2nd - if they weren't present check for neutral sub-langs/ generic translations (e.g. "Section.0a")
447 dwResult
= GetPrivateProfileStringW(m_szCachedINISectionLocaleNeutral
.GetString(),
452 szConfigPath
.GetString());
455 // 3rd - if they weren't present fallback to standard english strings (just "Section")
456 dwResult
= GetPrivateProfileStringW(L
"Section",
461 szConfigPath
.GetString());
465 ResultString
.ReleaseBuffer();
466 return (dwResult
!= 0 ? TRUE
: FALSE
);
469 UINT
CConfigParser::GetInt(const ATL::CStringW
& KeyName
)
471 ATL::CStringW Buffer
;
473 // grab the text version of our entry
474 if (!GetString(KeyName
, Buffer
))
477 if (Buffer
.IsEmpty())
480 // convert it to an actual integer
481 int result
= StrToIntW(Buffer
.GetString());
483 return (UINT
) (result
<= 0) ? 0 : result
;