[RAPPS] StringCbCatW => StringCchCatW, sizeof() => _countof()
[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 */
9
10 #include "rapps.h"
11
12 #include <atlbase.h>
13 #include <atlcom.h>
14 #include <shellapi.h>
15
16 HWND hMainWnd;
17 HINSTANCE hInst;
18 INT SelectedEnumType = ENUM_ALL_COMPONENTS;
19 SETTINGS_INFO SettingsInfo;
20
21 WCHAR szSearchPattern[MAX_STR_LEN] = L"";
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
52 FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
53 {
54 pSettingsInfo->bSaveWndPos = TRUE;
55 pSettingsInfo->bUpdateAtStart = FALSE;
56 pSettingsInfo->bLogEnabled = TRUE;
57 if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, pSettingsInfo->szDownloadDir)))
58 {
59 StringCchCatW(pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir), L"\\RAPPS Downloads");
60 }
61 else
62 {
63 ExpandEnvironmentStringsW(L"%SystemDrive%\\RAPPS Downloads",
64 pSettingsInfo->szDownloadDir, _countof(pSettingsInfo->szDownloadDir));
65 }
66
67 pSettingsInfo->bDelInstaller = FALSE;
68
69 pSettingsInfo->Maximized = FALSE;
70 pSettingsInfo->Left = CW_USEDEFAULT;
71 pSettingsInfo->Top = CW_USEDEFAULT;
72 pSettingsInfo->Width = 680;
73 pSettingsInfo->Height = 450;
74
75 pSettingsInfo->Proxy = 0;
76 StringCbCopyW(pSettingsInfo->szProxyServer, sizeof(pSettingsInfo->szProxyServer), L"");
77 StringCbCopyW(pSettingsInfo->szNoProxyFor, sizeof(pSettingsInfo->szNoProxyFor), L"");
78 }
79
80 static BOOL
81 LoadSettings(VOID)
82 {
83 HKEY hKey;
84 DWORD dwSize;
85
86 if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
87 {
88 dwSize = sizeof(SettingsInfo);
89 if (RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE)&SettingsInfo, &dwSize) == ERROR_SUCCESS)
90 {
91 RegCloseKey(hKey);
92 return TRUE;
93 }
94
95 RegCloseKey(hKey);
96 }
97
98 return FALSE;
99 }
100
101 VOID
102 SaveSettings(HWND hwnd)
103 {
104 WINDOWPLACEMENT wp;
105 HKEY hKey;
106
107 if (SettingsInfo.bSaveWndPos)
108 {
109 wp.length = sizeof(wp);
110 GetWindowPlacement(hwnd, &wp);
111
112 SettingsInfo.Left = wp.rcNormalPosition.left;
113 SettingsInfo.Top = wp.rcNormalPosition.top;
114 SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
115 SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
116 SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE || (wp.showCmd == SW_SHOWMINIMIZED && (wp.flags & WPF_RESTORETOMAXIMIZED)));
117 }
118
119 if (RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", 0, NULL,
120 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
121 {
122 RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE)&SettingsInfo, sizeof(SettingsInfo));
123 RegCloseKey(hKey);
124 }
125 }
126
127 int WINAPI
128 wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd)
129 {
130 WCHAR szWindowClass[] = L"ROSAPPMGR";
131 HANDLE hMutex = NULL;
132 HACCEL KeyBrd;
133 MSG Msg;
134
135 InitializeAtlModule(hInstance, TRUE);
136
137 switch (GetUserDefaultUILanguage())
138 {
139 case MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT):
140 SetProcessDefaultLayout(LAYOUT_RTL);
141 break;
142
143 default:
144 break;
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 hMainWnd = CreateMainWindow();
171 if (!hMainWnd) goto Exit;
172
173 /* Maximize it if we must */
174 ShowWindow(hMainWnd, (SettingsInfo.bSaveWndPos && SettingsInfo.Maximized ? SW_MAXIMIZE : nShowCmd));
175 UpdateWindow(hMainWnd);
176
177 if (SettingsInfo.bUpdateAtStart)
178 UpdateAppsDB();
179
180 /* Load the menu hotkeys */
181 KeyBrd = LoadAccelerators(NULL, MAKEINTRESOURCE(HOTKEYS));
182
183 /* Message Loop */
184 while (GetMessage(&Msg, NULL, 0, 0))
185 {
186 if (!TranslateAccelerator(hMainWnd, KeyBrd, &Msg))
187 {
188 TranslateMessage(&Msg);
189 DispatchMessage(&Msg);
190 }
191 }
192
193 Exit:
194 if (hMutex)
195 CloseHandle(hMutex);
196
197 InitializeAtlModule(hInstance, FALSE);
198
199 return 0;
200 }