1 /* 2 * Copyright (c) 2005, 2016, 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 29 import sun.awt.UNIXToolkit; 30 31 import java.io.File; 32 import java.io.IOException; 33 import java.net.MalformedURLException; 34 import java.net.URI; 35 36 import java.awt.Desktop.Action; 37 import java.awt.peer.DesktopPeer; 38 import java.util.ArrayList; 39 import java.util.Arrays; 40 import java.util.List; 41 42 43 /** 44 * Concrete implementation of the interface {@code DesktopPeer} for 45 * the Gnome desktop on Linux and Unix platforms. 46 * 47 * @see DesktopPeer 48 */ 49 public class XDesktopPeer implements DesktopPeer { 50 51 // supportedActions may be changed from native within an init() call 52 private static final List<Action> supportedActions 53 = new ArrayList<>(Arrays.asList(Action.OPEN, Action.MAIL, Action.BROWSE)); 54 55 private static boolean nativeLibraryLoaded = false; 56 private static boolean initExecuted = false; 57 initWithLock()58 private static void initWithLock(){ 59 XToolkit.awtLock(); 60 try { 61 if (!initExecuted) { 62 nativeLibraryLoaded = init(UNIXToolkit.getEnabledGtkVersion() 63 .getNumber(), UNIXToolkit.isGtkVerbose()); 64 } 65 } finally { 66 initExecuted = true; 67 XToolkit.awtUnlock(); 68 } 69 } 70 71 //package-private XDesktopPeer()72 XDesktopPeer(){ 73 initWithLock(); 74 } 75 isDesktopSupported()76 static boolean isDesktopSupported() { 77 initWithLock(); 78 return nativeLibraryLoaded && !supportedActions.isEmpty(); 79 } 80 isSupported(Action type)81 public boolean isSupported(Action type) { 82 return supportedActions.contains(type); 83 } 84 open(File file)85 public void open(File file) throws IOException { 86 try { 87 launch(file.toURI()); 88 } catch (MalformedURLException e) { 89 throw new IOException(file.toString()); 90 } 91 } 92 edit(File file)93 public void edit(File file) throws IOException { 94 throw new UnsupportedOperationException("The current platform " + 95 "doesn't support the EDIT action."); 96 } 97 print(File file)98 public void print(File file) throws IOException { 99 throw new UnsupportedOperationException("The current platform " + 100 "doesn't support the PRINT action."); 101 } 102 mail(URI uri)103 public void mail(URI uri) throws IOException { 104 launch(uri); 105 } 106 browse(URI uri)107 public void browse(URI uri) throws IOException { 108 launch(uri); 109 } 110 launch(URI uri)111 private void launch(URI uri) throws IOException { 112 byte[] uriByteArray = ( uri.toString() + '\0' ).getBytes(); 113 boolean result = false; 114 XToolkit.awtLock(); 115 try { 116 if (!nativeLibraryLoaded) { 117 throw new IOException("Failed to load native libraries."); 118 } 119 result = gnome_url_show(uriByteArray); 120 } finally { 121 XToolkit.awtUnlock(); 122 } 123 if (!result) { 124 throw new IOException("Failed to show URI:" + uri); 125 } 126 } 127 gnome_url_show(byte[] url)128 private native boolean gnome_url_show(byte[] url); init(int gtkVersion, boolean verbose)129 private static native boolean init(int gtkVersion, boolean verbose); 130 } 131