DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2011 TigerVNC Team. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
Brian Hinz | b213da6 | 2012-04-11 22:00:55 +0000 | [diff] [blame] | 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | package com.tigervnc.vncviewer; |
| 20 | |
| 21 | import java.util.Properties; |
| 22 | import java.io.FileOutputStream; |
| 23 | import javax.swing.filechooser.*; |
| 24 | |
| 25 | /** |
| 26 | * Simple, generic class to load up or save properties for an application. |
| 27 | * (eg user preferences) |
| 28 | * |
| 29 | * While reading in values, it will automatically convert from string format, |
| 30 | * to your choice of int or boolean, if desired. |
| 31 | * |
| 32 | * We assume that you want properties stored in a file named |
| 33 | * $HOME/.yourappname, where $HOME represents the users "home directory" |
| 34 | * |
| 35 | * Feel free to email comments or suggestions to the author |
| 36 | * |
| 37 | * @version 1.0, 09/16/1997 |
| 38 | * @author Philip Brown phil@bolthole.com |
| 39 | */ |
| 40 | |
| 41 | public class UserPrefs extends Properties { |
DRC | 979e2d1 | 2011-10-05 21:39:59 +0000 | [diff] [blame] | 42 | String userhome=null; //This will have fileseparator on end if it |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 43 | String prefFile; |
| 44 | String appName; |
| 45 | |
| 46 | Properties systemprops; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * We try to read in a preferences file, from the user's "HOME" |
| 51 | * directory. We base the name of the file, on the name of the |
| 52 | * application we are in. |
| 53 | * Use the getHomeDir() call if you want to know what directory |
| 54 | * this is in. |
| 55 | * |
| 56 | * @param appName_ name of application calling this class |
| 57 | */ |
| 58 | public UserPrefs(String appName_) { |
| 59 | appName = appName_; |
| 60 | |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 61 | try { |
| 62 | systemprops=System.getProperties(); |
| 63 | } catch(java.security.AccessControlException e) { |
| 64 | System.out.println("Cannot access system properties"); |
| 65 | } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 66 | // This is guaranteed as always being some valid directory, |
| 67 | // according to spec. |
DRC | 979e2d1 | 2011-10-05 21:39:59 +0000 | [diff] [blame] | 68 | prefFile= getHomeDir()+getFileSeparator()+ |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 69 | "."+appName; |
| 70 | try { |
| 71 | load(new java.io.FileInputStream(prefFile)); |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 72 | } catch(java.security.AccessControlException e) { |
| 73 | System.out.println("Cannot access system properties"); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 74 | } catch (Exception err) { |
| 75 | if(err instanceof java.io.FileNotFoundException) { |
| 76 | try { |
| 77 | store(new FileOutputStream(prefFile), appName+" preferences"); |
| 78 | } catch (Exception e) { /* FIXME */ } |
| 79 | } else { |
| 80 | // FIXME - should be a dialog |
| 81 | System.out.println("Error opening prefs file:"+err.getMessage()); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | // Strip off any comments |
| 88 | String trimValue(String prop) { |
| 89 | if(prop==null) |
| 90 | return null; |
| 91 | |
| 92 | int lastpos; |
| 93 | lastpos=prop.indexOf('#'); |
| 94 | if(lastpos==-1) |
| 95 | lastpos=prop.length()-1; |
| 96 | while((prop.charAt(lastpos)==' ') || |
| 97 | (prop.charAt(lastpos)=='\t')) { |
| 98 | lastpos--; |
| 99 | if(lastpos==0) |
| 100 | break; |
| 101 | } |
| 102 | |
| 103 | return prop.substring(0, lastpos+1); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * The java spec guarantees that a "home" directory be |
| 108 | * specified. We look it up for our own uses at initialization |
| 109 | * If you want to do other things in the user's home dir, |
| 110 | * this routine is an easy way to find out where it is. |
| 111 | * |
| 112 | * This returns string that will have trailing fileseparator, eg "/") |
| 113 | * so you can slap together a filename directly after it, and |
| 114 | * not worry about that sort of junk. |
| 115 | */ |
| 116 | final public static String getHomeDir() { |
| 117 | String homeDir = null; |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 118 | try { |
| 119 | String os = System.getProperty("os.name"); |
| 120 | try { |
| 121 | if (os.startsWith("Windows")) { |
| 122 | // JRE prior to 1.5 cannot reliably determine USERPROFILE |
| 123 | // return user.home and hope it's right... |
| 124 | if (Integer.parseInt(System.getProperty("java.version").split("\\.")[1]) < 5) { |
| 125 | try { |
| 126 | homeDir = System.getProperty("user.home"); |
| 127 | } catch(java.security.AccessControlException e) { |
| 128 | System.out.println("Cannot access user.home system property"); |
| 129 | } |
| 130 | } else { |
| 131 | homeDir = System.getenv("USERPROFILE"); |
| 132 | } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 133 | } else { |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 134 | try { |
| 135 | homeDir = FileSystemView.getFileSystemView(). |
| 136 | getDefaultDirectory().getCanonicalPath(); |
| 137 | } catch(java.security.AccessControlException e) { |
| 138 | System.out.println("Cannot access system property"); |
| 139 | } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 140 | } |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 141 | } catch (Exception e) { |
| 142 | e.printStackTrace(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 143 | } |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 144 | } catch(java.security.AccessControlException e) { |
| 145 | System.out.println("Cannot access os.name system property"); |
| 146 | } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 147 | return homeDir; |
| 148 | } |
| 149 | |
| 150 | final private String getUserName() { |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 151 | String userName = null; |
| 152 | try { |
| 153 | userName = (String) System.getProperties().get("user.name"); |
| 154 | } catch(java.security.AccessControlException e) { |
| 155 | System.out.println("Cannot access user.name system property"); |
| 156 | } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 157 | return userName; |
| 158 | } |
| 159 | |
DRC | 979e2d1 | 2011-10-05 21:39:59 +0000 | [diff] [blame] | 160 | final public static String getFileSeparator() { |
| 161 | String separator = null; |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 162 | try { |
DRC | 979e2d1 | 2011-10-05 21:39:59 +0000 | [diff] [blame] | 163 | separator = Character.toString(java.io.File.separatorChar); |
Brian Hinz | c800321 | 2011-10-05 03:45:43 +0000 | [diff] [blame] | 164 | } catch(java.security.AccessControlException e) { |
| 165 | System.out.println("Cannot access file.separator system property"); |
| 166 | } |
DRC | 979e2d1 | 2011-10-05 21:39:59 +0000 | [diff] [blame] | 167 | return separator; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /** |
| 171 | * way to directly set a preference. You'll have to |
| 172 | * do your own type conversion. |
| 173 | * @param prefname name of property |
| 174 | * @param value string value of property |
| 175 | */ |
| 176 | public void setPref(String prefname, String value) { |
| 177 | setProperty(prefname, value); |
| 178 | } |
| 179 | |
| 180 | public void setPref(String prefname, int value) { |
| 181 | systemprops.put(prefname, java.lang.Integer.toString(value)); |
| 182 | } |
| 183 | |
| 184 | public void setPref(String prefname, boolean value) { |
| 185 | put(prefname, (value ? "true" : "false")); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Gets named resource, as a string value. |
| 190 | * returns null if no such resource defined. |
| 191 | * @param name name of property |
| 192 | */ |
| 193 | public String getString(String name) { |
| 194 | return trimValue(getProperty(name)); |
| 195 | } |
| 196 | /** |
| 197 | * Gets named resource, as a string value. |
| 198 | * |
| 199 | * @param name name of property |
| 200 | * @param defval default value to remember and return , if |
| 201 | * no existing property name found. |
| 202 | */ |
| 203 | public String getString(String name, String defstr) { |
| 204 | String val = trimValue(getProperty(name)); |
| 205 | if(val==null) { |
| 206 | setPref(name, defstr); |
| 207 | return defstr; |
| 208 | } |
| 209 | return val; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * look up property that is an int value |
| 214 | * @param name name of property |
| 215 | */ |
| 216 | public int getInt(String name) { |
| 217 | String strint = trimValue(getProperty(name)); |
| 218 | int val=0; |
| 219 | try { |
| 220 | val = Integer.parseInt(strint); |
| 221 | } catch (NumberFormatException err) { |
| 222 | //we dont care |
| 223 | } |
| 224 | return val; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * look up property that is an int value |
| 229 | * @param name name of property |
| 230 | * @param defval default value to remember and return , if |
| 231 | * no existing property name found. |
| 232 | */ |
| 233 | public int getInt(String name, int defval) { |
| 234 | String strint = trimValue(getProperty(name)); |
| 235 | if(strint==null) { |
| 236 | setPref(name, String.valueOf(defval)); |
| 237 | return defval; |
| 238 | } |
| 239 | int val=0; |
| 240 | try { |
| 241 | val = Integer.parseInt(strint); |
| 242 | } catch (NumberFormatException err) { |
| 243 | //we dont care |
| 244 | } |
| 245 | return val; |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * look up property that is a boolean value |
| 250 | * @param name name of property |
| 251 | * @param defval default value to remember and return , if |
| 252 | * no existing property name found. |
| 253 | */ |
| 254 | public boolean getBool(String name) { |
| 255 | String strval = trimValue(getProperty(name)); |
| 256 | if(strval.equals("true")) |
| 257 | return true; |
| 258 | |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * @param name name of property |
| 264 | * @param defval default value to remember and return , if |
| 265 | * no existing property name found. |
| 266 | */ |
| 267 | public boolean getBool(String name, boolean defval) { |
| 268 | String strval = trimValue(getProperty(name)); |
| 269 | if(strval==null) { |
| 270 | setPref(name, String.valueOf(defval)); |
| 271 | return defval; |
| 272 | } |
| 273 | |
| 274 | if(strval.equals("true")) |
| 275 | return true; |
| 276 | |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * save user preferences to default file. Duh. |
| 282 | */ |
| 283 | public void Save() throws java.io.IOException { |
| 284 | store(new FileOutputStream(prefFile), appName+" preferences"); |
| 285 | } |
| 286 | } |