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.

268 lines
7.6KB

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