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

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