0633200aa97b2425299dcd256f8655c602a5e626
[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_INSTALLED;
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 static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
35 {
36 if (bInitialize)
37 {
38 gModule.Init(ObjectMap, hInstance, NULL);
39 }
40 else
41 {
42 gModule.Term();
43 }
44 }
45
46 VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
47 {
48 ATL::CStringW szDownloadDir;
49 ZeroMemory(pSettingsInfo, sizeof(SETTINGS_INFO));
50
51 pSettingsInfo->bSaveWndPos = TRUE;
52 pSettingsInfo->bUpdateAtStart = FALSE;
53 pSettingsInfo->bLogEnabled = TRUE;
54
55 if (FAILED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szDownloadDir.GetBuffer(MAX_PATH))))
56 {
57 szDownloadDir.ReleaseBuffer();
58 if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
59 {
60 szDownloadDir = L"C:";
61 }
62 }
63 else
64 {
65 szDownloadDir.ReleaseBuffer();
66 }
67
68 szDownloadDir += L"\\RAPPS Downloads";
69 ATL::CStringW::CopyChars(pSettingsInfo->szDownloadDir,
70 _countof(pSettingsInfo->szDownloadDir),
71 szDownloadDir.GetString(),
72 szDownloadDir.GetLength() + 1);
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 }
81
82 static BOOL LoadSettings()
83 {
84 ATL::CRegKey RegKey;
85 DWORD dwSize;
86 BOOL bResult = FALSE;
87 if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", KEY_READ) == ERROR_SUCCESS)
88 {
89 dwSize = sizeof(SettingsInfo);
90 bResult = (RegKey.QueryBinaryValue(L"Settings", (PVOID) &SettingsInfo, &dwSize) == ERROR_SUCCESS);
91
92 RegKey.Close();
93 }
94
95 return bResult;
96 }
97
98 VOID SaveSettings(HWND hwnd)
99 {
100 WINDOWPLACEMENT wp;
101 ATL::CRegKey RegKey;
102
103 if (SettingsInfo.bSaveWndPos)
104 {
105 wp.length = sizeof(wp);
106 GetWindowPlacement(hwnd, &wp);
107
108 SettingsInfo.Left = wp.rcNormalPosition.left;
109 SettingsInfo.Top = wp.rcNormalPosition.top;
110 SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
111 SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
112 SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE
113 || (wp.showCmd == SW_SHOWMINIMIZED
114 && (wp.flags & WPF_RESTORETOMAXIMIZED)));
115 }
116
117 if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", NULL,
118 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL) == ERROR_SUCCESS)
119 {
120 RegKey.SetBinaryValue(L"Settings", (const PVOID) &SettingsInfo, sizeof(SettingsInfo));
121 RegKey.Close();
122 }
123 }
124
125 INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nShowCmd)
126 {
127 LPCWSTR szWindowClass = L"ROSAPPMGR";
128 HANDLE hMutex;
129 HACCEL KeyBrd;
130 MSG Msg;
131 BOOL bIsFirstLaunch;
132
133 InitializeAtlModule(hInstance, TRUE);
134
135 if (GetUserDefaultUILanguage() == MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT))
136 {
137 SetProcessDefaultLayout(LAYOUT_RTL);
138 }
139
140 hInst = hInstance;
141
142 hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
143 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
144 {
145 /* If already started, it is found its window */
146 HWND hWindow = FindWindowW(szWindowClass, NULL);
147
148 /* Activate window */
149 ShowWindow(hWindow, SW_SHOWNORMAL);
150 SetForegroundWindow(hWindow);
151 return 1;
152 }
153 bIsFirstLaunch = !LoadSettings();
154 if (bIsFirstLaunch)
155 {
156 FillDefaultSettings(&SettingsInfo);
157 }
158
159 InitLogs();
160 InitCommonControls();
161
162 // skip window creation if there were some keys
163 if (!CmdParser(lpCmdLine))
164 {
165 if (SettingsInfo.bUpdateAtStart || bIsFirstLaunch)
166 CAvailableApps::ForceUpdateAppsDB();
167
168 hMainWnd = CreateMainWindow();
169
170 if (hMainWnd)
171 {
172 /* Maximize it if we must */
173 ShowWindow(hMainWnd, ((SettingsInfo.bSaveWndPos && SettingsInfo.Maximized) ? SW_MAXIMIZE : nShowCmd));
174 UpdateWindow(hMainWnd);
175
176 /* Load the menu hotkeys */
177 KeyBrd = LoadAcceleratorsW(NULL, MAKEINTRESOURCEW(HOTKEYS));
178
179 /* Message Loop */
180 while (GetMessageW(&Msg, NULL, 0, 0))
181 {
182 if (!TranslateAcceleratorW(hMainWnd, KeyBrd, &Msg))
183 {
184 TranslateMessage(&Msg);
185 DispatchMessageW(&Msg);
186 }
187 }
188 }
189 }
190
191 if (hMutex)
192 CloseHandle(hMutex);
193
194 InitializeAtlModule(hInstance, FALSE);
195
196 return 0;
197 }