[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 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
132 InitializeAtlModule(hInstance, TRUE);
133
134 if (GetUserDefaultUILanguage() == MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT))
135 {
136 SetProcessDefaultLayout(LAYOUT_RTL);
137 }
138
139 hInst = hInstance;
140
141 hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
142 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
143 {
144 /* If already started, it is found its window */
145 HWND hWindow = FindWindowW(szWindowClass, NULL);
146
147 /* Activate window */
148 ShowWindow(hWindow, SW_SHOWNORMAL);
149 SetForegroundWindow(hWindow);
150 return 1;
151 }
152
153 if (!LoadSettings())
154 {
155 FillDefaultSettings(&SettingsInfo);
156 }
157
158 InitLogs();
159 InitCommonControls();
160
161 //skip window creation if there were some keys
162 if (!CmdParser(lpCmdLine))
163 {
164 hMainWnd = CreateMainWindow();
165 if (hMainWnd)
166 {
167 /* Maximize it if we must */
168 ShowWindow(hMainWnd, (SettingsInfo.bSaveWndPos && SettingsInfo.Maximized ? SW_MAXIMIZE : nShowCmd));
169 UpdateWindow(hMainWnd);
170
171 //TODO: get around the ugliness
172 if (SettingsInfo.bUpdateAtStart)
173 GetAvailableApps()->UpdateAppsDB();
174
175 /* Load the menu hotkeys */
176 KeyBrd = LoadAcceleratorsW(NULL, MAKEINTRESOURCEW(HOTKEYS));
177
178 /* Message Loop */
179 while (GetMessageW(&Msg, NULL, 0, 0))
180 {
181 if (!TranslateAcceleratorW(hMainWnd, KeyBrd, &Msg))
182 {
183 TranslateMessage(&Msg);
184 DispatchMessageW(&Msg);
185 }
186 }
187 }
188 }
189
190 if (hMutex)
191 CloseHandle(hMutex);
192
193 InitializeAtlModule(hInstance, FALSE);
194
195 return 0;
196 }