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.

163 lines
5.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. static bool exeIsAvailable (const char* const executable)
  18. {
  19. ChildProcess child;
  20. const bool ok = child.start ("which " + String (executable))
  21. && child.readAllProcessOutput().trim().isNotEmpty();
  22. child.waitForProcessToFinish (60 * 1000);
  23. return ok;
  24. }
  25. bool FileChooser::isPlatformDialogAvailable()
  26. {
  27. #if JUCE_DISABLE_NATIVE_FILECHOOSERS
  28. return false;
  29. #else
  30. static bool canUseNativeBox = exeIsAvailable ("zenity") || exeIsAvailable ("kdialog");
  31. return canUseNativeBox;
  32. #endif
  33. }
  34. void FileChooser::showPlatformDialog (Array<File>& results,
  35. const String& title,
  36. const File& file,
  37. const String& filters,
  38. bool isDirectory,
  39. bool /* selectsFiles */,
  40. bool isSave,
  41. bool /* warnAboutOverwritingExistingFiles */,
  42. bool selectMultipleFiles,
  43. FilePreviewComponent* /* previewComponent */)
  44. {
  45. String separator;
  46. StringArray args;
  47. const File previousWorkingDirectory (File::getCurrentWorkingDirectory());
  48. const bool isKdeFullSession = SystemStats::getEnvironmentVariable ("KDE_FULL_SESSION", String::empty)
  49. .equalsIgnoreCase ("true");
  50. if (exeIsAvailable ("kdialog") && (isKdeFullSession || ! exeIsAvailable ("zenity")))
  51. {
  52. // use kdialog for KDE sessions or if zenity is missing
  53. args.add ("kdialog");
  54. if (title.isNotEmpty())
  55. args.add ("--title=" + title);
  56. if (selectMultipleFiles)
  57. {
  58. separator = "\n";
  59. args.add ("--multiple");
  60. args.add ("--separate-output");
  61. args.add ("--getopenfilename");
  62. }
  63. else
  64. {
  65. if (isSave) args.add ("--getsavefilename");
  66. else if (isDirectory) args.add ("--getexistingdirectory");
  67. else args.add ("--getopenfilename");
  68. }
  69. String startPath;
  70. if (file.exists())
  71. {
  72. startPath = file.getFullPathName();
  73. }
  74. else if (file.getParentDirectory().exists())
  75. {
  76. startPath = file.getParentDirectory().getFullPathName();
  77. }
  78. else
  79. {
  80. startPath = File::getSpecialLocation (File::userHomeDirectory).getFullPathName();
  81. if (isSave)
  82. startPath += "/" + file.getFileName();
  83. }
  84. args.add (startPath);
  85. args.add (filters.replaceCharacter (';', ' '));
  86. args.add ("2>/dev/null");
  87. }
  88. else
  89. {
  90. // zenity
  91. args.add ("zenity");
  92. args.add ("--file-selection");
  93. if (title.isNotEmpty())
  94. args.add ("--title=" + title);
  95. if (selectMultipleFiles)
  96. {
  97. separator = ":";
  98. args.add ("--multiple");
  99. args.add ("--separator=" + separator);
  100. }
  101. else
  102. {
  103. if (isDirectory) args.add ("--directory");
  104. if (isSave) args.add ("--save");
  105. }
  106. if (file.isDirectory())
  107. file.setAsCurrentWorkingDirectory();
  108. else if (file.getParentDirectory().exists())
  109. file.getParentDirectory().setAsCurrentWorkingDirectory();
  110. else
  111. File::getSpecialLocation (File::userHomeDirectory).setAsCurrentWorkingDirectory();
  112. if (! file.getFileName().isEmpty())
  113. args.add ("--filename=" + file.getFileName());
  114. }
  115. ChildProcess child;
  116. if (child.start (args, ChildProcess::wantStdOut))
  117. {
  118. const String result (child.readAllProcessOutput().trim());
  119. if (result.isNotEmpty())
  120. {
  121. StringArray tokens;
  122. if (selectMultipleFiles)
  123. tokens.addTokens (result, separator, "\"");
  124. else
  125. tokens.add (result);
  126. for (int i = 0; i < tokens.size(); ++i)
  127. results.add (File::getCurrentWorkingDirectory().getChildFile (tokens[i]));
  128. }
  129. child.waitForProcessToFinish (60 * 1000);
  130. }
  131. previousWorkingDirectory.setAsCurrentWorkingDirectory();
  132. }