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.

152 lines
5.8KB

  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. #pragma once
  20. namespace juce
  21. {
  22. /** A singleton class responsible for sharing content between apps and devices.
  23. You can share text, images, files or an arbitrary data block.
  24. @tags{GUI}
  25. */
  26. class JUCE_API ContentSharer
  27. {
  28. public:
  29. JUCE_DECLARE_SINGLETON (ContentSharer, false)
  30. /** Shares the given files. Each URL should be either a full file path
  31. or it should point to a resource within the application bundle. For
  32. resources on iOS it should be something like "content/image.png" if you
  33. want to specify a file from application bundle located in "content"
  34. directory. On Android you should specify only a filename, without an
  35. extension.
  36. Upon completion you will receive a callback with a sharing result. Note:
  37. Sadly on Android the returned success flag may be wrong as there is no
  38. standard way the sharing targets report if the sharing operation
  39. succeeded. Also, the optional error message is always empty on Android.
  40. */
  41. void shareFiles (const Array<URL>& files,
  42. std::function<void (bool /*success*/, const String& /*error*/)> callback);
  43. /** Shares the given text.
  44. Upon completion you will receive a callback with a sharing result. Note:
  45. Sadly on Android the returned success flag may be wrong as there is no
  46. standard way the sharing targets report if the sharing operation
  47. succeeded. Also, the optional error message is always empty on Android.
  48. */
  49. void shareText (const String& text,
  50. std::function<void (bool /*success*/, const String& /*error*/)> callback);
  51. /** A convenience function to share an image. This is useful when you have images
  52. loaded in memory. The images will be written to temporary files first, so if
  53. you have the images in question stored on disk already call shareFiles() instead.
  54. By default, images will be saved to PNG files, but you can supply a custom
  55. ImageFileFormat to override this. The custom file format will be owned and
  56. deleted by the sharer. e.g.
  57. @code
  58. Graphics g (myImage);
  59. g.setColour (Colours::green);
  60. g.fillEllipse (20, 20, 300, 200);
  61. Array<Image> images;
  62. images.add (myImage);
  63. ContentSharer::getInstance()->shareImages (images, myCallback);
  64. @endcode
  65. Upon completion you will receive a callback with a sharing result. Note:
  66. Sadly on Android the returned success flag may be wrong as there is no
  67. standard way the sharing targets report if the sharing operation
  68. succeeded. Also, the optional error message is always empty on Android.
  69. */
  70. void shareImages (const Array<Image>& images,
  71. std::function<void (bool /*success*/, const String& /*error*/)> callback,
  72. ImageFileFormat* imageFileFormatToUse = nullptr);
  73. /** A convenience function to share arbitrary data. The data will be written
  74. to a temporary file and then that file will be shared. If you have
  75. your data stored on disk already, call shareFiles() instead.
  76. Upon completion you will receive a callback with a sharing result. Note:
  77. Sadly on Android the returned success flag may be wrong as there is no
  78. standard way the sharing targets report if the sharing operation
  79. succeeded. Also, the optional error message is always empty on Android.
  80. */
  81. void shareData (const MemoryBlock& mb,
  82. std::function<void (bool /*success*/, const String& /*error*/)> callback);
  83. private:
  84. ContentSharer();
  85. ~ContentSharer();
  86. Array<File> temporaryFiles;
  87. std::function<void (bool, String)> callback;
  88. #if JUCE_IOS || JUCE_ANDROID
  89. struct Pimpl
  90. {
  91. virtual ~Pimpl() {}
  92. virtual void shareFiles (const Array<URL>& files) = 0;
  93. virtual void shareText (const String& text) = 0;
  94. };
  95. std::unique_ptr<Pimpl> pimpl;
  96. Pimpl* createPimpl();
  97. void startNewShare (std::function<void (bool, const String&)>);
  98. class ContentSharerNativeImpl;
  99. friend class ContentSharerNativeImpl;
  100. class PrepareImagesThread;
  101. friend class PrepareImagesThread;
  102. std::unique_ptr<PrepareImagesThread> prepareImagesThread;
  103. class PrepareDataThread;
  104. friend class PrepareDataThread;
  105. std::unique_ptr<PrepareDataThread> prepareDataThread;
  106. void filesToSharePrepared();
  107. #endif
  108. void deleteTemporaryFiles();
  109. void sharingFinished (bool, const String&);
  110. #if JUCE_ANDROID
  111. friend void* juce_contentSharerOpenFile (void*, void*, void*);
  112. friend void* juce_contentSharerQuery (void*, void*, void*, void*, void*, void*);
  113. friend void* juce_contentSharerGetStreamTypes (void*, void*);
  114. friend void juce_contentSharingCompleted (int);
  115. #endif
  116. };
  117. } // namespace juce