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.

261 lines
7.5KB

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