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.

KDialogProcess.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.dcc.kde;
  23. import com.google.common.io.CharStreams;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.io.InputStreamReader;
  27. import java.util.List;
  28. /**
  29. * Hold a Process and stream readers for a KDialog Process.
  30. */
  31. public class KDialogProcess {
  32. /** Is kdialog in /bin? */
  33. private static final boolean IS_BIN = new File("/bin/kdialog").exists();
  34. /** Does KDialog exist? */
  35. private static final boolean HAS_KDIALOG = IS_BIN || new File("/usr/bin/kdialog").exists();
  36. /** Output from the output stream. */
  37. private final List<String> stdOutput;
  38. /** Output from the error stream. */
  39. private final List<String> stdError;
  40. /** The actual process for this process. */
  41. private final Process process;
  42. /**
  43. * Execute kdialog with the Parameters in params.
  44. *
  45. * @param params Parameters to pass to kdialog
  46. *
  47. * @throws IOException if an I/O error occurs
  48. */
  49. public KDialogProcess(final String[] params) throws IOException {
  50. final String[] exec = new String[params.length + 1];
  51. System.arraycopy(params, 0, exec, 1, params.length);
  52. exec[0] = IS_BIN ? "/bin/kdialog" : "/usr/bin/kdialog";
  53. process = Runtime.getRuntime().exec(exec);
  54. stdOutput = CharStreams.readLines(new InputStreamReader(process.getInputStream()));
  55. stdError = CharStreams.readLines(new InputStreamReader(process.getErrorStream()));
  56. }
  57. /**
  58. * Does this system have kdialog?
  59. *
  60. * @return True if kdialog was found, else false
  61. */
  62. public static boolean hasKDialog() {
  63. return HAS_KDIALOG;
  64. }
  65. /**
  66. * Get the process object for this KDialogProcess.
  67. *
  68. * @return The process object for this KDialogProcess
  69. */
  70. public Process getProcess() {
  71. return process;
  72. }
  73. /**
  74. * Get the StreamReader for this KDialogProcess's stdout stream.
  75. *
  76. * @return The StreamReader for this KDialogProcess's stdout stream
  77. */
  78. public List<String> getStdOut() {
  79. return stdOutput;
  80. }
  81. /**
  82. * Get the StreamReader for this KDialogProcess's stderr stream.
  83. *
  84. * @return The StreamReader for this KDialogProcess's stderr stream
  85. */
  86. public List<String> getStdErr() {
  87. return stdError;
  88. }
  89. /**
  90. * Wait for the process to finish.
  91. *
  92. * @throws InterruptedException if the current thread is interrupted by another thread while it
  93. * is waiting
  94. */
  95. public void waitFor() throws InterruptedException {
  96. process.waitFor();
  97. }
  98. }