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.

BrowserLauncher.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2006-2007 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 uk.org.ownage.dmdirc;
  23. import java.awt.Desktop;
  24. import java.io.IOException;
  25. import java.net.MalformedURLException;
  26. import java.net.URISyntaxException;
  27. import java.net.URL;
  28. import java.util.Date;
  29. import uk.org.ownage.dmdirc.logger.ErrorLevel;
  30. import uk.org.ownage.dmdirc.logger.Logger;
  31. /**
  32. * The BrowserLauncher handles the opening of the user's web browser when
  33. * they click a link.
  34. */
  35. public final class BrowserLauncher {
  36. /** The time a browser was last launched. */
  37. private static Date lastLaunch;
  38. /** Prevents creation of a new instance of BrowserLauncher. */
  39. private BrowserLauncher() {
  40. }
  41. /**
  42. * Opens a URL in the default browser where possible, else any availble
  43. * browser it finds.
  44. * @param url url to open in the browser
  45. */
  46. public static void openURL(final String url) {
  47. try {
  48. if (url.startsWith("http://") || url.startsWith("https://")) {
  49. openURL(new URL(url));
  50. } else {
  51. openURL(new URL("http://" + url));
  52. }
  53. return;
  54. } catch (MalformedURLException ex) {
  55. Logger.error(ErrorLevel.WARNING, "Invalid URL", ex);
  56. }
  57. }
  58. /**
  59. * Opens a URL in the default browser where possible, else any availble
  60. * browser it finds.
  61. * @param url url to open in the browser
  62. */
  63. public static void openURL(final URL url) {
  64. final Date oldLaunch = lastLaunch;
  65. lastLaunch = new Date();
  66. if (Config.getOptionBool("browser", "uselaunchdelay") && lastLaunch != null) {
  67. final Long diff = lastLaunch.getTime() - oldLaunch.getTime();
  68. if (diff < Config.getOptionInt("browser", "launchdelay", 500)) {
  69. return;
  70. }
  71. }
  72. Desktop desktop = null;
  73. if (Desktop.isDesktopSupported()) {
  74. desktop = Desktop.getDesktop();
  75. }
  76. if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
  77. try {
  78. desktop.browse(url.toURI());
  79. } catch (IOException ex) {
  80. openURLLinux(url.toString());
  81. } catch (URISyntaxException ex) {
  82. Logger.error(ErrorLevel.WARNING, "Invalid URL", ex);
  83. }
  84. } else {
  85. openURLLinux(url.toString());
  86. }
  87. }
  88. /**
  89. * Attempts to open the url in a linux browser.
  90. *
  91. * @param url url to open
  92. */
  93. private static void openURLLinux(final String url) {
  94. String browser;
  95. browser = getBrowserLinux();
  96. if (browser == null) {
  97. Logger.error(ErrorLevel.ERROR, "Unable to find browser, "
  98. + "please set in preferences.");
  99. } else {
  100. runBrowser(url, browser);
  101. }
  102. }
  103. /**
  104. * Attempts to obtain a browser for linux, using specified values if
  105. * possible.
  106. *
  107. * @return full browser binary path
  108. */
  109. private static String getBrowserLinux() {
  110. String browser = null;
  111. if (Config.hasOption("general", "browser")) {
  112. browser = Config.getOption("general", "browser");
  113. } else {
  114. final String[] browsers =
  115. {"firefox", "konqueror", "epiphany", "opera", "mozilla", };
  116. for (int count = 0; count < browsers.length
  117. && browser == null; count++) {
  118. try {
  119. if (Runtime.getRuntime()
  120. .exec(new String[] {"which", browsers[count]}).waitFor() == 0) {
  121. browser = browsers[count];
  122. }
  123. } catch (IOException ex) {
  124. Logger.error(ErrorLevel.WARNING, "Unable to find a browser", ex);
  125. } catch (InterruptedException ex) {
  126. Logger.error(ErrorLevel.WARNING, "Unable to find a browser", ex);
  127. }
  128. }
  129. }
  130. return browser;
  131. }
  132. /**
  133. * Attempts to open the url in the specified browser.
  134. * @param url url to open
  135. * @param browser browser to use
  136. * @throws IOException if unable to open browser
  137. */
  138. private static void runBrowser(final String url, final String browser) {
  139. try {
  140. Runtime.getRuntime().exec(new String[] {browser, url});
  141. } catch (IOException ex) {
  142. Logger.error(ErrorLevel.WARNING, "Unable to run the browser", ex);
  143. }
  144. }
  145. }