Source for gnu.java.awt.peer.ClasspathDesktopPeer

   1: /* ClasspathDesktopPeer.java -- Offers a concrete implementation for DesktopPeer
   2:  Copyright (C) 2006, 2007 Free Software Foundation, Inc.
   3: 
   4:  This file is part of GNU Classpath.
   5: 
   6:  GNU Classpath is free software; you can redistribute it and/or modify
   7:  it under the terms of the GNU General Public License as published by
   8:  the Free Software Foundation; either version 2, or (at your option)
   9:  any later version.
  10: 
  11:  GNU Classpath is distributed in the hope that it will be useful, but
  12:  WITHOUT ANY WARRANTY; without even the implied warranty of
  13:  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14:  General Public License for more details.
  15: 
  16:  You should have received a copy of the GNU General Public License
  17:  along with GNU Classpath; see the file COPYING.  If not, write to the
  18:  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19:  02110-1301 USA.
  20: 
  21:  Linking this library statically or dynamically with other modules is
  22:  making a combined work based on this library.  Thus, the terms and
  23:  conditions of the GNU General Public License cover the whole
  24:  combination.
  25: 
  26:  As a special exception, the copyright holders of this library give you
  27:  permission to link this library with independent modules to produce an
  28:  executable, regardless of the license terms of these independent
  29:  modules, and to copy and distribute the resulting executable under
  30:  terms of your choice, provided that you also meet, for each linked
  31:  independent module, the terms and conditions of the license of that
  32:  module.  An independent module is a module which is not derived from
  33:  or based on this library.  If you modify this library, you may extend
  34:  this exception to your version of the library, but you are not
  35:  obligated to do so.  If you do not wish to do so, delete this
  36:  exception statement from your version. */
  37: 
  38: package gnu.java.awt.peer;
  39: 
  40: import java.awt.AWTPermission;
  41: import java.awt.Desktop.Action;
  42: import java.awt.peer.DesktopPeer;
  43: 
  44: import java.io.File;
  45: import java.io.IOException;
  46: 
  47: import java.net.URI;
  48: 
  49: import java.util.prefs.Preferences;
  50: 
  51: /**
  52:  * Offers a common implementation for the Desktop peers, that enables
  53:  * access to default system application within java processes.
  54:  *
  55:  * @author Mario Torre <neugens@limasoftware.net>
  56:  */
  57: public class ClasspathDesktopPeer
  58:     implements DesktopPeer
  59: {
  60:   /** This is the fallback browser, if no desktop was detected. */
  61:   protected static final String _DEFAULT_BROWSER = "firefox";
  62: 
  63:   /** gnu.java.awt.peer.Desktop.html.command */
  64:   protected static final String _BROWSE = "html";
  65: 
  66:   /** gnu.java.awt.peer.Desktop.mail.command */
  67:   protected static final String _MAIL = "mail";
  68: 
  69:   /** gnu.java.awt.peer.Desktop.edit.command */
  70:   protected static final String _EDIT = "edit";
  71: 
  72:   /** gnu.java.awt.peer.Desktop.print.command */
  73:   protected static final String _PRINT = "print";
  74: 
  75:   /** gnu.java.awt.peer.Desktop.open.command */
  76:   protected static final String _OPEN = "open";
  77: 
  78:   /** */
  79:   protected static final KDEDesktopPeer kde = new KDEDesktopPeer();
  80: 
  81:   /** */
  82:   protected static final GnomeDesktopPeer gnome = new GnomeDesktopPeer();
  83: 
  84:   /** */
  85:   protected static final ClasspathDesktopPeer classpath =
  86:     new ClasspathDesktopPeer();
  87: 
  88:   /**
  89:    * Preference subsystem. Packagers and users can override the default
  90:    * behaviour of this class via preferences and system properties.
  91:    */
  92:   protected Preferences prefs =
  93:     Preferences.userNodeForPackage(ClasspathDesktopPeer.class).node("Desktop");
  94: 
  95:   /**
  96:    * @param target
  97:    */
  98:   protected ClasspathDesktopPeer()
  99:   {
 100:     /* nothing to do */
 101:   }
 102: 
 103:   public boolean isSupported(Action action)
 104:   {
 105:     String check = null;
 106: 
 107:     switch(action)
 108:     {
 109:       case BROWSE:
 110:         check = _BROWSE;
 111:         break;
 112: 
 113:       case MAIL:
 114:         check = _MAIL;
 115:         break;
 116: 
 117:       case EDIT:
 118:         check = _EDIT;
 119:         break;
 120: 
 121:       case PRINT:
 122:         check = _PRINT;
 123:         break;
 124: 
 125:       case OPEN: default:
 126:         check = _OPEN;
 127:         break;
 128:     }
 129: 
 130:     return this.supportCommand(check);
 131:   }
 132: 
 133:   public void browse(URI url) throws IOException
 134:   {
 135:     checkPermissions();
 136: 
 137:     String browser = getCommand(_BROWSE);
 138: 
 139:     if (browser == null)
 140:       throw new UnsupportedOperationException();
 141: 
 142:     browser = browser + " " + url.toString();
 143: 
 144:     Runtime.getRuntime().exec(browser);
 145:   }
 146: 
 147:   public void edit(File file) throws IOException
 148:   {
 149:     checkPermissions(file, false);
 150: 
 151:     String edit = getCommand(_EDIT);
 152: 
 153:     if (edit == null)
 154:       throw new UnsupportedOperationException();
 155: 
 156:     edit = edit + " " + file.getAbsolutePath();
 157:     Runtime.getRuntime().exec(edit);
 158:   }
 159: 
 160:   public void mail(URI mailtoURL) throws IOException
 161:   {
 162:     checkPermissions();
 163: 
 164:     String scheme = mailtoURL.getScheme();
 165:     if (scheme == null || !scheme.equalsIgnoreCase("mailto"))
 166:       throw new IllegalArgumentException("URI Scheme not of type mailto");
 167: 
 168:     String mail = getCommand(_MAIL);
 169: 
 170:     if (mail == null)
 171:       throw new UnsupportedOperationException();
 172: 
 173:     mail = mail + " " + mailtoURL.toString();
 174: 
 175:     Runtime.getRuntime().exec(mail);
 176:   }
 177: 
 178:   public void mail() throws IOException
 179:   {
 180:     checkPermissions();
 181: 
 182:     String mail = getCommand(_MAIL);
 183: 
 184:     if (mail == null)
 185:       throw new UnsupportedOperationException();
 186: 
 187:     Runtime.getRuntime().exec(mail);
 188:   }
 189: 
 190:   public void open(File file) throws IOException
 191:   {
 192:     checkPermissions(file, true);
 193: 
 194:     String open = getCommand(_OPEN);
 195: 
 196:     if (open == null)
 197:       throw new UnsupportedOperationException();
 198: 
 199:     open = open + " " + file.getAbsolutePath();
 200:     Runtime.getRuntime().exec(open);
 201:   }
 202: 
 203:   public void print(File file) throws IOException
 204:   {
 205:     checkPrintPermissions(file);
 206: 
 207:     String print = getCommand(_PRINT);
 208: 
 209:     if (print == null)
 210:       throw new UnsupportedOperationException();
 211: 
 212:     print = print + " " + file.getAbsolutePath();
 213:     Runtime.getRuntime().exec(print);
 214:   }
 215: 
 216:   protected String getCommand(String action)
 217:   {
 218:     // check if a system property exist
 219:     String command =
 220:       System.getProperty("gnu.java.awt.peer.Desktop." + action + ".command");
 221: 
 222:     // otherwise, get it from preferences, if any
 223:     if (command == null)
 224:       {
 225:         command = prefs.node(action).get("command", null);
 226:       }
 227: 
 228:     return command;
 229:   }
 230: 
 231:   /**
 232:    * Note: Checks for AWTPermission("showWindowWithoutWarningBanner") only.
 233:    */
 234:   protected void checkPermissions()
 235:   {
 236:     SecurityManager sm = System.getSecurityManager();
 237:     if (sm != null) {
 238:         sm.checkPermission(new AWTPermission("showWindowWithoutWarningBanner"));
 239:     }
 240:   }
 241: 
 242:   /**
 243:    * Calls checkPermissions() and checks for SecurityManager.checkRead()
 244:    * and, if readOnly is false, for SecurityManager.checkWrite()
 245:    */
 246:   protected void checkPermissions(File file, boolean readOnly)
 247:   {
 248:     checkPermissions();
 249: 
 250:     SecurityManager sm = System.getSecurityManager();
 251:     if (sm != null) {
 252:         sm.checkRead(file.toString());
 253:         if (!readOnly) sm.checkWrite(file.toString());
 254:     }
 255:   }
 256: 
 257:   /**
 258:    * Calls checkPermissions(file, true) and checks for
 259:    * SecurityManager.checkPrintJobAccess()
 260:    */
 261:   protected void checkPrintPermissions(File file)
 262:   {
 263:     checkPermissions(file, true);
 264: 
 265:     SecurityManager sm = System.getSecurityManager();
 266:     if (sm != null) {
 267:       sm.checkPrintJobAccess();
 268:     }
 269:   }
 270: 
 271:   /**
 272:    * @param check
 273:    * @return
 274:    */
 275:   protected boolean supportCommand(String check)
 276:   {
 277:     return ((this.getCommand(check) != null) ?  true : false);
 278:   }
 279: 
 280:   /**
 281:    * @return
 282:    */
 283:   public static DesktopPeer getDesktop()
 284:   {
 285:     //  check if we are under Gnome or KDE or anything else
 286:     String desktopSession = System.getenv("GNOME_DESKTOP_SESSION_ID");
 287:     if (desktopSession == null)
 288:       {
 289:         desktopSession = System.getenv("KDE_FULL_SESSION");
 290:         if (desktopSession != null)
 291:           return kde;
 292:       }
 293:     else
 294:       {
 295:         return gnome;
 296:       }
 297: 
 298:     // revert to this class for default values
 299:     return classpath;
 300:   }
 301: }