1 /*
2 * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3 * Copyright (C) 2009 Mickael Guessant
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 package davmail.ui.browser;
20
21 import davmail.BundleMessage;
22 import davmail.Settings;
23 import davmail.ui.AboutFrame;
24 import davmail.ui.tray.DavGatewayTray;
25
26 import java.net.URI;
27 import java.net.URISyntaxException;
28
29 /**
30 * Open default browser.
31 */
32 public final class DesktopBrowser {
33 private DesktopBrowser() {
34 }
35
36 /**
37 * Open default browser at location URI.
38 * User Java 6 Desktop class, OSX open command or SWT program launch
39 *
40 * @param location location URI
41 */
42 public static void browse(URI location) {
43 try {
44 // trigger ClassNotFoundException
45 ClassLoader classloader = AboutFrame.class.getClassLoader();
46 classloader.loadClass("java.awt.Desktop");
47
48 // Open link in default browser
49 AwtDesktopBrowser.browse(location);
50 } catch (ClassNotFoundException e) {
51 DavGatewayTray.debug(new BundleMessage("LOG_JAVA6_DESKTOP_UNAVAILABLE"));
52 // failover for MacOSX
53 if (System.getProperty("os.name").toLowerCase().startsWith("mac os x")) {
54 try {
55 OSXDesktopBrowser.browse(location);
56 } catch (Exception e2) {
57 DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e2);
58 }
59 } else {
60 // failover : try SWT
61 try {
62 // trigger ClassNotFoundException
63 ClassLoader classloader = AboutFrame.class.getClassLoader();
64 classloader.loadClass("org.eclipse.swt.program.Program");
65 SwtDesktopBrowser.browse(location);
66 } catch (ClassNotFoundException e2) {
67 DavGatewayTray.error(new BundleMessage("LOG_OPEN_LINK_NOT_SUPPORTED"));
68 } catch (Exception e2) {
69 DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e2);
70 }
71 }
72 } catch (java.lang.UnsupportedOperationException e) {
73 if (Settings.isUnix()) {
74 try {
75 XdgDesktopBrowser.browse(location);
76 } catch (Exception e2) {
77 DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e2);
78 }
79 } else {
80 DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e);
81 }
82 } catch (Exception e) {
83 DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e);
84 }
85 }
86
87 /**
88 * Open default browser at location.
89 * User Java 6 Desktop class, OSX open command or SWT program launch
90 *
91 * @param location target location
92 */
93 public static void browse(String location) {
94 try {
95 DesktopBrowser.browse(new URI(location));
96 } catch (URISyntaxException e) {
97 DavGatewayTray.error(new BundleMessage("LOG_UNABLE_TO_OPEN_LINK"), e);
98 }
99 }
100
101 }