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.

253 lines
7.3KB

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