1 /* 2 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.awt.X11; 27 28 import sun.awt.UNIXToolkit; 29 30 import java.io.File; 31 import java.io.IOException; 32 import java.net.MalformedURLException; 33 import java.net.URI; 34 35 import java.awt.Desktop.Action; 36 import java.awt.peer.DesktopPeer; 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.List; 40 41 42 /** 43 * Concrete implementation of the interface <code>DesktopPeer</code> for 44 * the Gnome desktop on Linux and Unix platforms. 45 * 46 * @see DesktopPeer 47 */ 48 public class XDesktopPeer implements DesktopPeer { 49 50 // supportedActions may be changed from native within an init() call 51 private static final List<Action> supportedActions 52 = new ArrayList<>(Arrays.asList(Action.OPEN, Action.MAIL, Action.BROWSE)); 53 54 private static boolean nativeLibraryLoaded = false; 55 private static boolean initExecuted = false; 56 57 private static void initWithLock(){ 58 XToolkit.awtLock(); 59 try { 60 if (!initExecuted) { 61 nativeLibraryLoaded = init(UNIXToolkit.getEnabledGtkVersion() 62 .getNumber(), UNIXToolkit.isGtkVerbose()); 63 } 64 } finally { 65 initExecuted = true; 66 XToolkit.awtUnlock(); 67 } 68 } 69 70 //package-private 71 XDesktopPeer(){ 72 initWithLock(); 73 } 74 75 static boolean isDesktopSupported() { 76 initWithLock(); 77 return nativeLibraryLoaded && !supportedActions.isEmpty(); 78 } 79 80 public boolean isSupported(Action type) { 81 return supportedActions.contains(type); 82 } 83 84 public void open(File file) throws IOException { 85 try { 86 launch(file.toURI()); 87 } catch (MalformedURLException e) { 88 throw new IOException(file.toString()); 89 } 90 } 91 92 public void edit(File file) throws IOException { 93 throw new UnsupportedOperationException("The current platform " + 94 "doesn't support the EDIT action."); 95 } 96 97 public void print(File file) throws IOException { 98 throw new UnsupportedOperationException("The current platform " + 99 "doesn't support the PRINT action."); 100 } 101 102 public void mail(URI uri) throws IOException { 103 launch(uri); 104 } 105 106 public void browse(URI uri) throws IOException { 107 launch(uri); 108 } 109 110 private void launch(URI uri) throws IOException { 111 byte[] uriByteArray = ( uri.toString() + '\0' ).getBytes(); 112 boolean result = false; 113 XToolkit.awtLock(); 114 try { 115 if (!nativeLibraryLoaded) { 116 throw new IOException("Failed to load native libraries."); 117 } 118 result = gnome_url_show(uriByteArray); 119 } finally { 120 XToolkit.awtUnlock(); 121 } 122 if (!result) { 123 throw new IOException("Failed to show URI:" + uri); 124 } 125 } 126 127 private native boolean gnome_url_show(byte[] url); 128 private static native boolean init(int gtkVersion, boolean verbose); 129 }