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.

267 lines
7.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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_MODAL_LOOPS_PERMITTED
  21. static bool exeIsAvailable (String executable)
  22. {
  23. ChildProcess child;
  24. if (child.start ("which " + executable))
  25. {
  26. child.waitForProcessToFinish (60 * 1000);
  27. return (child.getExitCode() == 0);
  28. }
  29. return false;
  30. }
  31. class FileChooser::Native : public FileChooser::Pimpl,
  32. private Timer
  33. {
  34. public:
  35. Native (FileChooser& fileChooser, int flags)
  36. : owner (fileChooser),
  37. isDirectory ((flags & FileBrowserComponent::canSelectDirectories) != 0),
  38. isSave ((flags & FileBrowserComponent::saveMode) != 0),
  39. selectMultipleFiles ((flags & FileBrowserComponent::canSelectMultipleItems) != 0),
  40. warnAboutOverwrite ((flags & FileBrowserComponent::warnAboutOverwriting) != 0)
  41. {
  42. const File previousWorkingDirectory (File::getCurrentWorkingDirectory());
  43. // use kdialog for KDE sessions or if zenity is missing
  44. if (exeIsAvailable ("kdialog") && (isKdeFullSession() || ! exeIsAvailable ("zenity")))
  45. addKDialogArgs();
  46. else
  47. addZenityArgs();
  48. }
  49. ~Native() override
  50. {
  51. finish (true);
  52. }
  53. void runModally() override
  54. {
  55. child.start (args, ChildProcess::wantStdOut);
  56. while (child.isRunning())
  57. if (! MessageManager::getInstance()->runDispatchLoopUntil (20))
  58. break;
  59. finish (false);
  60. }
  61. void launch() override
  62. {
  63. child.start (args, ChildProcess::wantStdOut);
  64. startTimer (100);
  65. }
  66. private:
  67. FileChooser& owner;
  68. bool isDirectory, isSave, selectMultipleFiles, warnAboutOverwrite;
  69. ChildProcess child;
  70. StringArray args;
  71. String separator;
  72. void timerCallback() override
  73. {
  74. if (! child.isRunning())
  75. {
  76. stopTimer();
  77. finish (false);
  78. }
  79. }
  80. void finish (bool shouldKill)
  81. {
  82. String result;
  83. Array<URL> selection;
  84. if (shouldKill)
  85. child.kill();
  86. else
  87. result = child.readAllProcessOutput().trim();
  88. if (result.isNotEmpty())
  89. {
  90. StringArray tokens;
  91. if (selectMultipleFiles)
  92. tokens.addTokens (result, separator, "\"");
  93. else
  94. tokens.add (result);
  95. for (auto& token : tokens)
  96. selection.add (URL (File::getCurrentWorkingDirectory().getChildFile (token)));
  97. }
  98. if (! shouldKill)
  99. {
  100. child.waitForProcessToFinish (60 * 1000);
  101. owner.finished (selection);
  102. }
  103. }
  104. static uint64 getTopWindowID() noexcept
  105. {
  106. if (TopLevelWindow* top = TopLevelWindow::getActiveTopLevelWindow())
  107. return (uint64) (pointer_sized_uint) top->getWindowHandle();
  108. return 0;
  109. }
  110. static bool isKdeFullSession()
  111. {
  112. return SystemStats::getEnvironmentVariable ("KDE_FULL_SESSION", String())
  113. .equalsIgnoreCase ("true");
  114. }
  115. void addKDialogArgs()
  116. {
  117. args.add ("kdialog");
  118. if (owner.title.isNotEmpty())
  119. args.add ("--title=" + owner.title);
  120. if (uint64 topWindowID = getTopWindowID())
  121. {
  122. args.add ("--attach");
  123. args.add (String (topWindowID));
  124. }
  125. if (selectMultipleFiles)
  126. {
  127. separator = "\n";
  128. args.add ("--multiple");
  129. args.add ("--separate-output");
  130. args.add ("--getopenfilename");
  131. }
  132. else
  133. {
  134. if (isSave) args.add ("--getsavefilename");
  135. else if (isDirectory) args.add ("--getexistingdirectory");
  136. else args.add ("--getopenfilename");
  137. }
  138. File startPath;
  139. if (owner.startingFile.exists())
  140. {
  141. startPath = owner.startingFile;
  142. }
  143. else if (owner.startingFile.getParentDirectory().exists())
  144. {
  145. startPath = owner.startingFile.getParentDirectory();
  146. }
  147. else
  148. {
  149. startPath = File::getSpecialLocation (File::userHomeDirectory);
  150. if (isSave)
  151. startPath = startPath.getChildFile (owner.startingFile.getFileName());
  152. }
  153. args.add (startPath.getFullPathName());
  154. args.add ("(" + owner.filters.replaceCharacter (';', ' ') + ")");
  155. }
  156. void addZenityArgs()
  157. {
  158. args.add ("zenity");
  159. args.add ("--file-selection");
  160. if (warnAboutOverwrite)
  161. args.add("--confirm-overwrite");
  162. if (owner.title.isNotEmpty())
  163. args.add ("--title=" + owner.title);
  164. if (selectMultipleFiles)
  165. {
  166. separator = ":";
  167. args.add ("--multiple");
  168. args.add ("--separator=" + separator);
  169. }
  170. else
  171. {
  172. if (isDirectory) args.add ("--directory");
  173. if (isSave) args.add ("--save");
  174. }
  175. if (owner.filters.isNotEmpty() && owner.filters != "*" && owner.filters != "*.*")
  176. {
  177. StringArray tokens;
  178. tokens.addTokens (owner.filters, ";,|", "\"");
  179. args.add ("--file-filter=" + tokens.joinIntoString (" "));
  180. }
  181. if (owner.startingFile.isDirectory())
  182. owner.startingFile.setAsCurrentWorkingDirectory();
  183. else if (owner.startingFile.getParentDirectory().exists())
  184. owner.startingFile.getParentDirectory().setAsCurrentWorkingDirectory();
  185. else
  186. File::getSpecialLocation (File::userHomeDirectory).setAsCurrentWorkingDirectory();
  187. auto filename = owner.startingFile.getFileName();
  188. if (! filename.isEmpty())
  189. args.add ("--filename=" + filename);
  190. // supplying the window ID of the topmost window makes sure that Zenity pops up..
  191. if (uint64 topWindowID = getTopWindowID())
  192. setenv ("WINDOWID", String (topWindowID).toRawUTF8(), true);
  193. }
  194. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Native)
  195. };
  196. #endif
  197. bool FileChooser::isPlatformDialogAvailable()
  198. {
  199. #if JUCE_DISABLE_NATIVE_FILECHOOSERS || ! JUCE_MODAL_LOOPS_PERMITTED
  200. return false;
  201. #else
  202. static bool canUseNativeBox = exeIsAvailable ("zenity") || exeIsAvailable ("kdialog");
  203. return canUseNativeBox;
  204. #endif
  205. }
  206. FileChooser::Pimpl* FileChooser::showPlatformDialog (FileChooser& owner, int flags, FilePreviewComponent*)
  207. {
  208. #if JUCE_MODAL_LOOPS_PERMITTED
  209. return new Native (owner, flags);
  210. #else
  211. return nullptr;
  212. #endif
  213. }
  214. } // namespace juce