1
0

OBSBasic_Updater.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. #include <dialogs/OBSWhatsNew.hpp>
  18. #ifdef _WIN32
  19. #include <utility/AutoUpdateThread.hpp>
  20. #endif
  21. #ifdef ENABLE_SPARKLE_UPDATER
  22. #include <utility/MacUpdateThread.hpp>
  23. #include <utility/OBSSparkle.hpp>
  24. #endif
  25. #if defined(_WIN32) || defined(WHATSNEW_ENABLED)
  26. #include <utility/WhatsNewBrowserInitThread.hpp>
  27. #include <utility/models/whatsnew.hpp>
  28. #endif
  29. #ifdef BROWSER_AVAILABLE
  30. #include <browser-panel.hpp>
  31. #endif
  32. #include <qt-wrappers.hpp>
  33. #ifdef _WIN32
  34. #define UPDATE_CHECK_INTERVAL (60 * 60 * 24 * 4) /* 4 days */
  35. #endif
  36. struct QCef;
  37. struct QCefCookieManager;
  38. extern QCef *cef;
  39. extern QCefCookieManager *panel_cookies;
  40. using namespace std;
  41. namespace {
  42. QPointer<OBSWhatsNew> obsWhatsNew;
  43. template<typename OBSRef> struct SignalContainer {
  44. OBSRef ref;
  45. vector<shared_ptr<OBSSignal>> handlers;
  46. };
  47. } // namespace
  48. void OBSBasic::ReceivedIntroJson(const QString &text)
  49. {
  50. #ifdef WHATSNEW_ENABLED
  51. if (closing)
  52. return;
  53. WhatsNewList items;
  54. try {
  55. nlohmann::json json = nlohmann::json::parse(text.toStdString());
  56. items = json.get<WhatsNewList>();
  57. } catch (nlohmann::json::exception &e) {
  58. blog(LOG_WARNING, "Parsing whatsnew data failed: %s", e.what());
  59. return;
  60. }
  61. std::string info_url;
  62. int info_increment = -1;
  63. /* check to see if there's an info page for this version */
  64. for (const WhatsNewItem &item : items) {
  65. if (item.os) {
  66. WhatsNewPlatforms platforms = *item.os;
  67. #ifdef _WIN32
  68. if (!platforms.windows)
  69. continue;
  70. #elif defined(__APPLE__)
  71. if (!platforms.macos)
  72. continue;
  73. #else
  74. if (!platforms.linux)
  75. continue;
  76. #endif
  77. }
  78. int major = 0;
  79. int minor = 0;
  80. sscanf(item.version.c_str(), "%d.%d", &major, &minor);
  81. if (major == LIBOBS_API_MAJOR_VER && minor == LIBOBS_API_MINOR_VER &&
  82. item.RC == OBS_RELEASE_CANDIDATE && item.Beta == OBS_BETA) {
  83. info_url = item.url;
  84. info_increment = item.increment;
  85. }
  86. }
  87. /* this version was not found, or no info for this version */
  88. if (info_increment == -1) {
  89. return;
  90. }
  91. #if OBS_RELEASE_CANDIDATE > 0
  92. constexpr const char *lastInfoVersion = "InfoLastRCVersion";
  93. #elif OBS_BETA > 0
  94. constexpr const char *lastInfoVersion = "InfoLastBetaVersion";
  95. #else
  96. constexpr const char *lastInfoVersion = "InfoLastVersion";
  97. #endif
  98. constexpr uint64_t currentVersion = (uint64_t)LIBOBS_API_VER << 16ULL | OBS_RELEASE_CANDIDATE << 8ULL |
  99. OBS_BETA;
  100. uint64_t lastVersion = config_get_uint(App()->GetAppConfig(), "General", lastInfoVersion);
  101. int current_version_increment = -1;
  102. if ((lastVersion & ~0xFFFF0000ULL) < (currentVersion & ~0xFFFF0000ULL)) {
  103. config_set_int(App()->GetAppConfig(), "General", "InfoIncrement", -1);
  104. config_set_uint(App()->GetAppConfig(), "General", lastInfoVersion, currentVersion);
  105. } else {
  106. current_version_increment = config_get_int(App()->GetAppConfig(), "General", "InfoIncrement");
  107. }
  108. if (info_increment <= current_version_increment) {
  109. return;
  110. }
  111. config_set_int(App()->GetAppConfig(), "General", "InfoIncrement", info_increment);
  112. config_save_safe(App()->GetAppConfig(), "tmp", nullptr);
  113. cef->init_browser();
  114. WhatsNewBrowserInitThread *wnbit = new WhatsNewBrowserInitThread(QT_UTF8(info_url.c_str()));
  115. connect(wnbit, &WhatsNewBrowserInitThread::Result, this, &OBSBasic::ShowWhatsNew, Qt::QueuedConnection);
  116. whatsNewInitThread.reset(wnbit);
  117. whatsNewInitThread->start();
  118. #else
  119. UNUSED_PARAMETER(text);
  120. #endif
  121. }
  122. void OBSBasic::ShowWhatsNew(const QString &url)
  123. {
  124. #ifdef BROWSER_AVAILABLE
  125. if (closing)
  126. return;
  127. if (obsWhatsNew) {
  128. obsWhatsNew->close();
  129. }
  130. obsWhatsNew = new OBSWhatsNew(this, QT_TO_UTF8(url));
  131. #else
  132. UNUSED_PARAMETER(url);
  133. #endif
  134. }
  135. void OBSBasic::TimedCheckForUpdates()
  136. {
  137. if (App()->IsUpdaterDisabled())
  138. return;
  139. if (!config_get_bool(App()->GetAppConfig(), "General", "EnableAutoUpdates"))
  140. return;
  141. #if defined(ENABLE_SPARKLE_UPDATER)
  142. CheckForUpdates(false);
  143. #elif _WIN32
  144. long long lastUpdate = config_get_int(App()->GetAppConfig(), "General", "LastUpdateCheck");
  145. uint32_t lastVersion = config_get_int(App()->GetAppConfig(), "General", "LastVersion");
  146. if (lastVersion < LIBOBS_API_VER) {
  147. lastUpdate = 0;
  148. config_set_int(App()->GetAppConfig(), "General", "LastUpdateCheck", 0);
  149. }
  150. long long t = (long long)time(nullptr);
  151. long long secs = t - lastUpdate;
  152. if (secs > UPDATE_CHECK_INTERVAL)
  153. CheckForUpdates(false);
  154. #endif
  155. }
  156. void OBSBasic::CheckForUpdates(bool manualUpdate)
  157. {
  158. #if _WIN32
  159. ui->actionCheckForUpdates->setEnabled(false);
  160. ui->actionRepair->setEnabled(false);
  161. if (updateCheckThread && updateCheckThread->isRunning())
  162. return;
  163. updateCheckThread.reset(new AutoUpdateThread(manualUpdate));
  164. updateCheckThread->start();
  165. #elif defined(ENABLE_SPARKLE_UPDATER)
  166. ui->actionCheckForUpdates->setEnabled(false);
  167. if (updateCheckThread && updateCheckThread->isRunning())
  168. return;
  169. MacUpdateThread *mut = new MacUpdateThread(manualUpdate);
  170. connect(mut, &MacUpdateThread::Result, this, &OBSBasic::MacBranchesFetched, Qt::QueuedConnection);
  171. updateCheckThread.reset(mut);
  172. updateCheckThread->start();
  173. #else
  174. UNUSED_PARAMETER(manualUpdate);
  175. #endif
  176. }
  177. void OBSBasic::MacBranchesFetched(const QString &branch, bool manualUpdate)
  178. {
  179. #ifdef ENABLE_SPARKLE_UPDATER
  180. static OBSSparkle *updater;
  181. if (!updater) {
  182. updater = new OBSSparkle(QT_TO_UTF8(branch), ui->actionCheckForUpdates);
  183. return;
  184. }
  185. updater->setBranch(QT_TO_UTF8(branch));
  186. updater->checkForUpdates(manualUpdate);
  187. #else
  188. UNUSED_PARAMETER(branch);
  189. UNUSED_PARAMETER(manualUpdate);
  190. #endif
  191. }