You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
1.9KB

  1. #include <updater.hpp>
  2. #include <settings.hpp>
  3. #include <app/common.hpp>
  4. #include <network.hpp>
  5. #include <system.hpp>
  6. #include <app.hpp>
  7. #include <window.hpp>
  8. #include <asset.hpp>
  9. #include <thread>
  10. namespace rack {
  11. namespace updater {
  12. std::string version;
  13. std::string changelogUrl;
  14. float progress = 0.f;
  15. static std::string downloadUrl;
  16. static void checkVersion() {
  17. std::string versionUrl = app::API_URL + "/version";
  18. json_t *resJ = network::requestJson(network::METHOD_GET, versionUrl, NULL);
  19. if (!resJ) {
  20. WARN("Request for version failed");
  21. return;
  22. }
  23. DEFER({
  24. json_decref(resJ);
  25. });
  26. json_t *versionJ = json_object_get(resJ, "version");
  27. if (versionJ)
  28. version = json_string_value(versionJ);
  29. json_t *changelogUrlJ = json_object_get(resJ, "changelogUrl");
  30. if (changelogUrlJ)
  31. changelogUrl = json_string_value(changelogUrlJ);
  32. json_t *downloadUrlsJ = json_object_get(resJ, "downloadUrls");
  33. if (downloadUrlsJ) {
  34. json_t *downloadUrlJ = json_object_get(downloadUrlsJ, app::APP_ARCH.c_str());
  35. if (downloadUrlJ)
  36. downloadUrl = json_string_value(downloadUrlJ);
  37. }
  38. }
  39. void init() {
  40. if (!settings::devMode) {
  41. std::thread t([] {
  42. checkVersion();
  43. });
  44. t.detach();
  45. }
  46. }
  47. void update() {
  48. if (downloadUrl == "")
  49. return;
  50. #if defined ARCH_WIN
  51. // Download and launch the installer on Windows
  52. std::string filename = string::filename(network::urlPath(downloadUrl));
  53. std::string path = asset::user(filename);
  54. INFO("Download update %s to %s", downloadUrl.c_str(), path.c_str());
  55. network::requestDownload(downloadUrl, path, &progress);
  56. INFO("Launching update %s", path.c_str());
  57. system::runProcessDetached(path);
  58. #else
  59. // Open the browser on Mac and Linux. The user will know what to do.
  60. system::openBrowser(downloadUrl);
  61. #endif
  62. APP->window->close();
  63. }
  64. bool isUpdateAvailable() {
  65. return (version != "") && (version != app::APP_VERSION);
  66. }
  67. } // namespace updater
  68. } // namespace rack