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.

212 lines
7.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. namespace juce::detail
  19. {
  20. /*
  21. Instances of this type can show and dismiss a content sharer.
  22. This is an interface rather than a concrete type so that platforms can pick an implementation at
  23. runtime if necessary.
  24. */
  25. struct ScopedContentSharerInterface
  26. {
  27. virtual ~ScopedContentSharerInterface() = default;
  28. /* Shows the content sharer.
  29. When the content sharer exits normally, it should send the result to the passed-in function.
  30. The passed-in function is safe to call from any thread at any time.
  31. */
  32. virtual void runAsync (ContentSharer::Callback callback)
  33. {
  34. jassertfalse;
  35. NullCheckedInvocation::invoke (callback, false, "Content sharing not available on this platform!");
  36. }
  37. /* Forcefully closes the content sharer.
  38. This will be called when the content sharer handle has fallen out of scope.
  39. If the content sharer has already been closed by the user, this shouldn't do anything.
  40. */
  41. virtual void close() {}
  42. /* Implemented differently for each platform. */
  43. static std::unique_ptr<ScopedContentSharerInterface> shareFiles (const Array<URL>&, Component*);
  44. static std::unique_ptr<ScopedContentSharerInterface> shareText (const String&, Component*);
  45. /* Implemented below. */
  46. static std::unique_ptr<ScopedContentSharerInterface> shareImages (const Array<Image>&, std::unique_ptr<ImageFileFormat>, Component*);
  47. static std::unique_ptr<ScopedContentSharerInterface> shareData (MemoryBlock, Component*);
  48. };
  49. class TemporaryFilesDecorator : public ScopedContentSharerInterface,
  50. private AsyncUpdater
  51. {
  52. public:
  53. explicit TemporaryFilesDecorator (Component* parentIn)
  54. : parent (parentIn) {}
  55. void runAsync (ContentSharer::Callback cb) override
  56. {
  57. callback = std::move (cb);
  58. task = std::async (std::launch::async, [this]
  59. {
  60. std::tie (temporaryFiles, error) = prepareTemporaryFiles();
  61. triggerAsyncUpdate();
  62. });
  63. }
  64. void close() override
  65. {
  66. if (inner != nullptr)
  67. inner->close();
  68. }
  69. private:
  70. virtual std::tuple<Array<URL>, String> prepareTemporaryFiles() const = 0;
  71. void handleAsyncUpdate() override
  72. {
  73. if (error.isNotEmpty())
  74. {
  75. NullCheckedInvocation::invoke (callback, false, error);
  76. return;
  77. }
  78. inner = shareFiles (temporaryFiles, parent);
  79. if (inner == nullptr)
  80. {
  81. NullCheckedInvocation::invoke (callback, false, TRANS ("Failed to create file sharer"));
  82. return;
  83. }
  84. inner->runAsync (callback);
  85. }
  86. Array<URL> temporaryFiles;
  87. String error;
  88. std::unique_ptr<ScopedContentSharerInterface> inner;
  89. ContentSharer::Callback callback;
  90. std::future<void> task;
  91. Component* parent = nullptr;
  92. };
  93. std::unique_ptr<ScopedContentSharerInterface> ScopedContentSharerInterface::shareImages (const Array<Image>& images,
  94. std::unique_ptr<ImageFileFormat> format,
  95. Component* parent)
  96. {
  97. class Decorator : public TemporaryFilesDecorator
  98. {
  99. public:
  100. Decorator (Array<Image> imagesIn, std::unique_ptr<ImageFileFormat> formatIn, Component* parentIn)
  101. : TemporaryFilesDecorator (parentIn), images (std::move (imagesIn)), format (std::move (formatIn)) {}
  102. private:
  103. std::tuple<Array<URL>, String> prepareTemporaryFiles() const override
  104. {
  105. const auto extension = format->getFormatName().toLowerCase();
  106. Array<URL> result;
  107. for (const auto& image : images)
  108. {
  109. File tempFile = File::createTempFile (extension);
  110. if (! tempFile.create().wasOk())
  111. return { Array<URL>{}, TRANS ("Failed to create temporary file") };
  112. std::unique_ptr<FileOutputStream> outputStream (tempFile.createOutputStream());
  113. if (outputStream == nullptr)
  114. return { Array<URL>{}, TRANS ("Failed to open temporary file for writing") };
  115. if (format->writeImageToStream (image, *outputStream))
  116. result.add (URL (tempFile));
  117. }
  118. for (const auto& url : result)
  119. jassertquiet (url.isLocalFile() && url.getLocalFile().existsAsFile());
  120. return { std::move (result), String{} };
  121. }
  122. Array<Image> images;
  123. std::unique_ptr<ImageFileFormat> format;
  124. };
  125. return std::make_unique<Decorator> (images,
  126. format == nullptr ? std::make_unique<PNGImageFormat>() : std::move (format),
  127. parent);
  128. }
  129. std::unique_ptr<ScopedContentSharerInterface> ScopedContentSharerInterface::shareData (MemoryBlock mb, Component* parent)
  130. {
  131. class Decorator : public TemporaryFilesDecorator
  132. {
  133. public:
  134. Decorator (MemoryBlock mbIn, Component* parentIn)
  135. : TemporaryFilesDecorator (parentIn), mb (std::move (mbIn)) {}
  136. private:
  137. std::tuple<Array<URL>, String> prepareTemporaryFiles() const override
  138. {
  139. File tempFile = File::createTempFile ("data");
  140. if (! tempFile.create().wasOk())
  141. return { Array<URL>{}, TRANS ("Failed to create temporary file") };
  142. std::unique_ptr<FileOutputStream> outputStream (tempFile.createOutputStream());
  143. if (outputStream == nullptr)
  144. return { Array<URL>{}, TRANS ("Failed to open temporary file for writing") };
  145. size_t pos = 0;
  146. size_t totalSize = mb.getSize();
  147. while (pos < totalSize)
  148. {
  149. size_t numToWrite = std::min ((size_t) 8192, totalSize - pos);
  150. if (! outputStream->write (mb.begin() + pos, numToWrite))
  151. return { Array<URL>{}, TRANS ("Failed to write to temporary file") };
  152. pos += numToWrite;
  153. }
  154. return { Array<URL> { URL (tempFile) }, String{} };
  155. }
  156. MemoryBlock mb;
  157. };
  158. return std::make_unique<Decorator> (std::move (mb), parent);
  159. }
  160. } // namespace juce::detail