[RAPPS]
[reactos.git] / reactos / base / applications / rapps / available.c
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/available.c
5 * PURPOSE: Functions for working with availabled applications
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 */
8
9 #include "rapps.h"
10
11 BOOL
12 ShowAvailableAppInfo(INT Index)
13 {
14 PAPPLICATION_INFO Info = (PAPPLICATION_INFO) ListViewGetlParam(Index);
15 WCHAR szText[MAX_STR_LEN];
16
17 if (!Info) return FALSE;
18
19 NewRichEditText(Info->szName, CFE_BOLD);
20
21 InsertRichEditText(L"\n", 0);
22
23 #define ADD_TEXT(a, b, c, d) \
24 if (b[0] != '\0') \
25 { \
26 LoadStringW(hInst, a, szText, sizeof(szText) / sizeof(WCHAR)); \
27 InsertRichEditText(szText, c); \
28 InsertRichEditText(b, d); \
29 } \
30
31 ADD_TEXT(IDS_AINFO_VERSION, Info->szVersion, CFE_BOLD, 0);
32 ADD_TEXT(IDS_AINFO_LICENCE, Info->szLicence, CFE_BOLD, 0);
33 ADD_TEXT(IDS_AINFO_SIZE, Info->szSize, CFE_BOLD, 0);
34 ADD_TEXT(IDS_AINFO_URLSITE, Info->szUrlSite, CFE_BOLD, CFE_LINK);
35 ADD_TEXT(IDS_AINFO_DESCRIPTION, Info->szDesc, CFE_BOLD, 0);
36
37 return TRUE;
38 }
39
40 static BOOL
41 DeleteCurrentAppsDB(VOID)
42 {
43 HANDLE hFind = INVALID_HANDLE_VALUE;
44 WIN32_FIND_DATAW FindFileData;
45 WCHAR szCabPath[MAX_PATH];
46 WCHAR szSearchPath[MAX_PATH];
47 WCHAR szPath[MAX_PATH];
48 WCHAR szTmp[MAX_PATH];
49 HRESULT hr;
50
51 if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
52 return FALSE;
53
54 hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
55 L"%ls\\rappmgr.cab",
56 szPath);
57 if (FAILED(hr))
58 return FALSE;
59
60 if (GetFileAttributesW(szCabPath) != INVALID_FILE_ATTRIBUTES)
61 {
62 if (!DeleteFileW(szCabPath))
63 return FALSE;
64 }
65
66 hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
67 if (FAILED(hr))
68 return FALSE;
69
70 hr = StringCbPrintfW(szSearchPath, sizeof(szSearchPath),
71 L"%ls*.txt",
72 szPath);
73 if (FAILED(hr))
74 return FALSE;
75
76 hFind = FindFirstFileW(szSearchPath, &FindFileData);
77 if (hFind == INVALID_HANDLE_VALUE)
78 return TRUE;
79
80 do
81 {
82 hr = StringCbPrintfW(szTmp, sizeof(szTmp),
83 L"%ls%ls",
84 szPath, FindFileData.cFileName);
85 if (FAILED(hr) || !DeleteFileW(szTmp))
86 {
87 FindClose(hFind);
88 return FALSE;
89 }
90 }
91 while (FindNextFileW(hFind, &FindFileData) != 0);
92
93 FindClose(hFind);
94
95 return TRUE;
96 }
97
98
99 BOOL
100 UpdateAppsDB(VOID)
101 {
102 WCHAR szPath[MAX_PATH];
103 WCHAR szAppsPath[MAX_PATH];
104 WCHAR szCabPath[MAX_PATH];
105
106 if (!DeleteCurrentAppsDB())
107 return FALSE;
108
109 DownloadApplicationsDB(APPLICATION_DATEBASE_URL);
110
111 if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
112 return FALSE;
113
114 if (FAILED(StringCbPrintfW(szCabPath, sizeof(szCabPath),
115 L"%ls\\rappmgr.cab",
116 szPath)))
117 {
118 return FALSE;
119 }
120
121 if (FAILED(StringCbPrintfW(szAppsPath, sizeof(szAppsPath),
122 L"%ls\\rapps\\",
123 szPath)))
124 {
125 return FALSE;
126 }
127
128 ExtractFilesFromCab(szCabPath, szAppsPath);
129
130 return TRUE;
131 }
132
133
134 BOOL
135 EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
136 {
137 HANDLE hFind = INVALID_HANDLE_VALUE;
138 WIN32_FIND_DATAW FindFileData;
139 WCHAR szPath[MAX_PATH];
140 WCHAR szAppsPath[MAX_PATH];
141 WCHAR szSectionLocale[MAX_PATH] = L"Section.";
142 WCHAR szCabPath[MAX_PATH];
143 WCHAR szLocale[4 + 1];
144 APPLICATION_INFO Info;
145 HRESULT hr;
146
147 if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
148 {
149 return FALSE;
150 }
151
152 hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
153 L"%ls\\rappmgr.cab",
154 szPath);
155 if (FAILED(hr))
156 return FALSE;
157
158 hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
159 if (FAILED(hr))
160 return FALSE;
161 hr = StringCbCopyW(szAppsPath, sizeof(szAppsPath), szPath);
162 if (FAILED(hr))
163 return FALSE;
164
165 if (!CreateDirectory(szPath, NULL) &&
166 GetLastError() != ERROR_ALREADY_EXISTS)
167 {
168 return FALSE;
169 }
170
171 GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE, szLocale, sizeof(szLocale) / sizeof(WCHAR));
172 hr = StringCbCatW(szSectionLocale, sizeof(szSectionLocale), szLocale);
173 if (FAILED(hr))
174 return FALSE;
175
176 hr = StringCbCatW(szPath, sizeof(szPath), L"*.txt");
177 if (FAILED(hr))
178 return FALSE;
179
180 hFind = FindFirstFileW(szPath, &FindFileData);
181 if (hFind == INVALID_HANDLE_VALUE)
182 {
183 if (GetFileAttributesW(szCabPath) == INVALID_FILE_ATTRIBUTES)
184 DownloadApplicationsDB(APPLICATION_DATEBASE_URL);
185
186 ExtractFilesFromCab(szCabPath, szAppsPath);
187 hFind = FindFirstFileW(szPath, &FindFileData);
188 if (hFind == INVALID_HANDLE_VALUE)
189 return FALSE;
190 }
191
192 #define GET_STRING1(a, b) \
193 if (!ParserGetString(szSectionLocale, a, b, MAX_PATH, FindFileData.cFileName)) \
194 if (!ParserGetString(L"Section", a, b, MAX_PATH, FindFileData.cFileName)) \
195 continue;
196
197 #define GET_STRING2(a, b) \
198 if (!ParserGetString(szSectionLocale, a, b, MAX_PATH, FindFileData.cFileName)) \
199 if (!ParserGetString(L"Section", a, b, MAX_PATH, FindFileData.cFileName)) \
200 b[0] = '\0';
201
202 do
203 {
204 Info.Category = ParserGetInt(szSectionLocale, L"Category", FindFileData.cFileName);
205 if (Info.Category == -1)
206 {
207 Info.Category = ParserGetInt(L"Section", L"Category", FindFileData.cFileName);
208 if (Info.Category == -1)
209 continue;
210 }
211
212 if (EnumType != Info.Category && EnumType != ENUM_ALL_AVAILABLE) continue;
213
214 GET_STRING1(L"Name", Info.szName);
215 GET_STRING1(L"URLDownload", Info.szUrlDownload);
216
217 GET_STRING2(L"RegName", Info.szRegName);
218 GET_STRING2(L"Version", Info.szVersion);
219 GET_STRING2(L"Licence", Info.szLicence);
220 GET_STRING2(L"Description", Info.szDesc);
221 GET_STRING2(L"Size", Info.szSize);
222 GET_STRING2(L"URLSite", Info.szUrlSite);
223 GET_STRING2(L"CDPath", Info.szCDPath);
224
225 if (!lpEnumProc(Info)) break;
226 }
227 while (FindNextFileW(hFind, &FindFileData) != 0);
228
229 FindClose(hFind);
230
231 return TRUE;
232 }