Partial merge of condrv_restructure branch r65657.
[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_LICENSE, Info->szLicense, 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 BOOL result = TRUE;
51
52 if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
53 return FALSE;
54
55 hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
56 L"%ls\\rappmgr.cab",
57 szPath);
58 if (FAILED(hr))
59 return FALSE;
60
61 result = result && DeleteFileW(szCabPath);
62
63 hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
64 if (FAILED(hr))
65 return FALSE;
66
67 hr = StringCbPrintfW(szSearchPath, sizeof(szSearchPath),
68 L"%ls*.txt",
69 szPath);
70 if (FAILED(hr))
71 return FALSE;
72
73 hFind = FindFirstFileW(szSearchPath, &FindFileData);
74 if (hFind == INVALID_HANDLE_VALUE)
75 return result;
76
77 do
78 {
79 hr = StringCbPrintfW(szTmp, sizeof(szTmp),
80 L"%ls%ls",
81 szPath, FindFileData.cFileName);
82 if (FAILED(hr))
83 continue;
84 result = result && DeleteFileW(szTmp);
85 } while (FindNextFileW(hFind, &FindFileData) != 0);
86
87 FindClose(hFind);
88
89 return result;
90 }
91
92
93 BOOL
94 UpdateAppsDB(VOID)
95 {
96 WCHAR szPath[MAX_PATH];
97 WCHAR szAppsPath[MAX_PATH];
98 WCHAR szCabPath[MAX_PATH];
99
100 if (!DeleteCurrentAppsDB())
101 return FALSE;
102
103 DownloadApplicationsDB(APPLICATION_DATEBASE_URL);
104
105 if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
106 return FALSE;
107
108 if (FAILED(StringCbPrintfW(szCabPath, sizeof(szCabPath),
109 L"%ls\\rappmgr.cab",
110 szPath)))
111 {
112 return FALSE;
113 }
114
115 if (FAILED(StringCbPrintfW(szAppsPath, sizeof(szAppsPath),
116 L"%ls\\rapps\\",
117 szPath)))
118 {
119 return FALSE;
120 }
121
122 ExtractFilesFromCab(szCabPath, szAppsPath);
123
124 return TRUE;
125 }
126
127
128 BOOL
129 EnumAvailableApplications(INT EnumType, AVAILENUMPROC lpEnumProc)
130 {
131 HANDLE hFind = INVALID_HANDLE_VALUE;
132 WIN32_FIND_DATAW FindFileData;
133 WCHAR szPath[MAX_PATH];
134 WCHAR szAppsPath[MAX_PATH];
135 WCHAR szSectionLocale[MAX_PATH] = L"Section.";
136 WCHAR szCabPath[MAX_PATH];
137 WCHAR szLocale[4 + 1];
138 APPLICATION_INFO Info;
139 HRESULT hr;
140
141 if (!GetStorageDirectory(szPath, sizeof(szPath) / sizeof(szPath[0])))
142 return FALSE;
143
144 hr = StringCbPrintfW(szCabPath, sizeof(szCabPath),
145 L"%ls\\rappmgr.cab",
146 szPath);
147 if (FAILED(hr))
148 return FALSE;
149
150 hr = StringCbCatW(szPath, sizeof(szPath), L"\\rapps\\");
151 if (FAILED(hr))
152 return FALSE;
153 hr = StringCbCopyW(szAppsPath, sizeof(szAppsPath), szPath);
154 if (FAILED(hr))
155 return FALSE;
156
157 if (!CreateDirectory(szPath, NULL) &&
158 GetLastError() != ERROR_ALREADY_EXISTS)
159 {
160 return FALSE;
161 }
162
163 hr = StringCbCatW(szPath, sizeof(szPath), L"*.txt");
164 if (FAILED(hr))
165 return FALSE;
166
167 hFind = FindFirstFileW(szPath, &FindFileData);
168 if (hFind == INVALID_HANDLE_VALUE)
169 {
170 if (GetFileAttributesW(szCabPath) == INVALID_FILE_ATTRIBUTES)
171 DownloadApplicationsDB(APPLICATION_DATEBASE_URL);
172
173 ExtractFilesFromCab(szCabPath, szAppsPath);
174 hFind = FindFirstFileW(szPath, &FindFileData);
175 if (hFind == INVALID_HANDLE_VALUE)
176 return FALSE;
177 }
178
179 if (!GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE,
180 szLocale, sizeof(szLocale) / sizeof(WCHAR)))
181 {
182 FindClose(hFind);
183 return FALSE;
184 }
185
186 hr = StringCbCatW(szSectionLocale, sizeof(szSectionLocale), szLocale);
187 if (FAILED(hr))
188 {
189 FindClose(hFind);
190 return FALSE;
191 }
192
193 #define GET_STRING1(a, b) \
194 if (!ParserGetString(szSectionLocale, a, b, MAX_PATH, FindFileData.cFileName)) \
195 if (!ParserGetString(L"Section", a, b, MAX_PATH, FindFileData.cFileName)) \
196 continue;
197
198 #define GET_STRING2(a, b) \
199 if (!ParserGetString(szSectionLocale, a, b, MAX_PATH, FindFileData.cFileName)) \
200 if (!ParserGetString(L"Section", a, b, MAX_PATH, FindFileData.cFileName)) \
201 b[0] = '\0';
202
203 do
204 {
205 Info.Category = ParserGetInt(szSectionLocale, L"Category", FindFileData.cFileName);
206 if (Info.Category == -1)
207 {
208 Info.Category = ParserGetInt(L"Section", L"Category", FindFileData.cFileName);
209 if (Info.Category == -1)
210 continue;
211 }
212
213 if (EnumType != Info.Category && EnumType != ENUM_ALL_AVAILABLE) continue;
214
215 GET_STRING1(L"Name", Info.szName);
216 GET_STRING1(L"URLDownload", Info.szUrlDownload);
217
218 GET_STRING2(L"RegName", Info.szRegName);
219 GET_STRING2(L"Version", Info.szVersion);
220 GET_STRING2(L"License", Info.szLicense);
221 GET_STRING2(L"Description", Info.szDesc);
222 GET_STRING2(L"Size", Info.szSize);
223 GET_STRING2(L"URLSite", Info.szUrlSite);
224 GET_STRING2(L"CDPath", Info.szCDPath);
225
226 if (!lpEnumProc(&Info)) break;
227 } while (FindNextFileW(hFind, &FindFileData) != 0);
228
229 FindClose(hFind);
230
231 return TRUE;
232 }