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.

152 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. static bool exeIsAvailable (const char* const executable)
  19. {
  20. ChildProcess child;
  21. const bool ok = child.start ("which " + String (executable))
  22. && child.readAllProcessOutput().trim().isNotEmpty();
  23. child.waitForProcessToFinish (60 * 1000);
  24. return ok;
  25. }
  26. bool FileChooser::isPlatformDialogAvailable()
  27. {
  28. return exeIsAvailable ("zenity") || exeIsAvailable ("kdialog");
  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))
  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 (tokens[i]));
  118. }
  119. child.waitForProcessToFinish (60 * 1000);
  120. }
  121. previousWorkingDirectory.setAsCurrentWorkingDirectory();
  122. }