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.

259 lines
7.4KB

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