OBSBasic_Updater.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 (isClosing()) {
  52. return;
  53. }
  54. WhatsNewList items;
  55. try {
  56. nlohmann::json json = nlohmann::json::parse(text.toStdString());
  57. items = json.get<WhatsNewList>();
  58. } catch (nlohmann::json::exception &e) {
  59. blog(LOG_WARNING, "Parsing whatsnew data failed: %s", e.what());
  60. return;
  61. }
  62. std::string info_url;
  63. int info_increment = -1;
  64. /* check to see if there's an info page for this version */
  65. for (const WhatsNewItem &item : items) {
  66. if (item.os) {
  67. WhatsNewPlatforms platforms = *item.os;
  68. #ifdef _WIN32
  69. if (!platforms.windows)
  70. continue;
  71. #elif defined(__APPLE__)
  72. if (!platforms.macos)
  73. continue;
  74. #else
  75. if (!platforms.linux)
  76. continue;
  77. #endif
  78. }
  79. int major = 0;
  80. int minor = 0;
  81. sscanf(item.version.c_str(), "%d.%d", &major, &minor);
  82. if (major == LIBOBS_API_MAJOR_VER && minor == LIBOBS_API_MINOR_VER &&
  83. item.RC == OBS_RELEASE_CANDIDATE && item.Beta == OBS_BETA) {
  84. info_url = item.url;
  85. info_increment = item.increment;
  86. }
  87. }
  88. /* this version was not found, or no info for this version */
  89. if (info_increment == -1) {
  90. return;
  91. }
  92. #if OBS_RELEASE_CANDIDATE > 0
  93. constexpr const char *lastInfoVersion = "InfoLastRCVersion";
  94. #elif OBS_BETA > 0
  95. constexpr const char *lastInfoVersion = "InfoLastBetaVersion";
  96. #else
  97. constexpr const char *lastInfoVersion = "InfoLastVersion";
  98. #endif
  99. constexpr uint64_t currentVersion = (uint64_t)LIBOBS_API_VER << 16ULL | OBS_RELEASE_CANDIDATE << 8ULL |
  100. OBS_BETA;
  101. uint64_t lastVersion = config_get_uint(App()->GetAppConfig(), "General", lastInfoVersion);
  102. int current_version_increment = -1;
  103. if ((lastVersion & ~0xFFFF0000ULL) < (currentVersion & ~0xFFFF0000ULL)) {
  104. config_set_int(App()->GetAppConfig(), "General", "InfoIncrement", -1);
  105. config_set_uint(App()->GetAppConfig(), "General", lastInfoVersion, currentVersion);
  106. } else {
  107. current_version_increment = config_get_int(App()->GetAppConfig(), "General", "InfoIncrement");
  108. }
  109. if (info_increment <= current_version_increment) {
  110. return;
  111. }
  112. config_set_int(App()->GetAppConfig(), "General", "InfoIncrement", info_increment);
  113. config_save_safe(App()->GetAppConfig(), "tmp", nullptr);
  114. cef->init_browser();
  115. WhatsNewBrowserInitThread *wnbit = new WhatsNewBrowserInitThread(QT_UTF8(info_url.c_str()));
  116. connect(wnbit, &WhatsNewBrowserInitThread::Result, this, &OBSBasic::ShowWhatsNew, Qt::QueuedConnection);
  117. whatsNewInitThread.reset(wnbit);
  118. whatsNewInitThread->start();
  119. #else
  120. UNUSED_PARAMETER(text);
  121. #endif
  122. }
  123. void OBSBasic::ShowWhatsNew(const QString &url)
  124. {
  125. #ifdef BROWSER_AVAILABLE
  126. if (isClosing()) {
  127. return;
  128. }
  129. if (obsWhatsNew) {
  130. obsWhatsNew->close();
  131. }
  132. obsWhatsNew = new OBSWhatsNew(this, QT_TO_UTF8(url));
  133. #else
  134. UNUSED_PARAMETER(url);
  135. #endif
  136. }
  137. void OBSBasic::TimedCheckForUpdates()
  138. {
  139. if (App()->IsUpdaterDisabled())
  140. return;
  141. if (!config_get_bool(App()->GetAppConfig(), "General", "EnableAutoUpdates"))
  142. return;
  143. #if defined(ENABLE_SPARKLE_UPDATER)
  144. CheckForUpdates(false);
  145. #elif _WIN32
  146. long long lastUpdate = config_get_int(App()->GetAppConfig(), "General", "LastUpdateCheck");
  147. uint32_t lastVersion = config_get_int(App()->GetAppConfig(), "General", "LastVersion");
  148. if (lastVersion < LIBOBS_API_VER) {
  149. lastUpdate = 0;
  150. config_set_int(App()->GetAppConfig(), "General", "LastUpdateCheck", 0);
  151. }
  152. long long t = (long long)time(nullptr);
  153. long long secs = t - lastUpdate;
  154. if (secs > UPDATE_CHECK_INTERVAL)
  155. CheckForUpdates(false);
  156. #endif
  157. }
  158. void OBSBasic::CheckForUpdates(bool manualUpdate)
  159. {
  160. #if _WIN32
  161. ui->actionCheckForUpdates->setEnabled(false);
  162. ui->actionRepair->setEnabled(false);
  163. if (updateCheckThread && updateCheckThread->isRunning())
  164. return;
  165. updateCheckThread.reset(new AutoUpdateThread(manualUpdate));
  166. updateCheckThread->start();
  167. #elif defined(ENABLE_SPARKLE_UPDATER)
  168. ui->actionCheckForUpdates->setEnabled(false);
  169. if (updateCheckThread && updateCheckThread->isRunning())
  170. return;
  171. MacUpdateThread *mut = new MacUpdateThread(manualUpdate);
  172. connect(mut, &MacUpdateThread::Result, this, &OBSBasic::MacBranchesFetched, Qt::QueuedConnection);
  173. updateCheckThread.reset(mut);
  174. updateCheckThread->start();
  175. #else
  176. UNUSED_PARAMETER(manualUpdate);
  177. #endif
  178. }
  179. void OBSBasic::MacBranchesFetched(const QString &branch, bool manualUpdate)
  180. {
  181. #ifdef ENABLE_SPARKLE_UPDATER
  182. static OBSSparkle *updater;
  183. if (!updater) {
  184. updater = new OBSSparkle(QT_TO_UTF8(branch), ui->actionCheckForUpdates);
  185. return;
  186. }
  187. updater->setBranch(QT_TO_UTF8(branch));
  188. updater->checkForUpdates(manualUpdate);
  189. #else
  190. UNUSED_PARAMETER(branch);
  191. UNUSED_PARAMETER(manualUpdate);
  192. #endif
  193. }