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