[RAPPS] Extensive conversion to ATL and general improvements
[reactos.git] / reactos / base / applications / rapps / winmain.cpp
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/rapps/winmain.cpp
5 * PURPOSE: Main program
6 * PROGRAMMERS: Dmitry Chapyshev (dmitry@reactos.org)
7 * Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
8 * Alexander Shaposhnikov (chaez.san@gmail.com)
9 */
10
11 #include "rapps.h"
12
13 #include <atlbase.h>
14 #include <atlcom.h>
15 #include <shellapi.h>
16
17 HWND hMainWnd;
18 HINSTANCE hInst;
19 INT SelectedEnumType = ENUM_ALL_COMPONENTS;
20 SETTINGS_INFO SettingsInfo;
21
22 ATL::CStringW szSearchPattern;
23
24 class CRAppsModule : public CComModule
25 {
26 public:
27 };
28
29 BEGIN_OBJECT_MAP(ObjectMap)
30 END_OBJECT_MAP()
31
32 CRAppsModule gModule;
33 CAtlWinModule gWinModule;
34
35 //void *operator new (size_t, void *buf)
36 //{
37 // return buf;
38 //}
39
40 static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
41 {
42 if (bInitialize)
43 {
44 gModule.Init(ObjectMap, hInstance, NULL);
45 }
46 else
47 {
48 gModule.Term();
49 }
50 }
51
52 VOID
53 FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
54 {
55 ATL::CStringW szDownloadDir;
56 pSettingsInfo->bSaveWndPos = TRUE;
57 pSettingsInfo->bUpdateAtStart = FALSE;
58 pSettingsInfo->bLogEnabled = TRUE;
59
60 if (FAILED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szDownloadDir.GetBuffer(MAX_PATH))))
61 {
62 szDownloadDir.ReleaseBuffer();
63 if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
64 {
65 szDownloadDir = L"C:";
66 }
67 }
68 else
69 szDownloadDir.ReleaseBuffer();
70
71 szDownloadDir += L"\\RAPPS Downloads";
72 StringCchCopyW(pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir), szDownloadDir.GetString());
73
74 pSettingsInfo->bDelInstaller = FALSE;
75 pSettingsInfo->Maximized = FALSE;
76 pSettingsInfo->Left = CW_USEDEFAULT;
77 pSettingsInfo->Top = CW_USEDEFAULT;
78 pSettingsInfo->Width = 680;
79 pSettingsInfo->Height = 450;
80 pSettingsInfo->Proxy = 0;
81
82 StringCbCopyW(pSettingsInfo->szProxyServer, sizeof(pSettingsInfo->szProxyServer), L"");
83 StringCbCopyW(pSettingsInfo->szNoProxyFor, sizeof(pSettingsInfo->szNoProxyFor), L"");
84 }
85
86 static BOOL
87 LoadSettings(VOID)
88 {
89 HKEY hKey;
90 DWORD dwSize;
91
92 if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
93 {
94 dwSize = sizeof(SettingsInfo);
95 if (RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE) &SettingsInfo, &dwSize) == ERROR_SUCCESS)
96 {
97 RegCloseKey(hKey);
98 return TRUE;
99 }
100
101 RegCloseKey(hKey);
102 }
103
104 return FALSE;
105 }
106
107 VOID
108 SaveSettings(HWND hwnd)
109 {
110 WINDOWPLACEMENT wp;
111 HKEY hKey;
112
113 if (SettingsInfo.bSaveWndPos)
114 {
115 wp.length = sizeof(wp);
116 GetWindowPlacement(hwnd, &wp);
117
118 SettingsInfo.Left = wp.rcNormalPosition.left;
119 SettingsInfo.Top = wp.rcNormalPosition.top;
120 SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
121 SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
122 SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE || (wp.showCmd == SW_SHOWMINIMIZED && (wp.flags & WPF_RESTORETOMAXIMIZED)));
123 }
124
125 if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, NULL,
126 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
127 {
128 RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE) &SettingsInfo, sizeof(SettingsInfo));
129 RegCloseKey(hKey);
130 }
131 }
132
133 int WINAPI
134 wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
135 {
136 WCHAR szWindowClass[] = L"ROSAPPMGR";
137 HANDLE hMutex = NULL;
138 HACCEL KeyBrd;
139 MSG Msg;
140
141 InitializeAtlModule(hInstance, TRUE);
142
143 switch (GetUserDefaultUILanguage())
144 {
145 case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
146 SetProcessDefaultLayout(LAYOUT_RTL);
147 break;
148
149 default:
150 break;
151 }
152
153 hInst = hInstance;
154
155 hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
156 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
157 {
158 /* If already started, it is found its window */
159 HWND hWindow = FindWindowW(szWindowClass, NULL);
160
161 /* Activate window */
162 ShowWindow(hWindow, SW_SHOWNORMAL);
163 SetForegroundWindow(hWindow);
164 return 1;
165 }
166
167 if (!LoadSettings())
168 {
169 FillDefaultSettings(&SettingsInfo);
170 }
171
172 InitLogs();
173
174 InitCommonControls();
175
176 hMainWnd = CreateMainWindow();
177 if (!hMainWnd) goto Exit;
178
179 /* Maximize it if we must */
180 ShowWindow(hMainWnd, (SettingsInfo.bSaveWndPos && SettingsInfo.Maximized ? SW_MAXIMIZE : nShowCmd));
181 UpdateWindow(hMainWnd);
182
183 if (SettingsInfo.bUpdateAtStart)
184 UpdateAppsDB();
185
186 /* Load the menu hotkeys */
187 KeyBrd = LoadAccelerators(NULL, MAKEINTRESOURCE(HOTKEYS));
188
189 /* Message Loop */
190 while (GetMessage(&Msg, NULL, 0, 0))
191 {
192 if (!TranslateAccelerator(hMainWnd, KeyBrd, &Msg))
193 {
194 TranslateMessage(&Msg);
195 DispatchMessage(&Msg);
196 }
197 }
198
199 Exit:
200 if (hMutex)
201 CloseHandle(hMutex);
202
203 InitializeAtlModule(hInstance, FALSE);
204
205 return 0;
206 }