[RAPPS]
[reactos.git] / reactos / base / applications / rapps / misc.c
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/misc.c
5 * PURPOSE: Misc functions
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 */
8
9 #include "rapps.h"
10
11 /* SESSION Operation */
12 #define EXTRACT_FILLFILELIST 0x00000001
13 #define EXTRACT_EXTRACTFILES 0x00000002
14
15 static HANDLE hLog = NULL;
16
17 typedef struct
18 {
19 int erfOper;
20 int erfType;
21 BOOL fError;
22 } ERF, *PERF;
23
24 struct FILELIST
25 {
26 LPSTR FileName;
27 struct FILELIST *next;
28 BOOL DoExtract;
29 };
30
31 typedef struct
32 {
33 INT FileSize;
34 ERF Error;
35 struct FILELIST *FileList;
36 INT FileCount;
37 INT Operation;
38 CHAR Destination[MAX_PATH];
39 CHAR CurrentFile[MAX_PATH];
40 CHAR Reserved[MAX_PATH];
41 struct FILELIST *FilterList;
42 } SESSION;
43
44 HRESULT (WINAPI *pfnExtract)(SESSION *dest, LPCSTR szCabName);
45
46
47 INT
48 GetSystemColorDepth(VOID)
49 {
50 DEVMODE pDevMode;
51 INT ColorDepth;
52
53 pDevMode.dmSize = sizeof(DEVMODE);
54 pDevMode.dmDriverExtra = 0;
55
56 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &pDevMode))
57 {
58 /* TODO: Error message */
59 return ILC_COLOR;
60 }
61
62 switch (pDevMode.dmBitsPerPel)
63 {
64 case 32: ColorDepth = ILC_COLOR32; break;
65 case 24: ColorDepth = ILC_COLOR24; break;
66 case 16: ColorDepth = ILC_COLOR16; break;
67 case 8: ColorDepth = ILC_COLOR8; break;
68 case 4: ColorDepth = ILC_COLOR4; break;
69 default: ColorDepth = ILC_COLOR; break;
70 }
71
72 return ColorDepth;
73 }
74
75 int
76 GetWindowWidth(HWND hwnd)
77 {
78 RECT Rect;
79
80 GetWindowRect(hwnd, &Rect);
81 return (Rect.right - Rect.left);
82 }
83
84 int
85 GetWindowHeight(HWND hwnd)
86 {
87 RECT Rect;
88
89 GetWindowRect(hwnd, &Rect);
90 return (Rect.bottom - Rect.top);
91 }
92
93 int
94 GetClientWindowWidth(HWND hwnd)
95 {
96 RECT Rect;
97
98 GetClientRect(hwnd, &Rect);
99 return (Rect.right - Rect.left);
100 }
101
102 int
103 GetClientWindowHeight(HWND hwnd)
104 {
105 RECT Rect;
106
107 GetClientRect(hwnd, &Rect);
108 return (Rect.bottom - Rect.top);
109 }
110
111 VOID
112 CopyTextToClipboard(LPCWSTR lpszText)
113 {
114 HRESULT hr;
115
116 if(OpenClipboard(NULL))
117 {
118 HGLOBAL ClipBuffer;
119 WCHAR *Buffer;
120 DWORD cchBuffer;
121
122 EmptyClipboard();
123 cchBuffer = wcslen(lpszText) + 1;
124 ClipBuffer = GlobalAlloc(GMEM_DDESHARE, cchBuffer * sizeof(WCHAR));
125 Buffer = (WCHAR*)GlobalLock(ClipBuffer);
126 hr = StringCchCopyW(Buffer, cchBuffer, lpszText);
127 GlobalUnlock(ClipBuffer);
128
129 if (SUCCEEDED(hr))
130 SetClipboardData(CF_UNICODETEXT, ClipBuffer);
131
132 CloseClipboard();
133 }
134 }
135
136 VOID
137 SetWelcomeText(VOID)
138 {
139 WCHAR szText[MAX_STR_LEN*3];
140
141 LoadStringW(hInst, IDS_WELCOME_TITLE, szText, sizeof(szText) / sizeof(WCHAR));
142 NewRichEditText(szText, CFE_BOLD);
143
144 LoadStringW(hInst, IDS_WELCOME_TEXT, szText, sizeof(szText) / sizeof(WCHAR));
145 InsertRichEditText(szText, 0);
146
147 LoadStringW(hInst, IDS_WELCOME_URL, szText, sizeof(szText) / sizeof(WCHAR));
148 InsertRichEditText(szText, CFM_LINK);
149 }
150
151 VOID
152 ShowPopupMenu(HWND hwnd, UINT MenuID)
153 {
154 HMENU hPopupMenu = GetSubMenu(LoadMenuW(hInst, MAKEINTRESOURCEW(MenuID)), 0);
155 POINT pt;
156
157 GetCursorPos(&pt);
158
159 SetForegroundWindow(hwnd);
160 TrackPopupMenu(hPopupMenu, 0, pt.x, pt.y, 0, hMainWnd, NULL);
161
162 DestroyMenu(hPopupMenu);
163 }
164
165 BOOL
166 StartProcess(LPWSTR lpPath, BOOL Wait)
167 {
168 PROCESS_INFORMATION pi;
169 STARTUPINFOW si;
170 DWORD dwRet;
171 MSG msg;
172
173 ZeroMemory(&si, sizeof(si));
174 si.cb = sizeof(si);
175 si.dwFlags = STARTF_USESHOWWINDOW;
176 si.wShowWindow = SW_SHOW;
177
178 if (!CreateProcessW(NULL, lpPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
179 {
180 return FALSE;
181 }
182
183 CloseHandle(pi.hThread);
184 if (Wait) EnableWindow(hMainWnd, FALSE);
185
186 while (Wait)
187 {
188 dwRet = MsgWaitForMultipleObjects(1, &pi.hProcess, FALSE, INFINITE, QS_ALLEVENTS);
189 if (dwRet == WAIT_OBJECT_0 + 1)
190 {
191 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
192 {
193 TranslateMessage(&msg);
194 DispatchMessage(&msg);
195 }
196 }
197 else
198 {
199 if (dwRet == WAIT_OBJECT_0 || dwRet == WAIT_FAILED)
200 break;
201 }
202 }
203
204 CloseHandle(pi.hProcess);
205
206 if (Wait)
207 {
208 EnableWindow(hMainWnd, TRUE);
209 SetForegroundWindow(hMainWnd);
210 SetFocus(hMainWnd);
211 }
212
213 return TRUE;
214 }
215
216 BOOL
217 GetStorageDirectory(PWCHAR lpDirectory, DWORD cch)
218 {
219 if (cch < MAX_PATH)
220 return FALSE;
221
222 if (!SHGetSpecialFolderPathW(NULL, lpDirectory, CSIDL_LOCAL_APPDATA, TRUE))
223 return FALSE;
224
225 if (FAILED(StringCchCatW(lpDirectory, cch, L"\\rapps")))
226 return FALSE;
227
228 if (!CreateDirectoryW(lpDirectory, NULL) &&
229 GetLastError() != ERROR_ALREADY_EXISTS)
230 {
231 return FALSE;
232 }
233
234 return TRUE;
235 }
236
237 BOOL
238 ExtractFilesFromCab(LPWSTR lpCabName, LPWSTR lpOutputPath)
239 {
240 HINSTANCE hCabinetDll;
241 CHAR szCabName[MAX_PATH];
242 SESSION Dest;
243 HRESULT Result;
244
245 hCabinetDll = LoadLibraryW(L"cabinet.dll");
246 if (hCabinetDll)
247 {
248 pfnExtract = (void *) GetProcAddress(hCabinetDll, "Extract");
249 if (pfnExtract)
250 {
251 ZeroMemory(&Dest, sizeof(SESSION));
252
253 WideCharToMultiByte(CP_ACP, 0, lpOutputPath, -1, Dest.Destination, MAX_PATH, NULL, NULL);
254 WideCharToMultiByte(CP_ACP, 0, lpCabName, -1, szCabName, MAX_PATH, NULL, NULL);
255 Dest.Operation = EXTRACT_FILLFILELIST;
256
257 Result = pfnExtract(&Dest, szCabName);
258 if (Result == S_OK)
259 {
260 Dest.Operation = EXTRACT_EXTRACTFILES;
261 Result = pfnExtract(&Dest, szCabName);
262 if (Result == S_OK)
263 {
264 FreeLibrary(hCabinetDll);
265 return TRUE;
266 }
267 }
268 }
269 FreeLibrary(hCabinetDll);
270 }
271
272 return FALSE;
273 }
274
275 VOID
276 InitLogs(VOID)
277 {
278 WCHAR szBuf[MAX_PATH] = L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\ReactOS Application Manager\\ReactOS Application Manager";
279 WCHAR szPath[MAX_PATH];
280 DWORD dwCategoryNum = 1;
281 DWORD dwDisp, dwData;
282 HKEY hKey;
283
284 if (!SettingsInfo.bLogEnabled) return;
285
286 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
287 szBuf, 0, NULL,
288 REG_OPTION_NON_VOLATILE,
289 KEY_WRITE, NULL, &hKey, &dwDisp) != ERROR_SUCCESS)
290 {
291 return;
292 }
293
294 if (!GetModuleFileName(NULL, szPath, sizeof(szPath) / sizeof(szPath[0])))
295 return;
296
297 if (RegSetValueExW(hKey,
298 L"EventMessageFile",
299 0,
300 REG_EXPAND_SZ,
301 (LPBYTE)szPath,
302 (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
303 {
304 RegCloseKey(hKey);
305 return;
306 }
307
308 dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
309 EVENTLOG_INFORMATION_TYPE;
310
311 if (RegSetValueExW(hKey,
312 L"TypesSupported",
313 0,
314 REG_DWORD,
315 (LPBYTE)&dwData,
316 sizeof(DWORD)) != ERROR_SUCCESS)
317 {
318 RegCloseKey(hKey);
319 return;
320 }
321
322 if (RegSetValueExW(hKey,
323 L"CategoryMessageFile",
324 0,
325 REG_EXPAND_SZ,
326 (LPBYTE)szPath,
327 (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
328 {
329 RegCloseKey(hKey);
330 return;
331 }
332
333 if (RegSetValueExW(hKey,
334 L"CategoryCount",
335 0,
336 REG_DWORD,
337 (LPBYTE)&dwCategoryNum,
338 sizeof(DWORD)) != ERROR_SUCCESS)
339 {
340 RegCloseKey(hKey);
341 return;
342 }
343
344 RegCloseKey(hKey);
345
346 hLog = RegisterEventSourceW(NULL, L"ReactOS Application Manager");
347 }
348
349
350 VOID
351 FreeLogs(VOID)
352 {
353 if (hLog) DeregisterEventSource(hLog);
354 }
355
356
357 BOOL
358 WriteLogMessage(WORD wType, DWORD dwEventID, LPWSTR lpMsg)
359 {
360 if (!SettingsInfo.bLogEnabled) return TRUE;
361
362 if (!ReportEventW(hLog,
363 wType,
364 0,
365 dwEventID,
366 NULL,
367 1,
368 0,
369 (LPCWSTR*)&lpMsg,
370 NULL))
371 {
372 return FALSE;
373 }
374
375 return TRUE;
376 }