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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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() || file.getParentDirectory().exists())
  67. {
  68. startPath = file.getFullPathName();
  69. }
  70. else
  71. {
  72. startPath = File::getSpecialLocation (File::userHomeDirectory).getFullPathName();
  73. if (isSave)
  74. startPath += "/" + file.getFileName();
  75. }
  76. args.add (startPath);
  77. }
  78. else
  79. {
  80. // zenity
  81. args.add ("zenity");
  82. args.add ("--file-selection");
  83. if (title.isNotEmpty())
  84. args.add ("--title=" + title);
  85. if (selectMultipleFiles)
  86. {
  87. separator = ":";
  88. args.add ("--multiple");
  89. args.add ("--separator=" + separator);
  90. }
  91. else
  92. {
  93. if (isDirectory) args.add ("--directory");
  94. if (isSave) args.add ("--save");
  95. }
  96. if (file.isDirectory())
  97. file.setAsCurrentWorkingDirectory();
  98. else if (file.getParentDirectory().exists())
  99. file.getParentDirectory().setAsCurrentWorkingDirectory();
  100. else
  101. File::getSpecialLocation (File::userHomeDirectory).setAsCurrentWorkingDirectory();
  102. if (! file.getFileName().isEmpty())
  103. args.add ("--filename=" + file.getFileName());
  104. }
  105. ChildProcess child;
  106. if (child.start (args, ChildProcess::wantStdOut))
  107. {
  108. const String result (child.readAllProcessOutput().trim());
  109. if (result.isNotEmpty())
  110. {
  111. StringArray tokens;
  112. if (selectMultipleFiles)
  113. tokens.addTokens (result, separator, "\"");
  114. else
  115. tokens.add (result);
  116. for (int i = 0; i < tokens.size(); ++i)
  117. results.add (File::getCurrentWorkingDirectory().getChildFile (tokens[i]));
  118. }
  119. child.waitForProcessToFinish (60 * 1000);
  120. }
  121. previousWorkingDirectory.setAsCurrentWorkingDirectory();
  122. }