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