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.

juce_linux_FileChooser.cpp 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. static bool canUseNativeBox = exeIsAvailable ("zenity") || exeIsAvailable ("kdialog");
  28. return canUseNativeBox;
  29. }
  30. void FileChooser::showPlatformDialog (Array<File>& results,
  31. const String& title,
  32. const File& file,
  33. const String& filters,
  34. bool isDirectory,
  35. bool /* selectsFiles */,
  36. bool isSave,
  37. bool /* warnAboutOverwritingExistingFiles */,
  38. bool selectMultipleFiles,
  39. FilePreviewComponent* /* previewComponent */)
  40. {
  41. String separator;
  42. StringArray args;
  43. const File previousWorkingDirectory (File::getCurrentWorkingDirectory());
  44. const bool isKdeFullSession = SystemStats::getEnvironmentVariable ("KDE_FULL_SESSION", String::empty)
  45. .equalsIgnoreCase ("true");
  46. if (exeIsAvailable ("kdialog") && (isKdeFullSession || ! exeIsAvailable ("zenity")))
  47. {
  48. // use kdialog for KDE sessions or if zenity is missing
  49. args.add ("kdialog");
  50. if (title.isNotEmpty())
  51. args.add ("--title=" + title);
  52. if (selectMultipleFiles)
  53. {
  54. separator = "\n";
  55. args.add ("--multiple");
  56. args.add ("--separate-output");
  57. args.add ("--getopenfilename");
  58. }
  59. else
  60. {
  61. if (isSave) args.add ("--getsavefilename");
  62. else if (isDirectory) args.add ("--getexistingdirectory");
  63. else args.add ("--getopenfilename");
  64. }
  65. String startPath;
  66. if (file.exists())
  67. {
  68. startPath = file.getFullPathName();
  69. }
  70. else if (file.getParentDirectory().exists())
  71. {
  72. startPath = file.getParentDirectory().getFullPathName();
  73. }
  74. else
  75. {
  76. startPath = File::getSpecialLocation (File::userHomeDirectory).getFullPathName();
  77. if (isSave)
  78. startPath += "/" + file.getFileName();
  79. }
  80. args.add (startPath);
  81. args.add (filters.replaceCharacter (';', ' '));
  82. args.add ("2>/dev/null");
  83. }
  84. else
  85. {
  86. // zenity
  87. args.add ("zenity");
  88. args.add ("--file-selection");
  89. if (title.isNotEmpty())
  90. args.add ("--title=" + title);
  91. if (selectMultipleFiles)
  92. {
  93. separator = ":";
  94. args.add ("--multiple");
  95. args.add ("--separator=" + separator);
  96. }
  97. else
  98. {
  99. if (isDirectory) args.add ("--directory");
  100. if (isSave) args.add ("--save");
  101. }
  102. if (file.isDirectory())
  103. file.setAsCurrentWorkingDirectory();
  104. else if (file.getParentDirectory().exists())
  105. file.getParentDirectory().setAsCurrentWorkingDirectory();
  106. else
  107. File::getSpecialLocation (File::userHomeDirectory).setAsCurrentWorkingDirectory();
  108. if (! file.getFileName().isEmpty())
  109. args.add ("--filename=" + file.getFileName());
  110. }
  111. ChildProcess child;
  112. if (child.start (args, ChildProcess::wantStdOut))
  113. {
  114. const String result (child.readAllProcessOutput().trim());
  115. if (result.isNotEmpty())
  116. {
  117. StringArray tokens;
  118. if (selectMultipleFiles)
  119. tokens.addTokens (result, separator, "\"");
  120. else
  121. tokens.add (result);
  122. for (int i = 0; i < tokens.size(); ++i)
  123. results.add (File::getCurrentWorkingDirectory().getChildFile (tokens[i]));
  124. }
  125. child.waitForProcessToFinish (60 * 1000);
  126. }
  127. previousWorkingDirectory.setAsCurrentWorkingDirectory();
  128. }