a24e5cfd49d7032067fdcd4beb3a2cad52f61520
[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 ATL::CStringW::CopyChars(pSettingsInfo->szDownloadDir,
73 _countof(pSettingsInfo->szDownloadDir),
74 szDownloadDir.GetString(),
75 szDownloadDir.GetLength() + 1);
76
77 pSettingsInfo->bDelInstaller = FALSE;
78 pSettingsInfo->Maximized = FALSE;
79 pSettingsInfo->Left = CW_USEDEFAULT;
80 pSettingsInfo->Top = CW_USEDEFAULT;
81 pSettingsInfo->Width = 680;
82 pSettingsInfo->Height = 450;
83 pSettingsInfo->Proxy = 0;
84
85 pSettingsInfo->szProxyServer[0] = UNICODE_NULL;
86 pSettingsInfo->szNoProxyFor[0] = UNICODE_NULL;
87 }
88
89 static BOOL
90 LoadSettings(VOID)
91 {
92 HKEY hKey;
93 DWORD dwSize;
94
95 if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
96 {
97 dwSize = sizeof(SettingsInfo);
98 if (RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE) &SettingsInfo, &dwSize) == ERROR_SUCCESS)
99 {
100 RegCloseKey(hKey);
101 return TRUE;
102 }
103
104 RegCloseKey(hKey);
105 }
106
107 return FALSE;
108 }
109
110 VOID
111 SaveSettings(HWND hwnd)
112 {
113 WINDOWPLACEMENT wp;
114 HKEY hKey;
115
116 if (SettingsInfo.bSaveWndPos)
117 {
118 wp.length = sizeof(wp);
119 GetWindowPlacement(hwnd, &wp);
120
121 SettingsInfo.Left = wp.rcNormalPosition.left;
122 SettingsInfo.Top = wp.rcNormalPosition.top;
123 SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
124 SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
125 SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE || (wp.showCmd == SW_SHOWMINIMIZED && (wp.flags & WPF_RESTORETOMAXIMIZED)));
126 }
127
128 if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, NULL,
129 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
130 {
131 RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE) &SettingsInfo, sizeof(SettingsInfo));
132 RegCloseKey(hKey);
133 }
134 }
135
136 int WINAPI
137 wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
138 {
139 WCHAR szWindowClass[] = L"ROSAPPMGR";
140 HANDLE hMutex = NULL;
141 HACCEL KeyBrd;
142 MSG Msg;
143
144 InitializeAtlModule(hInstance, TRUE);
145
146 switch (GetUserDefaultUILanguage())
147 {
148 case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
149 SetProcessDefaultLayout(LAYOUT_RTL);
150 break;
151
152 default:
153 break;
154 }
155
156 hInst = hInstance;
157
158 hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
159 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
160 {
161 /* If already started, it is found its window */
162 HWND hWindow = FindWindowW(szWindowClass, NULL);
163
164 /* Activate window */
165 ShowWindow(hWindow, SW_SHOWNORMAL);
166 SetForegroundWindow(hWindow);
167 return 1;
168 }
169
170 if (!LoadSettings())
171 {
172 FillDefaultSettings(&SettingsInfo);
173 }
174
175 InitLogs();
176
177 InitCommonControls();
178
179 hMainWnd = CreateMainWindow();
180 if (!hMainWnd) goto Exit;
181
182 /* Maximize it if we must */
183 ShowWindow(hMainWnd, (SettingsInfo.bSaveWndPos && SettingsInfo.Maximized ? SW_MAXIMIZE : nShowCmd));
184 UpdateWindow(hMainWnd);
185
186 //TODO: get around the ugliness
187 if (SettingsInfo.bUpdateAtStart)
188 GetAvailableApps()->UpdateAppsDB();
189
190 /* Load the menu hotkeys */
191 KeyBrd = LoadAcceleratorsW(NULL, MAKEINTRESOURCE(HOTKEYS));
192
193 /* Message Loop */
194 while (GetMessage(&Msg, NULL, 0, 0))
195 {
196 if (!TranslateAcceleratorW(hMainWnd, KeyBrd, &Msg))
197 {
198 TranslateMessage(&Msg);
199 DispatchMessageW(&Msg);
200 }
201 }
202
203 Exit:
204 if (hMutex)
205 CloseHandle(hMutex);
206
207 InitializeAtlModule(hInstance, FALSE);
208
209 return 0;
210 }