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 * Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
10 #include <ndk/rtlfuncs.h>
13 /* SESSION Operation */
14 #define EXTRACT_FILLFILELIST 0x00000001
15 #define EXTRACT_EXTRACTFILES 0x00000002
17 static HANDLE hLog
= NULL
;
18 WCHAR szCachedINISectionLocale
[MAX_PATH
] = L
"Section.";
19 WCHAR szCachedINISectionLocaleNeutral
[MAX_PATH
] = {0};
20 BYTE bCachedSectionStatus
= FALSE
;
32 struct FILELIST
*next
;
40 struct FILELIST
*FileList
;
43 CHAR Destination
[MAX_PATH
];
44 CHAR CurrentFile
[MAX_PATH
];
45 CHAR Reserved
[MAX_PATH
];
46 struct FILELIST
*FilterList
;
49 HRESULT (WINAPI
*pfnExtract
)(SESSION
*dest
, LPCSTR szCabName
);
53 GetSystemColorDepth(VOID
)
58 pDevMode
.dmSize
= sizeof(DEVMODE
);
59 pDevMode
.dmDriverExtra
= 0;
61 if (!EnumDisplaySettings(NULL
, ENUM_CURRENT_SETTINGS
, &pDevMode
))
63 /* TODO: Error message */
67 switch (pDevMode
.dmBitsPerPel
)
69 case 32: ColorDepth
= ILC_COLOR32
; break;
70 case 24: ColorDepth
= ILC_COLOR24
; break;
71 case 16: ColorDepth
= ILC_COLOR16
; break;
72 case 8: ColorDepth
= ILC_COLOR8
; break;
73 case 4: ColorDepth
= ILC_COLOR4
; break;
74 default: ColorDepth
= ILC_COLOR
; break;
81 GetWindowWidth(HWND hwnd
)
85 GetWindowRect(hwnd
, &Rect
);
86 return (Rect
.right
- Rect
.left
);
90 GetWindowHeight(HWND hwnd
)
94 GetWindowRect(hwnd
, &Rect
);
95 return (Rect
.bottom
- Rect
.top
);
99 GetClientWindowWidth(HWND hwnd
)
103 GetClientRect(hwnd
, &Rect
);
104 return (Rect
.right
- Rect
.left
);
108 GetClientWindowHeight(HWND hwnd
)
112 GetClientRect(hwnd
, &Rect
);
113 return (Rect
.bottom
- Rect
.top
);
117 CopyTextToClipboard(LPCWSTR lpszText
)
121 if (OpenClipboard(NULL
))
128 cchBuffer
= wcslen(lpszText
) + 1;
129 ClipBuffer
= GlobalAlloc(GMEM_DDESHARE
, cchBuffer
* sizeof(WCHAR
));
130 Buffer
= GlobalLock(ClipBuffer
);
131 hr
= StringCchCopyW(Buffer
, cchBuffer
, lpszText
);
132 GlobalUnlock(ClipBuffer
);
135 SetClipboardData(CF_UNICODETEXT
, ClipBuffer
);
144 WCHAR szText
[MAX_STR_LEN
*3];
146 LoadStringW(hInst
, IDS_WELCOME_TITLE
, szText
, sizeof(szText
) / sizeof(WCHAR
));
147 NewRichEditText(szText
, CFE_BOLD
);
149 LoadStringW(hInst
, IDS_WELCOME_TEXT
, szText
, sizeof(szText
) / sizeof(WCHAR
));
150 InsertRichEditText(szText
, 0);
152 LoadStringW(hInst
, IDS_WELCOME_URL
, szText
, sizeof(szText
) / sizeof(WCHAR
));
153 InsertRichEditText(szText
, CFM_LINK
);
157 ShowPopupMenu(HWND hwnd
, UINT MenuID
, UINT DefaultItem
)
166 hMenu
= LoadMenuW(hInst
, MAKEINTRESOURCEW(MenuID
));
167 hPopupMenu
= GetSubMenu(hMenu
, 0);
170 hPopupMenu
= GetMenu(hwnd
);
172 ZeroMemory(&mii
, sizeof(mii
));
173 mii
.cbSize
= sizeof(mii
);
174 mii
.fMask
= MIIM_STATE
;
175 GetMenuItemInfo(hPopupMenu
, DefaultItem
, FALSE
, &mii
);
177 if (!(mii
.fState
& MFS_GRAYED
))
178 SetMenuDefaultItem(hPopupMenu
, DefaultItem
, FALSE
);
182 SetForegroundWindow(hwnd
);
183 TrackPopupMenu(hPopupMenu
, 0, pt
.x
, pt
.y
, 0, hMainWnd
, NULL
);
190 StartProcess(LPWSTR lpPath
, BOOL Wait
)
192 PROCESS_INFORMATION pi
;
197 ZeroMemory(&si
, sizeof(si
));
199 si
.dwFlags
= STARTF_USESHOWWINDOW
;
200 si
.wShowWindow
= SW_SHOW
;
202 if (!CreateProcessW(NULL
, lpPath
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &si
, &pi
))
207 CloseHandle(pi
.hThread
);
208 if (Wait
) EnableWindow(hMainWnd
, FALSE
);
212 dwRet
= MsgWaitForMultipleObjects(1, &pi
.hProcess
, FALSE
, INFINITE
, QS_ALLEVENTS
);
213 if (dwRet
== WAIT_OBJECT_0
+ 1)
215 while (PeekMessageW(&msg
, NULL
, 0, 0, PM_REMOVE
))
217 TranslateMessage(&msg
);
218 DispatchMessage(&msg
);
223 if (dwRet
== WAIT_OBJECT_0
|| dwRet
== WAIT_FAILED
)
228 CloseHandle(pi
.hProcess
);
232 EnableWindow(hMainWnd
, TRUE
);
233 SetForegroundWindow(hMainWnd
);
241 GetStorageDirectory(PWCHAR lpDirectory
, DWORD cch
)
246 if (!SHGetSpecialFolderPathW(NULL
, lpDirectory
, CSIDL_LOCAL_APPDATA
, TRUE
))
249 if (FAILED(StringCchCatW(lpDirectory
, cch
, L
"\\rapps")))
252 if (!CreateDirectoryW(lpDirectory
, NULL
) &&
253 GetLastError() != ERROR_ALREADY_EXISTS
)
262 ExtractFilesFromCab(LPWSTR lpCabName
, LPWSTR lpOutputPath
)
264 HINSTANCE hCabinetDll
;
265 CHAR szCabName
[MAX_PATH
];
269 hCabinetDll
= LoadLibraryW(L
"cabinet.dll");
272 pfnExtract
= (void *) GetProcAddress(hCabinetDll
, "Extract");
275 ZeroMemory(&Dest
, sizeof(SESSION
));
277 WideCharToMultiByte(CP_ACP
, 0, lpOutputPath
, -1, Dest
.Destination
, MAX_PATH
, NULL
, NULL
);
278 WideCharToMultiByte(CP_ACP
, 0, lpCabName
, -1, szCabName
, MAX_PATH
, NULL
, NULL
);
279 Dest
.Operation
= EXTRACT_FILLFILELIST
;
281 Result
= pfnExtract(&Dest
, szCabName
);
284 Dest
.Operation
= EXTRACT_EXTRACTFILES
;
285 Result
= pfnExtract(&Dest
, szCabName
);
288 FreeLibrary(hCabinetDll
);
293 FreeLibrary(hCabinetDll
);
302 WCHAR szBuf
[MAX_PATH
] = L
"SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\ReactOS Application Manager";
303 WCHAR szPath
[MAX_PATH
];
304 DWORD dwCategoryNum
= 1;
305 DWORD dwDisp
, dwData
;
308 if (!SettingsInfo
.bLogEnabled
) return;
310 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE
,
312 REG_OPTION_NON_VOLATILE
,
313 KEY_WRITE
, NULL
, &hKey
, &dwDisp
) != ERROR_SUCCESS
)
318 if (!GetModuleFileName(NULL
, szPath
, sizeof(szPath
) / sizeof(szPath
[0])))
321 if (RegSetValueExW(hKey
,
326 (DWORD
)(wcslen(szPath
) + 1) * sizeof(WCHAR
)) != ERROR_SUCCESS
)
332 dwData
= EVENTLOG_ERROR_TYPE
| EVENTLOG_WARNING_TYPE
|
333 EVENTLOG_INFORMATION_TYPE
;
335 if (RegSetValueExW(hKey
,
340 sizeof(DWORD
)) != ERROR_SUCCESS
)
346 if (RegSetValueExW(hKey
,
347 L
"CategoryMessageFile",
351 (DWORD
)(wcslen(szPath
) + 1) * sizeof(WCHAR
)) != ERROR_SUCCESS
)
357 if (RegSetValueExW(hKey
,
361 (LPBYTE
)&dwCategoryNum
,
362 sizeof(DWORD
)) != ERROR_SUCCESS
)
370 hLog
= RegisterEventSourceW(NULL
, L
"ReactOS Application Manager");
377 if (hLog
) DeregisterEventSource(hLog
);
382 WriteLogMessage(WORD wType
, DWORD dwEventID
, LPWSTR lpMsg
)
384 if (!SettingsInfo
.bLogEnabled
) return TRUE
;
386 if (!ReportEventW(hLog
,
403 LPWSTR
GetINIFullPath(LPCWSTR lpFileName
)
405 WCHAR szDir
[MAX_PATH
];
406 static WCHAR szBuffer
[MAX_PATH
];
408 GetStorageDirectory(szDir
, _countof(szDir
));
409 StringCbPrintfW(szBuffer
, sizeof(szBuffer
), L
"%ls\\rapps\\%ls", szDir
, lpFileName
);
415 UINT
ParserGetString(LPCWSTR lpKeyName
, LPWSTR lpReturnedString
, UINT nSize
, LPCWSTR lpFileName
)
417 PWSTR lpFullFileName
= GetINIFullPath(lpFileName
);
418 LPSTR lpRequiredBuf
= HeapAlloc(GetProcessHeap(), 0, nSize
);
424 /* we don't have cached section strings for the current system language, create them */
425 if(bCachedSectionStatus
== FALSE
)
427 WCHAR szLocale
[4 + 1];
430 /* find out what is the current system lang code (e.g. "0a") and append it to SectionLocale */
431 GetLocaleInfoW(GetUserDefaultLCID(), LOCALE_ILANGUAGE
,
432 szLocale
, _countof(szLocale
));
434 StringCbCatW(szCachedINISectionLocale
, sizeof(szCachedINISectionLocale
), szLocale
);
436 /* copy the locale-dependent string into the buffer of the future neutral one */
437 StringCbCopyW(szCachedINISectionLocaleNeutral
,
438 sizeof(szCachedINISectionLocale
),
439 szCachedINISectionLocale
);
441 /* turn "Section.0c0a" into "Section.0a", keeping just the neutral lang part */
442 len
= wcslen(szCachedINISectionLocale
);
444 memmove((szCachedINISectionLocaleNeutral
+ len
) - 4,
445 (szCachedINISectionLocaleNeutral
+ len
) - 2,
446 (2 * sizeof(WCHAR
)) + sizeof(UNICODE_NULL
));
448 /* finally, mark us as cache-friendly for the next time */
449 bCachedSectionStatus
= TRUE
;
452 /* 1st - find localized strings (e.g. "Section.0c0a") */
453 dwResult
= GetPrivateProfileStringW(szCachedINISectionLocale
,
463 /* 2nd - if they weren't present check for neutral sub-langs/ generic translations (e.g. "Section.0a") */
464 dwResult
= GetPrivateProfileStringW(szCachedINISectionLocaleNeutral
,
474 /* 3rd - if they weren't present fallback to standard english strings (just "Section") */
475 dwResult
= GetPrivateProfileStringW(L
"Section",
484 HeapFree(GetProcessHeap(), 0, lpRequiredBuf
);
490 /* get rid of the dynamically allocated ANSI buffer */
491 HeapFree(GetProcessHeap(), 0, lpRequiredBuf
);
496 UINT
ParserGetInt(LPCWSTR lpKeyName
, LPCWSTR lpFileName
)
499 UNICODE_STRING BufferW
;
502 /* grab the text version of our entry */
503 if (!ParserGetString(lpKeyName
, Buffer
, _countof(Buffer
), lpFileName
))
509 /* convert it to an actual integer */
510 RtlInitUnicodeString(&BufferW
, Buffer
);
511 RtlUnicodeStringToInteger(&BufferW
, 0, &Result
);