Audio plugin host https://kx.studio/carla
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.

274 lines
7.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. #if JUCE_MODAL_LOOPS_PERMITTED
  16. static bool exeIsAvailable (String executable)
  17. {
  18. ChildProcess child;
  19. if (child.start ("which " + executable))
  20. {
  21. child.waitForProcessToFinish (60 * 1000);
  22. return (child.getExitCode() == 0);
  23. }
  24. return false;
  25. }
  26. static bool isSet (int flags, int toCheck)
  27. {
  28. return (flags & toCheck) != 0;
  29. }
  30. class FileChooser::Native : public FileChooser::Pimpl,
  31. private Timer
  32. {
  33. public:
  34. Native (FileChooser& fileChooser, int flags)
  35. : owner (fileChooser),
  36. // kdialog/zenity only support opening either files or directories.
  37. // Files should take precedence, if requested.
  38. isDirectory (isSet (flags, FileBrowserComponent::canSelectDirectories) && ! isSet (flags, FileBrowserComponent::canSelectFiles)),
  39. isSave (isSet (flags, FileBrowserComponent::saveMode)),
  40. selectMultipleFiles (isSet (flags, FileBrowserComponent::canSelectMultipleItems)),
  41. warnAboutOverwrite (isSet (flags, FileBrowserComponent::warnAboutOverwriting))
  42. {
  43. const File previousWorkingDirectory (File::getCurrentWorkingDirectory());
  44. // use kdialog for KDE sessions or if zenity is missing
  45. if (exeIsAvailable ("kdialog") && (isKdeFullSession() || ! exeIsAvailable ("zenity")))
  46. addKDialogArgs();
  47. else
  48. addZenityArgs();
  49. }
  50. ~Native() override
  51. {
  52. finish (true);
  53. }
  54. void runModally() override
  55. {
  56. #if JUCE_MODAL_LOOPS_PERMITTED
  57. child.start (args, ChildProcess::wantStdOut);
  58. while (child.isRunning())
  59. if (! MessageManager::getInstance()->runDispatchLoopUntil (20))
  60. break;
  61. finish (false);
  62. #else
  63. jassertfalse;
  64. #endif
  65. }
  66. void launch() override
  67. {
  68. child.start (args, ChildProcess::wantStdOut);
  69. startTimer (100);
  70. }
  71. private:
  72. FileChooser& owner;
  73. bool isDirectory, isSave, selectMultipleFiles, warnAboutOverwrite;
  74. ChildProcess child;
  75. StringArray args;
  76. String separator;
  77. void timerCallback() override
  78. {
  79. if (! child.isRunning())
  80. {
  81. stopTimer();
  82. finish (false);
  83. }
  84. }
  85. void finish (bool shouldKill)
  86. {
  87. String result;
  88. Array<URL> selection;
  89. if (shouldKill)
  90. child.kill();
  91. else
  92. result = child.readAllProcessOutput().trim();
  93. if (result.isNotEmpty())
  94. {
  95. StringArray tokens;
  96. if (selectMultipleFiles)
  97. tokens.addTokens (result, separator, "\"");
  98. else
  99. tokens.add (result);
  100. for (auto& token : tokens)
  101. selection.add (URL (File::getCurrentWorkingDirectory().getChildFile (token)));
  102. }
  103. if (! shouldKill)
  104. {
  105. child.waitForProcessToFinish (60 * 1000);
  106. owner.finished (selection);
  107. }
  108. }
  109. static uint64 getTopWindowID() noexcept
  110. {
  111. if (TopLevelWindow* top = TopLevelWindow::getActiveTopLevelWindow())
  112. return (uint64) (pointer_sized_uint) top->getWindowHandle();
  113. return 0;
  114. }
  115. static bool isKdeFullSession()
  116. {
  117. return SystemStats::getEnvironmentVariable ("KDE_FULL_SESSION", String())
  118. .equalsIgnoreCase ("true");
  119. }
  120. void addKDialogArgs()
  121. {
  122. args.add ("kdialog");
  123. if (owner.title.isNotEmpty())
  124. args.add ("--title=" + owner.title);
  125. if (uint64 topWindowID = getTopWindowID())
  126. {
  127. args.add ("--attach");
  128. args.add (String (topWindowID));
  129. }
  130. if (selectMultipleFiles)
  131. {
  132. separator = "\n";
  133. args.add ("--multiple");
  134. args.add ("--separate-output");
  135. args.add ("--getopenfilename");
  136. }
  137. else
  138. {
  139. if (isSave) args.add ("--getsavefilename");
  140. else if (isDirectory) args.add ("--getexistingdirectory");
  141. else args.add ("--getopenfilename");
  142. }
  143. File startPath;
  144. if (owner.startingFile.exists())
  145. {
  146. startPath = owner.startingFile;
  147. }
  148. else if (owner.startingFile.getParentDirectory().exists())
  149. {
  150. startPath = owner.startingFile.getParentDirectory();
  151. }
  152. else
  153. {
  154. startPath = File::getSpecialLocation (File::userHomeDirectory);
  155. if (isSave)
  156. startPath = startPath.getChildFile (owner.startingFile.getFileName());
  157. }
  158. args.add (startPath.getFullPathName());
  159. args.add ("(" + owner.filters.replaceCharacter (';', ' ') + ")");
  160. }
  161. void addZenityArgs()
  162. {
  163. args.add ("zenity");
  164. args.add ("--file-selection");
  165. if (warnAboutOverwrite)
  166. args.add("--confirm-overwrite");
  167. if (owner.title.isNotEmpty())
  168. args.add ("--title=" + owner.title);
  169. if (selectMultipleFiles)
  170. {
  171. separator = ":";
  172. args.add ("--multiple");
  173. args.add ("--separator=" + separator);
  174. }
  175. else
  176. {
  177. if (isSave)
  178. args.add ("--save");
  179. }
  180. if (isDirectory)
  181. args.add ("--directory");
  182. if (owner.filters.isNotEmpty() && owner.filters != "*" && owner.filters != "*.*")
  183. {
  184. StringArray tokens;
  185. tokens.addTokens (owner.filters, ";,|", "\"");
  186. args.add ("--file-filter=" + tokens.joinIntoString (" "));
  187. }
  188. if (owner.startingFile.isDirectory())
  189. owner.startingFile.setAsCurrentWorkingDirectory();
  190. else if (owner.startingFile.getParentDirectory().exists())
  191. owner.startingFile.getParentDirectory().setAsCurrentWorkingDirectory();
  192. else
  193. File::getSpecialLocation (File::userHomeDirectory).setAsCurrentWorkingDirectory();
  194. auto filename = owner.startingFile.getFileName();
  195. if (! filename.isEmpty())
  196. args.add ("--filename=" + filename);
  197. // supplying the window ID of the topmost window makes sure that Zenity pops up..
  198. if (uint64 topWindowID = getTopWindowID())
  199. setenv ("WINDOWID", String (topWindowID).toRawUTF8(), true);
  200. }
  201. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Native)
  202. };
  203. #endif
  204. bool FileChooser::isPlatformDialogAvailable()
  205. {
  206. #if JUCE_DISABLE_NATIVE_FILECHOOSERS || ! JUCE_MODAL_LOOPS_PERMITTED
  207. return false;
  208. #else
  209. static bool canUseNativeBox = exeIsAvailable ("zenity") || exeIsAvailable ("kdialog");
  210. return canUseNativeBox;
  211. #endif
  212. }
  213. std::shared_ptr<FileChooser::Pimpl> FileChooser::showPlatformDialog (FileChooser& owner, int flags, FilePreviewComponent*)
  214. {
  215. #if JUCE_MODAL_LOOPS_PERMITTED
  216. return std::make_shared<Native> (owner, flags);
  217. #else
  218. return nullptr;
  219. #endif
  220. }
  221. } // namespace juce