blob: 51b8b9043a2e9cfe7e0b1ed9372b82126f33f0c2 [file] [log] [blame]
DRCc5dc0382011-05-13 21:42:14 +00001/* 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
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19package com.tigervnc.vncviewer;
20
21import java.util.Properties;
22import java.io.FileOutputStream;
23import 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
41public class UserPrefs extends Properties {
42 String userhome=null; //This will have fileseperator on end if it
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
61 systemprops=System.getProperties();
62 // This is guaranteed as always being some valid directory,
63 // according to spec.
64 prefFile= getHomeDir()+getFileSeperator()+
65 "."+appName;
66 try {
67 load(new java.io.FileInputStream(prefFile));
68 } catch (Exception err) {
69 if(err instanceof java.io.FileNotFoundException) {
70 try {
71 store(new FileOutputStream(prefFile), appName+" preferences");
72 } catch (Exception e) { /* FIXME */ }
73 } else {
74 // FIXME - should be a dialog
75 System.out.println("Error opening prefs file:"+err.getMessage());
76 }
77 }
78
79 }
80
81 // Strip off any comments
82 String trimValue(String prop) {
83 if(prop==null)
84 return null;
85
86 int lastpos;
87 lastpos=prop.indexOf('#');
88 if(lastpos==-1)
89 lastpos=prop.length()-1;
90 while((prop.charAt(lastpos)==' ') ||
91 (prop.charAt(lastpos)=='\t')) {
92 lastpos--;
93 if(lastpos==0)
94 break;
95 }
96
97 return prop.substring(0, lastpos+1);
98 }
99
100 /**
101 * The java spec guarantees that a "home" directory be
102 * specified. We look it up for our own uses at initialization
103 * If you want to do other things in the user's home dir,
104 * this routine is an easy way to find out where it is.
105 *
106 * This returns string that will have trailing fileseparator, eg "/")
107 * so you can slap together a filename directly after it, and
108 * not worry about that sort of junk.
109 */
110 final public static String getHomeDir() {
111 String homeDir = null;
112 String os = System.getProperty("os.name");
113 try {
114 if (os.startsWith("Windows")) {
115 // JRE prior to 1.5 cannot reliably determine USERPROFILE
116 // return user.home and hope it's right...
117 if (Integer.parseInt(System.getProperty("java.version").split("\\.")[1]) < 5) {
118 homeDir = System.getProperty("user.home");
119 } else {
120 homeDir = System.getenv("USERPROFILE");
121 }
122 } else {
123 homeDir = FileSystemView.getFileSystemView().
124 getDefaultDirectory().getCanonicalPath();
125 }
126 } catch (Exception e) {
127 e.printStackTrace();
128 }
129 return homeDir;
130 }
131
132 final private String getUserName() {
133 String userName = (String) System.getProperties().get("user.name");
134 return userName;
135 }
136
137 final public static String getFileSeperator() {
138 String seperator = System.getProperties().get("file.separator").toString();
139 return seperator;
140 }
141
142 /**
143 * way to directly set a preference. You'll have to
144 * do your own type conversion.
145 * @param prefname name of property
146 * @param value string value of property
147 */
148 public void setPref(String prefname, String value) {
149 setProperty(prefname, value);
150 }
151
152 public void setPref(String prefname, int value) {
153 systemprops.put(prefname, java.lang.Integer.toString(value));
154 }
155
156 public void setPref(String prefname, boolean value) {
157 put(prefname, (value ? "true" : "false"));
158 }
159
160 /**
161 * Gets named resource, as a string value.
162 * returns null if no such resource defined.
163 * @param name name of property
164 */
165 public String getString(String name) {
166 return trimValue(getProperty(name));
167 }
168 /**
169 * Gets named resource, as a string value.
170 *
171 * @param name name of property
172 * @param defval default value to remember and return , if
173 * no existing property name found.
174 */
175 public String getString(String name, String defstr) {
176 String val = trimValue(getProperty(name));
177 if(val==null) {
178 setPref(name, defstr);
179 return defstr;
180 }
181 return val;
182 }
183
184 /**
185 * look up property that is an int value
186 * @param name name of property
187 */
188 public int getInt(String name) {
189 String strint = trimValue(getProperty(name));
190 int val=0;
191 try {
192 val = Integer.parseInt(strint);
193 } catch (NumberFormatException err) {
194 //we dont care
195 }
196 return val;
197 }
198
199 /**
200 * look up property that is an int value
201 * @param name name of property
202 * @param defval default value to remember and return , if
203 * no existing property name found.
204 */
205 public int getInt(String name, int defval) {
206 String strint = trimValue(getProperty(name));
207 if(strint==null) {
208 setPref(name, String.valueOf(defval));
209 return defval;
210 }
211 int val=0;
212 try {
213 val = Integer.parseInt(strint);
214 } catch (NumberFormatException err) {
215 //we dont care
216 }
217 return val;
218 }
219
220 /**
221 * look up property that is a boolean value
222 * @param name name of property
223 * @param defval default value to remember and return , if
224 * no existing property name found.
225 */
226 public boolean getBool(String name) {
227 String strval = trimValue(getProperty(name));
228 if(strval.equals("true"))
229 return true;
230
231 return false;
232 }
233
234 /**
235 * @param name name of property
236 * @param defval default value to remember and return , if
237 * no existing property name found.
238 */
239 public boolean getBool(String name, boolean defval) {
240 String strval = trimValue(getProperty(name));
241 if(strval==null) {
242 setPref(name, String.valueOf(defval));
243 return defval;
244 }
245
246 if(strval.equals("true"))
247 return true;
248
249 return false;
250 }
251
252 /**
253 * save user preferences to default file. Duh.
254 */
255 public void Save() throws java.io.IOException {
256 store(new FileOutputStream(prefFile), appName+" preferences");
257 }
258}