001 /*
002 Copyright (C) 2003 Adam Olsen
003
004 This program is free software; you can redistribute it and/or modify
005 it under the terms of the GNU General Public License as published by
006 the Free Software Foundation; either version 1, or (at your option)
007 any later version.
008
009 This program is distributed in the hope that it will be useful,
010 but WITHOUT ANY WARRANTY; without even the implied warranty of
011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 GNU General Public License for more details.
013
014 You should have received a copy of the GNU General Public License
015 along with this program; if not, write to the Free Software
016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
017 */
018
019 package com.valhalla.settings;
020
021 import java.io.BufferedReader;
022 import java.io.File;
023 import java.io.FileInputStream;
024 import java.io.FileOutputStream;
025 import java.io.FileWriter;
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.io.InputStreamReader;
029 import java.io.PrintWriter;
030 import java.util.Properties;
031
032 import com.valhalla.jbother.JBother;
033
034 /**
035 * A singleton Properties class to access and save settings
036 *
037 * @author Adam Olsen
038 * @version 1.0
039 */
040 public class Settings extends Properties {
041 private static Settings instance;
042
043 private File settingsDir;
044
045 private File settingsFile;
046
047 private static boolean saving = false;
048
049 /**
050 * Reads the settings file and sets up the singleton
051 *
052 * @param dir
053 * the settings directory
054 * @param settingsFile
055 * the settings file
056 */
057 public static void loadSettings(String dir, String settingsFile) {
058 instance = new Settings(dir, settingsFile);
059 }
060
061 /**
062 * Gets the Settings instance
063 *
064 * @return the Settings singleton
065 */
066 public static Settings getInstance() {
067 return instance;
068 }
069
070 /**
071 * Gets the settings directory
072 *
073 * @return the settings directory
074 */
075 public File getSettingsDir() {
076 return settingsDir;
077 }
078
079 /**
080 * Gets the settings file
081 *
082 * @return the settings file
083 */
084 public File getSettingsFile() {
085 return settingsFile;
086 }
087
088 /**
089 * Private constructor
090 *
091 * @param dir
092 * the settings directory
093 * @param settingsFile
094 * the settings file
095 */
096 private Settings(String dir, String settingsFile) {
097 instance = this;
098 settingsDir = new File(dir);
099 this.settingsFile = new File(dir, settingsFile);
100 saving = true;
101
102 // make sure the settings directory and file is there
103 if (!settingsDir.isDirectory() || !this.settingsFile.isFile()
104 || JBother.kiosk_mode)
105 createDefaultSettings();
106
107 try {
108 // load the file into the properties
109 InputStream stream = new FileInputStream(this.settingsFile);
110 load(stream);
111 stream.close();
112 } catch (Exception e) {
113 com.valhalla.Logger.debug("Could not load settings file");
114 com.valhalla.Logger.debug(e.getMessage());
115 }
116 saving = false;
117 }
118
119 /**
120 * Gets a boolean value
121 *
122 * @param key
123 * the key to get a boolean for
124 * @return a boolean based on the key
125 */
126 public boolean getBoolean(String key) {
127 return (getProperty(key) != null);
128 }
129
130 /**
131 * Sets a boolean
132 *
133 * @param key
134 * the key to set
135 * @param value
136 * the value to set the key to
137 */
138 public void setBoolean(String key, boolean value) {
139 if (value)
140 setProperty(key, "true");
141 else
142 remove(key);
143 writeSettings();
144 }
145
146 /**
147 * Sets the settings
148 *
149 * @param settings
150 * the settings to set it to
151 */
152 public static void setSettings(Properties settings) {
153 if (instance != null)
154 instance = (Settings) settings;
155 writeSettings();
156 }
157
158 /**
159 * Writes the settings to the settings file
160 */
161 public static void writeSettings() {
162 if (saving)
163 return;
164 saving = true;
165 if (instance == null) {
166 com.valhalla.Logger
167 .debug("Fatal Error: Settings has not been initiated, and an attempt to access a value was made. Potential NullPointerException.");
168 System.exit(1);
169 }
170
171 try {
172 // create the output stream
173 FileOutputStream stream = new FileOutputStream(
174 instance.settingsFile);
175 StringBuffer buf = new StringBuffer();
176
177 // store the properties file
178 instance.store(stream, "JBother Settings File");
179 stream.close();
180 } catch (IOException e) {
181 com.valhalla.Logger
182 .debug("There was an error saving your settings.");
183 com.valhalla.Logger.debug(e.toString());
184 }
185 saving = false;
186 }
187
188 /**
189 * If a settings file is not found, a default settings file is copied in to
190 * place.
191 */
192 private void createDefaultSettings() {
193 saving = true;
194 if (!settingsDir.isDirectory() && !settingsDir.mkdirs()) {
195 //creating the settings directory failed....
196 com.valhalla.Logger
197 .debug("Fatal Error: Could not create the settings directory ("
198 + settingsDir.getName() + ").");
199 }
200
201 try {
202
203 // copy the default file in to place
204 InputStream stream = null;
205 if (JBother.kiosk_mode)
206 stream = instance.getClass().getClassLoader()
207 .getResourceAsStream("kiosksettings.properties");
208 else
209 stream = instance.getClass().getClassLoader()
210 .getResourceAsStream("defaultsettings.properties");
211 if (stream == null)
212 throw new IOException();
213
214 BufferedReader in = new BufferedReader(
215 new InputStreamReader(stream));
216
217 PrintWriter out = new PrintWriter(new FileWriter(settingsFile));
218 String line = null;
219
220 while ((line = in.readLine()) != null) {
221 int i = line.indexOf("%username%");
222 if (i > 0) {
223 line = line.substring(0, i)
224 + Arguments.getInstance().getProperty("kiosk_user")
225 + line.substring(i + 10);
226 }
227 i = line.indexOf("%password%");
228 if (i > 0) {
229 line = line.substring(0, i)
230 + Arguments.getInstance().getProperty("kiosk_pass")
231 + line.substring(i + 10);
232 }
233 out.println(line);
234 }
235
236 in.close();
237 out.close();
238
239 } catch (IOException ex) {
240 //creating an initial settings file failed...
241 com.valhalla.Logger
242 .debug("Fatal Error: Could not create the settings file ("
243 + settingsFile.getPath() + ")\n" + ex.getMessage());
244 System.exit(1);
245 }
246 saving = false;
247 }
248
249 public static void createKioskRoom() {
250 File bookMarksDirectory = new File(JBother.profileDir
251 + File.separatorChar + "gcbookmarks");
252 if (!bookMarksDirectory.isDirectory() && !bookMarksDirectory.mkdir()) {
253 com.valhalla.Logger
254 .debug("Could not create kiosk bookmarks directory.");
255 return;
256 }
257
258 String settingsFile = JBother.profileDir + File.separatorChar
259 + "gcbookmarks" + File.separatorChar
260 + Arguments.getInstance().getProperty("kiosk") + ".gcb";
261
262 try {
263 PrintWriter out = new PrintWriter(new FileWriter(settingsFile));
264
265 out.println(Arguments.getInstance().getProperty("kiosk"));
266 out.println(Arguments.getInstance()
267 .getProperty("kiosk_roomservice"));
268 out.println(Arguments.getInstance().getProperty("kiosk_nick"));
269 out.println(Arguments.getInstance().getProperty("kiosk_roompass"));
270 out.println("true");
271
272 out.close();
273 } catch (IOException ioe) {
274 com.valhalla.Logger
275 .debug("I/O exception writing kiosk room properties.");
276 com.valhalla.Logger.debug(ioe.getMessage());
277 // System.exit(-1);
278 }
279 }
280 }