1   /**
2    * Distribution License:
3    * JSword is free software; you can redistribute it and/or modify it under
4    * the terms of the GNU Lesser General Public License, version 2.1 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8    * See the GNU Lesser General Public License for more details.
9    *
10   * The License is available on the internet at:
11   *       http://www.gnu.org/copyleft/lgpl.html
12   * or by writing to:
13   *      Free Software Foundation, Inc.
14   *      59 Temple Place - Suite 330
15   *      Boston, MA 02111-1307, USA
16   *
17   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: OSType.java 2049 2010-12-09 13:52:58Z dmsmith $
21   */
22  package org.crosswire.common.util;
23  
24  import java.io.File;
25  import java.net.URI;
26  
27  /**
28   * Types of Operating Systems for which specialized behavior is needed.
29   * 
30   * @see gnu.lgpl.License for license details.<br>
31   *      The copyright to this program is held by it's authors.
32   * @author DM Smith [dmsmith555 at yahoo dot com]
33   */
34  public enum OSType {
35      MAC  ("Mac") {
36          @Override
37          public URI getUserArea() {
38              return NetUtil.lengthenURI(getUserHome(), MAC_USER_DATA_AREA);
39          }
40  
41          @Override
42          public URI getUserAreaFolder(String hiddenFolderName, String visibleFolderName) {
43              return NetUtil.lengthenURI(getUserArea(), visibleFolderName);
44          }
45      },
46  
47      WIN32  ("Win") {
48          @Override
49          public URI getUserArea() {
50              return NetUtil.lengthenURI(getUserHome(), WIN32_USER_DATA_AREA);
51          }
52  
53          @Override
54          public URI getUserAreaFolder(String hiddenFolderName, String visibleFolderName) {
55              return NetUtil.lengthenURI(getUserArea(), visibleFolderName);
56          }
57      },
58  
59      DEFAULT ("*nix") {
60          @Override
61          public URI getUserArea() {
62              return getUserHome();
63          }
64  
65          @Override
66          public URI getUserAreaFolder(String hiddenFolderName, String visibleFolderName) {
67              return NetUtil.lengthenURI(getUserArea(), hiddenFolderName);
68          }
69      };
70  
71      /**
72       * Simple ctor
73       */
74      private OSType(String name) {
75          this.name = name;
76      }
77  
78      /**
79       * Get the user area for this OSType.
80       * 
81       * @return the user area
82       */
83      public abstract URI getUserArea();
84  
85      /**
86       * A folder in the user area. This osType will determine which to use in
87       * constructing the URI to the folder.
88       * 
89       * @param hiddenFolderName
90       *            is typically a "unix" hidden folder name such as .jsword.
91       * @param visibleFolderName
92       *            is an visible folder name, such as JSword.
93       * 
94       * @return the user area folder
95       */
96      public abstract URI getUserAreaFolder(String hiddenFolderName, String visibleFolderName);
97  
98      public static URI getUserHome() {
99          return NetUtil.getURI(new File(System.getProperty("user.home")));
100     }
101 
102     /**
103      * Get the machine's OSType.
104      * 
105      * @return the machine's OSType
106      */
107     public static OSType getOSType() {
108         return osType;
109     }
110 
111     /**
112      * Lookup method to convert from a String
113      */
114     public static OSType fromString(String name) {
115         for (OSType v : values()) {
116             if (name.startsWith(v.name)) {
117                 return v;
118             }
119         }
120 
121         return DEFAULT;
122     }
123 
124     /*
125      * (non-Javadoc)
126      * 
127      * @see java.lang.Object#toString()
128      */
129     @Override
130     public String toString() {
131         return name;
132     }
133 
134     /**
135      * The name of the type
136      */
137     private String name;
138 
139     /**
140      * The Windows user settings parent directory
141      */
142     private static final String WIN32_USER_DATA_AREA = "Application Data";
143 
144     /**
145      * The Mac user settings parent directory
146      */
147     private static final String MAC_USER_DATA_AREA = "Library/Application Support";
148 
149     /**
150      * The machine's osType
151      */
152     private static OSType osType = fromString(System.getProperty("os.name"));
153 }
154