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