5660db81acd66c02e76e6299df254691945cc272
[reactos.git] / base / applications / rapps / winmain.cpp
1 /*
2 * PROJECT: ReactOS Applications Manager
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * FILE: base/applications/rapps/winmain.cpp
5 * PURPOSE: Main program
6 * COPYRIGHT: Copyright 2009 Dmitry Chapyshev (dmitry@reactos.org)
7 * Copyright 2015 Ismael Ferreras Morezuelas (swyterzone+ros@gmail.com)
8 * Copyright 2017 Alexander Shaposhnikov (sanchaez@reactos.org)
9 */
10 #include "rapps.h"
11
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 BOOL bIsFirstLaunch;
130
131 InitializeAtlModule(hInstance, TRUE);
132
133 if (GetUserDefaultUILanguage() == MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT))
134 {
135 SetProcessDefaultLayout(LAYOUT_RTL);
136 }
137
138 hInst = hInstance;
139
140 hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
141 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
142 {
143 /* If already started, it is found its window */
144 HWND hWindow = FindWindowW(szWindowClass, NULL);
145
146 /* Activate window */
147 ShowWindow(hWindow, SW_SHOWNORMAL);
148 SetForegroundWindow(hWindow);
149 return 1;
150 }
151 bIsFirstLaunch = !LoadSettings();
152 if (bIsFirstLaunch)
153 {
154 FillDefaultSettings(&SettingsInfo);
155 }
156
157 InitLogs();
158 InitCommonControls();
159
160 // skip window creation if there were some keys
161 if (!UseCmdParameters(GetCommandLineW()))
162 {
163 if (SettingsInfo.bUpdateAtStart || bIsFirstLaunch)
164 CAvailableApps::ForceUpdateAppsDB();
165
166 ShowMainWindow(nShowCmd);
167 }
168
169 if (hMutex)
170 CloseHandle(hMutex);
171
172 InitializeAtlModule(hInstance, FALSE);
173
174 return 0;
175 }