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