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.

257 lines
7.4KB

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