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.

168 lines
4.8KB

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