[RAPPS] CMainWindow: make szSearchPattern a member
[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 class CRAppsModule : public CComModule
22 {
23 public:
24 };
25
26 BEGIN_OBJECT_MAP(ObjectMap)
27 END_OBJECT_MAP()
28
29 CRAppsModule gModule;
30 CAtlWinModule gWinModule;
31
32 static VOID InitializeAtlModule(HINSTANCE hInstance, BOOL bInitialize)
33 {
34 if (bInitialize)
35 {
36 gModule.Init(ObjectMap, hInstance, NULL);
37 }
38 else
39 {
40 gModule.Term();
41 }
42 }
43
44 VOID FillDefaultSettings(PSETTINGS_INFO pSettingsInfo)
45 {
46 ATL::CStringW szDownloadDir;
47 ZeroMemory(pSettingsInfo, sizeof(SETTINGS_INFO));
48
49 pSettingsInfo->bSaveWndPos = TRUE;
50 pSettingsInfo->bUpdateAtStart = FALSE;
51 pSettingsInfo->bLogEnabled = TRUE;
52
53 if (FAILED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, szDownloadDir.GetBuffer(MAX_PATH))))
54 {
55 szDownloadDir.ReleaseBuffer();
56 if (!szDownloadDir.GetEnvironmentVariableW(L"SystemDrive"))
57 {
58 szDownloadDir = L"C:";
59 }
60 }
61 else
62 {
63 szDownloadDir.ReleaseBuffer();
64 }
65
66 szDownloadDir += L"\\RAPPS Downloads";
67 ATL::CStringW::CopyChars(pSettingsInfo->szDownloadDir,
68 _countof(pSettingsInfo->szDownloadDir),
69 szDownloadDir.GetString(),
70 szDownloadDir.GetLength() + 1);
71
72 pSettingsInfo->bDelInstaller = FALSE;
73 pSettingsInfo->Maximized = FALSE;
74 pSettingsInfo->Left = CW_USEDEFAULT;
75 pSettingsInfo->Top = CW_USEDEFAULT;
76 pSettingsInfo->Width = 680;
77 pSettingsInfo->Height = 450;
78 }
79
80 static BOOL LoadSettings()
81 {
82 ATL::CRegKey RegKey;
83 DWORD dwSize;
84 BOOL bResult = FALSE;
85 if (RegKey.Open(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", KEY_READ) == ERROR_SUCCESS)
86 {
87 dwSize = sizeof(SettingsInfo);
88 bResult = (RegKey.QueryBinaryValue(L"Settings", (PVOID) &SettingsInfo, &dwSize) == ERROR_SUCCESS);
89
90 RegKey.Close();
91 }
92
93 return bResult;
94 }
95
96 VOID SaveSettings(HWND hwnd)
97 {
98 WINDOWPLACEMENT wp;
99 ATL::CRegKey RegKey;
100
101 if (SettingsInfo.bSaveWndPos)
102 {
103 wp.length = sizeof(wp);
104 GetWindowPlacement(hwnd, &wp);
105
106 SettingsInfo.Left = wp.rcNormalPosition.left;
107 SettingsInfo.Top = wp.rcNormalPosition.top;
108 SettingsInfo.Width = wp.rcNormalPosition.right - wp.rcNormalPosition.left;
109 SettingsInfo.Height = wp.rcNormalPosition.bottom - wp.rcNormalPosition.top;
110 SettingsInfo.Maximized = (wp.showCmd == SW_MAXIMIZE
111 || (wp.showCmd == SW_SHOWMINIMIZED
112 && (wp.flags & WPF_RESTORETOMAXIMIZED)));
113 }
114
115 if (RegKey.Create(HKEY_CURRENT_USER, L"Software\\ReactOS\\rapps", NULL,
116 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, NULL) == ERROR_SUCCESS)
117 {
118 RegKey.SetBinaryValue(L"Settings", (const PVOID) &SettingsInfo, sizeof(SettingsInfo));
119 RegKey.Close();
120 }
121 }
122
123 INT WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, INT nShowCmd)
124 {
125 LPCWSTR szWindowClass = L"ROSAPPMGR";
126 HANDLE hMutex;
127 BOOL bIsFirstLaunch;
128
129 InitializeAtlModule(hInstance, TRUE);
130
131 if (GetUserDefaultUILanguage() == MAKELANGID(LANG_HEBREW, SUBLANG_DEFAULT))
132 {
133 SetProcessDefaultLayout(LAYOUT_RTL);
134 }
135
136 hInst = hInstance;
137
138 hMutex = CreateMutexW(NULL, FALSE, szWindowClass);
139 if ((!hMutex) || (GetLastError() == ERROR_ALREADY_EXISTS))
140 {
141 /* If already started, it is found its window */
142 HWND hWindow = FindWindowW(szWindowClass, NULL);
143
144 /* Activate window */
145 ShowWindow(hWindow, SW_SHOWNORMAL);
146 SetForegroundWindow(hWindow);
147 return 1;
148 }
149 bIsFirstLaunch = !LoadSettings();
150 if (bIsFirstLaunch)
151 {
152 FillDefaultSettings(&SettingsInfo);
153 }
154
155 InitLogs();
156 InitCommonControls();
157
158 // skip window creation if there were some keys
159 if (!UseCmdParameters(GetCommandLineW()))
160 {
161 if (SettingsInfo.bUpdateAtStart || bIsFirstLaunch)
162 CAvailableApps::ForceUpdateAppsDB();
163
164 ShowMainWindow(nShowCmd);
165 }
166
167 if (hMutex)
168 CloseHandle(hMutex);
169
170 InitializeAtlModule(hInstance, FALSE);
171
172 return 0;
173 }