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.

157 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../jucer_Headers.h"
  18. #include "jucer_DownloadCompileEngineThread.h"
  19. #include "../LiveBuildEngine/projucer_CompileEngineDLL.h"
  20. bool DownloadCompileEngineThread::downloadAndInstall()
  21. {
  22. DownloadCompileEngineThread d;
  23. if (d.runThread())
  24. {
  25. if (d.result.failed())
  26. return withError (d.result.getErrorMessage());
  27. return true;
  28. }
  29. if (d.cancelledByUser)
  30. return false;
  31. return withError (d.result.getErrorMessage());
  32. }
  33. DownloadCompileEngineThread::DownloadCompileEngineThread()
  34. : ThreadWithProgressWindow ("Downloading live-build engine", true, true),
  35. result (Result::ok()), cancelledByUser (false)
  36. {
  37. }
  38. void DownloadCompileEngineThread::threadComplete (bool userPressedCancel)
  39. {
  40. cancelledByUser = userPressedCancel;
  41. }
  42. void DownloadCompileEngineThread::run()
  43. {
  44. setProgress (-1.0);
  45. setStatusMessage ("Downloading...");
  46. MemoryBlock zipData;
  47. result = download (zipData);
  48. if (result.failed())
  49. return;
  50. setStatusMessage ("Installing...");
  51. File installFolder = getInstallFolder();
  52. if (! installFolder.createDirectory())
  53. {
  54. result = Result::fail ("Install error: cannot create target directory");
  55. return;
  56. }
  57. result = install (zipData, installFolder);
  58. }
  59. Result DownloadCompileEngineThread::download (MemoryBlock& dest)
  60. {
  61. int statusCode = 302;
  62. const int timeoutMs = 10000;
  63. StringPairArray responseHeaders;
  64. URL url = getDownloadUrl();
  65. ScopedPointer<InputStream> in = url.createInputStream (false, nullptr, nullptr,
  66. String(), timeoutMs, &responseHeaders,
  67. &statusCode, 0);
  68. if (in == nullptr || statusCode != 200)
  69. return Result::fail ("Download error: cannot establish connection");
  70. MemoryOutputStream mo (dest, true);
  71. int64 size = in->getTotalLength();
  72. int64 bytesReceived = -1;
  73. String msg("Downloading... (123)");
  74. for (int64 pos = 0; pos < size; pos += bytesReceived)
  75. {
  76. setStatusMessage (msg.replace ("123", File::descriptionOfSizeInBytes (pos)));
  77. if (threadShouldExit())
  78. return Result::fail ("Download error: operation interrupted");
  79. bytesReceived = mo.writeFromInputStream (*in, 8192);
  80. if (bytesReceived == 0)
  81. return Result::fail ("Download error: lost connection");
  82. }
  83. return Result::ok();
  84. }
  85. Result DownloadCompileEngineThread::install (const MemoryBlock& data, File& targetFolder)
  86. {
  87. MemoryInputStream input (data, false);
  88. ZipFile zip (input);
  89. if (zip.getNumEntries() == 0)
  90. return Result::fail ("Install error: downloaded file is corrupt");
  91. if (threadShouldExit())
  92. return Result::fail ("Install error: operation interrupted");
  93. return zip.uncompressTo (targetFolder);
  94. }
  95. URL DownloadCompileEngineThread::getDownloadUrl()
  96. {
  97. String urlStub ("http://assets.roli.com/juce/JUCECompileEngine_");
  98. #if JUCE_MAC
  99. urlStub << "osx_";
  100. #elif JUCE_WINDOWS
  101. urlStub << "windows_";
  102. #else
  103. jassertfalse;
  104. #endif
  105. return urlStub + ProjectInfo::versionString + ".zip";
  106. }
  107. File DownloadCompileEngineThread::getInstallFolder()
  108. {
  109. return CompileEngineDLL::getVersionedUserAppSupportFolder();
  110. }
  111. bool DownloadCompileEngineThread::withError(const String& msg)
  112. {
  113. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  114. "Download and install", msg);
  115. return false;
  116. }