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.

160 lines
4.6KB

  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. //==============================================================================
  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. int statusCode = 302;
  65. const int timeoutMs = 10000;
  66. StringPairArray responseHeaders;
  67. URL url = getDownloadUrl();
  68. std::unique_ptr<InputStream> in (url.createInputStream (false, nullptr, nullptr,
  69. String(), timeoutMs, &responseHeaders,
  70. &statusCode, 0));
  71. if (in == nullptr || statusCode != 200)
  72. return Result::fail ("Download error: cannot establish connection");
  73. MemoryOutputStream mo (dest, true);
  74. int64 size = in->getTotalLength();
  75. int64 bytesReceived = -1;
  76. String msg("Downloading... (123)");
  77. for (int64 pos = 0; pos < size; pos += bytesReceived)
  78. {
  79. setStatusMessage (msg.replace ("123", File::descriptionOfSizeInBytes (pos)));
  80. if (threadShouldExit())
  81. return Result::fail ("Download error: operation interrupted");
  82. bytesReceived = mo.writeFromInputStream (*in, 8192);
  83. if (bytesReceived == 0)
  84. return Result::fail ("Download error: lost connection");
  85. }
  86. return Result::ok();
  87. }
  88. Result DownloadCompileEngineThread::install (const MemoryBlock& data, File& targetFolder)
  89. {
  90. MemoryInputStream input (data, false);
  91. ZipFile zip (input);
  92. if (zip.getNumEntries() == 0)
  93. return Result::fail ("Install error: downloaded file is corrupt");
  94. if (threadShouldExit())
  95. return Result::fail ("Install error: operation interrupted");
  96. return zip.uncompressTo (targetFolder);
  97. }
  98. URL DownloadCompileEngineThread::getDownloadUrl()
  99. {
  100. String urlStub ("http://assets.roli.com/juce/JUCECompileEngine_");
  101. #if JUCE_MAC
  102. urlStub << "osx_";
  103. #elif JUCE_WINDOWS
  104. urlStub << "windows_";
  105. #else
  106. jassertfalse;
  107. #endif
  108. return urlStub + ProjectInfo::versionString + ".zip";
  109. }
  110. File DownloadCompileEngineThread::getInstallFolder()
  111. {
  112. return CompileEngineDLL::getVersionedUserAppSupportFolder();
  113. }
  114. bool DownloadCompileEngineThread::withError(const String& msg)
  115. {
  116. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  117. "Download and install", msg);
  118. return false;
  119. }