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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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 java.io.File;
  24. import java.io.IOException;
  25. import java.util.ArrayList;
  26. /**
  27. * Hold a Process and stream readers for a KDialog Process
  28. */
  29. public class KDialogProcess {
  30. /** Is kdialog in /bin? */
  31. private final static boolean isBin = (new File("/bin/kdialog")).exists();
  32. /** Does KDialog exist? */
  33. private final static boolean hasKDialog = (new File("/usr/bin/kdialog")).exists() || isBin;
  34. /** Stream for the stdout stream for this process */
  35. private final StreamReader stdOutputStream;
  36. /** Stream for the stderr stream for this process */
  37. private final StreamReader stdErrorStream;
  38. /** The actual process for this process */
  39. private final Process process;
  40. /**
  41. * Execute kdialog with the Parameters in params
  42. *
  43. * @param params Parameters to pass to kdialog
  44. */
  45. public KDialogProcess(final String[] params) throws IOException {
  46. final String[] exec = new String[params.length + 1];
  47. System.arraycopy(params, 0, exec, 1, params.length);
  48. exec[0] = (isBin) ? "/bin/kdialog" : "/usr/bin/kdialog";
  49. process = Runtime.getRuntime().exec(exec);
  50. stdOutputStream = new StreamReader(process.getInputStream(), new ArrayList<String>());
  51. stdErrorStream = new StreamReader(process.getErrorStream(), new ArrayList<String>());
  52. stdOutputStream.start();
  53. stdErrorStream.start();
  54. }
  55. /**
  56. * Does this system have kdialog?
  57. *
  58. * @return True if kdialog was found, else false
  59. */
  60. public static boolean hasKDialog() {
  61. return hasKDialog;
  62. }
  63. /**
  64. * Get the process object for this KDialogProcess
  65. *
  66. * @return The process object for this KDialogProcess
  67. */
  68. public Process getProcess() {
  69. return process;
  70. }
  71. /**
  72. * Get the StreamReader for this KDialogProcess's stdout stream
  73. *
  74. * @return The StreamReader for this KDialogProcess's stdout stream
  75. */
  76. public StreamReader getStdOutStream() {
  77. return stdOutputStream;
  78. }
  79. /**
  80. * Get the StreamReader for this KDialogProcess's stderr stream
  81. *
  82. * @return The StreamReader for this KDialogProcess's stderr stream
  83. */
  84. public StreamReader getStdErrStream() {
  85. return stdErrorStream;
  86. }
  87. /**
  88. * Wait for the process to finish.
  89. */
  90. public void waitFor() throws InterruptedException {
  91. process.waitFor();
  92. }
  93. }