[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 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
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 }