The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

167 lines
4.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include "../Application/jucer_Headers.h"
  19. #include "jucer_DownloadCompileEngineThread.h"
  20. #include "../LiveBuildEngine/jucer_CompileEngineDLL.h"
  21. #include "../Utility/Helpers/jucer_VersionInfo.h"
  22. //==============================================================================
  23. bool DownloadCompileEngineThread::downloadAndInstall()
  24. {
  25. DownloadCompileEngineThread d;
  26. if (d.runThread())
  27. {
  28. if (d.result.failed())
  29. return withError (d.result.getErrorMessage());
  30. return true;
  31. }
  32. if (d.cancelledByUser)
  33. return false;
  34. return withError (d.result.getErrorMessage());
  35. }
  36. DownloadCompileEngineThread::DownloadCompileEngineThread()
  37. : ThreadWithProgressWindow ("Downloading live-build engine", true, true),
  38. result (Result::ok()), cancelledByUser (false)
  39. {
  40. }
  41. void DownloadCompileEngineThread::threadComplete (bool userPressedCancel)
  42. {
  43. cancelledByUser = userPressedCancel;
  44. }
  45. void DownloadCompileEngineThread::run()
  46. {
  47. setProgress (-1.0);
  48. setStatusMessage ("Downloading...");
  49. MemoryBlock zipData;
  50. result = download (zipData);
  51. if (result.failed())
  52. return;
  53. setStatusMessage ("Installing...");
  54. File installFolder = getInstallFolder();
  55. if (! installFolder.createDirectory())
  56. {
  57. result = Result::fail ("Install error: cannot create target directory");
  58. return;
  59. }
  60. result = install (zipData, installFolder);
  61. }
  62. Result DownloadCompileEngineThread::download (MemoryBlock& dest)
  63. {
  64. auto info = VersionInfo::fetchFromUpdateServer (ProjectInfo::versionString);
  65. if (info == nullptr)
  66. return Result::fail ("Download error: cannot communicate with server");
  67. auto requiredAssetName = []
  68. {
  69. String name ("JUCECompileEngine_");
  70. #if JUCE_MAC
  71. name << "osx_";
  72. #elif JUCE_WINDOWS
  73. name << "windows_";
  74. #else
  75. jassertfalse;
  76. #endif
  77. return name + ProjectInfo::versionString + ".zip";
  78. }();
  79. for (auto& asset : info->assets)
  80. {
  81. if (asset.name == requiredAssetName)
  82. {
  83. int statusCode = 0;
  84. auto in = VersionInfo::createInputStreamForAsset (asset, statusCode);
  85. if (in == nullptr || statusCode != 200)
  86. return Result::fail ("Download error: cannot establish connection");
  87. MemoryOutputStream mo (dest, true);
  88. int64 size = in->getTotalLength();
  89. int64 bytesReceived = -1;
  90. String msg("Downloading... (123)");
  91. for (int64 pos = 0; pos < size; pos += bytesReceived)
  92. {
  93. setStatusMessage (msg.replace ("123", File::descriptionOfSizeInBytes (pos)));
  94. if (threadShouldExit())
  95. return Result::fail ("Download error: operation interrupted");
  96. bytesReceived = mo.writeFromInputStream (*in, 8192);
  97. if (bytesReceived == 0)
  98. return Result::fail ("Download error: lost connection");
  99. }
  100. return Result::ok();
  101. }
  102. }
  103. return Result::fail ("Download error: no downloads available");
  104. }
  105. Result DownloadCompileEngineThread::install (const MemoryBlock& data, File& targetFolder)
  106. {
  107. MemoryInputStream input (data, false);
  108. ZipFile zip (input);
  109. if (zip.getNumEntries() == 0)
  110. return Result::fail ("Install error: downloaded file is corrupt");
  111. if (threadShouldExit())
  112. return Result::fail ("Install error: operation interrupted");
  113. return zip.uncompressTo (targetFolder);
  114. }
  115. File DownloadCompileEngineThread::getInstallFolder()
  116. {
  117. return CompileEngineDLL::getVersionedUserAppSupportFolder();
  118. }
  119. bool DownloadCompileEngineThread::withError(const String& msg)
  120. {
  121. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  122. "Download and install", msg);
  123. return false;
  124. }