xref: /reactos/base/applications/rapps/winmain.cpp (revision 11345aed)
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     pSettingsInfo->bUseSource = FALSE;
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     PathAppendW(szDownloadDir.GetBuffer(MAX_PATH), L"\\RAPPS Downloads");
67     szDownloadDir.ReleaseBuffer();
68 
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 }
176