UpdateCheck.h 4.1 KB

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