[LOG2LINES]
[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 if(OpenClipboard(NULL))
115 {
116 HGLOBAL ClipBuffer;
117 WCHAR *Buffer;
118
119 EmptyClipboard();
120 ClipBuffer = GlobalAlloc(GMEM_DDESHARE, (wcslen(lpszText) + 1) * sizeof(TCHAR));
121 Buffer = (WCHAR*)GlobalLock(ClipBuffer);
122 wcscpy(Buffer, lpszText);
123 GlobalUnlock(ClipBuffer);
124
125 SetClipboardData(CF_UNICODETEXT, ClipBuffer);
126
127 CloseClipboard();
128 }
129 }
130
131 VOID
132 SetWelcomeText(VOID)
133 {
134 WCHAR szText[MAX_STR_LEN*3];
135
136 LoadStringW(hInst, IDS_WELCOME_TITLE, szText, sizeof(szText) / sizeof(WCHAR));
137 NewRichEditText(szText, CFE_BOLD);
138
139 LoadStringW(hInst, IDS_WELCOME_TEXT, szText, sizeof(szText) / sizeof(WCHAR));
140 InsertRichEditText(szText, 0);
141
142 LoadStringW(hInst, IDS_WELCOME_URL, szText, sizeof(szText) / sizeof(WCHAR));
143 InsertRichEditText(szText, CFM_LINK);
144 }
145
146 VOID
147 ShowPopupMenu(HWND hwnd, UINT MenuID)
148 {
149 HMENU hPopupMenu = GetSubMenu(LoadMenuW(hInst, MAKEINTRESOURCEW(MenuID)), 0);
150 POINT pt;
151
152 GetCursorPos(&pt);
153
154 SetForegroundWindow(hwnd);
155 TrackPopupMenu(hPopupMenu, 0, pt.x, pt.y, 0, hMainWnd, NULL);
156
157 DestroyMenu(hPopupMenu);
158 }
159
160 BOOL
161 StartProcess(LPWSTR lpPath, BOOL Wait)
162 {
163 PROCESS_INFORMATION pi;
164 STARTUPINFOW si;
165 DWORD dwRet;
166 MSG msg;
167
168 ZeroMemory(&si, sizeof(si));
169 si.cb = sizeof(si);
170 si.dwFlags = STARTF_USESHOWWINDOW;
171 si.wShowWindow = SW_SHOW;
172
173 if (!CreateProcessW(NULL, lpPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
174 {
175 return FALSE;
176 }
177
178 CloseHandle(pi.hThread);
179 if (Wait) EnableWindow(hMainWnd, FALSE);
180
181 while (Wait)
182 {
183 dwRet = MsgWaitForMultipleObjects(1, &pi.hProcess, FALSE, INFINITE, QS_ALLEVENTS);
184 if (dwRet == WAIT_OBJECT_0 + 1)
185 {
186 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
187 {
188 TranslateMessage(&msg);
189 DispatchMessage(&msg);
190 }
191 }
192 else
193 {
194 if (dwRet == WAIT_OBJECT_0 || dwRet == WAIT_FAILED)
195 break;
196 }
197 }
198
199 CloseHandle(pi.hProcess);
200
201 if (Wait)
202 {
203 EnableWindow(hMainWnd, TRUE);
204 SetForegroundWindow(hMainWnd);
205 SetFocus(hMainWnd);
206 }
207
208 return TRUE;
209 }
210
211 BOOL
212 GetStorageDirectory(PWCHAR lpDirectory, DWORD cch)
213 {
214 if (cch < MAX_PATH)
215 return FALSE;
216
217 if (!SHGetSpecialFolderPathW(NULL, lpDirectory, CSIDL_LOCAL_APPDATA, TRUE))
218 return FALSE;
219
220 if (FAILED(StringCchCatW(lpDirectory, cch, L"\\rapps")))
221 return FALSE;
222
223 if (!CreateDirectoryW(lpDirectory, NULL) &&
224 GetLastError() != ERROR_ALREADY_EXISTS)
225 {
226 return FALSE;
227 }
228
229 return TRUE;
230 }
231
232 BOOL
233 ExtractFilesFromCab(LPWSTR lpCabName, LPWSTR lpOutputPath)
234 {
235 HINSTANCE hCabinetDll;
236 CHAR szCabName[MAX_PATH];
237 SESSION Dest;
238 HRESULT Result;
239
240 hCabinetDll = LoadLibraryW(L"cabinet.dll");
241 if (hCabinetDll)
242 {
243 pfnExtract = (void *) GetProcAddress(hCabinetDll, "Extract");
244 if (pfnExtract)
245 {
246 ZeroMemory(&Dest, sizeof(SESSION));
247
248 WideCharToMultiByte(CP_ACP, 0, lpOutputPath, -1, Dest.Destination, MAX_PATH, NULL, NULL);
249 WideCharToMultiByte(CP_ACP, 0, lpCabName, -1, szCabName, MAX_PATH, NULL, NULL);
250 Dest.Operation = EXTRACT_FILLFILELIST;
251
252 Result = pfnExtract(&Dest, szCabName);
253 if (Result == S_OK)
254 {
255 Dest.Operation = EXTRACT_EXTRACTFILES;
256 Result = pfnExtract(&Dest, szCabName);
257 if (Result == S_OK)
258 {
259 FreeLibrary(hCabinetDll);
260 return TRUE;
261 }
262 }
263 }
264 FreeLibrary(hCabinetDll);
265 }
266
267 return FALSE;
268 }
269
270 VOID
271 InitLogs(VOID)
272 {
273 WCHAR szBuf[MAX_PATH] = L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\ReactOS Application Manager\\ReactOS Application Manager";
274 WCHAR szPath[MAX_PATH];
275 DWORD dwCategoryNum = 1;
276 DWORD dwDisp, dwData;
277 HKEY hKey;
278
279 if (!SettingsInfo.bLogEnabled) return;
280
281 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
282 szBuf, 0, NULL,
283 REG_OPTION_NON_VOLATILE,
284 KEY_WRITE, NULL, &hKey, &dwDisp) != ERROR_SUCCESS)
285 {
286 return;
287 }
288
289 if (!GetModuleFileName(NULL, szPath, sizeof(szPath) / sizeof(szPath[0])))
290 return;
291
292 if (RegSetValueExW(hKey,
293 L"EventMessageFile",
294 0,
295 REG_EXPAND_SZ,
296 (LPBYTE)szPath,
297 (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
298 {
299 RegCloseKey(hKey);
300 return;
301 }
302
303 dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
304 EVENTLOG_INFORMATION_TYPE;
305
306 if (RegSetValueExW(hKey,
307 L"TypesSupported",
308 0,
309 REG_DWORD,
310 (LPBYTE)&dwData,
311 sizeof(DWORD)) != ERROR_SUCCESS)
312 {
313 RegCloseKey(hKey);
314 return;
315 }
316
317 if (RegSetValueExW(hKey,
318 L"CategoryMessageFile",
319 0,
320 REG_EXPAND_SZ,
321 (LPBYTE)szPath,
322 (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
323 {
324 RegCloseKey(hKey);
325 return;
326 }
327
328 if (RegSetValueExW(hKey,
329 L"CategoryCount",
330 0,
331 REG_DWORD,
332 (LPBYTE)&dwCategoryNum,
333 sizeof(DWORD)) != ERROR_SUCCESS)
334 {
335 RegCloseKey(hKey);
336 return;
337 }
338
339 RegCloseKey(hKey);
340
341 hLog = RegisterEventSourceW(NULL, L"ReactOS Application Manager");
342 }
343
344
345 VOID
346 FreeLogs(VOID)
347 {
348 if (hLog) DeregisterEventSource(hLog);
349 }
350
351
352 BOOL
353 WriteLogMessage(WORD wType, DWORD dwEventID, LPWSTR lpMsg)
354 {
355 if (!SettingsInfo.bLogEnabled) return TRUE;
356
357 if (!ReportEventW(hLog,
358 wType,
359 0,
360 dwEventID,
361 NULL,
362 1,
363 0,
364 (LPCWSTR*)&lpMsg,
365 NULL))
366 {
367 return FALSE;
368 }
369
370 return TRUE;
371 }