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.

159 lines
4.5KB

  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 "../jucer_Headers.h"
  20. #include "jucer_DownloadCompileEngineThread.h"
  21. #include "../LiveBuildEngine/projucer_CompileEngineDLL.h"
  22. bool DownloadCompileEngineThread::downloadAndInstall()
  23. {
  24. DownloadCompileEngineThread d;
  25. if (d.runThread())
  26. {
  27. if (d.result.failed())
  28. return withError (d.result.getErrorMessage());
  29. return true;
  30. }
  31. if (d.cancelledByUser)
  32. return false;
  33. return withError (d.result.getErrorMessage());
  34. }
  35. DownloadCompileEngineThread::DownloadCompileEngineThread()
  36. : ThreadWithProgressWindow ("Downloading live-build engine", true, true),
  37. result (Result::ok()), cancelledByUser (false)
  38. {
  39. }
  40. void DownloadCompileEngineThread::threadComplete (bool userPressedCancel)
  41. {
  42. cancelledByUser = userPressedCancel;
  43. }
  44. void DownloadCompileEngineThread::run()
  45. {
  46. setProgress (-1.0);
  47. setStatusMessage ("Downloading...");
  48. MemoryBlock zipData;
  49. result = download (zipData);
  50. if (result.failed())
  51. return;
  52. setStatusMessage ("Installing...");
  53. File installFolder = getInstallFolder();
  54. if (! installFolder.createDirectory())
  55. {
  56. result = Result::fail ("Install error: cannot create target directory");
  57. return;
  58. }
  59. result = install (zipData, installFolder);
  60. }
  61. Result DownloadCompileEngineThread::download (MemoryBlock& dest)
  62. {
  63. int statusCode = 302;
  64. const int timeoutMs = 10000;
  65. StringPairArray responseHeaders;
  66. URL url = getDownloadUrl();
  67. ScopedPointer<InputStream> in = url.createInputStream (false, nullptr, nullptr,
  68. String(), timeoutMs, &responseHeaders,
  69. &statusCode, 0);
  70. if (in == nullptr || statusCode != 200)
  71. return Result::fail ("Download error: cannot establish connection");
  72. MemoryOutputStream mo (dest, true);
  73. int64 size = in->getTotalLength();
  74. int64 bytesReceived = -1;
  75. String msg("Downloading... (123)");
  76. for (int64 pos = 0; pos < size; pos += bytesReceived)
  77. {
  78. setStatusMessage (msg.replace ("123", File::descriptionOfSizeInBytes (pos)));
  79. if (threadShouldExit())
  80. return Result::fail ("Download error: operation interrupted");
  81. bytesReceived = mo.writeFromInputStream (*in, 8192);
  82. if (bytesReceived == 0)
  83. return Result::fail ("Download error: lost connection");
  84. }
  85. return Result::ok();
  86. }
  87. Result DownloadCompileEngineThread::install (const MemoryBlock& data, File& targetFolder)
  88. {
  89. MemoryInputStream input (data, false);
  90. ZipFile zip (input);
  91. if (zip.getNumEntries() == 0)
  92. return Result::fail ("Install error: downloaded file is corrupt");
  93. if (threadShouldExit())
  94. return Result::fail ("Install error: operation interrupted");
  95. return zip.uncompressTo (targetFolder);
  96. }
  97. URL DownloadCompileEngineThread::getDownloadUrl()
  98. {
  99. String urlStub ("http://assets.roli.com/juce/JUCECompileEngine_");
  100. #if JUCE_MAC
  101. urlStub << "osx_";
  102. #elif JUCE_WINDOWS
  103. urlStub << "windows_";
  104. #else
  105. jassertfalse;
  106. #endif
  107. return urlStub + ProjectInfo::versionString + ".zip";
  108. }
  109. File DownloadCompileEngineThread::getInstallFolder()
  110. {
  111. return CompileEngineDLL::getVersionedUserAppSupportFolder();
  112. }
  113. bool DownloadCompileEngineThread::withError(const String& msg)
  114. {
  115. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  116. "Download and install", msg);
  117. return false;
  118. }