1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* UpdateCheck.h
* -------------
* Purpose: Class for easy software update check.
* Notes : (currently none)
* Authors: OpenMPT Devs
* The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
*/
#pragma once
#include "openmpt/all/BuildSettings.hpp"
#include "mpt/uuid/uuid.hpp"
#include <time.h>
#include <atomic>
#include "resource.h"
#include "Settings.h"
OPENMPT_NAMESPACE_BEGIN
#if defined(MPT_ENABLE_UPDATE)
namespace HTTP {
class InternetSession;
}
enum UpdateChannel : uint32
{
UpdateChannelRelease = 1,
UpdateChannelNext = 2,
UpdateChannelDevelopment = 3,
};
struct UpdateCheckResult
{
time_t CheckTime = time_t{};
std::vector<std::byte> json;
bool IsFromCache() const noexcept { return CheckTime == time_t{}; }
};
class CUpdateCheck
{
private:
static std::atomic<int32> s_InstanceCount;
public:
static mpt::ustring GetStatisticsUserInformation(bool shortText);
static std::vector<mpt::ustring> GetDefaultUpdateSigningKeysRootAnchors();
static mpt::ustring GetDefaultAPIURL();
int32 GetNumCurrentRunningInstances();
static bool IsSuitableUpdateMoment();
static void DoAutoUpdateCheck() { StartUpdateCheckAsync(true); }
static void DoManualUpdateCheck() { StartUpdateCheckAsync(false); }
public:
struct Context
{
CWnd *window;
UINT msgStart;
UINT msgProgress;
UINT msgSuccess;
UINT msgFailure;
UINT msgCanceled;
bool autoUpdate;
bool loadPersisted;
std::string statistics;
};
struct Settings
{
int32 periodDays;
UpdateChannel channel;
mpt::PathString persistencePath;
mpt::ustring apiURL;
bool sendStatistics;
mpt::UUID statisticsUUID;
Settings();
};
class Error
: public std::runtime_error
{
private:
CString m_Message;
public:
Error(CString errorMessage);
Error(CString errorMessage, DWORD errorCode);
CString GetMessage() const;
protected:
static CString FormatErrorCode(CString errorMessage, DWORD errorCode);
};
class Cancel
: public std::exception
{
public:
Cancel();
};
static bool IsAutoUpdateFromMessage(WPARAM wparam, LPARAM lparam);
static const UpdateCheckResult &MessageAsResult(WPARAM wparam, LPARAM lparam);
static const CUpdateCheck::Error &MessageAsError(WPARAM wparam, LPARAM lparam);
static void AcknowledgeSuccess(const UpdateCheckResult &result);
static void ShowSuccessGUI(const bool autoUpdate, const UpdateCheckResult &result);
static void ShowFailureGUI(const bool autoUpdate, const CUpdateCheck::Error &error);
public:
// v3
static std::string GetStatisticsDataV3(const Settings &settings); // UTF8
protected:
static void StartUpdateCheckAsync(bool autoUpdate);
struct ThreadFunc
{
CUpdateCheck::Settings settings;
CUpdateCheck::Context context;
ThreadFunc(const CUpdateCheck::Settings &settings, const CUpdateCheck::Context &context);
void operator () ();
};
static void CheckForUpdate(const CUpdateCheck::Settings &settings, const CUpdateCheck::Context &context);
static UpdateCheckResult SearchUpdate(const CUpdateCheck::Context &context, const CUpdateCheck::Settings &settings, const std::string &statistics); // may throw
static void CleanOldUpdates(const CUpdateCheck::Settings &settings, const CUpdateCheck::Context &context);
static void SendStatistics(HTTP::InternetSession &internet, const CUpdateCheck::Settings &settings, const std::string &statistics); // may throw
static UpdateCheckResult SearchUpdateModern(HTTP::InternetSession &internet, const CUpdateCheck::Settings &settings); // may throw
};
class CUpdateSetupDlg: public CPropertyPage
, public ISettingChanged
{
public:
CUpdateSetupDlg();
public:
void SettingChanged(const SettingPath &changedPath) override;
protected:
void DoDataExchange(CDataExchange *pDX) override;
BOOL OnInitDialog() override;
void OnOK() override;
BOOL OnSetActive() override;
afx_msg void OnSettingsChanged();
afx_msg void OnCheckNow();
afx_msg void OnShowStatisticsData(NMHDR * /*pNMHDR*/, LRESULT * /*pResult*/);
void EnableDisableDialog();
DECLARE_MESSAGE_MAP()
private:
SettingChangedNotifyGuard m_SettingChangedNotifyGuard;
CComboBox m_CbnUpdateFrequency;
};
#endif // MPT_ENABLE_UPDATE
OPENMPT_NAMESPACE_END
|