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.

273 lines
7.6KB

  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
  19. {
  20. #if JUCE_CONTENT_SHARING
  21. //==============================================================================
  22. class ContentSharer::PrepareImagesThread : private Thread
  23. {
  24. public:
  25. PrepareImagesThread (ContentSharer& cs, const Array<Image>& imagesToUse,
  26. ImageFileFormat* imageFileFormatToUse)
  27. : Thread ("ContentSharer::PrepareImagesThread"),
  28. owner (cs),
  29. images (imagesToUse),
  30. imageFileFormat (imageFileFormatToUse == nullptr ? new PNGImageFormat()
  31. : imageFileFormatToUse),
  32. extension (imageFileFormat->getFormatName().toLowerCase())
  33. {
  34. startThread();
  35. }
  36. ~PrepareImagesThread() override
  37. {
  38. signalThreadShouldExit();
  39. waitForThreadToExit (10000);
  40. }
  41. private:
  42. void run() override
  43. {
  44. for (const auto& image : images)
  45. {
  46. if (threadShouldExit())
  47. return;
  48. File tempFile = File::createTempFile (extension);
  49. if (! tempFile.create().wasOk())
  50. break;
  51. std::unique_ptr<FileOutputStream> outputStream (tempFile.createOutputStream());
  52. if (outputStream == nullptr)
  53. break;
  54. if (imageFileFormat->writeImageToStream (image, *outputStream))
  55. owner.temporaryFiles.add (tempFile);
  56. }
  57. finish();
  58. }
  59. void finish()
  60. {
  61. MessageManager::callAsync ([this]() { owner.filesToSharePrepared(); });
  62. }
  63. ContentSharer& owner;
  64. const Array<Image> images;
  65. std::unique_ptr<ImageFileFormat> imageFileFormat;
  66. String extension;
  67. };
  68. //==============================================================================
  69. class ContentSharer::PrepareDataThread : private Thread
  70. {
  71. public:
  72. PrepareDataThread (ContentSharer& cs, const MemoryBlock& mb)
  73. : Thread ("ContentSharer::PrepareDataThread"),
  74. owner (cs),
  75. data (mb)
  76. {
  77. startThread();
  78. }
  79. ~PrepareDataThread() override
  80. {
  81. signalThreadShouldExit();
  82. waitForThreadToExit (10000);
  83. }
  84. private:
  85. void run() override
  86. {
  87. File tempFile = File::createTempFile ("data");
  88. if (tempFile.create().wasOk())
  89. {
  90. if (auto outputStream = std::unique_ptr<FileOutputStream> (tempFile.createOutputStream()))
  91. {
  92. size_t pos = 0;
  93. size_t totalSize = data.getSize();
  94. while (pos < totalSize)
  95. {
  96. if (threadShouldExit())
  97. return;
  98. size_t numToWrite = std::min ((size_t) 8192, totalSize - pos);
  99. outputStream->write (data.begin() + pos, numToWrite);
  100. pos += numToWrite;
  101. }
  102. owner.temporaryFiles.add (tempFile);
  103. }
  104. }
  105. finish();
  106. }
  107. void finish()
  108. {
  109. MessageManager::callAsync ([this]() { owner.filesToSharePrepared(); });
  110. }
  111. ContentSharer& owner;
  112. const MemoryBlock data;
  113. };
  114. #endif
  115. //==============================================================================
  116. JUCE_IMPLEMENT_SINGLETON (ContentSharer)
  117. ContentSharer::ContentSharer() {}
  118. ContentSharer::~ContentSharer() { clearSingletonInstance(); }
  119. void ContentSharer::shareFiles ([[maybe_unused]] const Array<URL>& files,
  120. std::function<void (bool, const String&)> callbackToUse)
  121. {
  122. #if JUCE_CONTENT_SHARING
  123. startNewShare (callbackToUse);
  124. pimpl->shareFiles (files);
  125. #else
  126. // Content sharing is not available on this platform!
  127. jassertfalse;
  128. if (callbackToUse)
  129. callbackToUse (false, "Content sharing is not available on this platform!");
  130. #endif
  131. }
  132. #if JUCE_CONTENT_SHARING
  133. void ContentSharer::startNewShare (std::function<void (bool, const String&)> callbackToUse)
  134. {
  135. // You should not start another sharing operation before the previous one is finished.
  136. // Forcibly stopping a previous sharing operation is rarely a good idea!
  137. jassert (pimpl == nullptr);
  138. pimpl.reset();
  139. prepareDataThread = nullptr;
  140. prepareImagesThread = nullptr;
  141. deleteTemporaryFiles();
  142. // You need to pass a valid callback.
  143. jassert (callbackToUse);
  144. callback = std::move (callbackToUse);
  145. pimpl.reset (createPimpl());
  146. }
  147. #endif
  148. void ContentSharer::shareText ([[maybe_unused]] const String& text,
  149. std::function<void (bool, const String&)> callbackToUse)
  150. {
  151. #if JUCE_CONTENT_SHARING
  152. startNewShare (callbackToUse);
  153. pimpl->shareText (text);
  154. #else
  155. // Content sharing is not available on this platform!
  156. jassertfalse;
  157. if (callbackToUse)
  158. callbackToUse (false, "Content sharing is not available on this platform!");
  159. #endif
  160. }
  161. void ContentSharer::shareImages ([[maybe_unused]] const Array<Image>& images,
  162. std::function<void (bool, const String&)> callbackToUse,
  163. [[maybe_unused]] ImageFileFormat* imageFileFormatToUse)
  164. {
  165. #if JUCE_CONTENT_SHARING
  166. startNewShare (callbackToUse);
  167. prepareImagesThread.reset (new PrepareImagesThread (*this, images, imageFileFormatToUse));
  168. #else
  169. // Content sharing is not available on this platform!
  170. jassertfalse;
  171. if (callbackToUse)
  172. callbackToUse (false, "Content sharing is not available on this platform!");
  173. #endif
  174. }
  175. #if JUCE_CONTENT_SHARING
  176. void ContentSharer::filesToSharePrepared()
  177. {
  178. Array<URL> urls;
  179. for (const auto& tempFile : temporaryFiles)
  180. urls.add (URL (tempFile));
  181. prepareImagesThread = nullptr;
  182. prepareDataThread = nullptr;
  183. pimpl->shareFiles (urls);
  184. }
  185. #endif
  186. void ContentSharer::shareData ([[maybe_unused]] const MemoryBlock& mb,
  187. std::function<void (bool, const String&)> callbackToUse)
  188. {
  189. #if JUCE_CONTENT_SHARING
  190. startNewShare (callbackToUse);
  191. prepareDataThread.reset (new PrepareDataThread (*this, mb));
  192. #else
  193. if (callbackToUse)
  194. callbackToUse (false, "Content sharing not available on this platform!");
  195. #endif
  196. }
  197. void ContentSharer::sharingFinished (bool succeeded, const String& errorDescription)
  198. {
  199. deleteTemporaryFiles();
  200. std::function<void (bool, String)> cb;
  201. std::swap (cb, callback);
  202. String error (errorDescription);
  203. #if JUCE_CONTENT_SHARING
  204. pimpl.reset();
  205. #endif
  206. if (cb)
  207. cb (succeeded, error);
  208. }
  209. void ContentSharer::deleteTemporaryFiles()
  210. {
  211. for (auto& f : temporaryFiles)
  212. f.deleteFile();
  213. temporaryFiles.clear();
  214. }
  215. } // namespace juce