- Create another branch for networking fixes
[reactos.git] / 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
12 /* SESSION Operation */
13 #define EXTRACT_FILLFILELIST 0x00000001
14 #define EXTRACT_EXTRACTFILES 0x00000002
15
16 static HANDLE hLog = NULL;
17
18 typedef struct
19 {
20 int erfOper;
21 int erfType;
22 BOOL fError;
23 } ERF, *PERF;
24
25 struct FILELIST
26 {
27 LPSTR FileName;
28 struct FILELIST *next;
29 BOOL DoExtract;
30 };
31
32 typedef struct
33 {
34 INT FileSize;
35 ERF Error;
36 struct FILELIST *FileList;
37 INT FileCount;
38 INT Operation;
39 CHAR Destination[MAX_PATH];
40 CHAR CurrentFile[MAX_PATH];
41 CHAR Reserved[MAX_PATH];
42 struct FILELIST *FilterList;
43 } SESSION;
44
45 HRESULT (WINAPI *pfnExtract)(SESSION *dest, LPCSTR szCabName);
46
47
48 INT
49 GetSystemColorDepth(VOID)
50 {
51 DEVMODE pDevMode;
52 INT ColorDepth;
53
54 pDevMode.dmSize = sizeof(DEVMODE);
55 pDevMode.dmDriverExtra = 0;
56
57 if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &pDevMode))
58 {
59 /* TODO: Error message */
60 return ILC_COLOR;
61 }
62
63 switch (pDevMode.dmBitsPerPel)
64 {
65 case 32: ColorDepth = ILC_COLOR32; break;
66 case 24: ColorDepth = ILC_COLOR24; break;
67 case 16: ColorDepth = ILC_COLOR16; break;
68 case 8: ColorDepth = ILC_COLOR8; break;
69 case 4: ColorDepth = ILC_COLOR4; break;
70 default: ColorDepth = ILC_COLOR; break;
71 }
72
73 return ColorDepth;
74 }
75
76 int
77 GetWindowWidth(HWND hwnd)
78 {
79 RECT Rect;
80
81 GetWindowRect(hwnd, &Rect);
82 return (Rect.right - Rect.left);
83 }
84
85 int
86 GetWindowHeight(HWND hwnd)
87 {
88 RECT Rect;
89
90 GetWindowRect(hwnd, &Rect);
91 return (Rect.bottom - Rect.top);
92 }
93
94 int
95 GetClientWindowWidth(HWND hwnd)
96 {
97 RECT Rect;
98
99 GetClientRect(hwnd, &Rect);
100 return (Rect.right - Rect.left);
101 }
102
103 int
104 GetClientWindowHeight(HWND hwnd)
105 {
106 RECT Rect;
107
108 GetClientRect(hwnd, &Rect);
109 return (Rect.bottom - Rect.top);
110 }
111
112 VOID
113 CopyTextToClipboard(LPCWSTR lpszText)
114 {
115 if(OpenClipboard(NULL))
116 {
117 HGLOBAL ClipBuffer;
118 WCHAR *Buffer;
119
120 EmptyClipboard();
121 ClipBuffer = GlobalAlloc(GMEM_DDESHARE, (wcslen(lpszText) + 1) * sizeof(TCHAR));
122 Buffer = (WCHAR*)GlobalLock(ClipBuffer);
123 wcscpy(Buffer, lpszText);
124 GlobalUnlock(ClipBuffer);
125
126 SetClipboardData(CF_UNICODETEXT, ClipBuffer);
127
128 CloseClipboard();
129 }
130 }
131
132 VOID
133 SetWelcomeText(VOID)
134 {
135 WCHAR szText[MAX_STR_LEN*3];
136
137 LoadStringW(hInst, IDS_WELCOME_TITLE, szText, sizeof(szText) / sizeof(WCHAR));
138 NewRichEditText(szText, CFE_BOLD);
139
140 LoadStringW(hInst, IDS_WELCOME_TEXT, szText, sizeof(szText) / sizeof(WCHAR));
141 InsertRichEditText(szText, 0);
142
143 LoadStringW(hInst, IDS_WELCOME_URL, szText, sizeof(szText) / sizeof(WCHAR));
144 InsertRichEditText(szText, CFM_LINK);
145 }
146
147 VOID
148 ShowPopupMenu(HWND hwnd, UINT MenuID)
149 {
150 HMENU hPopupMenu = GetSubMenu(LoadMenuW(hInst, MAKEINTRESOURCEW(MenuID)), 0);
151 POINT pt;
152
153 GetCursorPos(&pt);
154
155 SetForegroundWindow(hwnd);
156 TrackPopupMenu(hPopupMenu, 0, pt.x, pt.y, 0, hMainWnd, NULL);
157
158 DestroyMenu(hPopupMenu);
159 }
160
161 BOOL
162 StartProcess(LPWSTR lpPath, BOOL Wait)
163 {
164 PROCESS_INFORMATION pi;
165 STARTUPINFOW si;
166 DWORD dwRet;
167 MSG msg;
168
169 ZeroMemory(&si, sizeof(si));
170 si.cb = sizeof(si);
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
212 BOOL
213 ExtractFilesFromCab(LPWSTR lpCabName, LPWSTR lpOutputPath)
214 {
215 HINSTANCE hCabinetDll;
216 CHAR szCabName[MAX_PATH];
217 SESSION Dest;
218 HRESULT Result;
219
220 hCabinetDll = LoadLibraryW(L"cabinet.dll");
221 if (hCabinetDll)
222 {
223 pfnExtract = (void *) GetProcAddress(hCabinetDll, "Extract");
224 if (pfnExtract)
225 {
226 ZeroMemory(&Dest, sizeof(SESSION));
227
228 WideCharToMultiByte(CP_ACP, 0, lpOutputPath, -1, Dest.Destination, MAX_PATH, NULL, NULL);
229 WideCharToMultiByte(CP_ACP, 0, lpCabName, -1, szCabName, MAX_PATH, NULL, NULL);
230 Dest.Operation = EXTRACT_FILLFILELIST;
231
232 Result = pfnExtract(&Dest, szCabName);
233 if (Result == S_OK)
234 {
235 Dest.Operation = EXTRACT_EXTRACTFILES;
236 Result = pfnExtract(&Dest, szCabName);
237 if (Result == S_OK)
238 {
239 FreeLibrary(hCabinetDll);
240 return TRUE;
241 }
242 }
243 }
244 FreeLibrary(hCabinetDll);
245 }
246
247 return FALSE;
248 }
249
250 VOID
251 InitLogs(VOID)
252 {
253 WCHAR szBuf[MAX_PATH] = L"SYSTEM\\CurrentControlSet\\Services\\EventLog\\ReactOS Application Manager\\ReactOS Application Manager";
254 WCHAR szPath[MAX_PATH];
255 DWORD dwCategoryNum = 1;
256 DWORD dwDisp, dwData;
257 HKEY hKey;
258
259 if (!SettingsInfo.bLogEnabled) return;
260
261 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,
262 szBuf, 0, NULL,
263 REG_OPTION_NON_VOLATILE,
264 KEY_WRITE, NULL, &hKey, &dwDisp) != ERROR_SUCCESS)
265 {
266 return;
267 }
268
269 if (!GetCurrentDirectoryW(MAX_PATH, szPath)) return;
270 wcscat(szPath, L"\\rapps.exe");
271
272 if (RegSetValueExW(hKey,
273 L"EventMessageFile",
274 0,
275 REG_EXPAND_SZ,
276 (LPBYTE)szPath,
277 (DWORD)(wcslen(szPath) + 1) * sizeof(WCHAR)) != ERROR_SUCCESS)
278 {
279 RegCloseKey(hKey);
280 return;
281 }
282
283 dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE |
284 EVENTLOG_INFORMATION_TYPE;
285
286 if (RegSetValueExW(hKey,
287 L"TypesSupported",
288 0,
289 REG_DWORD,
290 (LPBYTE)&dwData,
291 sizeof(DWORD)) != ERROR_SUCCESS)
292 {
293 RegCloseKey(hKey);
294 return;
295 }
296
297 if (RegSetValueExW(hKey,
298 L"CategoryMessageFile",
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 if (RegSetValueExW(hKey,
309 L"CategoryCount",
310 0,
311 REG_DWORD,
312 (LPBYTE)&dwCategoryNum,
313 sizeof(DWORD)) != ERROR_SUCCESS)
314 {
315 RegCloseKey(hKey);
316 return;
317 }
318
319 RegCloseKey(hKey);
320
321 hLog = RegisterEventSourceW(NULL, L"ReactOS Application Manager");
322 }
323
324
325 VOID
326 FreeLogs(VOID)
327 {
328 if (hLog) DeregisterEventSource(hLog);
329 }
330
331
332 BOOL
333 WriteLogMessage(WORD wType, DWORD dwEventID, LPWSTR lpMsg)
334 {
335 if (!SettingsInfo.bLogEnabled) return TRUE;
336
337 if (!ReportEventW(hLog,
338 wType,
339 0,
340 dwEventID,
341 NULL,
342 1,
343 0,
344 (LPCWSTR*)&lpMsg,
345 NULL))
346 {
347 return FALSE;
348 }
349
350 return TRUE;
351 }