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